-
Notifications
You must be signed in to change notification settings - Fork 1
/
tuto_mailtemplate.php
273 lines (240 loc) · 8.94 KB
/
tuto_mailtemplate.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* 2007-2019 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_CAN_LOAD_FILES_')) {
exit;
}
use PrestaShop\Module\TutoMailtemplate\MailTemplate\Transformation\CustomMessageColorTransformation;
use PrestaShop\PrestaShop\Core\MailTemplate\FolderThemeScanner;
use PrestaShop\PrestaShop\Core\MailTemplate\Layout\Layout;
use PrestaShop\PrestaShop\Core\MailTemplate\Layout\LayoutInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\Layout\LayoutVariablesBuilderInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\MailTemplateInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\MailTemplateRendererInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\ThemeCatalogInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\ThemeCollectionInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\ThemeInterface;
use PrestaShop\PrestaShop\Core\MailTemplate\Transformation\TransformationCollectionInterface;
class tuto_mailtemplate extends Module
{
public function __construct()
{
$this->name = 'tuto_mailtemplate';
$this->author = 'PrestaShop';
$this->version = '1.0.0';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Tutorial MailTemplate', array(), 'Modules.TutoMailtemplate.Admin');
$this->description = $this->trans('Tutorial for Mail Template in PrestaShop.', array(), 'Modules.TutoMailtemplate.Admin');
$this->secure_key = Tools::encrypt($this->name);
$this->ps_versions_compliancy = array('min' => '1.7.6.0', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install()
&& $this->registerHooks()
&& $this->installTab()
;
}
public function uninstall()
{
return parent::uninstall()
&& $this->unregisterHooks()
&& $this->uninstallTab()
;
}
public function enable($force_all = false)
{
return parent::enable($force_all)
&& $this->registerHooks()
&& $this->installTab()
;
}
public function disable($force_all = false)
{
return parent::disable($force_all)
&& $this->unregisterHooks()
&& $this->uninstallTab()
;
}
private function installTab()
{
$tabId = (int) Tab::getIdFromClassName('TutoMailtemplate');
if (!$tabId) {
$tabId = null;
}
$tab = new Tab($tabId);
$tab->active = 1;
$tab->class_name = 'TutoMailtemplate';
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'Tutorial MailTemplate';
}
$tab->id_parent = (int) Tab::getIdFromClassName('AdminMailThemeParent');
$tab->module = $this->name;
return $tab->save();
}
private function uninstallTab()
{
$tabId = (int) Tab::getIdFromClassName('TutoMailtemplate');
if (!$tabId) {
return true;
}
$tab = new Tab($tabId);
return $tab->delete();
}
private function registerHooks()
{
return
$this->registerHook(ThemeCatalogInterface::LIST_MAIL_THEMES_HOOK)
&& $this->registerHook(LayoutVariablesBuilderInterface::BUILD_MAIL_LAYOUT_VARIABLES_HOOK)
&& $this->registerHook(MailTemplateRendererInterface::GET_MAIL_LAYOUT_TRANSFORMATIONS)
;
}
private function unregisterHooks()
{
return
$this->unregisterHook(ThemeCatalogInterface::LIST_MAIL_THEMES_HOOK)
&& $this->unregisterHook(LayoutVariablesBuilderInterface::BUILD_MAIL_LAYOUT_VARIABLES_HOOK)
&& $this->unregisterHook(MailTemplateRendererInterface::GET_MAIL_LAYOUT_TRANSFORMATIONS)
;
}
public function getContent()
{
//This controller actually does not exist, it is used in the tab
//and is accessible thanks to routing settings with _legacy_link
Tools::redirectAdmin(
$this->context->link->getAdminLink('TutoMailtemplate')
);
}
/**
* @param array $hookParams
*/
public function hookActionListMailThemes(array $hookParams)
{
if (!isset($hookParams['mailThemes'])) {
return;
}
/** @var ThemeCollectionInterface $themes */
$themes = $hookParams['mailThemes'];
$this->addTutoTheme($themes);
$this->addAdditionalLayout($themes);
$this->addExtendedLayout($themes);
}
/**
* @param ThemeCollectionInterface $themes
*/
private function addAdditionalLayout(ThemeCollectionInterface $themes)
{
/** @var ThemeInterface $theme */
foreach ($themes as $theme) {
if (!in_array($theme->getName(), ['classic', 'modern'])) {
continue;
}
// Add a layout to each theme (don't forget to specify the module name)
$theme->getLayouts()->add(new Layout(
'additional_template',
__DIR__ . '/mails/layouts/additional_' . $theme->getName() . '_layout.html.twig',
'',
$this->name
));
}
}
/**
* @param ThemeCollectionInterface $themes
*/
private function addExtendedLayout(ThemeCollectionInterface $themes)
{
$theme = $themes->getByName('modern');
if (!$theme) {
return;
}
// First parameter is the layout name, second one is the module name (empty value matches the core layouts)
$orderConfLayout = $theme->getLayouts()->getLayout('order_conf', '');
if (null === $orderConfLayout) {
return;
}
//The layout collection extends from ArrayCollection so it has more feature than it seems..
//It allows to REPLACE the existing layout easily
$orderIndex = $theme->getLayouts()->indexOf($orderConfLayout);
$theme->getLayouts()->offsetSet($orderIndex, new Layout(
$orderConfLayout->getName(),
__DIR__ . '/mails/layouts/order_conf.html.twig',
''
));
}
/**
* @param ThemeCollectionInterface $themes
*/
private function addTutoTheme(ThemeCollectionInterface $themes)
{
$scanner = new FolderThemeScanner();
$tutoTheme = $scanner->scan(__DIR__ . '/mails/themes/tuto_modern');
if (null !== $tutoTheme && $tutoTheme->getLayouts()->count() > 0) {
$themes->add($tutoTheme);
}
}
/**
* @param array $hookParams
*/
public function hookActionBuildMailLayoutVariables(array $hookParams)
{
if (!isset($hookParams['mailLayout'])) {
return;
}
/** @var LayoutInterface $mailLayout */
$mailLayout = $hookParams['mailLayout'];
if ($mailLayout->getModuleName() != $this->name || $mailLayout->getName() != 'additional_template') {
return;
}
$locale = $hookParams['mailLayoutVariables']['locale'];
if (strpos($locale, 'fr') === 0) {
$hookParams['mailLayoutVariables']['customMessage'] = 'Mon message personnalisé';
} else {
$hookParams['mailLayoutVariables']['customMessage'] = 'My custom message';
}
}
/**
* @param array $hookParams
*/
public function hookActionGetMailLayoutTransformations(array $hookParams)
{
if (!isset($hookParams['templateType']) ||
MailTemplateInterface::HTML_TYPE !== $hookParams['templateType'] ||
!isset($hookParams['mailLayout']) ||
!isset($hookParams['layoutTransformations'])) {
return;
}
/** @var LayoutInterface $mailLayout */
$mailLayout = $hookParams['mailLayout'];
if ($mailLayout->getModuleName() != $this->name) {
return;
}
/** @var TransformationCollectionInterface $transformations */
$transformations = $hookParams['layoutTransformations'];
$transformations->add(new CustomMessageColorTransformation('#FF0000'));
}
}