lib/composer/OrderBundle/src/VisualMedia/OrderBundle/Form/Type/CheckoutType.php line 237

Open in your IDE?
  1. <?php
  2. /**
  3. * Checkout Type
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\OrderBundle\Form\Type;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  13. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  14. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  15. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  16. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  19. use Symfony\Component\Form\FormBuilderInterface;
  20. use Symfony\Component\Form\FormEvent;
  21. use Symfony\Component\Form\FormEvents;
  22. use Symfony\Component\Form\FormInterface;
  23. use Symfony\Component\OptionsResolver\OptionsResolver;
  24. use Symfony\Component\Validator\Constraints\Email;
  25. use Symfony\Component\Validator\Constraints\IsTrue;
  26. use Symfony\Component\Validator\Constraints\NotBlank;
  27. use VisualMedia\DiscountBundle\Form\Type\DiscountOrderType;
  28. use VisualMedia\LisaBundle\Form\Type\BooleanType;
  29. use VisualMedia\OrderBundle\Entity\BaseOrder;
  30. use VisualMedia\OrderBundle\Form\Constraint\OrderCreateUserDuplicateEntryConstraint;
  31. use VisualMedia\OrderBundle\Form\Type\ShippingMethodType;
  32. use VisualMedia\PaymentBundle\Component\PaymentMethod;
  33. use VisualMedia\PaymentBundle\Form\Type\PaymentMethodType;
  34. use VisualMedia\PaymentBundle\Provider\PaymentMethodProvider;
  35. use VisualMedia\OrderBundle\Provider\ShippingMethodProvider;
  36. /**
  37. * Checkout Type
  38. */
  39. class CheckoutType extends AbstractType
  40. {
  41. public const NAME = 'checkout';
  42. public const STEPS = 6;
  43. public const STEP = 'step';
  44. public const NEXT = 'next';
  45. public const PREVIOUS = 'previous';
  46. public const SUBMIT = 'submit';
  47. /**
  48. * Shipping Method Provider
  49. * @var ShippingMethodProvider
  50. */
  51. protected $shippingMethodProvider;
  52. /**
  53. * Payment Method Provider
  54. * @var PaymentMethodProvider
  55. */
  56. protected $paymentMethodProvider;
  57. /**
  58. * Constructor
  59. *
  60. * @param ShippingMethodProvider $shippingMethodProvider
  61. * @param PaymentMethodProvider $paymentMethodProvider
  62. */
  63. public function __construct(ShippingMethodProvider $shippingMethodProvider, PaymentMethodProvider $paymentMethodProvider)
  64. {
  65. $this->shippingMethodProvider = $shippingMethodProvider;
  66. $this->paymentMethodProvider = $paymentMethodProvider;
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function buildForm(FormBuilderInterface $builder, array $options): void
  72. {
  73. $step = $options['step'] ?: 1;
  74. // Build hidden step type.
  75. $builder->add(static::STEP, HiddenType::class, array(
  76. 'data' => $step,
  77. 'mapped' => false,
  78. ));
  79. // Build current step types.
  80. $this->buildStep($builder, $options);
  81. // Build submit types.
  82. switch ($step) {
  83. case static::STEPS:
  84. $this->buildPrevious($builder, $options);
  85. $this->buildSubmit($builder, $options);
  86. break;
  87. case 1:
  88. $this->buildNext($builder, $options);
  89. break;
  90. default:
  91. $this->buildPrevious($builder, $options);
  92. $this->buildNext($builder, $options);
  93. break;
  94. }
  95. }
  96. /**
  97. * Get Steps
  98. *
  99. * @return int
  100. */
  101. public function getSteps(): int
  102. {
  103. return static::STEPS;
  104. }
  105. /**
  106. * Build Step
  107. *
  108. * @param FormBuilderInterface $builder
  109. * @param array $options
  110. *
  111. * @return void
  112. */
  113. public function buildStep(FormBuilderInterface $builder, array $options): void
  114. {
  115. $step = $options['step'] ?: 1;
  116. switch ($step) {
  117. case 1:
  118. $this->buildOrderType($builder, $options);
  119. $this->buildOrderTypeData($builder, $options);
  120. break;
  121. case 2:
  122. $this->buildAccountType($builder, $options);
  123. $this->buildInvoiceData($builder, $options);
  124. $this->buildDeliveryEqualToInvoiceRadio($builder, $options);
  125. $this->buildDeliveryData($builder, $options);
  126. $this->buildNote($builder, $options);
  127. break;
  128. case 3:
  129. $this->buildShippingMethodType($builder, $options);
  130. break;
  131. case 4:
  132. $this->buildDiscount($builder, $options);
  133. break;
  134. case 5:
  135. $this->buildPaymentMethodType($builder, $options);
  136. break;
  137. case 6:
  138. $this->buildTermsAndConditions($builder, $options);
  139. break;
  140. }
  141. }
  142. /**
  143. * Get Order Type Choices
  144. *
  145. * @return array
  146. */
  147. public static function getOrderTypeChoices(): array
  148. {
  149. return array(
  150. BaseOrder::ORDER_TYPE_PRIVATE,
  151. BaseOrder::ORDER_TYPE_BUSINESS,
  152. );
  153. }
  154. /**
  155. * Build Order Type
  156. *
  157. * @param FormBuilderInterface $builder
  158. * @param array $options
  159. *
  160. * @return void
  161. */
  162. public function buildOrderType(FormBuilderInterface $builder, array $options): void
  163. {
  164. $builder->add('orderType', ChoiceType::class, array(
  165. 'label' => 'checkout.order_type.label',
  166. 'choices' => static::getOrderTypeChoices(),
  167. 'choice_label' => function($choice, $key, $value) {
  168. return sprintf('checkout.order_type.choices.%s', $choice);
  169. },
  170. 'expanded' => true,
  171. ));
  172. }
  173. /**
  174. * Build Order Type Data
  175. *
  176. * @param FormBuilderInterface $builder
  177. * @param array $options
  178. *
  179. * @return void
  180. */
  181. public function buildOrderTypeData(FormBuilderInterface $builder, array $options): void
  182. {
  183. $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
  184. $form = $event->getForm();
  185. $order = $event->getData();
  186. if ($order->getOrderType() === $order::ORDER_TYPE_BUSINESS) {
  187. $form->add('companyName', TextType::class, array(
  188. 'label' => 'checkout.company_name',
  189. 'constraints' => array(
  190. new NotBlank(array('message' => 'checkout.company_name.not_blank')),
  191. ),
  192. ));
  193. $form->add('companyReference', TextType::class, array(
  194. 'label' => 'checkout.company_reference',
  195. 'required' => false,
  196. ));
  197. }
  198. });
  199. $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
  200. $form = $event->getForm();
  201. $order = $event->getData();
  202. if ($order->getOrderType() !== $order::ORDER_TYPE_BUSINESS) {
  203. $order->setCompanyName(null);
  204. $order->setCompanyReference(null);
  205. }
  206. });
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function buildAccountType(FormBuilderInterface $builder, array $options): void
  212. {
  213. $passwordFormModifier = function(FormInterface $form, BaseOrder $order, string $event = null) {
  214. $form->add('password', RepeatedType::class, array(
  215. 'type' => PasswordType::class,
  216. 'first_options' => array(
  217. 'label' => 'checkout.password.first.label',
  218. 'always_empty' => false,
  219. 'attr' => array(
  220. 'value' => $order->getPassword() ?? null
  221. )
  222. ),
  223. 'second_options' => array(
  224. 'label' => 'checkout.password.second.label',
  225. 'always_empty' => false,
  226. 'attr' => array(
  227. 'value' => $order->getPassword() ?? null
  228. ),
  229. ),
  230. 'required' => false,
  231. ));
  232. };
  233. $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) use ($passwordFormModifier)
  234. {
  235. $form = $event->getForm();
  236. $order = $event->getData();
  237. $passwordFormModifier($form, $order, FormEvents::POST_SET_DATA);
  238. });
  239. $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($passwordFormModifier)
  240. {
  241. $form = $event->getForm();
  242. $order = $event->getData();
  243. $passwordFormModifier($form, $order, FormEvents::SUBMIT);
  244. });
  245. }
  246. /**
  247. * Build Invoice Data
  248. *
  249. * @param FormBuilderInterface $builder
  250. * @param array $options
  251. *
  252. * @return void
  253. */
  254. public function buildInvoiceData(FormBuilderInterface $builder, array $options): void
  255. {
  256. $builder->add('invoiceData', InvoiceDataType::class, array(
  257. 'autocomplete' => $options['autocomplete'],
  258. ));
  259. $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
  260. $form = $event->getForm();
  261. $order = $event->getData();
  262. // override email to add custom OrderCreateUserDuplicateEntryConstraint
  263. $invoiceFormFields = $form->get('invoiceData');
  264. $invoiceFormFields->add('email', EmailType::class, array(
  265. 'label' => 'checkout.email',
  266. 'constraints' => array(
  267. new NotBlank(array('message' => 'checkout.email.not_blank')),
  268. new Email(),
  269. new OrderCreateUserDuplicateEntryConstraint(array('order' => $order)),
  270. ),
  271. ));
  272. });
  273. }
  274. /**
  275. * Build Delivery Equal To Invoice Checkbox
  276. *
  277. * @param FormBuilderInterface $builder
  278. * @param array $options
  279. *
  280. * @return void
  281. */
  282. public function buildDeliveryEqualToInvoiceCheckbox(FormBuilderInterface $builder, array $options): void
  283. {
  284. $builder->add('deliveryEqualToInvoice', CheckboxType::class, array(
  285. 'label' => 'checkout.delivery_equal_to_invoice.checkbox',
  286. 'required' => false,
  287. ));
  288. }
  289. /**
  290. * Build Delivery Equal To Invoice Radio
  291. *
  292. * @param FormBuilderInterface $builder
  293. * @param array $options
  294. *
  295. * @return void
  296. */
  297. public function buildDeliveryEqualToInvoiceRadio(FormBuilderInterface $builder, array $options): void
  298. {
  299. $builder->add('deliveryEqualToInvoice', BooleanType::class, array(
  300. 'label' => 'checkout.delivery_equal_to_invoice.label',
  301. 'choice_label' => function($choice, $key, $value) {
  302. return sprintf('checkout.delivery_equal_to_invoice.choices.%s', $key);
  303. },
  304. 'expanded' => true,
  305. ));
  306. }
  307. /**
  308. * Build Delivery Data
  309. *
  310. * @param FormBuilderInterface $builder
  311. * @param array $options
  312. *
  313. * @return void
  314. */
  315. public function buildDeliveryData(FormBuilderInterface $builder, array $options): void
  316. {
  317. $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) use ($options) {
  318. $form = $event->getForm();
  319. $order = $event->getData();
  320. if (!$order->getDeliveryEqualToInvoice()) {
  321. $form->add('deliveryData', DeliveryDataType::class, array(
  322. 'autocomplete' => $options['autocomplete'],
  323. ));
  324. }
  325. });
  326. $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
  327. $form = $event->getForm();
  328. $order = $event->getData();
  329. if ($order->getDeliveryEqualToInvoice()) {
  330. $order->setDeliveryData(null);
  331. }
  332. });
  333. }
  334. /**
  335. * Build Note
  336. *
  337. * @param FormBuilderInterface $builder
  338. * @param array $options
  339. *
  340. * @return void
  341. */
  342. public function buildNote(FormBuilderInterface $builder, array $options): void
  343. {
  344. $builder->add('note', TextareaType::class, array(
  345. 'label' => 'checkout.note',
  346. 'required' => false,
  347. ));
  348. }
  349. /**
  350. * Get Shipping Method Choices
  351. *
  352. * @return array
  353. */
  354. public function getShippingMethodChoices(): array
  355. {
  356. $choices = array();
  357. foreach ($this->shippingMethodProvider->getAllEnabled() as $shippingMethod) {
  358. $choices[] = $shippingMethod::getName();
  359. }
  360. return $choices;
  361. }
  362. /**
  363. * Build Shipping Method Type
  364. *
  365. * @param FormBuilderInterface $builder
  366. * @param array $options
  367. *
  368. * @return void
  369. */
  370. public function buildShippingMethodType(FormBuilderInterface $builder, array $options): void
  371. {
  372. $builder->add('shippingMethod', ShippingMethodType::class, array(
  373. 'label' => 'checkout.shipping_method.label',
  374. 'choices' => $this->getShippingMethodChoices(),
  375. 'choice_label' => function($choice, $key, $value) {
  376. return sprintf('checkout.shipping_method.choices.%s', $choice);
  377. },
  378. 'constraints' => array(
  379. new NotBlank(array('message' => 'checkout.shipping_method.not_blank')),
  380. ),
  381. 'expanded' => true,
  382. ));
  383. }
  384. /**
  385. * Get Payment Method Choices
  386. *
  387. * @return array
  388. */
  389. public function getPaymentMethodChoices(): array
  390. {
  391. $choices = array();
  392. foreach ($this->paymentMethodProvider->getAllEnabled() as $paymentMethod) {
  393. $choices[] = $paymentMethod::getName();
  394. }
  395. return $choices;
  396. }
  397. /**
  398. * Get Payment Method Issuer Choices
  399. *
  400. * @param PaymentMethod $paymentMethod
  401. *
  402. * @return array
  403. */
  404. public function getPaymentMethodIssuerChoices(PaymentMethod $paymentMethod): array
  405. {
  406. $choices = array();
  407. foreach ($paymentMethod->getIssuers() as $issuer) {
  408. $choices[$issuer->getProviderName()] = $issuer->getProviderId();
  409. }
  410. return $choices;
  411. }
  412. /**
  413. * Build Discount
  414. *
  415. * @param FormBuilderInterface $builder
  416. * @param array $options
  417. *
  418. * @return void
  419. */
  420. public function buildDiscount(FormBuilderInterface $builder, array $options): void
  421. {
  422. $builder->add('discount', DiscountOrderType::class, array(
  423. 'mapped' => false,
  424. 'order' => $options['data'] ?? null
  425. ));
  426. }
  427. /**
  428. * Build Payment Method Type
  429. *
  430. * @param FormBuilderInterface $builder
  431. * @param array $options
  432. *
  433. * @return void
  434. */
  435. public function buildPaymentMethodType(FormBuilderInterface $builder, array $options): void
  436. {
  437. $builder->add('paymentMethod', PaymentMethodType::class, array(
  438. 'label' => 'checkout.payment_method.label',
  439. 'choices' => $this->getPaymentMethodChoices(),
  440. 'choice_label' => function($choice, $key, $value) {
  441. if (null !== $paymentMethod = $this->paymentMethodProvider->getEnabled($choice)) {
  442. if (null !== $name = $paymentMethod->getProviderName()) {
  443. return $name;
  444. }
  445. }
  446. return sprintf('checkout.payment_method.choices.%s', $choice);
  447. },
  448. 'constraints' => array(
  449. new NotBlank(array('message' => 'checkout.payment_method.not_blank')),
  450. ),
  451. 'expanded' => true,
  452. ));
  453. $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
  454. $form = $event->getForm();
  455. $order = $event->getData();
  456. if (null === $paymentMethod = $order->getPaymentMethod()) {
  457. $order->setPaymentMethod($paymentMethod = static::getPaymentMethodChoices()[0] ?? null);
  458. }
  459. if (null === $paymentMethod = $this->paymentMethodProvider->getEnabled($paymentMethod)) {
  460. return;
  461. }
  462. if (empty($paymentMethod->getIssuers())) {
  463. return;
  464. }
  465. $form->add('issuer', PaymentMethodType::class, array(
  466. 'label' => 'checkout.issuer.label',
  467. 'placeholder' => 'checkout.issuer.placeholder',
  468. 'choices' => $this->getPaymentMethodIssuerChoices($paymentMethod),
  469. 'choice_label' => function($choice, $key, $value) {
  470. return $key;
  471. },
  472. 'choice_value' => function($choice) {
  473. return $choice;
  474. },
  475. 'constraints' => array(
  476. new NotBlank(array('message' => 'checkout.issuer.not_blank')),
  477. ),
  478. ));
  479. });
  480. $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
  481. $form = $event->getForm();
  482. $order = $event->getData();
  483. if (null === $paymentMethod = $this->paymentMethodProvider->getEnabled($order->getPaymentMethod())) {
  484. return;
  485. }
  486. if (!empty($paymentMethod->getIssuers())) {
  487. return;
  488. }
  489. $order->setIssuer(null);
  490. });
  491. }
  492. /**
  493. * Build Terms And Conditions
  494. *
  495. * @param FormBuilderInterface $builder
  496. * @param array $options
  497. *
  498. * @return void
  499. */
  500. public function buildTermsAndConditions(FormBuilderInterface $builder, array $options): void
  501. {
  502. $builder->add('termsAndConditions', CheckboxType::class, array(
  503. 'label' => 'checkout.terms_and_conditions',
  504. 'constraints' => array(
  505. new IsTrue(array(
  506. 'message' => 'checkout.terms_and_conditions.is_true',
  507. )),
  508. ),
  509. 'data' => false,
  510. ));
  511. }
  512. /**
  513. * Build Next
  514. *
  515. * @param FormBuilderInterface $builder
  516. * @param array $options
  517. *
  518. * @return void
  519. */
  520. public function buildNext(FormBuilderInterface $builder, array $options): void
  521. {
  522. $builder->add('next', SubmitType::class, array(
  523. 'label' => 'checkout.next',
  524. ));
  525. }
  526. /**
  527. * Build Previous
  528. *
  529. * @param FormBuilderInterface $builder
  530. * @param array $options
  531. *
  532. * @return void
  533. */
  534. public function buildPrevious(FormBuilderInterface $builder, array $options): void
  535. {
  536. $builder->add('previous', SubmitType::class, array(
  537. 'label' => 'checkout.previous',
  538. ));
  539. }
  540. /**
  541. * Build Submit
  542. *
  543. * @param FormBuilderInterface $builder
  544. * @param array $options
  545. *
  546. * @return void
  547. */
  548. public function buildSubmit(FormBuilderInterface $builder, array $options): void
  549. {
  550. $builder->add('submit', SubmitType::class, array(
  551. 'label' => 'checkout.submit',
  552. ));
  553. }
  554. /**
  555. * {@inheritDoc}
  556. */
  557. public function getName(): string
  558. {
  559. return static::NAME;
  560. }
  561. /**
  562. * {@inheritDoc}
  563. */
  564. public function configureOptions(OptionsResolver $resolver): void
  565. {
  566. $resolver->setDefaults(array(
  567. 'translation_domain' => 'forms',
  568. 'data_class' => BaseOrder::class,
  569. 'step' => null,
  570. 'autocomplete' => false,
  571. ));
  572. }
  573. }