1 <?php
2
3 abstract class MagratheaModelControl{
4 protected static $modelName;
5 protected static $dbTable;
6
7 8 9 10 11
12 public static function RunQuery($sql){
13 $magdb = MagratheaDatabase::Instance();
14 $objects = array();
15 $result = $magdb->queryAll($sql);
16 foreach($result as $item){
17 $splitResult = MagratheaQuery::SplitArrayResult($item);
18 $new_object = new static::$modelName();
19 if(count($splitResult) > 0)
20 $item = $splitResult[$new_object->GetDbTable()];
21 $new_object->LoadObjectFromTableRow($item);
22 array_push($objects, $new_object);
23 }
24 return $objects;
25 }
26 27 28 29 30
31 public static function RunRow($sql){
32 $magdb = MagratheaDatabase::Instance();
33 $row = $magdb->QueryRow($sql);
34 $new_object = null;
35 if(!empty($row)){
36 $splitResult = MagratheaQuery::SplitArrayResult($row);
37 $new_object = new static::$modelName();
38 if(count($splitResult) > 0)
39 $row = $splitResult[$new_object->GetDbTable()];
40 $new_object->LoadObjectFromTableRow($row);
41 }
42 return $new_object;
43 }
44 45 46 47 48
49 public static function QueryResult($sql){
50 return MagratheaDatabase::Instance()->queryAll($sql);
51 }
52 53 54 55 56
57 public static function QueryOne($sql){
58 return MagratheaDatabase::Instance()->queryOne($sql);
59 }
60
61 62 63 64 65 66
67 public static function RunMagQuery($magQuery){
68 return self::Run($magQuery);
69 }
70
71 72 73 74 75 76 77 78 79
80 public static function RunPagination($magQuery, &$total, $page=0, $limit=20){
81 $total = self::QueryOne($magQuery->Count());
82 $magQuery->Limit($limit)->Page($page);
83 return self::Run($magQuery);
84 }
85 86 87 88 89 90
91 public static function Run($magQuery, $onlyFirst=false){
92 $array_joins = $magQuery->GetJoins();
93 $arrayObjs = array();
94
95 if(count($array_joins) > 0){
96 $objects = array();
97 $result = static::QueryResult($magQuery->SQL());
98 foreach ($result as $r) {
99 $splitResult = MagratheaQuery::SplitArrayResult($r);
100 $new_object = new static::$modelName();
101 if(count($splitResult) > 0)
102 $r = $splitResult[$new_object->GetDbTable()];
103 $new_object->LoadObjectFromTableRow($r);
104 foreach($array_joins as $join){
105 $obj = $join["obj"];
106 $obj->LoadObjectFromTableRow($splitResult[$obj->GetDbTable()]);
107 $objname = get_class($obj);
108 if($join["type"] == "has_many"){
109 $objnameField = $objname."s";
110 if( empty($arrayObjs[$new_object->GetID()]) )
111 $arrayObjs[$new_object->GetID()] = array();
112 if( empty($arrayObjs[$new_object->GetID()][$objnameField]) )
113 $arrayObjs[$new_object->GetID()][$objnameField] = array();
114 $objIndex = count($arrayObjs[$new_object->GetID()][$objnameField]);
115 $arrayObjs[$new_object->GetID()][$objnameField][$objIndex] = clone $obj;
116 $new_object->$objnameField = $arrayObjs[$new_object->GetID()][$objnameField];
117 } else {
118 $new_object->$objname = clone $obj;
119 }
120 unset($obj);
121 }
122 array_push($objects, clone $new_object);
123 }
124 if($onlyFirst){
125 if(count($objects) > 0) return $objects[0];
126 } else return $objects;
127 } else {
128 return $onlyFirst ? self::RunRow($magQuery->SQL()) : self::RunQuery($magQuery->SQL());
129 }
130 }
131
132 133 134 135
136 public static function GetAll(){
137 $sql = "SELECT * FROM ".static::$dbTable." ORDER BY created_at DESC";
138 return static::RunQuery($sql);
139 }
140
141 142 143 144 145
146 public static function GetSimpleWhere($whereSql){
147 $sql = "SELECT * FROM ".static::$dbTable." WHERE ".$whereSql;
148 return static::RunQuery($sql);
149 }
150
151 152 153 154 155 156
157 public static function GetWhere($arr, $condition = "AND"){
158 if(!is_array($arr)){
159 return static::GetSimpleWhere($arr);
160 }
161 $whereSql = MAgratheaQuery::BuildWhere($arr, $condition);
162 $sql = "SELECT * FROM ".static::$dbTable." WHERE ".$whereSql." ORDER BY created_at DESC";
163 return static::RunQuery($sql);
164 }
165
166 167 168 169 170 171
172 public static function GetRowWhere($arr, $condition = "AND"){
173 if(!is_array($arr)){
174 return static::GetSimpleWhere($arr);
175 }
176 $whereSql = MagratheaQuery::BuildWhere($arr, $condition);
177 $sql = "SELECT * FROM ".static::$dbTable." WHERE ".$whereSql;
178 return static::RunRow($sql);
179 }
180
181 182 183 184 185 186 187
188 public static function GetMultipleObjects($array_objects, $joinGlue, $where=""){
189 $magQuery = new MagratheaQuery();
190 $magQuery->Table(static::$dbTable)->SelectArrObj($array_objects)->Join($joinGlue)->Where($where);
191
192
193 $objects = array();
194 $result = MagratheaDatabase::Instance()->queryAll($magQuery->SQL());
195
196 foreach($result as $item){
197
198 $splitResult = MagratheaQuery::SplitArrayResult($item);
199 $itemArr = array();
200 foreach ($array_objects as $key => $value) {
201 $new_object = new $value();
202 $new_object->LoadObjectFromTableRow($splitResult[$new_object->GetDbTable()]);
203 $itemArr[$key] = $new_object;
204 }
205 array_push($objects, $itemArr);
206 }
207 return $objects;
208 }
209
210 211 212
213 public static function ShowAll(){
214 $baseObj = new static::$modelName();
215 $all = static::GetAll();
216 $properties = $baseObj->GetProperties();
217 echo "<table class='magratheaShowAll'>";
218 echo "<tr>";
219 foreach ($properties as $key => $value) {
220 echo "<th>".$key." - (".$value.")</th>";
221 }
222 echo "</tr>";
223 foreach ($all as $i) {
224 echo "<tr>";
225 foreach ($properties as $key => $value) {
226 echo "<td>".$i->$key."</td>";
227 }
228 echo "</tr>";
229 }
230 echo "</tr>";
231 echo "</table>";
232 }
233
234 }
235
236 ?>