ソースを参照

Merge branch 'refactoring' of git://github.com/bschussek/BaseApplicationBundle

Thomas 14 年 前
コミット
80f8b8f0f9

+ 99 - 58
Admin/Admin.php

@@ -33,7 +33,7 @@ abstract class Admin extends ContainerAware
     protected $baseRouteName;
 
     protected $baseRoutePattern;
-    
+
     protected $baseControllerName;
 
     protected $formGroups = false;
@@ -54,7 +54,7 @@ abstract class Admin extends ContainerAware
     protected $urls = array();
 
     protected $subject;
-    
+
     /**
      * Reference the parent FieldDescription related to this admin
      * only set for FieldDescription which is associated to an Sub Admin instance
@@ -70,28 +70,6 @@ abstract class Admin extends ContainerAware
         'urls'        => false,
     );
 
-    /**
-     * todo: put this in the DIC
-     *
-     * @var array
-     */
-    protected $formFieldClasses = array(
-        'string'     =>  'Symfony\\Component\\Form\\TextField',
-        'text'       =>  'Symfony\\Component\\Form\\TextareaField',
-        'boolean'    =>  'Symfony\\Component\\Form\\CheckboxField',
-        'integer'    =>  'Symfony\\Component\\Form\\IntegerField',
-        'tinyint'    =>  'Symfony\\Component\\Form\\IntegerField',
-        'smallint'   =>  'Symfony\\Component\\Form\\IntegerField',
-        'mediumint'  =>  'Symfony\\Component\\Form\\IntegerField',
-        'bigint'     =>  'Symfony\\Component\\Form\\IntegerField',
-        'decimal'    =>  'Symfony\\Component\\Form\\NumberField',
-        'datetime'   =>  'Symfony\\Component\\Form\\DateTimeField',
-        'date'       =>  'Symfony\\Component\\Form\\DateField',
-        'choice'     =>  'Symfony\\Component\\Form\\ChoiceField',
-        'array'      =>  'Symfony\\Component\\Form\\FieldGroup',
-        'country'    =>  'Symfony\\Component\\Form\\CountryField',
-    );
-
     protected $choicesCache = array();
 
     /**
@@ -101,14 +79,6 @@ abstract class Admin extends ContainerAware
      */
     abstract public function getEntityManager();
 
-    /**
-     * build the fields to use in the form
-     *
-     * @throws RuntimeException
-     * @return
-     */
-    abstract protected function buildFormFields();
-
     /**
      * build the field to use in the list view
      *
@@ -118,16 +88,23 @@ abstract class Admin extends ContainerAware
 
     abstract protected function getChoices(FieldDescription $description);
 
-    abstract public function getForm($object, $fields);
+    public function getForm($object)
+    {
+        $form = $this->getBaseForm($object);
+
+        $this->configureFormFields($form);
+
+        return $form;
+    }
 
     public function configure()
     {
 
     }
-    
+
     /**
      * return the baseRoutePattern used to generate the routing information
-     * 
+     *
      * @throws RuntimeException
      * @return string the baseRoutePattern used to generate the routing information
      */
@@ -177,7 +154,7 @@ abstract class Admin extends ContainerAware
 
     public function urlize($word, $sep = '_')
     {
-        
+
         return strtolower(preg_replace('~(?<=\\w)([A-Z])~', $sep.'$1', $word));
     }
 
@@ -193,7 +170,7 @@ abstract class Admin extends ContainerAware
 
     /**
      * return the doctrine class metadata handled by the Admin instance
-     * 
+     *
      * @return ClassMetadataInfo the doctrine class metadata handled by the Admin instance
      */
     public function getClassMetaData()
@@ -293,7 +270,7 @@ abstract class Admin extends ContainerAware
 
     public function configureUrls()
     {
-        
+
     }
 
     /**
@@ -375,9 +352,9 @@ abstract class Admin extends ContainerAware
      */
     public function getBaseForm($object)
     {
-        $this->container->get('session')->start();
+        // TODO $this->formOptions
 
-        return new Form('data', $object, $this->container->get('validator'), $this->formOptions);
+        return $this->container->get('form.factory')->getForm('data', $object);
     }
 
 
