· 8 years ago · Mar 04, 2018, 06:26 AM
1<?php
2
3// Security
4if (!defined('_PS_VERSION_'))
5 exit;
6
7// Checking compatibility with older PrestaShop and fixing it
8if (!defined('_MYSQL_ENGINE_'))
9 define('_MYSQL_ENGINE_', 'MyISAM');
10
11require (dirname(__FILE__).'/megatoplinks.class.php');
12// Loading Models
13class Posmegamenu extends Module {
14
15 private $_html = '';
16 private $_postErrors = array();
17 private $_show_level = 1;
18 private $pattern = '/^([A-Z_]*)[0-9]+/';
19 private $_menuLink = '';
20 private $spacer_size = '5';
21 private $page_name = '';
22
23 public function __construct() {
24 $this->name = 'posmegamenu';
25 $this->tab = 'front_office_features';
26 $this->version = '1.0';
27 $this->author = 'posthemes';
28 $this->need_instance = 0;
29 $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
30 $this->_show_level = Configuration::get($this->name . '_show_depth');
31 parent::__construct();
32
33 $this->displayName = $this->l('Pos Megamenu Customer');
34 $this->description = $this->l('block config');
35
36 $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
37 $this->admin_tpl_path = _PS_MODULE_DIR_ . $this->name . '/views/templates/admin/';
38
39 }
40
41 public function install() {
42 Configuration::updateGlobalValue('MOD_BLOCKPOSMENU_ITEMS', 'CAT3,CAT4,CAT5');
43 Configuration::updateValue($this->name . '_show_homepage', 1);
44 Configuration::updateValue($this->name . '_menu_depth', 4);
45 Configuration::updateValue($this->name . '_merge_cate', 1);
46 Configuration::updateValue($this->name . '_show_depth', 4);
47 Configuration::updateValue($this->name . '_top_offset', 46);
48 Configuration::updateValue($this->name . '_effect', 0);
49 $this->installDb();
50 return parent::install() &&
51 $this->registerHook('displayHeader')
52 &&
53 $this->registerHook('megamenu');
54 }
55
56 public function uninstall() {
57 Configuration::deleteByName('POSMEGAMENU');
58 Configuration::updateGlobalValue('MOD_BLOCKPOSMENU_ITEMS', 'CAT1');
59 $this->uninstallDb();
60 // Uninstall Module
61 if (!parent::uninstall())
62 return false;
63 // !$this->unregisterHook('actionObjectExampleDataAddAfter')
64 return true;
65 }
66
67 public function getContent() {
68 $this->_html .= '<h2>' . $this->displayName . '</h2>';
69
70 $id_lang = (int)Context::getContext()->language->id;
71 $languages = $this->context->controller->getLanguages();
72 $default_language = (int)Configuration::get('PS_LANG_DEFAULT');
73 $labels = Tools::getValue('label') ? array_filter(Tools::getValue('label'), 'strlen') : array();
74 $links_label = Tools::getValue('link') ? array_filter(Tools::getValue('link'), 'strlen') : array();
75 $divLangName = 'link_label';
76 if (Tools::isSubmit('submitPosMegamenu')) {
77 //$this->_postValidation();
78
79 if (!sizeof($this->_postErrors))
80 $this->_postProcess();
81 else {
82 foreach ($this->_postErrors AS $err) {
83 $this->_html .= '<div class="alert error">' . $err . '</div>';
84 }
85 }
86 } else if (Tools::isSubmit('submitBlockposmenu')) {
87
88 if (Configuration::updateValue('MOD_BLOCKPOSMENU_ITEMS', Tools::getValue('items')))
89 $this->_html .= $this->displayConfirmation($this->l('The settings have been updated.'));
90
91 }else if (Tools::isSubmit('submitBlocktopmegaLinks')){
92
93 if ((!count($links_label)) && (!count($labels)));
94 else if (!count($links_label))
95 $this->_html .= $this->displayError($this->l('Please complete the "link" field.'));
96 else if (!count($labels))
97 $this->_html .= $this->displayError($this->l('Please add a label'));
98 else if (!isset($labels[$default_language]))
99 $this->_html .= $this->displayError($this->l('Please add a label for your default language.'));
100 else
101 {
102 MegaTopLinks::add(Tools::getValue('link'), Tools::getValue('label'), Tools::getValue('new_window', 0), (int)Shop::getContextShopID());
103 $this->_html .= $this->displayConfirmation($this->l('The link has been added.'));
104 }
105 }
106 else if (Tools::isSubmit('submitBlocktopmegaRemove'))
107 {
108 $id_linksmegatop = Tools::getValue('id_linksmegatop', 0);
109 MegaTopLinks::remove($id_linksmegatop, (int)Shop::getContextShopID());
110 Configuration::updateValue('MOD_BLOCKPOSMENU_ITEMS', str_replace(array('LNK'.$id_linksmegatop.',', 'LNK'.$id_linksmegatop), '', Configuration::get('MOD_BLOCKPOSMENU_ITEMS')));
111 $this->_html .= $this->displayConfirmation($this->l('The link has been removed'));
112 $update_cache = true;
113 }
114
115 $this->_displayForm();
116
117 return $this->_html;
118 }
119
120 public function getSelectOptionsHtml($options = NULL, $name = NULL, $selected = NULL) {
121 $html = "";
122 $html .='<select name =' . $name . ' style="width:130px">';
123 if (count($options) > 0) {
124 foreach ($options as $key => $val) {
125 if (trim($key) == trim($selected)) {
126 $html .='<option value=' . $key . ' selected="selected">' . $val . '</option>';
127 } else {
128 $html .='<option value=' . $key . '>' . $val . '</option>';
129 }
130 }
131 }
132 $html .= '</select>';
133 return $html;
134 }
135
136 private function _postProcess() {
137
138 Configuration::updateValue($this->name . '_show_homepage', Tools::getValue('show_homepage'));
139 Configuration::updateValue($this->name . '_menu_depth', Tools::getValue('menu_depth'));
140 if(Tools::getValue('list_cate')) {
141 Configuration::updateValue($this->name . '_list_cate', implode(',', Tools::getValue('list_cate')));
142 }
143 Configuration::updateValue($this->name . '_merge_cate', Tools::getValue('merge_cate'));
144 Configuration::updateValue($this->name . '_show_depth', Tools::getValue('show_depth'));
145 Configuration::updateValue($this->name . '_top_offset', Tools::getValue('top_offset'));
146 Configuration::updateValue($this->name . '_effect', Tools::getValue('effect'));
147
148 $this->_html .= '<div class="conf confirm">' . $this->l('Settings updated') . '</div>';
149 }
150
151 private function _displayForm() {
152 $id_lang = (int)Context::getContext()->language->id;
153 $languages = $this->context->controller->getLanguages();
154 $default_language = (int)Configuration::get('PS_LANG_DEFAULT');
155 $spacer = str_repeat(' ', $this->spacer_size);
156 $divLangName = 'link_label';
157
158 if (Tools::isSubmit('submitBlocktopmegaEdit'))
159 {
160 $id_linksmegatop = (int)Tools::getValue('id_linksmegatop', 0);
161
162 $id_shop = (int)Shop::getContextShopID();
163
164 if (!Tools::isSubmit('link'))
165 {
166 $tmp = MegaTopLinks::getLinkLang($id_linksmegatop, $id_shop);
167 $links_label_edit = $tmp['link'];
168 $labels_edit = $tmp['label'];
169 $new_window_edit = $tmp['new_window'];
170 }
171 else
172 {
173 MegaTopLinks::update(Tools::getValue('link'), Tools::getValue('label'), Tools::getValue('new_window', 0), (int)$id_shop, (int)$id_linksmegatop, (int)$id_linksmegatop);
174 $this->_html .= $this->displayConfirmation($this->l('The link has been edited'));
175 }
176
177 }
178 $this->_html .= '
179 <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
180 <fieldset>
181 <legend><img src="../img/admin/cog.gif" alt="" class="middle" />' . $this->l('Settings') . '</legend>
182 <label>' . $this->l('Show Homepage: ') . '</label>
183 <div class="margin-form">';
184 $this->_html .= $this->getSelectOptionsHtml(array(0 => 'No', 1 => 'Yes'), 'show_homepage', (Tools::getValue('show_homepage') ? Tools::getValue('show_homepage') : Configuration::get($this->name . '_show_homepage')));
185
186 $this->_html .='
187 </div>
188 <label>' . $this->l('Show number columns: ') . '</label>
189 <div class="margin-form">
190 <input type = "text" name="menu_depth" value =' . (Tools::getValue('menu_depth') ? Tools::getValue('menu_depth') : Configuration::get($this->name . '_menu_depth')) . ' ></input>
191 </div>';
192 $this->_html .= '<label>' . $this->l('Show Levels: ') . '</label>
193 <div class="margin-form">
194 <input type = "text" name="show_depth" value =' . (Tools::getValue('show_depth') ? Tools::getValue('show_depth') : Configuration::get($this->name . '_show_depth')) . ' ></input>
195 </div>';
196 $this->_html .= '<label>' . $this->l('Show Link/Label Category: ') . '</label>';
197 $this->_html .= '<div class="margin-form">';
198 $this->_html .= '<select multiple="multiple" name ="list_cate[]" style="width: 200px; height: 160px;">';
199 // BEGIN Categories
200 $id_lang = (int) Context::getContext()->language->id;
201 $this->getCategoryOption(1, (int) $id_lang, (int) Shop::getContextShopID());
202 $this->_html .= '</select>
203 </div>
204 <label>' . $this->l('Merge small subcategories: ') . '</label>
205 <div class="margin-form">';
206 $this->_html .= $this->getSelectOptionsHtml(array(0 => 'No', 1 => 'Yes'), 'merge_cate', (Tools::getValue('merge_cate') ? Tools::getValue('merge_cate') : Configuration::get($this->name . '_merge_cate')));
207
208 $this->_html .='
209 </div>';
210 $this->_html .= '<label>' . $this->l('Top offset: ') . '</label>
211 <div class="margin-form">
212 <input type = "text" name="top_offset" value =' . (Tools::getValue('top_offset') ? Tools::getValue('top_offset') : Configuration::get($this->name . '_top_offset')) . ' ></input>
213 </div>';
214 $this->_html .='<label>' . $this->l('Effect Popup: ') . '</label>
215 <div class="margin-form">';
216 $this->_html .= $this->getSelectOptionsHtml(array(0 => 'SlideDown', 1 => 'FadeIn', 2 => 'Show'), 'effect', (Tools::getValue('effect') ? Tools::getValue('effect') : Configuration::get($this->name . '_effect')));
217
218 $this->_html .='
219 </div>';
220 $this->_html .='<div class ="submit">
221 <input type="submit" name="submitPosMegamenu" value="' . $this->l('Update') . '" class="button" />
222 </div>
223 </fieldset>
224 </form>';
225
226
227 $this->_html .= '
228 <fieldset>
229 <div class="multishop_info">
230 '.$this->l('The modifications will be applied to').' '.(Shop::getContext() == Shop::CONTEXT_SHOP ? $this->l('shop').' '.$this->context->shop->name : $this->l('all shops')).'.
231 </div>
232 <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
233 <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" id="form">
234 <div style="display: none">
235 <label>'.$this->l('Items').'</label>
236 <div class="margin-form">
237 <input type="text" name="items" id="itemsInput" value="'.Tools::safeOutput(Configuration::get('MOD_BLOCKPOSMENU_ITEMS')).'" size="70" />
238 </div>
239 </div>
240
241 <div class="clear"> </div>
242 <table style="margin-left: 130px;">
243 <tbody>
244 <tr>
245 <td style="padding-left: 20px;">
246 <select multiple="multiple" id="availableItems" style="width: 300px; height: 160px;">';
247
248 // BEGIN CMS
249 $this->_html .= '<optgroup label="'.$this->l('CMS').'">';
250 $this->getCMSOptions(0, 1, $id_lang);
251 $this->_html .= '</optgroup>';
252
253 // BEGIN SUPPLIER
254 $this->_html .= '<optgroup label="'.$this->l('Supplier').'">';
255 // Option to show all Suppliers
256 $this->_html .= '<option value="ALLSUP0">'.$this->l('All suppliers').'</option>';
257 $suppliers = Supplier::getSuppliers(false, $id_lang);
258 foreach ($suppliers as $supplier)
259 $this->_html .= '<option value="SUP'.$supplier['id_supplier'].'">'.$spacer.$supplier['name'].'</option>';
260 $this->_html .= '</optgroup>';
261
262 // BEGIN Manufacturer
263 $this->_html .= '<optgroup label="'.$this->l('Manufacturer').'">';
264 // Option to show all Manufacturers
265 $this->_html .= '<option value="ALLMAN0">'.$this->l('All manufacturers').'</option>';
266 $manufacturers = Manufacturer::getManufacturers(false, $id_lang);
267 foreach ($manufacturers as $manufacturer)
268 $this->_html .= '<option value="MAN'.$manufacturer['id_manufacturer'].'">'.$spacer.$manufacturer['name'].'</option>';
269 $this->_html .= '</optgroup>';
270
271 // BEGIN Categories
272 $this->_html .= '<optgroup label="'.$this->l('Categories').'">';
273 $this->getCategoryOptionLink(1, (int)$id_lang, (int)Shop::getContextShopID());
274 $this->_html .= '</optgroup>';
275
276 // BEGIN Shops
277 if (Shop::isFeatureActive())
278 {
279 $this->_html .= '<optgroup label="'.$this->l('Shops').'">';
280 $shops = Shop::getShopsCollection();
281 foreach ($shops as $shop)
282 {
283 if (!$shop->setUrl() && !$shop->getBaseURL())
284 continue;
285 $this->_html .= '<option value="SHOP'.(int)$shop->id.'">'.$spacer.$shop->name.'</option>';
286 }
287 $this->_html .= '</optgroup>';
288 }
289
290 // BEGIN Products
291 $this->_html .= '<optgroup label="'.$this->l('Products').'">';
292 $this->_html .= '<option value="PRODUCT" style="font-style:italic">'.$spacer.$this->l('Choose product ID').'</option>';
293 $this->_html .= '</optgroup>';
294
295 // BEGIN Menu Top Links
296 $this->_html .= '<optgroup label="'.$this->l('Mega menu Top Links').'">';
297 $links = MegaTopLinks::gets($id_lang, null, (int)Shop::getContextShopID());
298 foreach ($links as $link)
299 {
300 if ($link['label'] == '')
301 {
302 $link = MegaTopLinks::get($link['id_linksmegatop'], $default_language, (int)Shop::getContextShopID());
303 $this->_html .= '<option value="LNK'.(int)$link[0]['id_linksmegatop'].'">'.$spacer.$link[0]['label'].'</option>';
304 }
305 else
306 $this->_html .= '<option value="LNK'.(int)$link['id_linksmegatop'].'">'.$spacer.$link['label'].'</option>';
307 }
308 $this->_html .= '</optgroup>';
309
310
311 $this->_html .= '</select><br />
312 <br />
313 <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').' >></a>
314 </td>
315 <td>
316 <select multiple="multiple" id="items" style="width: 300px; height: 160px;">';
317 $this->makeMenuOption();
318 $this->_html .= '</select><br/>
319 <br/>
320 <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);"><< '.$this->l('Remove').'</a>
321 </td>
322 <td style="vertical-align:top;padding:5px 15px;">
323 <h4 style="margin-top:5px;">'.$this->l('Change position').'</h4>
324 <a href="#" id="menuOrderUp" class="button" style="font-size:20px;display:block;">↑</a><br/>
325 <a href="#" id="menuOrderDown" class="button" style="font-size:20px;display:block;">↓</a><br/>
326 </td>
327 </tr>
328 </tbody>
329 </table>
330 <div class="clear"> </div>
331 <script type="text/javascript">
332 function add()
333 {
334 $("#availableItems option:selected").each(function(i){
335 var val = $(this).val();
336 var text = $(this).text();
337 text = text.replace(/(^\s*)|(\s*$)/gi,"");
338 if (val == "PRODUCT")
339 {
340 val = prompt("'.$this->l('Set ID product').'");
341 if (val == null || val == "" || isNaN(val))
342 return;
343 text = "'.$this->l('Product ID').' "+val;
344 val = "PRD"+val;
345 }
346 $("#items").append("<option value=\""+val+"\">"+text+"</option>");
347 });
348 serialize();
349 return false;
350 }
351
352 function remove()
353 {
354 $("#items option:selected").each(function(i){
355 $(this).remove();
356 });
357 serialize();
358 return false;
359 }
360
361 function serialize()
362 {
363 var options = "";
364 $("#items option").each(function(i){
365 options += $(this).val() + ",";
366 });
367 $("#itemsInput").val(options.substr(0, options.length - 1));
368 }
369
370 function move(up)
371 {
372 var tomove = $("#items option:selected");
373 if (tomove.length >1)
374 {
375 alert(\''.Tools::htmlentitiesUTF8($this->l('Please select just one item')).'\');
376 return false;
377 }
378 if (up)
379 tomove.prev().insertAfter(tomove);
380 else
381 tomove.next().insertBefore(tomove);
382 serialize();
383 return false;
384 }
385
386 $(document).ready(function(){
387 $("#addItem").click(add);
388 $("#availableItems").dblclick(add);
389 $("#removeItem").click(remove);
390 $("#items").dblclick(remove);
391 $("#menuOrderUp").click(function(e){
392 e.preventDefault();
393 move(true);
394 });
395 $("#menuOrderDown").click(function(e){
396 e.preventDefault();
397 move();
398 });
399 });
400 </script>
401 <p class="center">
402 <input type="submit" name="submitBlockposmenu" value="'.$this->l('Save ').'" class="button" />
403 </p>
404 </form>
405 </fieldset><br />';
406
407 $this->_html .= '
408 <fieldset>
409 <legend><img src="../img/admin/add.gif" alt="" title="" />'.$this->l('Add Menu Top Link').'</legend>
410 <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" id="form">
411
412 ';
413 foreach ($languages as $language)
414 {
415 $this->_html .= '
416 <div id="link_label_'.(int)$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang ? 'block' : 'none').';">
417 <label>'.$this->l('Label').'</label>
418 <div class="margin-form">
419 <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']] : '').'" />
420 </div>
421 ';
422
423 $this->_html .= '
424 <label>'.$this->l('Link').'</label>
425 <div class="margin-form">
426 <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" />
427 </div>
428 </div>';
429 }
430
431 $this->_html .= '<label>'.$this->l('Language').'</label>
432 <div class="margin-form">'.$this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true).'</div><p style="clear: both;"> </p>';
433
434 $this->_html .= '<label style="clear: both;">'.$this->l('New Window').'</label>
435 <div class="margin-form">
436 <input style="clear: both;" type="checkbox" name="new_window" value="1" '.(isset($new_window_edit) && $new_window_edit ? 'checked' : '').'/>
437 </div>
438 <div class="margin-form">';
439
440 if (Tools::isSubmit('id_linksmegatop'))
441 $this->_html .= '<input type="hidden" name="id_linksmegatop" value="'.(int)Tools::getValue('id_linksmegatop').'" />';
442
443 if (Tools::isSubmit('submitBlocktopmegaEdit'))
444 $this->_html .= '<input type="submit" name="submitBlocktopmegaEdit" value="'.$this->l('Edit').'" class="button" />';
445
446 $this->_html .= '
447 <input type="submit" name="submitBlocktopmegaLinks" value="'.$this->l('Add ').'" class="button" />
448 </div>
449
450 </form>
451 </fieldset><br />';
452
453
454 $links = MegaTopLinks::gets((int)$id_lang, null, (int)Shop::getContextShopID());
455
456 if (!count($links))
457 return $this->_html;
458
459 $this->_html .= '
460 <fieldset>
461 <legend><img src="../img/admin/details.gif" alt="" title="" />'.$this->l('List Menu Top Link').'</legend>
462 <table style="width:100%;">
463 <thead>
464 <tr style="text-align: left;">
465 <th>'.$this->l('Id Link').'</th>
466 <th>'.$this->l('Label').'</th>
467 <th>'.$this->l('Link').'</th>
468 <th>'.$this->l('New Window').'</th>
469 <th>'.$this->l('Action').'</th>
470 </tr>
471 </thead>
472 <tbody>';
473 foreach ($links as $link)
474 {
475 $this->_html .= '
476 <tr>
477 <td>'.(int)$link['id_linksmegatop'].'</td>
478 <td>'.Tools::safeOutput($link['label']).'</td>
479 <td><a href="'.Tools::safeOutput($link['link']).'"'.(($link['new_window']) ? ' target="_blank"' : '').'>'.Tools::safeOutput($link['link']).'</a></td>
480 <td>'.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).'</td>
481 <td>
482 <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
483 <input type="hidden" name="id_linksmegatop" value="'.(int)$link['id_linksmegatop'].'" />
484 <input type="submit" name="submitBlocktopmegaEdit" value="'.$this->l('Edit').'" class="button" />
485 <input type="submit" name="submitBlocktopmegaRemove" value="'.$this->l('Remove').'" class="button" />
486 </form>
487 </td>
488 </tr>';
489 }
490 $this->_html .= '</tbody>
491 </table>
492 </fieldset>';
493 return $this->_html;
494 }
495
496 private function getCategoryOption($id_category = 1, $id_lang = false, $id_shop = false, $recursive = true) {
497 $cateCurrent = Configuration::get($this->name . '_list_cate');
498 $cateCurrent = explode(',', $cateCurrent);
499 $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
500 $category = new Category((int)$id_category, (int)$id_lang, (int)$id_shop);
501
502 if (is_null($category->id))
503 return;
504
505 if ($recursive)
506 {
507 $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);
508 $spacer = str_repeat(' ', $this->spacer_size * (int)$category->level_depth);
509 }
510
511 $shop = (object) Shop::getShop((int)$category->getShopID());
512 if (in_array('CAT'.(int)$category->id, $cateCurrent)) {
513 $this->_html .= '<option value="CAT'.(int)$category->id.'" selected ="selected" >'.(isset($spacer) ? $spacer : '').$category->name.' ('.$shop->name.')</option>';
514 } else {
515 $this->_html .= '<option value="CAT'.(int)$category->id.'">'.(isset($spacer) ? $spacer : '').$category->name.' ('.$shop->name.')</option>';
516 }
517
518 if (isset($children) && count($children))
519 foreach ($children as $child)
520 $this->getCategoryOption((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);
521 }
522
523 private function getCategoryOptionLink($id_category = 1, $id_lang = false, $id_shop = false, $recursive = true)
524 {
525 $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
526 $category = new Category((int)$id_category, (int)$id_lang, (int)$id_shop);
527
528 if (is_null($category->id))
529 return;
530
531 if ($recursive)
532 {
533 $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);
534 $spacer = str_repeat(' ', $this->spacer_size * (int)$category->level_depth);
535 }
536
537 $shop = (object) Shop::getShop((int)$category->getShopID());
538 $this->_html .= '<option value="CAT'.(int)$category->id.'">'.(isset($spacer) ? $spacer : '').$category->name.' ('.$shop->name.')</option>';
539
540 if (isset($children) && count($children))
541 foreach ($children as $child)
542 $this->getCategoryOptionLink((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);
543 }
544
545 public function getStaticblockLists($id_shop = NULL, $identify= NULL) {
546 if (!Combination::isFeatureActive())
547 return array();
548 $id_lang = (int)$this->context->language->id;
549 return Db::getInstance()->executeS('
550 SELECT * FROM ' . _DB_PREFIX_ . 'pos_staticblock AS psb
551 LEFT JOIN ' . _DB_PREFIX_ . 'pos_staticblock_lang AS psl ON psb.id_posstaticblock = psl.id_posstaticblock
552 LEFT JOIN ' . _DB_PREFIX_ . 'pos_staticblock_shop AS pss ON psb.id_posstaticblock = pss.id_posstaticblock
553 WHERE id_shop =' . $id_shop . '
554 AND id_lang =' . $id_lang .'
555 AND `identify` = "' . $identify . '"
556 ');
557 }
558
559 public function getStaticblockCustommerLists($id_shop = NULL) {
560 if (!Combination::isFeatureActive())
561 return array();
562 $id_lang = (int)$this->context->language->id;
563 return Db::getInstance()->executeS('
564 SELECT * FROM ' . _DB_PREFIX_ . 'pos_staticblock AS psb
565 LEFT JOIN ' . _DB_PREFIX_ . 'pos_staticblock_lang AS psl ON psb.id_posstaticblock = psl.id_posstaticblock
566 LEFT JOIN ' . _DB_PREFIX_ . 'pos_staticblock_shop AS pss ON psb.id_posstaticblock = pss.id_posstaticblock
567 where id_shop =' . $id_shop . '
568 AND id_lang =' . $id_lang .'
569 AND `identify` like "pt_item_menu' . '%"
570 ');
571 }
572
573 public function getStaticBlockContent($blockId = NULL, $task = NULL) {
574 $id_shop = (int) Context::getContext()->shop->id;
575 $staticBlock = $this->getStaticblockLists($id_shop, $blockId);
576 //echo "<pre>"; print_r($staticBlock);
577 $html = "";
578 if (count($staticBlock) > 0) {
579 $description= $staticBlock[0]['description'];
580 $description = str_replace('/pos_timeplus/',__PS_BASE_URI__,$description);
581 $html .= $description;
582 }
583 if ($task == 'item') {
584 $staticBlock = $this->getStaticblockCustommerLists($id_shop);
585 return $staticBlock;
586 } else {
587 return $html;
588 }
589 }
590
591 public function getCurrentCategoriesId($lang_id = NULL) {
592 if (isset($_GET['id_category'])) {
593 $lastCateId = $_GET['id_category'];
594 } else {
595 $lastCateId = 0;
596 }
597
598 $lastCate = new Category((int) $lastCateId);
599 //echo $lastCate->name[1]; echo '--------';
600 $parentCate = $lastCate->getParentsCategories($lang_id);
601 $arrayCateCurrent = array();
602 foreach ($parentCate as $pcate) {
603 $arrayCateCurrent[] = $pcate['id_category'];
604 }
605 return $arrayCateCurrent;
606 }
607
608 public function haveCateChildren($cate_id = NULL, $lang_id = NULL) {
609 $cate = new Category();
610 $childCates = $cate->getChildren($cate_id, $lang_id);
611 if (count($childCates) > 0)
612 return true;
613 return false;
614 }
615
616 public function drawCustomMenuItem($category, $level = 0, $last = false, $item, $lang_id) {
617 if ($level > $this->_show_level)
618 continue;
619 $cateCurrent = $this->getCurrentCategoriesId($lang_id);
620 $categoryObject = new Category();
621 $html = array();
622 $blockHtml = '';
623 $id_shop = (int) Context::getContext()->shop->id;
624 $id = $category;
625 $blockId = sprintf('pt_menu_idcat_%d', $id);
626 $staticBlock = $this->getStaticBlockContent($blockId);
627 $blockIdRight = sprintf('pt_menu_idcat_%d_right', $id);
628 $staticBlockRight = $this->getStaticBlockContent($blockIdRight);
629 // --- Static Block ---
630 $blockHtml = $staticBlock;
631 /* check block right */
632 $blockHtmlRight = $staticBlockRight;
633
634 if ($blockHtmlRight)
635 $blockHtml = $blockHtmlRight;
636 // --- Sub Categories ---
637 $activeChildren = $categoryObject->getChildren($category, $lang_id);
638 $activeChildren = $this->getCategoryByLevelMax($activeChildren);
639 // --- class for active category ---
640 $active = '';
641 if (isset($cateCurrent[0]) && in_array($category, array($cateCurrent[0])))
642 $active = ' act';
643 // --- Popup functions for show ---
644 $drawPopup = ($blockHtml || count($activeChildren));
645 if ($drawPopup) {
646 $html[] = '<div id="pt_menu' . $id . '" class="pt_menu' . $active . ' nav-' . $item . '">';
647 } else {
648 $html[] = '<div id="pt_menu' . $id . '" class="pt_menu' . $active . ' nav-' . $item . ' pt_menu_no_child">';
649 }
650 //echo $category;
651 //$cate = new Category((int) $category);
652 $id_lang = (int)Context::getContext()->language->id;
653 $cate = new Category((int)$category,$id_lang,$id_shop);
654 //$link = $categoryObject->getLinkRewrite($category, $lang_id);
655 $parameters = "";
656 $link = Context::getContext()->link->getCategoryLink((int) $category, null, null, ltrim($parameters, '/'));
657 // --- Top Menu Item ---
658 $html[] = '<div class="parentMenu">';
659 $html[] = '<a href="' . $link . '">';
660 $name = strip_tags($cate->name);
661 $name = str_replace(' ', ' ', $name);
662 $name = $this->l($name);
663 $html[] = '<span>' . $name . '</span>';
664 if ($drawPopup) {$html[] = '<i class="icon-caret-down"></i>';}
665 $html[] = '</a>';
666 $html[] = '</div>';
667
668 // --- Add Popup block (hidden) ---
669 if ($drawPopup) {
670 if ($this->_show_level > 2) {
671 // --- Popup function for hide ---
672 $html[] = '<div id="popup' . $id . '" class="popup" style="display: none; width: 1228px;">';
673 // --- draw Sub Categories ---
674 if (count($activeChildren) || $blockHtml) {
675 $html[] = '<div class="block1" id="block1' . $id . '">';
676 $html[] = $this->drawColumns($activeChildren, $id, $lang_id);
677 if ($blockHtml && $blockHtmlRight) {
678 $html[] = '<div class="column blockright">';
679 $html[] = $blockHtml;
680 $html[] = '</div>';
681 }
682 $html[] = '<div class="clearBoth"></div>';
683 $html[] = '</div>';
684 }
685 // --- draw Custom User Block ---
686 if ($blockHtml && !$blockHtmlRight) {
687 $html[] = '<div class="block2" id="block2' . $id . '">';
688 $html[] = $blockHtml;
689 $html[] = '</div>';
690 }
691 $html[] = '</div>';
692 }
693 }
694
695 $html[] = '</div>';
696 $html = implode("\n", $html);
697 return $html;
698 }
699
700 public function getCategoryByLevelMax($cates = NULL) {
701 if (count($cates) < 1)
702 return array();
703 $cateArray = array();
704 foreach ($cates as $key => $cate) {
705 $cate_id = $cate['id_category'];
706 $cateObject = new Category((int) $cate_id);
707 $cate_level = $cateObject->level_depth;
708 if ($cate_level <= $this->_show_level) {
709 $cateArray[$key] = $cate;
710 }
711 }
712
713 if ($cateArray)
714 return $cateArray;
715 return array();
716 }
717
718 public function drawMenuItem($children, $level = 1, $columChunk = 0, $lang_id = 1) {
719 $html = '<div class="itemMenu level' . $level . '">';
720 $keyCurrent = NULL;
721 if (isset($_GET['id_category'])) {
722 $keyCurrent = $_GET['id_category'];
723 }
724 $countChildren = 0;
725 $ClassNoChildren = '';
726 $category = new Category();
727 foreach ($children as $child) {
728 $activeChildCat = $category->getChildren($child['id_category'], $lang_id);
729 $activeChildCat = $this->getCategoryByLevelMax($activeChildCat);
730 if ($activeChildCat) {
731 $countChildren++;
732 }
733 }
734 if ($countChildren == 0 && $columChunk == 1) {
735 $ClassNoChildren = ' nochild';
736 }
737 $catsid = '';
738 $catsid = Configuration::get($this->name . '_list_cate');
739 $arr_catsid = array();
740 if ($catsid) {
741 if (stristr($catsid, ',') === FALSE) {
742 $arr_catsid = array(0 => $catsid);
743 } else {
744 $arr_catsid = explode(",", $catsid);
745 }
746 }
747 $id_shop = (int) Context::getContext()->shop->id;
748 foreach ($children as $child) {
749 $info = new Category((int) $child['id_category'], $lang_id,$id_shop);
750 $level = $info->level_depth;
751 $active = '';
752 $currentCate = $this->getCurrentCategoriesId($lang_id);
753 $cate_id = (int) $child['id_category'];
754 if (in_array($cate_id, $currentCate)) {
755 if ($this->haveCateChildren($cate_id, $lang_id)) {
756 $active = ' actParent';
757 } else {
758 $active = ' act';
759 }
760 }
761 // --- format category name ---
762 $name = strip_tags($child['name']);
763 $name = str_replace(' ', ' ', $name);
764
765 if (count($child) > 0) {
766 $parameters = null;
767 $link = Context::getContext()->link->getCategoryLink((int) $child['id_category'], null, null, ltrim($parameters, '/'));
768 if (in_array('CAT'.$child['id_category'], $arr_catsid)) {
769 $html.= '<h4 class="itemMenuName level' . $level . $active . $ClassNoChildren . '"><span>' . $name . '</span></h4>';
770 } else {
771 $html.= '<a class="itemMenuName level' . $level . $active . $ClassNoChildren . '" href="' . $link . '"><span>' . $name . '</span></a>';
772 }
773
774 $activeChildren = $category->getChildren($child['id_category'], $lang_id);
775 $activeChildren = $this->getCategoryByLevelMax($activeChildren);
776 if (count($activeChildren) > 0) {
777 $html.= '<div class="itemSubMenu level' . $level . '">';
778 //$html.= $this->drawMenuItem($activeChildren, $level + 1);
779 $html.= $this->drawMenuItem($activeChildren, $level + 1,$columChunk, $lang_id);
780 $html.= '</div>';
781 }
782 }
783 }
784 $html.= '</div>';
785 return $html;
786 }
787
788 public function drawColumns($children, $id, $lang_id) {
789 $html = '';
790 // --- explode by columns ---
791 $columns = Configuration::get($this->name . '_menu_depth');
792 if ($columns < 1)
793 $columns = 1;
794 $chunks = $this->seperateColumns($children, $columns, $lang_id);
795 $columChunk = count($chunks);
796 // --- draw columns ---
797 $classSpecial = '';
798 $keyLast = 0;
799 foreach ($chunks as $key => $value) {
800 if (count($value))
801 $keyLast++;
802 }
803 $blockHtml = '';
804 $id_shop = (int) Context::getContext()->shop->id;
805 $blockId = sprintf('pt_menu_idcat_%d', $id);
806 $staticBlock = $this->getStaticBlockContent($blockId);
807 $blockIdRight = sprintf('pt_menu_idcat_%d_right', $id);
808 $staticBlockRight = $this->getStaticBlockContent($blockIdRight);
809 // --- Static Block ---
810 $blockHtml = $staticBlock;
811 /* check block right */
812 $blockHtmlRight = $staticBlockRight;
813
814 foreach ($chunks as $key => $value) {
815 if (!count($value))
816 continue;
817 if ($key == $keyLast - 1) {
818 $classSpecial = ($blockHtmlRight && $blockHtml) ? '' : ' last';
819 } elseif ($key == 0) {
820 $classSpecial = ' first';
821 } else {
822 $classSpecial = '';
823 }
824 $html.= '<div class="column' . $classSpecial . ' col' . ($key + 1) . '">';
825 $html.= $this->drawMenuItem($value, 1, $columChunk, $lang_id);
826
827 $html.= '</div>';
828 }
829 return $html;
830 }
831
832 public function drawCustomMenuBlock($blockId, $bc) {
833 $html = array();
834 $id = '_' . $blockId;
835
836
837 $blockHtml = str_replace('/pos_timeplus/',__PS_BASE_URI__,$bc['description']);
838 $drawPopup = $blockHtml;
839 if ($drawPopup) {
840 $html[] = '<div id="pt_menu' . $id . '" class="pt_menu">';
841 } else {
842 $html[] = '<div id="pt_menu' . $id . '" class="pt_menu">';
843 }
844 // --- Top Menu Item ---
845 $html[] = '<div class="parentMenu">';
846// $html[] = '<a href="#">';
847 $name = $this->l($bc['title']);
848 $name = str_replace(' ', ' ', $name);
849 $html[] = '<span class="block-title">' . $name . '</span>';
850// $html[] = '</a>';
851 $html[] = '</div>';
852 // --- Add Popup block (hidden) ---
853 if ($drawPopup) {
854 // --- Popup function for hide ---
855 $html[] = '<div id="popup' . $id . '" class="popup cmsblock" style="display: none; width: 904px;">';
856 if ($blockHtml) {
857 $html[] = '<div class="block2" id="block2' . $id . '">';
858 $html[] = $blockHtml;
859 $html[] = '</div>';
860 }
861 $html[] = '</div>';
862 }
863 $html[] = '</div>';
864 $html = implode("\n", $html);
865 return $html;
866 }
867
868 private function seperateColumns($parentCates, $num, $lang_id) {
869 $category = new Category();
870 $countChildren = 0;
871 foreach ($parentCates as $cat => $childCat) {
872 $activeChildCat = $category->getChildren($childCat['id_category'], $lang_id);
873 $activeChildCat = $this->getCategoryByLevelMax($activeChildCat);
874 if ($activeChildCat) {
875 $countChildren++;
876 }
877 }
878 if ($countChildren == 0) {
879 $num = 1;
880 }
881
882
883 $count = count($parentCates);
884 if ($count)
885 $parentCates = array_chunk($parentCates, ceil($count / $num));
886
887 $parentCates = array_pad($parentCates, $num, array());
888 $is_merge = Configuration::get($this->name . '_merge_cate');
889 if ($is_merge && count($parentCates)) {
890 // --- combine consistently numerically small column ---
891 // --- 1. calc length of each column ---
892 $max = 0;
893 $columnsLength = array();
894 foreach ($parentCates as $key => $child) {
895 $count = 0;
896 $this->_countChild($child, 1, $count, $lang_id);
897
898 if ($max < $count)
899 $max = $count;
900 $columnsLength[$key] = $count;
901 }
902 // --- 2. merge small columns with next ---
903 $xColumns = array();
904 $column = array();
905 $cnt = 0;
906 $xColumnsLength = array();
907 $k = 0;
908
909 foreach ($columnsLength as $key => $count) {
910 $cnt+= $count;
911 if ($cnt > $max && count($column)) {
912 $xColumns[$k] = $column;
913 $xColumnsLength[$k] = $cnt - $count;
914 $k++;
915 $column = array();
916 $cnt = $count;
917 }
918 $column = array_merge($column, $parentCates[$key]);
919 }
920 $xColumns[$k] = $column;
921 $xColumnsLength[$k] = $cnt - $count;
922 // --- 3. integrate columns of one element ---
923 $parentCates = $xColumns;
924 $xColumns = array();
925 $nextKey = -1;
926 if ($max > 1 && count($parentCates) > 1) {
927 foreach ($parentCates as $key => $column) {
928 if ($key == $nextKey)
929 continue;
930 if ($xColumnsLength[$key] == 1) {
931 // --- merge with next column ---
932 $nextKey = $key + 1;
933 if (isset($parentCates[$nextKey]) && count($parentCates[$nextKey])) {
934 $xColumns[] = array_merge($column, $parentCates[$nextKey]);
935 continue;
936 }
937 }
938 $xColumns[] = $column;
939 }
940 $parentCates = $xColumns;
941 }
942 }
943
944 return $parentCates;
945 }
946
947 private function _countChild($children, $level, &$count, $lang_id) {
948 $category = new Category();
949 foreach ($children as $child) {
950
951 $count++;
952 $activeChildren = $category->getChildren($child['id_category'], $lang_id);
953 $activeChildren = $this->getCategoryByLevelMax($activeChildren);
954 if (count($activeChildren) > 0)
955 $this->_countChild($activeChildren, $level + 1, $count, $lang_id);
956 }
957 }
958
959 public function hookDisplayHeader() {
960
961 $this->context->controller->addCSS($this->_path . 'css/custommenu.css');
962 $this->context->controller->addJS($this->_path . 'js/custommenu.js');
963 $this->context->controller->addJS($this->_path . 'js/mobile_menu.js');
964 }
965
966 //mobile megamenu
967 public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0) {
968 if (is_null($id_category))
969 $id_category = $this->context->shop->getCategory();
970
971 $children = array();
972 if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth))
973 foreach ($resultParents[$id_category] as $subcat)
974 $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
975 if (!isset($resultIds[$id_category]))
976 return false;
977 $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
978 'name' => $resultIds[$id_category]['name'], 'desc' => $resultIds[$id_category]['description'],
979 'children' => $children);
980 return $return;
981 }
982
983 public function getblockCategTree() {
984
985 // Get all groups for this customer and concatenate them as a string: "1,2,3..."
986 $groups = implode(', ', Customer::getGroupsStatic((int) $this->context->customer->id));
987 $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
988 if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
989 SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
990 FROM `' . _DB_PREFIX_ . 'category` c
991 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') . ')
992 INNER JOIN `' . _DB_PREFIX_ . 'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = ' . (int) $this->context->shop->id . ')
993 WHERE (c.`active` = 1 OR c.`id_category` = ' . (int) Configuration::get('PS_HOME_CATEGORY') . ')
994 AND c.`id_category` != ' . (int) Configuration::get('PS_ROOT_CATEGORY') . '
995 ' . ((int) $maxdepth != 0 ? ' AND `level_depth` <= ' . (int) $maxdepth : '') . '
996 AND c.id_category IN (SELECT id_category FROM `' . _DB_PREFIX_ . 'category_group` WHERE `id_group` IN (' . pSQL($groups) . '))
997 ORDER BY `level_depth` ASC, ' . (Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`') . ' ' . (Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC')))
998 return;
999
1000 $resultParents = array();
1001 $resultIds = array();
1002
1003 foreach ($result as &$row) {
1004 $resultParents[$row['id_parent']][] = &$row;
1005 $resultIds[$row['id_category']] = &$row;
1006 }
1007
1008 $blockCategTree = $this->getTree($resultParents, $resultIds, Configuration::get('BLOCK_CATEG_MAX_DEPTH'));
1009 unset($resultParents, $resultIds);
1010
1011 $id_category = (int) Tools::getValue('id_category');
1012 $id_product = (int) Tools::getValue('id_product');
1013
1014
1015 if (Tools::isSubmit('id_category')) {
1016 $this->context->cookie->last_visited_category = $id_category;
1017 $this->smarty->assign('currentCategoryId', $this->context->cookie->last_visited_category);
1018 }
1019 if (Tools::isSubmit('id_product')) {
1020 if (!isset($this->context->cookie->last_visited_category)
1021 || !Product::idIsOnCategoryId($id_product, array('0' => array('id_category' => $this->context->cookie->last_visited_category)))
1022 || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop)) {
1023 $product = new Product($id_product);
1024 if (isset($product) && Validate::isLoadedObject($product))
1025 $this->context->cookie->last_visited_category = (int) $product->id_category_default;
1026 }
1027 $this->smarty->assign('currentCategoryId', (int) $this->context->cookie->last_visited_category);
1028 }
1029 return $blockCategTree;
1030 }
1031
1032 public function getMenuCustomerLink($lang_id = NULL) {
1033 $menu_items = $this->getMenuItems();
1034 $item1=0;
1035 $id_lang = (int)$this->context->language->id;
1036 $id_shop = (int)Shop::getContextShopID();
1037 foreach ($menu_items as $item)
1038 {
1039 if (!$item)
1040 continue;
1041
1042 preg_match($this->pattern, $item, $value);
1043 $id = (int)substr($item, strlen($value[1]), strlen($item));
1044
1045 switch (substr($item, 0, strlen($value[1])))
1046 {
1047 case 'CAT':
1048 $item1 = $item1+ 1;
1049 $this->_menuLink .= $this->drawCustomMenuItem($id, 0, false, $item1, $lang_id);
1050 break;
1051 case 'PRD':
1052 $selected = ($this->page_name == 'product' && (Tools::getValue('id_product') == $id)) ? ' class="sfHover"' : '';
1053 $product = new Product((int)$id, true, (int)$id_lang);
1054 if (!is_null($product->id))
1055 $this->_menuLink .= '<div id ="pt_menu_product" class ="pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($product->getLink()).'"><span>'.$product->name.'</span></a></div></div>'.PHP_EOL;
1056
1057 break;
1058 case 'CMS':
1059 $selected = ($this->page_name == 'cms' && (Tools::getValue('id_cms') == $id)) ? ' class="sfHover"' : '';
1060 $cms = CMS::getLinks((int)$id_lang, array($id));
1061 if (count($cms))
1062 $this->_menuLink .= '<div class ="pt_menu_cms pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($cms[0]['link']).'"><span>'.$cms[0]['meta_title'].'</span></a></div></div>'.PHP_EOL;
1063 break;
1064
1065 case 'CMS_CAT':
1066 $category = new CMSCategory((int)$id, (int)$id_lang);
1067 if (Validate::isLoadedObject($category))
1068 $this->_menuLink .= '<div class ="pt_menu_cms pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($category->getLink()).'"><span>'.$category->name.'</span></a>';
1069 $this->_menuLink .= '</div>'.PHP_EOL;
1070 $this->_menuLink .= $this->getCMSMenuItems($category->id);
1071
1072 break;
1073
1074 // Case to handle the option to show all Manufacturers
1075 case 'ALLMAN':
1076
1077 $link = new Link;
1078 $this->_menuLink .= '<div id ="pt_menu_allsub" class ="pt_menu"><div class="parentMenu" ><a href="'.$link->getPageLink('manufacturer').'" ><span>'.$this->l('All manufacturers').'</span></a></div>'.PHP_EOL;
1079
1080 $manufacturers = Manufacturer::getManufacturers();
1081 $this->_menuLink .= '<div class ="popup" style ="display:none">';
1082 foreach ($manufacturers as $key => $manufacturer)
1083 $this->_menuLink .= '<div class ="block1"><div class="column col1"><div class="itemSubMenu level3"><a href="'.$link->getManufacturerLink((int)$manufacturer['id_manufacturer'], $manufacturer['link_rewrite']).'">'.$manufacturer['name'].'</a></div></div></div>'.PHP_EOL;
1084 $this->_menuLink .= '</div></div>';
1085 break;
1086 case 'MAN':
1087 $selected = ($this->page_name == 'manufacturer' && (Tools::getValue('id_manufacturer') == $id)) ? ' class="sfHover"' : '';
1088 $manufacturer = new Manufacturer((int)$id, (int)$id_lang);
1089 if (!is_null($manufacturer->id))
1090 {
1091 if (intval(Configuration::get('PS_REWRITING_SETTINGS')))
1092 $manufacturer->link_rewrite = Tools::link_rewrite($manufacturer->name);
1093 else
1094 $manufacturer->link_rewrite = 0;
1095 $link = new Link;
1096 $this->_menuLink .= '<div id ="pt_menu_man" class ="pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($link->getManufacturerLink((int)$id, $manufacturer->link_rewrite)).'"><span>'.$manufacturer->name.'</span></a></div></div>'.PHP_EOL;
1097
1098 }
1099 break;
1100
1101 // Case to handle the option to show all Suppliers
1102 case 'ALLSUP':
1103 $link = new Link;
1104 $this->_menuLink .= '<div id ="pt_menu_allsub" class ="pt_menu"><div class="parentMenu" ><a href="'.$link->getPageLink('supplier').'" ><span>'.$this->l('All suppliers').'</span></a></div>'.PHP_EOL;
1105
1106 $suppliers = Supplier::getSuppliers();
1107 $this->_menuLink .= '<div class ="popup" style ="display:none">';
1108 foreach ($suppliers as $key => $supplier)
1109 $this->_menuLink .= '<div class ="block1"><div class="column col1"><div class="itemSubMenu level3"><a href="'.$link->getSupplierLink((int)$supplier['id_supplier'], $supplier['link_rewrite']).'">'.$supplier['name'].'</a></div></div></div>'.PHP_EOL;
1110 $this->_menuLink .= '</div></div>';
1111 break;
1112
1113 case 'SUP':
1114 $selected = ($this->page_name == 'supplier' && (Tools::getValue('id_supplier') == $id)) ? ' class="sfHover"' : '';
1115 $supplier = new Supplier((int)$id, (int)$id_lang);
1116 if (!is_null($supplier->id))
1117 {
1118 $link = new Link;
1119
1120 $this->_menuLink .= '<div id ="pt_menu_sub" class ="pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($link->getSupplierLink((int)$id, $supplier->link_rewrite)).'"><span>'.$supplier->name.'</span></a></div></div>'.PHP_EOL;
1121
1122 }
1123 break;
1124
1125 case 'SHOP':
1126 $selected = ($this->page_name == 'index' && ($this->context->shop->id == $id)) ? ' class="sfHover"' : '';
1127 $shop = new Shop((int)$id);
1128 if (Validate::isLoadedObject($shop))
1129 {
1130 $link = new Link;
1131 $this->_menuLink .= '<div id ="pt_menu_sub" class ="pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($shop->getBaseURL()).'"><span>'.$supplier->name.'</span></a></div></div>'.PHP_EOL;
1132 }
1133 break;
1134 case 'LNK':
1135 $link = MegaTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
1136 if (count($link))
1137 {
1138 if (!isset($link[0]['label']) || ($link[0]['label'] == ''))
1139 {
1140 $default_language = Configuration::get('PS_LANG_DEFAULT');
1141 $link = MegaTopLinks::get($link[0]['id_linksmegatop'], $default_language, (int)Shop::getContextShopID());
1142 }
1143 $this->_menuLink .= '<div id ="pt_menu_link" class ="pt_menu"><div class="parentMenu" ><a href="'.Tools::HtmlEntitiesUTF8($link[0]['link']).'"'.(($link[0]['new_window']) ? ' target="_blank"': '').'><span>'.$link[0]['label'].'</span></a></div></div>'.PHP_EOL;
1144 }
1145 break;
1146
1147
1148 }
1149
1150 }
1151
1152 }
1153
1154 public function hookTop() {
1155 //$lang_id = (int) Configuration::get('PS_LANG_DEFAULT');
1156 $lang_id = (int)Context::getContext()->language->id;
1157 $category = new Category();
1158 //$homeCates = $category->getHomeCategories($lang_id);
1159 $this->getMenuCustomerLink($lang_id);
1160 $item = 0;
1161 $html = "";
1162 $showhome = Configuration::get($this->name . '_show_homepage');
1163 if ($showhome) {
1164 $page_name = Dispatcher::getInstance()->getController();
1165 $active = null;
1166 if ($page_name == 'index')
1167 $active = ' act';
1168 $id = "_home";
1169 $html .= '<div id="pt_menu' . $id . '" class="pt_menu' . $active . '">';
1170 $html .= '<div class="parentMenu">';
1171 $html .= '<a href="' . __PS_BASE_URI__ . '">';
1172 $html .= '<span>' . $this->l('Home') . '</span>';
1173 $html .= '</a>';
1174 $html .= '</div>';
1175 $html .= '</div>';
1176 }
1177
1178 // foreach ($homeCates as $cate) {
1179 // $item++;
1180 // // $html .= $this->drawCustomMenuItem($cate['id_category'], 0, false, $item, $lang_id);
1181 // }
1182 $html .= $this->_menuLink;
1183
1184 $blockCustomer = $this->getStaticBlockContent(null, 'item');
1185 foreach ($blockCustomer as $bc) {
1186 $html .= $this->drawCustomMenuBlock($bc['identify'], $bc);
1187 }
1188 $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
1189 $blockCategTree = $this->getblockCategTree();
1190 $this->smarty->assign('blockCategTree', $blockCategTree);
1191 if (file_exists(_PS_THEME_DIR_ . 'modules/blockcategories/blockcategories.tpl'))
1192 $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_ . 'modules/blockcategories/category-tree-branch.tpl');
1193 else
1194 $this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_ . 'blockcategories/category-tree-branch.tpl');
1195 $this->smarty->assign('isDhtml', $isDhtml);
1196 $this->context->smarty->assign(
1197 array(
1198 'megamenu' => $html,
1199 'top_offset' => Configuration::get($this->name . '_top_offset'),
1200 'effect' => Configuration::get($this->name . '_effect'),
1201 'menu_link' => $this->_menuLink,
1202 )
1203 );
1204 //$test = $this->seperateColumns($homeCates,4, $lang_id);
1205
1206
1207 return $this->display(__FILE__, 'megamenu.tpl');
1208 }
1209 public function hookMegamenu() {
1210 //$lang_id = (int) Configuration::get('PS_LANG_DEFAULT');
1211 $lang_id = (int)Context::getContext()->language->id;
1212 $category = new Category();
1213 //$homeCates = $category->getHomeCategories($lang_id);
1214 $this->getMenuCustomerLink($lang_id);
1215 $item = 0;
1216 $html = "";
1217 $showhome = Configuration::get($this->name . '_show_homepage');
1218 if ($showhome) {
1219 $page_name = Dispatcher::getInstance()->getController();
1220 $active = null;
1221 if ($page_name == 'index')
1222 $active = ' act';
1223 $id = "_home";
1224 $html .= '<div id="pt_menu' . $id . '" class="pt_menu' . $active . '">';
1225 $html .= '<div class="parentMenu">';
1226 $html .= '<a href="' . __PS_BASE_URI__ . '">';
1227 $html .= '<span>' . $this->l('Home') .'</span>';
1228 $html .= '</a>';
1229 $html .= '</div>';
1230 $html .= '</div>';
1231 }
1232
1233 // foreach ($homeCates as $cate) {
1234 // $item++;
1235 // // $html .= $this->drawCustomMenuItem($cate['id_category'], 0, false, $item, $lang_id);
1236 // }
1237 $html .= $this->_menuLink;
1238
1239 $blockCustomer = $this->getStaticBlockContent(null, 'item');
1240 foreach ($blockCustomer as $bc) {
1241 $html .= $this->drawCustomMenuBlock($bc['identify'], $bc);
1242 }
1243 $isDhtml = (Configuration::get('BLOCK_CATEG_DHTML') == 1 ? true : false);
1244 $blockCategTree = $this->getblockCategTree();
1245 $this->smarty->assign('blockCategTree', $blockCategTree);
1246 if (file_exists(_PS_THEME_DIR_ . 'modules/blockcategories/blockcategories.tpl'))
1247 $this->smarty->assign('branche_tpl_path', _PS_THEME_DIR_ . 'modules/blockcategories/category-tree-branch.tpl');
1248 else
1249 $this->smarty->assign('branche_tpl_path', _PS_MODULE_DIR_ . 'blockcategories/category-tree-branch.tpl');
1250 $this->smarty->assign('isDhtml', $isDhtml);
1251 $this->context->smarty->assign(
1252 array(
1253 'megamenu' => $html,
1254 'top_offset' => Configuration::get($this->name . '_top_offset'),
1255 'effect' => Configuration::get($this->name . '_effect'),
1256 'menu_link' => $this->_menuLink,
1257 )
1258 );
1259 //$test = $this->seperateColumns($homeCates,4, $lang_id);
1260
1261
1262 return $this->display(__FILE__, 'megamenu.tpl');
1263 }
1264
1265 private function getCMSMenuItems($parent, $depth = 1, $id_lang = false)
1266 {
1267 $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
1268
1269 if ($depth > 3)
1270 return;
1271
1272 $categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang);
1273
1274 $pages = $this->getCMSPages((int)$parent);
1275 if (count($categories) || count($pages))
1276 {
1277
1278
1279 $this->_menuLink .= '<div class ="popup" style ="display:none">';
1280
1281 foreach ($pages as $page) {
1282 $cms = new CMS($page['id_cms'], (int)$id_lang);
1283 $links = $cms->getLinks((int)$id_lang, array((int)$cms->id));
1284 $selected = ($this->page_name == 'cms' && ((int)Tools::getValue('id_cms') == $page['id_cms'])) ? ' class="sfHoverForce"' : '';
1285
1286 $this->_menuLink .= '<div class ="block1"><div class="column col1"><div class="itemSubMenu level3"><a href="'.$links[0]['link'].'">'.$cms->meta_title.'</a></div></div></div>'.PHP_EOL;
1287 }
1288 $this->_menuLink .= '</div></div>';
1289
1290
1291
1292 }
1293 }
1294
1295 private function getCMSOptions($parent = 0, $depth = 1, $id_lang = false)
1296 {
1297 $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
1298
1299 $categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang);
1300 $pages = $this->getCMSPages((int)$parent, false, (int)$id_lang);
1301
1302 $spacer = str_repeat(' ', $this->spacer_size * (int)$depth);
1303
1304 foreach ($categories as $category)
1305 {
1306 $this->_html .= '<option value="CMS_CAT'.$category['id_cms_category'].'" style="font-weight: bold;">'.$spacer.$category['name'].'</option>';
1307 $this->getCMSOptions($category['id_cms_category'], (int)$depth + 1, (int)$id_lang);
1308 }
1309
1310 foreach ($pages as $page)
1311 $this->_html .= '<option value="CMS'.$page['id_cms'].'">'.$spacer.$page['meta_title'].'</option>';
1312 }
1313
1314 private function getCMSCategories($recursive = false, $parent = 1, $id_lang = false)
1315 {
1316 $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
1317
1318 if ($recursive === false)
1319 {
1320 $sql = 'SELECT bcp.`id_cms_category`, bcp.`id_parent`, bcp.`level_depth`, bcp.`active`, bcp.`position`, cl.`name`, cl.`link_rewrite`
1321 FROM `'._DB_PREFIX_.'cms_category` bcp
1322 INNER JOIN `'._DB_PREFIX_.'cms_category_lang` cl
1323 ON (bcp.`id_cms_category` = cl.`id_cms_category`)
1324 WHERE cl.`id_lang` = '.(int)$id_lang.'
1325 AND bcp.`id_parent` = '.(int)$parent;
1326
1327 return Db::getInstance()->executeS($sql);
1328 }
1329 else
1330 {
1331 $sql = 'SELECT bcp.`id_cms_category`, bcp.`id_parent`, bcp.`level_depth`, bcp.`active`, bcp.`position`, cl.`name`, cl.`link_rewrite`
1332 FROM `'._DB_PREFIX_.'cms_category` bcp
1333 INNER JOIN `'._DB_PREFIX_.'cms_category_lang` cl
1334 ON (bcp.`id_cms_category` = cl.`id_cms_category`)
1335 WHERE cl.`id_lang` = '.(int)$id_lang.'
1336 AND bcp.`id_parent` = '.(int)$parent;
1337
1338 $results = Db::getInstance()->executeS($sql);
1339 foreach ($results as $result)
1340 {
1341 $sub_categories = $this->getCMSCategories(true, $result['id_cms_category'], (int)$id_lang);
1342 if ($sub_categories && count($sub_categories) > 0)
1343 $result['sub_categories'] = $sub_categories;
1344 $categories[] = $result;
1345 }
1346
1347 return isset($categories) ? $categories : false;
1348 }
1349
1350 }
1351
1352 private function getCMSPages($id_cms_category, $id_shop = false, $id_lang = false)
1353 {
1354 $id_shop = ($id_shop !== false) ? (int)$id_shop : (int)Context::getContext()->shop->id;
1355 $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
1356
1357 $sql = 'SELECT c.`id_cms`, cl.`meta_title`, cl.`link_rewrite`
1358 FROM `'._DB_PREFIX_.'cms` c
1359 INNER JOIN `'._DB_PREFIX_.'cms_shop` cs
1360 ON (c.`id_cms` = cs.`id_cms`)
1361 INNER JOIN `'._DB_PREFIX_.'cms_lang` cl
1362 ON (c.`id_cms` = cl.`id_cms`)
1363 WHERE c.`id_cms_category` = '.(int)$id_cms_category.'
1364 AND cs.`id_shop` = '.(int)$id_shop.'
1365 AND cl.`id_lang` = '.(int)$id_lang.'
1366 AND c.`active` = 1
1367 ORDER BY `position`';
1368
1369 return Db::getInstance()->executeS($sql);
1370 }
1371
1372 private function makeMenuOption()
1373 {
1374 $menu_item = $this->getMenuItems();
1375 $id_lang = (int)$this->context->language->id;
1376 $id_shop = (int)Shop::getContextShopID();
1377 foreach ($menu_item as $item)
1378 {
1379 if (!$item)
1380 continue;
1381
1382 preg_match($this->pattern, $item, $values);
1383 $id = (int)substr($item, strlen($values[1]), strlen($item));
1384
1385 switch (substr($item, 0, strlen($values[1])))
1386 {
1387 case 'CAT':
1388 $category = new Category((int)$id, (int)$id_lang);
1389 if (Validate::isLoadedObject($category))
1390 $this->_html .= '<option value="CAT'.$id.'">'.$category->name.'</option>'.PHP_EOL;
1391 break;
1392
1393 case 'PRD':
1394 $product = new Product((int)$id, true, (int)$id_lang);
1395 if (Validate::isLoadedObject($product))
1396 $this->_html .= '<option value="PRD'.$id.'">'.$product->name.'</option>'.PHP_EOL;
1397 break;
1398
1399 case 'CMS':
1400 $cms = new CMS((int)$id, (int)$id_lang);
1401 if (Validate::isLoadedObject($cms))
1402 $this->_html .= '<option value="CMS'.$id.'">'.$cms->meta_title.'</option>'.PHP_EOL;
1403 break;
1404
1405 case 'CMS_CAT':
1406 $category = new CMSCategory((int)$id, (int)$id_lang);
1407 if (Validate::isLoadedObject($category))
1408 $this->_html .= '<option value="CMS_CAT'.$id.'">'.$category->name.'</option>'.PHP_EOL;
1409 break;
1410
1411 // Case to handle the option to show all Manufacturers
1412 case 'ALLMAN':
1413 $this->_html .= '<option value="ALLMAN0">'.$this->l('All manufacturers').'</option>'.PHP_EOL;
1414 break;
1415
1416 case 'MAN':
1417 $manufacturer = new Manufacturer((int)$id, (int)$id_lang);
1418 if (Validate::isLoadedObject($manufacturer))
1419 $this->_html .= '<option value="MAN'.$id.'">'.$manufacturer->name.'</option>'.PHP_EOL;
1420 break;
1421
1422 // Case to handle the option to show all Suppliers
1423 case 'ALLSUP':
1424 $this->_html .= '<option value="ALLSUP0">'.$this->l('All suppliers').'</option>'.PHP_EOL;
1425 break;
1426
1427 case 'SUP':
1428 $supplier = new Supplier((int)$id, (int)$id_lang);
1429 if (Validate::isLoadedObject($supplier))
1430 $this->_html .= '<option value="SUP'.$id.'">'.$supplier->name.'</option>'.PHP_EOL;
1431 break;
1432 case 'LNK':
1433 $link = MegaTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
1434 if (count($link))
1435 {
1436 if (!isset($link[0]['label']) || ($link[0]['label'] == ''))
1437 {
1438 $default_language = Configuration::get('PS_LANG_DEFAULT');
1439 $link = MegaTopLinks::get($link[0]['id_linksmegatop'], (int)$default_language, (int)Shop::getContextShopID());
1440 }
1441 $this->_html .= '<option value="LNK'.$link[0]['id_linksmegatop'].'">'.$link[0]['label'].'</option>';
1442 }
1443 break;
1444
1445 case 'SHOP':
1446 $shop = new Shop((int)$id);
1447 if (Validate::isLoadedObject($shop))
1448 $this->_html .= '<option value="SHOP'.(int)$id.'">'.$shop->name.'</option>'.PHP_EOL;
1449 break;
1450 }
1451 }
1452 }
1453
1454 private function getMenuItems(){
1455
1456 return explode(',', Configuration::get('MOD_BLOCKPOSMENU_ITEMS'));
1457 }
1458
1459 public function installDb(){
1460
1461 return (Db::getInstance()->execute('
1462 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmegatop` (
1463 `id_linksmegatop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
1464 `id_shop` INT(11) UNSIGNED NOT NULL,
1465 `new_window` TINYINT( 1 ) NOT NULL,
1466 INDEX (`id_shop`)
1467 ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;') &&
1468 Db::getInstance()->execute('
1469 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmegatop_lang` (
1470 `id_linksmegatop` INT(11) UNSIGNED NOT NULL,
1471 `id_lang` INT(11) UNSIGNED NOT NULL,
1472 `id_shop` INT(11) UNSIGNED NOT NULL,
1473 `label` VARCHAR( 128 ) NOT NULL ,
1474 `link` VARCHAR( 128 ) NOT NULL ,
1475 INDEX ( `id_linksmegatop` , `id_lang`, `id_shop`)
1476 ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;'));
1477 }
1478
1479 private function uninstallDb() {
1480 Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmegatop`');
1481 Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmegatop_lang`');
1482 return true;
1483 }
1484 private function _installHookCustomer(){
1485 $hookspos = array(
1486 'megamenu',
1487 );
1488 foreach( $hookspos as $hook ){
1489 if( Hook::getIdByName($hook) ){
1490
1491 } else {
1492 $new_hook = new Hook();
1493 $new_hook->name = pSQL($hook);
1494 $new_hook->title = pSQL($hook);
1495 $new_hook->add();
1496 $id_hook = $new_hook->id;
1497 }
1498 }
1499 return true;
1500 }
1501
1502
1503}