Przeglądaj źródła

Fix filter binding, clean up some codes

Thomas Rabaix 14 lat temu
rodzic
commit
e44feceb42

+ 30 - 27
Admin/Admin.php

@@ -490,6 +490,35 @@ abstract class Admin implements AdminInterface
         foreach ($this->filterFieldDescriptions as $fieldDescription) {
             $this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
         }
+
+        $parameters = array();
+        // build the values array
+        if ($this->hasRequest()) {
+            $parameters = array_merge(
+                $this->getModelManager()->getDefaultSortValues($this->getClass()),
+                $this->datagridValues,
+                $this->request->query->all()
+            );
+
+            // always force the parent value
+            if ($this->isChild() && $this->getParentAssociationMapping()) {
+                $parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
+            }
+        }
+
+        // initialize the datagrid
+        $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
+        $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
+
+        $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
+
+        // build the datagrid filter
+        $this->buildFilterFieldDescriptions();
+        $this->configureDatagridFilters($mapper);
+
+        foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
+            $mapper->add($fieldDescription);
+        }
     }
 
     /**
@@ -1001,33 +1030,7 @@ abstract class Admin implements AdminInterface
      */
     public function getDatagrid()
     {
-        if (!$this->datagrid) {
-            // build the values array
-            $parameters = array_merge(
-                $this->getModelManager()->getDefaultSortValues($this->getClass()),
-                $this->datagridValues,
-                $this->request->query->all()
-            );
-
-            // always force the parent value
-            if ($this->isChild() && $this->getParentAssociationMapping()) {
-                $parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
-            }
-
-            // initialize the datagrid
-            $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
-            $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
-
-            $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
-
-            // build the datagrid filter
-            $this->buildFilterFieldDescriptions();
-            $this->configureDatagridFilters($mapper);
-
-            foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
-                $mapper->add($fieldDescription);
-            }
-        }
+        $this->buildFilterFieldDescriptions();
 
         return $this->datagrid;
     }

+ 1 - 0
Builder/ORM/DatagridBuilder.php

