|
|
@@ -0,0 +1,130 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Entity;
|
|
|
+
|
|
|
+abstract class ModelBase
|
|
|
+{
|
|
|
+ private $fieldsValues = array();
|
|
|
+ protected static $dbo = null;
|
|
|
+ private $changed = array();
|
|
|
+ private static $config = null;
|
|
|
+ private $id;
|
|
|
+
|
|
|
+ protected abstract function install();
|
|
|
+
|
|
|
+ public function __construct($id = null)
|
|
|
+ {
|
|
|
+ self::init();
|
|
|
+ $this->id = null;
|
|
|
+ if ($id !== null)
|
|
|
+ $this->selectById($id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function init()
|
|
|
+ {
|
|
|
+ if (self::$dbo !== null)
|
|
|
+ return true;
|
|
|
+ self::$config = @include("core/config.inc.php");
|
|
|
+ if (empty(self::$config))
|
|
|
+ return false;
|
|
|
+ self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function getDbPrefix()
|
|
|
+ {
|
|
|
+ return self::$config[3];
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function setup()
|
|
|
+ {
|
|
|
+ $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
|
|
|
+
|
|
|
+ self::init();
|
|
|
+ self::$dbo->beginTransaction();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ foreach ($tables as $i)
|
|
|
+ {
|
|
|
+ $i = "Entity\\".$i;
|
|
|
+ $table = new $i();
|
|
|
+ if ($table->install() != true)
|
|
|
+ throw new \Exception();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (\Exception $e)
|
|
|
+ {
|
|
|
+ echo $e->getMessage();
|
|
|
+ self::$dbo->rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ self::$dbo->commit();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getMeta($lang=null)
|
|
|
+ {
|
|
|
+ $fetcher = new \Entity\Meta();
|
|
|
+ return $fetcher->query(array("type" => get_class()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function __get($key)
|
|
|
+ {
|
|
|
+ if ($key == "meta")
|
|
|
+ return $this->getMeta();
|
|
|
+ if (!isset($this->fieldsValues[$key]))
|
|
|
+ return null;
|
|
|
+ return $this->fieldsValues[$key];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function __set($key, $value)
|
|
|
+ {
|
|
|
+ if ($value instanceof \DateTime)
|
|
|
+ $value = $value->format("Y-m-d");
|
|
|
+ $this->fieldsValues[$key] = $value;
|
|
|
+ $this->changed[$key] = self::$dbo->quote($value);
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function save()
|
|
|
+ {
|
|
|
+ $className = new \ReflectionClass($this);
|
|
|
+ $className = strtolower($className->getShortName());
|
|
|
+ //TODO send pre-hook
|
|
|
+ if ($this->id === null)
|
|
|
+ {
|
|
|
+ if (empty ($this->changed))
|
|
|
+ {
|
|
|
+ //TODO
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ $query = "INSERT INTO `{$this->getDbPrefix()}{$className}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
|
|
|
+ $statement = self::$dbo->prepare($query);
|
|
|
+ $result = $statement->execute();
|
|
|
+ if (!$result)
|
|
|
+ throw new \Exception($statement->errorInfo()[2]);
|
|
|
+ $this->changed = array();
|
|
|
+ }
|
|
|
+ //TODO get last id
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if ($this->id === false)
|
|
|
+ throw new \Exception("Cannot update private row");
|
|
|
+ }
|
|
|
+ //TODO send hook
|
|
|
+ }
|
|
|
+
|
|
|
+ //TODO
|
|
|
+ public function selectById($id)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ private function populate($data)
|
|
|
+ {
|
|
|
+ $this->id = FALSE;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|