isundil 10 жил өмнө
parent
commit
a05b9fcb63

+ 0 - 0
content/plugins/index.php → content/modules/index.php


+ 22 - 5
core/models/ModelBase.php

@@ -44,7 +44,7 @@ abstract class ModelBase
 
     public static function setup()
     {
-        $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
+        $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module");
 
         self::init();
         self::$dbo->beginTransaction();
@@ -88,7 +88,10 @@ abstract class ModelBase
         if ($value instanceof \DateTime)
             $value = $value->format("Y-m-d");
         $this->fieldsValues[$key] = $value;
-        $this->changed[$key] = self::$dbo->quote($value);
+        if (is_bool($value))
+            $this->changed[$key] = $value ? 1 : 0;
+        else
+            $this->changed[$key] = self::$dbo->quote($value);
         return $value;
     }
 
@@ -105,18 +108,28 @@ abstract class ModelBase
             else
             {
                 $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
-                $statement = self::$dbo->prepare($query);
-                $result = $statement->execute();
+                $result = self::$dbo->exec($query);
                 if (!$result)
-                    throw new \Exception($statement->errorInfo()[2]);
+                    throw new \Exception(self::$dbo->errorInfo()[2]);
                 $this->changed = array();
             }
                 //TODO get last id
         }
         else
         {
+            if (empty($this->changed))
+                return true;
             if ($this->id === false)
                 throw new \Exception("Cannot update private row");
+            $query = "UPDATE {$this->getTableName()} SET ";
+            $newValues = array();
+            foreach ($this->changed as $i => $j)
+                $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
+            $query .= implode(",",$newValues)." WHERE id={$this->id}";
+            $result = self::$dbo->exec($query);
+            if (!$result)
+                throw new \Exception(self::$dbo->errorInfo()[2]);
+            $this->changed = array();
         }
         //TODO send hook
     }
@@ -138,6 +151,8 @@ abstract class ModelBase
             $query .= " WHERE ".implode(" AND ", $subQuery);
         }
         $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
+        if ($result === false)
+            throw new \Exception(self::$dbo->errorInfo()[2]);
         $resultObj = array();
         $className = get_class($this);
         foreach ($result as $i)
@@ -159,6 +174,8 @@ abstract class ModelBase
         $this->id = FALSE;
         foreach ($data as $i => $j)
             $this->fieldsValues[$i] = $j;
+        if (isset($data["id"]))
+            $this->id = (int) $data["id"];
     }
 }
 

+ 28 - 0
core/models/Module.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Entity;
+
+class Module extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}module` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `name` VARCHAR(128) NOT NULL,
+            `description` VARCHAR(255) NOT NULL,
+            `directory` VARCHAR(255) NOT NULL,
+            `active` BOOLEAN DEFAULT FALSE NOT NULL
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+
+    public static function getActivated()
+    {
+        $fetcher = new self();
+        return $fetcher->selects(array("active" => true));
+    }
+}
+

+ 20 - 3
core/tools/Context.php

@@ -11,10 +11,27 @@ class Context
 
     public function __construct()
     {
-        \Entity\ModelBase::init();
         $this->router = new Router();
-        $this->moduleManager = new ModuleManager();
-        $this->router->init($this);
+        if (!\Entity\ModelBase::init())
+        {
+            @require_once(getcwd().'/core/setup/index.php');
+            die;
+        }
+        $this->router->init();
+        $this->moduleManager = new ModuleManager($this);
+        $this->router->serveUrl();
+    }
+
+    public function __get($key)
+    {
+        switch ($key)
+        {
+            case "router": return $this->router; break;
+            case "cart": return $this->cart; break;
+            case "user": return $this->user; break;
+            case "moduleManager": return $this->moduleManager; break;
+        }
+        throw new \Exception("Cannot access attribute {$key}");
     }
 }
 

+ 38 - 1
core/tools/ModuleManager.php

@@ -4,8 +4,45 @@ namespace Tools;
 
 class ModuleManager
 {
-    public function __construct()
+    private $context;
+    private $modules = array();
+
+    public function __construct($context)
+    {
+        $this->context = $context;
+        $modulesRoot = $context->router->getModulesPath();
+        $modules = \Entity\Module::getActivated();
+        foreach ($modules as $i)
+        {
+            $modulePath = "{$modulesRoot}{$i->directory}/main.php";
+            if (file_exists($modulePath))
+                $this->loadModule($modulePath, $i);
+            else
+            {
+                $i->active = false;
+                $i->save();
+            }
+        }
+        var_dump($this->modules);
+    }
+
+    private function loadModule($path, $module)
+    {
+        $this->modules[] = $module;
+        return true;
+    }
+
+    public function listAvailableModules()
     {
+        $modulesRoot = $context->router->getModulesPath();
+        $modules = scandir($modulesRoot, SCANDIR_SORT_NONE);
+        foreach ($modules as $i)
+        {
+            $path = $modulesRoot.$i;
+            if ($i == '.' || $i == '..' || !is_dir($path))
+                continue;
+            $this->loadModule($path);
+        }
     }
 }
 

+ 12 - 5
core/tools/Router.php

@@ -8,6 +8,14 @@ class Router
     private $rootUrl;
 
     public function __construct()
+    {
+        $pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
+        $relativePath = (($pos === FALSE) ? "" : substr($_SERVER["SCRIPT_NAME"], 0, $pos));
+        $this->rootPath = $_SERVER["DOCUMENT_ROOT"] . $relativePath . "/";
+        $this->rootUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $relativePath ."/";
+    }
+
+    public function init()
     {
         $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
         if ($siteUrl != $_SERVER["HTTP_HOST"])
@@ -15,13 +23,12 @@ class Router
             header("location: http://{$siteUrl}{$_SERVER['REQUEST_URI']}");
             die;
         }
-        $pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
-        $relativePath = (($pos === FALSE) ? "" : substr($_SERVER["SCRIPT_NAME"], 0, $pos));
-        $this->rootPath = $_SERVER["DOCUMENT_ROOT"] . $relativePath . "/";
-        $this->rootUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $relativePath ."/";
     }
 
-    public function init($context)
+    public function getModulesPath()
+    { return $this->rootPath . "content/modules/"; }
+
+    public function serveUrl()
     {
     }
 }