]> git.andy128k.dev Git - ipf.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/admin/model.php
ipf/admin/views.php
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 842a9659aa422b6e07ba316e48ea1c7655a96e4a..32e09c1d7cde19600c7bdb44798ea8d7096b4cce 100644 (file)
@@ -387,11 +387,15 @@ class IPF_Admin_Model{
         return $this->header;
     }
 
-    public function ListItemsQuery(){
-        if (method_exists($this->model,'ordering'))
+    public function ListItemsQuery()
+    {
+        if (method_exists($this->model,'ordering')) {
             $ord = $this->model->ordering();
-        else
+        } elseif ($this->model->getTable()->hasTemplate('IPF_ORM_Template_Orderable')) {
+            $ord = $this->model->getTable()->getTemplate('IPF_ORM_Template_Orderable')->getColumnName();
+        } else {
             $ord = '1 desc';
+        }
         $this->q = IPF_ORM_Query::create()->from($this->modelName)->orderby($ord);
     }
 
@@ -758,8 +762,19 @@ class IPF_Admin_Model{
         }
     }
 
-    protected function _orderable(){
-        return method_exists($this, 'list_order');
+    public function _orderable()
+    {
+        return $this->_orderableColumn() !== null;
+    }
+
+    public function _orderableColumn()
+    {
+        if (method_exists($this, 'list_order'))
+            return $this->list_order();
+        elseif ($this->model->getTable()->hasTemplate('IPF_ORM_Template_Orderable'))
+            return $this->model->getTable()->getTemplate('IPF_ORM_Template_Orderable')->getColumnName();
+        else
+            return null;
     }
 }
 
index f2116478806e53348778580970f1c6a8a97ebe79..14dbc4fd28104d3a7a4163c112f5a97ffef594ef 100644 (file)
@@ -185,9 +185,8 @@ function IPF_Admin_Views_Reorder($request, $match)
         $user_perms = IPF_Auth_App::checkPermissions($request, $app, $m, array('view', 'change'));
         $ma = ($user_perms['view'] && $user_perms['change']) ? IPF_Admin_Model::getModelAdmin($m) : null;
         
-        if ($ma !==null && method_exists($ma, 'list_order'))
-        {
-            $ord_field = $ma->list_order();
+        if ($ma !==null && $ma->_orderable()) {
+            $ord_field = $ma->_orderableColumn();
 
             $ids      = explode(',',(string)$request->POST['ids']);
             $prev_ids = explode(',',(string)$request->POST['prev_ids']);
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));
+    }
+}
+