]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
orderable template
authorAndrey Kutejko <andy128k@gmail.com>
Fri, 19 Apr 2013 22:55:29 +0000 (01:55 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Fri, 19 Apr 2013 22:55:29 +0000 (01:55 +0300)
ipf/orm/record.php
ipf/orm/template.php
ipf/orm/template/listener/orderable.php [new file with mode: 0644]
ipf/orm/template/orderable.php [new file with mode: 0644]

index 402b660258af7482952eb0bf81bae9b84734d30f..3510d9dac846a6ea0ffde016333028806e09840e 100644 (file)
@@ -1265,7 +1265,8 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
         return (string) $this->_oid;
     }
 
-    public function ModelAdmin(){
+    public function ModelAdmin()
+    {
         $cn = get_class($this);
         if (isset(IPF_Admin_Model::$models[$cn]))
             return IPF_Admin_Model::$models[$cn];
@@ -1291,22 +1292,26 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
         }
     }
 
-    public function SetCustom($name, $val){
+    public function SetCustom($name, $val)
+    {
         $this->_custom[$name] = $val;
     }
 
-    public function GetCustom($name){
+    public function GetCustom($name)
+    {
         if (isset($this->_custom[$name]))
             return $this->_custom[$name];
         return null;
     }
 
-    public function _reorder($ids, $ord_field, $drop_id, $prev_ids, $ord=1){
-        foreach($ids as $id){
+    public function _reorder($ids, $ord_field, $drop_id, $prev_ids, $ord=1)
+    {
+        foreach($ids as $id) {
             $item = $this->getTable()->find($id);
             $item[$ord_field] = $ord;
             $item->save();
             $ord++;
         }
     }
-}
\ No newline at end of file
+}
+
index 3de6e4d1b076db8bd4e5c45cd8c1f1dd451c0257..9bca51fc25045e3bf5d18b94567b58c19c322264 100644 (file)
@@ -4,6 +4,7 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract
 {
     protected $_invoker;
     protected $_plugin;
+
     public function setTable(IPF_ORM_Table $table)
     {
         $this->_table = $table;
@@ -27,7 +28,6 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract
     public function addChild(IPF_ORM_Template $template)
     {
         $this->_plugin->addChild($template);
-        
         return $this;
     }
 
@@ -48,11 +48,10 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract
 
     public function setUp()
     {
-
     }
 
     public function setTableDefinition()
     {
-
     }
-}
\ No newline at end of file
+}
+
diff --git a/ipf/orm/template/listener/orderable.php b/ipf/orm/template/listener/orderable.php
new file mode 100644 (file)
index 0000000..8b210e1
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+class IPF_ORM_Template_Listener_Orderable extends IPF_ORM_Record_Listener
+{
+    private $columnName = 'ord';
+
+    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;
+
+        $res = IPF_ORM_Query::create()
+             ->select('max('.$this->columnName.') as x_ord')
+             ->from(get_class($obj))
+             ->execute();
+        if (isset($res[0]->x_ord))
+            $obj->$columnName = (int)$res[0]->x_ord + 1;
+        else
+            $obj->$columnName = 1;
+    }
+}
+
diff --git a/ipf/orm/template/orderable.php b/ipf/orm/template/orderable.php
new file mode 100644 (file)
index 0000000..2f95bf7
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+class IPF_ORM_Template_Orderable extends IPF_ORM_Template
+{
+    private $columnName = 'ord';
+
+    public function __construct(array $options=array())
+    {
+        if ($options && array_key_exists('name', $options))
+            $this->columnName = $options['name'];
+    }
+
+    public function getColumnName()
+    {
+        return $this->columnName;
+    }
+
+    public function setTableDefinition()
+    {
+        $this->hasColumn($this->columnName, 'integer', null, '');
+        $this->addListener(new IPF_ORM_Template_Listener_Orderable($this->columnName));
+    }
+}
+