-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathctconfiguration.php
298 lines (265 loc) · 9.19 KB
/
ctconfiguration.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
<?php
/**
* Copyright (C) 2017-2024 thirty bees
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://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 license@thirtybees.com so we can send you a copy immediately.
*
* @author thirty bees <modules@thirtybees.com>
* @copyright 2017-2024 thirty bees
* @license Academic Free License (AFL 3.0)
*/
/**
* Class CTConfiguration
*/
class CTConfiguration extends Module
{
/**
* CTConfiguration constructor.
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function __construct()
{
$this->name = 'ctconfiguration';
$this->tab = 'front_office_features';
$this->version = '1.0.6';
$this->author = 'thirty bees';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Community Theme Configuration');
$this->description = $this->l('Configuration for community theme blocks and content.');
$this->tb_versions_compliancy = '> 1.0.0';
$this->tb_min_version = '1.0.0';
$this->bootstrap = true;
}
/**
* Installs module to PrestaShop
*
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function install()
{
parent::install();
$hooksToUnhook = [
['module' => 'blockcategories', 'hook' => 'footer'],
];
foreach ($hooksToUnhook as $unhook) {
$this->unhookModule($unhook['module'], $unhook['hook']);
}
$hooksToInstall = ['displayHeader', 'displayFooterProduct'];
foreach ($hooksToInstall as $hookName) {
$this->registerHook($hookName);
}
// Disable scenes in config for faster loading, scenes are removed in category template
Configuration::updateValue('PS_SCENE_FEATURE_ACTIVE', false);
// Translatable configuration items
$copyrightValue = '© ' . Configuration::get(Configuration::SHOP_NAME) . ' ' . date('Y');
$copyrightValues = [];
foreach (Language::getLanguages(false) as $language) {
$idLanguage = (int) $language['id_lang'];
$copyrightValues[$idLanguage] = $copyrightValue;
}
Configuration::updateValue('CT_CFG_COPYRIGHT_CONTENT', $copyrightValues);
return true;
}
/**
* Unhooks a module hook
*
* @param string $module
* @param string $hook
*
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
protected function unhookModule($module, $hook)
{
$id_module = Module::getModuleIdByName($module);
$id_hook = Hook::getIdByName($hook);
return Db::getInstance()->delete('hook_module', 'id_module = '.(int) $id_module.' AND id_hook = '.(int) $id_hook);
}
/***
* Uninstalls module from PrestaShop
*
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function uninstall()
{
$keysToDrop = [
'CT_CFG_BLOCKCATEGORIES_FOOTER',
'CT_CFG_COPYRIGHT_CONTENT',
];
foreach ($keysToDrop as $key) {
Configuration::deleteByName($key);
}
return parent::uninstall();
}
/**
* Compiles and returns module configuration page content
*
* @return string
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws SmartyException
*/
public function getContent()
{
if (Tools::isSubmit('submit'.$this->name)) {
$this->postProcess();
}
$moduleUrl = $this->context->link->getAdminLink('AdminModules').'&configure='.$this->name;
$fieldSets = [
'general' => [
'title' => $this->l('Module settings'),
'fields' => $this->getOptionFields(),
'buttons' => [
'cancelBlock' => [
'title' => $this->l('Cancel'),
'href' => $moduleUrl,
'icon' => 'process-icon-cancel',
],
],
'submit' => [
'name' => 'submit'.$this->name,
'title' => $this->l('Save'),
],
],
];
$h = new HelperOptions();
$h->token = Tools::getAdminTokenLite('AdminModules');
$h->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
$h->id = Tab::getIdFromClassName('AdminTools');
return $h->generateOptions($fieldSets);
}
/**
* Processes submitted configuration variables
*
* @throws PrestaShopException
*/
protected function postProcess()
{
$castFunctions = ['boolval', 'doubleval', 'floatval', 'intval', 'strval'];
$langIds = Language::getIDs(false);
$values = [];
foreach ($this->getOptionFields() as $key => $field) {
$htmlAllowed = isset($field['html']) && $field['html'];
if ($field['type'] == 'textareaLang' || $field['type'] == 'textLang') {
$values[$key] = [];
foreach ($langIds as $idLang) {
$value = Tools::getValue($key.'_'.$idLang);
if (isset($field['cast']) && $field['cast'] && in_array($field['cast'], $castFunctions)) {
$value = call_user_func($field['cast'], $value);
}
$values[$key][$idLang] = $value;
}
} else {
$value = Tools::getValue($key);
if (isset($field['cast']) && $field['cast'] && in_array($field['cast'], $castFunctions)) {
$value = call_user_func($field['cast'], $value);
}
$values[$key] = $value;
}
Configuration::updateValue($key, $values[$key], $htmlAllowed);
}
if ($values['CT_CFG_BLOCKCATEGORIES_FOOTER']) {
$this->hookModule('blockcategories', 'footer');
} else {
$this->unhookModule('blockcategories', 'footer');
}
}
/**
* Return HelperOptions fields that are using in module configuration form.
*
* @return array
*/
protected function getOptionFields()
{
return [
'CT_CFG_BLOCKCATEGORIES_FOOTER' => [
'title' => $this->l('Show blockcategories footer block'),
'desc' => $this->l('If enabled, shows category tree block in the footer.'),
'cast' => 'boolval',
'type' => 'bool',
],
'CT_CFG_COPYRIGHT_CONTENT' => [
'title' => $this->l('Copyright footer text'),
'desc' => $this->l('Text to be displayed in the copyright footer block.').' '.$this->l('Leave empty to not display the block.'),
'hint' => $this->l('HTML is allowed. Enter &copy; for copyright symbol.'),
'cast' => 'strval',
'type' => 'textareaLang',
'html' => true,
'size' => 50,
],
];
}
/**
* Registers a module hook
*
* @param string $module
* @param string $hook
*
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
protected function hookModule($module, $hook)
{
$module = Module::getInstanceByName($module);
return $module->registerHook($hook);
}
/**
* Adds assets to page header
* and passes configuration variables to smarty
*
* @throws PrestaShopException
*/
public function hookDisplayHeader()
{
$idLang = (int) $this->context->language->id;
$copyrigthContent = Configuration::get('CT_CFG_COPYRIGHT_CONTENT', $idLang);
$this->context->smarty->assign(
[
'ctheme' => [
'footer' => [
'copyright' => [
'display' => !!$copyrigthContent,
'html' => $copyrigthContent,
],
],
],
]
);
}
/**
* Adds JS files to product page
*/
public function hookDisplayFooterProduct()
{
try {
$jqZoom = (bool) Configuration::get('PS_DISPLAY_JQZOOM');
} catch (PrestaShopException $e) {
$jqZoom = false;
}
if ($jqZoom) {
// Remove jQuery Zoom
$jqZoomPluginPath = Media::getJqueryPluginPath('jqzoom');
$this->context->controller->removeJS($jqZoomPluginPath['js']);
$this->context->controller->removeCSS($jqZoomPluginPath['css']);
// Add new version of jqZoom plugin
$this->context->controller->addJS($this->_path.'views/js/vendor/jquery.zoom.min.js');
}
}
}