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