ModelBase.php 6.5 KB

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