forked from PrestaShop/blockreassurance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockreassurance.php
564 lines (503 loc) · 20.9 KB
/
blockreassurance.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<?php
/**
* 2007-2019 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 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:
* http://opensource.org/licenses/afl-3.0.php
* 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
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
$autoloadPath = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoloadPath)) {
require_once $autoloadPath;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class blockreassurance extends Module implements WidgetInterface
{
const ALLOWED_CONTROLLERS_CHECKOUT = array(
'cart',
'order',
);
const ALLOWED_CONTROLLERS_PRODUCT = array(
'product',
);
const POSITION_NONE = 0;
const POSITION_BELOW_HEADER = 1;
const POSITION_ABOVE_HEADER = 2;
/** @var string */
public $name;
/** @var string */
public $version;
/** @var string */
public $author;
/** @var bool */
public $need_instance;
/** @var string */
public $module_key;
/** @var string */
public $author_address;
/** @var string */
public $controller_name;
/** @var bool */
public $bootstrap;
/** @var string */
public $displayName;
/** @var string */
public $description;
/** @var string */
public $js_path;
/** @var string */
public $css_path;
/** @var string */
public $img_path;
/** @var string */
public $old_path_img;
/** @var string */
public $img_path_perso;
/** @var string */
public $lib_path;
/** @var string */
public $docs_path;
/** @var string */
public $logo_path;
/** @var string */
public $module_path;
/** @var string Text to display when ask for confirmation on uninstall action */
public $confirmUninstall;
/** @var string */
public $ps_url;
/** @var string */
public $folder_file_upload;
/** @var string */
private $templateFile;
public function __construct()
{
// Settings
$this->name = 'blockreassurance';
$this->tab = 'seo';
$this->version = '4.1.1';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->module_key = '938b96386d4d79aa7cb891439cb0ef11';
$this->author_address = '0x64aa3c1e4034d07015f639b0e171b0d7b27d01aa';
$this->bootstrap = true;
parent::__construct();
if ($this->context->link == null) {
$protocolPrefix = Tools::getCurrentUrlProtocolPrefix();
$this->context->link = new Link($protocolPrefix, $protocolPrefix);
}
$this->displayName = $this->trans('Customer Reassurance', array(), 'Modules.Blockreassurance.Admin');
$this->description = $this->trans('Connect with your customers and reassure them by highlighting your services: secure payment, free shipping, returns, etc.', array(), 'Modules.Blockreassurance.Admin');
// Settings paths
if (!$this->_path) {
$this->_path = __PS_BASE_URI__ . 'modules/' . $this->name . '/';
}
$this->js_path = $this->_path . 'views/js/';
$this->css_path = $this->_path . 'views/css/';
$this->img_path = $this->_path . 'views/img/';
$this->old_path_img = $this->_path . 'img/';
$this->img_path_perso = $this->img_path . 'img_perso';
$this->lib_path = $this->_path . 'views/lib/';
$this->docs_path = $this->_path . 'docs/';
$this->logo_path = $this->_path . 'logo.png';
$this->module_path = $this->_path;
$this->folder_file_upload = _PS_MODULE_DIR_ . $this->name . '/views/img/img_perso/';
// Confirm uninstall
$this->confirmUninstall = $this->trans('Are you sure you want to uninstall this module?', array(), 'Modules.Blockreassurance.Admin');
$this->ps_url = $this->context->link->getBaseLink();
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->templateFile = 'module:blockreassurance/views/templates/hook/blockreassurance.tpl';
}
/**
* install pre-config
*
* @return bool
*/
public function install()
{
// SQL
$sqlQueries = [];
$sqlQueries[] = ' CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'psreassurance` (
`id_psreassurance` int(10) unsigned NOT NULL AUTO_INCREMENT,
`icon` varchar(255) NULL,
`custom_icon` varchar(255) NULL,
`status` int(10) unsigned NOT NULL,
`position` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
`type_link` int(10) unsigned NULL,
`id_cms` int(10) unsigned NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NULL,
PRIMARY KEY (`id_psreassurance`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8;';
$sqlQueries[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'psreassurance_lang` (
`id_psreassurance` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
PRIMARY KEY (`id_psreassurance`,`id_shop`,`id_lang`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8;';
$sqlQueries[] = 'INSERT INTO ' . _DB_PREFIX_ . 'psreassurance (icon, custom_icon, status, position, id_shop, type_link, id_cms, date_add) VALUES '
. "('" . $this->img_path . "reassurance/pack2/security.svg', null, 1, 1, 1, null, null, now()),"
. "('" . $this->img_path . "reassurance/pack2/carrier.svg', null, 1, 2, 1, null, null, now()),"
. "('" . $this->img_path . "reassurance/pack2/parcel.svg', null, 1, 3, 1, null, null, now())";
foreach (Language::getLanguages(false) as $lang) {
$sqlQueries[] = 'INSERT INTO ' . _DB_PREFIX_ . 'psreassurance_lang (id_psreassurance, id_lang, id_shop, title, description, link) VALUES '
. '(1, ' . $lang['id_lang'] . ", 1, '" . $this->trans('Security policy', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', '" . $this->trans('(edit with the Customer Reassurance module)', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', ''),"
. '(2, ' . $lang['id_lang'] . ", 1, '" . $this->trans('Delivery policy', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', '" . $this->trans('(edit with the Customer Reassurance module)', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', ''),"
. '(3, ' . $lang['id_lang'] . ", 1, '" . $this->trans('Return policy', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', '" . $this->trans('(edit with the Customer Reassurance module)', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', '')";
}
foreach ($sqlQueries as $query) {
if (Db::getInstance()->execute($query) == false) {
return false;
}
}
// Configuration
Configuration::updateValue('PSR_HOOK_HEADER', self::POSITION_NONE);
Configuration::updateValue('PSR_HOOK_FOOTER', self::POSITION_NONE);
Configuration::updateValue('PSR_HOOK_PRODUCT', self::POSITION_BELOW_HEADER);
Configuration::updateValue('PSR_HOOK_CHECKOUT', self::POSITION_BELOW_HEADER);
Configuration::updateValue('PSR_ICON_COLOR', '#F19D76');
Configuration::updateValue('PSR_TEXT_COLOR', '#000000');
// Hooks
if (parent::install() &&
$this->registerHook('displayAfterBodyOpeningTag') &&
$this->registerHook('displayNavFullWidth') &&
$this->registerHook('displayFooterAfter') &&
$this->registerHook('displayFooterBefore') &&
$this->registerHook('displayReassurance') &&
$this->registerHook('actionFrontControllerSetMedia')
) {
return true;
}
$this->_errors[] = $this->trans('There was an error during the installation. Please contact us through Addons website.', array(), 'Modules.Blockreassurance.Admin');
return false;
}
/**
* Uninstall module configuration
*
* @return bool
*/
public function uninstall()
{
// SQL
$sql = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'psreassurance`, `' . _DB_PREFIX_ . 'psreassurance_lang`';
if (Db::getInstance()->execute($sql) == false) {
return false;
}
// Configuration
Configuration::deleteByName('PSR_HOOK_HEADER');
Configuration::deleteByName('PSR_HOOK_FOOTER');
Configuration::deleteByName('PSR_HOOK_PRODUCT');
Configuration::deleteByName('PSR_HOOK_CHECKOUT');
Configuration::deleteByName('PSR_ICON_COLOR');
Configuration::deleteByName('PSR_TEXT_COLOR');
if (parent::uninstall()) {
return true;
}
$this->_errors[] = $this->trans('There was an error during the uninstallation. Please contact us through Addons website.', array(), 'Modules.Blockreassurance.Admin');
return false;
}
/**
* load dependencies
*/
public function loadAsset()
{
$this->addJsDefList();
$cssAssets = [
'//fonts.googleapis.com/icon?family=Material+Icons',
$this->lib_path . 'pickr/css/pickr.min.css',
$this->lib_path . 'pickr/css/pickr-override.css',
$this->css_path . 'back.css',
];
$javascriptAssets = [
// Libraries
$this->lib_path . '/pickr/js/pickr.js',
$this->js_path . 'vue.min.js',
$this->js_path . 'back.js',
];
$this->context->controller->addCSS($cssAssets, 'all');
$this->context->controller->addJS($javascriptAssets);
$this->context->controller->addJqueryPlugin('colorpicker');
$this->context->controller->addJqueryUI('ui.sortable');
}
/**
* Check if folder img_perso is writable and executable
*
* @return bool
*/
private function folderUploadFilesHasGoodRights()
{
return is_writable($this->folder_file_upload)
&& is_executable($this->folder_file_upload);
}
/**
* @return string
*
* @throws PrestaShopException
*/
public function getContent()
{
$this->loadAsset();
$parsedUrl = parse_url('/en/517-blocks-tabs-banners');
$parameters = [];
if (!empty($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $parameters);
}
$parameters['utm_source'] = 'back-office';
$parameters['utm_medium'] = 'modules';
$parameters['utm_campaign'] = 'back-office-' . strtoupper($this->context->language->iso_code);
$link = 'https://addons.prestashop.com' . $parsedUrl['path'] . '?' . http_build_query($parameters);
$categoryFetcher = array(
'name' => 'Blocks, Tabs and Banners',
'link' => $link,
'description' => 'With these modules, you can personalize your e-commerce website by adding reassurance blocks (free delivery, satisfaction guaranteed or your money back, etc.), illustrating your categories with visuals on your homepage or adding advertising banners to your PrestaShop store.',
);
$id_lang = $this->context->language->id;
$currentPage = 'global';
$getPage = Tools::getValue('page');
if (!empty($getPage)) {
$currentPage = $getPage;
}
$moduleAdminLink = Context::getContext()->link->getAdminLink('AdminModules', true) . '&configure=' . $this->name . '&module_name=' . $this->name;
$allCms = CMS::listCms($id_lang);
$this->context->smarty->assign(array(
'addons_category' => $categoryFetcher,
'psr_hook_header' => (int) Configuration::get('PSR_HOOK_HEADER'),
'psr_hook_footer' => (int) Configuration::get('PSR_HOOK_FOOTER'),
'psr_hook_product' => (int) Configuration::get('PSR_HOOK_PRODUCT'),
'psr_hook_checkout' => (int) Configuration::get('PSR_HOOK_CHECKOUT'),
'psr_text_color' => Configuration::get('PSR_TEXT_COLOR'),
'psr_icon_color' => Configuration::get('PSR_ICON_COLOR'),
'logo_path' => $this->logo_path,
'languages' => Language::getLanguages(),
'allblock' => ReassuranceActivity::getAllBlockByLang($id_lang, $this->context->shop->id),
'allblockByShop' => ReassuranceActivity::getAllBlockByShop(),
'currentPage' => $currentPage,
'moduleAdminLink' => $moduleAdminLink,
'img_path' => $this->img_path,
'allCms' => $allCms,
'defaultFormLanguage' => (int) $this->context->employee->id_lang,
'img_url' => $this->img_path,
'old_img_url' => $this->old_path_img,
'folderIsWritable' => $this->folderUploadFilesHasGoodRights(),
'folderPath' => $this->img_path_perso,
// constants
'LINK_TYPE_NONE' => ReassuranceActivity::TYPE_LINK_NONE,
'LINK_TYPE_CMS' => ReassuranceActivity::TYPE_LINK_CMS_PAGE,
'LINK_TYPE_URL' => ReassuranceActivity::TYPE_LINK_URL,
));
return $this->display(__FILE__, 'views/templates/admin/configure.tpl');
}
/**
* @param array $params
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
public function hookdisplayAfterBodyOpeningTag($params)
{
$position = (int) Configuration::get('PSR_HOOK_HEADER');
return $position === self::POSITION_ABOVE_HEADER ? $this->renderTemplateInHook('displayBlock.tpl') : '';
}
/**
* @param array $params
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
public function hookdisplayNavFullWidth($params)
{
$position = (int) Configuration::get('PSR_HOOK_HEADER');
return $position === self::POSITION_BELOW_HEADER ? $this->renderTemplateInHook('displayBlock.tpl') : '';
}
/**
* @param array $params
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
public function hookdisplayFooterAfter($params)
{
$position = (int) Configuration::get('PSR_HOOK_FOOTER');
return $position === self::POSITION_BELOW_HEADER ? $this->renderTemplateInHook('displayBlockWhite.tpl') : '';
}
/**
* @param array $params
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
public function hookdisplayFooterBefore($params)
{
$position = (int) Configuration::get('PSR_HOOK_FOOTER');
return $position === self::POSITION_ABOVE_HEADER ? $this->renderTemplateInHook('displayBlockWhite.tpl') : '';
}
/**
* @param array $params
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
public function hookdisplayReassurance($params)
{
$enableCheckout = (int) Configuration::get('PSR_HOOK_CHECKOUT');
$enableProduct = (int) Configuration::get('PSR_HOOK_PRODUCT');
$controller = Tools::getValue('controller');
if (!$this->shouldWeDisplayOnBlockProduct($enableCheckout, $enableProduct, $controller)) {
return '';
}
return $this->renderTemplateInHook('displayBlockProduct.tpl');
}
public function hookActionFrontControllerSetMedia()
{
Media::addJsDef(array(
'psr_icon_color' => Configuration::get('PSR_ICON_COLOR'),
));
$this->context->controller->registerStylesheet(
'front-css',
'modules/' . $this->name . '/views/css/front.css'
);
$this->context->controller->registerJavascript(
'front-js',
'modules/' . $this->name . '/views/js/front.js'
);
}
/**
* @param string $hookName
* @param array $configuration
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
public function renderWidget($hookName = null, array $configuration = [])
{
if ($hookName === 'displayFooter') {
return '';
}
if (!$this->isCached($this->templateFile, $this->getCacheId('blockreassurance'))) {
$this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
}
return $this->fetch($this->templateFile, $this->getCacheId('blockreassurance'));
}
/**
* @param string $hookName
* @param array $configuration
*
* @return array
*
* @throws PrestaShopDatabaseException
*/
public function getWidgetVariables($hookName = null, array $configuration = [])
{
$blocks = ReassuranceActivity::getAllBlockByStatus(
$this->context->language->id,
$this->context->shop->id
);
$elements = [];
foreach ($blocks as $key => $value) {
if (!empty($value['icon'])) {
$elements[$key]['image'] = $value['icon'];
} elseif (!empty($value['custom_icon'])) {
$elements[$key]['image'] = $value['custom_icon'];
} else {
$elements[$key]['image'] = '';
}
$elements[$key]['text'] = $value['title'] . ' ' . $value['description'];
$elements[$key]['title'] = $value['title'];
$elements[$key]['description'] = $value['description'];
}
return [
'elements' => $elements,
];
}
/**
* Check if we can display the hook on product page or cart page.
* The HOOK must be active
*
* @param int $enableCheckout
* @param int $enableProduct
* @param string $controller
*
* @return bool
*/
private function shouldWeDisplayOnBlockProduct($enableCheckout, $enableProduct, $controller)
{
if ($enableCheckout === self::POSITION_BELOW_HEADER && in_array($controller, self::ALLOWED_CONTROLLERS_CHECKOUT)) {
return true;
}
if ($enableProduct === self::POSITION_BELOW_HEADER && in_array($controller, self::ALLOWED_CONTROLLERS_PRODUCT)) {
return true;
}
return false;
}
/**
* Assign smarty variables and display the hook
*
* @param string $template
*
* @return string
*
* @throws PrestaShopDatabaseException
*/
private function renderTemplateInHook($template)
{
$id_lang = $this->context->language->id;
$this->context->smarty->assign(array(
'blocks' => ReassuranceActivity::getAllBlockByStatus($id_lang, $this->context->shop->id),
'iconColor' => Configuration::get('PSR_ICON_COLOR'),
'textColor' => Configuration::get('PSR_TEXT_COLOR'),
// constants
'LINK_TYPE_NONE' => ReassuranceActivity::TYPE_LINK_NONE,
'LINK_TYPE_CMS' => ReassuranceActivity::TYPE_LINK_CMS_PAGE,
'LINK_TYPE_URL' => ReassuranceActivity::TYPE_LINK_URL,
));
return $this->display(__FILE__, 'views/templates/hook/' . $template);
}
/**
* @throws PrestaShopException
*/
protected function addJsDefList()
{
Media::addJsDef(array(
'psr_icon_color' => Configuration::get('PSR_ICON_COLOR'),
'psr_text_color' => Configuration::get('PSR_TEXT_COLOR'),
'psr_controller_block_url' => $this->context->link->getAdminLink('AdminBlockListing'),
'psr_controller_block' => 'AdminBlockListing',
'psr_lang' => (int) Configuration::get('PS_LANG_DEFAULT'),
'block_updated' => $this->trans('Block updated', array(), 'Modules.Blockreassurance.Admin'),
'active_error' => $this->trans('Oops... looks like an error occurred', array(), 'Modules.Blockreassurance.Admin'),
'min_field_error' => $this->trans('The field %field_name% is required at least in your default language.', array('%field_name%' => sprintf('"%s"', $this->trans('Title', array(), 'Admin.Global'))), 'Admin.Notifications.Error'),
'psre_success' => $this->trans('Configuration updated successfully!', array(), 'Modules.Blockreassurance.Admin'),
'successPosition' => $this->trans('Position changed successfully!', array(), 'Modules.Blockreassurance.Admin'),
'errorPosition' => $this->trans('An error occurred when switching position', array(), 'Modules.Blockreassurance.Admin'),
));
}
}