src/Form/Ecommerce/GiftType.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Form\Ecommerce;
  3. use App\DataTransformer\Ecommerce\GiftTransformer;
  4. use App\Enum\Ecommerce\ShippingMethodsEnum;
  5. use App\Repository\Interfaces\HotelRepositoryInterface;
  6. use Application\FrontendBundle\Manager\HotelManager;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormError;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Symfony\Component\Validator\Constraints\IsTrue;
  17. use Symfony\Component\Validator\Constraints\NotBlank;
  18. use WAM\Bundle\CoreBundle\Form\PrivacyType;
  19. use App\Form\Ecommerce\AddressBillableType;
  20. use App\Form\Ecommerce\AddressShippableType;
  21. /**
  22.  * @author Juanjo Martínez <jmartinez@wearemarketing.com>
  23.  */
  24. class GiftType extends AbstractType
  25. {
  26.     const SHIPPING_METHOD_FIELD_NAME 'shipping_method';
  27.     const PICKING_PLACE_FIELD_NAME 'picking_place';
  28.     private GiftTransformer $giftTransformer;
  29.     private HotelRepositoryInterface $hotelRepository;
  30.     private array $validationGroups = [];
  31.     /**
  32.      * GiftType constructor.
  33.      * @param GiftTransformer $giftTransformer
  34.      * @param HotelRepositoryInterface $hotelRepository
  35.      * @param $validationGroups
  36.      */
  37.     public function __construct(GiftTransformer $giftTransformerHotelRepositoryInterface $hotelRepository, array $validationGroups)
  38.     {
  39.         $this->giftTransformer $giftTransformer;
  40.         $this->hotelRepository $hotelRepository;
  41.         $this->validationGroups $validationGroups;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function buildForm(FormBuilderInterface $builder, array $options)
  47.     {
  48.         $builder
  49.             ->add($builder->create('gift'HiddenType::class, [
  50.                     'data_class' => null,
  51.                     'data' => $options['gift'],]
  52.             )->addModelTransformer($this->giftTransformer))
  53.             ->add('from'TextType::class, [
  54.                 'label' => 'gift_form.from',
  55.                 'required' => true,
  56.                 'constraints' => [new NotBlank(['groups' => ['Gift']])]
  57.             ])
  58.             ->add('to'TextType::class, [
  59.                 'label' => 'gift_form.to',
  60.                 'required' => true,
  61.                 'constraints' => [new NotBlank(['groups' => ['Gift']])]
  62.             ])
  63.             ->add('dedication'TextareaType::class, [
  64.                 'label' => 'gift_form.dedication',
  65.                 'required' => false
  66.             ])
  67.             ->add(self::SHIPPING_METHOD_FIELD_NAMEChoiceType::class, [
  68.                 'label' => '',
  69.                 'expanded' => true,
  70.                 'required' => true,
  71.                 'choices' => [
  72.                     'gift_form.email_send' => ShippingMethodsEnum::email()->raw(),
  73.                     'gift_form.express' => ShippingMethodsEnum::express()->raw(),
  74.                     'gift_form.standard' => ShippingMethodsEnum::standard()->raw(),
  75.                     'gift_form.picking_up' => ShippingMethodsEnum::picking()->raw(),
  76.                 ],
  77.                 'attr' => [
  78.                     'class' => 'js-shipping-method',
  79.                     'data-picking' => ShippingMethodsEnum::picking()->raw(),
  80.                 ]
  81.             ])
  82.             ->add(self::PICKING_PLACE_FIELD_NAMEChoiceType::class, [
  83.                 'label' => '',
  84.                 'required' => false,
  85.                 'placeholder' => 'app.gift_form.placeholder.picking_place',
  86.                 'label_attr' => [
  87.                     'style' => 'display:none;',
  88.                     'class' => 'js-picking-place-label',
  89.                 ],
  90.                 'choices' => $this->generateHotelsWithPickingUp(),
  91.                 'attr' => [
  92.                     'style' => 'display:none;',
  93.                     'class' => 'js-picking-place',
  94.                 ],
  95.             ])
  96.             ->add('billing'AddressBillableType::class)
  97.             ->add(ShippingMethodsEnum::picking()->raw(), PickingUpType::class)
  98.             ->add(ShippingMethodsEnum::email()->raw(), SendingEmailType::class)
  99.             ->add(ShippingMethodsEnum::shipping()->raw(), AddressShippableType::class)
  100.             ->add('privacy'PrivacyType::class, [
  101.                 'constraints' => [new IsTrue(['groups' => ['Gift'], 'message' => 'validator.privacy.error'])]
  102.             ]);
  103.     }
  104.     /**
  105.      * @param OptionsResolver $resolver
  106.      */
  107.     public function configureOptions(OptionsResolver $resolver)
  108.     {
  109.         $resolver->setDefaults([
  110.             'validation_groups' => function (FormInterface $form) {
  111.                 $shippingMethod $form->get(self::SHIPPING_METHOD_FIELD_NAME)->getData();
  112.                 if ($shippingMethod == ShippingMethodsEnum::picking()->raw() and is_null($form->get(self::PICKING_PLACE_FIELD_NAME)->getData())) {
  113.                     $form->get(self::PICKING_PLACE_FIELD_NAME)->addError(new FormError('app.gift_form.errors.picking_place'));
  114.                 }
  115.                 if (
  116.                     $shippingMethod == ShippingMethodsEnum::shipping()->raw() ||
  117.                     $shippingMethod == ShippingMethodsEnum::spain()->raw() ||
  118.                     $shippingMethod == ShippingMethodsEnum::menorca()->raw() ||
  119.                     $shippingMethod == ShippingMethodsEnum::asturias()->raw() ||
  120.                     $shippingMethod == ShippingMethodsEnum::express()->raw() ||
  121.                     $shippingMethod == ShippingMethodsEnum::standard()->raw()
  122.                 ) {
  123.                     return array_merge($this->validationGroups, ['Shippable']);
  124.                 } elseif ($shippingMethod == ShippingMethodsEnum::email()->raw()) {
  125.                     return array_merge($this->validationGroups, ['Email']);
  126.                 } else {
  127.                     return array_merge($this->validationGroups, ['PickUp']);
  128.                 }
  129.             },
  130.             'gift' => null,
  131.             'allow_extra_fields' => true,
  132.         ]);
  133.     }
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public function getBlockPrefix()
  138.     {
  139.         return 'app_gift_type';
  140.     }
  141.     private function generateHotelsWithPickingUp()
  142.     {
  143.         $hotelsWithEnabledPickingUp $this->hotelRepository->getWithEnabledPickingUp();
  144.         $hotelsName = [];
  145.         foreach ($hotelsWithEnabledPickingUp as $hotel) {
  146.             $hotelsName[ucfirst(strtolower($hotel->getSimpleName()))] = strtolower($hotel->getSimpleName());
  147.         }
  148.         return $hotelsName;
  149.     }
  150. }