@@ -137,6 +137,7 @@ class DatagridBuilder implements DatagridBuilderInterface
     public function getChoices(FieldDescriptionInterface $fieldDescription)
     {
         $targets = $fieldDescription->getAdmin()->getModelManager()
+            ->getEntityManager()
             ->createQueryBuilder()
             ->select('t')
             ->from($fieldDescription->getTargetEntity(), 't')

+ 4 - 1
CHANGES

@@ -1,8 +1,11 @@
+01/04/2011
+----------
+* migrate to the new form framework
+
 03/03/2011
 ----------
 * add sortable option
 
-
  08/02/2011
 ----------
 * add prototype for nested admin

+ 1 - 1
Datagrid/Datagrid.php

@@ -71,7 +71,7 @@ class Datagrid implements DatagridInterface
         foreach ($this->getFilters() as $name => $filter) {
             $value = isset($this->values[$name]) ? $this->values[$name] : null;
 
-            $filter->getField()->setData($value);
+            $filter->getField()->bind($value);
             $filter->apply($this->query, $value);
         }
 

+ 5 - 7
Datagrid/DatagridMapper.php

@@ -39,17 +39,15 @@ class DatagridMapper
      * @throws \RuntimeException
      * @param string $name
      * @param array $fieldDescriptionOptions
-     * @return \Sonata\AdminBundle\Datagrid\FilterInterface
+     * @return \Sonata\AdminBundle\Datagrid\DatagridMapper
      */
     public function add($name, array $fieldDescriptionOptions = array())
     {
         if ($name instanceof FieldDescriptionInterface) {
-
             $fieldDescription = $name;
             $fieldDescription->mergeOptions($fieldDescriptionOptions);
 
         } else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {
-
             $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
                 $this->admin->getClass(),
                 $name,
@@ -57,22 +55,22 @@ class DatagridMapper
             );
 
             $this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
-            $this->admin->addListFieldDescription($name, $fieldDescription);
+            $this->admin->addFilterFieldDescription($name, $fieldDescription);
 
         } else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {
-
             $fieldDescription = $this->admin->getFormFieldDescription($name);
 
         } else {
-
             throw new \RuntimeException('invalid state');
         }
 
         // add the field with the FormBuilder
-        return $this->datagridBuilder->addFilter(
+        $this->datagridBuilder->addFilter(
             $this->datagrid,
             $fieldDescription
         );
+
+        return $this;
     }
 
     /**

+ 4 - 2
Datagrid/ListMapper.php

@@ -39,7 +39,7 @@ class ListMapper
      * @throws \RuntimeException
      * @param string $name
      * @param array $fieldDescriptionOptions
-     * @return
+     * @return \Sonata\AdminBundle\Datagrid\ListMapper
      */
     public function add($name, array $fieldDescriptionOptions = array())
     {
@@ -67,10 +67,12 @@ class ListMapper
         }
 
         // add the field with the FormBuilder
-        return $this->listBuilder->addField(
+        $this->listBuilder->addField(
             $this->list,
             $fieldDescription
         );
+
+        return $this;
     }
 
     /**

+ 1 - 1
Filter/Filter.php

@@ -45,7 +45,7 @@ abstract class Filter implements FilterInterface
     public function getField()
     {
         if (!$this->field) {
-            throw new \RuntimeException('No field attached');
+            throw new \RuntimeException(sprintf('No field instance attached for the filter `%s`', $this->name));
         }
 
         return $this->field;

+ 2 - 2
Filter/ORM/BooleanFilter.php

@@ -62,11 +62,11 @@ class BooleanFilter extends Filter
                 'true'  => 'true',
                 'false' => 'false'
             ),
-            'required' => false
+            'required' => true
         );
 
         $options = array_merge($options, $this->getFieldDescription()->getOption('filter_field_options', array()));
 
-        $this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options);
+        $this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options)->getForm();
     }
 }

+ 4 - 2
Filter/ORM/CallbackFilter.php

@@ -23,7 +23,7 @@ class CallbackFilter extends Filter
 
     public function filter($queryBuilder, $alias, $field, $value)
     {
-        if (is_callable($this->getOption('filter'))) {
+        if (!is_callable($this->getOption('filter'))) {
             throw new \RuntimeException('Please provide a valid callback option');
         }
 
@@ -51,6 +51,8 @@ class CallbackFilter extends Filter
 
     public function defineFieldBuilder(FormFactory $formFactory)
     {
-        return $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null);
+        $options = $this->getFieldDescription()->getOption('filter_field_options', array());
+
+        $this->field = $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null, $options)->getForm();
     }
 }

+ 5 - 1
Filter/ORM/ChoiceFilter.php

@@ -18,6 +18,10 @@ class ChoiceFilter extends Filter
 {
     public function filter($queryBuilder, $alias, $field, $value)
     {
+        if (!is_array($value)) {
+            return;
+        }
+
         if ($this->getField()->getAttribute('multiple')) {
             if (in_array('all', $value)) {
                 return;
@@ -52,6 +56,6 @@ class ChoiceFilter extends Filter
     {
         $options = $this->getFieldDescription()->getOption('filter_field_options', array('required' => false));
 
-        $this->field = $formFactory->createNamedBuilder('choice', $this->getName(), $value, $options);
+        $this->field = $formFactory->createNamedBuilder('choice', $this->getName(), $value, $options)->getForm();
     }
 }

+ 1 - 1
Filter/ORM/IntegerFilter.php

@@ -45,6 +45,6 @@ class IntegerFilter extends Filter
    {
        $options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
 
-       $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
+       $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
    }
 }

+ 1 - 1
Filter/ORM/StringFilter.php

@@ -46,6 +46,6 @@ class StringFilter extends Filter
    {
        $options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
 
-       $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
+       $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
    }
 }

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

@@ -19,7 +19,7 @@ file that was distributed with this source code.
         <br />
     {% endblock %}
 
-    <div class="sonata-ba-field{% if filter_form.vars.errors %} sonata-ba-field-error{% endif %}">
+    <div class="sonata-ba-field{% if filter_form.vars.errors %} sonata-ba-field-error"{% endif %}">
         {% block field %}{{ form_widget(filter_form) }}{% endblock %}
     </div>
 </div>

+ 3 - 34
Twig/Extension/SonataAdminExtension.php

@@ -18,8 +18,6 @@ use Symfony\Component\Form\FormView;
 class SonataAdminExtension extends \Twig_Extension
 {
 
-    protected $templating;
-
     /**
      * {@inheritdoc}
      */
@@ -44,10 +42,7 @@ class SonataAdminExtension extends \Twig_Extension
 
     public function getTokenParsers()
     {
-
-        return array(
-
-        );
+        return array();
     }
 
     /**
@@ -70,7 +65,6 @@ class SonataAdminExtension extends \Twig_Extension
      */
     public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
     {
-
         $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
 
         return $this->output($fieldDescription, $template->render(array_merge($params, array(
@@ -84,7 +78,6 @@ class SonataAdminExtension extends \Twig_Extension
 
     public function output(FieldDescriptionInterface $fieldDescription, $content)
     {
-
         return sprintf("\n<!-- fieldName: %s, template: %s -->\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content);
     }
 
@@ -99,10 +92,8 @@ class SonataAdminExtension extends \Twig_Extension
      */
     public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
     {
-
         if (isset($params['loop']) && $object instanceof \ArrayAccess) {
             throw new \RuntimeException('remove the loop requirement');
-//            $object = $object[$params['loop']['index0']];
         }
 
         $value = $fieldDescription->getValue($object);
@@ -111,7 +102,6 @@ class SonataAdminExtension extends \Twig_Extension
         // if so, create an empty object instance
         // fixme: not sure this is the best place to do that
         if (!$value && $fieldDescription->getAssociationAdmin()) {
-
             $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
         }
 
@@ -121,7 +111,7 @@ class SonataAdminExtension extends \Twig_Extension
     /**
      * render a filter element
      *
-     * @param Filter $filter
+     * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
      * @param array $params
      * @return
      */
@@ -133,7 +123,7 @@ class SonataAdminExtension extends \Twig_Extension
 
         return $template->render(array_merge($params, array(
             'filter'        => $filter,
-            'filter_form'   => $filter->getField()->getForm()->createView()
+            'filter_form'   => $filter->getField()->createView()
         )));
     }
 
@@ -192,26 +182,5 @@ class SonataAdminExtension extends \Twig_Extension
             'base_template'     => $fieldDescription->getOption('base_template', $base_template)
         ))));
     }
-
-    /**
-     * set the templating engine
-     *
-     * @param  $templating
-     * @return void
-     */
-    public function setTemplating($templating)
-    {
-        $this->templating = $templating;
-    }
-
-    /**
-     * return the templating engine
-     *
-     * @return Engine
-     */
-    public function getTemplating()
-    {
-        return $this->templating;
-    }
 }