Browse Source

Merge pull request #645 from gimler/reorder

Reorder fields
Thomas 13 years ago
parent
commit
1d8102ef23

+ 14 - 0
Admin/Admin.php

@@ -1290,6 +1290,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         $this->formGroups = $formGroups;
     }
 
+    public function reorderFormGroup($group, array $keys)
+    {
+        $formGroups = $this->getFormGroups();
+        $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
+        $this->setFormGroups($formGroups);
+    }
+
     public function getShowGroups()
     {
         return $this->showGroups;
@@ -1300,6 +1307,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         $this->showGroups = $showGroups;
     }
 
+    public function reorderShowGroup($group, array $keys)
+    {
+        $showGroups = $this->getShowGroups();
+        $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
+        $this->setShowGroups($showGroups);
+    }
+
     /**
      * {@inheritdoc}
      */

+ 6 - 0
Admin/FieldDescriptionCollection.php

@@ -91,4 +91,10 @@ class FieldDescriptionCollection implements \ArrayAccess, \Countable
     {
         return count($this->elements);
     }
+
+    public function reorder(array $keys)
+    {
+        array_unshift($keys, 'batch');
+        $this->elements = array_merge(array_flip($keys), $this->elements);
+    }
 }

+ 5 - 0
Datagrid/Datagrid.php

@@ -159,6 +159,11 @@ class Datagrid implements DatagridInterface
         return $this->filters;
     }
 
+    public function reorderFilters(array $keys)
+    {
+        $this->filters = array_merge(array_flip($keys), $this->filters);
+    }
+
     /**
      * @return array
      */

+ 11 - 0
Datagrid/DatagridMapper.php

@@ -110,4 +110,15 @@ class DatagridMapper
 
         return $this;
     }
+
+    /**
+     * @param array $keys field names
+     * @return \Sonata\AdminBundle\Datagrid\ListMapper
+     */
+    public function reorder(array $keys)
+    {
+        $this->datagrid->reorderFilters($keys);
+
+        return $this;
+    }
 }

+ 11 - 0
Datagrid/ListMapper.php

@@ -112,4 +112,15 @@ class ListMapper
 
         return $this;
     }
+
+    /**
+     * @param array $keys field names
+     * @return \Sonata\AdminBundle\Datagrid\ListMapper
+     */
+    public function reorder(array $keys)
+    {
+        $this->list->reorder($keys);
+
+        return $this;
+    }
 }

+ 15 - 0
Form/FormMapper.php

@@ -67,6 +67,21 @@ class FormMapper
         return $this;
     }
 
+    /**
+     * @param array $keys field names
+     * @return \Sonata\AdminBundle\Form\FormMapper
+     */
+    public function reorder(array $keys)
+    {
+        if (!$this->currentGroup) {
+            $this->with($this->admin->getLabel());
+        }
+
+        $this->admin->reorderFormGroup($this->currentGroup, $keys);
+
+        return $this;
+    }
+
     /**
      * @param string $name
      * @param string $type

+ 15 - 0
Show/ShowMapper.php

@@ -115,6 +115,21 @@ class ShowMapper
         return $this;
     }
 
+    /**
+     * @param array $keys field names
+     * @return \Sonata\AdminBundle\Show\ShowMapper
+     */
+    public function reorder(array $keys)
+    {
+        if (!$this->currentGroup) {
+            $this->with($this->admin->getLabel());
+        }
+
+        $this->admin->reorderShowGroup($this->currentGroup, $keys);
+
+        return $this;
+    }
+
     /**
      * @param string $name
      * @param array $options