-
Notifications
You must be signed in to change notification settings - Fork 9
/
Comment.php
executable file
·217 lines (183 loc) · 8.2 KB
/
Comment.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : [email protected] */
/* 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 Comment;
use Comment\Model\CommentQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Core\Translation\Translator;
use Thelia\Install\Database;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Module\BaseModule;
/**
* Class Comment
* @package Comment
*
* @author Michaël Espeche <[email protected]>
* @author Julien Chanséaume <[email protected]>
*/
class Comment extends BaseModule
{
const MESSAGE_DOMAIN = "comment";
const MESSAGE_DOMAIN_EMAIL = "comment.email.default";
/** Use comment */
const CONFIG_ACTIVATED = 1;
/** Use moderation */
const CONFIG_MODERATE = 1;
/** Allowed ref */
const CONFIG_REF_ALLOWED = 'product,content';
/** Only customers are abled to post comment */
const CONFIG_ONLY_CUSTOMER = 1;
/** Allow only verified customer (for product, customers that have bought the product) */
const CONFIG_ONLY_VERIFIED = 1;
/** request customer comment, x days after an order */
const CONFIG_REQUEST_CUSTOMMER_TTL = 15;
/** Send an email notification to the store admins when a new comment is posted */
const CONFIG_NOTIFY_ADMIN_NEW_COMMENT = true;
public function postActivation(ConnectionInterface $con = null): void
{
// Config
if (null === ConfigQuery::read('comment_activated')) {
ConfigQuery::write('comment_activated', Comment::CONFIG_ACTIVATED);
}
if (null === ConfigQuery::read('comment_moderate')) {
ConfigQuery::write('comment_moderate', Comment::CONFIG_MODERATE);
}
if (null === ConfigQuery::read('comment_ref_allowed')) {
ConfigQuery::write('comment_ref_allowed', Comment::CONFIG_REF_ALLOWED);
}
if (null === ConfigQuery::read('comment_only_customer')) {
ConfigQuery::write('comment_only_customer', Comment::CONFIG_ONLY_CUSTOMER);
}
if (null === ConfigQuery::read('comment_only_verified')) {
ConfigQuery::write('comment_only_verified', Comment::CONFIG_ONLY_VERIFIED);
}
if (null === ConfigQuery::read('comment_request_customer_ttl')) {
ConfigQuery::write('comment_request_customer_ttl', Comment::CONFIG_REQUEST_CUSTOMMER_TTL);
}
if (null === ConfigQuery::read('comment_notify_admin_new_comment')) {
ConfigQuery::write('comment_notify_admin_new_comment', Comment::CONFIG_NOTIFY_ADMIN_NEW_COMMENT);
}
// Schema
if (!self::getConfigValue('is_initialized', false)) {
$database = new Database($con);
$database->insertSql(null, [__DIR__ . DS . 'Config' . DS . 'thelia.sql']);
self::setConfigValue('is_initialized', true);
}
// Messages
// load the email localization files (the module was just loaded so they are not loaded yet)
$languages = LangQuery::create()->find();
/** @var Lang $language */
foreach ($languages as $language) {
Translator::getInstance()->addResource(
"php",
__DIR__ . "/I18n/email/default/" . $language->getLocale() . ".php",
$language->getLocale(),
self::MESSAGE_DOMAIN_EMAIL
);
}
// Request comment from customer
if (null === MessageQuery::create()->findOneByName('comment_request_customer')) {
$message = new Message();
$message
->setName('comment_request_customer')
->setHtmlTemplateFileName('request-customer-comment.html')
->setHtmlLayoutFileName('')
->setTextTemplateFileName('request-customer-comment.txt')
->setTextLayoutFileName('')
->setSecured(0);
foreach ($languages as $language) {
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
Translator::getInstance()->trans('Request customer comment', [], self::MESSAGE_DOMAIN)
);
$message->setSubject(
Translator::getInstance()->trans('', [], self::MESSAGE_DOMAIN)
);
}
$message->save();
}
// Notify admin of new comment
if (null === MessageQuery::create()->findOneByName('new_comment_notification_admin')) {
$message = new Message();
$message
->setName('new_comment_notification_admin')
->setHtmlTemplateFileName('new-comment-notification-admin.html')
->setHtmlLayoutFileName('')
->setTextTemplateFileName('new-comment-notification-admin.txt')
->setTextLayoutFileName('')
->setSecured(0);
foreach ($languages as $language) {
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
Translator::getInstance()->trans(
'Notify store admin of new comment',
[],
self::MESSAGE_DOMAIN_EMAIL,
$locale
)
);
$subject = Translator::getInstance()->trans(
'New comment on %ref_type_title "%ref_title"',
[],
self::MESSAGE_DOMAIN_EMAIL,
$locale
);
$subject = str_replace('%ref_type_title', '{$ref_type_title|lower}', $subject);
$subject = str_replace('%ref_title', '{$ref_title}', $subject);
$message->setSubject($subject);
}
$message->save();
}
}
public static function getConfig()
{
$config = [
'activated' => (
(int)ConfigQuery::read('comment_activated', self::CONFIG_ACTIVATED) === 1
),
'moderate' => (
(int)ConfigQuery::read('comment_moderate', self::CONFIG_MODERATE) === 1
),
'ref_allowed' => explode(
',',
ConfigQuery::read('comment_ref_allowed', self::CONFIG_REF_ALLOWED)
),
'only_customer' => (
(int)ConfigQuery::read('comment_only_customer', self::CONFIG_ONLY_CUSTOMER) === 1
),
'only_verified' => (
(int)ConfigQuery::read('comment_only_verified', self::CONFIG_ONLY_VERIFIED) === 1
),
'request_customer_ttl' => (
(int)ConfigQuery::read('comment_request_customer_ttl', self::CONFIG_REQUEST_CUSTOMMER_TTL)
),
'notify_admin_new_comment' => (
(int)ConfigQuery::read('comment_notify_admin_new_comment', self::CONFIG_NOTIFY_ADMIN_NEW_COMMENT)
=== 1
),
];
return $config;
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
}