<?php
namespace CoreBundle\Controller;
use CoreBundle\Model\Customer;
use Pimcore\Mail;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\DataObject\ProductStockInformCustomer;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class ProductStockInformCustomerController extends AbstractController
{
/**
* @param Request $request
* @return RedirectResponse
* @throws \Exception
*/
public function addAction(Request $request): RedirectResponse
{
$productId = $request->get('productId');
$product = DataObject::getById($productId);
$customer = $this->getUser();
if ($customer instanceof Customer) {
if ($this->entryExists($customer->getEmail(), $productId)) {
return $this->redirect($request->get('backurl') . '#entryAlreadyExistsCustomer__' . $productId);
}
$key = trim($customer->getEmail()) . '_' . $productId;
$email = $customer->getEmail();
$this->createObject($key, $email, $product, true);
} else {
$email = $request->get('email');
if ($this->entryExists($email, $productId)) {
$data = AbstractController::getProductStockInformCustomerExists(null, $productId, $email);
if (!$data['optInMailSent']) {
$this->sendOptInMail($email, $product);
}
if ($data['res'] && $data['lastSent'] !== null) {
$url = $this->generateUrl('shopHandlerProductDetail', ['name' => $product->getUrlTitle(), 'articlenumber' => $product->getArticleNumber(), 'email' => $email]);
return $this->redirect($url . '#entryAlreadyExistsEmailLastSent__' . $productId);
}
return $this->redirect($request->get('backurl') . '#entryAlreadyExistsEmail__' . $productId);
}
$key = trim($email) . '_' . $productId;
$this->createObject($key, $email, $product, true);
}
return $this->redirect($request->get('backurl') . '#add__' . $productId);
}
public function removeAction(Request $request)
{
}
/**
* @param Request $request
* @return RedirectResponse
* @throws \Exception
*/
public function renewAction(Request $request): RedirectResponse
{
$email = $request->get('email');
$productId = $request->get('productId');
$product = DataObject::getById($productId);
$customer = $this->getUser();
if ($customer instanceof Customer && !$email) {
$email = $customer->getEmail();
}
$listing = new ProductStockInformCustomer\Listing();
$listing->addConditionParam('email = ? AND product__id = ?', [$email, $productId]);
$key = trim($email) . '_' . $productId;
if ($listing->count() == 0) {
$this->createObject($key, $email, $product, true);
return $this->redirect($request->get('backurl') . '#new__' . $productId);
} elseif ($listing->count() == 1) {
$listing->load();
$productStockInformCustomer = $listing->getObjects()[0];
if ($productStockInformCustomer instanceof ProductStockInformCustomer) {
$productStockInformCustomer->setLastMailSent(null);
$productStockInformCustomer->save();
// Resend Opt-In Mail
if (!$productStockInformCustomer->getOptInMailSent()) {
$this->sendOptInMail($email, $product);
}
return $this->redirect($request->get('backurl') . '#renew__' . $productId);
}
}
return $this->redirect($request->get('backurl') . '#customerNotFound__' . $productId);
}
/**
* @param Request $request
* @return RedirectResponse
*/
public function optInAction(Request $request): RedirectResponse
{
$email = $request->get('email');
$productId = $request->get('productId');
$object = DataObject::getById($productId);
$listing = new ProductStockInformCustomer\Listing();
$listing->addConditionParam('email = ? AND product__id = ?', [$email, $productId]);
if ($listing->count() == 1) {
$listing->load();
$productStockInformCustomer = $listing->getObjects()[0];
/**
* @var ProductStockInformCustomer $productStockInformCustomer
*/
$productStockInformCustomer->setOptIn(true);
$productStockInformCustomer->save();
$url = $this->generateUrl('shopHandlerProductDetail', ['name' => $object->getUrlTitle(), 'articlenumber' => $object->getArticleNumber(), 'email' => $email]);
return $this->redirect($url . '#optInSuccess__' . $productId);
}
return $this->redirect('/');
}
/**
* @param string $email
* @param int $productId
* @return bool
*/
private function entryExists(string $email, int $productId): bool
{
$list = ProductStockInformCustomer::getByEmail($email);
//$list->load();
if ($list->count() > 0) {
foreach ($list as $entry) {
$product = $entry->getProduct();
if ($product) {
if ($productId == $product->getId()) {
return true;
}
}
}
}
return false;
}
/**
* @param string $key
* @param string $email
* @param DataObject $product
* @param bool $sendOptInEmail
* @throws \Exception
*/
private function createObject(string $key, string $email, DataObject $product, bool $sendOptInEmail = false)
{
$currentSide = Site::getCurrentSite();
$productStockInformCustomer = new ProductStockInformCustomer();
$productStockInformCustomer->setKey($key);
$productStockInformCustomer->setParent(Folder::getByPath('/productAvailabilityCustomers'));
$productStockInformCustomer->setEmail($email);
$productStockInformCustomer->setProduct($product);
$productStockInformCustomer->setSiteId($currentSide->getId());
$productStockInformCustomer->setPublished(true);
$productStockInformCustomer->save();
if ($sendOptInEmail) {
$this->sendOptInMail($email, $product);
}
}
/**
* @param string $email
* @param DataObject $product
* @throws \Exception
*/
private function sendOptInMail(string $email, DataObject $product)
{
$listing = new ProductStockInformCustomer\Listing();
$listing->addConditionParam('email = ? AND product__id = ?', [$email, $product->getId()]);
$listing->load();
$productStockInformCustomer = $listing->getObjects()[0];
if ($productStockInformCustomer instanceof ProductStockInformCustomer) {
$productStockInformCustomer->setOptInMailSent(true);
$productStockInformCustomer->save();
}
$params = [
'email' => $email,
'productId' => $product->getId(),
'productName' => $product->getName('de'),
];
$template = Site::getCurrentSite()->getRootPath() . '/mail/productStockInformCustomerOptIn';
$emailDoc = Document::getByPath($template);
$mail = new Mail(['document' => $emailDoc, 'params' => $params]);
$mail->setParams($params);
$mail->addTo($email);
$mail->send();
}
}