VEKIA

blockcategories2.php

Nov 12th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.97 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2013 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 [email protected] 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 <[email protected]>
  22. * @copyright 2007-2013 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.  
  27. if (!defined('_PS_VERSION_'))
  28. exit;
  29.  
  30. class BlockCategories2 extends Module
  31. {
  32. public function __construct()
  33. {
  34. $this->name = 'blockcategories2';
  35. $this->tab = 'front_office_features';
  36. $this->version = '2.0';
  37. $this->author = 'PrestaShop';
  38.  
  39. parent::__construct();
  40.  
  41. $this->displayName = $this->l('Categories block22');
  42. $this->description = $this->l('Adds a block featuring product categories.');
  43. }
  44.  
  45. public function install()
  46. {
  47. if (!parent::install() ||
  48. !$this->registerHook('leftColumn') ||
  49. !$this->registerHook('footer') ||
  50. !$this->registerHook('header') ||
  51. // Temporary hooks. Do NOT hook any module on it. Some CRUD hook will replace them as soon as possible.
  52. !$this->registerHook('categoryAddition') ||
  53. !$this->registerHook('categoryUpdate') ||
  54. !$this->registerHook('categoryDeletion') ||
  55. !$this->registerHook('actionAdminMetaControllerUpdate_optionsBefore') ||
  56. !$this->registerHook('actionAdminLanguagesControllerStatusBefore') ||
  57. !Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 4) ||
  58. !Configuration::updateValue('BLOCK_CATEG_DHTML', 1))
  59. return false;
  60. return true;
  61. }
  62.  
  63. public function uninstall()
  64. {
  65. if (!parent::uninstall() ||
  66. !Configuration::deleteByName('BLOCK_CATEG_MAX_DEPTH') ||
  67. !Configuration::deleteByName('BLOCK_CATEG_DHTML'))
  68. return false;
  69. return true;
  70. }
  71.  
  72. public function getContent()
  73. {
  74. $output = '<h2>'.$this->displayName.'</h2>';
  75. if (Tools::isSubmit('submitBlockCategories2'))
  76. {
  77. $maxDepth = (int)(Tools::getValue('maxDepth'));
  78. $dhtml = Tools::getValue('dhtml');
  79. $nbrColumns = Tools::getValue('nbrColumns', 4);
  80. if ($maxDepth < 0)
  81. $output .= '<div class="alert error">'.$this->l('Maximum depth: Invalid number.').'</div>';
  82. elseif ($dhtml != 0 && $dhtml != 1)
  83. $output .= '<div class="alert error">'.$this->l('Dynamic HTML: Invalid choice.').'</div>';
  84. else
  85. {
  86. Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', (int)($maxDepth));
  87. Configuration::updateValue('BLOCK_CATEG_DHTML', (int)($dhtml));
  88. Configuration::updateValue('BLOCK_CATEG_NBR_COLUMN_FOOTER', $nbrColumns);
  89. Configuration::updateValue('BLOCK_CATEG_SORT_WAY', Tools::getValue('BLOCK_CATEG_SORT_WAY'));
  90. Configuration::updateValue('BLOCK_CATEG_SORT', Tools::getValue('BLOCK_CATEG_SORT'));
  91.  
  92. $this->_clearBlockcategories2Cache();
  93. $output .= '<div class="conf confirm">'.$this->l('Settings updated').'</div>';
  94. }
  95. }
  96. return $output.$this->displayForm();
  97. }
  98.  
  99. public function displayForm()
  100. {
  101. return '
  102. <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
  103. <fieldset>
  104. <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
  105. <label>'.$this->l('Maximum depth').'</label>
  106. <div class="margin-form">
  107. <input type="text" name="maxDepth" value="'.(int)Configuration::get('BLOCK_CATEG_MAX_DEPTH').'" />
  108. <p class="clear">'.$this->l('Set the maximum depth of sublevels displayed in this block (0 = infinite)').'</p>
  109. </div>
  110. <label>'.$this->l('Dynamic').'</label>
  111.  
  112. <div class="margin-form">
  113. <input type="radio" name="dhtml" id="dhtml_on" value="1" '.(Tools::getValue('dhtml', Configuration::get('BLOCK_CATEG_DHTML')) ? 'checked="checked" ' : '').'/>
  114. <label class="t" for="dhtml_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
  115. <input type="radio" name="dhtml" id="dhtml_off" value="0" '.(!Tools::getValue('dhtml', Configuration::get('BLOCK_CATEG_DHTML')) ? 'checked="checked" ' : '').'/>
  116. <label class="t" for="dhtml_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
  117. <p class="clear">'.$this->l('Activate dynamic (animated) mode for sublevels.').'</p>
  118. </div>
  119. <label>'.$this->l('Sort').'</label>
  120.  
  121. <div class="margin-form">
  122. <input type="radio" name="BLOCK_CATEG_SORT" id="sort_on" value="0" '.(!Tools::getValue('BLOCK_CATEG_SORT', Configuration::get('BLOCK_CATEG_SORT')) ? 'checked="checked" ' : '').'/>
  123. <label class="t" for="sort_on"> <img src="../modules/'.$this->name.'/sort_number.png" alt="'.$this->l('Enabled').'" title="'.$this->l('By position').'" />'.$this->l('By position').'</label>
  124. <input type="radio" name="BLOCK_CATEG_SORT" id="sort_off" value="1" '.(Tools::getValue('BLOCK_CATEG_SORT', Configuration::get('BLOCK_CATEG_SORT')) ? 'checked="checked" ' : '').'/>
  125. <label class="t" for="sort_off"> <img src="../modules/'.$this->name.'/sort_alphabet.png" alt="'.$this->l('Disabled').'" title="'.$this->l('By name').'" />'.$this->l('By name').'</label> -
  126. <select name="BLOCK_CATEG_SORT_WAY">
  127. <option value="0" '.(!Tools::getValue('BLOCK_CATEG_SORT_WAY', Configuration::get('BLOCK_CATEG_SORT_WAY')) ? 'selected="selected" ' : '').'>'.$this->l('Ascending').'</option>
  128. <option value="1" '.(Tools::getValue('BLOCK_CATEG_SORT_WAY', Configuration::get('BLOCK_CATEG_SORT_WAY')) ? 'selected="selected" ' : '').'>'.$this->l('Descending').'</option>
  129. </select>
  130. </div>
  131. <label>'.$this->l('How many footer columns would you like?').'</label>
  132. <div class="margin-form">
  133. <input type="text" name="nbrColumns" value="'.(int)Configuration::get('BLOCK_CATEG_NBR_COLUMN_FOOTER').'" />
  134. <p class="clear">'.$this->l('Define the number of footer columns.').'</p>
  135. </div>
  136. <center><input type="submit" name="submitBlockCategories2" value="'.$this->l('Save').'" class="button" /></center>
  137. </fieldset>
  138. </form>';
  139. }
  140.  
  141. public function getTree($resultParents2, $resultIds2, $maxDepth, $id_category = 19, $currentDepth = 0)
  142. {
  143. if (is_null($id_category))
  144. $id_category = $this->context->shop->getCategory();
  145.  
  146. $children = array();
  147. if (isset($resultParents2[$id_category]) && count($resultParents2[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth))
  148. foreach ($resultParents2[$id_category] as $subcat)
  149. $children[] = $this->getTree($resultParents2, $resultIds2, $maxDepth, $subcat['id_category'], $currentDepth + 1);
  150. if (!isset($resultIds2[$id_category]))
  151. return false;
  152. $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds2[$id_category]['link_rewrite']),
  153. 'name' => $resultIds2[$id_category]['name'], 'desc'=> $resultIds2[$id_category]['description'],
  154. 'children' => $children);
  155. return $return;
  156. }
  157.  
  158. public function hookLeftColumn($params)
  159. {
  160.  
  161. // Get all groups for this customer and concatenate them as a string: "1,2,3..."
  162. $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id));
  163. $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
  164. if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  165. SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
  166. FROM `'._DB_PREFIX_.'category` c
  167. INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('cl').')
  168. INNER JOIN `'._DB_PREFIX_.'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = '.(int)$this->context->shop->id.')
  169. WHERE (c.`active` = 1 OR c.`id_category` = '.(int)Configuration::get('PS_HOME_CATEGORY').')
  170. AND c.`id_category` != '.(int)Configuration::get('PS_ROOT_CATEGORY').'
  171. '.((int)$maxdepth != 0 ? ' AND `level_depth` <= '.(int)$maxdepth : '').'
  172. AND c.id_category IN (SELECT id_category FROM `'._DB_PREFIX_.'category_group` WHERE `id_group` IN ('.pSQL($groups).'))
  173. ORDER BY `level_depth` ASC, '.(Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`').' '.(Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC')))
  174. return;
  175.  
  176. $resultParents2 = array();
  177. $resultIds2 = array();
  178.  
  179. foreach ($result as &$row)
  180. {
  181. $resultParents2[$row['id_parent']][] = &$row;
  182. $resultIds2[$row['id_category']] = &$row;
  183. }
  184.  
  185. $blockCategTree2 = $this->getTree($resultParents2, $resultIds2, Configuration::get('BLOCK_CATEG_MAX_DEPTH'),19);
  186. unset($resultParents2, $resultIds2);
  187.  
  188. $id_category = (int)Tools::getValue('id_category');
  189. $id_product = (int)Tools::getValue('id_product');
  190.  
  191. $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
  192. if (Tools::isSubmit('id_category'))
  193. {
  194. $this->context->cookie->last_visited_category = $id_category;
  195. $this->smarty->assign('currentCategoryId', $this->context->cookie->last_visited_category);
  196. }
  197. if (Tools::isSubmit('id_product'))
  198. {
  199. if (!isset($this->context->cookie->last_visited_category)
  200. || !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $this->context->cookie->last_visited_category)))
  201. || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop))
  202. {
  203. $product = new Product($id_product);
  204. if (isset($product) && Validate::isLoadedObject($product))
  205. $this->context->cookie->last_visited_category = (int)$product->id_category_default;
  206. }
  207. $this->smarty->assign('currentCategoryId', (int)$this->context->cookie->last_visited_category);
  208. }
  209. $this->smarty->assign('blockCategTree2', $blockCategTree2);
  210.  
  211. if (file_exists(_PS_THEME_DIR_.'modules/blockcategories2/blockcategories2.tpl'))
  212. $this->smarty->assign('branche_tpl_path2', _PS_THEME_DIR_.'modules/blockcategories2/category-tree-branch.tpl');
  213. else
  214. $this->smarty->assign('branche_tpl_path2', _PS_MODULE_DIR_.'blockcategories2/category-tree-branch.tpl');
  215. $this->smarty->assign('isDhtml', $isDhtml);
  216.  
  217. $display = $this->display(__FILE__, 'blockcategories2.tpl');
  218. return $display;
  219. }
  220.  
  221. protected function getCacheId($name = null)
  222. {
  223. parent::getCacheId($name);
  224.  
  225. $groups = implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id));
  226. $id_product = (int)Tools::getValue('id_product', 0);
  227. $id_category = (int)Tools::getValue('id_category', 0);
  228. $id_lang = (int)$this->context->language->id;
  229. return 'blockcategories2|'.(int)Tools::usingSecureMode().'|'.$this->context->shop->id.'|'.$groups.'|'.$id_lang.'|'.$id_product.'|'.$id_category;
  230. }
  231.  
  232.  
  233. public function hookRightColumn($params)
  234. {
  235. return $this->hookLeftColumn($params);
  236. }
  237.  
  238. public function hookHeader()
  239. {
  240. $this->context->controller->addJS(_THEME_JS_DIR_.'tools/treeManagement.js');
  241. $this->context->controller->addCSS(($this->_path).'blockcategories.css', 'all');
  242. }
  243.  
  244. private function _clearBlockcategories2Cache()
  245. {
  246. $this->_clearCache('blockcategories2.tpl');
  247. $this->_clearCache('blockcategories_footer.tpl');
  248. }
  249.  
  250. public function hookCategoryAddition($params)
  251. {
  252. $this->_clearBlockcategories2Cache();
  253. }
  254.  
  255. public function hookCategoryUpdate($params)
  256. {
  257. $this->_clearBlockcategories2Cache();
  258. }
  259.  
  260. public function hookCategoryDeletion($params)
  261. {
  262. $this->_clearBlockcategories2Cache();
  263. }
  264.  
  265. public function hookActionAdminMetaControllerUpdate_optionsBefore($params)
  266. {
  267. $this->_clearBlockcategories2Cache();
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment