]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
ordered inlines
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 14 Sep 2014 12:42:34 +0000 (15:42 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 14 Sep 2014 12:42:34 +0000 (15:42 +0300)
src/adminmodel.php
src/adminmodelinline.php

index 75d0b94498f3151e50ec82938b132fc7f005aea7..a97ec6f8cc73a033b4ccd14e8b85cb4366b5b423 100644 (file)
@@ -312,7 +312,10 @@ class IPF_Admin_ModelForm extends IPF_Form_Model
         $modelName = $inline->getModelName();
         $fk_local = $inline->getFkLocal();
 
-        foreach ($this->cleaned_data[$modelName] as $objData) {
+        foreach ($this->cleaned_data[$modelName] as $index => $objData) {
+            if ($index === 'reorder')
+                continue;
+
             if (array_key_exists('id', $objData) && array_key_exists($objData['id'], $objIndex)) {
                 $obj = $objIndex[$objData['id']];
                 if (isset($objData['is_remove'])) {
@@ -323,11 +326,40 @@ class IPF_Admin_ModelForm extends IPF_Form_Model
                 }
             } else {
                 $objData[$fk_local] = $model->id;
+                unset($objData['id']);
                 $obj = new $modelName;
                 $obj->synchronizeWithArray($objData);
                 $obj->save();
             }
         }
+
+        $reorder = \PFF\Arr::get($this->cleaned_data[$modelName], 'reorder');
+        $this->reorderInline($inline, $objIndex, $reorder);
+    }
+
+    private function reorderInline($inline, $objIndex, $reorder)
+    {
+        $ord = $inline->_orderableColumn();
+        if ($ord === null || !$reorder)
+            return;
+
+        $objs = array();
+        $ords = array();
+        foreach ($reorder as $id) {
+            if (array_key_exists($id, $objIndex)) {
+                $objs[] = $objIndex[$id];
+                $ords[] = $objIndex[$id]->$ord;
+            }
+        }
+
+        sort($ords);
+
+        $i = 0;
+        foreach ($objs as $obj) {
+            $obj->$ord = $ords[$i];
+            $obj->save();
+            $i++;
+        }
     }
 }
 
index bb5e4dd158dc1f30bc9d0dacb30546fe7ee30adf..c9e70e03814ef1fea7040bcd9fdbd78fcb11331c 100644 (file)
@@ -87,24 +87,30 @@ abstract class IPF_Admin_ModelInline
 
     public function createField()
     {
+        $args = array(
+            'fields' => array(),
+            'addCount' => $this->getAddNum(),
+        );
+
         $exclude = array(
             $this->getFkName(),
             $this->getFkLocal(),
         );
         $o = $this->_orderableColumn();
-        if ($o)
+        if ($o) {
+            $fieldClass = 'IPF_Form_Field_OrderedSet';
             $exclude[] = $o;
+            $args['orderBy'] = $o;
+        } else {
+            $fieldClass = 'IPF_Form_Field_Set';
+        }
 
-        $fields = array();
         foreach (IPF_Form_Model::suggestFields($this->getTable(), null, $exclude, null) as $field) {
             list($n, $f) = $field;
-            $fields[$n] = $f;
+            $args['fields'][$n] = $f;
         }
 
-        return new IPF_Form_Field_Set(array(
-            'fields' => $fields,
-            'addCount' => $this->getAddNum(),
-        ));
+        return new $fieldClass($args);
     }
 }