瀏覽代碼

add field groups

Thomas 14 年之前
父節點
當前提交
1d86ac9fd0

+ 29 - 2
Admin/Admin.php

@@ -32,6 +32,8 @@ abstract class Admin extends ContainerAware
 
     protected $base_controller_name;
 
+    protected $form_groups = false;
+
     // note : don't like this, but havn't find a better way to do it
     protected $configuration_pool;
 
@@ -42,7 +44,10 @@ abstract class Admin extends ContainerAware
     public function configure()
     {
         $this->buildFormFields();
+        $this->buildFormGroups();
+        
         $this->buildListFields();
+
     }
 
     public function getClass()
@@ -57,9 +62,9 @@ abstract class Admin extends ContainerAware
 
     public function getClassMetaData()
     {
-        $em             = $this->getEntityManager();
 
-        return $em->getClassMetaData($this->getClass());
+        return $this->getEntityManager()
+            ->getClassMetaData($this->getClass());
     }
 
     public function getBatchActions()
@@ -207,6 +212,18 @@ abstract class Admin extends ContainerAware
             ->find($this->getClass(), $id);
     }
 
+    public function buildFormGroups()
+    {
+
+        if(!$this->form_groups) {
+            $this->form_groups = array(
+                false => array('fields' => array_keys($this->form_fields))
+            );
+
+            return;
+        }
+    }
+
     /**
      * build the fields to use in the form
      *
@@ -579,4 +596,14 @@ abstract class Admin extends ContainerAware
     {
         return $this->max_per_page;
     }
+
+    public function setFormGroups($form_groups)
+    {
+        $this->form_groups = $form_groups;
+    }
+
+    public function getFormGroups()
+    {
+        return $this->form_groups;
+    }
 }

+ 2 - 0
Controller/CRUDController.php

@@ -124,6 +124,7 @@ class CRUDController extends Controller
             'form'   => $form,
             'object' => $object,
             'fields' => $fields,
+            'form_groups'    => $this->configuration->getFormGroups(),
             'configuration'  => $this->configuration,
             'base_template'  => $this->getBaseTemplate(),
         ));
@@ -222,6 +223,7 @@ class CRUDController extends Controller
             'form'   => $form,
             'object' => $object,
             'fields' => $fields,
+            'form_groups'    => $this->configuration->getFormGroups(),
             'configuration'     => $this->configuration,
             'base_template'     => $this->getBaseTemplate(),
         ));

+ 15 - 0
Resources/public/base.js

@@ -1,6 +1,7 @@
 jQuery(document).ready(function() {
 
     BaseApplication.add_pretty_errors(document);
+    BaseApplication.add_collapsed_toggle(document);
 
 });
 
@@ -42,5 +43,19 @@ var BaseApplication = {
                 }
             })
         });
+    },
+
+    add_collapsed_toggle: function(subject) {
+        jQuery('fieldset legend a.sonata-ba-collapsed', subject).click(function(event) {
+            event.preventDefault();
+
+            var fieldset = jQuery(this).closest('fieldset');
+            
+            jQuery('div.sonata-ba-collapsed-fields', fieldset).toggle();
+            fieldset.toggleClass('sonata-ba-collapsed-fields-close');
+        });
+
+         jQuery('fieldset legend a.sonata-ba-collapsed', subject).trigger('click');
+        
     }
 }

+ 22 - 2
Resources/views/CRUD/base_edit.twig

@@ -35,9 +35,29 @@ file that was distributed with this source code.
     <form action="{{ configuration.generateUrl('update', {'id': object.id}) }}" method="POST">
 
         {{ form|render_hidden }}
+        
+        {% for name, form_group in form_groups %}
+            {% if name %}
+                <fieldset>
+                    <legend>
+                        {{ name }}
+                        {% if form_group.collapsed %}
+                            <a href="" class="sonata-ba-collapsed">{% trans 'link_expand' from 'BaseApplicationBundle' %}</a>
+                        {% endif %}
+                    </legend>
 
-        {% for field in fields %}
-            {{ field|render_form_element(form, object, {'configuration' : configuration}) }}
+                    <div class="sonata-ba-collapsed-fields">
+            {% endif %}
+
+
+            {% for field_name in form_group.fields %}
+                {{ fields[field_name]|render_form_element(form, object, {'configuration' : configuration}) }}
+            {% endfor %}
+
+            {% if name %}
+                    </div>
+                </fieldset>
+            {% endif %}
         {% endfor %}
 
         {% if object.id %}

+ 4 - 2
Resources/views/CRUD/edit_many_association_script.twig

@@ -92,7 +92,8 @@ This code manage the many-to-[one|many] association field popup
                 field_dialog_{{ configuration.code }}_{{ field_element.id }}.html(html);
 
                 BaseApplication.add_pretty_errors(field_dialog_{{ configuration.code }}_{{ field_element.id }});
-                
+                BaseApplication.add_collapsed_toggle(field_dialog_{{ configuration.code }}_{{ field_element.id }});
+
                 // reattach the event
                 jQuery('form', field_dialog_{{ configuration.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }});
             }
@@ -128,5 +129,6 @@ This code manage the many-to-[one|many] association field popup
     }
 
     BaseApplication.add_pretty_errors(field_dialog_{{ configuration.code }}_{{ field_element.id }});
-
+    BaseApplication.add_collapsed_toggle(field_dialog_{{ configuration.code }}_{{ field_element.id }});
+    
 </script>