| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 76.92% covered (warning) | 76.92% | 10 / 13 | CRAP | 94.74% covered (success) | 94.74% | 144 / 152
ModelBase | 0.00% covered (danger) | 0.00% | 0 / 1 | 78.57% covered (warning) | 78.57% | 11 / 14 | 59.51 | 94.74% covered (success) | 94.74% | 144 / 152
install | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |  |  |
__construct | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 5 / 5
init | 0.00% covered (danger) | 0.00% | 0 / 1 | 5.93 | 66.67% covered (warning) | 66.67% | 4 / 6
getDbPrefix | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 1 / 1
getTableName | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 2 / 2
setup | 100.00% covered (success) | 100.00% | 1 / 1 | 4 | 100.00% covered (success) | 100.00% | 15 / 15
getMeta | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 | 0.00% covered (danger) | 0.00% | 0 / 2
__get | 100.00% covered (success) | 100.00% | 1 / 1 | 5 | 100.00% covered (success) | 100.00% | 7 / 7
__set | 100.00% covered (success) | 100.00% | 1 / 1 | 5 | 100.00% covered (success) | 100.00% | 9 / 9
save | 100.00% covered (success) | 100.00% | 1 / 1 | 9 | 100.00% covered (success) | 100.00% | 35 / 35
delete | 100.00% covered (success) | 100.00% | 1 / 1 | 5 | 100.00% covered (success) | 100.00% | 13 / 13
selects | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.12 | 90.48% covered (success) | 90.48% | 38 / 42
selectById | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 6 / 6
populate | 100.00% covered (success) | 100.00% | 1 / 1 | 4 | 100.00% covered (success) | 100.00% | 9 / 9

