VEKIA

blocktopmenu.php no cache

Dec 6th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.92 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. require (dirname(__FILE__).'/menutoplinks.class.php');
  28.  
  29. class Blocktopmenu extends Module
  30. {
  31. private $_menu = '';
  32. private $_html = '';
  33. private $user_groups;
  34.  
  35. /*
  36. * Pattern for matching config values
  37. */
  38. private $pattern = '/^([A-Z_]*)[0-9]+/';
  39.  
  40. /*
  41. * Name of the controller
  42. * Used to set item selected or not in top menu
  43. */
  44. private $page_name = '';
  45.  
  46. /*
  47. * Spaces per depth in BO
  48. */
  49. private $spacer_size = '5';
  50.  
  51. public function __construct()
  52. {
  53. $this->name = 'blocktopmenu';
  54. $this->tab = 'front_office_features';
  55. $this->version = 1.7;
  56. $this->author = 'PrestaShop';
  57.  
  58. parent::__construct();
  59.  
  60. $this->displayName = $this->l('Top horizontal menu');
  61. $this->description = $this->l('Add a new horizontal menu to the top of your e-commerce website.');
  62. }
  63.  
  64. public function install()
  65. {
  66. if (!parent::install() ||
  67. !$this->registerHook('displayTop') ||
  68. !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_ITEMS', 'CAT1,CMS1,CMS2,PRD1') ||
  69. !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_SEARCH', '1') ||
  70. !$this->registerHook('actionObjectCategoryUpdateAfter') ||
  71. !$this->registerHook('actionObjectCategoryDeleteAfter') ||
  72. !$this->registerHook('actionObjectCategoryAddAfter') ||
  73. !$this->registerHook('actionObjectCmsUpdateAfter') ||
  74. !$this->registerHook('actionObjectCmsDeleteAfter') ||
  75. !$this->registerHook('actionObjectCmsAddAfter') ||
  76. !$this->registerHook('actionObjectSupplierUpdateAfter') ||
  77. !$this->registerHook('actionObjectSupplierDeleteAfter') ||
  78. !$this->registerHook('actionObjectSupplierAddAfter') ||
  79. !$this->registerHook('actionObjectManufacturerUpdateAfter') ||
  80. !$this->registerHook('actionObjectManufacturerDeleteAfter') ||
  81. !$this->registerHook('actionObjectManufacturerAddAfter') ||
  82. !$this->registerHook('actionObjectProductUpdateAfter') ||
  83. !$this->registerHook('actionObjectProductDeleteAfter') ||
  84. !$this->registerHook('actionObjectProductAddAfter') ||
  85. !$this->registerHook('categoryUpdate') ||
  86. !$this->registerHook('actionShopDataDuplication') ||
  87. !$this->installDB())
  88. return false;
  89. return true;
  90. }
  91.  
  92. public function installDb()
  93. {
  94. return (Db::getInstance()->execute('
  95. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmenutop` (
  96. `id_linksmenutop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  97. `id_shop` INT(11) UNSIGNED NOT NULL,
  98. `new_window` TINYINT( 1 ) NOT NULL,
  99. INDEX (`id_shop`)
  100. ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;') &&
  101. Db::getInstance()->execute('
  102. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmenutop_lang` (
  103. `id_linksmenutop` INT(11) UNSIGNED NOT NULL,
  104. `id_lang` INT(11) UNSIGNED NOT NULL,
  105. `id_shop` INT(11) UNSIGNED NOT NULL,
  106. `label` VARCHAR( 128 ) NOT NULL ,
  107. `link` VARCHAR( 128 ) NOT NULL ,
  108. INDEX ( `id_linksmenutop` , `id_lang`, `id_shop`)
  109. ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;'));
  110. }
  111.  
  112. public function uninstall()
  113. {
  114. if (!parent::uninstall() ||
  115. !Configuration::deleteByName('MOD_BLOCKTOPMENU_ITEMS') ||
  116. !Configuration::deleteByName('MOD_BLOCKTOPMENU_SEARCH') ||
  117. !$this->uninstallDB())
  118. return false;
  119. return true;
  120. }
  121.  
  122. private function uninstallDb()
  123. {
  124. Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmenutop`');
  125. Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmenutop_lang`');
  126. return true;
  127. }
  128.  
  129. public function getContent()
  130. {
  131. $id_lang = (int)Context::getContext()->language->id;
  132. $languages = $this->context->controller->getLanguages();
  133. $default_language = (int)Configuration::get('PS_LANG_DEFAULT');
  134.  
  135. $labels = Tools::getValue('label') ? array_filter(Tools::getValue('label'), 'strlen') : array();
  136. $links_label = Tools::getValue('link') ? array_filter(Tools::getValue('link'), 'strlen') : array();
  137. $spacer = str_repeat('&nbsp;', $this->spacer_size);
  138. $divLangName = 'link_label';
  139.  
  140. $update_cache = false;
  141.  
  142. if (Tools::isSubmit('submitBlocktopmenu'))
  143. {
  144. if (Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', Tools::getValue('items')))
  145. $this->_html .= $this->displayConfirmation($this->l('The settings have been updated.'));
  146. else
  147. $this->_html .= $this->displayError($this->l('Unable to update settings.'));
  148. Configuration::updateValue('MOD_BLOCKTOPMENU_SEARCH', (bool)Tools::getValue('search'));
  149. $update_cache = true;
  150. }
  151. else if (Tools::isSubmit('submitBlocktopmenuLinks'))
  152. {
  153.  
  154. if ((!count($links_label)) && (!count($labels)))
  155. ;
  156. else if (!count($links_label))
  157. $this->_html .= $this->displayError($this->l('Please complete the "link" field.'));
  158. else if (!count($labels))
  159. $this->_html .= $this->displayError($this->l('Please add a label'));
  160. else if (!isset($labels[$default_language]))
  161. $this->_html .= $this->displayError($this->l('Please add a label for your default language.'));
  162. else
  163. {
  164. MenuTopLinks::add(Tools::getValue('link'), Tools::getValue('label'), Tools::getValue('new_window', 0), (int)Shop::getContextShopID());
  165. $this->_html .= $this->displayConfirmation($this->l('The link has been added.'));
  166. }
  167. $update_cache = true;
  168. }
  169. else if (Tools::isSubmit('submitBlocktopmenuRemove'))
  170. {
  171. $id_linksmenutop = Tools::getValue('id_linksmenutop', 0);
  172. MenuTopLinks::remove($id_linksmenutop, (int)Shop::getContextShopID());
  173. Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', str_replace(array('LNK'.$id_linksmenutop.',', 'LNK'.$id_linksmenutop), '', Configuration::get('MOD_BLOCKTOPMENU_ITEMS')));
  174. $this->_html .= $this->displayConfirmation($this->l('The link has been removed'));
  175. $update_cache = true;
  176. }
  177. else if (Tools::isSubmit('submitBlocktopmenuEdit'))
  178. {
  179. $id_linksmenutop = (int)Tools::getValue('id_linksmenutop', 0);
  180. $id_shop = (int)Shop::getContextShopID();
  181.  
  182. if (!Tools::isSubmit('link'))
  183. {
  184. $tmp = MenuTopLinks::getLinkLang($id_linksmenutop, $id_shop);
  185. $links_label_edit = $tmp['link'];
  186. $labels_edit = $tmp['label'];
  187. $new_window_edit = $tmp['new_window'];
  188. }
  189. else
  190. {
  191. MenuTopLinks::update(Tools::getValue('link'), Tools::getValue('label'), Tools::getValue('new_window', 0), (int)$id_shop, (int)$id_linksmenutop, (int)$id_linksmenutop);
  192. $this->_html .= $this->displayConfirmation($this->l('The link has been edited'));
  193. }
  194. $update_cache = true;
  195. }
  196.  
  197. //if ($update_cache)
  198. //
  199.  
  200. $this->_html .= '
  201. <fieldset>
  202. <div class="multishop_info">
  203. '.$this->l('The modifications will be applied to').' '.(Shop::getContext() == Shop::CONTEXT_SHOP ? $this->l('shop').' '.$this->context->shop->name : $this->l('all shops')).'.
  204. </div>
  205. <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
  206. <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" id="form">
  207. <div style="display: none">
  208. <label>'.$this->l('Items').'</label>
  209. <div class="margin-form">
  210. <input type="text" name="items" id="itemsInput" value="'.Tools::safeOutput(Configuration::get('MOD_BLOCKTOPMENU_ITEMS')).'" size="70" />
  211. </div>
  212. </div>
  213.  
  214. <div class="clear">&nbsp;</div>
  215. <table style="margin-left: 130px;">
  216. <tbody>
  217. <tr>
  218. <td style="padding-left: 20px;">
  219. <select multiple="multiple" id="availableItems" style="width: 300px; height: 160px;">';
  220.  
  221. // BEGIN CMS
  222. $this->_html .= '<optgroup label="'.$this->l('CMS').'">';
  223. $this->getCMSOptions(0, 1, $id_lang);
  224. $this->_html .= '</optgroup>';
  225.  
  226. // BEGIN SUPPLIER
  227. $this->_html .= '<optgroup label="'.$this->l('Supplier').'">';
  228. // Option to show all Suppliers
  229. $this->_html .= '<option value="ALLSUP0">'.$this->l('All suppliers').'</option>';
  230. $suppliers = Supplier::getSuppliers(false, $id_lang);
  231. foreach ($suppliers as $supplier)
  232. $this->_html .= '<option value="SUP'.$supplier['id_supplier'].'">'.$spacer.$supplier['name'].'</option>';
  233. $this->_html .= '</optgroup>';
  234.  
  235. // BEGIN Manufacturer
  236. $this->_html .= '<optgroup label="'.$this->l('Manufacturer').'">';
  237. // Option to show all Manufacturers
  238. $this->_html .= '<option value="ALLMAN0">'.$this->l('All manufacturers').'</option>';
  239. $manufacturers = Manufacturer::getManufacturers(false, $id_lang);
  240. foreach ($manufacturers as $manufacturer)
  241. $this->_html .= '<option value="MAN'.$manufacturer['id_manufacturer'].'">'.$spacer.$manufacturer['name'].'</option>';
  242. $this->_html .= '</optgroup>';
  243.  
  244. // BEGIN Categories
  245. $this->_html .= '<optgroup label="'.$this->l('Categories').'">';
  246. $this->getCategoryOption(1, (int)$id_lang, (int)Shop::getContextShopID());
  247. $this->_html .= '</optgroup>';
  248.  
  249. // BEGIN Shops
  250. if (Shop::isFeatureActive())
  251. {
  252. $this->_html .= '<optgroup label="'.$this->l('Shops').'">';
  253. $shops = Shop::getShopsCollection();
  254. foreach ($shops as $shop)
  255. {
  256. if (!$shop->setUrl() && !$shop->getBaseURL())
  257. continue;
  258. $this->_html .= '<option value="SHOP'.(int)$shop->id.'">'.$spacer.$shop->name.'</option>';
  259. }
  260. $this->_html .= '</optgroup>';
  261. }
  262.  
  263. // BEGIN Products
  264. $this->_html .= '<optgroup label="'.$this->l('Products').'">';
  265. $this->_html .= '<option value="PRODUCT" style="font-style:italic">'.$spacer.$this->l('Choose product ID').'</option>';
  266. $this->_html .= '</optgroup>';
  267.  
  268. // BEGIN Menu Top Links
  269. $this->_html .= '<optgroup label="'.$this->l('Menu Top Links').'">';
  270. $links = MenuTopLinks::gets($id_lang, null, (int)Shop::getContextShopID());
  271. foreach ($links as $link)
  272. {
  273. if ($link['label'] == '')
  274. {
  275. $link = MenuTopLinks::get($link['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
  276. $this->_html .= '<option value="LNK'.(int)$link[0]['id_linksmenutop'].'">'.$spacer.$link[0]['label'].'</option>';
  277. }
  278. else
  279. $this->_html .= '<option value="LNK'.(int)$link['id_linksmenutop'].'">'.$spacer.$link['label'].'</option>';
  280. }
  281. $this->_html .= '</optgroup>';
  282.  
  283. $this->_html .= '</select><br />
  284. <br />
  285. <a href="#" id="addItem" style="border: 1px solid rgb(170, 170, 170); margin: 2px; padding: 2px; text-align: center; display: block; text-decoration: none; background-color: rgb(250, 250, 250); color: rgb(18, 52, 86);">'.$this->l('Add').' &gt;&gt;</a>
  286. </td>
  287. <td>
  288. <select multiple="multiple" id="items" style="width: 300px; height: 160px;">';
  289. $this->makeMenuOption();
  290. $this->_html .= '</select><br/>
  291. <br/>
  292. <a href="#" id="removeItem" style="border: 1px solid rgb(170, 170, 170); margin: 2px; padding: 2px; text-align: center; display: block; text-decoration: none; background-color: rgb(250, 250, 250); color: rgb(18, 52, 86);">&lt;&lt; '.$this->l('Remove').'</a>
  293. </td>
  294. <td style="vertical-align:top;padding:5px 15px;">
  295. <h4 style="margin-top:5px;">'.$this->l('Change position').'</h4>
  296. <a href="#" id="menuOrderUp" class="button" style="font-size:20px;display:block;">&uarr;</a><br/>
  297. <a href="#" id="menuOrderDown" class="button" style="font-size:20px;display:block;">&darr;</a><br/>
  298. </td>
  299. </tr>
  300. </tbody>
  301. </table>
  302. <div class="clear">&nbsp;</div>
  303. <script type="text/javascript">
  304. function add()
  305. {
  306. $("#availableItems option:selected").each(function(i){
  307. var val = $(this).val();
  308. var text = $(this).text();
  309. text = text.replace(/(^\s*)|(\s*$)/gi,"");
  310. if (val == "PRODUCT")
  311. {
  312. val = prompt("'.$this->l('Set ID product').'");
  313. if (val == null || val == "" || isNaN(val))
  314. return;
  315. text = "'.$this->l('Product ID').' "+val;
  316. val = "PRD"+val;
  317. }
  318. $("#items").append("<option value=\""+val+"\">"+text+"</option>");
  319. });
  320. serialize();
  321. return false;
  322. }
  323.  
  324. function remove()
  325. {
  326. $("#items option:selected").each(function(i){
  327. $(this).remove();
  328. });
  329. serialize();
  330. return false;
  331. }
  332.  
  333. function serialize()
  334. {
  335. var options = "";
  336. $("#items option").each(function(i){
  337. options += $(this).val() + ",";
  338. });
  339. $("#itemsInput").val(options.substr(0, options.length - 1));
  340. }
  341.  
  342. function move(up)
  343. {
  344. var tomove = $("#items option:selected");
  345. if (tomove.length >1)
  346. {
  347. alert(\''.Tools::htmlentitiesUTF8($this->l('Please select just one item')).'\');
  348. return false;
  349. }
  350. if (up)
  351. tomove.prev().insertAfter(tomove);
  352. else
  353. tomove.next().insertBefore(tomove);
  354. serialize();
  355. return false;
  356. }
  357.  
  358. $(document).ready(function(){
  359. $("#addItem").click(add);
  360. $("#availableItems").dblclick(add);
  361. $("#removeItem").click(remove);
  362. $("#items").dblclick(remove);
  363. $("#menuOrderUp").click(function(e){
  364. e.preventDefault();
  365. move(true);
  366. });
  367. $("#menuOrderDown").click(function(e){
  368. e.preventDefault();
  369. move();
  370. });
  371. });
  372. </script>
  373. <label for="s">'.$this->l('Search Bar').'</label>
  374. <div class="margin-form">
  375. <input type="checkbox" name="search" id="s" value="1"'.((Configuration::get('MOD_BLOCKTOPMENU_SEARCH')) ? ' checked=""': '').'/>
  376. </div>
  377. <p class="center">
  378. <input type="submit" name="submitBlocktopmenu" value="'.$this->l('Save ').'" class="button" />
  379. </p>
  380. </form>
  381. </fieldset><br />';
  382.  
  383. $this->_html .= '
  384. <fieldset>
  385. <legend><img src="../img/admin/add.gif" alt="" title="" />'.$this->l('Add Menu Top Link').'</legend>
  386. <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" id="form">
  387.  
  388. ';
  389. foreach ($languages as $language)
  390. {
  391. $this->_html .= '
  392. <div id="link_label_'.(int)$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang ? 'block' : 'none').';">
  393. <label>'.$this->l('Label').'</label>
  394. <div class="margin-form">
  395. <input type="text" name="label['.(int)$language['id_lang'].']" id="label_'.(int)$language['id_lang'].'" size="70" value="'.(isset($labels_edit[$language['id_lang']]) ? $labels_edit[$language['id_lang']] : '').'" />
  396. </div>
  397. ';
  398.  
  399. $this->_html .= '
  400. <label>'.$this->l('Link').'</label>
  401. <div class="margin-form">
  402. <input type="text" name="link['.(int)$language['id_lang'].']" id="link_'.(int)$language['id_lang'].'" value="'.(isset($links_label_edit[$language['id_lang']]) ? $links_label_edit[$language['id_lang']] : '').'" size="70" />
  403. </div>
  404. </div>';
  405. }
  406.  
  407. $this->_html .= '<label>'.$this->l('Language').'</label>
  408. <div class="margin-form">'.$this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true).'</div><p style="clear: both;"> </p>';
  409.  
  410. $this->_html .= '<label style="clear: both;">'.$this->l('New Window').'</label>
  411. <div class="margin-form">
  412. <input style="clear: both;" type="checkbox" name="new_window" value="1" '.(isset($new_window_edit) && $new_window_edit ? 'checked' : '').'/>
  413. </div>
  414. <div class="margin-form">';
  415.  
  416. if (Tools::isSubmit('id_linksmenutop'))
  417. $this->_html .= '<input type="hidden" name="id_linksmenutop" value="'.(int)Tools::getValue('id_linksmenutop').'" />';
  418.  
  419. if (Tools::isSubmit('submitBlocktopmenuEdit'))
  420. $this->_html .= '<input type="submit" name="submitBlocktopmenuEdit" value="'.$this->l('Edit').'" class="button" />';
  421.  
  422. $this->_html .= '
  423. <input type="submit" name="submitBlocktopmenuLinks" value="'.$this->l('Add ').'" class="button" />
  424. </div>
  425.  
  426. </form>
  427. </fieldset><br />';
  428.  
  429. $links = MenuTopLinks::gets((int)$id_lang, null, (int)Shop::getContextShopID());
  430.  
  431. if (!count($links))
  432. return $this->_html;
  433.  
  434. $this->_html .= '
  435. <fieldset>
  436. <legend><img src="../img/admin/details.gif" alt="" title="" />'.$this->l('List Menu Top Link').'</legend>
  437. <table style="width:100%;">
  438. <thead>
  439. <tr style="text-align: left;">
  440. <th>'.$this->l('Id Link').'</th>
  441. <th>'.$this->l('Label').'</th>
  442. <th>'.$this->l('Link').'</th>
  443. <th>'.$this->l('New Window').'</th>
  444. <th>'.$this->l('Action').'</th>
  445. </tr>
  446. </thead>
  447. <tbody>';
  448. foreach ($links as $link)
  449. {
  450. $this->_html .= '
  451. <tr>
  452. <td>'.(int)$link['id_linksmenutop'].'</td>
  453. <td>'.Tools::safeOutput($link['label']).'</td>
  454. <td><a href="'.Tools::safeOutput($link['link']).'"'.(($link['new_window']) ? ' target="_blank"' : '').'>'.Tools::safeOutput($link['link']).'</a></td>
  455. <td>'.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).'</td>
  456. <td>
  457. <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
  458. <input type="hidden" name="id_linksmenutop" value="'.(int)$link['id_linksmenutop'].'" />
  459. <input type="submit" name="submitBlocktopmenuEdit" value="'.$this->l('Edit').'" class="button" />
  460. <input type="submit" name="submitBlocktopmenuRemove" value="'.$this->l('Remove').'" class="button" />
  461. </form>
  462. </td>
  463. </tr>';
  464. }
  465. $this->_html .= '</tbody>
  466. </table>
  467. </fieldset>';
  468. return $this->_html;
  469. }
  470.  
  471. private function getMenuItems()
  472. {
  473. return explode(',', Configuration::get('MOD_BLOCKTOPMENU_ITEMS'));
  474. }
  475.  
  476. private function makeMenuOption()
  477. {
  478. $menu_item = $this->getMenuItems();
  479. $id_lang = (int)$this->context->language->id;
  480. $id_shop = (int)Shop::getContextShopID();
  481. foreach ($menu_item as $item)
  482. {
  483. if (!$item)
  484. continue;
  485.  
  486. preg_match($this->pattern, $item, $values);
  487. $id = (int)substr($item, strlen($values[1]), strlen($item));
  488.  
  489. switch (substr($item, 0, strlen($values[1])))
  490. {
  491. case 'CAT':
  492. $category = new Category((int)$id, (int)$id_lang);
  493. if (Validate::isLoadedObject($category))
  494. $this->_html .= '<option value="CAT'.$id.'">'.$category->name.'</option>'.PHP_EOL;
  495. break;
  496.  
  497. case 'PRD':
  498. $product = new Product((int)$id, true, (int)$id_lang);
  499. if (Validate::isLoadedObject($product))
  500. $this->_html .= '<option value="PRD'.$id.'">'.$product->name.'</option>'.PHP_EOL;
  501. break;
  502.  
  503. case 'CMS':
  504. $cms = new CMS((int)$id, (int)$id_lang);
  505. if (Validate::isLoadedObject($cms))
  506. $this->_html .= '<option value="CMS'.$id.'">'.$cms->meta_title.'</option>'.PHP_EOL;
  507. break;
  508.  
  509. case 'CMS_CAT':
  510. $category = new CMSCategory((int)$id, (int)$id_lang);
  511. if (Validate::isLoadedObject($category))
  512. $this->_html .= '<option value="CMS_CAT'.$id.'">'.$category->name.'</option>'.PHP_EOL;
  513. break;
  514.  
  515. // Case to handle the option to show all Manufacturers
  516. case 'ALLMAN':
  517. $this->_html .= '<option value="ALLMAN0">'.$this->l('All manufacturers').'</option>'.PHP_EOL;
  518. break;
  519.  
  520. case 'MAN':
  521. $manufacturer = new Manufacturer((int)$id, (int)$id_lang);
  522. if (Validate::isLoadedObject($manufacturer))
  523. $this->_html .= '<option value="MAN'.$id.'">'.$manufacturer->name.'</option>'.PHP_EOL;
  524. break;
  525.  
  526. // Case to handle the option to show all Suppliers
  527. case 'ALLSUP':
  528. $this->_html .= '<option value="ALLSUP0">'.$this->l('All suppliers').'</option>'.PHP_EOL;
  529. break;
  530.  
  531. case 'SUP':
  532. $supplier = new Supplier((int)$id, (int)$id_lang);
  533. if (Validate::isLoadedObject($supplier))
  534. $this->_html .= '<option value="SUP'.$id.'">'.$supplier->name.'</option>'.PHP_EOL;
  535. break;
  536.  
  537. case 'LNK':
  538. $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
  539. if (count($link))
  540. {
  541. if (!isset($link[0]['label']) || ($link[0]['label'] == ''))
  542. {
  543. $default_language = Configuration::get('PS_LANG_DEFAULT');
  544. $link = MenuTopLinks::get($link[0]['id_linksmenutop'], (int)$default_language, (int)Shop::getContextShopID());
  545. }
  546. $this->_html .= '<option value="LNK'.$link[0]['id_linksmenutop'].'">'.$link[0]['label'].'</option>';
  547. }
  548. break;
  549. case 'SHOP':
  550. $shop = new Shop((int)$id);
  551. if (Validate::isLoadedObject($shop))
  552. $this->_html .= '<option value="SHOP'.(int)$id.'">'.$shop->name.'</option>'.PHP_EOL;
  553. break;
  554. }
  555. }
  556. }
  557.  
  558. private function makeMenu()
  559. {
  560. $menu_items = $this->getMenuItems();
  561. $id_lang = (int)$this->context->language->id;
  562. $id_shop = (int)Shop::getContextShopID();
  563.  
  564. foreach ($menu_items as $item)
  565. {
  566. if (!$item)
  567. continue;
  568.  
  569. preg_match($this->pattern, $item, $value);
  570. $id = (int)substr($item, strlen($value[1]), strlen($item));
  571.  
  572. switch (substr($item, 0, strlen($value[1])))
  573. {
  574. case 'CAT':
  575. $this->getCategory($id, $id_lang, $id_shop);
  576. break;
  577.  
  578. case 'PRD':
  579. $selected = ($this->page_name == 'product' && (Tools::getValue('id_product') == $id)) ? ' class="sfHover"' : '';
  580. $product = new Product((int)$id, true, (int)$id_lang);
  581. if (!is_null($product->id))
  582. $this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($product->getLink()).'">'.$product->name.'</a></li>'.PHP_EOL;
  583. break;
  584.  
  585. case 'CMS':
  586. $selected = ($this->page_name == 'cms' && (Tools::getValue('id_cms') == $id)) ? ' class="sfHover"' : '';
  587. $cms = CMS::getLinks((int)$id_lang, array($id));
  588. if (count($cms))
  589. $this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($cms[0]['link']).'">'.$cms[0]['meta_title'].'</a></li>'.PHP_EOL;
  590. break;
  591.  
  592. case 'CMS_CAT':
  593. $category = new CMSCategory((int)$id, (int)$id_lang);
  594. if (count($category))
  595. {
  596. $this->_menu .= '<li><a href="'.Tools::HtmlEntitiesUTF8($category->getLink()).'">'.$category->name.'</a>';
  597. $this->getCMSMenuItems($category->id);
  598. $this->_menu .= '</li>'.PHP_EOL;
  599. }
  600. break;
  601.  
  602. // Case to handle the option to show all Manufacturers
  603. case 'ALLMAN':
  604. $link = new Link;
  605. $this->_menu .= '<li><a href="'.$link->getPageLink('manufacturer').'">'.$this->l('All manufacturers').'</a><ul>'.PHP_EOL;
  606. $manufacturers = Manufacturer::getManufacturers();
  607. foreach ($manufacturers as $key => $manufacturer)
  608. $this->_menu .= '<li><a href="'.$link->getManufacturerLink((int)$manufacturer['id_manufacturer'], $manufacturer['link_rewrite']).'">'.$manufacturer['name'].'</a></li>'.PHP_EOL;
  609. $this->_menu .= '</ul>';
  610. break;
  611.  
  612. case 'MAN':
  613. $selected = ($this->page_name == 'manufacturer' && (Tools::getValue('id_manufacturer') == $id)) ? ' class="sfHover"' : '';
  614. $manufacturer = new Manufacturer((int)$id, (int)$id_lang);
  615. if (!is_null($manufacturer->id))
  616. {
  617. if (intval(Configuration::get('PS_REWRITING_SETTINGS')))
  618. $manufacturer->link_rewrite = Tools::link_rewrite($manufacturer->name);
  619. else
  620. $manufacturer->link_rewrite = 0;
  621. $link = new Link;
  622. $this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($link->getManufacturerLink((int)$id, $manufacturer->link_rewrite)).'">'.$manufacturer->name.'</a></li>'.PHP_EOL;
  623. }
  624. break;
  625.  
  626. // Case to handle the option to show all Suppliers
  627. case 'ALLSUP':
  628. $link = new Link;
  629. $this->_menu .= '<li><a href="'.$link->getPageLink('supplier').'">'.$this->l('All suppliers').'</a><ul>'.PHP_EOL;
  630. $suppliers = Supplier::getSuppliers();
  631. foreach ($suppliers as $key => $supplier)
  632. $this->_menu .= '<li><a href="'.$link->getSupplierLink((int)$supplier['id_supplier'], $supplier['link_rewrite']).'">'.$supplier['name'].'</a></li>'.PHP_EOL;
  633. $this->_menu .= '</ul>';
  634. break;
  635.  
  636. case 'SUP':
  637. $selected = ($this->page_name == 'supplier' && (Tools::getValue('id_supplier') == $id)) ? ' class="sfHover"' : '';
  638. $supplier = new Supplier((int)$id, (int)$id_lang);
  639. if (!is_null($supplier->id))
  640. {
  641. $link = new Link;
  642. $this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($link->getSupplierLink((int)$id, $supplier->link_rewrite)).'">'.$supplier->name.'</a></li>'.PHP_EOL;
  643. }
  644. break;
  645.  
  646. case 'SHOP':
  647. $selected = ($this->page_name == 'index' && ($this->context->shop->id == $id)) ? ' class="sfHover"' : '';
  648. $shop = new Shop((int)$id);
  649. if (Validate::isLoadedObject($shop))
  650. {
  651. $link = new Link;
  652. $this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($shop->getBaseURL()).'">'.$shop->name.'</a></li>'.PHP_EOL;
  653. }
  654. break;
  655. case 'LNK':
  656. $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
  657. if (count($link))
  658. {
  659. if (!isset($link[0]['label']) || ($link[0]['label'] == ''))
  660. {
  661. $default_language = Configuration::get('PS_LANG_DEFAULT');
  662. $link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
  663. }
  664. $this->_menu .= '<li><a href="'.Tools::HtmlEntitiesUTF8($link[0]['link']).'"'.(($link[0]['new_window']) ? ' target="_blank"': '').'>'.$link[0]['label'].'</a></li>'.PHP_EOL;
  665. }
  666. break;
  667. }
  668. }
  669. }
  670.  
  671. private function getCategoryOption($id_category = 1, $id_lang = false, $id_shop = false, $recursive = true)
  672. {
  673. $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
  674. $category = new Category((int)$id_category, (int)$id_lang, (int)$id_shop);
  675.  
  676. if (is_null($category->id))
  677. return;
  678.  
  679. if ($recursive)
  680. {
  681. $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);
  682. $spacer = str_repeat('&nbsp;', $this->spacer_size * (int)$category->level_depth);
  683. }
  684.  
  685. $shop = (object) Shop::getShop((int)$category->getShopID());
  686. $this->_html .= '<option value="CAT'.(int)$category->id.'">'.(isset($spacer) ? $spacer : '').$category->name.' ('.$shop->name.')</option>';
  687.  
  688. if (isset($children) && count($children))
  689. foreach ($children as $child)
  690. $this->getCategoryOption((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);
  691. }
  692.  
  693. private function getCategory($id_category, $id_lang = false, $id_shop = false)
  694. {
  695. $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
  696. $category = new Category((int)$id_category, (int)$id_lang);
  697.  
  698. if ($category->level_depth > 0)
  699. $category_link = $category->getLink();
  700. else
  701. $category_link = $this->context->link->getPageLink('index');
  702.  
  703. if (is_null($category->id))
  704. return;
  705.  
  706. $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);
  707. $selected = ($this->page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : '';
  708.  
  709. $is_intersected = array_intersect($category->getGroups(), $this->user_groups);
  710. // filter the categories that the user is allowed to see and browse
  711. if (!empty($is_intersected))
  712. {
  713. $this->_menu .= '<li '.$selected.'>';
  714. $this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';
  715.  
  716. if (count($children))
  717. {
  718. $this->_menu .= '<ul>';
  719.  
  720. foreach ($children as $child)
  721. $this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);
  722.  
  723. $this->_menu .= '</ul>';
  724. }
  725. $this->_menu .= '</li>';
  726. }
  727. }
  728.  
  729. private function getCMSMenuItems($parent, $depth = 1, $id_lang = false)
  730. {
  731. $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
  732.  
  733. if ($depth > 3)
  734. return;
  735.  
  736. $categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang);
  737. $pages = $this->getCMSPages((int)$parent);
  738.  
  739. if (count($categories) || count($pages))
  740. {
  741. $this->_menu .= '<ul>';
  742.  
  743. foreach ($categories as $category)
  744. {
  745. $this->_menu .= '<li>';
  746. $this->_menu .= '<a href="#">'.$category['name'].'</a>';
  747. $this->getCMSMenuItems($category['id_cms_category'], (int)$depth + 1);
  748. $this->_menu .= '</li>';
  749. }
  750.  
  751. foreach ($pages as $page)
  752. {
  753. $cms = new CMS($page['id_cms'], (int)$id_lang);
  754. $links = $cms->getLinks((int)$id_lang, array((int)$cms->id));
  755.  
  756. $selected = ($this->page_name == 'cms' && ((int)Tools::getValue('id_cms') == $page['id_cms'])) ? ' class="sfHoverForce"' : '';
  757. $this->_menu .= '<li '.$selected.'>';
  758. $this->_menu .= '<a href="'.$links[0]['link'].'">'.$cms->meta_title.'</a>';
  759. $this->_menu .= '</li>';
  760. }
  761.  
  762. $this->_menu .= '</ul>';
  763. }
  764. }
  765.  
  766. private function getCMSOptions($parent = 0, $depth = 1, $id_lang = false)
  767. {
  768. $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
  769.  
  770. $categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang);
  771. $pages = $this->getCMSPages((int)$parent, false, (int)$id_lang);
  772.  
  773. $spacer = str_repeat('&nbsp;', $this->spacer_size * (int)$depth);
  774.  
  775. foreach ($categories as $category)
  776. {
  777. $this->_html .= '<option value="CMS_CAT'.$category['id_cms_category'].'" style="font-weight: bold;">'.$spacer.$category['name'].'</option>';
  778. $this->getCMSOptions($category['id_cms_category'], (int)$depth + 1, (int)$id_lang);
  779. }
  780.  
  781. foreach ($pages as $page)
  782. $this->_html .= '<option value="CMS'.$page['id_cms'].'">'.$spacer.$page['meta_title'].'</option>';
  783. }
  784.  
  785.  
  786.  
  787. public function hookDisplayTop($param)
  788. {
  789. $this->user_groups = ($this->context->customer->isLogged() ? $this->context->customer->getGroups() : array(Configuration::get('PS_UNIDENTIFIED_GROUP')));
  790. $this->page_name = Dispatcher::getInstance()->getController();
  791.  
  792. $this->makeMenu();
  793. $this->smarty->assign('MENU_SEARCH', Configuration::get('MOD_BLOCKTOPMENU_SEARCH'));
  794. $this->smarty->assign('MENU', $this->_menu);
  795. $this->smarty->assign('this_path', $this->_path);
  796.  
  797.  
  798. $this->context->controller->addJS($this->_path.'js/hoverIntent.js');
  799. $this->context->controller->addJS($this->_path.'js/superfish-modified.js');
  800. $this->context->controller->addCSS($this->_path.'css/superfish-modified.css');
  801.  
  802. $html = $this->display(__FILE__, 'blocktopmenu.tpl');
  803. return $html;
  804. }
  805.  
  806. private function getCMSCategories($recursive = false, $parent = 1, $id_lang = false)
  807. {
  808. $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
  809.  
  810. if ($recursive === false)
  811. {
  812. $sql = 'SELECT bcp.`id_cms_category`, bcp.`id_parent`, bcp.`level_depth`, bcp.`active`, bcp.`position`, cl.`name`, cl.`link_rewrite`
  813. FROM `'._DB_PREFIX_.'cms_category` bcp
  814. INNER JOIN `'._DB_PREFIX_.'cms_category_lang` cl
  815. ON (bcp.`id_cms_category` = cl.`id_cms_category`)
  816. WHERE cl.`id_lang` = '.(int)$id_lang.'
  817. AND bcp.`id_parent` = '.(int)$parent;
  818.  
  819. return Db::getInstance()->executeS($sql);
  820. }
  821. else
  822. {
  823. $sql = 'SELECT bcp.`id_cms_category`, bcp.`id_parent`, bcp.`level_depth`, bcp.`active`, bcp.`position`, cl.`name`, cl.`link_rewrite`
  824. FROM `'._DB_PREFIX_.'cms_category` bcp
  825. INNER JOIN `'._DB_PREFIX_.'cms_category_lang` cl
  826. ON (bcp.`id_cms_category` = cl.`id_cms_category`)
  827. WHERE cl.`id_lang` = '.(int)$id_lang.'
  828. AND bcp.`id_parent` = '.(int)$parent;
  829.  
  830. $results = Db::getInstance()->executeS($sql);
  831. foreach ($results as $result)
  832. {
  833. $sub_categories = $this->getCMSCategories(true, $result['id_cms_category'], (int)$id_lang);
  834. if ($sub_categories && count($sub_categories) > 0)
  835. $result['sub_categories'] = $sub_categories;
  836. $categories[] = $result;
  837. }
  838.  
  839. return isset($categories) ? $categories : false;
  840. }
  841.  
  842. }
  843.  
  844. private function getCMSPages($id_cms_category, $id_shop = false, $id_lang = false)
  845. {
  846. $id_shop = ($id_shop !== false) ? (int)$id_shop : (int)Context::getContext()->shop->id;
  847. $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
  848.  
  849. $sql = 'SELECT c.`id_cms`, cl.`meta_title`, cl.`link_rewrite`
  850. FROM `'._DB_PREFIX_.'cms` c
  851. INNER JOIN `'._DB_PREFIX_.'cms_shop` cs
  852. ON (c.`id_cms` = cs.`id_cms`)
  853. INNER JOIN `'._DB_PREFIX_.'cms_lang` cl
  854. ON (c.`id_cms` = cl.`id_cms`)
  855. WHERE c.`id_cms_category` = '.(int)$id_cms_category.'
  856. AND cs.`id_shop` = '.(int)$id_shop.'
  857. AND cl.`id_lang` = '.(int)$id_lang.'
  858. AND c.`active` = 1
  859. ORDER BY `position`';
  860.  
  861. return Db::getInstance()->executeS($sql);
  862. }
  863.  
  864. public function hookActionObjectCategoryAddAfter($params)
  865. {
  866.  
  867. }
  868.  
  869. public function hookActionObjectCategoryUpdateAfter($params)
  870. {
  871.  
  872. }
  873.  
  874. public function hookActionObjectCategoryDeleteAfter($params)
  875. {
  876.  
  877. }
  878.  
  879. public function hookActionObjectCmsUpdateAfter($params)
  880. {
  881.  
  882. }
  883.  
  884. public function hookActionObjectCmsDeleteAfter($params)
  885. {
  886.  
  887. }
  888.  
  889. public function hookActionObjectCmsAddAfter($params)
  890. {
  891.  
  892. }
  893.  
  894. public function hookActionObjectSupplierUpdateAfter($params)
  895. {
  896.  
  897. }
  898.  
  899. public function hookActionObjectSupplierDeleteAfter($params)
  900. {
  901.  
  902. }
  903.  
  904. public function hookActionObjectSupplierAddAfter($params)
  905. {
  906.  
  907. }
  908.  
  909. public function hookActionObjectManufacturerUpdateAfter($params)
  910. {
  911.  
  912. }
  913.  
  914. public function hookActionObjectManufacturerDeleteAfter($params)
  915. {
  916.  
  917. }
  918.  
  919. public function hookActionObjectManufacturerAddAfter($params)
  920. {
  921.  
  922. }
  923.  
  924. public function hookActionObjectProductUpdateAfter($params)
  925. {
  926.  
  927. }
  928.  
  929. public function hookActionObjectProductDeleteAfter($params)
  930. {
  931.  
  932. }
  933.  
  934. public function hookActionObjectProductAddAfter($params)
  935. {
  936.  
  937. }
  938.  
  939. public function hookCategoryUpdate($params)
  940. {
  941.  
  942. }
  943.  
  944. private function clearMenuCache()
  945. {
  946. //$this->_clearCache('blocktopmenu.tpl');
  947. }
  948.  
  949. public function hookActionShopDataDuplication($params)
  950. {
  951. $linksmenutop = Db::getInstance()->executeS('
  952. SELECT *
  953. FROM '._DB_PREFIX_.'linksmenutop
  954. WHERE id_shop = '.(int)$params['old_id_shop']
  955. );
  956.  
  957. foreach($linksmenutop as $id => $link)
  958. {
  959. Db::getInstance()->execute('
  960. INSERT IGNORE INTO '._DB_PREFIX_.'linksmenutop (id_linksmenutop, id_shop, new_window)
  961. VALUES (null, '.(int)$params['new_id_shop'].', '.(int)$link['new_window'].')');
  962.  
  963. $linksmenutop[$id]['new_id_linksmenutop'] = Db::getInstance()->Insert_ID();
  964. }
  965.  
  966. foreach($linksmenutop as $id => $link)
  967. {
  968. $lang = Db::getInstance()->executeS('
  969. SELECT id_lang, '.(int)$params['new_id_shop'].', label, link
  970. FROM '._DB_PREFIX_.'linksmenutop_lang
  971. WHERE id_linksmenutop = '.(int)$link['id_linksmenutop'].' AND id_shop = '.(int)$params['old_id_shop']);
  972.  
  973. foreach($lang as $l)
  974. Db::getInstance()->execute('
  975. INSERT IGNORE INTO '._DB_PREFIX_.'linksmenutop_lang (id_linksmenutop, id_lang, id_shop, label, link)
  976. VALUES ('.(int)$link['new_id_linksmenutop'].', '.(int)$l['id_lang'].', '.(int)$params['new_id_shop'].', '.(int)$l['label'].', '.(int)$l['link'].' )');
  977. }
  978.  
  979.  
  980. }
  981. }
Advertisement
Add Comment
Please, Sign In to add comment