1 <?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 interface iMagratheaModel {
22 public function __construct($id);
23
24 public function Save();
25 public function Insert();
26 public function Update();
27 public function GetID();
28 }
29
30 abstract class MagratheaModel{
31 protected $dbTable;
32 protected $autoLoad = null;
33 protected $dbValues = array();
34 protected $dbAlias = array();
35 protected $relations = array();
36 protected $dbPk;
37
38 39 40 41
42 public function GetDbTable(){ return $this->dbTable; }
43 44 45 46
47 public function GetDbValues(){ return $this->dbValues; }
48 49 50 51
52 public function GetProperties(){
53 $properties = $this->dbValues;
54 $properties["created_at"] = "datetime";
55 $properties["updated_at"] = "datetime";
56 return $properties;
57 }
58
59 60 61 62
63 public function GetFieldsForSelect(){
64 $fields = $this->GetProperties();
65 array_walk($fields, 'MagratheaQuery::BuildSelect', $this->dbTable);
66 return implode(', ', $fields);
67 }
68
69 70 71 72
73 public function GetPkName(){
74 return $this->dbPk;
75 }
76 77 78 79
80 public function GetID(){
81 $pk = $this->dbPk;
82 return $this->$pk;
83 }
84 85 86 87
88 public function GetAutoLoad(){
89 return $this->autoLoad;
90 }
91
92 93 94 95
96 public function LoadObjectFromTableRow($row){
97 if(!is_array($row)) return;
98 foreach($row as $field => $value){
99 $field = strtolower($field);
100 if( property_exists($this, $field))
101 $this->$field = $value;
102 }
103 }
104
105 106 107 108 109 110 111 112
113 public function GetById($id){
114 if( empty($id) ) return null;
115 if( $this->autoload && count($this->autoload) > 0 ) {
116 $sql = MagratheaQuery::Select()->Table($this->dbTable)->SelectObj($this)->Where($this->dbTable.".".$this->dbPk." = ".$id);
117 $tabs = array();
118 foreach ($this->autoload as $objName => $relation) {
119 $obj = new $objName();
120 $sql->InnerObject($obj, $obj->dbTable.".".$obj->GetPkName()." = ".$this->dbTable.".".$relation);
121 $tabs[$objName] = $obj->dbTable;
122 }
123 $result = MagratheaDatabase::Instance()->queryRow($sql->SQL());
124 if( empty($result) ) throw new MagratheaModelException("Could not find ".get_class($this)." with id ".$id."!");
125
126 $splitResult = MagratheaQuery::SplitArrayResult($result);
127 $this->LoadObjectFromTableRow($splitResult[$this->GetDbTable()]);
128 foreach ($tabs as $obj => $table) {
129 $new_object = new $obj();
130 $new_object->LoadObjectFromTableRow($splitResult[$new_object->GetDbTable()]);
131 $this->$obj = $new_object;
132 }
133 } else {
134 $sql = "SELECT * FROM ".$this->dbTable." WHERE ".$this->dbPk." = ".$id;
135 $result = MagratheaDatabase::Instance()->queryRow($sql);
136 if( empty($result) ) throw new MagratheaModelException("Could not find ".get_class($this)." with id ".$id."!");
137 $this->LoadObjectFromTableRow($result);
138 }
139 }
140
141 142 143 144
145 public function GetNextID(){
146 $sql = "SHOW TABLE STATUS LIKE '".$this->dbTable."'";
147 $data = MagratheaDatabase::Instance()->QueryRow($sql);
148 return $data['auto_increment'];
149 }
150
151 152 153 154 155
156 public function Save(){
157 $pk = $this->dbPk;
158 if( empty ($this->$pk ) )
159 return $this->Insert();
160 else
161 return $this->Update();
162 }
163 164 165 166 167
168 public function Insert(){
169 $arr_Types = array();
170 $arr_Fields = array();
171 $arr_Values = array();
172 foreach( $this->dbValues as $field => $type ){
173 if( $field == $this->dbPk ){
174 if(empty($this->$field)) continue;
175 }
176 array_push($arr_Types, $this->GetDataTypeFromField($type));
177 array_push($arr_Fields, $field);
178 $arr_Values[$field] = $this->$field;
179 }
180
181
182 $query_run = "INSERT INTO ".$this->dbTable." (".implode(",", $arr_Fields).") VALUES (".implode(", ", array_fill(0, count($arr_Fields), "?")).") ";
183 $lastId = MagratheaDatabase::Instance()->PrepareAndExecute($query_run, $arr_Types, $arr_Values);
184 $pk = $this->dbPk;
185 $this->$pk = $lastId;
186 return $lastId;
187 }
188 189 190 191
192 public function Update(){
193 $arr_Types = array();
194 $arr_Fields = array();
195 $arr_Values = array();
196 $pkField = $this->dbPk;
197 foreach( $this->dbValues as $field => $type ){
198 if( $field == $pkField ) continue;
199 $arr_Values[$field] = $this->$field;
200 array_push($arr_Types, $this->GetDataTypeFromField($type));
201 array_push($arr_Fields, $field."= ? ");
202 }
203 $query_run = "UPDATE ".$this->dbTable." SET ".implode(",", $arr_Fields)." WHERE ".$this->dbPk."= ? ";
204
205 $arr_Values[$pkField] = $this->$pkField;
206 $arr_Types[$pkField] = $this->GetDataTypeFromField($pkField);
207 return MagratheaDatabase::Instance()->PrepareAndExecute($query_run, $arr_Types, $arr_Values);
208 }
209 210 211 212
213 public function Delete(){
214 $pkField = $this->dbPk;
215 $arr_Types[$pkField] = $this->GetDataTypeFromField($this->dbValues[$pkField]);
216 $arr_Values[$pkField] = $this->$pkField;
217
218
219 $query_run = "DELETE FROM ".$this->dbTable." WHERE ".$this->dbPk."= ? ";
220 return MagratheaDatabase::Instance()->PrepareAndExecute($query_run, $arr_Types, $arr_Values);
221 }
222
223 224 225 226 227 228
229 public function __get($key){
230 if( array_key_exists($key, $this->dbAlias) ){
231 $real_key = $this->dbAlias[$key];
232 return $this->$real_key;
233 } else if( @is_array($this->relations["properties"]) && array_key_exists($key, $this->relations["properties"]) ){
234 if( is_null($this->relations["properties"][$key]) ){
235 if( $this->relations["lazyload"][$key] ){
236 $loadFunction = $this->relations["methods"][$key];
237 $this->relations["properties"][$key] = $this->$loadFunction();
238 }
239 }
240 return $this->relations["properties"][$key];
241 } else {
242 throw new MagratheaModelException("Property ".$key." does not exists in ".get_class($this)."!");
243 }
244 }
245
246 247 248 249 250 251 252
253 public function __set($key, $value){
254 if( $key == "created_at" || $key == "updated_at" ) return false;
255 if( array_key_exists($key, $this->dbAlias) ){
256 $real_key = $this->dbAlias[$key];
257 $this->$real_key = $value;
258 } else if( @is_array($this->relations["properties"]) && array_key_exists($key, $this->relations["properties"]) ){
259 $method_set = $this->relations["methods"][$key];
260 $this->relations["properties"][$key] = $value;
261 } else {
262 throw new MagratheaModelException("Property ".$key." does not exists in ".get_class($this)."!");
263 }
264 }
265
266 267 268 269
270 public static function GetDataTypeFromField($field){
271 switch($field){
272 case "text":
273 case "string":
274 return "text";
275 case "boolean":
276 case "int":
277 case "integer":
278 return "integer";
279 case "double":
280 case "float":
281 return "decimal";
282 case "datetime":
283 return "date";
284 }
285 }
286
287 288 289
290 public static function IncludeAllModels(){
291 $modelsFolder = MagratheaConfig::Instance()->GetConfigFromDefault("site_path")."/Models";
292 if($handle = @opendir($modelsFolder)){
293 while (false !== ($file = readdir($handle))) {
294 $filename = explode('.', $file);
295 $ext = array_pop($filename);
296 if(empty($ext)) continue;
297 if($ext == "php"){
298 include_once($modelsFolder."/".$file);
299 }
300 }
301 closedir($handle);
302 }
303 }
304
305 306 307 308
309 public function __toString(){
310 $print_this = "Class ".get_class($this).":\n";
311 $print_this .= count($this->dbValues > 0 ) ? "\tProperties\n" : "";
312 foreach( $this->dbValues as $field => $type ){
313 $print_this .= "\t\t[".$field."] (".$type.") = ".$this->$field."\n";
314 }
315 $print_this .= count($this->dbAlias>0) ? "\tAlias\n" : "";
316 foreach( $this->dbAlias as $alias => $field ){
317 $print_this .= "\t\t[".$alias."] (alias for ".$field.") = ".$this->$field."\n";
318 }
319 return "<pre>".$print_this."</pre>";
320 }
321
322 }
323