src/Form/BusinessCardOptionType.php line 349

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\BusinessCardOption;
  4. use App\Entity\Imposition;
  5. use App\Entity\Machine;
  6. use App\Entity\MachinePaperSizeMatrix;
  7. use App\Entity\PaperSize;
  8. use App\Entity\Product;
  9. use App\Entity\Task;
  10. use App\Form\DataTransformer\PrintingTypeToNumberTransformer;
  11. use App\Repository\MachinePaperSizeMatrixRepository;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\CallbackTransformer;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  18. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  19. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  20. use Symfony\Component\Form\Extension\Core\Type\TextType;
  21. use Symfony\Component\Form\FormBuilderInterface;
  22. use Symfony\Component\Form\FormEvent;
  23. use Symfony\Component\Form\FormEvents;
  24. use Symfony\Component\OptionsResolver\OptionsResolver;
  25. class BusinessCardOptionType extends AbstractType
  26. {
  27.     private $printingTypeToNumberTransformer;
  28.     private EntityManagerInterface $entityManager;
  29.     public function __construct(PrintingTypeToNumberTransformer $printingTypeToNumberTransformerEntityManagerInterface $em)
  30.     {
  31.         $this->printingTypeToNumberTransformer $printingTypeToNumberTransformer;
  32.         $this->entityManager $em;
  33.     }
  34.     public function buildForm(FormBuilderInterface $builder, array $options): void
  35.     {
  36.         $builder
  37.             ->add('name'TextType::class, [
  38.                 'required' => false,
  39.                 'attr' =>[
  40.                     'class' => 'form-control-sm',
  41.                 ],
  42.             ])
  43.             ->add('printingType'EntityType::class,[
  44.                 'choices' => [],
  45.                 'class' => Machine::class,
  46.                 'expanded' => false,
  47.                 'required' => true,
  48.                 'multiple' => false,
  49.                 'empty_data'  => null,
  50.                 'choice_label' => 'name',
  51.                 'attr' => [
  52.                     'class' => 'printingTypeSelect',
  53.                     'required' => 'required',
  54.                 ]
  55.             ])
  56.             ->add('machine'EntityType::class,[
  57.                 'choices' => [],
  58.                 'class' => Machine::class,
  59.                 'expanded' => false,
  60.                 'required' => true,
  61.                 'multiple' => false,
  62.                 'choice_label' => 'name',
  63.                 'by_reference' => true,
  64.                 'attr' => [
  65.                     'class' => 'machineSelect',
  66.                     'required' => 'required'
  67.                 ]
  68.             ])
  69.             ->add('paperSize'EntityType::class,[
  70.                 'choices' => [],
  71.                 'class' => PaperSize::class,
  72.                 'expanded' => false,
  73.                 'required' => false,
  74.                 'choice_label' => 'name',
  75.                 'by_reference' => true,
  76.                 'attr' => [
  77.                     'class' => 'paperSizeSelect',
  78.                     'required' => 'required'
  79.                 ]
  80.             ])
  81.             ->add('finishedSizeX'IntegerType::class,[
  82.                 /*'row_attr' => [
  83.                     'class' => 'input-group',
  84.                 ],*/
  85.                 'label' => 'Finished Size X',
  86.                 'attr' =>[
  87.                     'class' => 'finishedSizeX form-control-sm text-end',
  88.                     'min' => 1
  89.                 ],
  90.                 'required' => true
  91.             ])
  92.             ->add('finishedSizeY'IntegerType::class,[
  93.                 'label' => 'Finished Size Y',
  94.                 'attr' =>[
  95.                     'class' => 'finishedSizeY form-control-sm text-end',
  96.                     'min' => 1
  97.                 ],
  98.                 'required' => true
  99.             ])
  100.             ->add('imposition'EntityType::class,[
  101.                 'choices' => [],
  102.                 'class' => Imposition::class,
  103.                 'expanded' => false,
  104.                 'required' => false,
  105.                 'choice_label' => 'name',
  106.                 'by_reference' => true,
  107.                 'attr' => [
  108.                     'class' => 'impositionSelect',
  109.                     'required' => 'required'
  110.                 ]
  111.             ])
  112.             ->add('paperSearch'EntityType::class,[
  113.                 'mapped' => false,
  114.                 'choices' => [],
  115.                 'class' => Product::class,
  116.                 'expanded' => false,
  117.                 'required' => false,
  118.                 'choice_label' => 'name',
  119.                 'by_reference' => true,
  120.                 'attr' => [
  121.                     'class' => 'paperSearchSelect',
  122.                     /*'required' => 'required'*/
  123.                 ]
  124.             ])
  125.             ->add('paperType'EntityType::class,[
  126.                 'choices' => [],
  127.                 'class' => Product::class,
  128.                 'expanded' => false,
  129.                 'required' => false,
  130.                 'choice_label' => 'name',
  131.                 'by_reference' => true,
  132.                 'attr' => [
  133.                     'class' => 'paperTypeSelect',
  134.                     /*'required' => 'required'*/
  135.                 ]
  136.             ])
  137.             ->add('sideOne'ChoiceType::class, [
  138.                 'choices'  => [
  139.                 ],
  140.                 'attr' => [
  141.                     'class' => 'sideOneSelect form-select-sm',
  142.                     'style' => 'width:70%;height:32px',
  143.                     'required' => 'required'
  144.                 ],
  145.                 'required' => true
  146.             ])
  147.             ->add('sideTwo'ChoiceType::class, [
  148.                 'choices'  => [
  149.                 ],
  150.                 'attr' => [
  151.                     'class' => 'sideTwoSelect form-select-sm',
  152.                     'style' => 'width:70%;height:32px',
  153.                     'required' => 'required'
  154.                 ],
  155.                 'required' => true
  156.             ])
  157.             ->add('searchTask',ChoiceType::class, [
  158.                 'mapped' => false,
  159.                 'label' => false,
  160.                 'attr' => [
  161.                     'class' => 'taskSelect'
  162.                 ],
  163.                 'required' => false
  164.             ])
  165.            /* ->add('tasks', EntityType::class,[
  166.                 'choices' => [],
  167.                 'class' => Task::class,
  168.                 'multiple' => true,
  169.             ]);*/
  170.            ->add('tasks'CollectionType::class,[
  171.                'entry_type' => BusinessCardOptionTaskType::class,
  172.                'entry_options' => [
  173.                    'label' => false
  174.                ],
  175.                'allow_add' => true,
  176.                'allow_delete' => true,
  177.                'by_reference' => false,
  178.                'prototype' => true,
  179.                'label' => false,
  180.                'required' => false,
  181.            ])
  182.         ;
  183. /*        $builder->get('printingType')
  184.         ->addModelTransformer(
  185.             $this->printingTypeToNumberTransformer
  186.         );*/
  187. /*        $builder->get('printingType')
  188.             ->addViewTransformer(
  189.                 $this->printingTypeToNumberTransformer
  190.             );*/
  191.         /*$builder->get('tasks')
  192.             ->addModelTransformer(new CallbackTransformer(
  193.                 function ($tasksAsArray): string {
  194.                     // transform the array to a string
  195.                     if ($tasksAsArray)
  196.                     return implode(', ', $tasksAsArray);
  197.                 },
  198.                 function ($tasksAsString): array {
  199.                     // transform the string back to an array
  200.                     return explode(', ', $tasksAsString);
  201.                 }
  202.             ))
  203.         ;*/
  204.         $builder->addEventListener(
  205.             FormEvents::PRE_SET_DATA,
  206.             function(FormEvent $event) use ($options){
  207.                 $data $event->getData();
  208.                 $form $event->getForm();
  209.                 if (!$data) {
  210.                     if ($options['testing'] === true) {
  211.                         //For testing
  212.                         $printingType $this->entityManager->getRepository(Machine::class)->find(4);
  213.                         $machine $this->entityManager->getRepository(Machine::class)->find(2);
  214.                         $paperSize $this->entityManager->getRepository(PaperSize::class)->find(1);
  215.                         $imposition $this->entityManager->getRepository(Imposition::class)->find(1);
  216.                         $paper $this->entityManager->getRepository(Product::class)->find(5398);
  217.                         //Side one
  218.                         /** @var  $machinePaperSizeMatrix MachinePaperSizeMatrix*/
  219.                         $machinePaperSizeMatrix =  $this->entityManager->getRepository(MachinePaperSizeMatrix::class)->findOneBy([
  220.                             'paperSize' => $paperSize->getId(),
  221.                             'machine' => $machine->getId()
  222.                         ]);
  223.                         $sideOne = [
  224.                             $machinePaperSizeMatrix->getColourClickLabel() => $machinePaperSizeMatrix->getColourClickLabel(),
  225.                             $machinePaperSizeMatrix->getBlackOnlyClickLabel() => $machinePaperSizeMatrix->getBlackOnlyClickLabel()
  226.                         ];
  227.                         $sideTwo = [
  228.                             'none' => 'none',
  229.                             $machinePaperSizeMatrix->getColourClickLabel() => $machinePaperSizeMatrix->getColourClickLabel(),
  230.                             $machinePaperSizeMatrix->getBlackOnlyClickLabel() => $machinePaperSizeMatrix->getBlackOnlyClickLabel()
  231.                         ];
  232.                         $form->add('printingType'EntityType::class, [
  233.                             'choices' => [$printingType],
  234.                             'class' => Machine::class,
  235.                             'choice_label' => function (Machine $machine): string {
  236.                                 return $machine->getCalculatorDisplayName();
  237.                             },
  238.                             'attr' => [
  239.                                 'class' => 'printingTypeSelect'
  240.                             ]
  241.                         ])->add('machine'EntityType::class, [
  242.                             'choices' => [$machine],
  243.                             'class' => Machine::class,
  244.                             'expanded' => false,
  245.                             'required' => true,
  246.                             'choice_label' => function (Machine $machine): string {
  247.                                 return $machine->getCalculatorDisplayName();
  248.                             },
  249.                             'by_reference' => true,
  250.                             'attr' => [
  251.                                 'class' => 'machineSelect'
  252.                             ]
  253.                         ])->add('paperSize'EntityType::class, [
  254.                             'choices' => [$paperSize],
  255.                             'class' => PaperSize::class,
  256.                             'expanded' => false,
  257.                             'choice_label' => function (PaperSize $paperSize): string {
  258.                                 return $paperSize->getCalculatorDisplayName();
  259.                             },
  260.                             'attr' => [
  261.                                 'class' => 'paperSizeSelect'
  262.                             ]
  263.                         ])->add('imposition'EntityType::class, [
  264.                                 'choices' => [$imposition],
  265.                                 'class' => Imposition::class,
  266.                                 'expanded' => false,
  267.                                 'choice_label' =>  function (Imposition $imposition): string {
  268.                                     return $imposition->getCalculatorDisplayName();
  269.                                 },
  270.                                 'attr' => [
  271.                                     'class' => 'impositionSelect'
  272.                                 ]
  273.                             ]);
  274.                         //
  275.                         $form->add('paperSearch'EntityType::class, [
  276.                             'choices' => !is_null($paper)?[$paper]:[],
  277.                             'mapped' => false,
  278.                             'class' => Product::class,
  279.                             'required' => false,
  280.                             'expanded' => false,
  281.                             'choice_label' => function (Product $product): string {
  282.                                 return $product->getCalculatorPaperSearchDisplayName();
  283.                             },
  284.                             'by_reference' => true,
  285.                             'attr' => [
  286.                                 'class' => 'paperSearchSelect'
  287.                             ]
  288.                         ]);
  289.                         $form->add('sideOne'ChoiceType::class, [
  290.                             'choices' => [$sideOne],
  291.                             'required' => true,
  292.                             'attr' => [
  293.                                 'class' => 'sideOneSelect form-select-sm',
  294.                                 'style' => 'width:70%;height:32px'
  295.                             ]
  296.                         ]);
  297.                         $form->add('sideTwo'ChoiceType::class, [
  298.                             'choices' => [$sideTwo],
  299.                             'required' => true,
  300.                             'attr' => [
  301.                                 'class' => 'sideTwoSelect form-select-sm',
  302.                                 'style' => 'width:70%;height:32px'
  303.                             ]
  304.                         ]);
  305.                     }else{
  306.                         $form->add('paperSearch'EntityType::class, [
  307.                             'choices' => [],
  308.                             'mapped' => false,
  309.                             'class' => Product::class,
  310.                             'required' => false,
  311.                             'expanded' => false,
  312.                             'by_reference' => true,
  313.                             'attr' => [
  314.                                 'class' => 'paperSearchSelect'
  315.                             ]
  316.                         ]);
  317.                         $form->add('searchTask',ChoiceType::class, [
  318.                             'choices' => [],
  319.                             'mapped' => false,
  320.                             'label' => false,
  321.                             'attr' => [
  322.                                 'class' => 'taskSelect'
  323.                             ],
  324.                             'required' => false
  325.                         ]);
  326.                     }
  327.                 }else{
  328.                     $form->add('printingType'EntityType::class,[
  329.                         'choices' => [$data->getPrintingType()],
  330.                         'class' => Machine::class,
  331.                         'expanded' => false,
  332.                         'required' => true,
  333.                         'choice_label' => function (Machine $machine): string {
  334.                             return $machine->getCalculatorDisplayName();
  335.                         },
  336.                         'by_reference' => true,
  337.                         'attr' => [
  338.                             'class' => 'printingTypeSelect'
  339.                         ]
  340.                     ]);
  341.                     $form->add('machine'EntityType::class,[
  342.                         'choices' => [$data->getMachine()],
  343.                         'class' => Machine::class,
  344.                         'expanded' => false,
  345.                         'required' => true,
  346.                         'choice_label' => function (Machine $machine): string {
  347.                             return $machine->getCalculatorDisplayName();
  348.                         },
  349.                         'by_reference' => true,
  350.                         'attr' => [
  351.                             'class' => 'machineSelect'
  352.                         ]
  353.                     ])->add('paperSize'EntityType::class,[
  354.                         'choices' => [$data->getPaperSize()],
  355.                         'class' => PaperSize::class,
  356.                         'expanded' => false,
  357.                         'required' => false,
  358.                         'choice_label' => function (PaperSize $paperSize): string {
  359.                             return $paperSize->getCalculatorDisplayName();
  360.                         },
  361.                         'by_reference' => true,
  362.                         'attr' => [
  363.                             'class' => 'paperSizeSelect'
  364.                         ]
  365.                     ])
  366.                     ->add('imposition'EntityType::class,[
  367.                         'choices' => [$data->getImposition()],
  368.                         'class' => Imposition::class,
  369.                         'expanded' => false,
  370.                         'required' => true,
  371.                         'choice_label' => 'name',
  372.                         'by_reference' => true,
  373.                         'attr' => [
  374.                             'class' => 'impositionSelect'
  375.                         ]
  376.                     ]);
  377.                    //
  378.                     $paperChoices =
  379.                     $form->add('paperType'EntityType::class,[
  380.                        'choices' => !is_null($data->getPaperType())?[$data->getPaperType()]:[],
  381.                         'class' => Product::class,
  382.                         'expanded' => false,
  383.                         'required' => false,
  384.                         'choice_label' =>  function (Product $product): string {
  385.                             return $product->getCalculatorPaperDisplayName();
  386.                         },
  387.                         'by_reference' => true,
  388.                         'attr' => [
  389.                             'class' => 'paperTypeSelect'
  390.                         ]
  391.                     ]);
  392.                     $form->add('sideOne'ChoiceType::class, [
  393.                         'choices' => [$data->getSideOne() => $data->getSideOne()],
  394.                         'required' => true,
  395.                         'attr' => [
  396.                             'class' => 'sideOneSelect form-select-sm',
  397.                             'style' => 'width:70%;height:32px'
  398.                         ]
  399.                     ]);
  400.                     $form->add('sideTwo'ChoiceType::class, [
  401.                         'choices' => [$data->getSideTwo() => $data->getSideTwo()],
  402.                         'required' => true,
  403.                         'attr' => [
  404.                             'class' => 'sideTwoSelect form-select-sm',
  405.                             'style' => 'width:70%;height:32px'
  406.                         ]
  407.                     ]);
  408.                     $form->add('paperSearch'EntityType::class, [
  409.                         'choices' => [],
  410.                         'mapped' => false,
  411.                         'class' => Product::class,
  412.                         'required' => false,
  413.                         'expanded' => false,
  414.                         'by_reference' => true,
  415.                         'attr' => [
  416.                             'class' => 'paperSearchSelect'
  417.                         ]
  418.                     ]);
  419.                 }
  420.             });
  421. /*        $builder->addEventListener(
  422.             FormEvents::SUBMIT,
  423.             function(FormEvent $event) use ($options){
  424.                 $test = $event->getData();
  425.             });*/
  426.         $builder->addEventListener(
  427.             FormEvents::PRE_SUBMIT,
  428.             function(FormEvent $event) use ($options){
  429.                 $data  $event->getData();
  430.                 $form $event->getForm();
  431.                 //Set printing Type
  432.                 if (isset($data['printingType']) && $data['printingType'] != null){
  433.                     $printingType $this->entityManager
  434.                         ->getRepository(Machine::class)
  435.                         // query for the issue with this id
  436.                         ->find($data['printingType']);
  437.                     $form->add('printingType'EntityType::class,[
  438.                         'choices' => [$printingType],
  439.                         'class' => Machine::class,
  440.                     ]);
  441.                 }
  442.                 //Machine
  443.                 if (isset($data['machine']) && $data['machine'] != null){
  444.                     $machine $this->entityManager
  445.                         ->getRepository(Machine::class)
  446.                         // query for the issue with this id
  447.                         ->find($data['machine'])
  448.                     ;
  449.                     $form->add('machine'EntityType::class,[
  450.                         'choices' => [$machine],
  451.                         'class' => Machine::class,
  452.                     ]);
  453.                 }
  454.                 //Imposition
  455.                 if (isset($data['imposition']) && $data['imposition'] != null){
  456.                     $imposition $this->entityManager
  457.                         ->getRepository(Imposition::class)
  458.                         // query for the issue with this id
  459.                         ->find($data['imposition']);
  460.                     $form->add('imposition'EntityType::class,[
  461.                         'choices' => [$imposition],
  462.                         'class' => Imposition::class,
  463.                     ]);
  464.                 }
  465.                 //PaperSize
  466.                 if (isset($data['paperSize']) && $data['paperSize'] != null){
  467.                     $paperSize $this->entityManager
  468.                         ->getRepository(PaperSize::class)
  469.                         // query for the issue with this id
  470.                         ->find($data['paperSize']);
  471.                     $form->add('paperSize'EntityType::class,[
  472.                         'choices' => [$paperSize],
  473.                         'class' => PaperSize::class,
  474.                     ]);
  475.                 }
  476.                 //Paper type - Product
  477.                 if (isset($data['paperType']) && $data['paperType'] != null){
  478.                     $paperType $this->entityManager
  479.                         ->getRepository(Product::class)
  480.                         // query for the issue with this id
  481.                         ->find($data['paperType']);
  482.                     $form->add('paperType'EntityType::class,[
  483.                         'choices' => [$paperType],
  484.                         'class' => Product::class,
  485.                     ]);
  486.                 }
  487.                 if (isset($data['tasks']) && $data['tasks'] != null){
  488.                     $tasks $this->entityManager
  489.                         ->getRepository(Task::class)
  490.                         // query for the issue with this id
  491.                         ->findBy(['id' => $data['tasks']]);
  492.                     $form->remove('tasks');
  493.                     $form->add('tasks'EntityType::class,[
  494.                         'choices' => [$tasks],
  495.                         'class' => Task::class,
  496.                         'multiple' => true
  497.                     ]);
  498.                 }else{
  499.                     //No task selected
  500.                     $form->add('tasks'EntityType::class,[
  501.                         'choices' => [],
  502.                         'class' => Task::class,
  503.                         'multiple' => true
  504.                     ]);
  505.                     //$form->remove('tasks');
  506.                 }
  507.                 /*$form->remove('paperSearch');
  508.                 $form->remove('searchTask');*/
  509.                 if (isset($data['sideOne']) && $data['sideOne'] != null){
  510.                     $form->add('sideOne'TextType::class,[
  511.                         'data' => $data['sideOne']
  512.                     ]);
  513.                 }
  514.                 if (isset($data['sideTwo']) && $data['sideTwo'] != null){
  515.                     $form->add('sideTwo'TextType::class,[
  516.                         'data' => $data['sideTwo']
  517.                     ]);
  518.                 }
  519.                 $form->remove('paperSearch');
  520.                 $form->remove('searchTask');
  521.         });
  522.     }
  523.     public function configureOptions(OptionsResolver $resolver): void
  524.     {
  525.         $resolver->setDefaults([
  526.             'data_class' => BusinessCardOption::class,
  527.             'allow_extra_fields' => true,
  528.             'testing' => false
  529.         ]);
  530.     }
  531. }