Advertisement
Guest User

Plik TM categoriess php

a guest
Oct 22nd, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.74 KB | None | 0 0
  1. <?php
  2. if (!defined('_CAN_LOAD_FILES_'))
  3. exit;
  4. class TMCategories extends Module
  5. {
  6. public function __construct()
  7. {
  8. $this->name = 'tmcategories';
  9. $this->tab = 'front_office_features';
  10. $this->version = '1.4';
  11. $this->author = 'TM';
  12. parent::__construct();
  13. $this->displayName = $this->l('TM Categories');
  14. $this->description = $this->l('Adds a block featuring product categories at the top of page');
  15. }
  16. public function install()
  17. {
  18. if (!parent::install() OR
  19. !$this->registerHook('top') OR
  20. // Temporary hooks. Do NOT hook any module on it. Some CRUD hook will replace them as soon as possible.
  21. !$this->registerHook('categoryAddition') OR
  22. !$this->registerHook('categoryUpdate') OR
  23. !$this->registerHook('categoryDeletion') OR
  24. !$this->registerHook('afterSaveAdminMeta') OR
  25. !Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 3) OR
  26. !Configuration::updateValue('BLOCK_CATEG_DHTML', 1))
  27. return false;
  28. return true;
  29. }
  30. public function uninstall()
  31. {
  32. if (!parent::uninstall() OR
  33. !Configuration::deleteByName('BLOCK_CATEG_MAX_DEPTH') OR
  34. !Configuration::deleteByName('BLOCK_CATEG_DHTML'))
  35. return false;
  36. return true;
  37. }
  38. public function getContent()
  39. {
  40. $output = '<h2>'.$this->displayName.'</h2>';
  41. if (Tools::isSubmit('submitBlockCategories'))
  42. {
  43. $maxDepth = (int)(Tools::getValue('maxDepth'));
  44. $dhtml = Tools::getValue('dhtml');
  45. $nbrColumns = Tools::getValue('nbrColumns',4);
  46. if ($maxDepth < 0)
  47. $output .= '<div class="alert error">'.$this->l('Maximum depth: Invalid number.').'</div>';
  48. elseif ($dhtml != 0 AND $dhtml != 1)
  49. $output .= '<div class="alert error">'.$this->l('Dynamic HTML: Invalid choice.').'</div>';
  50. else
  51. {
  52. Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', (int)($maxDepth));
  53. Configuration::updateValue('BLOCK_CATEG_DHTML', (int)($dhtml));
  54. Configuration::updateValue('BLOCK_CATEG_NBR_COLUMN_FOOTER', $nbrColumns);
  55. $this->_clearBlockcategoriesCache();
  56. $output .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="'.$this->l('Confirmation').'" />'.$this->l('Settings updated').'</div>';
  57. }
  58. }
  59. return $output.$this->displayForm();
  60. }
  61. public function displayForm()
  62. {
  63. return '
  64. <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
  65. <fieldset>
  66. <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
  67. <label>'.$this->l('Maximum depth').'</label>
  68. <div class="margin-form">
  69. <input type="text" name="maxDepth" value="'.Configuration::get('BLOCK_CATEG_MAX_DEPTH').'" />
  70. <p class="clear">'.$this->l('Set the maximum depth of sublevels displayed in this block (0 = infinite)').'</p>
  71. </div>
  72. <label>'.$this->l('Dynamic').'</label>
  73. <div class="margin-form">
  74. <input type="radio" name="dhtml" id="dhtml_on" value="1" '.(Tools::getValue('dhtml', Configuration::get('BLOCK_CATEG_DHTML')) ? 'checked="checked" ' : '').'/>
  75. <label class="t" for="dhtml_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
  76. <input type="radio" name="dhtml" id="dhtml_off" value="0" '.(!Tools::getValue('dhtml', Configuration::get('BLOCK_CATEG_DHTML')) ? 'checked="checked" ' : '').'/>
  77. <label class="t" for="dhtml_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
  78. <p class="clear">'.$this->l('Activate dynamic (animated) mode for sublevels').'</p>
  79. </div>
  80. <label>'.$this->l('Footer columns number').'</label>
  81. <div class="margin-form">
  82. <input type="text" name="nbrColumns" value="'.Configuration::get('BLOCK_CATEG_NBR_COLUMN_FOOTER').'" />
  83. <p class="clear">'.$this->l('Set the number of footer columns').'</p>
  84. </div>
  85. <center><input type="submit" name="submitBlockCategories" value="'.$this->l('Save').'" class="button" /></center>
  86. </fieldset>
  87. </form>';
  88. }
  89. public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0)
  90. {
  91. global $link;
  92. $children = array();
  93. if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth))
  94. foreach ($resultParents[$id_category] as $subcat)
  95. $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
  96. if (!isset($resultIds[$id_category]))
  97. return false;
  98. return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
  99. 'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
  100. 'children' => $children);
  101. }
  102. public function hookTop($params)
  103. {
  104. global $smarty, $cookie;
  105. $id_customer = (int)($params['cookie']->id_customer);
  106. // Get all groups for this customer and concatenate them as a string: "1,2,3..."
  107. // It is necessary to keep the group query separate from the main select query because it is used for the cache
  108. $groups = $id_customer ? implode(', ', Customer::getGroupsStatic($id_customer)) : _PS_DEFAULT_CUSTOMER_GROUP_;
  109. $id_product = (int)(Tools::getValue('id_product', 0));
  110. $id_category = (int)(Tools::getValue('id_category', 0));
  111. $id_lang = (int)($params['cookie']->id_lang);
  112. $smartyCacheId = 'blockcategories|'.$groups.'_'.$id_lang.'_'.$id_product.'_'.$id_category;
  113. Tools::enableCache();
  114. if (!$this->isCached('tmcategories.tpl', $smartyCacheId))
  115. {
  116. $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
  117. if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
  118. SELECT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
  119. FROM `'._DB_PREFIX_.'category` c
  120. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.')
  121. LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`)
  122. WHERE (c.`active` = 1 OR c.`id_category` = 1)
  123. '.((int)($maxdepth) != 0 ? ' AND `level_depth` <= '.(int)($maxdepth) : '').'
  124. AND cg.`id_group` IN ('.pSQL($groups).')
  125. GROUP BY id_category
  126. ORDER BY `level_depth` ASC, c.`position` ASC')
  127. )
  128. return;
  129. $resultParents = array();
  130. $resultIds = array();
  131. foreach ($result as &$row)
  132. {
  133. $resultParents[$row['id_parent']][] = &$row;
  134. $resultIds[$row['id_category']] = &$row;
  135. }
  136. $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH'));
  137. unset($resultParents);
  138. unset($resultIds);
  139. $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
  140. if (Tools::isSubmit('id_category'))
  141. {
  142. $cookie->last_visited_category = $id_category;
  143. $smarty->assign('currentCategoryId', $cookie->last_visited_category);
  144. }
  145. if (Tools::isSubmit('id_product'))
  146. {
  147. if (!isset($cookie->last_visited_category) OR !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $cookie->last_visited_category))))
  148. {
  149. $product = new Product($id_product);
  150. if (isset($product) AND Validate::isLoadedObject($product))
  151. $cookie->last_visited_category = (int)($product->id_category_default);
  152. }
  153. $smarty->assign('currentCategoryId', (int)($cookie->last_visited_category));
  154. }
  155. $smarty->assign('blockCategTree', $blockCategTree);
  156. if (file_exists(_PS_THEME_DIR_.'modules/tmcategories/tmcategories.tpl'))
  157. $smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/tmcategories/category-tree-branch.tpl');
  158. else
  159. $smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'tmcategories/category-tree-branch.tpl');
  160. $smarty->assign('isDhtml', $isDhtml);
  161. }
  162. $smarty->cache_lifetime = 31536000; // 1 Year
  163. $display = $this->display(__FILE__, 'tmcategories.tpl', $smartyCacheId);
  164. Tools::restoreCacheSettings();
  165. return $display;
  166. }
  167. /*
  168. public function hookFooter($params)
  169. {
  170. global $smarty, $cookie;
  171. $id_customer = (int)($params['cookie']->id_customer);
  172. // Get all groups for this customer and concatenate them as a string: "1,2,3..."
  173. $groups = $id_customer ? implode(', ', Customer::getGroupsStatic($id_customer)) : _PS_DEFAULT_CUSTOMER_GROUP_;
  174. $id_product = (int)(Tools::getValue('id_product', 0));
  175. $id_category = (int)(Tools::getValue('id_category', 0));
  176. $id_lang = (int)($params['cookie']->id_lang);
  177. $smartyCacheId = 'blockcategories|'.$groups.'_'.$id_lang.'_'.$id_product.'_'.$id_category;
  178. Tools::enableCache();
  179. if (!$this->isCached('blockcategories_footer.tpl', $smartyCacheId))
  180. {
  181. $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
  182. if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
  183. SELECT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
  184. FROM `'._DB_PREFIX_.'category` c
  185. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.$id_lang.')
  186. LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`)
  187. WHERE (c.`active` = 1 OR c.`id_category` = 1)
  188. '.((int)($maxdepth) != 0 ? ' AND `level_depth` <= '.(int)($maxdepth) : '').'
  189. AND cg.`id_group` IN ('.pSQL($groups).')
  190. ORDER BY `level_depth` ASC, c.`position` ASC')
  191. )
  192. return;
  193. $resultParents = array();
  194. $resultIds = array();
  195. foreach ($result as &$row)
  196. {
  197. $resultParents[$row['id_parent']][] = &$row;
  198. $resultIds[$row['id_category']] = &$row;
  199. }
  200. //$nbrColumns = Configuration::get('BLOCK_CATEG_NBR_COLUMNS_FOOTER');
  201. $nbrColumns = Configuration::get('BLOCK_CATEG_NBR_COLUMN_FOOTER');
  202. if(!$nbrColumns)
  203. $nbrColumns=3;
  204. $numberColumn = abs(sizeof($result)/$nbrColumns);
  205. $widthColumn= floor(100/$nbrColumns);
  206. $smarty->assign('numberColumn', $numberColumn);
  207. $smarty->assign('widthColumn', $widthColumn);
  208.  
  209. $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH'));
  210. unset($resultParents);
  211. unset($resultIds);
  212. $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
  213. if (Tools::isSubmit('id_category'))
  214. {
  215. $cookie->last_visited_category = $id_category;
  216. $smarty->assign('currentCategoryId', $cookie->last_visited_category);
  217. }
  218. if (Tools::isSubmit('id_product'))
  219. {
  220. if (!isset($cookie->last_visited_category) OR !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $cookie->last_visited_category))))
  221. {
  222. $product = new Product($id_product);
  223. if (isset($product) AND Validate::isLoadedObject($product))
  224. $cookie->last_visited_category = (int)($product->id_category_default);
  225. }
  226. $smarty->assign('currentCategoryId', (int)($cookie->last_visited_category));
  227. }
  228. $smarty->assign('blockCategTree', $blockCategTree);
  229. if (file_exists(_PS_THEME_DIR_.'modules/blockcategories/blockcategories_footer.tpl'))
  230. $smarty->assign('branche_tpl_path', _PS_THEME_DIR_.'modules/blockcategories/category-tree-branch.tpl');
  231. else
  232. $smarty->assign('branche_tpl_path', _PS_MODULE_DIR_.'blockcategories/category-tree-branch.tpl');
  233. $smarty->assign('isDhtml', $isDhtml);
  234. }
  235. $smarty->cache_lifetime = 31536000; // 1 Year
  236. $display = $this->display(__FILE__, 'blockcategories_footer.tpl', $smartyCacheId);
  237. Tools::restoreCacheSettings();
  238. return $display;
  239. }
  240. */
  241. private function _clearBlockcategoriesCache()
  242. {
  243. $this->_clearCache('tmcategories.tpl');
  244. Tools::restoreCacheSettings();
  245. }
  246. public function hookCategoryAddition($params)
  247. {
  248. $this->_clearBlockcategoriesCache();
  249. }
  250. public function hookCategoryUpdate($params)
  251. {
  252. $this->_clearBlockcategoriesCache();
  253. }
  254. public function hookCategoryDeletion($params)
  255. {
  256. $this->_clearBlockcategoriesCache();
  257. }
  258. public function hookAfterSaveAdminMeta($params)
  259. {
  260. $this->_clearBlockcategoriesCache();
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement