]> git.andy128k.dev Git - ipf.git/commitdiff
admin: rework reordering, allow to reorder inlines
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 7 May 2013 18:06:11 +0000 (21:06 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 7 May 2013 18:06:11 +0000 (21:06 +0300)
ipf/admin/media/css/forms.css
ipf/admin/modelinline.php
ipf/admin/templates/admin/change.html
ipf/admin/templates/admin/items.html
ipf/admin/views.php
ipf/orm/record.php
ipf/utils.php

index ee2ddef38018ab44986015c2933040040d9f2c4d..2d112cfad15d45a8158abbfede2e1024c048c8f1 100644 (file)
@@ -69,3 +69,6 @@ fieldset.monospace textarea { font-family:"Bitstream Vera Sans Mono",Monaco,"Cou
 .flatpages-flatpage #id_content { height:40.2em; }
 .module table .vPositiveSmallIntegerField { width:2.2em; }
 
+/* INLINE REORDER */
+.ItemsDragClass{background-color:#E3F3FE;}
+
index 87a5a3157a8f411aeda7cdabbc6401da38ec1991..4ab936cdf8ade403f9151612c9cf88f47f953026 100644 (file)
@@ -19,6 +19,11 @@ abstract class IPF_Admin_ModelInline{
 
     abstract function getModelName();
 
+    public function getApplication()
+    {
+        return IPF_Utils::appByModel($this->getModelName());
+    }
+
     function getAddNum(){ return 4; }
 
     function getLegend(){
@@ -60,14 +65,28 @@ abstract class IPF_Admin_ModelInline{
 
         $this->formset = array();
 
+        $o = $this->_orderableColumn();
+
+        $form_extra = array(
+            'exclude' => array(
+                $this->getFkName(),
+                $this->getFkLocal(),
+            ),
+        );
+        if ($o)
+            $form_extra['exclude'][] = $o;
+
         $first = true;
 
         if ($this->parentModel->exists()){
-            $objects = IPF_ORM_Query::create()
+            $query = IPF_ORM_Query::create()
                 ->from(get_class($this->model))
-                ->orderby($this->orderby)
-                ->where($this->getFkLocal().'='.$this->parentModel->id)
-                ->execute();
+                ->where($this->getFkLocal().'='.$this->parentModel->id);
+
+            if ($o)
+                $objects = $query->orderby($o)->execute();
+            else
+                $objects = $query->orderby($this->orderby)->execute();
 
             foreach ($objects as $obj){
                 $prefix = 'edit_'.get_class($this->model).'_'.$obj->id.'_';
@@ -85,9 +104,7 @@ abstract class IPF_Admin_ModelInline{
                     }
                 }
 
-                $form = $this->_getForm($obj, $d,
-                    array('exclude'=>array($this->getFkName(),$this->getFkLocal()))
-                );
+                $form = $this->_getForm($obj, $d, $form_extra);
 
 
                 $form->prefix = $prefix;
@@ -108,7 +125,7 @@ abstract class IPF_Admin_ModelInline{
 
         $n_addnum = $this->getAddNum();
         for($i=0; $i<$n_addnum; $i++ ){
-            $form = $this->_getForm($this->model->copy(), null, array('exclude'=>array($this->getFkName(),$this->getFkLocal())));
+            $form = $this->_getForm($this->model->copy(), null, $form_extra);
             $form->fields = array_merge(array(new IPF_Form_Field_Boolean(array('label'=>'Del','name'=>'delete_', 'widget_attrs'=>array('disabled'=>'disabled')))),$form->fields);
             $form->prefix = 'add_'.get_class($this->model).'_'.$i.'_';
             $form->data = $data;
@@ -171,4 +188,18 @@ abstract class IPF_Admin_ModelInline{
             }
         }
     }
+
+    public function _orderable()
+    {
+        return $this->_orderableColumn() !== null;
+    }
+
+    public function _orderableColumn()
+    {
+        if ($this->model->getTable()->hasTemplate('IPF_ORM_Template_Orderable'))
+            return $this->model->getTable()->getTemplate('IPF_ORM_Template_Orderable')->getColumnName();
+        else
+            return null;
+    }
 }
+
index 2e0f150a00c395430dc771705cfd2bcaaf4e406d..8cbc64093602aa65b4f531e2e4c6d81ecedf6618 100644 (file)
@@ -5,17 +5,6 @@
 {block breadcrumbs}<div class="breadcrumbs"><a href="{url 'IPF_Admin_Views_Index'}">Home</a> &raquo; <a href="{url 'IPF_Admin_Views_ListItems', array($lapp, $lmodel)}">{$classname}</a> &raquo; {$page_title}</div>{/block}
 
 {block content}
-
-<script type="text/javascript">
-{literal}
-$(document).ready(function(){
-    $("#id_ipf_referrer").val(document.referrer);
-    var del_href = $("#id_a_delete").attr("href");
-    $("#id_a_delete").attr("href", del_href+'?ipf_referrer='+document.referrer);
-});
-{/literal}
-</script>
-
 <div id="content" class="colM">
     <h1>{$page_title}</h1>
     <div id="content-main">
@@ -37,7 +26,10 @@ $(document).ready(function(){
                 <div class="tabular inline-related">
                     <fieldset class="module">
                     <h2>{$inline->getLegend()}</h2>
-                    <table>
+                    <table{if $inline->_orderable()}
+                        class="orderable-inlne"
+                        data-url="{url 'IPF_Admin_Views_ListItems', array($inline->getApplication()->getSlug(), strtolower($inline->getModelName()))}"
+                    {/if}>
                         {foreach $inline.formset as $formset}
                         {if $formset.isFirst}
                         <thead>
@@ -49,7 +41,7 @@ $(document).ready(function(){
                         </thead>
                         <tbody>
                         {/if}
-                        <tr>
+                        <tr{if !$formset.isAdd} data-id="{$formset.model.pk()}"{else} class="nodrag nodrop"{/if}>
                             {foreach $formset.fields as $fieldname=>$field}
                             <td{if $field.widget.is_hidden} style="display:none;"{/if}>{$formset.field($fieldname).fieldErrors()}{$formset.field($fieldname)|safe}</td>
                             {/foreach}
@@ -73,6 +65,32 @@ $(document).ready(function(){
     </div>
     <br class="clear" />
 </div>
+{/block}
 
+{block scripts}
+<script type="text/javascript" src="{$ADMIN_MEDIA_URL}js/jquery.tablednd.js"></script>
+<script type="text/javascript">
+{literal}
+$(document).ready(function(){
+    $("#id_ipf_referrer").val(document.referrer);
+    var del_href = $("#id_a_delete").attr("href");
+    $("#id_a_delete").attr("href", del_href+'?ipf_referrer='+document.referrer);
+
+    $('.orderable-inlne').each(function(){
+        var $table = $(this);
+        $table.tableDnD({
+            onDragClass: "ItemsDragClass",
+            onDrop: function(table, row) {
+                var ids = [];
+                $table.find('tr[data-id]').each(function(){
+                    ids.push($(this).data('id'));
+                });
+                $.post($table.data('url') + 'reorder/', { 'ids[]': ids }, function(data){});
+            }
+        });
+    });
+});
+{/literal}
+</script>
 {/block}
 
index 24d1787e0fd3b06572be57ea1b6ca11f88e8e494..057484117a1f427f984b21897e926e48b8c4c84d 100644 (file)
@@ -6,29 +6,6 @@
 {block breadcrumbs}<div class="breadcrumbs"><a href="{url 'IPF_Admin_Views_Index'}">Home</a> &raquo; {$classname}</div>{/block}
 
 {block content}
-
-{if $orderable}
-<script language="javascript" type="text/javascript" src="{$ADMIN_MEDIA_URL}js/jquery.tablednd.js"></script>
-<script type="text/javascript">
-var reorder_url = "reorder/";
-var prev_ids;
-{literal}
-$(document).ready(function()
-{
-    $('#items-grid').tableDnD({
-        onDragClass: "ItemsDragClass",
-        onDragStart: function(table, row) {
-            prev_ids = $.tableDnD.serialize();
-        },
-        onDrop: function(table, row) {
-            $.post(reorder_url, {'ids': $.tableDnD.serialize(), 'prev_ids': prev_ids, 'drop_id':row.id}, function(data){});
-        }
-    });
-});
-{/literal}
-</script>
-{/if}
-
 <div id="content" class="flex">
     <h1>{$page_title}</h1>
     <div id="content-main">
@@ -92,10 +69,27 @@ $(document).ready(function()
     </div>
     <br class="clear" />
 </div>
+{/block}
 
-
-
-
-
+{block scripts}
+{if $orderable}
+<script type="text/javascript" src="{$ADMIN_MEDIA_URL}js/jquery.tablednd.js"></script>
+<script type="text/javascript">
+{literal}
+$(document).ready(function(){
+    $('#items-grid').tableDnD({
+        onDragClass: "ItemsDragClass",
+        onDrop: function(table, row) {
+            var ids = [];
+            $('#items-grid .trsort').each(function(){
+                ids.push(this.id);
+            });
+            $.post('reorder/', { 'ids[]': ids }, function(data){});
+        }
+    });
+});
+{/literal}
+</script>
+{/if}
 {/block}
 
index 14dbc4fd28104d3a7a4163c112f5a97ffef594ef..86b47ee268970b8529a35937862008ffdcef46a9 100644 (file)
@@ -169,7 +169,7 @@ function IPF_Admin_Views_Reorder($request, $match)
     $ca = IPF_Admin_App::checkAdminAuth($request);
     if ($ca!==true) return $ca;
 
-    if ($request->method != 'POST' || !isset($request->POST['ids']) || !isset($request->POST['prev_ids']) || !isset($request->POST['drop_id']))
+    if ($request->method != 'POST' || !isset($request->POST['ids']) || !is_array($request->POST['ids']))
         return new IPF_HTTP_Response_NotFound();
 
     $lapp = $match[1];
@@ -188,13 +188,30 @@ function IPF_Admin_Views_Reorder($request, $match)
         if ($ma !==null && $ma->_orderable()) {
             $ord_field = $ma->_orderableColumn();
 
-            $ids      = explode(',',(string)$request->POST['ids']);
-            $prev_ids = explode(',',(string)$request->POST['prev_ids']);
-            $drop_id  = $request->POST['drop_id'];
+            $ids = $request->POST['ids'];
+
+            $table = IPF_ORM::getTable($m);
+            $conn = $table->getConnection();
+
+            $idColumn = $table->getIdentifier();
+            if (is_array($idColumn))
+                $idColumn = $idColumn[0];
+
+            $questions = str_repeat('?,', count($ids)-1) . '?';
+            $query = 'SELECT ' . $conn->quoteIdentifier($ord_field) .
+                ' FROM ' . $conn->quoteIdentifier($table->getTableName()) .
+                ' WHERE ' . $conn->quoteIdentifier($idColumn) . ' IN (' . $questions . ')' .
+                ' ORDER BY ' . $conn->quoteIdentifier($ord_field);
+            $ords = $conn->fetchColumn($query, $ids);
+
+            $i = 0;
+            foreach ($ids as $id) {
+                $item = $table->find($id);
+                $item[$ord_field] = $ords[$i];
+                $item->save();
+                $i++;
+            }
 
-            $o = new $m();
-            $o->_reorder($ids, $ord_field, $drop_id, $prev_ids);
-            
             return new IPF_HTTP_Response_Json("Ok");
          }
     }
index 3510d9dac846a6ea0ffde016333028806e09840e..ec67315ccc739cdc4a43e3c975e2a2c162ddf526 100644 (file)
@@ -1303,15 +1303,5 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
             return $this->_custom[$name];
         return null;
     }
-
-    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++;
-        }
-    }
 }
 
index 040e45fb330a82cd069200f0996479e1c80135ec..bda6deb591099dcf0ed690e2813bdcde09d1ffb6 100644 (file)
@@ -131,15 +131,22 @@ class IPF_Utils
         return $date2 - $date1;
     }
 
+    static function appByModel($model)
+    {
+        foreach (IPF_Project::getInstance()->appList() as $app)
+            foreach ($app->modelList() as $m)
+                if ($model == $m)
+                    return $app;
+        return null;
+    }
+
     static function appLabelByModel($model)
     {
-        foreach (IPF_Project::getInstance()->appList() as $app) {
-            foreach($app->modelList() as $m) {
-                if ($model==$m)
-                    return strtolower($app->getLabel());
-            }
-        }
-        return '';
+        $app = self::appByModel($model);
+        if ($app)
+            return strtolower($app->getLabel());
+        else
+            return '';
     }
 
     public static function insertDirectory($path, $directory)