Explorar o código

Add boolean type

Thomas Rabaix %!s(int64=13) %!d(string=hai) anos
pai
achega
8884058b3b

+ 0 - 1
Filter/FilterFactory.php

@@ -83,7 +83,6 @@ class FilterFactory implements FilterFactoryInterface
         }
 
         $filter->setFieldDescription($fieldDescription);
-        $options['field_options']['csrf_protection'] = false;
         $options['field_options']['required'] = false;
 
         $filter->initialize($options);

+ 7 - 44
Filter/ORM/BooleanFilter.php

@@ -14,19 +14,10 @@ namespace Sonata\AdminBundle\Filter\ORM;
 use Doctrine\ORM\QueryBuilder;
 use Symfony\Component\Form\FormFactory;
 use Symfony\Component\Translation\TranslatorInterface;
+use Sonata\AdminBundle\Form\Type\Filter\BooleanType;
 
 class BooleanFilter extends Filter
 {
-    protected $translator;
-
-    /**
-     * @param \Symfony\Component\Translation\TranslatorInterface $translator
-     */
-    public function __construct(TranslatorInterface $translator)
-    {
-        $this->translator = $translator;
-    }
-
     /**
      * @param QueryBuilder $queryBuilder
      * @param string $alias
@@ -40,55 +31,27 @@ class BooleanFilter extends Filter
 
             $values = array();
             foreach ($value as $v) {
-                if ($v == 'all') {
+                if ($v === null) {
                     return;
                 }
 
-                $values[] = $v == 'true' ? 1 : 0;
+                $values[] = $v ==  ((int)$value == BooleanType::TYPE_YES) ? 1 : 0;
             }
 
             if (count($values) == 0) {
                 return;
             }
 
-            $queryBuilder->andWhere($queryBuilder->expr()->in(sprintf('%s.%s',
-                $alias,
-                $field
-            ), $values));
+            $queryBuilder->andWhere($queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $values));
 
         } else {
 
-            if ($value === null || $value == 'all') {
+            if ($value === null) {
                 return;
             }
 
-            $queryBuilder->andWhere(sprintf('%s.%s = :%s',
-                $alias,
-                $field,
-                $this->getName()
-            ));
-
-            $queryBuilder->setParameter($this->getName(), $value == 'true' ? 1 : 0);
+            $queryBuilder->andWhere(sprintf('%s.%s = :%s', $alias, $field, $this->getName()));
+            $queryBuilder->setParameter($this->getName(), ((int)$value == BooleanType::TYPE_YES) ? 1 : 0);
         }
     }
-
-    /**
-     * @param \Symfony\Component\Form\FormFactory $formFactory
-     * @return void
-     */
-    public function defineFieldBuilder(FormFactory $formFactory)
-    {
-        $options = array(
-            'choices' => array(
-                'all'   => $this->translator->trans('choice_all', array(), 'SonataAdminBundle'),
-                'true'  => $this->translator->trans('choice_true', array(), 'SonataAdminBundle'),
-                'false' => $this->translator->trans('choice_false', array(), 'SonataAdminBundle'),
-            ),
-            'required' => true
-        );
-
-        $options = array_merge($options, $this->getOption('filter_field_options', array()));
-
-        $this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options)->getForm();
-    }
 }

+ 46 - 0
Form/Type/Filter/BooleanType.php

@@ -0,0 +1,46 @@
+<?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\Form\Type\Filter;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormTypeInterface;
+use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\FormView;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Translation\TranslatorInterface;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
+
+class BooleanType extends ChoiceType
+{
+    const TYPE_YES = 1;
+
+    const TYPE_NO = 2;
+
+    protected $translator;
+
+    public function __construct(TranslatorInterface $translator)
+    {
+        $this->translator = $translator;
+    }
+
+    public function getDefaultOptions(array $options)
+    {
+        $options = parent::getDefaultOptions($options);
+
+        $options['choices'] = array(
+            self::TYPE_YES  => $this->translator->trans('label_type_yes', array(), 'SonataAdminBundle'),
+            self::TYPE_NO   => $this->translator->trans('label_type_no', array(), 'SonataAdminBundle'),
+        );
+
+        return $options;
+    }
+}

+ 10 - 7
Guesser/ORM/FilterTypeGuesser.php

@@ -42,12 +42,9 @@ class FilterTypeGuesser implements TypeGuesserInterface
         }
 
         $options = array(
-            'field_type' => false,
-            'field_options' => array(
-                'required' => false,
-                'csrf_protection' => false
-            ),
-            'options' => array(),
+            'field_type'     => false,
+            'field_options'  => array(),
+            'options'        => array(),
         );
 
         list($metadata, $name) = $ret;
@@ -75,7 +72,10 @@ class FilterTypeGuesser implements TypeGuesserInterface
             //case 'array':
             //  return new TypeGuess('Collection', $options, Guess::HIGH_CONFIDENCE);
             case 'boolean':
-                return new TypeGuess('doctrine_orm_checkbox', $options, Guess::HIGH_CONFIDENCE);
+                $options['field_type'] = 'sonata_type_filter_boolean';
+                $options['field_options'] = array();
+
+                return new TypeGuess('doctrine_orm_boolean', $options, Guess::HIGH_CONFIDENCE);
             case 'datetime':
             case 'vardatetime':
             case 'datetimetz':
@@ -89,6 +89,9 @@ class FilterTypeGuesser implements TypeGuesserInterface
             case 'bigint':
             case 'smallint':
                 $options['field_type'] = 'sonata_type_filter_number';
+                $options['field_options'] = array(
+                    'csrf_protection' => false
+                );
 
                 return new TypeGuess('doctrine_orm_number', $options, Guess::MEDIUM_CONFIDENCE);
             case 'string':

+ 2 - 3
Resources/config/doctrine_orm_filter_types.xml

@@ -5,9 +5,8 @@
            xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
 
     <services>
-        <service id="sonata.admin.orm.filter.type.admin" class="Sonata\AdminBundle\Filter\ORM\BooleanFilter">
+        <service id="sonata.admin.orm.filter.type.boolean" class="Sonata\AdminBundle\Filter\ORM\BooleanFilter">
             <tag name="sonata.admin.filter.type" alias="doctrine_orm_boolean" />
-            <argument type="service" id="translator" />
         </service>
 
         <service id="sonata.admin.orm.filter.type.callback" class="Sonata\AdminBundle\Filter\ORM\CallbackFilter">
@@ -26,7 +25,7 @@
             <tag name="sonata.admin.filter.type" alias="doctrine_orm_string" />
         </service>
 
-        <service id="sonata.admin.orm.filter.type.string" class="Sonata\AdminBundle\Filter\ORM\NumberFilter">
+        <service id="sonata.admin.orm.filter.type.number" class="Sonata\AdminBundle\Filter\ORM\NumberFilter">
             <tag name="sonata.admin.filter.type" alias="doctrine_orm_number" />
         </service>
     </services>

+ 6 - 0
Resources/config/form_types.xml

@@ -34,6 +34,12 @@
             <argument type="service" id="translator" />
         </service>
 
+        <service id="sonata.admin.form.filter.type.boolean" class="Sonata\AdminBundle\Form\Type\Filter\BooleanType">
+            <tag name="form.type" alias="sonata_type_filter_boolean" />
+
+            <argument type="service" id="translator" />
+        </service>
+
     </services>
 
 </container>