src/V4/Form/Type/Task/TaskSearchType.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\V4\Form\Type\Task;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use App\Form\Type\AbstractViewOrderAwareType;
  5. use App\Form\Type\Compare\DateCompareType;
  6. use App\Form\Type\SubresourceChoicesTrait;
  7. use App\Listing\Transformer\ListingResponseTransformer;
  8. use App\Model\IriNormalizableInterface;
  9. use App\Model\Traits\TimeStampableFormType;
  10. use App\Model\ViewOrder\ViewOrder;
  11. use App\Security\SecurityConfig;
  12. use App\Service\ApiWebServiceFilterBuilder\ListWithEmptyFilterBuilder;
  13. use App\Service\Cache\CacheManager;
  14. use App\V4\Form\AsyncSubresourceChoicesLoader;
  15. use App\V4\Form\Type\IdSearchTypeTrait;
  16. use App\V4\Form\Type\ManagedByFilterTrait;
  17. use App\V4\Form\Type\SectionNameFilterTrait;
  18. use App\V4\Logger\SentryLogger;
  19. use App\V4\Model\Contact\Contact;
  20. use App\V4\Model\Task\Task;
  21. use App\V4\Model\Task\TaskSearch;
  22. use App\V4\Model\TaskType\TaskType;
  23. use Psr\Cache\CacheException;
  24. use Psr\Cache\InvalidArgumentException;
  25. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  26. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  27. use Symfony\Component\Form\Extension\Core\Type\TextType;
  28. use Symfony\Component\Form\FormBuilderInterface;
  29. use Symfony\Component\Form\FormEvent;
  30. use Symfony\Component\Form\FormEvents;
  31. use Symfony\Component\OptionsResolver\OptionsResolver;
  32. use Symfony\Component\Routing\RouterInterface;
  33. use Symfony\Component\Security\Core\Security;
  34. use Symfony\Contracts\Translation\TranslatorInterface;
  35. class TaskSearchType extends AbstractViewOrderAwareType
  36. {
  37.     use ManagedByFilterTrait;
  38.     use SectionNameFilterTrait;
  39.     use TimeStampableFormType;
  40.     use SubresourceChoicesTrait {
  41.         getEntityChoices as private getEntityChoicesTrait;
  42.     }
  43.     use IdSearchTypeTrait;
  44.     /**
  45.      * @var CollectionDataProviderInterface
  46.      */
  47.     private $collectionDataProvider;
  48.     /**
  49.      * @var CacheManager
  50.      */
  51.     private $cacheManager;
  52.     /**
  53.      * @var Security
  54.      */
  55.     private $security;
  56.     /**
  57.      * @var RouterInterface
  58.      */
  59.     private $router;
  60.     /**
  61.      * @var TranslatorInterface
  62.      */
  63.     private $translator;
  64.     /**
  65.      * @var SentryLogger
  66.      */
  67.     private $sentryLogger;
  68.     /**
  69.      * @param CollectionDataProviderInterface $collectionDataProvider
  70.      * @param CacheManager                    $cacheManager
  71.      * @param Security                        $security
  72.      * @param RouterInterface                 $router
  73.      * @param TranslatorInterface             $translator
  74.      * @param SentryLogger                    $sentryLogger
  75.      */
  76.     public function __construct(
  77.         CollectionDataProviderInterface $collectionDataProvider,
  78.         CacheManager $cacheManager,
  79.         Security $security,
  80.         RouterInterface $router,
  81.         TranslatorInterface $translator,
  82.         SentryLogger $sentryLogger
  83.     ) {
  84.         $this->collectionDataProvider $collectionDataProvider;
  85.         $this->cacheManager $cacheManager;
  86.         $this->security $security;
  87.         $this->router $router;
  88.         $this->translator $translator;
  89.         $this->sentryLogger $sentryLogger;
  90.     }
  91.     /**
  92.      * @param FormBuilderInterface $builder
  93.      * @param array                $options
  94.      *
  95.      * @return void
  96.      *
  97.      * @throws CacheException
  98.      * @throws InvalidArgumentException
  99.      */
  100.     public function buildForm(FormBuilderInterface $builder, array $options): void
  101.     {
  102.         parent::buildForm($builder$options);
  103.         $this->addIdsField($builder);
  104.         $builder
  105.             ->add('name'TextType::class, [
  106.                 'label' => 'task_object',
  107.                 'attr' => [
  108.                     'data-get-url' => $this->router->generate('get_task_names'),
  109.                     'data-field' => 'name',
  110.                     'type' => 'autocomplete',
  111.                 ],
  112.             ])
  113.             ->add('taskType'ChoiceType::class, [
  114.                 'choices' => $this->getEntityChoices(TaskType::class, [], 'name'),
  115.                 'choice_label' => 'name',
  116.                 'choice_value' => 'id',
  117.                 'multiple' => true,
  118.             ])
  119.             ->add('contactsConcerned'ChoiceType::class, [
  120.                 'label' => 'contactsConcerned',
  121.                 'choice_label' => 'name',
  122.                 'choice_value' => 'id',
  123.                 'choice_loader' => new AsyncSubresourceChoicesLoader(
  124.                     $this->collectionDataProvider,
  125.                     $this->cacheManager,
  126.                     $this->sentryLogger,
  127.                     Contact::class
  128.                 ),
  129.                 'multiple' => true,
  130.                 'attr' => [
  131.                     'type' => 'autocomplete',
  132.                     'autocomplete_entity' => 'contact',
  133.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contacts',
  134.                 ],
  135.             ])
  136.             ->add('taskPeriod'ChoiceType::class, [
  137.                 'label' => 'taskPeriod',
  138.                 'choices' => [
  139.                     $this->translator->trans('of_day') => 'day',
  140.                     $this->translator->trans('of_week') => 'week',
  141.                     $this->translator->trans('of_month') => 'month',
  142.                     $this->translator->trans('all') => 'all',
  143.                 ],
  144.             ])
  145.             ->add('createdAt'DateCompareType::class, [
  146.                 'label' => 'createdAt_task',
  147.                 'attr' => [
  148.                     'type' => 'date_compare',
  149.                 ],
  150.             ])
  151.             ->add('beginAt'DateCompareType::class, [
  152.                 'label' => 'beginAt_task',
  153.                 'attr' => [
  154.                     'type' => 'date_compare',
  155.                 ],
  156.             ])
  157.             ->add('updatedAt'DateCompareType::class, [
  158.                 'label' => 'updatedAt_task',
  159.                 'attr' => [
  160.                     'type' => 'date_compare',
  161.                 ],
  162.             ])
  163.             ->add('myTasks'CheckboxType::class, [
  164.                 'false_values' => [null'0'0false'''false'],
  165.                 'data' => $this->security->isGranted(SecurityConfig::MY_TASK_BY_DEFAULT),
  166.             ])
  167.             ->add('managedBy'ChoiceType::class, [
  168.                 'choices' => $this->getManagedByChoices(),
  169.                 'multiple' => true,
  170.             ])
  171.             ->add('closed'ChoiceType::class, [
  172.                 'label' => 'closed',
  173.                 'choices' => [
  174.                     'no' => 'no',
  175.                     'yes' => 'yes',
  176.                     'all' => 'all',
  177.                 ],
  178.                 'attr' => [
  179.                     'is-expanded' => true,
  180.                 ],
  181.             ])
  182.             ->add('readState'ChoiceType::class, [
  183.                 'label' => 'readState',
  184.                 'choices' => [
  185.                     'no' => 'no',
  186.                     'yes' => 'yes',
  187.                     'all' => 'all',
  188.                 ],
  189.                 'attr' => [
  190.                     'is-expanded' => true,
  191.                 ],
  192.             ])
  193.             ->add('hitState'ChoiceType::class, [
  194.                 'label' => 'hitState',
  195.                 'choices' => [
  196.                     'no' => 'no',
  197.                     'yes' => 'yes',
  198.                     'all' => 'all',
  199.                 ],
  200.                 'attr' => [
  201.                     'is-expanded' => true,
  202.                 ],
  203.             ])
  204.             ->add('sectionName'ChoiceType::class, [
  205.                 'choices' => $this->getSectionNameChoices(),
  206.             ])
  207.             ->add('prospectIds'ChoiceType::class, [
  208.                 'label' => 'prospectIds',
  209.                 'attr' => [
  210.                     'type' => 'hidden',
  211.                     ListingResponseTransformer::FORM_FILTER_KEY => 'prospect',
  212.                 ],
  213.                 'multiple' => 'true',
  214.             ])
  215.             ->add('contactIds'ChoiceType::class, [
  216.                 'label' => 'contactIds',
  217.                 'attr' => [
  218.                     'type' => 'hidden',
  219.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contacts',
  220.                 ],
  221.                 'multiple' => 'true',
  222.             ])
  223.             ->add('fileLabel'TextType::class, [
  224.                 'label' => 'task_search_file_label',
  225.             ])
  226.             ->add('isDraft'ChoiceType::class, [
  227.                 'label' => 'taskSearch_isDraft',
  228.                 'choices' => [
  229.                     'no' => 'no',
  230.                     'yes' => 'yes',
  231.                     'all' => 'all',
  232.                 ],
  233.                 'attr' => [
  234.                     'is-expanded' => true,
  235.                 ],
  236.             ])
  237.         ;
  238.         //La recherche enchainĂ©e des contacts est prioritaire sur le champ de recherche contacts classique
  239.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  240.             $data $event->getData();
  241.             if (!empty($data['contactIds'])) {
  242.                 $data['contactsConcerned'] = $data['contactIds'];
  243.                 $data['contactIds'] = [];
  244.                 $event->setData($data);
  245.             }
  246.         });
  247.         $builder->get('prospectIds')->resetViewTransformers();
  248.         $builder->get('contactIds')->resetViewTransformers();
  249.         $this->addTimeStampableFields($builder'_task');
  250.     }
  251.     /**
  252.      * @param OptionsResolver $resolver
  253.      *
  254.      * @return void
  255.      */
  256.     public function configureOptions(OptionsResolver $resolver): void
  257.     {
  258.         parent::configureOptions($resolver);
  259.         $resolver->setDefaults([
  260.             'contactsConcerned' => [],
  261.             'csrf_protection' => false,
  262.             'data_class' => TaskSearch::class,
  263.             'required' => false,
  264.             self::FORM_CONFIG_VIEW_ORDER_ENTITY => Task::class,
  265.             self::FORM_CONFIG_VIEW_ORDER_TYPE => ViewOrder::VIEWORDER_TYPE_SEARCH,
  266.             self::FORM_CONFIG_SPECIFIC_FIELD_ENTITY => Task::class,
  267.             self::FORM_CONFIG_VIEW_ORDER_ENTITY_TYPE => null,
  268.         ]);
  269.     }
  270.     /**
  271.      * @param string $entityFQCN
  272.      * @param string $labelProperty
  273.      * @param string $valueProperty
  274.      *
  275.      * @return array<string, object>
  276.      *
  277.      * @throws CacheException
  278.      * @throws InvalidArgumentException
  279.      * @noinspection PhpParameterNameChangedDuringInheritanceInspection
  280.      */
  281.     private function getEntityChoices(string $entityFQCN, array $filters = [], string $labelProperty 'value'string $valueProperty 'id'): array
  282.     {
  283.         $choices $this->getEntityChoicesTrait(
  284.             $this->collectionDataProvider,
  285.             $this->cacheManager,
  286.             $this->sentryLogger,
  287.             $entityFQCN,
  288.             $filters
  289.         );
  290.         $valueSetter 'set'.ucfirst($valueProperty);
  291.         $labelSetter 'set'.ucfirst($labelProperty);
  292.         $emptyValue = (new $entityFQCN())
  293.             ->$valueSetter(ListWithEmptyFilterBuilder::EMPTY_VALUE)
  294.             ->$labelSetter('(Vide)')
  295.         ;
  296.         if ($emptyValue instanceof IriNormalizableInterface) {
  297.             $emptyValue->setNormalizeAsIRI(true);
  298.         }
  299.         $choices[] = $emptyValue;
  300.         return $choices;
  301.     }
  302. }