@@ -405,12 +382,12 @@ abstract class Admin extends ContainerAware
      */
     public function getObject($id)
     {
-        
+
         return $this->getEntityManager()
             ->find($this->getClass(), $id);
     }
 
-    public function buildFormGroups()
+    public function buildFormGroups(Form $form)
     {
 
         if ($this->loaded['form_groups']) {
@@ -418,11 +395,11 @@ abstract class Admin extends ContainerAware
         }
 
         $this->loaded['form_groups'] = true;
-                
+
 
         if (!$this->formGroups) {
             $this->formGroups = array(
-                false => array('fields' => array_keys($this->formFields))
+                false => array('fields' => array_keys($this->getFormFields($form)))
             );
         }
 
@@ -451,7 +428,7 @@ abstract class Admin extends ContainerAware
 
     public function postInsert($object)
     {
-        
+
     }
 
     public function configureListFields()
@@ -461,14 +438,11 @@ abstract class Admin extends ContainerAware
 
     public function configureFilterFields()
     {
-        
-    }
-
-    public function configureFormFields()
-    {
 
     }
 
+    abstract protected function configureFormFields(Form $form);
+
     public function getFilterDatagrid()
     {
         if (!$this->filterDatagrid) {
@@ -490,7 +464,7 @@ abstract class Admin extends ContainerAware
 
             // set the final value to the datagrid
             $this->filterDatagrid->setFilterFields($this->filterFields);
-            
+
         }
 
         return $this->filterDatagrid;
@@ -526,17 +500,84 @@ abstract class Admin extends ContainerAware
      *
      * @return list form field definition
      */
-    public function getFormFields()
+    public function getFormFields(Form $form)
     {
-        $this->buildFormFields();
+        $fields = array();
+
+        foreach ($form as $fieldName => $field) {
+            $fields[$fieldName] = new FieldDescription();
+            $fields[$fieldName]->setFieldName($fieldName);
+            $fields[$fieldName]->setAdmin($this);
+            $fields[$fieldName]->setTemplate('SonataBaseApplicationBundle:CRUD:edit_text.twig.html');
+
+            // TODO: set description properties
+        }
+
+        /*
+        $this->formFields = self::getBaseFields($form);
+
+        foreach ($this->formFields as $name => $fieldDescription) {
+
+            if (!$fieldDescription->getType()) {
+                throw new \RuntimeException(sprintf('You must declare a type for the field `%s`', $name));
+            }
+
+            $fieldDescription->setAdmin($this);
+            $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
+
+            // fix template value for doctrine association fields
+            if (!$fieldDescription->getTemplate()) {
+
+                $fieldDescription->setTemplate(sprintf('SonataBaseApplicationBundle:CRUD:edit_%s.twig.html', $fieldDescription->getType()));
 
-        return $this->formFields;
+                if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_ONE) {
+                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_one_to_one.twig.html');
+                    $this->attachAdminClass($fieldDescription);
+                }
+
+                if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_ONE) {
+                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_many_to_one.twig.html');
+                    $this->attachAdminClass($fieldDescription);
+                }
+
+                if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_MANY) {
+                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_many_to_many.twig.html');
+                    $this->attachAdminClass($fieldDescription);
+                }
+
+                if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_MANY) {
+                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_one_to_many.twig.html');
+
+                    if($fieldDescription->getOption('edit') == 'inline' && !$fieldDescription->getOption('widget')) {
+                        $fieldDescription->setOption('widget', 'Bundle\\Sonata\\BaseApplicationBundle\\Form\\EditableGroupField');
+                    }
+
+                    $this->attachAdminClass($fieldDescription);
+                }
+            }
+
+            // set correct default value
+            if ($fieldDescription->getType() == 'datetime') {
+                $options = $fieldDescription->getOption('form_fields', array());
+                if (!isset($options['years'])) {
+                    $options['years'] = range(1900, 2100);
+                }
+                $fieldDescription->setOption('form_field', $options);
+            }
+
+            // unset the identifier field as it is not required to update an object
+            if ($fieldDescription->isIdentifier()) {
+                unset($this->formFields[$name]);
+            }
+        */
+
+        return $fields;
     }
 
     public function getListFields()
     {
         $this->buildListFields();
-        
+
         return $this->listFields;
     }
 
@@ -605,10 +646,10 @@ abstract class Admin extends ContainerAware
         $this->formGroups = $formGroups;
     }
 
-    public function getFormGroups()
+    public function getFormGroups(Form $form)
     {
-        $this->buildFormGroups();
-        
+        $this->buildFormGroups($form);
+
         return $this->formGroups;
     }
 

+ 55 - 110
Admin/EntityAdmin.php

@@ -20,7 +20,7 @@ use Sonata\BaseApplicationBundle\Tool\Datagrid;
 
 abstract class EntityAdmin extends Admin
 {
-    
+
     /**
      * make sure the base fields are set in the correct format
      *
@@ -55,7 +55,7 @@ abstract class EntityAdmin extends Admin
                     $description->setTemplate($options['template']);
                     unset($options['template']);
                 }
-                
+
                 $description->setOptions($options);
             } else {
                 $name = $options;
@@ -87,83 +87,6 @@ abstract class EntityAdmin extends Admin
         return $this->container->get('doctrine.orm.default_entity_manager');
     }
 
-    /**
-     * build the fields to use in the form
-     *
-     * @throws RuntimeException
-     * @return
-     */
-    protected function buildFormFields()
-    {
-
-        if ($this->loaded['form_fields']) {
-            return;
-        }
-
-        $this->loaded['form_fields'] = true;
-        
-        $this->formFields = self::getBaseFields($this->getClassMetaData(), $this->formFields);
-
-        foreach ($this->formFields as $name => $fieldDescription) {
-
-            if (!$fieldDescription->getType()) {
-                throw new \RuntimeException(sprintf('You must declare a type for the field `%s`', $name));
-            }
-
-            $fieldDescription->setAdmin($this);
-            $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
-
-            // fix template value for doctrine association fields
-            if (!$fieldDescription->getTemplate()) {
-
-                $fieldDescription->setTemplate(sprintf('SonataBaseApplicationBundle:CRUD:edit_%s.twig.html', $fieldDescription->getType()));
-                
-                if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_ONE) {
-                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_one_to_one.twig.html');
-                    $this->attachAdminClass($fieldDescription);
-                }
-
-                if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_ONE) {
-                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_many_to_one.twig.html');
-                    $this->attachAdminClass($fieldDescription);
-                }
-
-                if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_MANY) {
-                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_many_to_many.twig.html');
-                    $this->attachAdminClass($fieldDescription);
-                }
-
-                if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_MANY) {
-                    $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:edit_one_to_many.twig.html');
-
-                    if($fieldDescription->getOption('edit') == 'inline' && !$fieldDescription->getOption('widget')) {
-                        $fieldDescription->setOption('widget', 'Bundle\\Sonata\\BaseApplicationBundle\\Form\\EditableGroupField');
-                    }
-
-                    $this->attachAdminClass($fieldDescription);
-                }
-            }
-            
-            // set correct default value
-            if ($fieldDescription->getType() == 'datetime') {
-                $options = $fieldDescription->getOption('form_fields', array());
-                if (!isset($options['years'])) {
-                    $options['years'] = range(1900, 2100);
-                }
-                $fieldDescription->setOption('form_field', $options);
-            }
-
-            // unset the identifier field as it is not required to update an object
-            if ($fieldDescription->isIdentifier()) {
-                unset($this->formFields[$name]);
-            }
-        }
-
-        $this->configureFormFields();
-
-        return $this->formFields;
-    }
-
     /**
      * build the field to use in the list view
      *
@@ -177,7 +100,7 @@ abstract class EntityAdmin extends Admin
         }
 
         $this->loaded['list_fields'] = true;
-        
+
         $this->listFields = self::getBaseFields($this->getClassMetaData(), $this->listFields);
 
         // normalize field
@@ -220,7 +143,7 @@ abstract class EntityAdmin extends Admin
         }
 
         $this->configureListFields();
-        
+
         if (!isset($this->listFields['_batch'])) {
             $fieldDescription = new FieldDescription();
             $fieldDescription->setOptions(array(
@@ -242,6 +165,9 @@ abstract class EntityAdmin extends Admin
      */
     protected function getChoices(FieldDescription $description, $prependChoices = array())
     {
+        /*
+
+        UNUSED RIGHT NOW
 
         if (!isset($this->choicesCache[$description->getTargetEntity()])) {
             $targets = $this->getEntityManager()
@@ -266,6 +192,8 @@ abstract class EntityAdmin extends Admin
         }
 
         return $prependChoices + $this->choicesCache[$description->getTargetEntity()];
+
+        */
     }
 
     /**
@@ -280,14 +208,18 @@ abstract class EntityAdmin extends Admin
      */
     protected function getRelatedAssociatedField($object, FieldDescription $fieldDescription, $fieldName = null)
     {
+        /*
+
+        UNUSED RIGHT NOW
+
         $fieldName = $fieldName ?: $fieldDescription->getFieldName();
-        
+
         $associatedAdmin = $fieldDescription->getAssociationAdmin();
-        
+
         if (!$associatedAdmin) {
             throw new \RuntimeException(sprintf('inline mode for field `%s` required an Admin definition', $fieldName));
         }
-        
+
         // retrieve the related object
         $targetObject = $associatedAdmin->getNewInstance();
 
@@ -312,7 +244,7 @@ abstract class EntityAdmin extends Admin
                 'value_transformer' => $transformer,
             ));
         }
-        
+
         foreach ($targetForm->getFields() as $name => $formField) {
             if ($name == '_token') {
                 continue;
@@ -322,6 +254,8 @@ abstract class EntityAdmin extends Admin
         }
 
         return $field;
+
+        */
     }
 
 
