src/V4/Form/Type/Section/SectionType.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\V4\Form\Type\Section;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  6. use App\Model\Traits\TimeStampableFormType;
  7. use App\Service\Cache\CacheManager;
  8. use App\V4\Entity\SavedRequest;
  9. use App\V4\Form\Type\LeaderMemberFilterTrait;
  10. use App\V4\Model\Customer\Customer;
  11. use App\V4\Model\Section\Section;
  12. use App\V4\Model\Security\UserInfo;
  13. use App\V4\Repository\SavedRequestRepository;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. class SectionType extends AbstractType
  21. {
  22.     use LeaderMemberFilterTrait;
  23.     use TimeStampableFormType;
  24.     /**
  25.      * @var CollectionDataProviderInterface
  26.      */
  27.     private $collectionDataProvider;
  28.     /**
  29.      * @var CacheManager
  30.      */
  31.     private $cacheManager;
  32.     /**
  33.      * @var ItemDataProviderInterface
  34.      */
  35.     private $itemDataProvider;
  36.     /**
  37.      * @var SavedRequestRepository
  38.      */
  39.     private $savedRequestRepository;
  40.     /**
  41.      * @param ItemDataProviderInterface       $itemDataProvider
  42.      * @param CollectionDataProviderInterface $collectionDataProvider
  43.      * @param CacheManager                    $cacheManager
  44.      * @param SavedRequestRepository          $savedRequestRepository
  45.      */
  46.     public function __construct(
  47.         ItemDataProviderInterface $itemDataProvider,
  48.         CollectionDataProviderInterface $collectionDataProvider,
  49.         CacheManager $cacheManager,
  50.         SavedRequestRepository $savedRequestRepository
  51.     ) {
  52.         $this->collectionDataProvider $collectionDataProvider;
  53.         $this->cacheManager $cacheManager;
  54.         $this->itemDataProvider $itemDataProvider;
  55.         $this->savedRequestRepository $savedRequestRepository;
  56.     }
  57.     /**
  58.      * @param FormBuilderInterface $builder
  59.      * @param array                $options
  60.      *
  61.      * @return void
  62.      *
  63.      * @throws ResourceClassNotSupportedException
  64.      */
  65.     public function buildForm(FormBuilderInterface $builder, array $options): void
  66.     {
  67.         parent::buildForm($builder$options);
  68.         /** @var Section $section */
  69.         $section $builder->getData();
  70.         $customerId $section instanceof Section && $section->getCustomer() instanceof Customer
  71.             $section->getCustomer()->getId()
  72.             : $options['customerId']
  73.         ;
  74.         $customer null !== $customerId $this->itemDataProvider->getItem(Customer::class, $customerId) : null;
  75.         $sections = [];
  76.         if ($customer instanceof Customer) {
  77.             $sections $this->collectionDataProvider->getCollection(Section::class, null, [
  78.                 'filters' => [
  79.                     'customerId' => $customer->getId(),
  80.                 ],
  81.             ]);
  82.         }
  83.         if ($section instanceof Section && null !== $section->getId()) {
  84.             foreach ($sections as $key => $sectionChoice) {
  85.                 if ($section->getId() === $sectionChoice->getId()) {
  86.                     unset($sections[$key]);
  87.                 }
  88.                 if ($sectionChoice->getParent() instanceof Section
  89.                     && !$this->isSelectableSection($section->getId(), $sectionChoice->getParent())) {
  90.                     unset($sections[$key]);
  91.                 }
  92.             }
  93.         }
  94.         $builder
  95.             ->add('code'TextType::class, [
  96.                 'required' => true,
  97.             ])
  98.             ->add('parent'ChoiceType::class, [
  99.                 'choices' => $sections,
  100.                 'choice_label' => 'code',
  101.                 'choice_value' => 'id',
  102.                 'data' => $section instanceof Section && $section->getParent() instanceof Section
  103.                     $section->getParent()->getId()
  104.                     : null,
  105.                 'required' => false,
  106.             ])
  107.             ->add('leaders'ChoiceType::class, [
  108.                 'choices' => $customer instanceof Customer
  109.                     $this->getLeaderMemberChoices($this->collectionDataProvider$customer->getId())
  110.                     : [],
  111.                 'choice_label' => 'fullname',
  112.                 'choice_value' => 'id',
  113.                 'data' => $section instanceof Section array_map(function (UserInfo $user) {
  114.                     return $user->getId();
  115.                 }, $section->getLeaders()->toArray()) : null,
  116.                 'multiple' => true,
  117.                 'required' => false,
  118.             ])
  119.             ->add('members'ChoiceType::class, [
  120.                 'choices' => $customer instanceof Customer
  121.                     $this->getLeaderMemberChoices($this->collectionDataProvider$customer->getId())
  122.                     : [],
  123.                 'choice_label' => 'fullname',
  124.                 'choice_value' => 'id',
  125.                 'data' => $section instanceof Section array_map(function (UserInfo $user) {
  126.                     return $user->getId();
  127.                 }, $section->getMembers()->toArray()) : null,
  128.                 'multiple' => true,
  129.                 'required' => false,
  130.             ])
  131.             ->add('isVisible'CheckboxType::class, [
  132.                 'required' => false,
  133.                 'false_values' => ['false''0'''null],
  134.             ])
  135.             ->add('customer'ChoiceType::class, [
  136.                 'choices' => $customer ? [$customer] : [],
  137.                 'choice_value' => 'id',
  138.                 'data' => $customer instanceof Customer $customer->getId() : null,
  139.                 'required' => false,
  140.                 'attr' => [
  141.                     'type' => 'hidden',
  142.                 ],
  143.             ])
  144.             ->add('sectionSavedRequests'ChoiceType::class, [
  145.                 'choices' => $customer instanceof Customer
  146.                     $this->savedRequestRepository->findBy([
  147.                         'customerId' => $customer->getId(),
  148.                         'type' => SavedRequest::TYPE_SECTION_FILTER,
  149.                     ])
  150.                     : [],
  151.                 'choice_label' => 'name',
  152.                 'choice_value' => 'id',
  153.                 'data' => $section instanceof Section array_map(function (SavedRequest $savedRequest) {
  154.                     return $savedRequest->getId();
  155.                 }, $section->getSectionSavedRequests()->toArray()) : null,
  156.                 'multiple' => true,
  157.                 'required' => false,
  158.             ]);
  159.     }
  160.     /**
  161.      * @param OptionsResolver $resolver
  162.      *
  163.      * @return void
  164.      */
  165.     public function configureOptions(OptionsResolver $resolver): void
  166.     {
  167.         parent::configureOptions($resolver);
  168.         $resolver->setDefaults([
  169.             'data_class' => Section::class,
  170.             'customerId' => null,
  171.             'csrf_protection' => false,
  172.             'allow_extra_fields' => true,
  173.         ]);
  174.     }
  175.     /**
  176.      * @param string|null $id
  177.      * @param Section     $sectionChoice
  178.      *
  179.      * @return bool
  180.      */
  181.     private function isSelectableSection(?string $idSection $sectionChoice): bool
  182.     {
  183.         if ($id === $sectionChoice->getId()) {
  184.             return false;
  185.         }
  186.         if ($sectionChoice->getParent() instanceof Section) {
  187.             return $this->isSelectableSection($id$sectionChoice->getParent());
  188.         }
  189.         return true;
  190.     }
  191. }