src/CoreBundle/Controller/ProductStockInformCustomerController.php line 135

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Controller;
  3. use CoreBundle\Model\Customer;
  4. use Pimcore\Mail;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Model\DataObject\Folder;
  7. use Pimcore\Model\DataObject\ProductStockInformCustomer;
  8. use Pimcore\Model\Document;
  9. use Pimcore\Model\Site;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. class ProductStockInformCustomerController extends AbstractController
  13. {
  14.     /**
  15.      * @param Request $request
  16.      * @return RedirectResponse
  17.      * @throws \Exception
  18.      */
  19.     public function addAction(Request $request): RedirectResponse
  20.     {
  21.         $productId $request->get('productId');
  22.         $product DataObject::getById($productId);
  23.         $customer $this->getUser();
  24.         if ($customer instanceof Customer) {
  25.             if ($this->entryExists($customer->getEmail(), $productId)) {
  26.                 return $this->redirect($request->get('backurl') . '#entryAlreadyExistsCustomer__' $productId);
  27.             }
  28.             $key trim($customer->getEmail()) . '_' $productId;
  29.             $email $customer->getEmail();
  30.             $this->createObject($key$email$producttrue);
  31.         } else {
  32.             $email $request->get('email');
  33.             if ($this->entryExists($email$productId)) {
  34.                 $data AbstractController::getProductStockInformCustomerExists(null$productId$email);
  35.                 if (!$data['optInMailSent']) {
  36.                     $this->sendOptInMail($email$product);
  37.                 }
  38.                 if ($data['res'] && $data['lastSent'] !== null) {
  39.                     $url $this->generateUrl('shopHandlerProductDetail', ['name' => $product->getUrlTitle(), 'articlenumber' => $product->getArticleNumber(), 'email' => $email]);
  40.                     return $this->redirect($url '#entryAlreadyExistsEmailLastSent__' $productId);
  41.                 }
  42.                 return $this->redirect($request->get('backurl') . '#entryAlreadyExistsEmail__' $productId);
  43.             }
  44.             $key trim($email) . '_' $productId;
  45.             $this->createObject($key$email$producttrue);
  46.         }
  47.         return $this->redirect($request->get('backurl') . '#add__' $productId);
  48.     }
  49.     public function removeAction(Request $request)
  50.     {
  51.     }
  52.     /**
  53.      * @param Request $request
  54.      * @return RedirectResponse
  55.      * @throws \Exception
  56.      */
  57.     public function renewAction(Request $request): RedirectResponse
  58.     {
  59.         $email $request->get('email');
  60.         $productId $request->get('productId');
  61.         $product DataObject::getById($productId);
  62.         $customer $this->getUser();
  63.         if ($customer instanceof Customer && !$email) {
  64.             $email $customer->getEmail();
  65.         }
  66.         $listing = new ProductStockInformCustomer\Listing();
  67.         $listing->addConditionParam('email = ? AND product__id = ?', [$email$productId]);
  68.         $key trim($email) . '_' $productId;
  69.         if ($listing->count() == 0) {
  70.             $this->createObject($key$email$producttrue);
  71.             return $this->redirect($request->get('backurl') . '#new__' $productId);
  72.         } elseif ($listing->count() == 1) {
  73.             $listing->load();
  74.             $productStockInformCustomer $listing->getObjects()[0];
  75.             if ($productStockInformCustomer instanceof ProductStockInformCustomer) {
  76.                 $productStockInformCustomer->setLastMailSent(null);
  77.                 $productStockInformCustomer->save();
  78.                 // Resend Opt-In Mail
  79.                 if (!$productStockInformCustomer->getOptInMailSent()) {
  80.                     $this->sendOptInMail($email$product);
  81.                 }
  82.                 return $this->redirect($request->get('backurl') . '#renew__' $productId);
  83.             }
  84.         }
  85.         return $this->redirect($request->get('backurl') . '#customerNotFound__' $productId);
  86.     }
  87.     /**
  88.      * @param Request $request
  89.      * @return RedirectResponse
  90.      */
  91.     public function optInAction(Request $request): RedirectResponse
  92.     {
  93.         $email $request->get('email');
  94.         $productId $request->get('productId');
  95.         $object DataObject::getById($productId);
  96.         $listing = new ProductStockInformCustomer\Listing();
  97.         $listing->addConditionParam('email = ? AND product__id = ?', [$email$productId]);
  98.         if ($listing->count() == 1) {
  99.             $listing->load();
  100.             $productStockInformCustomer $listing->getObjects()[0];
  101.             /**
  102.              * @var ProductStockInformCustomer $productStockInformCustomer
  103.              */
  104.             $productStockInformCustomer->setOptIn(true);
  105.             $productStockInformCustomer->save();
  106.             $url $this->generateUrl('shopHandlerProductDetail', ['name' => $object->getUrlTitle(), 'articlenumber' => $object->getArticleNumber(), 'email' => $email]);
  107.             return $this->redirect($url '#optInSuccess__' $productId);
  108.         }
  109.         return $this->redirect('/');
  110.     }
  111.     /**
  112.      * @param string $email
  113.      * @param int $productId
  114.      * @return bool
  115.      */
  116.     private function entryExists(string $emailint $productId): bool
  117.     {
  118.         $list ProductStockInformCustomer::getByEmail($email);
  119.         //$list->load();
  120.         if ($list->count() > 0) {
  121.             foreach ($list as $entry) {
  122.                 $product $entry->getProduct();
  123.                 if ($product) {
  124.                     if ($productId == $product->getId()) {
  125.                         return true;
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.         return false;
  131.     }
  132.     /**
  133.      * @param string $key
  134.      * @param string $email
  135.      * @param DataObject $product
  136.      * @param bool $sendOptInEmail
  137.      * @throws \Exception
  138.      */
  139.     private function createObject(string $keystring $emailDataObject $productbool $sendOptInEmail false)
  140.     {
  141.         $currentSide Site::getCurrentSite();
  142.         $productStockInformCustomer = new ProductStockInformCustomer();
  143.         $productStockInformCustomer->setKey($key);
  144.         $productStockInformCustomer->setParent(Folder::getByPath('/productAvailabilityCustomers'));
  145.         $productStockInformCustomer->setEmail($email);
  146.         $productStockInformCustomer->setProduct($product);
  147.         $productStockInformCustomer->setSiteId($currentSide->getId());
  148.         $productStockInformCustomer->setPublished(true);
  149.         $productStockInformCustomer->save();
  150.         if ($sendOptInEmail) {
  151.             $this->sendOptInMail($email$product);
  152.         }
  153.     }
  154.     /**
  155.      * @param string $email
  156.      * @param DataObject $product
  157.      * @throws \Exception
  158.      */
  159.     private function sendOptInMail(string $emailDataObject $product)
  160.     {
  161.         $listing = new ProductStockInformCustomer\Listing();
  162.         $listing->addConditionParam('email = ? AND product__id = ?', [$email$product->getId()]);
  163.         $listing->load();
  164.         $productStockInformCustomer $listing->getObjects()[0];
  165.         if ($productStockInformCustomer instanceof ProductStockInformCustomer) {
  166.             $productStockInformCustomer->setOptInMailSent(true);
  167.             $productStockInformCustomer->save();
  168.         }
  169.         $params = [
  170.             'email' => $email,
  171.             'productId' => $product->getId(),
  172.             'productName' => $product->getName('de'),
  173.         ];
  174.         $template Site::getCurrentSite()->getRootPath() . '/mail/productStockInformCustomerOptIn';
  175.         $emailDoc Document::getByPath($template);
  176.         $mail = new Mail(['document' => $emailDoc'params' => $params]);
  177.         $mail->setParams($params);
  178.         $mail->addTo($email);
  179.         $mail->send();
  180.     }
  181. }