Bläddra i källkod

Added a grouped mapper to handle mappers that group fields together

Dean de Bree 12 år sedan
förälder
incheckning
890f19736f
4 ändrade filer med 149 tillägg och 92 borttagningar
  1. 21 52
      Form/FormMapper.php
  2. 88 0
      Mapper/BaseGroupedMapper.php
  3. 29 1
      Mapper/BaseMapper.php
  4. 11 39
      Show/ShowMapper.php

+ 21 - 52
Form/FormMapper.php

@@ -13,18 +13,16 @@ namespace Sonata\AdminBundle\Form;
 use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Admin\AdminInterface;
 use Symfony\Component\Form\FormBuilder;
-use Sonata\AdminBundle\Mapper\BaseMapper;
+use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
 
 /**
  * This class is use to simulate the Form API
  *
  */
-class FormMapper extends BaseMapper
+class FormMapper extends BaseGroupedMapper
 {
     protected $formBuilder;
 
-    protected $currentGroup;
-
     /**
      * @param \Sonata\AdminBundle\Builder\FormContractorInterface $formContractor
      * @param \Symfony\Component\Form\FormBuilder                 $formBuilder
@@ -36,42 +34,6 @@ class FormMapper extends BaseMapper
         $this->formBuilder    = $formBuilder;
     }
 
-    /**
-     * @param string $name
-     * @param array  $options
-     *
-     * @return \Sonata\AdminBundle\Form\FormMapper
-     */
-    public function with($name, array $options = array())
-    {
-        $formGroups = $this->admin->getFormGroups();
-        if (!isset($formGroups[$name])) {
-            $formGroups[$name] = array();
-        }
-
-        $formGroups[$name] = array_merge(array(
-            'collapsed'   => false,
-            'fields'      => array(),
-            'description' => false
-        ), $formGroups[$name], $options);
-
-        $this->admin->setFormGroups($formGroups);
-
-        $this->currentGroup = $name;
-
-        return $this;
-    }
-
-    /**
-     * @return \Sonata\AdminBundle\Form\FormMapper
-     */
-    public function end()
-    {
-        $this->currentGroup = null;
-
-        return $this;
-    }
-
     /**
      * @param array $keys field names
      *
@@ -79,11 +41,7 @@ class FormMapper extends BaseMapper
      */
     public function reorder(array $keys)
     {
-        if (!$this->currentGroup) {
-            $this->with($this->admin->getLabel());
-        }
-
-        $this->admin->reorderFormGroup($this->currentGroup, $keys);
+        $this->admin->reorderFormGroup($this->getCurrentGroupName(), $keys);
 
         return $this;
     }
@@ -98,15 +56,9 @@ class FormMapper extends BaseMapper
      */
     public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
     {
-        if (!$this->currentGroup) {
-            $this->with($this->admin->getLabel());
-        }
-
         $label = $name instanceof FormBuilder ? $name->getName() : $name;
 
-        $formGroups                                        = $this->admin->getFormGroups();
-        $formGroups[$this->currentGroup]['fields'][$label] = $label;
-        $this->admin->setFormGroups($formGroups);
+        $this->addFieldToCurrentGroup($label);
 
         if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
             $fieldDescriptionOptions['type'] = $type;
@@ -217,4 +169,21 @@ class FormMapper extends BaseMapper
 
         return $this;
     }
+    
+    /**
+     * {@inheritdoc}
+     */
+    protected function getGroups() 
+    {
+        return $this->admin->getFormGroups();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function setGroups(array $groups) 
+    {
+        $this->admin->setFormGroups($groups);
+    }
+    
 }

+ 88 - 0
Mapper/BaseGroupedMapper.php

@@ -0,0 +1,88 @@
+<?php
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+namespace Sonata\AdminBundle\Mapper;
+
+/**
+ * This class is used to simulate the Form API
+ *
+ */
+abstract class BaseGroupedMapper extends BaseMapper
+{
+
+    protected $currentGroup;
+    
+    protected abstract function getGroups();
+    
+    protected abstract function setGroups(array $groups);
+    
+    /**
+     * @param string $name
+     * @param array  $options
+     *
+     * @return \Sonata\AdminBundle\Mapper\BaseGroupedMapper
+     */
+    public function with($name, array $options = array())
+    {
+        $groups = $this->getGroups();
+        
+        if (!isset($groups[$name])) {
+            $groups[$name] = array();
+        }
+
+        $groups[$name] = array_merge(array(
+            'collapsed'   => false,
+            'fields'      => array(),
+            'description' => false
+        ), $groups[$name], $options);
+        
+        $this->setGroups($groups);
+
+        $this->currentGroup = $name;
+
+        return $this;
+    }
+    
+    /**
+     * @return \Sonata\AdminBundle\Mapper\BaseGroupedMapper
+     */
+    public function end()
+    {
+        $this->currentGroup = null;
+
+        return $this;
+    }
+    
+    /**
+     * Add the fieldname to the current group
+     * 
+     * @param string $fieldName
+     */
+    protected function addFieldToCurrentGroup($fieldName) 
+    {
+        $groups = $this->getGroups();
+        $groups[$this->getCurrentGroupName()]['fields'][$fieldName] = $fieldName;
+        $this->setGroups($groups);
+    }
+    
+    /**
+     * Return the name of the currently selected group. The method also makes 
+     * sure a valid group name is currently selected
+     * 
+     * @return string
+     */
+    protected function getCurrentGroupName() {
+        if (!$this->currentGroup) {
+            $this->with($this->admin->getLabel());
+        }
+        return $this->currentGroup;
+    }
+    
+}

+ 29 - 1
Mapper/BaseMapper.php

@@ -17,7 +17,7 @@ use Sonata\AdminBundle\Builder\BuilderInterface;
  * This class is used to simulate the Form API
  *
  */
-class BaseMapper
+abstract class BaseMapper
 {
 
     protected $admin;
@@ -42,4 +42,32 @@ class BaseMapper
         return $this->admin;
     }
     
+    /**
+     * @param string $name
+     *
+     * @return mixed
+     */
+    public abstract function get($key);
+
+    /**
+     * @param string $key
+     *
+     * @return boolean
+     */
+    public abstract function has($key);
+
+    /**
+     * @param string $key
+     *
+     * @return \Sonata\AdminBundle\Mapper\BaseMapper
+     */
+    public abstract function remove($key);
+    
+    /**
+     * @param array $keys field names
+     *
+     * @return \Sonata\AdminBundle\Mapper\BaseMapper
+     */
+    public abstract function reorder(array $keys);
+    
 }

