-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStockAlert.php
More file actions
161 lines (143 loc) · 6.25 KB
/
StockAlert.php
File metadata and controls
161 lines (143 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace StockAlert;
use Propel\Runtime\Connection\ConnectionInterface;
use StockAlert\Model\RestockingAlertQuery;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Core\Translation\Translator;
use Thelia\Install\Database;
use Thelia\Model\ConfigQuery;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Module\BaseModule;
/**
* Class StockAlert
* @package StockAlert
* @author Baixas Alban <abaixas@openstudio.fr>
*/
class StockAlert extends BaseModule
{
const MESSAGE_DOMAIN = "stockalert";
const CONFIG_ENABLED = "stockalert_enabled";
const CONFIG_THRESHOLD = "stockalert_threshold";
const CONFIG_EMAILS = "stockalert_emails";
const CONFIG_NOTIFY = "stockalert_notify";
const DEFAULT_ENABLED = "1";
const DEFAULT_THRESHOLD = "1";
const DEFAULT_EMAILS = "";
const DEFAULT_NOTIFY = "1";
/** @var Translator */
protected $translator = null;
public static function getConfig()
{
$config = [
'enabled' => ("1" == ConfigQuery::read(self::CONFIG_ENABLED, self::DEFAULT_ENABLED)),
'threshold' => intval(ConfigQuery::read(self::CONFIG_THRESHOLD, self::DEFAULT_THRESHOLD)),
'emails' => explode(',', ConfigQuery::read(self::CONFIG_EMAILS, self::DEFAULT_EMAILS)),
'notify' => ("1" == ConfigQuery::read(self::CONFIG_NOTIFY, self::DEFAULT_NOTIFY))
];
return $config;
}
/**
* @param ConnectionInterface|null $con
* @throws \Propel\Runtime\Exception\PropelException
*/
public function postActivation(ConnectionInterface $con = null): void
{
ConfigQuery::write(self::CONFIG_ENABLED, self::DEFAULT_ENABLED);
ConfigQuery::write(self::CONFIG_THRESHOLD, self::DEFAULT_THRESHOLD);
ConfigQuery::write(self::CONFIG_EMAILS, ConfigQuery::read('store_notification_emails'));
ConfigQuery::write(self::CONFIG_NOTIFY, self::DEFAULT_NOTIFY);
// create new message
if (null === MessageQuery::create()->findOneByName('stockalert_customer')) {
(new Message())
->setName('stockalert_customer')
->setHtmlTemplateFileName('alert-customer.html')
->setTextTemplateFileName('alert-customer.txt')
->setSecured(0)
->setLocale('en_US')
->setTitle('Stock Alert - Customer')
->setSubject('Product {$product_title} is available again')
->setLocale('fr_FR')
->setTitle('Alerte Stock - Client')
->setSubject('Le produit {$product_title} est à nouveau disponible')
->save();
(new Message())
->setName('stockalert_administrator')
->setHtmlTemplateFileName('alert-administrator.html')
->setTextTemplateFileName('alert-administrator.txt')
->setSecured(0)
->setLocale('en_US')
->setTitle('Stock Alert - Administrator')
->setSubject('List of products nearly out of stock')
->setLocale('fr_FR')
->setTitle('Alerte Stock - Administrateur')
->setSubject('Liste des produits qui seront bientôt en rupture de stock')
->save();
}
if (!self::getConfigValue('is_initialized', false)){
$database = new Database($con);
$database->insertSql(null, [__DIR__ . "/Config/thelia.sql"]);
self::setConfigValue('is_initialized', true);
}
}
/**
* @param ConnectionInterface|null $con
* @param bool $deleteModuleData
* @throws \Propel\Runtime\Exception\PropelException
*/
public function destroy(ConnectionInterface $con = null, $deleteModuleData = false): void
{
if (null !== $msg = MessageQuery::create()->findOneByName('stockalert_customer')) {
$msg->delete();
}
if (null !== $msg = MessageQuery::create()->findOneByName('stockalert_administrator')) {
$msg->delete();
}
ConfigQuery::create()
->filterByName([ self::CONFIG_ENABLED, self::CONFIG_THRESHOLD, self::CONFIG_EMAILS, self::CONFIG_NOTIFY ])
->delete()
;
$database = new Database($con);
$database->insertSql(null, [__DIR__ . '/Config/destroy.sql']);
}
protected function trans($id, array $parameters = [], $locale = null)
{
if (null === $this->translator) {
$this->translator = Translator::getInstance();
}
return $this->translator->trans($id, $parameters, StockAlert::MESSAGE_DOMAIN, $locale);
}
public function getHooks()
{
return [
[
'code' => 'product.stock-alert',
'type' => TemplateDefinition::FRONT_OFFICE,
"title" => array(
"fr_FR" => "Hook alertes stock",
"en_US" => "Stock alert hook",
),
"active" => true
]
];
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
}