PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/blockmanufacturer/blockmanufacturer.php

https://gitlab.com/sutrix.hoa.tran/Research-Prestashop
PHP | 235 lines | 191 code | 20 blank | 24 comment | 11 complexity | b0914bc9476e28bb77d1887d057a3db5 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2016 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-2016 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 BlockManufacturer extends Module
  29. {
  30. public function __construct()
  31. {
  32. $this->name = 'blockmanufacturer';
  33. $this->tab = 'front_office_features';
  34. $this->version = '1.4.1';
  35. $this->author = 'PrestaShop';
  36. $this->need_instance = 0;
  37. $this->bootstrap = true;
  38. parent::__construct();
  39. $this->displayName = $this->l('Manufacturers block');
  40. $this->description = $this->l('Displays a block listing product manufacturers and/or brands.');
  41. $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.99.99');
  42. }
  43. public function install()
  44. {
  45. Configuration::updateValue('MANUFACTURER_DISPLAY_TEXT', true);
  46. Configuration::updateValue('MANUFACTURER_DISPLAY_TEXT_NB', 5);
  47. Configuration::updateValue('MANUFACTURER_DISPLAY_FORM', false);
  48. $success = (parent::install() &&
  49. $this->registerHook('header') && $this->registerHook('leftColumn') &&
  50. $this->registerHook('actionObjectManufacturerDeleteAfter') &&
  51. $this->registerHook('actionObjectManufacturerAddAfter') &&
  52. $this->registerHook('actionObjectManufacturerUpdateAfter')
  53. );
  54. return $success;
  55. }
  56. public function uninstall()
  57. {
  58. if (!parent::uninstall() ||
  59. !Configuration::deleteByName('MANUFACTURER_DISPLAY_TEXT') ||
  60. !Configuration::deleteByName('MANUFACTURER_DISPLAY_TEXT_NB') ||
  61. !Configuration::deleteByName('MANUFACTURER_DISPLAY_FORM')
  62. )
  63. return false;
  64. return true;
  65. }
  66. public function hookLeftColumn($params)
  67. {
  68. if (!$this->isCached('blockmanufacturer.tpl', $this->getCacheId()))
  69. {
  70. $manufacturers = Manufacturer::getManufacturers();
  71. foreach ($manufacturers as &$manufacturer)
  72. {
  73. $manufacturer['image'] = $this->context->language->iso_code.'-default';
  74. if (file_exists(_PS_MANU_IMG_DIR_.$manufacturer['id_manufacturer'].'-'.ImageType::getFormatedName('medium').'.jpg'))
  75. $manufacturer['image'] = $manufacturer['id_manufacturer'];
  76. }
  77. $this->smarty->assign(array(
  78. 'manufacturers' => $manufacturers,
  79. 'text_list' => Configuration::get('MANUFACTURER_DISPLAY_TEXT'),
  80. 'text_list_nb' => Configuration::get('MANUFACTURER_DISPLAY_TEXT_NB'),
  81. 'form_list' => Configuration::get('MANUFACTURER_DISPLAY_FORM'),
  82. 'display_link_manufacturer' => Configuration::get('PS_DISPLAY_SUPPLIERS'),
  83. ));
  84. }
  85. return $this->display(__FILE__, 'blockmanufacturer.tpl', $this->getCacheId());
  86. }
  87. public function hookRightColumn($params)
  88. {
  89. return $this->hookLeftColumn($params);
  90. }
  91. public function getContent()
  92. {
  93. $output = '';
  94. if (Tools::isSubmit('submitBlockManufacturers'))
  95. {
  96. $text_list = (int)(Tools::getValue('MANUFACTURER_DISPLAY_TEXT'));
  97. $text_nb = (int)(Tools::getValue('MANUFACTURER_DISPLAY_TEXT_NB'));
  98. $form_list = (int)(Tools::getValue('MANUFACTURER_DISPLAY_FORM'));
  99. if ($text_list && !Validate::isUnsignedInt($text_nb))
  100. $errors[] = $this->l('There is an invalid number of elements.');
  101. elseif (!$text_list && !$form_list)
  102. $errors[] = $this->l('Please activate at least one system list.');
  103. else
  104. {
  105. Configuration::updateValue('MANUFACTURER_DISPLAY_TEXT', $text_list);
  106. Configuration::updateValue('MANUFACTURER_DISPLAY_TEXT_NB', $text_nb);
  107. Configuration::updateValue('MANUFACTURER_DISPLAY_FORM', $form_list);
  108. $this->_clearCache('blockmanufacturer.tpl');
  109. }
  110. if (isset($errors) && count($errors))
  111. $output .= $this->displayError(implode('<br />', $errors));
  112. else
  113. $output .= $this->displayConfirmation($this->l('Settings updated.'));
  114. }
  115. return $output.$this->renderForm();
  116. }
  117. public function hookHeader($params)
  118. {
  119. $this->context->controller->addCSS(($this->_path).'blockmanufacturer.css', 'all');
  120. }
  121. public function hookActionObjectManufacturerUpdateAfter($params)
  122. {
  123. $this->_clearCache('blockmanufacturer.tpl');
  124. }
  125. public function hookActionObjectManufacturerAddAfter($params)
  126. {
  127. $this->_clearCache('blockmanufacturer.tpl');
  128. }
  129. public function hookActionObjectManufacturerDeleteAfter($params)
  130. {
  131. $this->_clearCache('blockmanufacturer.tpl');
  132. }
  133. public function renderForm()
  134. {
  135. $fields_form = array(
  136. 'form' => array(
  137. 'legend' => array(
  138. 'title' => $this->l('Settings'),
  139. 'icon' => 'icon-cogs'
  140. ),
  141. 'input' => array(
  142. array(
  143. 'type' => 'switch',
  144. 'label' => $this->l('Use a plain-text list'),
  145. 'name' => 'MANUFACTURER_DISPLAY_TEXT',
  146. 'desc' => $this->l('Display manufacturers in a plain-text list.'),
  147. 'values' => array(
  148. array(
  149. 'id' => 'active_on',
  150. 'value' => 1,
  151. 'label' => $this->l('Enabled')
  152. ),
  153. array(
  154. 'id' => 'active_off',
  155. 'value' => 0,
  156. 'label' => $this->l('Disabled')
  157. )
  158. ),
  159. ),
  160. array(
  161. 'type' => 'text',
  162. 'label' => $this->l('Number of elements to display'),
  163. 'name' => 'MANUFACTURER_DISPLAY_TEXT_NB',
  164. 'class' => 'fixed-width-xs'
  165. ),
  166. array(
  167. 'type' => 'switch',
  168. 'label' => $this->l('Use a drop-down list'),
  169. 'name' => 'MANUFACTURER_DISPLAY_FORM',
  170. 'desc' => $this->l('Display manufacturers in a drop-down list.'),
  171. 'values' => array(
  172. array(
  173. 'id' => 'active_on',
  174. 'value' => 1,
  175. 'label' => $this->l('Enabled')
  176. ),
  177. array(
  178. 'id' => 'active_off',
  179. 'value' => 0,
  180. 'label' => $this->l('Disabled')
  181. )
  182. ),
  183. )
  184. ),
  185. 'submit' => array(
  186. 'title' => $this->l('Save'),
  187. )
  188. ),
  189. );
  190. $helper = new HelperForm();
  191. $helper->show_toolbar = false;
  192. $helper->table = $this->table;
  193. $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  194. $helper->default_form_language = $lang->id;
  195. $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
  196. $helper->identifier = $this->identifier;
  197. $helper->submit_action = 'submitBlockManufacturers';
  198. $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
  199. $helper->token = Tools::getAdminTokenLite('AdminModules');
  200. $helper->tpl_vars = array(
  201. 'fields_value' => $this->getConfigFieldsValues(),
  202. 'languages' => $this->context->controller->getLanguages(),
  203. 'id_language' => $this->context->language->id
  204. );
  205. return $helper->generateForm(array($fields_form));
  206. }
  207. public function getConfigFieldsValues()
  208. {
  209. return array(
  210. 'MANUFACTURER_DISPLAY_TEXT' => Tools::getValue('MANUFACTURER_DISPLAY_TEXT', Configuration::get('MANUFACTURER_DISPLAY_TEXT')),
  211. 'MANUFACTURER_DISPLAY_TEXT_NB' => Tools::getValue('MANUFACTURER_DISPLAY_TEXT_NB', Configuration::get('MANUFACTURER_DISPLAY_TEXT_NB')),
  212. 'MANUFACTURER_DISPLAY_FORM' => Tools::getValue('MANUFACTURER_DISPLAY_FORM', Configuration::get('MANUFACTURER_DISPLAY_FORM')),
  213. );
  214. }
  215. }