+ 11 - 39
Show/ShowMapper.php

@@ -14,18 +14,16 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
-use Sonata\AdminBundle\Mapper\BaseMapper;
+use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
 
 /**
  * This class is used to simulate the Form API
  *
  */
-class ShowMapper extends BaseMapper
+class ShowMapper extends BaseGroupedMapper
 {
     protected $list;
 
-    protected $currentGroup;
-
     /**
      * @param \Sonata\AdminBundle\Builder\ShowBuilderInterface     $showBuilder
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
@@ -48,15 +46,9 @@ class ShowMapper extends BaseMapper
      */
     public function add($name, $type = null, array $fieldDescriptionOptions = array())
     {
-        if (!$this->currentGroup) {
-            $this->with($this->admin->getLabel());
-        }
-
         $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name;
 
-        $formGroups = $this->admin->getShowGroups();
-        $formGroups[$this->currentGroup]['fields'][$fieldKey] = $fieldKey;
-        $this->admin->setShowGroups($formGroups);
+        $this->addFieldToCurrentGroup($fieldKey);
 
         if ($name instanceof FieldDescriptionInterface) {
             $fieldDescription = $name;
@@ -123,45 +115,25 @@ class ShowMapper extends BaseMapper
      */
     public function reorder(array $keys)
     {
-        if (!$this->currentGroup) {
-            $this->with($this->admin->getLabel());
-        }
-
-        $this->admin->reorderShowGroup($this->currentGroup, $keys);
+        $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys);
 
         return $this;
     }
 
     /**
-     * @param string $name
-     * @param array  $options
-     *
-     * @return \Sonata\AdminBundle\Show\ShowMapper
+     * {@inheritdoc}
      */
-    public function with($name, array $options = array())
+    protected function getGroups() 
     {
-        $showGroups = $this->admin->getShowGroups();
-        if (!isset($showGroups[$name])) {
-            $showGroups[$name] = array_merge(array(
-                'collapsed' => false,
-                'fields'    => array()
-            ), $options);
-        }
-
-        $this->admin->setShowGroups($showGroups);
-
-        $this->currentGroup = $name;
-
-        return $this;
+        return $this->admin->getShowGroups();
     }
 
     /**
-     * @return \Sonata\AdminBundle\Show\ShowMapper
+     * {@inheritdoc}
      */
-    public function end()
+    protected function setGroups(array $groups)
     {
-        $this->currentGroup = null;
-
-        return $this;
+        $this->admin->setShowGroups($groups);
     }
+
 }