|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sylius\LegacyBridgePlugin\Controller\Trait; |
| 6 | + |
| 7 | +use Sylius\Bundle\AddressingBundle\Form\Type\ProvinceCodeChoiceType; |
| 8 | +use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
| 9 | +use Sylius\Component\Addressing\Model\CountryInterface; |
| 10 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 11 | +use Symfony\Component\Form\FormInterface; |
| 12 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 16 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 17 | + |
| 18 | +/** @mixin ResourceController */ |
| 19 | +trait ProvinceTrait |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @throws AccessDeniedException |
| 23 | + * @throws NotFoundHttpException |
| 24 | + */ |
| 25 | + public function choiceOrTextFieldFormAction(Request $request): Response |
| 26 | + { |
| 27 | + $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
| 28 | + |
| 29 | + if (!$configuration->isHtmlRequest() || null === $countryCode = $request->query->get('countryCode')) { |
| 30 | + throw new AccessDeniedException(); |
| 31 | + } |
| 32 | + |
| 33 | + /** @var CountryInterface|null $country */ |
| 34 | + $country = $this->get('sylius.repository.country')->findOneBy(['code' => $countryCode]); |
| 35 | + if ($country === null) { |
| 36 | + throw new NotFoundHttpException('Requested country does not exist.'); |
| 37 | + } |
| 38 | + |
| 39 | + if (!$country->hasProvinces()) { |
| 40 | + $form = $this->createProvinceTextForm(); |
| 41 | + |
| 42 | + $content = $this->renderView( |
| 43 | + $configuration->getTemplate('_provinceText.html'), |
| 44 | + [ |
| 45 | + 'metadata' => $this->metadata, |
| 46 | + 'form' => $form->createView(), |
| 47 | + ], |
| 48 | + ); |
| 49 | + |
| 50 | + return new JsonResponse([ |
| 51 | + 'content' => $content, |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + $form = $this->createProvinceChoiceForm($country); |
| 56 | + |
| 57 | + $content = $this->renderView( |
| 58 | + $configuration->getTemplate('_provinceChoice.html'), |
| 59 | + [ |
| 60 | + 'metadata' => $this->metadata, |
| 61 | + 'form' => $form->createView(), |
| 62 | + ], |
| 63 | + ); |
| 64 | + |
| 65 | + return new JsonResponse([ |
| 66 | + 'content' => $content, |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + protected function createProvinceChoiceForm(CountryInterface $country): FormInterface |
| 71 | + { |
| 72 | + return $this->get('form.factory')->createNamed('sylius_address_province', ProvinceCodeChoiceType::class, null, [ |
| 73 | + 'country' => $country, |
| 74 | + 'label' => 'sylius.form.address.province', |
| 75 | + 'placeholder' => 'sylius.form.province.select', |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + protected function createProvinceTextForm(): FormInterface |
| 80 | + { |
| 81 | + return $this->get('form.factory')->createNamed('sylius_address_province', TextType::class, null, [ |
| 82 | + 'required' => false, |
| 83 | + 'label' => 'sylius.form.address.province', |
| 84 | + ]); |
| 85 | + } |
| 86 | +} |
0 commit comments