0576268a58e1d889b92c66fcd68ed445d9a1087f
[mtweb] / mw / env / modules / mw_env_models.php
1 <?php
2
3   class mw_env_models extends mw_env{
4
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";
11         }
12         else $model_class_name = "mw_model";
13       }
14       $model = new $model_class_name($this->root_inst, $model_name, $default_values, $RETURN_INSERT_ID);
15       return $model;
16     }
17
18   }
19
20   class mw_model{
21
22     var $env;
23     var $model_name;
24     var $attributs;
25
26     var $RETURN_INSERT_ID;
27
28     function env(){
29       return $this->env;
30     }
31
32     function get_values(){
33       $values = array();
34       foreach($this->attributs as $attribut_name => $attribut){
35         $values[$attribut_name] = isset($attribut["value"]) ? $attribut["value"] : null;
36       }
37       return $values;
38     }
39
40     function mw_model(&$env, $model_name, $default_values = array(), $RETURN_INSERT_ID = false){
41       $this->env = &$env;
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);
48       }
49     }
50
51     function load($index_name, $index_value){
52       if(!isset($this->model_name)) return false;
53       $data = $this->env->data();
54       if(
55         (
56           $values = $data->data_read(
57             array(
58               "table_name" => $this->model_name,
59               "index_name" => $index_name,
60               "index_value" => $index_value
61             )
62           )
63         ) === false
64       ) return false;
65       foreach($values as $key => $value){
66         if(!isset($this->attributs[$key])) $this->attributs[$key] = array();
67         $this->attributs[$key]["value"] = $value;
68       }
69       return true;
70     }
71
72     function __call($method_name, $arguments){
73       $r = false;
74       if(substr($method_name, 0, 4) == "get_"){
75         if(
76               ($attribut_name = substr($method_name, 4))
77         ){
78           if(isset($this->attributs[$attribut_name]["value"])){
79             $r = $this->attributs[$attribut_name]["value"];
80           }
81           else $r = null;
82         }
83       }
84       elseif(substr($method_name, 0, 4) == "set_"){
85         if(
86               ($attribut_name = substr($method_name, 4))
87           &&  isset($arguments[0])
88         ){
89           if(!isset($this->attributs[$attribut_name])) $this->attributs[$attribut_name] = array();
90           $this->attributs[$attribut_name]["value"] = $arguments[0];
91           $r = true;
92         }
93       }
94       elseif(substr($method_name, 0, 6) == "valid_"){
95         if(
96               ($attribut_name = substr($method_name, 6))
97           &&  isset($arguments[0])
98         ){
99           $r = true;
100         }
101       }
102       return $r;
103     }
104
105     function insert(){
106       if(!isset($this->model_name)) return false;
107       $data = $this->env->data();
108       return $data->data_insert(
109         array(
110           "table_name" => $this->model_name,
111           "values" => $this->get_values(),
112           "return_insert_id" => $this->RETURN_INSERT_ID
113         )
114       );
115     }
116
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();
123       }
124       if(!isset($index_value) || !$index_value) return false;
125       return $data->data_update(
126         array(
127           "table_name" => $this->model_name,
128           "index_name" => $index_name,
129           "index_value" => $index_value,
130           "values" => $this->get_values()
131         )
132       );
133     }
134
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();
141       }
142       if(!isset($index_value) || !$index_value) return false;
143       return $data->data_delete(
144         array(
145           "table_name" => $this->model_name,
146           "index_name" => $index_name,
147           "index_value" => $index_value
148         )
149       );
150     }
151
152     function valid(){
153       $res = array(
154         "VALID" => true,
155         "messages" => array()
156       );
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;
163         }
164       }
165       return $res;
166     }
167
168   }
169
170 ?>