<?php
/**
* Checkout Type
*
* @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
*/
namespace VisualMedia\OrderBundle\Form\Type;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotBlank;
use VisualMedia\DiscountBundle\Form\Type\DiscountOrderType;
use VisualMedia\LisaBundle\Form\Type\BooleanType;
use VisualMedia\OrderBundle\Entity\BaseOrder;
use VisualMedia\OrderBundle\Form\Constraint\OrderCreateUserDuplicateEntryConstraint;
use VisualMedia\OrderBundle\Form\Type\ShippingMethodType;
use VisualMedia\PaymentBundle\Component\PaymentMethod;
use VisualMedia\PaymentBundle\Form\Type\PaymentMethodType;
use VisualMedia\PaymentBundle\Provider\PaymentMethodProvider;
use VisualMedia\OrderBundle\Provider\ShippingMethodProvider;
/**
* Checkout Type
*/
class CheckoutType extends AbstractType
{
public const NAME = 'checkout';
public const STEPS = 6;
public const STEP = 'step';
public const NEXT = 'next';
public const PREVIOUS = 'previous';
public const SUBMIT = 'submit';
/**
* Shipping Method Provider
* @var ShippingMethodProvider
*/
protected $shippingMethodProvider;
/**
* Payment Method Provider
* @var PaymentMethodProvider
*/
protected $paymentMethodProvider;
/**
* Constructor
*
* @param ShippingMethodProvider $shippingMethodProvider
* @param PaymentMethodProvider $paymentMethodProvider
*/
public function __construct(ShippingMethodProvider $shippingMethodProvider, PaymentMethodProvider $paymentMethodProvider)
{
$this->shippingMethodProvider = $shippingMethodProvider;
$this->paymentMethodProvider = $paymentMethodProvider;
}
/**
* {@inheritDoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$step = $options['step'] ?: 1;
// Build hidden step type.
$builder->add(static::STEP, HiddenType::class, array(
'data' => $step,
'mapped' => false,
));
// Build current step types.
$this->buildStep($builder, $options);
// Build submit types.
switch ($step) {
case static::STEPS:
$this->buildPrevious($builder, $options);
$this->buildSubmit($builder, $options);
break;
case 1:
$this->buildNext($builder, $options);
break;
default:
$this->buildPrevious($builder, $options);
$this->buildNext($builder, $options);
break;
}
}
/**
* Get Steps
*
* @return int
*/
public function getSteps(): int
{
return static::STEPS;
}
/**
* Build Step
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildStep(FormBuilderInterface $builder, array $options): void
{
$step = $options['step'] ?: 1;
switch ($step) {
case 1:
$this->buildOrderType($builder, $options);
$this->buildOrderTypeData($builder, $options);
break;
case 2:
$this->buildAccountType($builder, $options);
$this->buildInvoiceData($builder, $options);
$this->buildDeliveryEqualToInvoiceRadio($builder, $options);
$this->buildDeliveryData($builder, $options);
$this->buildNote($builder, $options);
break;
case 3:
$this->buildShippingMethodType($builder, $options);
break;
case 4:
$this->buildDiscount($builder, $options);
break;
case 5:
$this->buildPaymentMethodType($builder, $options);
break;
case 6:
$this->buildTermsAndConditions($builder, $options);
break;
}
}
/**
* Get Order Type Choices
*
* @return array
*/
public static function getOrderTypeChoices(): array
{
return array(
BaseOrder::ORDER_TYPE_PRIVATE,
BaseOrder::ORDER_TYPE_BUSINESS,
);
}
/**
* Build Order Type
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildOrderType(FormBuilderInterface $builder, array $options): void
{
$builder->add('orderType', ChoiceType::class, array(
'label' => 'checkout.order_type.label',
'choices' => static::getOrderTypeChoices(),
'choice_label' => function($choice, $key, $value) {
return sprintf('checkout.order_type.choices.%s', $choice);
},
'expanded' => true,
));
}
/**
* Build Order Type Data
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildOrderTypeData(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
$form = $event->getForm();
$order = $event->getData();
if ($order->getOrderType() === $order::ORDER_TYPE_BUSINESS) {
$form->add('companyName', TextType::class, array(
'label' => 'checkout.company_name',
'constraints' => array(
new NotBlank(array('message' => 'checkout.company_name.not_blank')),
),
));
$form->add('companyReference', TextType::class, array(
'label' => 'checkout.company_reference',
'required' => false,
));
}
});
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$order = $event->getData();
if ($order->getOrderType() !== $order::ORDER_TYPE_BUSINESS) {
$order->setCompanyName(null);
$order->setCompanyReference(null);
}
});
}
/**
* {@inheritdoc}
*/
public function buildAccountType(FormBuilderInterface $builder, array $options): void
{
$passwordFormModifier = function(FormInterface $form, BaseOrder $order, string $event = null) {
$form->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array(
'label' => 'checkout.password.first.label',
'always_empty' => false,
'attr' => array(
'value' => $order->getPassword() ?? null
)
),
'second_options' => array(
'label' => 'checkout.password.second.label',
'always_empty' => false,
'attr' => array(
'value' => $order->getPassword() ?? null
),
),
'required' => false,
));
};
$builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) use ($passwordFormModifier)
{
$form = $event->getForm();
$order = $event->getData();
$passwordFormModifier($form, $order, FormEvents::POST_SET_DATA);
});
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($passwordFormModifier)
{
$form = $event->getForm();
$order = $event->getData();
$passwordFormModifier($form, $order, FormEvents::SUBMIT);
});
}
/**
* Build Invoice Data
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildInvoiceData(FormBuilderInterface $builder, array $options): void
{
$builder->add('invoiceData', InvoiceDataType::class, array(
'autocomplete' => $options['autocomplete'],
));
$builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
$form = $event->getForm();
$order = $event->getData();
// override email to add custom OrderCreateUserDuplicateEntryConstraint
$invoiceFormFields = $form->get('invoiceData');
$invoiceFormFields->add('email', EmailType::class, array(
'label' => 'checkout.email',
'constraints' => array(
new NotBlank(array('message' => 'checkout.email.not_blank')),
new Email(),
new OrderCreateUserDuplicateEntryConstraint(array('order' => $order)),
),
));
});
}
/**
* Build Delivery Equal To Invoice Checkbox
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildDeliveryEqualToInvoiceCheckbox(FormBuilderInterface $builder, array $options): void
{
$builder->add('deliveryEqualToInvoice', CheckboxType::class, array(
'label' => 'checkout.delivery_equal_to_invoice.checkbox',
'required' => false,
));
}
/**
* Build Delivery Equal To Invoice Radio
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildDeliveryEqualToInvoiceRadio(FormBuilderInterface $builder, array $options): void
{
$builder->add('deliveryEqualToInvoice', BooleanType::class, array(
'label' => 'checkout.delivery_equal_to_invoice.label',
'choice_label' => function($choice, $key, $value) {
return sprintf('checkout.delivery_equal_to_invoice.choices.%s', $key);
},
'expanded' => true,
));
}
/**
* Build Delivery Data
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildDeliveryData(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) use ($options) {
$form = $event->getForm();
$order = $event->getData();
if (!$order->getDeliveryEqualToInvoice()) {
$form->add('deliveryData', DeliveryDataType::class, array(
'autocomplete' => $options['autocomplete'],
));
}
});
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$order = $event->getData();
if ($order->getDeliveryEqualToInvoice()) {
$order->setDeliveryData(null);
}
});
}
/**
* Build Note
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildNote(FormBuilderInterface $builder, array $options): void
{
$builder->add('note', TextareaType::class, array(
'label' => 'checkout.note',
'required' => false,
));
}
/**
* Get Shipping Method Choices
*
* @return array
*/
public function getShippingMethodChoices(): array
{
$choices = array();
foreach ($this->shippingMethodProvider->getAllEnabled() as $shippingMethod) {
$choices[] = $shippingMethod::getName();
}
return $choices;
}
/**
* Build Shipping Method Type
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildShippingMethodType(FormBuilderInterface $builder, array $options): void
{
$builder->add('shippingMethod', ShippingMethodType::class, array(
'label' => 'checkout.shipping_method.label',
'choices' => $this->getShippingMethodChoices(),
'choice_label' => function($choice, $key, $value) {
return sprintf('checkout.shipping_method.choices.%s', $choice);
},
'constraints' => array(
new NotBlank(array('message' => 'checkout.shipping_method.not_blank')),
),
'expanded' => true,
));
}
/**
* Get Payment Method Choices
*
* @return array
*/
public function getPaymentMethodChoices(): array
{
$choices = array();
foreach ($this->paymentMethodProvider->getAllEnabled() as $paymentMethod) {
$choices[] = $paymentMethod::getName();
}
return $choices;
}
/**
* Get Payment Method Issuer Choices
*
* @param PaymentMethod $paymentMethod
*
* @return array
*/
public function getPaymentMethodIssuerChoices(PaymentMethod $paymentMethod): array
{
$choices = array();
foreach ($paymentMethod->getIssuers() as $issuer) {
$choices[$issuer->getProviderName()] = $issuer->getProviderId();
}
return $choices;
}
/**
* Build Discount
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildDiscount(FormBuilderInterface $builder, array $options): void
{
$builder->add('discount', DiscountOrderType::class, array(
'mapped' => false,
'order' => $options['data'] ?? null
));
}
/**
* Build Payment Method Type
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildPaymentMethodType(FormBuilderInterface $builder, array $options): void
{
$builder->add('paymentMethod', PaymentMethodType::class, array(
'label' => 'checkout.payment_method.label',
'choices' => $this->getPaymentMethodChoices(),
'choice_label' => function($choice, $key, $value) {
if (null !== $paymentMethod = $this->paymentMethodProvider->getEnabled($choice)) {
if (null !== $name = $paymentMethod->getProviderName()) {
return $name;
}
}
return sprintf('checkout.payment_method.choices.%s', $choice);
},
'constraints' => array(
new NotBlank(array('message' => 'checkout.payment_method.not_blank')),
),
'expanded' => true,
));
$builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
$form = $event->getForm();
$order = $event->getData();
if (null === $paymentMethod = $order->getPaymentMethod()) {
$order->setPaymentMethod($paymentMethod = static::getPaymentMethodChoices()[0] ?? null);
}
if (null === $paymentMethod = $this->paymentMethodProvider->getEnabled($paymentMethod)) {
return;
}
if (empty($paymentMethod->getIssuers())) {
return;
}
$form->add('issuer', PaymentMethodType::class, array(
'label' => 'checkout.issuer.label',
'placeholder' => 'checkout.issuer.placeholder',
'choices' => $this->getPaymentMethodIssuerChoices($paymentMethod),
'choice_label' => function($choice, $key, $value) {
return $key;
},
'choice_value' => function($choice) {
return $choice;
},
'constraints' => array(
new NotBlank(array('message' => 'checkout.issuer.not_blank')),
),
));
});
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$order = $event->getData();
if (null === $paymentMethod = $this->paymentMethodProvider->getEnabled($order->getPaymentMethod())) {
return;
}
if (!empty($paymentMethod->getIssuers())) {
return;
}
$order->setIssuer(null);
});
}
/**
* Build Terms And Conditions
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildTermsAndConditions(FormBuilderInterface $builder, array $options): void
{
$builder->add('termsAndConditions', CheckboxType::class, array(
'label' => 'checkout.terms_and_conditions',
'constraints' => array(
new IsTrue(array(
'message' => 'checkout.terms_and_conditions.is_true',
)),
),
'data' => false,
));
}
/**
* Build Next
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildNext(FormBuilderInterface $builder, array $options): void
{
$builder->add('next', SubmitType::class, array(
'label' => 'checkout.next',
));
}
/**
* Build Previous
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildPrevious(FormBuilderInterface $builder, array $options): void
{
$builder->add('previous', SubmitType::class, array(
'label' => 'checkout.previous',
));
}
/**
* Build Submit
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return void
*/
public function buildSubmit(FormBuilderInterface $builder, array $options): void
{
$builder->add('submit', SubmitType::class, array(
'label' => 'checkout.submit',
));
}
/**
* {@inheritDoc}
*/
public function getName(): string
{
return static::NAME;
}
/**
* {@inheritDoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
'translation_domain' => 'forms',
'data_class' => BaseOrder::class,
'step' => null,
'autocomplete' => false,
));
}
}