ModelBase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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)
  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 (!isset($this->fieldsValues[$key]))
  71. return null;
  72. return $this->fieldsValues[$key];
  73. }
  74. public function __set($key, $value)
  75. {
  76. if ($value instanceof \DateTime)
  77. $value = $value->format("Y-m-d");
  78. $this->fieldsValues[$key] = $value;
  79. if (is_bool($value))
  80. $this->changed[$key] = $value ? 1 : 0;
  81. else
  82. $this->changed[$key] = self::$dbo->quote($value);
  83. return $value;
  84. }
  85. public function save()
  86. {
  87. \Tools\Hook::trigger("onBeforeEntitySave");
  88. \Tools\Hook::trigger("onBeforeEntitySave".$this->getTableName());
  89. //TODO send pre-hook
  90. if ($this->id === null)
  91. {
  92. if (empty ($this->changed))
  93. {
  94. //TODO
  95. return true;
  96. }
  97. else
  98. {
  99. $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
  100. $result = self::$dbo->exec($query);
  101. if (!$result)
  102. throw new \Exception(self::$dbo->errorInfo()[2]);
  103. $this->changed = array();
  104. }
  105. //TODO get last id
  106. }
  107. else
  108. {
  109. if (!empty($this->changed))
  110. {
  111. if ($this->id === false)
  112. throw new \Exception("Cannot update private row");
  113. $query = "UPDATE {$this->getTableName()} SET ";
  114. $newValues = array();
  115. foreach ($this->changed as $i => $j)
  116. $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
  117. $query .= implode(",",$newValues)." WHERE id={$this->id}";
  118. $result = self::$dbo->exec($query);
  119. if (!$result)
  120. throw new \Exception(self::$dbo->errorInfo()[2]);
  121. $this->changed = array();
  122. }
  123. }
  124. //TODO send hook
  125. \Tools\Hook::trigger("onAfterEntitySave");
  126. \Tools\Hook::trigger("onAfterEntitySave".$this->getTableName());
  127. }
  128. public function selects($criteria = null)
  129. {
  130. $query = "SELECT * FROM {$this->getTableName()}";
  131. if (!empty($criteria))
  132. {
  133. $subQuery = array();
  134. foreach ($criteria as $i => $j)
  135. {
  136. if ($j == null)
  137. $subQuery[] = "`{$i}` IS NULL";
  138. else
  139. $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
  140. }
  141. $query .= " WHERE ".implode(" AND ", $subQuery);
  142. }
  143. $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
  144. if ($result === false)
  145. throw new \Exception(self::$dbo->errorInfo()[2]);
  146. $resultObj = array();
  147. $className = get_class($this);
  148. foreach ($result as $i)
  149. {
  150. $iObj = new $className($this);
  151. $iObj->populate($i);
  152. $resultObj[] = $iObj;
  153. }
  154. return $resultObj;
  155. }
  156. //TODO
  157. public function selectById($id)
  158. {
  159. }
  160. private function populate($data)
  161. {
  162. $this->id = FALSE;
  163. foreach ($data as $i => $j)
  164. $this->fieldsValues[$i] = $j;
  165. if (isset($data["id"]))
  166. $this->id = (int) $data["id"];
  167. }
  168. }