]> git.andy128k.dev Git - ipf.git/commitdiff
owned template
authorAndrey Kutejko <andy128k@gmail.com>
Mon, 15 Jul 2013 21:57:07 +0000 (00:57 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Mon, 15 Jul 2013 21:57:07 +0000 (00:57 +0300)
ipf/orm/template/listener/owned.php [new file with mode: 0644]
ipf/orm/template/owned.php [new file with mode: 0644]
ipf/project.php

diff --git a/ipf/orm/template/listener/owned.php b/ipf/orm/template/listener/owned.php
new file mode 100644 (file)
index 0000000..a968232
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+class IPF_ORM_Template_Listener_Owned
+{
+    private $columnName;
+
+    public function __construct($columnName)
+    {
+        $this->columnName = $columnName;
+    }
+
+    public function preInsert(IPF_ORM_Event $event)
+    {
+        $this->setOrderValue($event->getInvoker());
+    }
+
+    public function preUpdate(IPF_ORM_Event $event)
+    {
+        $this->setOrderValue($event->getInvoker());
+    }
+
+    private function setOrderValue($obj)
+    {
+        $columnName = $this->columnName;
+        if ($obj->$columnName)
+            return;
+
+        $request = IPF_Project::getInstance()->request;
+        if ($request && !$request->user->isAnonymous()) {
+            $obj->$columnName = $request->user->id;
+        }
+    }
+}
+
diff --git a/ipf/orm/template/owned.php b/ipf/orm/template/owned.php
new file mode 100644 (file)
index 0000000..40ce263
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+class IPF_ORM_Template_Owned extends IPF_ORM_Template
+{
+    private $name = 'owner';
+    private $columnName = 'owner_id';
+    private $exclude = true;
+    private $verbose = 'owner';
+
+    public function __construct(array $options=array())
+    {
+        if ($options) {
+            if (array_key_exists('column', $options))
+                $this->columnName = $options['column'];
+            if (array_key_exists('name', $options))
+                $this->name = $options['name'];
+            if (array_key_exists('exclude', $options))
+                $this->exclude = $options['exclude'];
+            if (array_key_exists('verbose', $options))
+                $this->verbose = $options['verbose'];
+        }
+    }
+
+    public function getColumnName()
+    {
+        return $this->columnName;
+    }
+
+    public function setTableDefinition()
+    {
+        $this->hasColumn($this->columnName, 'integer', null, array(
+            'exclude'   => $this->exclude,
+            'notblank'  => true,
+            'notnull'   => true,
+            'verbose'   => $this->verbose,
+        ));
+        $this->hasOne('User as '.$this->name, array('local' => $this->columnName, 'foreign' => 'id', 'onDelete' => 'CASCADE'));
+        $this->getTable()->listeners['Owned_'.$this->columnName] = new IPF_ORM_Template_Listener_Owned($this->columnName);
+    }
+}
+
index 7a48770745e95abc0ba1095342a6eea9b7aab5d4..c6a4e7a3ceb470c304a9883facaaf038480e9054 100644 (file)
@@ -4,6 +4,7 @@ final class IPF_Project
 {
     private $apps = array();
     public $router = null;
+    public $request = null;
     public $sqlProfiler = null;
 
     static private $instance = NULL;
@@ -106,8 +107,10 @@ final class IPF_Project
             $cli->run();
         } else {
             $this->loadAllModels();
-            $request = new IPF_HTTP_Request;
+
+            $this->request = new IPF_HTTP_Request;
             $this->router->dispatch($request);
+            $this->request = null;
         }
 
         return true;