ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables Pages
ModelBase.php
Go to the documentation of this file.
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()
24  {
25  if (self::$dbo !== null)
26  return true;
27  self::$config = @include("core/config.inc.php");
28  if (empty(self::$config))
29  return false;
30  self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
31  return true;
32  }
33 
34  public static function getDbPrefix()
35  {
36  return self::$config[3];
37  }
38 
39  public function getTableName()
40  {
41  $className = new \ReflectionClass($this);
42  return $this->getDbPrefix().strtolower($className->getShortName());
43  }
44 
45  public static function setup()
46  {
47  $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module", "ModuleHook");
48 
49  self::init();
50  self::$dbo->beginTransaction();
51  try
52  {
53  foreach ($tables as $i)
54  {
55  $i = "Entity\\".$i;
56  $table = new $i();
57  if ($table->install() != true)
58  throw new \Exception();
59  }
60  }
61  catch (\Exception $e)
62  {
63  echo $e->getMessage();
64  self::$dbo->rollBack();
65  return false;
66  }
67  self::$dbo->commit();
68  return true;
69  }
70 
71  public function getMeta($lang=null)
72  {
73  $fetcher = new \Entity\Meta();
74  return $fetcher->query(array("type" => get_class()));
75  }
76 
77  public function __get($key)
78  {
79  if ($key == "meta")
80  return $this->getMeta();
81  if ($key == "id")
82  return $this->id;
83  if (!isset($this->fieldsValues[$key]))
84  return null;
85  return $this->fieldsValues[$key];
86  }
87 
88  public function __set($key, $value)
89  {
90  if ($value instanceof \DateTime)
91  $value = $value->format("Y-m-d");
92  $this->fieldsValues[$key] = $value;
93  if (is_bool($value))
94  $this->changed[$key] = $value ? 1 : 0;
95  else
96  $this->changed[$key] = self::$dbo->quote($value);
97  return $value;
98  }
99 
100  public function save()
101  {
102  \Tools\Hooks::trigger("onBeforeEntitySave");
103  \Tools\Hooks::trigger("onBeforeEntitySave".$this->getTableName());
104  if ($this->id === null)
105  {
106  if (empty ($this->changed))
107  {
108  $query = "INSERT INTO `{$this->getTableName()}` () VALUES ()";
109  $result = self::$dbo->exec($query);
110  if (!$result)
111  throw new \Exception(self::$dbo->errorInfo()[2]);
112  }
113  else
114  {
115  $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
116  $result = self::$dbo->exec($query);
117  if (!$result)
118  throw new \Exception(self::$dbo->errorInfo()[2]);
119  $this->changed = array();
120  }
121  $this->id = self::$dbo->lastInsertId();
122  }
123  else
124  {
125  if (!empty($this->changed))
126  {
127  if ($this->id === false)
128  throw new \Exception("Cannot update private row");
129  $query = "UPDATE {$this->getTableName()} SET ";
130  $newValues = array();
131  foreach ($this->changed as $i => $j)
132  $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
133  $query .= implode(",",$newValues)." WHERE id={$this->id}";
134  $result = self::$dbo->exec($query);
135  if (!$result)
136  throw new \Exception(self::$dbo->errorInfo()[2]);
137  $this->changed = array();
138  }
139  }
140  \Tools\Hooks::trigger("onAfterEntitySave");
141  \Tools\Hooks::trigger("onAfterEntitySave".$this->getTableName());
142  }
143 
144  public function selects($criteria = null, $orderBy = null)
145  {
146  $query = "SELECT * FROM {$this->getTableName()}";
147 
148  if (!empty($criteria))
149  {
150  $subQuery = array();
151  foreach ($criteria as $i => $j)
152  {
153  if ($j == null)
154  $subQuery[] = "`{$i}` IS NULL";
155  else if (is_array($j))
156  {
157  $inArray = [];
158  foreach ($j as $k)
159  $inArray[] = self::$dbo->quote($k);
160  $subQuery[] = "`{$i}` IN (".implode(",", $inArray).")";
161  }
162  else
163  $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
164  }
165  $query .= " WHERE ".implode(" AND ", $subQuery);
166  }
167  if (!empty($orderBy))
168  {
169  $_orderBy = array();
170  foreach ($orderBy as $i => $j)
171  {
172  if (is_numeric($i))
173  $_orderBy[] = "`{$j}` ASC";
174  else
175  {
176  $orderType = "ASC";
177  if (strtoupper($j == "DESC"))
178  $orderType = "DESC";
179  $_orderBy[] = "`{$i}` {$orderType}";
180  }
181  }
182  $query .= " ORDER BY ".implode(",", $_orderBy);
183  }
184  $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
185  if ($result === false)
186  throw new \Exception(self::$dbo->errorInfo()[2]);
187  $resultObj = array();
188  $className = get_class($this);
189  foreach ($result as $i)
190  {
191  $iObj = new $className();
192  $iObj->populate($i);
193  $resultObj[] = $iObj;
194  }
195  return $resultObj;
196  }
197 
198  public function selectById($id)
199  {
200  $query = "SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id." LIMIT 1";
201  $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
202  if ($result === false)
203  throw new \Exception("Cannot fetch data: ".self::$dbo->errorInfo()[2]);
204  $this->populate($result);
205  }
206 
207  private function populate($data)
208  {
209  $this->id = FALSE;
210  foreach ($data as $i => $j)
211  $this->fieldsValues[$i] = $j;
212  if (isset($data["id"]))
213  $this->id = (int) $data["id"];
214  }
215 }
216 
static init()
Definition: ModelBase.php:23
selects($criteria=null, $orderBy=null)
Definition: ModelBase.php:144
getMeta($lang=null)
Definition: ModelBase.php:71
__set($key, $value)
Definition: ModelBase.php:88
static getDbPrefix()
Definition: ModelBase.php:34
__construct($id=null)
Definition: ModelBase.php:15
static setup()
Definition: ModelBase.php:45
trigger($hookName, $params=null)
Definition: Hooks.php:62