134 lines
4.0 KiB
PHP
134 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace KupShop\KupShopBundle\Email;
|
|
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\DomainContext;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\WatchdogBundle\Util\Watchdog;
|
|
|
|
class WatchdogEmail extends BaseEmail
|
|
{
|
|
protected static $name = 'Hlídané zboží';
|
|
protected static $type = 'WATCHDOG';
|
|
protected static $priority = 0;
|
|
|
|
protected $subject = 'Hlídané zboží můžete nyní objednat';
|
|
protected $template = 'email/watchdog.tpl';
|
|
|
|
/** @var Product[] */
|
|
protected $products;
|
|
|
|
protected $user;
|
|
|
|
protected $userHash;
|
|
|
|
protected ?Watchdog $watchdog = null;
|
|
|
|
public static function isAllowed()
|
|
{
|
|
return findModule(\Modules::WATCHDOG);
|
|
}
|
|
|
|
/** @required */
|
|
public function setWatchdog(?Watchdog $watchdog): void
|
|
{
|
|
$this->watchdog = $watchdog;
|
|
}
|
|
|
|
public function setProducts($products)
|
|
{
|
|
$this->products = $products;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setUser($user)
|
|
{
|
|
$this->user = $user;
|
|
$this->userHash = $this->watchdog->getWatchdogHash($user['id']);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function testEmail(): array
|
|
{
|
|
$userId = ['id_user' => getVal('userId')];
|
|
if (!$userId['id_user']) {
|
|
$userId = sqlFetch(sqlQuery('SELECT id_user FROM products_watchdog GROUP BY id_user ORDER BY COUNT(id_user) DESC, RAND() LIMIT 1'));
|
|
}
|
|
$user = \User::createFromId($userId['id_user']);
|
|
|
|
$ids = sqlFetchAll(sqlQuery('SELECT id_product, id_variation FROM products_watchdog WHERE id_user = '.$user->id.' ORDER BY RAND() LIMIT 3'));
|
|
$products = [];
|
|
$return = null;
|
|
$contextManager = ServiceContainer::getService(ContextManager::class);
|
|
$contextManager->activateContexts(
|
|
[
|
|
LanguageContext::class => $user->id_language ?? null,
|
|
DomainContext::class => $contextManager->getDomainFromLanguage($user->id_language ?? null),
|
|
],
|
|
function () use (&$products, $ids, $user, &$return) {
|
|
foreach ($ids as $watchdogItem) {
|
|
$product = \Variation::createProductOrVariation($watchdogItem['id_product'], $watchdogItem['id_variation'] ?? null);
|
|
|
|
$product->createFromDB($watchdogItem['id_product']);
|
|
$product->fetchProducer();
|
|
// add some random data that are fetched to products when watchdog is being sent - so we are able to test email completely
|
|
$product->inStore = rand(1, 10);
|
|
$product->watchdogPrice = toDecimal(rand(10, 999));
|
|
$product->watchdogAvailability = 1;
|
|
$products[] = $product;
|
|
}
|
|
|
|
$this->setProducts($products);
|
|
$this->setUser($user);
|
|
|
|
$return = parent::testEmail();
|
|
});
|
|
|
|
return $return;
|
|
}
|
|
|
|
public function renderEmail($template, $data = [], $base_template = 'watchdog.tpl'): array
|
|
{
|
|
if ($this->products) {
|
|
$data = array_merge($data, ['products' => $this->products]);
|
|
}
|
|
|
|
if ($this->user) {
|
|
$data = array_merge($data, ['user' => $this->user]);
|
|
}
|
|
|
|
if ($this->userHash) {
|
|
$data = array_merge($data, ['userHash' => $this->userHash]);
|
|
}
|
|
|
|
return parent::renderEmail($template, $data, $base_template);
|
|
}
|
|
|
|
public static function getPlaceholders()
|
|
{
|
|
$placeholders = [
|
|
'PRODUKTY' => [
|
|
'text' => 'Hlídané zboží',
|
|
],
|
|
];
|
|
|
|
return [self::$type => $placeholders] + (parent::getPlaceholders() ?? []);
|
|
}
|
|
|
|
public function __wakeup()
|
|
{
|
|
parent::__wakeup();
|
|
|
|
$this->watchdog = ServiceContainer::getService(Watchdog::class);
|
|
}
|
|
|
|
public function __sleep()
|
|
{
|
|
return array_diff(parent::__sleep(), ['watchdog']);
|
|
}
|
|
}
|