Browse Source

Refactor form group

Thomas Rabaix 14 years ago
parent
commit
80a56ffed9
2 changed files with 44 additions and 14 deletions
  1. 5 14
      Admin/Admin.php
  2. 39 0
      Form/FormMapper.php

+ 5 - 14
Admin/Admin.php

@@ -624,20 +624,6 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         }
 
         $this->form = $this->getFormBuilder()->getForm();
-
-        // configure the form group thing
-        if (!$this->formGroups) {
-            $this->formGroups = array(
-                false => array('fields' => array_keys($this->formFieldDescriptions))
-            );
-        }
-
-        // normalize array
-        foreach ($this->formGroups as $name => $group) {
-            if (!isset($this->formGroups[$name]['collapsed'])) {
-                $this->formGroups[$name]['collapsed'] = false;
-            }
-        }
     }
 
     /**
@@ -1136,6 +1122,11 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         return $this->formGroups;
     }
 
+    public function setFormGroups(array $formGroups)
+    {
+        $this->formGroups = $formGroups;
+    }
+
     public function getViewGroups()
     {
         return $this->viewGroups;

+ 39 - 0
Form/FormMapper.php

@@ -27,6 +27,8 @@ class FormMapper
 
     protected $admin;
 
+    protected $currentGroup;
+
     public function __construct(FormContractorInterface $formContractor, FormBuilder $formBuilder, AdminInterface $admin)
     {
         $this->formBuilder      = $formBuilder;
@@ -34,6 +36,35 @@ class FormMapper
         $this->admin            = $admin;
     }
 
+    /**
+     * @param $name
+     * @param array $options
+     * @return void
+     */
+    public function with($name, array $options = array())
+    {
+        $formGroups = $this->admin->getFormGroups();
+        if (!isset($formGroups[$name])) {
+            $formGroups[$name] = array_merge(array('collapsed' => false, 'fields' => array()), $options);
+        }
+
+        $this->admin->setFormGroups($formGroups);
+
+        $this->currentGroup = $name;
+
+        return $this;
+    }
+
+    /**
+     * @return void
+     */
+    public function end()
+    {
+        $this->currentGroup = null;
+
+        return $this;
+    }
+
     /**
      * @param string $name
      * @param string $type
@@ -43,6 +74,14 @@ class FormMapper
      */
     public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
     {
+        if (!$this->currentGroup) {
+            $this->with($this->admin->getLabel());
+        }
+
+        $formGroups = $this->admin->getFormGroups();
+        $formGroups[$this->currentGroup]['fields'][$name] = $name;
+        $this->admin->setFormGroups($formGroups);
+
         if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
             $fieldDescriptionOptions['type'] = $type;
         }