]> git.andy128k.dev Git - ipf-legacy-orm.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]

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);
+    }
+}
+