src/Elements/Bundle/DemiFrontendBundle/Controller/SavingRequestController.php line 240

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Bundle\DemiFrontendBundle\Controller;
  11. use Elements\Bundle\CDMUserManagerBundle\Templating\Helper\CDMPasswordHelper;
  12. use Elements\Demi\Accommodation\Search\Parameter;
  13. use Elements\Demi\Frontend\Service\Configuration;
  14. use Elements\Bundle\DemiFrontendBundle\Service\Customer;
  15. use Elements\Bundle\DemiFrontendBundle\Service\Newsletter;
  16. use Elements\Bundle\DemiFrontendBundle\Service\SavingRequest;
  17. use Elements\Demi\AbstractSavingRequest;
  18. use Elements\Demi\CustomerFactory;
  19. use Elements\Demi\DataObject\AbstractCustomer;
  20. use Elements\Demi\Deskline\SavingRequest\GroupRequest;
  21. use Elements\Demi\Deskline\SavingRequest\RequestPool;
  22. use Elements\Demi\Deskline\SavingRequest\ServiceProviderRequest;
  23. use Elements\Demi\Model\Region;
  24. use Elements\Demi\Model\Town;
  25. use Elements\Demi\SavingRequest\RequestPoolSettings;
  26. use Elements\Demi\SavingRequest\ServiceProviderRequestSettings;
  27. use ErrorException;
  28. use Exception;
  29. use Pimcore;
  30. use Pimcore\File;
  31. use Pimcore\Model\DataObject\Folder;
  32. use Pimcore\Model\Document;
  33. use Pimcore\Model\Document\Email;
  34. use Pimcore\Tool\DeviceDetector;
  35. use Symfony\Component\HttpFoundation\RedirectResponse;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Component\HttpFoundation\Response;
  38. use Symfony\Component\Security\Core\User\UserInterface;
  39. class SavingRequestController extends AbstractDemiController
  40. {
  41.     public function __construct(Configuration $configuration, protected SavingRequest $savingRequest, protected CustomerFactory $customerFactory,
  42.                                 protected Customer      $customerHelper, protected RequestPool $requestPool, protected GroupRequest $groupRequest, protected ServiceProviderRequest $serviceProviderRequest, protected Newsletter $newsletterHandler, protected ?CDMPasswordHelper $CDMPasswordHelper)
  43.     {
  44.         $this->setConfiguration($configuration);
  45.     }
  46.     public function savingRequestAction(Request $request, array $viewParams nullUserInterface $user nullbool $useFilter true): RedirectResponse|bool|Response
  47.     {
  48.         if (empty($this->configuration->getCurrentDocument())) {
  49.             $this->configuration->setDocument($this->document ?? $request->attributes->get('contentDocument'));
  50.         }
  51.         $listDoc $this->configuration->getAccoParentDocument();
  52.         $enabledFilters $this->configuration->getFilterBlockItems();
  53.         if ($useFilter) {
  54.             $documentFilterSnippet $this->document->getEditable('requestFilterSnippet');
  55.             if ($documentFilterSnippet !== null &&!$documentFilterSnippet->isEmpty()) {
  56.                 $elementArray $this->savingRequest->getSavingRequestElementsFromFilter($documentFilterSnippet$request$enabledFilters);
  57.             } else {
  58.                 $elementArray $listDoc $this->savingRequest->getSavingRequestElementsFromFilter($listDoc->getEditable('filterSnippet'), $request$enabledFilters) :
  59.                     $this->savingRequest->getSavingRequestElements($request);
  60.             }
  61.             $viewParams['stars'] = $elementArray['stars'];
  62.             $viewParams['countries'] = $elementArray['countries'];
  63.             $viewParams['salutations'] = $elementArray['salutations'];
  64.             $viewParams['roomFacilities'] = $elementArray['roomFacilities'];
  65.             $viewParams['facilities'] = $elementArray['facilities'];
  66.             $viewParams['categories'] = $elementArray['categories'];
  67.             $viewParams['marketingGroups'] = $elementArray['marketingGroups'];
  68.             $viewParams['holidayThemes'] = $elementArray['holidayThemes'];
  69.             $viewParams['mealTypes'] = $elementArray['mealTypes'];
  70.             $viewParams['towns'] = $elementArray['towns'];
  71.             $viewParams['regions'] = $elementArray['regions'];
  72.             $viewParams['user'] = $user;
  73.         }
  74.         if ($request->get('submitRequest')) {
  75.             $pp $request->get('savingRequestTypeService') === 'groupRequest' trim($this->configuration->getPrivacyPolicyGroup()) :
  76.                 trim($this->configuration->getPrivacyPolicyPool());
  77.             //privacy policy is no longer a checkbox
  78.             /*
  79.             $checkPp = !empty($pp);
  80.             if ($checkPp && !$request->get('pp')) {
  81.                 $viewParams['error'] = 'demi.saving-request.fail.privacy-policy';
  82.             }
  83.             */
  84.             if ($user && $request->get('mergeToAccount')) {
  85.                 $customer $this->customerHelper->updateCustomerFromRequest($request$user)['customer'];
  86.             } else {
  87.                 if ($request->get('createAccount') === 'on' && !$this->customerHelper->validateCustomerAccount($request)) {
  88.                     $viewParams['error'] = 'demi.saving-request.fail-reload';
  89.                 }
  90.                 $tmpCustomer $request->get('createAccount') === 'on' $this->customerFactory->createCustomer() : $this->customerHelper->getInactiveCustomerByMail($request->get('email'));
  91.                 $customer $this->customerHelper->createCustomerFromRequest($request$tmpCustomer);
  92.             }
  93.             /**
  94.              * @var mixed $ignoreCountry
  95.              * legacy issue - can be null | true | false
  96.              */
  97.             $ignoreCountry null;
  98.             $service $request->query->get('savingRequestTypeService');
  99.             $customOriginator $this->document->getProperty('demi_customOriginator');
  100.             if($customOriginator){
  101.                 $customer->setProperty('demi_customOriginator''text'$customOriginator);
  102.             }
  103.             $savingRequest $this->$service;
  104.             $savingRequest->setCustomOriginator($customOriginator);
  105.             if ($request->get('contactType') || $request->get('maxTickets')) {
  106.                 $savingRequest->setRequestPoolSettings(new RequestPoolSettings(
  107.                     $request->get('contactType'), $request->get('maxTickets')
  108.                 ));
  109.             }
  110.             if ($request->get('enquire')) {
  111.                 $savingRequest->setServiceProviderRequestSettings(new ServiceProviderRequestSettings(
  112.                     $request->get('accoIds'),
  113.                     $request->get('productId')
  114.                 ));
  115.                 $ignoreCountry true;
  116.             }
  117.             $this->customerHelper->transferToExternalSystem($customer$ignoreCountry);
  118.             $savingRequest->setComment($request->get('comment'));
  119.             $savingRequest->setCustomer($customer);
  120.             if ($this->document->getProperty('demi_linkkey')) {
  121.                 $savingRequest->setLinkKey($this->document->getProperty('demi_linkkey'));
  122.             }
  123.             $searchParams $this->savingRequest->createSearchParams($request);
  124.             if (empty($searchParams->getTowns()) && empty($searchParams->getRegions()) && empty($searchParams->getDistricts())) {
  125.                 $regionIds = [];
  126.                 $townIds = [];
  127.                 if (empty($elementArray['regions']) && empty($elementArray['towns'])) {
  128.                     //default set all regions if no configured towns or regions in filter, one of the parameters needs to be set!
  129.                     $demiRegions Region::getBySystemIdentifier('Deskline');
  130.                     foreach ($demiRegions as $region) {
  131.                         $regionIds[] = $region->getId();
  132.                     }
  133.                     if (empty($regionIds)) {
  134.                         $demiTowns Town::getBySystemIdentifier('Deskline');
  135.                         foreach ($demiTowns as $demiTown) {
  136.                             $townIds[] = $demiTown->getId();
  137.                         }
  138.                     }
  139.                 } else {
  140.                     if(!empty($elementArray['regions'])){
  141.                         //set all regions available in filter if none selected by user
  142.                         foreach ($elementArray['regions'] as $region) {
  143.                             $regionIds[] = $region->getId();
  144.                         }
  145.                     }
  146.                     if(!empty($elementArray['towns'])){
  147.                         //set all towns available in filter if none selected by user
  148.                         foreach ($elementArray['towns'] as $town) {
  149.                             $townIds[] = $town->getId();
  150.                         }
  151.                     }
  152.                 }
  153.                 if (!empty($townIds)) {
  154.                     $searchParams->setTowns($townIds);
  155.                 } else {
  156.                     $searchParams->setRegions($regionIds);
  157.                 }
  158.             }
  159.             $savingRequest->setSearchParams($searchParams);
  160.             try {
  161.                 $savingRequest->save();
  162.                 $this->handleCustomerCreation($request$customer$request->get('createAccount') === 'on');
  163.                 if ($this->newsletterHandler && $request->get('newsletter') === 'on') {
  164.                     try {
  165.                         //handle newsletter subscripton
  166.                         $modifiedCustomer $this->newsletterHandler->addRecipient($customer->getEmail(), $customer->getFirstname(), $customer->getLastname(), ['request' => $request], $user$customer);
  167.                         $modifiedCustomer->save();
  168.                     } catch (Exception $exception) {
  169.                         if (Pimcore::inDebugMode()) {
  170.                             //silently ignore newsletter subscription errors in live mode, show them in debug
  171.                             throw $exception;
  172.                         }
  173.                     }
  174.                 }
  175.                 if (!($savingRequest instanceof ServiceProviderRequest)) {
  176.                     $this->addResponseHeader('X-Robots-Tag''noindex,nofollow');
  177.                     return $this->redirect($request->query->get('thxPage'));
  178.                 }
  179.             } catch (Exception $exception) {
  180.                 if (!($savingRequest instanceof ServiceProviderRequest)) {
  181.                     $viewParams['error'] = 'demi.saving-request.fail';
  182.                 } else {
  183.                     throw $exception;
  184.                 }
  185.             }
  186.             if ($savingRequest instanceof ServiceProviderRequest) {
  187.                 return true;
  188.             }
  189.         }
  190.         $deviceDetector DeviceDetector::getInstance();
  191.         return $this->renderTemplate('@ElementsDemiFrontend/SavingRequest/saving-request.'.($deviceDetector->isPhone() ? 'mobile' 'desktop').'.html.twig'$viewParams);
  192.     }
  193.     /**
  194.      * @param Request $request
  195.      * @param AbstractCustomer $customer
  196.      * @param $createAccount
  197.      *
  198.      * @throws Exception
  199.      */
  200.     protected function handleCustomerCreation(Request $requestAbstractCustomer $customerbool $createAccount): void
  201.     {
  202.         if (empty($customer->getKey())) {
  203.             $customer->setParent(Folder::getByPath($this->customerFactory->getCustomerParent()));
  204.             $customer->setPublished(true);
  205.             $customer->setKey(Pimcore\Model\Element\Service::getValidKey(uniqid($customer->getEmail() . '_'), 'object'));
  206.         }
  207.         $customer->save();
  208.         if ($createAccount) {
  209.             try {
  210.                 $emailDoc $this->configuration->getCreatePasswordDocument();
  211.                 if ($this->CDMPasswordHelper && method_exists($this->CDMPasswordHelper'initPassword') && $customer instanceof \Pimcore\Model\DataObject\Customer && $emailDoc && $emailDoc instanceof Email) {
  212.                     $this->CDMPasswordHelper->initPassword($customer$emailDoc);
  213.                 }
  214.             } catch (Exception $exception) { /* this should actually not happen if a valid $emailDoc is set */
  215.             }
  216.         }
  217.     }
  218.     public function poolRequestAction(Request $requestUserInterface $user null): RedirectResponse|bool|Response
  219.     {
  220.         if (empty($this->configuration->getCurrentDocument())) {
  221.             $this->configuration->setDocument($this->document ?? $request->attributes->get('contentDocument'));
  222.         }
  223.         if (!$this->editmode && !$this->configuration->getPoolRequestThxPage()) {
  224.             throw new ErrorException('Pool Request thx page is not configured in demiConfig');
  225.         }
  226.         if ($this->configuration->getPoolRequestThxPage() instanceof Document) {
  227.             if ($request->request->get('affiliate')) {
  228.                 $request->query->set('thxPage'$this->configuration->getPoolRequestThxPage()->getFullPath() . '?affiliate=1');
  229.             } else {
  230.                 $request->query->set('thxPage'$this->configuration->getPoolRequestThxPage()->getFullPath());
  231.             }
  232.         }
  233.         $request->query->set('savingRequestTypeService''requestPool');
  234.         $viewParams["possibleNights"] = $this->configuration->getPoolRequestPossibleNights();
  235.         $viewParams["possibleAdults"] = $this->configuration->getPoolRequestPossibleAdults();
  236.         $viewParams["possibleChildren"] = $this->configuration->getPoolRequestPossibleChildren();
  237.         return $this->savingRequestAction($request$viewParams$user);
  238.     }
  239.     /**
  240.      * @param Request $request
  241.      * @param UserInterface|null $user
  242.      *
  243.      * @return Response
  244.      *
  245.      * @throws ErrorException
  246.      */
  247.     public function groupRequestAction(Request $requestUserInterface $user null)
  248.     {
  249.         if (empty($this->configuration->getCurrentDocument())) {
  250.             $this->configuration->setDocument($this->document ?? $request->attributes->get('contentDocument'));
  251.         }
  252.         if (!$this->editmode && !$this->configuration->getGroupRequestThxPage()) {
  253.             throw new ErrorException('Group Request thx page is not configured in demiConfig');
  254.         }
  255.         $viewParams["isGroupRequest"] = true;
  256.         if ($this->configuration->getGroupRequestThxPage() instanceof Document) {
  257.             if ($request->request->get('affiliate')) {
  258.                 $request->query->set('thxPage'$this->configuration->getGroupRequestThxPage()->getFullPath() . '?affiliate=1');
  259.             } else {
  260.                 $request->query->set('thxPage'$this->configuration->getGroupRequestThxPage()->getFullPath());
  261.             }
  262.         }
  263.         $request->query->set('savingRequestTypeService''groupRequest');
  264.         $viewParams["possibleNights"] = $this->configuration->getGroupRequestPossibleNights();
  265.         $viewParams["possibleAdults"] = $this->configuration->getGroupRequestPossibleAdults();
  266.         $viewParams["possibleChildren"] = $this->configuration->getGroupRequestPossibleChildren();
  267.         return $this->savingRequestAction($request$viewParams$user);
  268.     }
  269.     public function serviceProviderRequestAction(Request $requestUserInterface $user null): bool
  270.     {
  271.         $request->query->set('savingRequestTypeService''serviceProviderRequest');
  272.         if ($request->get('u0') === null) {
  273.             $request->query->set('u0''1');
  274.             $request->query->set('a0''1');
  275.         }
  276.         return $this->savingRequestAction($request, [],  $userfalse);
  277.     }
  278. }