PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/homefeatured/homefeatured.php

https://gitlab.com/staging06/myproject
PHP | 272 lines | 216 code | 32 blank | 24 comment | 26 complexity | cb44a895fe47ebb5bbc14742189856cf MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. if (!defined('_PS_VERSION_'))
  27. exit;
  28. class HomeFeatured extends Module
  29. {
  30. protected static $cache_products;
  31. public function __construct()
  32. {
  33. $this->name = 'homefeatured';
  34. $this->tab = 'front_office_features';
  35. $this->version = '1.8.0';
  36. $this->author = 'PrestaShop';
  37. $this->need_instance = 0;
  38. $this->bootstrap = true;
  39. parent::__construct();
  40. $this->displayName = $this->l('Featured products on the homepage');
  41. $this->description = $this->l('Displays featured products in the central column of your homepage.');
  42. }
  43. public function install()
  44. {
  45. $this->_clearCache('*');
  46. Configuration::updateValue('HOME_FEATURED_NBR', 8);
  47. Configuration::updateValue('HOME_FEATURED_CAT', (int)Context::getContext()->shop->getCategory());
  48. Configuration::updateValue('HOME_FEATURED_RANDOMIZE', false);
  49. if (!parent::install()
  50. || !$this->registerHook('header')
  51. || !$this->registerHook('addproduct')
  52. || !$this->registerHook('updateproduct')
  53. || !$this->registerHook('deleteproduct')
  54. || !$this->registerHook('categoryUpdate')
  55. || !$this->registerHook('displayHomeTab')
  56. || !$this->registerHook('displayHomeTabContent')
  57. )
  58. return false;
  59. return true;
  60. }
  61. public function uninstall()
  62. {
  63. $this->_clearCache('*');
  64. return parent::uninstall();
  65. }
  66. public function getContent()
  67. {
  68. $output = '';
  69. $errors = array();
  70. if (Tools::isSubmit('submitHomeFeatured'))
  71. {
  72. $nbr = Tools::getValue('HOME_FEATURED_NBR');
  73. if (!Validate::isInt($nbr) || $nbr <= 0)
  74. $errors[] = $this->l('The number of products is invalid. Please enter a positive number.');
  75. $cat = Tools::getValue('HOME_FEATURED_CAT');
  76. if (!Validate::isInt($cat) || $cat <= 0)
  77. $errors[] = $this->l('The category ID is invalid. Please choose an existing category ID.');
  78. $rand = Tools::getValue('HOME_FEATURED_RANDOMIZE');
  79. if (!Validate::isBool($rand))
  80. $errors[] = $this->l('Invalid value for the "randomize" flag.');
  81. if (isset($errors) && count($errors))
  82. $output = $this->displayError(implode('<br />', $errors));
  83. else
  84. {
  85. Configuration::updateValue('HOME_FEATURED_NBR', (int)$nbr);
  86. Configuration::updateValue('HOME_FEATURED_CAT', (int)$cat);
  87. Configuration::updateValue('HOME_FEATURED_RANDOMIZE', (bool)$rand);
  88. Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('homefeatured.tpl'));
  89. $output = $this->displayConfirmation($this->l('Your settings have been updated.'));
  90. }
  91. }
  92. return $output.$this->renderForm();
  93. }
  94. public function hookDisplayHeader($params)
  95. {
  96. $this->hookHeader($params);
  97. }
  98. public function hookHeader($params)
  99. {
  100. if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'index')
  101. $this->context->controller->addCSS(_THEME_CSS_DIR_.'product_list.css');
  102. $this->context->controller->addCSS(($this->_path).'css/homefeatured.css', 'all');
  103. }
  104. public function _cacheProducts()
  105. {
  106. if (!isset(HomeFeatured::$cache_products))
  107. {
  108. $category = new Category((int)Configuration::get('HOME_FEATURED_CAT'), (int)Context::getContext()->language->id);
  109. $nb = (int)Configuration::get('HOME_FEATURED_NBR');
  110. if (Configuration::get('HOME_FEATURED_RANDOMIZE'))
  111. HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), null, null, false, true, true, ($nb ? $nb : 8));
  112. else
  113. HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
  114. }
  115. if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
  116. return false;
  117. }
  118. public function hookDisplayHomeTab($params)
  119. {
  120. if (!$this->isCached('tab.tpl', $this->getCacheId('homefeatured-tab')))
  121. $this->_cacheProducts();
  122. return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('homefeatured-tab'));
  123. }
  124. public function hookDisplayHome($params)
  125. {
  126. if (!$this->isCached('homefeatured.tpl', $this->getCacheId()))
  127. {
  128. $this->_cacheProducts();
  129. $this->smarty->assign(
  130. array(
  131. 'products' => HomeFeatured::$cache_products,
  132. 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
  133. 'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
  134. )
  135. );
  136. }
  137. return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId());
  138. }
  139. public function hookDisplayHomeTabContent($params)
  140. {
  141. return $this->hookDisplayHome($params);
  142. }
  143. public function hookAddProduct($params)
  144. {
  145. $this->_clearCache('*');
  146. }
  147. public function hookUpdateProduct($params)
  148. {
  149. $this->_clearCache('*');
  150. }
  151. public function hookDeleteProduct($params)
  152. {
  153. $this->_clearCache('*');
  154. }
  155. public function hookCategoryUpdate($params)
  156. {
  157. $this->_clearCache('*');
  158. }
  159. public function _clearCache($template, $cache_id = NULL, $compile_id = NULL)
  160. {
  161. parent::_clearCache('homefeatured.tpl');
  162. parent::_clearCache('tab.tpl', 'homefeatured-tab');
  163. }
  164. public function renderForm()
  165. {
  166. $fields_form = array(
  167. 'form' => array(
  168. 'legend' => array(
  169. 'title' => $this->l('Settings'),
  170. 'icon' => 'icon-cogs'
  171. ),
  172. 'description' => $this->l('To add products to your homepage, simply add them to the corresponding product category (default: "Home").'),
  173. 'input' => array(
  174. array(
  175. 'type' => 'text',
  176. 'label' => $this->l('Number of products to be displayed'),
  177. 'name' => 'HOME_FEATURED_NBR',
  178. 'class' => 'fixed-width-xs',
  179. 'desc' => $this->l('Set the number of products that you would like to display on homepage (default: 8).'),
  180. ),
  181. array(
  182. 'type' => 'text',
  183. 'label' => $this->l('Category from which to pick products to be displayed'),
  184. 'name' => 'HOME_FEATURED_CAT',
  185. 'class' => 'fixed-width-xs',
  186. 'desc' => $this->l('Choose the category ID of the products that you would like to display on homepage (default: 2 for "Home").'),
  187. ),
  188. array(
  189. 'type' => 'switch',
  190. 'label' => $this->l('Randomly display featured products'),
  191. 'name' => 'HOME_FEATURED_RANDOMIZE',
  192. 'class' => 'fixed-width-xs',
  193. 'desc' => $this->l('Enable if you wish the products to be displayed randomly (default: no).'),
  194. 'values' => array(
  195. array(
  196. 'id' => 'active_on',
  197. 'value' => 1,
  198. 'label' => $this->l('Yes')
  199. ),
  200. array(
  201. 'id' => 'active_off',
  202. 'value' => 0,
  203. 'label' => $this->l('No')
  204. )
  205. ),
  206. ),
  207. ),
  208. 'submit' => array(
  209. 'title' => $this->l('Save'),
  210. )
  211. ),
  212. );
  213. $helper = new HelperForm();
  214. $helper->show_toolbar = false;
  215. $helper->table = $this->table;
  216. $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  217. $helper->default_form_language = $lang->id;
  218. $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
  219. $this->fields_form = array();
  220. $helper->id = (int)Tools::getValue('id_carrier');
  221. $helper->identifier = $this->identifier;
  222. $helper->submit_action = 'submitHomeFeatured';
  223. $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
  224. $helper->token = Tools::getAdminTokenLite('AdminModules');
  225. $helper->tpl_vars = array(
  226. 'fields_value' => $this->getConfigFieldsValues(),
  227. 'languages' => $this->context->controller->getLanguages(),
  228. 'id_language' => $this->context->language->id
  229. );
  230. return $helper->generateForm(array($fields_form));
  231. }
  232. public function getConfigFieldsValues()
  233. {
  234. return array(
  235. 'HOME_FEATURED_NBR' => Tools::getValue('HOME_FEATURED_NBR', (int)Configuration::get('HOME_FEATURED_NBR')),
  236. 'HOME_FEATURED_CAT' => Tools::getValue('HOME_FEATURED_CAT', (int)Configuration::get('HOME_FEATURED_CAT')),
  237. 'HOME_FEATURED_RANDOMIZE' => Tools::getValue('HOME_FEATURED_RANDOMIZE', (bool)Configuration::get('HOME_FEATURED_RANDOMIZE')),
  238. );
  239. }
  240. }