1 | <?php
2 |
3 | namespace Entity;
4 |
5 | abstract class ModelBase
6 | {
7 | private $fieldsValues = array();
8 | protected static $dbo = null;
9 | private $changed = array();
10 | private static $config = null;
11 | private $id;
12 |
13 | protected abstract function install();
14 |
15 | public function __construct($id = null)
16 | {
17 | self::init();
18 | $this->id = null;
19 | if ($id !== null && is_numeric($id))
20 | $this->selectById($id);
21 | }
22 |
23 | public static function init($config = null)
24 | {
25 | if (self::$dbo !== null)
26 | return true;
27 | // @codeCoverageIgnoreStart
28 | // Load config from written file; this file is created from setup script
29 | if ($config === null && self::$config === null)
30 | {
31 | self::$config = @include("core/config.inc.php");
32 | if (empty(self::$config))
33 | return false;
34 | }
35 | // @codeCoverageIgnoreEnd
36 | else
37 | self::$config = $config;
38 | self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
39 | return true;
40 | }
41 |
42 | public static function getDbPrefix()
43 | {
44 | return self::$config[3];
45 | }
46 |
47 | public function getTableName()
48 | {
49 | $className = new \ReflectionClass($this);
50 | return $this->getDbPrefix().strtolower($className->getShortName());
51 | }
52 |
53 | public static function setup()
54 | {
55 | $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module", "ModuleHook");
56 |
57 | self::init();
58 | self::$dbo->beginTransaction();
59 | try
60 | {
61 | foreach ($tables as $i)
62 | {
63 | $i = "Entity\\".$i;
64 | $table = new $i();
65 | if ($table->install() != true)
66 | throw new \Exception("{$i}: Cannot table setup failure");
67 | }
68 | }
69 | catch (\Exception $e)
70 | {
71 | self::$dbo->rollBack();
72 | error_log($e->getMessage());
73 | return false;
74 | }
75 | self::$dbo->commit();
76 | return true;
77 | }
78 |
79 | public function getMeta($lang=null)
80 | {
81 | $fetcher = new \Entity\Meta();
82 | return $fetcher->query(array("type" => get_class($this)));
83 | }
84 |
85 | public function __get($key)
86 | {
87 | if ($key == "meta")
88 | return $this->getMeta();
89 | if ($key == "id")
90 | return $this->id === null ? null : (int) $this->id;
91 | if (!isset($this->fieldsValues[$key]))
92 | return null;
93 | return $this->fieldsValues[$key];
94 | }
95 |
96 | public function __set($key, $value)
97 | {
98 | if ($value instanceof \DateTime)
99 | $value = $value->format("Y-m-d");
100 | $this->fieldsValues[$key] = $value;
101 | if (is_bool($value))
102 | $this->changed[$key] = $value ? 1 : 0;
103 | else if ($value === null)
104 | $this->changed[$key] = "NULL";
105 | else
106 | $this->changed[$key] = self::$dbo->quote($value);
107 | return $value;
108 | }
109 |
110 | public function save()
111 | {
112 | \Tools\Hooks::trigger("onBeforeEntitySave");
113 | \Tools\Hooks::trigger("onBeforeEntitySave".$this->getTableName());
114 | if ($this->id === null)
115 | {
116 | if (empty ($this->changed))
117 | {
118 | $query = "INSERT INTO `{$this->getTableName()}` () VALUES ()";
119 | $result = self::$dbo->exec($query);
120 | if (!$result)
121 | throw new \Exception(self::$dbo->errorInfo()[2]);
122 | }
123 | else
124 | {
125 | $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
126 | $result = self::$dbo->exec($query);
127 | if (!$result)
128 | throw new \Exception(self::$dbo->errorInfo()[2]);
129 | $this->changed = array();
130 | }
131 | $this->id = self::$dbo->lastInsertId();
132 | }
133 | else
134 | {
135 | if (!empty($this->changed))
136 | {
137 | if ($this->id === false)
138 | throw new \Exception("Cannot update private row");
139 | $query = "UPDATE {$this->getTableName()} SET ";
140 | $newValues = array();
141 | foreach ($this->changed as $i => $j)
142 | $newValues[] = "`{$i}`=" . $j;
143 | $query .= implode(",",$newValues)." WHERE id={$this->id}";
144 | $result = self::$dbo->exec($query);
145 | if (!$result)
146 | throw new \Exception(self::$dbo->errorInfo()[2]);
147 | $this->changed = array();
148 | }
149 | }
150 | \Tools\Hooks::trigger("onAfterEntitySave");
151 | \Tools\Hooks::trigger("onAfterEntitySave".$this->getTableName());
152 | }
153 |
154 | /**
155 | * Remove entity from database
156 | * $entity->delete() then $entity->save() should save entity with a new id and the same data
157 | * @return Boolean true on success
158 | **/
159 | public function delete()
160 | {
161 | if ($this->id === false)
162 | return true;
163 | \Tools\Hooks::trigger("onBeforeEntityDelete");
164 | \Tools\Hooks::trigger("onBeforeEntityDelete".$this->getTableName());
165 | $id = (int) $this->id;
166 | foreach ($this->fieldsValues as $i => $j)
167 | if (!isset($this->changed[$i]))
168 | $this->changed[$i] = $j;
169 | if (self::$dbo->exec("DELETE FROM {$this->getTableName()} WHERE `id`={$id}") === false)
170 | return false;
171 | \Tools\Hooks::trigger("onAfterEntityDelete");
172 | \Tools\Hooks::trigger("onAfterEntityDelete".$this->getTableName());
173 | return true;
174 | }
175 |
176 | public function selects($criteria = null, $orderBy = null)
177 | {
178 | $query = "SELECT * FROM {$this->getTableName()}";
179 |
180 | if (!empty($criteria))
181 | {
182 | $subQuery = array();
183 | foreach ($criteria as $i => $j)
184 | {
185 | if ($j == null)
186 | $subQuery[] = "`{$i}` IS NULL";
187 | else if (is_array($j))
188 | {
189 | $inArray = [];
190 | foreach ($j as $k)
191 | $inArray[] = self::$dbo->quote($k);
192 | $subQuery[] = "`{$i}` IN (".implode(",", $inArray).")";
193 | }
194 | else
195 | $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
196 | }
197 | $query .= " WHERE ".implode(" AND ", $subQuery);
198 | }
199 | if (!empty($orderBy))
200 | {
201 | $_orderBy = array();
202 | foreach ($orderBy as $i => $j)
203 | {
204 | if (is_numeric($i))
205 | $_orderBy[] = "`{$j}` ASC";
206 | else
207 | {
208 | $orderType = "ASC";
209 | if (strtoupper($j == "DESC"))
210 | $orderType = "DESC";
211 | $_orderBy[] = "`{$i}` {$orderType}";
212 | }
213 | }
214 | $query .= " ORDER BY ".implode(",", $_orderBy);
215 | }
216 | $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
217 | if ($result === false)
218 | throw new \Exception(self::$dbo->errorInfo()[2]);
219 | $resultObj = array();
220 | $className = get_class($this);
221 | foreach ($result as $i)
222 | {
223 | $iObj = new $className();
224 | $iObj->populate($i);
225 | $resultObj[] = $iObj;
226 | }
227 | return $resultObj;
228 | }
229 |
230 | public function selectById($id)
231 | {
232 | $query = "SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id." LIMIT 1";
233 | $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
234 | if ($result === false || empty($result))
235 | throw new \Exception("Cannot fetch data: ".self::$dbo->errorInfo()[2]);
236 | $this->populate($result->fetch());
237 | }
238 |
239 | private function populate($data)
240 | {
241 | $this->id = FALSE;
242 | if ($data === false)
243 | throw new \Exception("Cannot load entity: no result found");
244 | foreach ($data as $i => $j)
245 | $this->fieldsValues[$i] = $j;
246 | if (isset($this->fieldsValues["id"]))
247 | $this->id = (int) $this->fieldsValues["id"];
248 | $this->changed = array();
249 | }
250 | }
251 |