vendor/coreshop/core-shop/src/CoreShop/Bundle/FrontendBundle/Controller/CustomerController.php line 338

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * CoreShop
  5.  *
  6.  * This source file is available under two different licenses:
  7.  *  - GNU General Public License version 3 (GPLv3)
  8.  *  - CoreShop Commercial License (CCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.org)
  13.  * @license    https://www.coreshop.org/license     GPLv3 and CCL
  14.  *
  15.  */
  16. namespace CoreShop\Bundle\FrontendBundle\Controller;
  17. use CoreShop\Bundle\AddressBundle\Form\Type\AddressType;
  18. use CoreShop\Bundle\CustomerBundle\Form\Type\ChangePasswordType;
  19. use CoreShop\Bundle\CustomerBundle\Form\Type\CustomerType;
  20. use CoreShop\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  21. use CoreShop\Component\Address\Model\AddressIdentifierInterface;
  22. use CoreShop\Component\Address\Model\AddressInterface;
  23. use CoreShop\Component\Core\Customer\Address\AddressAssignmentManagerInterface;
  24. use CoreShop\Component\Core\Model\CustomerInterface;
  25. use CoreShop\Component\Customer\Context\CustomerContextInterface;
  26. use CoreShop\Component\Order\Model\OrderInterface;
  27. use CoreShop\Component\Pimcore\DataObject\VersionHelper;
  28. use CoreShop\Component\User\Model\UserInterface;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\Response;
  31. class CustomerController extends FrontendController
  32. {
  33.     public function headerAction(Request $request): Response
  34.     {
  35.         return $this->render($this->templateConfigurator->findTemplate('Customer/_header.html'), [
  36.             'catalogMode' => false,
  37.             'customer' => $this->getCustomer(),
  38.         ]);
  39.     }
  40.     public function footerAction(): Response
  41.     {
  42.         return $this->render($this->templateConfigurator->findTemplate('Customer/_footer.html'), [
  43.             'catalogMode' => false,
  44.             'customer' => $this->getCustomer(),
  45.         ]);
  46.     }
  47.     public function profileAction(): Response
  48.     {
  49.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE');
  50.         $customer $this->getCustomer();
  51.         if (!$customer instanceof CustomerInterface) {
  52.             return $this->redirectToRoute('coreshop_index');
  53.         }
  54.         return $this->render($this->templateConfigurator->findTemplate('Customer/profile.html'), [
  55.             'customer' => $customer,
  56.         ]);
  57.     }
  58.     public function ordersAction(): Response
  59.     {
  60.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_ORDERS');
  61.         $customer $this->getCustomer();
  62.         if (!$customer instanceof CustomerInterface) {
  63.             return $this->redirectToRoute('coreshop_index');
  64.         }
  65.         return $this->render($this->templateConfigurator->findTemplate('Customer/orders.html'), [
  66.             'customer' => $customer,
  67.             'orders' => $this->get('coreshop.repository.order')->findOrdersByCustomer($this->getCustomer()),
  68.         ]);
  69.     }
  70.     public function orderDetailAction(Request $request): Response
  71.     {
  72.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_ORDER_DETAIL');
  73.         $orderId $this->getParameterFromRequest($request'order');
  74.         $customer $this->getCustomer();
  75.         if (!$customer instanceof CustomerInterface) {
  76.             return $this->redirectToRoute('coreshop_index');
  77.         }
  78.         $order $this->get('coreshop.repository.order')->find($orderId);
  79.         if (!$order instanceof OrderInterface) {
  80.             return $this->redirectToRoute('coreshop_customer_orders');
  81.         }
  82.         if (!$order->getCustomer() instanceof CustomerInterface || $order->getCustomer()->getId() !== $customer->getId()) {
  83.             return $this->redirectToRoute('coreshop_customer_orders');
  84.         }
  85.         return $this->render($this->templateConfigurator->findTemplate('Customer/order_detail.html'), [
  86.             'customer' => $customer,
  87.             'order' => $order,
  88.         ]);
  89.     }
  90.     public function addressesAction(): Response
  91.     {
  92.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_ADDRESSES');
  93.         $customer $this->getCustomer();
  94.         if (!$customer instanceof CustomerInterface) {
  95.             return $this->redirectToRoute('coreshop_index');
  96.         }
  97.         return $this->render($this->templateConfigurator->findTemplate('Customer/addresses.html'), [
  98.             'customer' => $customer,
  99.         ]);
  100.     }
  101.     public function addressAction(Request $request): Response
  102.     {
  103.         $customer $this->getCustomer();
  104.         if (!$customer instanceof CustomerInterface) {
  105.             return $this->redirectToRoute('coreshop_index');
  106.         }
  107.         $addressId $this->getParameterFromRequest($request'address');
  108.         $address $this->get('coreshop.repository.address')->find($addressId);
  109.         if ($address instanceof AddressInterface) {
  110.             $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_ADDRESS_EDIT');
  111.         } else {
  112.             $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_ADDRESS_ADD');
  113.         }
  114.         $addressAssignmentManager $this->get(AddressAssignmentManagerInterface::class);
  115.         $eventType 'update';
  116.         if (!$address instanceof AddressInterface) {
  117.             $eventType 'add';
  118.             /** @var AddressInterface $address */
  119.             $address $this->get('coreshop.factory.address')->createNew();
  120.             if ($request->query->has('address_identifier')) {
  121.                 $addressIdentifier $this->get('coreshop.repository.address_identifier')->findByName($request->query->get('address_identifier'));
  122.                 if ($addressIdentifier instanceof AddressIdentifierInterface) {
  123.                     $address->setAddressIdentifier($addressIdentifier);
  124.                 }
  125.             }
  126.         }
  127.         if ($eventType === 'update' && $addressAssignmentManager->checkAddressAffiliationPermissionForCustomer($customer$address) === false) {
  128.             return $this->redirectToRoute('coreshop_customer_addresses');
  129.         }
  130.         $addressFormOptions = [
  131.             'available_affiliations' => $addressAssignmentManager->getAddressAffiliationTypesForCustomer($customer),
  132.             'selected_affiliation' => $addressAssignmentManager->detectAddressAffiliationForCustomer($customer$address),
  133.         ];
  134.         $form $this->get('form.factory')->createNamed('coreshop'AddressType::class, $address$addressFormOptions);
  135.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  136.             $handledForm $form->handleRequest($request);
  137.             $addressAffiliation $form->has('addressAffiliation') ? $form->get('addressAffiliation')->getData() : null;
  138.             if ($handledForm->isSubmitted() && $handledForm->isValid()) {
  139.                 $address $handledForm->getData();
  140.                 $address->setPublished(true);
  141.                 $address->setKey(uniqid());
  142.                 $address $addressAssignmentManager->allocateAddressByAffiliation($customer$address$addressAffiliation);
  143.                 $this->fireEvent($request$addresssprintf('%s.%s.%s_post''coreshop''address'$eventType));
  144.                 $this->addFlash('success'$this->get('translator')->trans(sprintf('coreshop.ui.customer.address_successfully_%s'$eventType === 'add' 'added' 'updated')));
  145.                 return $this->redirect(
  146.                     $this->getParameterFromRequest($request'_redirect'$this->generateUrl('coreshop_customer_addresses')),
  147.                 );
  148.             }
  149.         }
  150.         return $this->render($this->templateConfigurator->findTemplate('Customer/address.html'), [
  151.             'address' => $address,
  152.             'customer' => $customer,
  153.             'form' => $form->createView(),
  154.         ]);
  155.     }
  156.     public function addressDeleteAction(Request $request): Response
  157.     {
  158.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_ADDRESS_DELETE');
  159.         $customer $this->getCustomer();
  160.         $addressAssignmentManager $this->get(AddressAssignmentManagerInterface::class);
  161.         if (!$customer instanceof CustomerInterface) {
  162.             return $this->redirectToRoute('coreshop_index');
  163.         }
  164.         $address $this->get('coreshop.repository.address')->find(
  165.             $this->getParameterFromRequest($request'address'),
  166.         );
  167.         if (!$address instanceof AddressInterface) {
  168.             return $this->redirectToRoute('coreshop_customer_addresses');
  169.         }
  170.         if ($addressAssignmentManager->checkAddressAffiliationPermissionForCustomer($customer$address) === false) {
  171.             return $this->redirectToRoute('coreshop_customer_addresses');
  172.         }
  173.         $this->fireEvent($request$addresssprintf('%s.%s.%s_pre''coreshop''address''delete'));
  174.         $address->delete();
  175.         $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.customer.address_successfully_deleted'));
  176.         return $this->redirectToRoute('coreshop_customer_addresses');
  177.     }
  178.     public function settingsAction(Request $request): Response
  179.     {
  180.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_SETTINGS');
  181.         $customer $this->getCustomer();
  182.         if (!$customer instanceof CustomerInterface) {
  183.             return $this->redirectToRoute('coreshop_index');
  184.         }
  185.         $form $this->get('form.factory')->createNamed('coreshop'CustomerType::class, $customer, [
  186.             'customer' => $customer->getId(),
  187.             'allow_default_address' => true,
  188.         ]);
  189.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  190.             $handledForm $form->handleRequest($request);
  191.             if ($handledForm->isSubmitted() && $handledForm->isValid()) {
  192.                 $customer $handledForm->getData();
  193.                 $customer->save();
  194.                 $this->fireEvent($request$customersprintf('%s.%s.%s_post''coreshop''customer''update'));
  195.                 $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.customer.profile_successfully_updated'));
  196.                 return $this->redirectToRoute('coreshop_customer_profile');
  197.             }
  198.         }
  199.         return $this->render($this->templateConfigurator->findTemplate('Customer/settings.html'), [
  200.             'customer' => $customer,
  201.             'form' => $form->createView(),
  202.         ]);
  203.     }
  204.     public function changePasswordAction(Request $request): Response
  205.     {
  206.         $this->denyAccessUnlessGranted('CORESHOP_CUSTOMER_PROFILE_CHANGE_PASSWORD');
  207.         $customer $this->getCustomer();
  208.         if (!$customer instanceof CustomerInterface) {
  209.             return $this->redirectToRoute('coreshop_index');
  210.         }
  211.         if (!$customer->getUser() instanceof UserInterface) {
  212.             return $this->redirectToRoute('coreshop_index');
  213.         }
  214.         $form $this->get('form.factory')->createNamed('coreshop'ChangePasswordType::class);
  215.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  216.             $handledForm $form->handleRequest($request);
  217.             if ($handledForm->isSubmitted() && $handledForm->isValid()) {
  218.                 $formData $handledForm->getData();
  219.                 $customer->getUser()->setPassword($formData['password']);
  220.                 $customer->getUser()->save();
  221.                 $this->fireEvent($request$customer->getUser(), sprintf('%s.%s.%s_post''coreshop''user''change_password'));
  222.                 $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.customer.password_successfully_changed'));
  223.                 return $this->redirectToRoute('coreshop_customer_profile');
  224.             }
  225.         }
  226.         return $this->render($this->templateConfigurator->findTemplate('Customer/change_password.html'), [
  227.             'customer' => $customer,
  228.             'form' => $form->createView(),
  229.         ]);
  230.     }
  231.     public function confirmNewsletterAction(Request $request): Response
  232.     {
  233.         $success false;
  234.         $token $this->getParameterFromRequest($request'token');
  235.         $newsletterUser null;
  236.         if (!$token) {
  237.             return $this->redirectToRoute('coreshop_index');
  238.         }
  239.         /**
  240.          * @var CustomerInterface $customer
  241.          */
  242.         $customer $this->get('coreshop.repository.customer')->findByNewsletterToken($token);
  243.         if ($customer instanceof CustomerInterface) {
  244.             $customer->setNewsletterConfirmed(true);
  245.             $customer->setNewsletterToken(null);
  246.             VersionHelper::useVersioning(function () use ($customer) {
  247.                 $customer->save();
  248.             }, false);
  249.             $this->fireEvent($request$customersprintf('%s.%s.%s_post''coreshop''customer''newsletter_confirm'));
  250.             $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.newsletter_confirmed'));
  251.             $success true;
  252.         } else {
  253.             $this->addFlash('error'$this->get('translator')->trans('coreshop.ui.newsletter_confirmation_error'));
  254.         }
  255.         return $this->render($this->templateConfigurator->findTemplate('Customer/confirm_newsletter.html'), [
  256.             'newsletterUser' => $newsletterUser,
  257.             'success' => $success,
  258.         ]);
  259.     }
  260.     protected function getCustomer(): ?CustomerInterface
  261.     {
  262.         try {
  263.             /**
  264.              * @var CustomerInterface $customer
  265.              */
  266.             $customer $this->get(CustomerContextInterface::class)->getCustomer();
  267.             return $customer;
  268.         } catch (\Exception) {
  269.             // fail silently
  270.         }
  271.         return null;
  272.     }
  273.     protected function fireEvent(Request $requestmixed $objectstring $eventName): void
  274.     {
  275.         //@todo: move this to a resource controller event
  276.         $event = new ResourceControllerEvent($object, ['request' => $request]);
  277.         $this->get('event_dispatcher')->dispatch($event$eventName);
  278.     }
  279. }