-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
homecategoriez.php
385 lines (342 loc) · 16.7 KB
/
homecategoriez.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
<?php
/**
* Home Categories Block: module for PrestaShop.
*
* @author Maksim T. <[email protected]>
* @copyright 2012 Maksim T.
* @link https://prestashop.modulez.ru/en/frontend-features/31-block-of-categories-on-the-homepage.html The module's homepage
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (false === defined('_PS_VERSION_')) {
exit;
}
/**
* Module HomeCategoriez.
*
* @author Maksim T. <[email protected]>
*/
class HomeCategoriez extends Module
{
/** The product ID of the module on its homepage. */
const HOMEPAGE_PRODUCT_ID = 31;
/** @var bool Is smarty vars already assigned. */
private static $vars_assigned = false;
/** @var array Default settings. */
private $conf_default = [
'HOMECATEGORIEZ_CATALOG' => '',
'HOMECATEGORIEZ_IMAGE_TYPE' => '',
'HOMECATEGORIEZ_LIMIT' => '6',
'HOMECATEGORIEZ_SHUFFLE' => '0',
'HOMECATEGORIEZ_COLS' => '4',
'HOMECATEGORIEZ_WIDTH_ADJUST' => '538',
];
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function __construct()
{
$this->name = 'homecategoriez';
$this->tab = version_compare(_PS_VERSION_, '1.4', '>=') ? 'front_office_features' : 'Tools';
$this->version = '1.12.0';
$this->author = 'zapalm';
$this->need_instance = false;
$this->bootstrap = false;
parent::__construct();
$this->displayName = $this->l('Categories on the homepage');
$this->description = $this->l('Displays categories in the middle of your homepage');
$this->renewConfiguration();
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function install()
{
$result = parent::install();
if ($result) {
foreach ($this->conf_default as $c => $v) {
Configuration::updateValue($c, $v);
}
$result &= $this->registerHook('header');
if (version_compare(_PS_VERSION_, '1.6', '<') || version_compare(_PS_VERSION_, '1.7', '>=')) {
$result &= $this->registerHook('home');
} else {
$result &= $this->registerHook('displayHomeTab');
$result &= $this->registerHook('displayHomeTabContent');
}
}
$this->registerModuleOnQualityService('installation');
return (bool)$result;
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function uninstall()
{
$result = (bool)parent::uninstall();
if ($result) {
foreach ($this->conf_default as $c => $v) {
Configuration::deleteByName($c);
}
}
$this->registerModuleOnQualityService('uninstallation');
return $result;
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function getContent()
{
global $cookie;
$output = (version_compare(_PS_VERSION_, '1.6', '>=') ? '' : '<h2>' . $this->displayName . '</h2>');
if (Tools::isSubmit('submit_save')) {
$res = 1;
foreach ($this->conf_default as $k => $v) {
$res &= Configuration::updateValue($k, Tools::getValue($k));
}
$output .= $res ? $this->displayConfirmation($this->l('Settings updated')) : $this->displayError($this->l('Some setting not updated'));
}
$conf = Configuration::getMultiple(array_keys($this->conf_default));
$categories = Category::getHomeCategories($cookie->id_lang, false);
$root_cat = Category::getRootCategory($cookie->id_lang);
$imageTypes = ImageType::getImagesTypes('categories', true);
$output .= '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<fieldset>
<legend><img src="' . $this->_path . 'logo.png" width="15" height="16" alt="" />' . $this->l('Settings') . '</legend>
<label>' . $this->l('Root category of children categories to display') . '</label>
<div class="margin-form">
<select name="HOMECATEGORIEZ_CATALOG">
<option value="' . $root_cat->id . '"' . ($conf['HOMECATEGORIEZ_CATALOG'] == $root_cat->id ? ' selected="selected"' : '') . '>' . $root_cat->name . '</option>';
foreach ($categories as $v) {
$output .= '<option value="' . $v['id_category'] . '"' . ($conf['HOMECATEGORIEZ_CATALOG'] == $v['id_category'] ? ' selected="selected"' : '') . '>' . $v['name'] . '</option>';
}
$output .= '
</select>
<p class="clear">' . $this->l('Choose a root category (default : Home category).') . '</p>
</div>
<label>' . $this->l('Image size') . '</label>
<div class="margin-form">
<select name="HOMECATEGORIEZ_IMAGE_TYPE">';
foreach ($imageTypes as $v) {
$output .= '<option value="' . $v['name'] . '"' . ($conf['HOMECATEGORIEZ_IMAGE_TYPE'] === $v['name'] ? ' selected="selected"' : '') . '>' . $v['name'] . '</option>';
}
$output .= '
</select>
<p class="clear">' . $this->l('See the configuration in "Design / Image settings" for PS 1.7 or in "Preferences / Images" for older versions.') . '</p>
</div>
<label>' . $this->l('The number of categories to display') . '</label>
<div class="margin-form">
<input type="text" size="1" name="HOMECATEGORIEZ_LIMIT" value="' . $conf['HOMECATEGORIEZ_LIMIT'] . '" />
<p class="clear">' . $this->l('The number of categories to display on homepage.') . ' ' . sprintf($this->l('Default: %s.'), $this->conf_default['HOMECATEGORIEZ_LIMIT']) . '</p>
</div>
<label>' . $this->l('Shuffle categories') . '</label>
<div class="margin-form">
<input type="checkbox" name="HOMECATEGORIEZ_SHUFFLE" value="1" ' . ($conf['HOMECATEGORIEZ_SHUFFLE'] ? 'checked="checked"' : '') . '>
<p class="clear">' . $this->l('Check it if you want categories to be shuffled.') . '</p>
</div>
<label><sup style="color: red;">*</sup> ' . $this->l('The number of columns to display') . '</label>
<div class="margin-form">
<input type="text" size="1" name="HOMECATEGORIEZ_COLS" value="' . $conf['HOMECATEGORIEZ_COLS'] . '" />
<p class="clear">' . $this->l('The number of columns to display on homepage.') . ' ' . sprintf($this->l('Default: %s.'), $this->conf_default['HOMECATEGORIEZ_COLS']) . '</p>
</div>
<label><sup style="color: red;">*</sup> ' . $this->l('Width adjust for the block of categories') . '</label>
<div class="margin-form">
<input type="text" size="3" name="HOMECATEGORIEZ_WIDTH_ADJUST" value="' . $conf['HOMECATEGORIEZ_WIDTH_ADJUST'] . '" /> px.
<p class="clear">' . $this->l('Input a number of pixels to adjust width of the block of categories.') . ' ' . sprintf($this->l('Default: %s.'), $this->conf_default['HOMECATEGORIEZ_WIDTH_ADJUST']) . '</p>
</div>
<label><span style="color: red;">*</span> — ' . $this->l('These options are only for PrestaShop 1.5, 1.4 and 1.3.') . '</label>
<br class="clear" />
<div class="margin-form">
<input type="submit" name="submit_save" value="' . $this->l('Save') . '" class="button" />
</div>
</fieldset>
</form>
';
// The block about the module (version: 2021-08-19)
$modulezUrl = 'https://prestashop.modulez.ru' . (Language::getIsoById(false === empty($GLOBALS['cookie']->id_lang) ? $GLOBALS['cookie']->id_lang : Context::getContext()->language->id) === 'ru' ? '/ru/' : '/en/');
$modulePage = $modulezUrl . self::HOMEPAGE_PRODUCT_ID . '-' . $this->name . '.html';
$licenseTitle = 'Academic Free License (AFL 3.0)';
$output .=
(version_compare(_PS_VERSION_, '1.6', '<') ? '<br class="clear" />' : '') . '
<div class="panel">
<div class="panel-heading">
<img src="' . $this->_path . 'logo.png" width="16" height="16" alt=""/>
' . $this->l('Module info') . '
</div>
<div class="form-wrapper">
<div class="row">
<div class="form-group col-lg-4" style="display: block; clear: none !important; float: left; width: 33.3%;">
<span><b>' . $this->l('Version') . ':</b> ' . $this->version . '</span><br/>
<span><b>' . $this->l('License') . ':</b> ' . $licenseTitle . '</span><br/>
<span><b>' . $this->l('Website') . ':</b> <a class="link" href="' . $modulePage . '" target="_blank">prestashop.modulez.ru</a></span><br/>
<span><b>' . $this->l('Author') . ':</b> ' . $this->author . '</span><br/><br/>
</div>
<div class="form-group col-lg-2" style="display: block; clear: none !important; float: left; width: 16.6%;">
<img width="250" alt="' . $this->l('Website') . '" src="https://prestashop.modulez.ru/img/marketplace-logo.png" />
</div>
</div>
</div>
</div> ' .
(version_compare(_PS_VERSION_, '1.6', '<') ? '<br class="clear" />' : '') . '
';
return $output;
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function hookHeader()
{
if (version_compare(_PS_VERSION_, '1.7', '>=')) {
$cssFile = '1.7.css';
} elseif (version_compare(_PS_VERSION_, '1.6', '>=')) {
$cssFile = '1.6.css';
} else {
$cssFile = '1.3-1.5.css';
}
return '<link href="' . $this->_path . 'views/css/' . $cssFile . '" rel="stylesheet">';
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function hookHome($params)
{
$this->assignCommonVariables($params);
$templateName = version_compare(_PS_VERSION_, '1.7', '>=')
? 'homecategoriez-boilerplate.tpl'
: 'homecategoriez.tpl'
;
return $this->display(__FILE__, 'views/templates/' . $templateName);
}
/**
* Assign common variables.
*
* @param array $params Hook params.
*
* @author Maksim T. <[email protected]>
*/
private function assignCommonVariables($params)
{
global $smarty, $link;
if (self::$vars_assigned) {
return;
}
$idLanguage = (int)$params['cookie']->id_lang;
$categories = Category::getChildren((int)Configuration::get('HOMECATEGORIEZ_CATALOG'), $idLanguage, true);
foreach ($categories as $i => $category) {
$categories[$i] = new Category($category['id_category'], $idLanguage);
}
if (Configuration::get('HOMECATEGORIEZ_SHUFFLE')) {
shuffle($categories);
}
$limit = (int)Configuration::get('HOMECATEGORIEZ_LIMIT');
if ($limit > 0) {
$categories = array_splice($categories, 0, $limit);
}
$conf = Configuration::getMultiple(array_keys($this->conf_default));
$block_width = (int)$conf['HOMECATEGORIEZ_WIDTH_ADJUST'];
$nb_items_per_line = (int)$conf['HOMECATEGORIEZ_COLS'];
$pic_size_type = $conf['HOMECATEGORIEZ_IMAGE_TYPE'];
$block_width_adjust = ceil($nb_items_per_line * 2) + 2;
$block_content_width = $block_width - $block_width_adjust;
$block_li_width = ceil($block_content_width / $nb_items_per_line);
$smarty->assign([
'categories' => $categories,
'link' => $link,
'block_width' => $block_width,
'nb_items_per_line' => $nb_items_per_line,
'block_li_width' => $block_li_width,
'pic_size_type' => $pic_size_type,
'pic_size' => Image::getSize($pic_size_type),
]);
self::$vars_assigned = true;
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function hookDisplayHomeTabContent($params)
{
$this->assignCommonVariables($params);
return $this->display(__FILE__, 'views/templates/homecategoriez-bootstrap.tpl');
}
/**
* @inheritdoc
*
* @author Maksim T. <[email protected]>
*/
public function hookDisplayHomeTab($params)
{
return $this->display(__FILE__, 'views/templates/homecategoriez-bootstrap-tab.tpl');
}
/**
* Updates the module configuration.
*
* The advantage of the method is not to make a migration script in a case when just added a new option, which has a default value or not.
*
* @author Maksim T. <[email protected]>
*/
private function renewConfiguration()
{
if ($this->active) {
foreach (array_keys($this->conf_default) as $settingName) {
$settingValue = (string)Configuration::get($settingName);
if ('' === $settingValue) {
if ('HOMECATEGORIEZ_CATALOG' === $settingName) {
$settingValue = (Configuration::get('PS_HOME_CATEGORY') ? Configuration::get('PS_HOME_CATEGORY') : 1);
Configuration::updateValue($settingName, $settingValue);
} elseif ('HOMECATEGORIEZ_IMAGE_TYPE' === $settingName) {
$imageTypes = ImageType::getImagesTypes('categories', true);
$imageType = array_pop($imageTypes);
if (is_array($imageType)) {
Configuration::updateValue($settingName, $imageType['name']);
}
} else {
Configuration::updateValue($settingName, $this->conf_default[$settingName]);
}
}
}
}
}
/**
* Registers current module installation/uninstallation in the quality service.
*
* This method is needed for a developer to quickly find out about a problem with installing or uninstalling a module.
*
* @param string $operation The operation. Possible values: installation, uninstallation.
*
* @author Maksim T. <[email protected]>
*/
private function registerModuleOnQualityService($operation)
{
@file_get_contents('https://prestashop.modulez.ru/scripts/quality-service/index.php?' . http_build_query([
'data' => json_encode([
'productId' => self::HOMEPAGE_PRODUCT_ID,
'productSymbolicName' => $this->name,
'productVersion' => $this->version,
'operation' => $operation,
'status' => (empty($this->_errors) ? 'success' : 'error'),
'message' => (false === empty($this->_errors) ? strip_tags(stripslashes(implode(' ', (array)$this->_errors))) : ''),
'prestashopVersion' => _PS_VERSION_,
'thirtybeesVersion' => (defined('_TB_VERSION_') ? _TB_VERSION_ : ''),
'shopDomain' => (method_exists('Tools', 'getShopDomain') && Tools::getShopDomain() ? Tools::getShopDomain() : (Configuration::get('PS_SHOP_DOMAIN') ? Configuration::get('PS_SHOP_DOMAIN') : Tools::getHttpHost())),
'shopEmail' => Configuration::get('PS_SHOP_EMAIL'), // This public e-mail from a shop's contacts can be used by a developer to send only an urgent information about security issue of a module!
'phpVersion' => PHP_VERSION,
'ioncubeVersion' => (function_exists('ioncube_loader_iversion') ? ioncube_loader_iversion() : ''),
'languageIsoCode' => Language::getIsoById(false === empty($GLOBALS['cookie']->id_lang) ? $GLOBALS['cookie']->id_lang : Context::getContext()->language->id),
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]));
}
}