isundil 10 gadi atpakaļ
vecāks
revīzija
7998acd93e

+ 4 - 1
.htaccess

@@ -1 +1,4 @@
- 
+RewriteEngine  on
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^(.*)$ index.php [L,QSA]
+

+ 3 - 1
core/autoload.php

@@ -7,7 +7,9 @@ function __autoload($className)
     switch ($namespace)
     {
     case "Entity":
-        $path = "core/models/".$class.".php";
+        $path = "core/models/{$class}.php"; break;
+    case "Tools":
+        $path = "core/tools/{$class}.php"; break;
     }
     include_once ($path);
     return $path;

+ 15 - 0
core/models/Config.php

@@ -4,6 +4,8 @@ namespace Entity;
 
 class Config extends ModelBase
 {
+    private static $config = array();
+
     protected function install()
     {
         $dbPrefix = $this->getDbPrefix();
@@ -17,5 +19,18 @@ class Config extends ModelBase
             throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
         return true;
     }
+
+    public static function getConfig($lang =null, $key =null, $defaultValue =null)
+    {
+        $fetcher = new self();
+        if (isset(self::$config[$lang]))
+            return;
+        $values = $fetcher->selects(array("lang" => $lang));
+        foreach ($values as $i)
+            self::$config[$lang][$i->key] = $i->value;
+        if ($key)
+            return (isset(self::$config[$lang][$key]) ? self::$config[$lang][$key] : $defaultValue);
+        return $defaultValue;
+    }
 }
 

+ 37 - 3
core/models/ModelBase.php

@@ -36,6 +36,12 @@ abstract class ModelBase
         return self::$config[3];
     }
 
+    public function getTableName()
+    {
+        $className = new \ReflectionClass($this);
+        return $this->getDbPrefix().strtolower($className->getShortName());
+    }
+
     public static function setup()
     {
         $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
@@ -88,8 +94,6 @@ abstract class ModelBase
 
     public function save()
     {
-        $className = new \ReflectionClass($this);
-        $className = strtolower($className->getShortName());
         //TODO send pre-hook
         if ($this->id === null)
         {
@@ -100,7 +104,7 @@ abstract class ModelBase
             }
             else
             {
-                $query = "INSERT INTO `{$this->getDbPrefix()}{$className}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
+                $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
                 $statement = self::$dbo->prepare($query);
                 $result = $statement->execute();
                 if (!$result)
@@ -117,6 +121,34 @@ abstract class ModelBase
         //TODO send hook
     }
 
+    public function selects($criteria = null)
+    {
+        $query = "SELECT * FROM {$this->getTableName()}";
+
+        if (!empty($criteria))
+        {
+            $subQuery = array();
+            foreach ($criteria as $i => $j)
+            {
+                if ($j == null)
+                    $subQuery[] = "`{$i}` IS NULL";
+                else
+                    $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
+            }
+            $query .= " WHERE ".implode(" AND ", $subQuery);
+        }
+        $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
+        $resultObj = array();
+        $className = get_class($this);
+        foreach ($result as $i)
+        {
+            $iObj = new $className($this);
+            $iObj->populate($i);
+            $resultObj[] = $iObj;
+        }
+        return $resultObj;
+    }
+
     //TODO
     public function selectById($id)
     {
@@ -125,6 +157,8 @@ abstract class ModelBase
     private function populate($data)
     {
         $this->id = FALSE;
+        foreach ($data as $i => $j)
+            $this->fieldsValues[$i] = $j;
     }
 }
 

+ 20 - 0
core/tools/Context.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Tools;
+
+class Context
+{
+    private $moduleManager;
+    private $router;
+    private $cart;
+    private $user;
+
+    public function __construct()
+    {
+        \Entity\ModelBase::init();
+        $this->router = new Router();
+        $this->moduleManager = new ModuleManager();
+        $this->router->init($this);
+    }
+}
+

+ 11 - 0
core/tools/ModuleManager.php

@@ -0,0 +1,11 @@
+<?php 
+
+namespace Tools;
+
+class ModuleManager
+{
+    public function __construct()
+    {
+    }
+}
+

+ 28 - 0
core/tools/Router.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Tools;
+
+class Router
+{
+    private $rootPath;
+    private $rootUrl;
+
+    public function __construct()
+    {
+        $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
+        if ($siteUrl != $_SERVER["HTTP_HOST"])
+        {
+            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)
+    {
+    }
+}
+

+ 3 - 1
index.php

@@ -3,13 +3,15 @@
 chdir(dirname(__FILE__));
 require_once("core/autoload.php");
 
-Entity\ModelBase::init();
+$GLOBALS["context"] = new \Tools\Context();
 
 //var_dump(Entity\ModelBase::setup());
+/*
 $user = new \Entity\User();
 $user->password = "test";
 $user->email = "isundill@gmail.com";
 $user->sexe = "MALE";
 $user->optin = true;
 $user->save();
+*/