src/App/Form/Forms/SearchForm.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Form\Forms;
  3. use App\Model\Searching\AbstractSearch;
  4. use Products\SchoolNowBundle\Form\Type\SimpleSelectType;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormInterface;
  9. use Symfony\Component\Form\FormView;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. /**
  12.  * Base search form that all other search forms should extend.
  13.  * NOTE: since this is a base class, be sure to call parent methods when overriding anything.
  14.  */
  15. abstract class SearchForm extends AbstractType
  16. {
  17.     /**
  18.      * Override to set a more appropriate search class type.
  19.      */
  20.     protected const DATA_CLASS AbstractSearch::class;
  21.     /**
  22.      * {@inheritDoc}
  23.      */
  24.     public function buildForm(FormBuilderInterface $builder, array $options)
  25.     {
  26.         // form should have a search type assigned to it
  27.         $search $builder->getData();
  28.         if ( ! $search instanceof AbstractSearch) {
  29.             throw new \Exception();
  30.         }
  31.         // all search forms will be get requests (so data comes across in url query string)
  32.         $builder->setMethod('GET');
  33.         // filters are not always used
  34.         if ($search::FILTERS) {
  35.             $builder->add('filter'SimpleSelectType::class, [
  36.                 'empty_data' => $search::FILTERS__DEFAULT,
  37.                 'choices' => $search::FILTERS,
  38.                 'choice_label' => function ($choice$key$value) use ($options) {
  39.                     if ($options['search_filters_label_format']) {
  40.                         return sprintf(
  41.                             $options['search_filters_label_format'],
  42.                             $value,
  43.                         );
  44.                     }
  45.                     return $value;
  46.                 },
  47.             ]);
  48.         }
  49.         // attach fields
  50.         $builder
  51.             ->add('sort'HiddenType::class, [
  52.                 'empty_data' => $search::SORTS__DEFAULT,
  53.             ])
  54.             ->add('direction'HiddenType::class, [
  55.                 'empty_data' => $search::DIRECTIONS[$search::SORTS__DEFAULT],
  56.             ])
  57.         ;
  58.     }
  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     public function buildView(FormView $viewFormInterface $form, array $options)
  63.     {
  64.         // form should have a sort type assigned to it
  65.         $search $form->getData();
  66.         if ( ! $search instanceof AbstractSearch) {
  67.             throw new \Exception();
  68.         }
  69.         // attach information about the sorting so that we can use it in templates
  70.         $view->vars['filters'] = $options['search_filters'];
  71.         $view->vars['sorts'] = $options['search_sorts'];
  72.         $view->vars['dirs'] = $search::DIRECTIONS;
  73.         // legacy
  74.         $view->vars['search_filters'] = $search::FILTERS;
  75.         $view->vars['search_sorts'] = $search::DIRECTIONS;
  76.     }
  77.     /**
  78.      * {@inheritDoc}
  79.      */
  80.     public function configureOptions(OptionsResolver $resolver)
  81.     {
  82.         $resolver->setDefaults([
  83.             'csrf_protection' => false,
  84.             'data_class' => static::DATA_CLASS,
  85.             'search_filters' => defined(static::DATA_CLASS '::FILTERS') ? constant(static::DATA_CLASS '::FILTERS') : [],
  86.             'search_sorts' => array_keys(defined(static::DATA_CLASS '::DIRECTIONS') ? constant(static::DATA_CLASS '::DIRECTIONS') : []),
  87.             'search_filters_label_format' => null,
  88.         ]);
  89.     }
  90.     /**
  91.      * {@inheritDoc}
  92.      */
  93.     public function getBlockPrefix(): string
  94.     {
  95.         // all search forms will use the same naming pattern
  96.         return 'query';
  97.     }
  98. }