Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Controller/Account/AddressController.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Account;
  3. use App\Entity\Address;
  4. use App\Entity\Region;
  5. use App\Entity\User;
  6. use App\Form\AddressFormType;
  7. use App\Form\DeleteConfirmationFormType;
  8. use App\Form\ShippingAddressFormType;
  9. use App\Service\GoogleAPIService;
  10. use App\Service\SessionService;
  11. use App\Service\ShipmentService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use Throwable;
  21. class AddressController extends AbstractController
  22. {
  23.     private EntityManagerInterface $em;
  24.     /**
  25.      * @param EntityManagerInterface $em
  26.      */
  27.     public function __construct(EntityManagerInterface $em, private TranslatorInterface $translator, private ParameterBagInterface $parameterBag)
  28.     {
  29.         $this->em $em;
  30.     }
  31.     #[Route('/mon-compte/mes-adresses'name'app_account_addresses')]
  32.     public function addresses(): Response
  33.     {
  34.         /** @var User $user */
  35.         if (!$user $this->getUser()) {
  36.             return $this->redirectToRoute('app_login');
  37.         }
  38.         return $this->render('account/address/index.html.twig', array(
  39.             'addresses' => $user->getAddresses()
  40.         ));
  41.     }
  42.     #[Route('/mon-compte/mes-adresses/{address}/edition'name'app_account_addresses_edit')]
  43.     public function addressEdit(Request $requestAddress $addressSessionService $sessionService): Response
  44.     {
  45.         if (!$this->getUser()) {
  46.             return $this->redirectToRoute('app_login');
  47.         }
  48.         if (!$this->getUser()->getAddresses()->contains($address)) {
  49.             throw $this->createAccessDeniedException();
  50.         }
  51.         $addressForm $this->createForm(AddressFormType::class, $address, array(
  52.             'user' => $this->getUser()
  53.         ));
  54.         $addressForm->handleRequest($request);
  55.         if ($addressForm->isSubmitted() && $addressForm->isValid()) {
  56.             if ($addressForm->get('defaultBilling')->getData()) {
  57.                 $this->getUser()->setBillingAddress($address);
  58.             }
  59.             if ($addressForm->get('defaultDelivery')->getData()) {
  60.                 $this->getUser()->setDeliveryAddress($address);
  61.             }
  62.             $this->em->persist($this->getUser());
  63.             $this->em->persist($address);
  64.             $this->em->flush();
  65.             $sessionService->clearShippingAddresses();
  66.             $this->addFlash('success'$this->translator->trans('app.success.address.modified'));
  67.             return $this->redirectToRoute('app_account_addresses');
  68.         }
  69.         return $this->render('account/address/edit.html.twig', array(
  70.             'address' => $address,
  71.             'form' => $addressForm->createView()
  72.         ));
  73.     }
  74.     #[Route('/mon-compte/mes-adresses/nouvelle-adresse'name'app_account_addresses_add')]
  75.     public function addressAdd(Request $requestSessionService $sessionService): Response
  76.     {
  77.         if (!$this->getUser()) {
  78.             return $this->redirectToRoute('app_login');
  79.         }
  80.         $addressForm $this->createForm(AddressFormType::class, new Address(), array(
  81.             'user' => $this->getUser()
  82.         ));
  83.         $addressForm->handleRequest($request);
  84.         if ($addressForm->isSubmitted() && $addressForm->isValid()) {
  85.             /** @var Address $address */
  86.             $address $addressForm->getData();
  87.             $address->setUser($this->getUser());
  88.             if ($addressForm->get('defaultBilling')->getData()) {
  89.                 $this->getUser()->setBillingAddress($address);
  90.             }
  91.             if ($addressForm->get('defaultDelivery')->getData()) {
  92.                 $this->getUser()->setDeliveryAddress($address);
  93.             }
  94.             $this->em->persist($this->getUser());
  95.             $this->em->persist($addressForm->getData());
  96.             $this->em->flush();
  97.             $sessionService->clearShippingAddresses();
  98.             $this->addFlash('success'$this->translator->trans('app.success.address.new'));
  99.             return $this->redirectToRoute('app_account_addresses');
  100.         }
  101.         return $this->render('account/address/edit.html.twig', array(
  102.             'address' => null,
  103.             'form' => $addressForm->createView()
  104.         ));
  105.     }
  106.     #[Route('/account/address/{address}/delete'name'app_account_addresses_delete')]
  107.     public function addressDelete(Request $requestAddress $address): Response
  108.     {
  109.         if (!$this->getUser()) {
  110.             return $this->redirectToRoute('app_login');
  111.         }
  112.         if (!$this->getUser()->getAddresses()->contains($address)) {
  113.             throw $this->createAccessDeniedException();
  114.         }
  115.         $this->em->remove($address);
  116.         $this->em->flush();
  117.         $this->addFlash('success'$this->translator->trans('app.success.address.deleted'));
  118.         $route $request->headers->get('referer');
  119.         return $this->redirect($route);
  120.     }
  121.     #[Route('/account/address/form/{address}'name'app_account_addresses_form'options: ['expose' => true])]
  122.     public function getAddressForm(Request $requestEntityManagerInterface $emShipmentService $shipmentServiceAddress $address null): Response
  123.     {
  124.         /** @var User $user */
  125.         if (!$user $this->getUser()) {
  126.             return $this->redirectToRoute('app_login');
  127.         }
  128.         if ($address && !$user->getAddresses()->contains($address)) {
  129.             return $this->redirectToRoute('app_account_addresses');
  130.         }
  131.         if (!$address) {
  132.             $address = new Address();
  133.             $address->setUser($this->getUser());
  134.         }
  135.         $form $this->createForm(AddressFormType::class, $address, array(
  136.             'user' => $this->getUser(),
  137.             'onlyDeliveryEnable' => true,
  138.             'attr' => array(
  139.                 'data-restrict-regions' => true,
  140.                 'data-submit-target' => $this->generateUrl('app_account_addresses_form', array('address' => $address?->getId()))
  141.             )
  142.         ));
  143.         $form->handleRequest($request);
  144.         if ($form->isSubmitted() && $form->isValid()) {
  145.             if ($form->get('defaultBilling')->getData()) {
  146.                 $this->getUser()->setBillingAddress($address);
  147.             }
  148.             if ($form->get('defaultDelivery')->getData()) {
  149.                 $this->getUser()->setDeliveryAddress($address);
  150.             }
  151.             $em->persist($form->getData());
  152.             $em->flush();
  153.             $shippingForm $this->createForm(ShippingAddressFormType::class);
  154.             $response = array(
  155.                 'html' => array(
  156.                     'delivery' => $this->renderView('account/shipping/addresses.html.twig', array(
  157.                         'formElement' => $shippingForm->get('deliveryAddress')->createView(),
  158.                         'userDefaultAddress' => $this->getUser()->getDeliveryAddress()
  159.                     )),
  160.                     'billing' => $this->renderView('account/shipping/addresses.html.twig', array(
  161.                         'formElement' => $shippingForm->get('billingAddress')->createView(),
  162.                         'userDefaultAddress' => $this->getUser()->getBillingAddress()
  163.                     ))
  164.                 ),
  165.                 'redirect' => false
  166.             );
  167.             $deliveryAddresses $user->getAddresses()->filter(fn(Address $address) => $shipmentService->canDeliver($address));
  168.             if ($deliveryAddresses->isEmpty()) {
  169.                 $response['redirect'] = true;
  170.                 $response['redirect_target'] = $this->generateUrl('app_account_delivery_notes_shipping_excluded');
  171.             }
  172.             return new JsonResponse($response);
  173.         }
  174.         if ($form->isSubmitted() && !$form->isValid()) {
  175.             $code Response::HTTP_BAD_REQUEST;
  176.             $address->setCountry(null);
  177.             $address->setRegion(null);
  178.         } else $code Response::HTTP_OK;
  179.         return new JsonResponse(array(
  180.             'title' => $this->translator->trans('app.addresses.' . ($address->getId() ? 'edit' 'add') . '.pageTitle'),
  181.             'html' => $this->renderView('account/address/form.html.twig', array(
  182.                 'form' => $form->createView(),
  183.             ))
  184.         ), $code);
  185.     }
  186.     #[Route('/account/address/form/{address}/delete'name'app_account_addresses_delete_form'options: ['expose' => true])]
  187.     public function getAddressDeleteForm(Request $requestEntityManagerInterface $emAddress $address null): Response
  188.     {
  189.         /** @var User $user */
  190.         if (!$user $this->getUser()) {
  191.             return $this->redirectToRoute('app_login');
  192.         }
  193.         if ($address && !$user->getAddresses()->contains($address)) {
  194.             return $this->redirectToRoute('app_account_addresses');
  195.         }
  196.         if (!$address) {
  197.             $address = new Address();
  198.             $address->setUser($this->getUser());
  199.         }
  200.         $form $this->createForm(DeleteConfirmationFormType::class, null, array(
  201.             'attr' => array(
  202.                 'data-submit-target' => $this->generateUrl('app_account_addresses_delete_form', array('address' => $address?->getId()))
  203.             )
  204.         ));
  205.         $form->handleRequest($request);
  206.         if ($form->isSubmitted() && $form->isValid()) {
  207.             $em->remove($address);
  208.             $em->flush();
  209.             $shippingForm $this->createForm(ShippingAddressFormType::class);
  210.             return new JsonResponse(array(
  211.                 'html' => array(
  212.                     'delivery' => $this->renderView('account/shipping/addresses.html.twig', array(
  213.                         'formElement' => $shippingForm->get('deliveryAddress')->createView(),
  214.                         'userDefaultAddress' => $this->getUser()->getDeliveryAddress()
  215.                     )),
  216.                     'billing' => $this->renderView('account/shipping/addresses.html.twig', array(
  217.                         'formElement' => $shippingForm->get('billingAddress')->createView(),
  218.                         'userDefaultAddress' => $this->getUser()->getBillingAddress()
  219.                     ))
  220.                 )
  221.             ));
  222.         }
  223.         return new JsonResponse(array(
  224.             'title' => $this->translator->trans('app.modals.deleteAddress.title'),
  225.             'html' => $this->renderView('account/modal/delete-address-confirmation.html.twig', array(
  226.                 'form' => $form->createView()
  227.             ))
  228.         ));
  229.     }
  230.     #[Route('/api/account/address/autocomplete/search'name'app_autocomplete_address'options: ['expose' => true], locale'fr')]
  231.     public function autocompleteAddress(Request $requestGoogleAPIService $googleAPIService): Response
  232.     {
  233.         if (!$request->get('input') || strlen($request->get('input')) < 3) return new JsonResponse([
  234.             'message' => 'La recherche doit faire minimum 3 caractères'
  235.         ], 400);
  236.         try {
  237.             return new JsonResponse(json_decode($googleAPIService->
  238.             autocomplete($request->get('input'), $request->get('sessionToken'), $this->getUser()?->getPreferredLocale())), 200);
  239.         } catch (Throwable) {
  240.             return new JsonResponse([
  241.                 'message' => 'Erreur lors de la requête à Google'
  242.             ], 400);
  243.         }
  244.     }
  245.     #[Route('api/region/search'name'app_search_region'options: ['expose' => true], locale'fr')]
  246.     public function searchRegion(Request $request): Response
  247.     {
  248.         $locale $this->getUser()?->getPreferredLocale() ?? $this->parameterBag->get('app.default_locale');
  249.         $input $request->get('input');
  250.         if($input){
  251.             if (strlen($input) < 2) return new JsonResponse([
  252.                 'message' => 'La recherche doit faire minimum 2 caractères'
  253.             ], 400);
  254.         }
  255.         try {
  256.             $countryCodes $request->get('countries');
  257.             if (!$countryCodes$countryCodes null;
  258.             $items array_map(fn(Region $r) => [
  259.                 'name' => $r->getName(),
  260.                 'code' => $r->getCode(),
  261.                 'country' => $r->getCountry()->getCode(),
  262.                 'id' => $r->getId(),
  263.                 'text' => $r->getName() . ' (' $r->getCountry()->getLocalizedName(locale$locale) . ')'
  264.             ], $this->em->getRepository(Region::class)->search($input$countryCodes));
  265.             return new JsonResponse(['items' => $items], 200);
  266.         } catch (Throwable) {
  267.             return new JsonResponse([
  268.                 'message' => 'Erreur lors de la requête à la base de données'
  269.             ], 400);
  270.         }
  271.     }
  272.     #[Route('/api/account/address/autocomplete/detail'name'app_autocomplete_place_detail'options: ['expose' => true], locale'fr')]
  273.     public function autocompletePlaceDetail(Request $requestGoogleAPIService $googleAPIService): Response
  274.     {
  275.         if (!$request->get('place_id')) return new JsonResponse([
  276.             'message' => 'L\'id de l\'adresse doit être renseigné'
  277.         ], 400);
  278.         $defaultErrorResponse = new JsonResponse([
  279.             'message' => 'Erreur lors de la requête à Google'
  280.         ], 400);
  281.         try {
  282.             $response json_decode($googleAPIService->placeDetail($request->get('place_id'), $request->get('sessionToken'), $this->getUser()?->getPreferredLocale()));
  283.         } catch (Throwable) {
  284.             return $defaultErrorResponse;
  285.         }
  286.         if ($response->status !== "OK") return $defaultErrorResponse;
  287.         return new JsonResponse($response200);
  288.     }
  289. }