src/V4/Form/Type/Prospect/ProspectSearchType.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\V4\Form\Type\Prospect;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use App\Form\Type\AbstractViewOrderAwareType;
  5. use App\Form\Type\Compare\DateCompareType;
  6. use App\Form\Type\Compare\NumberCompareType;
  7. use App\Form\Type\SubresourceChoicesTrait;
  8. use App\Listing\Transformer\ListingResponseTransformer;
  9. use App\Model\IriNormalizableInterface;
  10. use App\Model\Traits\TimeStampableFormType;
  11. use App\Model\ViewOrder\ViewOrder;
  12. use App\Security\SecurityConfig;
  13. use App\Service\ApiWebServiceFilterBuilder\ListWithEmptyFilterBuilder;
  14. use App\Service\Cache\CacheManager;
  15. use App\Service\Form\Api\FormApiChoicesManager;
  16. use App\V4\Form\Type\IdSearchTypeTrait;
  17. use App\V4\Form\Type\ManagedByFilterTrait;
  18. use App\V4\Form\Type\SectionNameFilterTrait;
  19. use App\V4\Logger\SentryLogger;
  20. use App\V4\Model\LegalForm\LegalForm;
  21. use App\V4\Model\Origin\Origin;
  22. use App\V4\Model\Potential\Potential;
  23. use App\V4\Model\Prospect\Prospect;
  24. use App\V4\Model\Prospect\ProspectSearch;
  25. use App\V4\Model\ProspectType\ProspectType;
  26. use App\V4\Model\QuoteState\QuoteState;
  27. use App\V4\Model\StructureType\StructureType;
  28. use App\V4\Model\TVA\TVA;
  29. use App\V4\Model\Workforce\Workforce;
  30. use Psr\Cache\CacheException;
  31. use Psr\Cache\InvalidArgumentException;
  32. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  33. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  34. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  35. use Symfony\Component\Form\Extension\Core\Type\TelType;
  36. use Symfony\Component\Form\Extension\Core\Type\TextType;
  37. use Symfony\Component\Form\FormBuilderInterface;
  38. use Symfony\Component\OptionsResolver\OptionsResolver;
  39. use Symfony\Component\Security\Core\Security;
  40. use Symfony\Contracts\Translation\TranslatorInterface;
  41. class ProspectSearchType extends AbstractViewOrderAwareType
  42. {
  43.     use ManagedByFilterTrait;
  44.     use NafFilterTrait;
  45.     use SectionNameFilterTrait;
  46.     use TimeStampableFormType;
  47.     use SubresourceChoicesTrait {
  48.         getEntityChoices as private getEntityChoicesTrait;
  49.     }
  50.     use IdSearchTypeTrait;
  51.     /**
  52.      * @var CollectionDataProviderInterface
  53.      */
  54.     private $collectionDataProvider;
  55.     /**
  56.      * @var CacheManager
  57.      */
  58.     private $cacheManager;
  59.     /**
  60.      * @var Security
  61.      */
  62.     private $security;
  63.     /**
  64.      * @var FormApiChoicesManager
  65.      */
  66.     private $formApiChoicesManager;
  67.     /**
  68.      * @var TranslatorInterface
  69.      */
  70.     private $translator;
  71.     /**
  72.      * @var SentryLogger
  73.      */
  74.     private $sentryLogger;
  75.     /**
  76.      * @param CollectionDataProviderInterface $collectionDataProvider
  77.      * @param CacheManager                    $cacheManager
  78.      * @param Security                        $security
  79.      * @param FormApiChoicesManager           $formApiChoicesManager
  80.      * @param TranslatorInterface             $translator
  81.      * @param SentryLogger                    $sentryLogger
  82.      */
  83.     public function __construct(
  84.         CollectionDataProviderInterface $collectionDataProvider,
  85.         CacheManager $cacheManager,
  86.         Security $security,
  87.         FormApiChoicesManager $formApiChoicesManager,
  88.         TranslatorInterface $translator,
  89.         SentryLogger $sentryLogger
  90.     ) {
  91.         $this->collectionDataProvider $collectionDataProvider;
  92.         $this->cacheManager $cacheManager;
  93.         $this->security $security;
  94.         $this->formApiChoicesManager $formApiChoicesManager;
  95.         $this->translator $translator;
  96.         $this->sentryLogger $sentryLogger;
  97.     }
  98.     /**
  99.      * @param FormBuilderInterface $builder
  100.      * @param array                $options
  101.      *
  102.      * @return void
  103.      *
  104.      * @throws CacheException
  105.      * @throws InvalidArgumentException
  106.      */
  107.     public function buildForm(FormBuilderInterface $builder, array $options): void
  108.     {
  109.         parent::buildForm($builder$options);
  110.         $this->addIdsField($builder);
  111.         $builder
  112.             ->add('name'TextType::class, [
  113.                 'label' => 'prospect_search_name',
  114.                 'attr' => [
  115.                     ListingResponseTransformer::FORM_FILTER_KEY => 'fullname',
  116.                 ],
  117.             ])
  118.             ->add('tasksLinked'CheckboxType::class, [
  119.                 'false_values' => [null'0'0false'''false'],
  120.             ])
  121.             ->add('customerCode'TextType::class, [
  122.                 'label' => 'prospect_search_customer_code',
  123.             ])
  124.             ->add('groupName'TextType::class)
  125.             ->add('companyMail'TextType::class, [
  126.                 'attr' => [
  127.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.email',
  128.                 ],
  129.             ])
  130.             ->add('tva'ChoiceType::class, [
  131.                 'choices' => $this->getEntityChoices(TVA::class),
  132.                 'choice_label' => 'value',
  133.                 'choice_value' => 'id',
  134.                 'multiple' => true,
  135.             ])
  136.             ->add('workforce'ChoiceType::class, [
  137.                 'choices' => $this->getEntityChoices(Workforce::class),
  138.                 'choice_label' => 'value',
  139.                 'choice_value' => 'id',
  140.                 'multiple' => true,
  141.                 'attr' => [
  142.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.workforce',
  143.                 ],
  144.             ])
  145.             ->add('type'ChoiceType::class, [
  146.                 'label' => 'prospectType',
  147.                 'choices' => $this->getEntityChoices(ProspectType::class),
  148.                 'choice_label' => 'value',
  149.                 'choice_value' => 'id',
  150.                 'multiple' => true,
  151.                 'attr' => [
  152.                     ListingResponseTransformer::FORM_FILTER_KEY => 'prospectType',
  153.                 ],
  154.             ])
  155.             ->add('origin'ChoiceType::class, [
  156.                 'choices' => $this->getEntityChoices(Origin::class),
  157.                 'choice_label' => 'value',
  158.                 'choice_value' => 'id',
  159.                 'multiple' => true,
  160.             ])
  161.             ->add('naf'ChoiceType::class, [
  162.                 'choices' => $this->getNafChoices(),
  163.                 'choice_value' => 'id',
  164.                 'multiple' => true,
  165.                 'attr' => [
  166.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.naf',
  167.                 ],
  168.             ])
  169.             ->add('potential'ChoiceType::class, [
  170.                 'choices' => $this->getEntityChoices(Potential::class),
  171.                 'choice_label' => 'value',
  172.                 'choice_value' => 'id',
  173.                 'multiple' => true,
  174.             ])
  175.             ->add('siret'TextType::class, [
  176.                 'attr' => [
  177.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.siret',
  178.                 ],
  179.             ])
  180.             ->add('fax'TelType::class, [
  181.                 'attr' => [
  182.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.fax',
  183.                 ],
  184.             ])
  185.             ->add('webSite'TextType::class, [
  186.                 'attr' => [
  187.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.webSite',
  188.                 ],
  189.             ])
  190.             ->add('socialCapital'NumberCompareType::class, [
  191.                 'attr' => [
  192.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.socialCapital',
  193.                 ],
  194.             ])
  195.             ->add('structureType'ChoiceType::class, [
  196.                 'choices' => $this->getEntityChoices(StructureType::class),
  197.                 'choice_label' => 'value',
  198.                 'choice_value' => 'id',
  199.                 'multiple' => true,
  200.                 'attr' => [
  201.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.structureType',
  202.                 ],
  203.             ])
  204.             ->add('managedBy'ChoiceType::class, [
  205.                 'choices' => $this->getManagedByChoices(),
  206.                 'multiple' => true,
  207.             ])
  208.             ->add('myAccounts'CheckboxType::class, [
  209.                 'false_values' => [null'0'0false'''false'],
  210.                 'data' => $this->security->isGranted(SecurityConfig::MY_PROSPECT_BY_DEFAULT),
  211.             ])
  212.             ->add('phone'TelType::class, [
  213.                 'attr' => [
  214.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.phone',
  215.                 ],
  216.             ])
  217.             ->add('address'TextType::class, [
  218.                 'attr' => [
  219.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.address',
  220.                 ],
  221.             ])
  222.             ->add('addressComplement'TextType::class, [
  223.                 'attr' => [
  224.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.addressMore',
  225.                 ],
  226.             ])
  227.             ->add('nameComplement'TextType::class, [
  228.                 'attr' => [
  229.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.nameComplement',
  230.                 ],
  231.             ])
  232.             ->add('cedex'TextType::class, [
  233.                 'attr' => [
  234.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.postalCedex',
  235.                 ],
  236.             ])
  237.             ->add('departments'ChoiceType::class, [
  238.                 'choices' => array_merge(array_map('strval'array_flip($this->formApiChoicesManager->getDepartements())), ['(Vide)' => 'empty']),
  239.                 'multiple' => true,
  240.                 'attr' => [
  241.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.departments',
  242.                 ],
  243.             ])
  244.             ->add('zip'TextType::class, [
  245.                 'attr' => [
  246.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.postalCode',
  247.                 ],
  248.             ])
  249.             ->add('city'TextType::class, [
  250.                 'attr' => [
  251.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.city',
  252.                 ],
  253.             ])
  254.             ->add('country'ChoiceType::class, [
  255.                 'choices' => array_combine(
  256.                     $this->formApiChoicesManager->getCountries(),
  257.                     $this->formApiChoicesManager->getCountries()
  258.                 ),
  259.                 'multiple' => true,
  260.                 'attr' => [
  261.                     ListingResponseTransformer::FORM_FILTER_KEY => 'contact.addresses.country',
  262.                 ],
  263.             ])
  264.             ->add('legalForm'ChoiceType::class, [
  265.                 'choices' => $this->getEntityChoices(LegalForm::class),
  266.                 'choice_label' => 'value',
  267.                 'choice_value' => 'id',
  268.                 'multiple' => true,
  269.                 'attr' => [
  270.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.legalForm',
  271.                 ],
  272.             ])
  273.             ->add('devisState'ChoiceType::class, [
  274.                 'choices' => $this->getEntityChoices(QuoteState::class, [], 'name'),
  275.                 'choice_label' => 'name',
  276.                 'choice_value' => 'id',
  277.                 'multiple' => true,
  278.             ])
  279.             ->add('creationYear'DateCompareType::class, [
  280.                 'attr' => [
  281.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.creationYear',
  282.                 ],
  283.             ])
  284.             ->add('turnOver'NumberCompareType::class, [
  285.                 'attr' => [
  286.                     ListingResponseTransformer::FORM_FILTER_KEY => 'company.turnover',
  287.                 ],
  288.             ])
  289.             ->add('justLabelAndId'CheckboxType::class, [
  290.                 'false_values' => [null'0'0false'''false'],
  291.                 'attr' => ['type' => 'hidden'],
  292.             ])
  293.             ->add('sectionName'ChoiceType::class, [
  294.                 'choices' => $this->getSectionNameChoices(),
  295.                 'multiple' => true,
  296.             ])
  297.             ->add('taskIds'ChoiceType::class, [
  298.                 'label' => 'taskIds',
  299.                 'required' => false,
  300.                 'attr' => [
  301.                     'type' => 'hidden',
  302.                     ListingResponseTransformer::FORM_FILTER_KEY => 'tasks',
  303.                 ],
  304.                 'multiple' => 'true',
  305.             ])
  306.             ->add('quoteIds'ChoiceType::class, [
  307.                 'label' => 'quoteIds',
  308.                 'required' => false,
  309.                 'attr' => ['type' => 'hidden'],
  310.                 'multiple' => 'true',
  311.             ])
  312.             ->add('prospectIds'ChoiceType::class, [
  313.                 'label' => 'prospectIds',
  314.                 'required' => false,
  315.                 'attr' => [
  316.                     'type' => 'hidden',
  317.                     ListingResponseTransformer::FORM_FILTER_KEY => 'prospect',
  318.                 ],
  319.                 'multiple' => 'true',
  320.                 'mapped' => false,
  321.             ])
  322.             ->add('isReverse'CheckboxType::class, [
  323.                 'label' => 'isReverse',
  324.                 'required' => false,
  325.                 'attr' => ['type' => 'hidden'],
  326.             ])
  327.             ->add('distance'HiddenType::class, [
  328.                 'required' => false,
  329.                 'attr' => [
  330.                     'data-vo-forced-display' => true,
  331.                 ],
  332.             ])
  333.             ->add('lat'HiddenType::class, [
  334.                 'attr' => [
  335.                     'data-vo-forced-display' => true,
  336.                 ],
  337.             ])
  338.             ->add('lng'HiddenType::class, [
  339.                 'attr' => [
  340.                     'data-vo-forced-display' => true,
  341.                 ],
  342.             ])
  343.             ->add('fileLabel'TextType::class, [
  344.                 'label' => 'prospect_search_file_label',
  345.             ])
  346.         ;
  347.         if ($this->security->isGranted(SecurityConfig::MODULE_PRODUCT)) {
  348.             $builder->add('hasSubscription'ChoiceType::class, [
  349.                 'choices' => [
  350.                     $this->translator->trans('no') => '0',
  351.                     $this->translator->trans('yes') => '1',
  352.                 ],
  353.             ]);
  354.         }
  355.         $builder->get('taskIds')->resetViewTransformers();
  356.         $builder->get('quoteIds')->resetViewTransformers();
  357.         $builder->get('prospectIds')->resetViewTransformers();
  358.         $this->addTimeStampableFields($builder'_prospect');
  359.     }
  360.     /**
  361.      * @param OptionsResolver $resolver
  362.      *
  363.      * @return void
  364.      */
  365.     public function configureOptions(OptionsResolver $resolver): void
  366.     {
  367.         parent::configureOptions($resolver);
  368.         $resolver->setDefaults([
  369.             'csrf_protection' => false,
  370.             'data_class' => ProspectSearch::class,
  371.             'required' => false,
  372.             'isGeolocationSearch' => false,
  373.             self::FORM_CONFIG_VIEW_ORDER_ENTITY => Prospect::class,
  374.             self::FORM_CONFIG_VIEW_ORDER_TYPE => ViewOrder::VIEWORDER_TYPE_SEARCH,
  375.             self::FORM_CONFIG_SPECIFIC_FIELD_ENTITY => Prospect::class,
  376.         ]);
  377.     }
  378.     /**
  379.      * @param string $entityFQCN
  380.      * @param array  $filters
  381.      * @param string $labelProperty
  382.      * @param string $valueProperty
  383.      *
  384.      * @return array<string, object>
  385.      *
  386.      * @throws CacheException
  387.      * @throws InvalidArgumentException
  388.      */
  389.     private function getEntityChoices(string $entityFQCN, array $filters = [], string $labelProperty 'value'string $valueProperty 'id'): array
  390.     {
  391.         $choices $this->getEntityChoicesTrait(
  392.             $this->collectionDataProvider,
  393.             $this->cacheManager,
  394.             $this->sentryLogger,
  395.             $entityFQCN,
  396.             $filters
  397.         );
  398.         $valueSetter 'set'.ucfirst($valueProperty);
  399.         $labelSetter 'set'.ucfirst($labelProperty);
  400.         $emptyValue = (new $entityFQCN())
  401.             ->$valueSetter(ListWithEmptyFilterBuilder::EMPTY_VALUE)
  402.             ->$labelSetter('(Vide)')
  403.         ;
  404.         if ($emptyValue instanceof IriNormalizableInterface) {
  405.             $emptyValue->setNormalizeAsIRI(true);
  406.         }
  407.         $choices[] = $emptyValue;
  408.         return $choices;
  409.     }
  410. }