<?php
namespace App\Form\Ecommerce;
use App\DataTransformer\Ecommerce\GiftTransformer;
use App\Enum\Ecommerce\ShippingMethodsEnum;
use App\Repository\Interfaces\HotelRepositoryInterface;
use Application\FrontendBundle\Manager\HotelManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotBlank;
use WAM\Bundle\CoreBundle\Form\PrivacyType;
use App\Form\Ecommerce\AddressBillableType;
use App\Form\Ecommerce\AddressShippableType;
/**
* @author Juanjo MartÃnez <jmartinez@wearemarketing.com>
*/
class GiftType extends AbstractType
{
const SHIPPING_METHOD_FIELD_NAME = 'shipping_method';
const PICKING_PLACE_FIELD_NAME = 'picking_place';
private GiftTransformer $giftTransformer;
private HotelRepositoryInterface $hotelRepository;
private array $validationGroups = [];
/**
* GiftType constructor.
* @param GiftTransformer $giftTransformer
* @param HotelRepositoryInterface $hotelRepository
* @param $validationGroups
*/
public function __construct(GiftTransformer $giftTransformer, HotelRepositoryInterface $hotelRepository, array $validationGroups)
{
$this->giftTransformer = $giftTransformer;
$this->hotelRepository = $hotelRepository;
$this->validationGroups = $validationGroups;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add($builder->create('gift', HiddenType::class, [
'data_class' => null,
'data' => $options['gift'],]
)->addModelTransformer($this->giftTransformer))
->add('from', TextType::class, [
'label' => 'gift_form.from',
'required' => true,
'constraints' => [new NotBlank(['groups' => ['Gift']])]
])
->add('to', TextType::class, [
'label' => 'gift_form.to',
'required' => true,
'constraints' => [new NotBlank(['groups' => ['Gift']])]
])
->add('dedication', TextareaType::class, [
'label' => 'gift_form.dedication',
'required' => false
])
->add(self::SHIPPING_METHOD_FIELD_NAME, ChoiceType::class, [
'label' => '',
'expanded' => true,
'required' => true,
'choices' => [
'gift_form.email_send' => ShippingMethodsEnum::email()->raw(),
'gift_form.express' => ShippingMethodsEnum::express()->raw(),
'gift_form.standard' => ShippingMethodsEnum::standard()->raw(),
'gift_form.picking_up' => ShippingMethodsEnum::picking()->raw(),
],
'attr' => [
'class' => 'js-shipping-method',
'data-picking' => ShippingMethodsEnum::picking()->raw(),
]
])
->add(self::PICKING_PLACE_FIELD_NAME, ChoiceType::class, [
'label' => '',
'required' => false,
'placeholder' => 'app.gift_form.placeholder.picking_place',
'label_attr' => [
'style' => 'display:none;',
'class' => 'js-picking-place-label',
],
'choices' => $this->generateHotelsWithPickingUp(),
'attr' => [
'style' => 'display:none;',
'class' => 'js-picking-place',
],
])
->add('billing', AddressBillableType::class)
->add(ShippingMethodsEnum::picking()->raw(), PickingUpType::class)
->add(ShippingMethodsEnum::email()->raw(), SendingEmailType::class)
->add(ShippingMethodsEnum::shipping()->raw(), AddressShippableType::class)
->add('privacy', PrivacyType::class, [
'constraints' => [new IsTrue(['groups' => ['Gift'], 'message' => 'validator.privacy.error'])]
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'validation_groups' => function (FormInterface $form) {
$shippingMethod = $form->get(self::SHIPPING_METHOD_FIELD_NAME)->getData();
if ($shippingMethod == ShippingMethodsEnum::picking()->raw() and is_null($form->get(self::PICKING_PLACE_FIELD_NAME)->getData())) {
$form->get(self::PICKING_PLACE_FIELD_NAME)->addError(new FormError('app.gift_form.errors.picking_place'));
}
if (
$shippingMethod == ShippingMethodsEnum::shipping()->raw() ||
$shippingMethod == ShippingMethodsEnum::spain()->raw() ||
$shippingMethod == ShippingMethodsEnum::menorca()->raw() ||
$shippingMethod == ShippingMethodsEnum::asturias()->raw() ||
$shippingMethod == ShippingMethodsEnum::express()->raw() ||
$shippingMethod == ShippingMethodsEnum::standard()->raw()
) {
return array_merge($this->validationGroups, ['Shippable']);
} elseif ($shippingMethod == ShippingMethodsEnum::email()->raw()) {
return array_merge($this->validationGroups, ['Email']);
} else {
return array_merge($this->validationGroups, ['PickUp']);
}
},
'gift' => null,
'allow_extra_fields' => true,
]);
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'app_gift_type';
}
private function generateHotelsWithPickingUp()
{
$hotelsWithEnabledPickingUp = $this->hotelRepository->getWithEnabledPickingUp();
$hotelsName = [];
foreach ($hotelsWithEnabledPickingUp as $hotel) {
$hotelsName[ucfirst(strtolower($hotel->getSimpleName()))] = strtolower($hotel->getSimpleName());
}
return $hotelsName;
}
}