Browse Source

Refactor the show groups feature

Thomas Rabaix 14 years ago
parent
commit
9172b8f70f
3 changed files with 57 additions and 19 deletions
  1. 8 16
      Admin/Admin.php
  2. 1 1
      Resources/views/CRUD/base_show.html.twig
  3. 48 2
      Show/ShowMapper.php

+ 8 - 16
Admin/Admin.php

@@ -135,7 +135,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      *
      * @var array|boolean
      */
-    protected $viewGroups = false;
+    protected $showGroups = false;
 
     /**
      * The label class name  (used in the title/breadcrumb ...)
@@ -486,19 +486,6 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         $this->configureShowField($mapper);
 
-        if (!$this->viewGroups) {
-            $this->viewGroups = array(
-                false => array('fields' => array_keys($this->getShowFieldDescriptions()))
-            );
-        }
-
-        // normalize array
-        foreach ($this->viewGroups as $name => $group) {
-            if (!isset($this->viewGroups[$name]['collapsed'])) {
-                $this->viewGroups[$name]['collapsed'] = false;
-            }
-        }
-
         $this->show = $collection;
     }
 
@@ -1127,9 +1114,14 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         $this->formGroups = $formGroups;
     }
 
-    public function getViewGroups()
+    public function getShowGroups()
+    {
+        return $this->showGroups;
+    }
+
+    public function setShowGroups(array $showGroups)
     {
-        return $this->viewGroups;
+        $this->showGroups = $showGroups;
     }
 
     /**

+ 1 - 1
Resources/views/CRUD/base_show.html.twig

@@ -29,7 +29,7 @@ file that was distributed with this source code.
 
 {% block show %}
     <div class="sonata-ba-view">
-        {% for name, view_group in admin.viewgroups %}
+        {% for name, view_group in admin.showgroups %}
             <table>
                 {% if name %}
                     <tr class="sonata-ba-view-title">

+ 48 - 2
Show/ShowMapper.php

@@ -28,6 +28,13 @@ class ShowMapper
 
     protected $admin;
 
+    protected $currentGroup;
+
+    /**
+     * @param \Sonata\AdminBundle\Builder\ShowBuilderInterface $showBuilder
+     * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
+     */
     public function __construct(ShowBuilderInterface $showBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
     {
         $this->showBuilder  = $showBuilder;
@@ -37,12 +44,22 @@ class ShowMapper
 
     /**
      * @throws \RuntimeException
-     * @param string $name
+     * @param $name
+     * @param null $type
      * @param array $fieldDescriptionOptions
-     * @return \Sonata\AdminBundle\Datagrid\ListMapper
+     * @return ShowMapper
      */
     public function add($name, $type = null, array $fieldDescriptionOptions = array())
     {
+        if (!$this->currentGroup) {
+            $this->with($this->admin->getLabel());
+        }
+
+        $formGroups = $this->admin->getShowGroups();
+        $formGroups[$this->currentGroup]['fields'][$name] = $name;
+        $this->admin->setShowGroups($formGroups);
+
+
         if ($name instanceof FieldDescriptionInterface) {
 
             $fieldDescription = $name;
@@ -93,4 +110,33 @@ class ShowMapper
         $this->admin->removeShowFieldDescription($key);
         $this->list->remove($key);
     }
+
+    /**
+     * @param $name
+     * @param array $options
+     * @return void
+     */
+    public function with($name, array $options = array())
+    {
+        $showGroups = $this->admin->getShowGroups();
+        if (!isset($showGroups[$name])) {
+            $showGroups[$name] = array_merge(array('collapsed' => false, 'fields' => array()), $options);
+        }
+
+        $this->admin->setFormGroups($showGroups);
+
+        $this->currentGroup = $name;
+
+        return $this;
+    }
+
+    /**
+     * @return void
+     */
+    public function end()
+    {
+        $this->currentGroup = null;
+
+        return $this;
+    }
 }