This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblocknewproducts.php
273 lines (238 loc) · 8.46 KB
/
blocknewproducts.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-2016 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-2016 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;
class BlockNewProducts extends Module
{
protected static $cache_new_products;
public function __construct()
{
$this->name = 'blocknewproducts';
$this->tab = 'front_office_features';
$this->version = '1.10.1';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('New products block');
$this->description = $this->l('Displays a block featuring your store\'s newest products.');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.99.99');
}
public function install()
{
$success = (parent::install()
&& $this->registerHook('header')
&& $this->registerHook('leftColumn')
&& $this->registerHook('addproduct')
&& $this->registerHook('updateproduct')
&& $this->registerHook('deleteproduct')
&& Configuration::updateValue('NEW_PRODUCTS_NBR', 5)
&& $this->registerHook('displayHomeTab')
&& $this->registerHook('displayHomeTabContent')
);
$this->_clearCache('*');
return $success;
}
public function uninstall()
{
$this->_clearCache('*');
return parent::uninstall();
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submitBlockNewProducts'))
{
if (!($productNbr = Tools::getValue('NEW_PRODUCTS_NBR')) || empty($productNbr))
$output .= $this->displayError($this->l('Please complete the "products to display" field.'));
elseif ((int)($productNbr) == 0)
$output .= $this->displayError($this->l('Invalid number.'));
else
{
Configuration::updateValue('PS_NB_DAYS_NEW_PRODUCT', (int)(Tools::getValue('PS_NB_DAYS_NEW_PRODUCT')));
Configuration::updateValue('PS_BLOCK_NEWPRODUCTS_DISPLAY', (int)(Tools::getValue('PS_BLOCK_NEWPRODUCTS_DISPLAY')));
Configuration::updateValue('NEW_PRODUCTS_NBR', (int)($productNbr));
$this->_clearCache('*');
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->renderForm();
}
protected function getNewProducts()
{
if (!Configuration::get('NEW_PRODUCTS_NBR'))
return;
$newProducts = false;
if (Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
$newProducts = Product::getNewProducts((int) $this->context->language->id, 0, (int)Configuration::get('NEW_PRODUCTS_NBR'));
if (!$newProducts && Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY'))
return;
return $newProducts;
}
public function hookRightColumn($params)
{
if (!$this->isCached('blocknewproducts.tpl', $this->getCacheId()))
{
if (!isset(BlockNewProducts::$cache_new_products))
BlockNewProducts::$cache_new_products = $this->getNewProducts();
$this->smarty->assign(array(
'new_products' => BlockNewProducts::$cache_new_products,
'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
'homeSize' => Image::getSize(ImageType::getFormatedName('home'))
));
}
if (BlockNewProducts::$cache_new_products === false)
return false;
return $this->display(__FILE__, 'blocknewproducts.tpl', $this->getCacheId());
}
protected function getCacheId($name = null)
{
if ($name === null)
$name = 'blocknewproducts';
return parent::getCacheId($name.'|'.date('Ymd'));
}
public function hookLeftColumn($params)
{
return $this->hookRightColumn($params);
}
public function hookdisplayHomeTab($params)
{
if (!$this->isCached('tab.tpl', $this->getCacheId('blocknewproducts-tab')))
BlockNewProducts::$cache_new_products = $this->getNewProducts();
if (BlockNewProducts::$cache_new_products === false)
return false;
return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('blocknewproducts-tab'));
}
public function hookdisplayHomeTabContent($params)
{
if (!$this->isCached('blocknewproducts_home.tpl', $this->getCacheId('blocknewproducts-home')))
{
$this->smarty->assign(array(
'new_products' => BlockNewProducts::$cache_new_products,
'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
'homeSize' => Image::getSize(ImageType::getFormatedName('home'))
));
}
if (BlockNewProducts::$cache_new_products === false)
return false;
return $this->display(__FILE__, 'blocknewproducts_home.tpl', $this->getCacheId('blocknewproducts-home'));
}
public function hookHeader($params)
{
if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'index')
$this->context->controller->addCSS(_THEME_CSS_DIR_.'product_list.css');
$this->context->controller->addCSS($this->_path.'blocknewproducts.css', 'all');
}
public function hookAddProduct($params)
{
$this->_clearCache('*');
}
public function hookUpdateProduct($params)
{
$this->_clearCache('*');
}
public function hookDeleteProduct($params)
{
$this->_clearCache('*');
}
public function _clearCache($template, $cache_id = NULL, $compile_id = NULL)
{
parent::_clearCache('blocknewproducts.tpl');
parent::_clearCache('blocknewproducts_home.tpl', 'blocknewproducts-home');
parent::_clearCache('tab.tpl', 'blocknewproducts-tab');
}
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Products to display'),
'name' => 'NEW_PRODUCTS_NBR',
'class' => 'fixed-width-xs',
'desc' => $this->l('Define the number of products to be displayed in this block.')
),
array(
'type' => 'text',
'label' => $this->l('Number of days for which the product is considered \'new\''),
'name' => 'PS_NB_DAYS_NEW_PRODUCT',
'class' => 'fixed-width-xs',
),
array(
'type' => 'switch',
'label' => $this->l('Always display this block'),
'name' => 'PS_BLOCK_NEWPRODUCTS_DISPLAY',
'desc' => $this->l('Show the block even if no new products are available.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
)
),
'submit' => array(
'title' => $this->l('Save'),
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitBlockNewProducts';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
public function getConfigFieldsValues()
{
return array(
'PS_NB_DAYS_NEW_PRODUCT' => Tools::getValue('PS_NB_DAYS_NEW_PRODUCT', Configuration::get('PS_NB_DAYS_NEW_PRODUCT')),
'PS_BLOCK_NEWPRODUCTS_DISPLAY' => Tools::getValue('PS_BLOCK_NEWPRODUCTS_DISPLAY', Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY')),
'NEW_PRODUCTS_NBR' => Tools::getValue('NEW_PRODUCTS_NBR', Configuration::get('NEW_PRODUCTS_NBR')),
);
}
}