3 class mw_env_models extends mw_env{
5 function get_model($model_name, $default_values = array(), $RETURN_INSERT_ID = false){
6 $model_class_name = "mw_model_".$model_name;
7 if(!class_exists($model_class_name)){
8 if($this->app_file_exists("data/models/".$model_name.".php")){
9 require_once $this->app_file("data/models/".$model_name.".php");
10 if(!class_exists($model_class_name)) $model_class_name = "mw_model";
12 else $model_class_name = "mw_model";
14 $model = new $model_class_name($this->root_inst, $model_name, $default_values, $RETURN_INSERT_ID);
26 var $RETURN_INSERT_ID;
32 function get_values(){
34 foreach($this->attributs as $attribut_name => $attribut){
35 $values[$attribut_name] = isset($attribut["value"]) ? $attribut["value"] : null;
40 function mw_model(&$env, $model_name, $default_values = array(), $RETURN_INSERT_ID = false){
42 $this->model_name = $model_name;
43 $this->attributs = array();
44 $this->RETURN_INSERT_ID = $RETURN_INSERT_ID;
45 foreach($default_values as $attribut_name => $attribut_value){
46 $set_function = "set_".$attribut_name;
47 $this->$set_function($attribut_value);
51 function load($index_name, $index_value){
52 if(!isset($this->model_name)) return false;
53 $data = $this->env->data();
56 $values = $data->data_read(
58 "table_name" => $this->model_name,
59 "index_name" => $index_name,
60 "index_value" => $index_value
65 foreach($values as $key => $value){
66 if(!isset($this->attributs[$key])) $this->attributs[$key] = array();
67 $this->attributs[$key]["value"] = $value;
72 function __call($method_name, $arguments){
74 if(substr($method_name, 0, 4) == "get_"){
76 ($attribut_name = substr($method_name, 4))
78 if(isset($this->attributs[$attribut_name]["value"])){
79 $r = $this->attributs[$attribut_name]["value"];
84 elseif(substr($method_name, 0, 4) == "set_"){
86 ($attribut_name = substr($method_name, 4))
87 && isset($arguments[0])
89 if(!isset($this->attributs[$attribut_name])) $this->attributs[$attribut_name] = array();
90 $this->attributs[$attribut_name]["value"] = $arguments[0];
94 elseif(substr($method_name, 0, 6) == "valid_"){
96 ($attribut_name = substr($method_name, 6))
97 && isset($arguments[0])
106 if(!isset($this->model_name)) return false;
107 $data = $this->env->data();
108 return $data->data_insert(
110 "table_name" => $this->model_name,
111 "values" => $this->get_values(),
112 "return_insert_id" => $this->RETURN_INSERT_ID
117 function update($index_name, $index_value = null){
118 if(!isset($this->model_name)) return false;
119 $data = $this->env->data();
120 if(!isset($index_value)){
121 $get_function = "get_".$index_name;
122 $index_value = $this->$get_function();
124 if(!isset($index_value) || !$index_value) return false;
125 return $data->data_update(
127 "table_name" => $this->model_name,
128 "index_name" => $index_name,
129 "index_value" => $index_value,
130 "values" => $this->get_values()
135 function delete($index_name, $index_value = null){
136 if(!isset($this->model_name)) return false;
137 $data = $this->env->data();
138 if(!isset($index_value)){
139 $get_function = "get_".$index_name;
140 $index_value = $this->$get_function();
142 if(!isset($index_value) || !$index_value) return false;
143 return $data->data_delete(
145 "table_name" => $this->model_name,
146 "index_name" => $index_name,
147 "index_value" => $index_value
155 "messages" => array()
157 foreach($this->attributs as $attribut_name => $attribut){
158 $get_function = "get_".$attribut_name;
159 $valid_function = "valid_".$attribut_name;
160 if(($r = $this->$valid_function($this->$get_function())) !== true){
161 $res["VALID"] = false;
162 $res["messages"][] = $r;