@@ -334,6 +268,9 @@ abstract class EntityAdmin extends Admin
      */
     public function getFormFieldClass(FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
 
         $class = isset($this->formFieldClasses[$fieldDescription->getType()]) ? $this->formFieldClasses[$fieldDescription->getType()] : false;
 
@@ -348,6 +285,8 @@ abstract class EntityAdmin extends Admin
         }
 
         return $class;
+
+        */
     }
 
     /**
@@ -359,12 +298,18 @@ abstract class EntityAdmin extends Admin
      */
     public function addNewInstance($object, FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
+
         $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
         $mapping  = $fieldDescription->getAssociationMapping();
 
         $method = sprintf('add%s', FieldDescription::camelize($mapping['fieldName']));
 
         $object->$method($instance);
+
+        */
     }
 
     /**
@@ -376,6 +321,9 @@ abstract class EntityAdmin extends Admin
      */
     protected function getOneToOneField($object, FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
 
         // tweak the widget depend on the edit mode
         if ($fieldDescription->getOption('edit') == 'inline') {
@@ -416,6 +364,8 @@ abstract class EntityAdmin extends Admin
         }
 
         return new $class($fieldDescription->getFieldName(), $options);
+
+        */
     }
 
     /**
@@ -427,6 +377,9 @@ abstract class EntityAdmin extends Admin
      */
     protected function getOneToManyField($object, FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
 
         if ($fieldDescription->getOption('edit') == 'inline') {
             $prototype = $this->getRelatedAssociatedField($object, $fieldDescription);
@@ -447,10 +400,15 @@ abstract class EntityAdmin extends Admin
         }
 
         return $this->getManyToManyField($object, $fieldDescription);
+
+        */
     }
 
     protected function getManyToManyField($object, FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
 
         $options = array(
             'value_transformer' => new \Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\CollectionToChoiceTransformer(array(
@@ -481,10 +439,16 @@ abstract class EntityAdmin extends Admin
         }
 
         return new $class($fieldDescription->getFieldName(), $options);
+
+        */
     }
 
     protected function getManyToOneField($object, FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
+
                 // tweak the widget depend on the edit mode
         if ($fieldDescription->getOption('edit') == 'inline') {
 
@@ -524,10 +488,15 @@ abstract class EntityAdmin extends Admin
         }
 
         return new $class($fieldDescription->getFieldName(), $options);
+
+        */
     }
 
     protected function getFormFieldInstance($object, FieldDescription $fieldDescription)
     {
+        /*
+
+        UNUSED RIGHT NOW
 
         switch ($fieldDescription->getType()) {
 
@@ -552,31 +521,7 @@ abstract class EntityAdmin extends Admin
 
                 return new $class($fieldDescription->getFieldName(), $options);
         }
-    }
-    
-    /**
-     * return a form depend on the given $object and FieldDescription $fields array
-     *
-     * @throws RuntimeException
-     * @param  $object
-     * @param  $fields
-     * @return Symfony\Component\Form\Form
-     */
-    public function getForm($object, $fields)
-    {
-
-        $form = $this->getBaseForm($object);
-
-        foreach ($fields as $fieldDescription) {
-
-            if (!$fieldDescription->getType()) {
-
-                continue;
-            }
-
-            $form->add($this->getFormFieldInstance($object, $fieldDescription));
-        }
 
-        return $form;
+        */
     }
 }

+ 15 - 30
Controller/CRUDController.php

@@ -57,7 +57,7 @@ class CRUDController extends Controller
     }
 
     public function getBaseTemplate()
-    {        
+    {
         if ($this->get('request')->isXmlHttpRequest()) {
             return $this->container->getParameter('base_application.templates.ajax');
         }
@@ -92,7 +92,7 @@ class CRUDController extends Controller
             ->add('where', $query_builder->expr()->in('o.id', $idx))
             ->getQuery()
             ->execute();
-        
+
         foreach ($objects as $object) {
             $em->remove($object);
         }
@@ -110,11 +110,6 @@ class CRUDController extends Controller
 
     public function editAction($id)
     {
-
-        $this->get('session')->start();
-
-        $fields = $this->admin->getFormFields();
-
         if ($id instanceof Form) {
             $object = $id->getData();
             $form   = $id;
@@ -125,7 +120,7 @@ class CRUDController extends Controller
                 throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
             }
 
-            $form   = $this->admin->getForm($object, $fields);
+            $form = $this->admin->getForm($object);
         }
 
         $this->admin->setSubject($object);
@@ -133,8 +128,8 @@ class CRUDController extends Controller
         return $this->render($this->admin->getEditTemplate(), array(
             'form'           => $form,
             'object'         => $object,
-            'fields'         => $fields,
-            'form_groups'    => $this->admin->getFormGroups(),
+            'fields'         => $this->admin->getFormFields($form),
+            'form_groups'    => $this->admin->getFormGroups($form),
             'admin'          => $this->admin,
             'base_template'  => $this->getBaseTemplate(),
         ));
@@ -142,9 +137,6 @@ class CRUDController extends Controller
 
     public function updateAction()
     {
-
-        $this->get('session')->start();
-
         if ($this->get('request')->getMethod() != 'POST') {
            throw new \RuntimeException('invalid request type, POST expected');
         }
@@ -165,9 +157,7 @@ class CRUDController extends Controller
             $action = 'create';
         }
 
-        $fields = $this->admin->getFormFields();
-        $form   = $this->admin->getForm($object, $fields);
-
+        $form = $this->admin->getForm($object);
         $form->bind($this->get('request')->get('data'));
 
         if ($form->isValid()) {
@@ -177,7 +167,7 @@ class CRUDController extends Controller
             } else {
                 $this->admin->preUpdate($object);
             }
-            
+
             $this->admin->getEntityManager()->persist($object);
             $this->admin->getEntityManager()->flush($object);
 
@@ -217,7 +207,7 @@ class CRUDController extends Controller
         if ($this->get('request')->get('btn_create_and_create')) {
             $url = $this->admin->generateUrl('create');
         }
-        
+
         if (!$url) {
             $url = $this->admin->generateUrl('edit', array('id' => $object->getId()));
         }
@@ -251,28 +241,23 @@ class CRUDController extends Controller
 
     public function createAction($id = null)
     {
-        $this->get('session')->start();
-
-        $fields = $this->admin->getFormFields();
-
         if ($id instanceof Form) {
             $object = $id->getData();
-            $form   = $id;
+            $form = $id;
         } else {
             $object = $this->admin->getNewInstance();
-
-            $form   = $this->admin->getForm($object, $fields);
+            $form = $this->admin->getForm($object);
         }
 
         $this->admin->setSubject($object);
 
         return $this->render($this->admin->getEditTemplate(), array(
-            'form'   => $form,
+            'form' => $form,
             'object' => $object,
-            'fields' => $fields,
-            'form_groups'    => $this->admin->getFormGroups(),
-            'admin'     => $this->admin,
-            'base_template'     => $this->getBaseTemplate(),
+            'fields' => $this->admin->getFormFields($form),
+            'form_groups' => $this->admin->getFormGroups($form),
+            'admin' => $this->admin,
+            'base_template' => $this->getBaseTemplate(),
         ));
     }
 

+ 10 - 1
Controller/CoreController.php

@@ -18,6 +18,14 @@ use Symfony\Component\Form\RecursiveFieldIterator;
 
 class CoreController extends Controller
 {
+    public function getBaseTemplate()
+    {        
+        if ($this->get('request')->isXmlHttpRequest()) {
+            return $this->container->getParameter('base_application.templates.ajax');
+        }
+
+        return $this->container->getParameter('base_application.templates.layout');
+    }
 
     public function retrieveFormFieldElementAction($code, $element_id)
     {
@@ -176,7 +184,8 @@ class CoreController extends Controller
     {
 
         return $this->render('SonataBaseApplicationBundle:Core:dashboard.twig.html', array(
-            'groups' => $this->get('base_application.admin.pool')->getGroups()
+            'groups' => $this->get('base_application.admin.pool')->getGroups(),
+            'base_template'  => $this->getBaseTemplate(),
         ));
     }
 }

+ 1 - 1
Resources/views/Core/dashboard.twig.html

@@ -9,7 +9,7 @@ file that was distributed with this source code.
 
 #}
 
-{% extends 'SonataBaseApplicationBundle::standard_layout.twig.html' %}
+{% extends base_template %}
 
 {% block title %}{% trans 'title_dashboard' from 'BaseApplicationBundle' %}{% endblock%}
 {% block content %}

+ 3 - 3
Tool/Datagrid.php

@@ -42,7 +42,7 @@ class Datagrid
      * The filter instances
      * @var array
      */
-    protected $filters;
+    protected $filters = array();
 
     protected $values;
 
@@ -216,7 +216,7 @@ class Datagrid
 
                 $options = $fieldDescription->getOption('filter_field_options');
                 $options['choices'] = $this->getChoices($fieldDescription);
-                
+
                 $fieldDescription->setOption('filter_field_options', $options);
 
                 $filter = new ChoiceFilter($name, $fieldDescription);
@@ -249,7 +249,7 @@ class Datagrid
 
     public function configureFilterFields()
     {
-        
+
     }
 
     public function getFilters()