forked from thelia-modules/StockAlert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockAlert.php
133 lines (108 loc) · 4.7 KB
/
StockAlert.php
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
<?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 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 DEFAULT_ENABLED = "0";
const DEFAULT_THRESHOLD = "1";
const DEFAULT_EMAILS = "";
/** @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))
];
return $config;
}
public function postActivation(ConnectionInterface $con = null)
{
$languages = LangQuery::create()->find();
ConfigQuery::write(self::CONFIG_ENABLED, self::DEFAULT_ENABLED);
ConfigQuery::write(self::CONFIG_THRESHOLD, self::DEFAULT_THRESHOLD);
ConfigQuery::write(self::CONFIG_EMAILS, self::DEFAULT_EMAILS);
// create new message
if (null === MessageQuery::create()->findOneByName('stockalert_customer')) {
$message = new Message();
$message
->setName('stockalert_customer')
->setHtmlTemplateFileName('alert-customer.html')
->setHtmlLayoutFileName('')
->setTextTemplateFileName('alert-customer.txt')
->setTextLayoutFileName('')
->setSecured(0);
foreach ($languages as $language) {
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
$this->trans('Stock Alert - Customer', [], $locale)
);
$message->setSubject(
$this->trans('Product {$product_title} is available again', [], $locale)
);
}
$message->save();
$message = new Message();
$message
->setName('stockalert_administrator')
->setHtmlTemplateFileName('alert-administrator.html')
->setHtmlLayoutFileName('')
->setTextTemplateFileName('alert-administrator.txt')
->setTextLayoutFileName('')
->setSecured(0);
foreach ($languages as $language) {
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
$this->trans('Stock Alert - Administrator', [], $locale)
);
$message->setSubject(
$this->trans('Product {$product_title} is (nearly) out of stock', [], $locale)
);
}
$message->save();
}
try {
RestockingAlertQuery::create()->findOne();
} catch (\Exception $e) {
$database = new Database($con);
$database->insertSql(null, [__DIR__ . '/Config/thelia.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);
}
}