From e6046abdc6d781da260012cdf099368c5cd4dade Mon Sep 17 00:00:00 2001 From: gCharmes Date: Mon, 21 May 2012 03:08:05 +0000 Subject: [PATCH] [-] MO : #PSCFV-2415 : Add multilanguage support to blocktopmenu --- modules/blocktopmenu/blocktopmenu.php | 190 ++++++++++++-------- modules/blocktopmenu/menutoplinks.class.php | 65 ++++++- 2 files changed, 172 insertions(+), 83 deletions(-) diff --git a/modules/blocktopmenu/blocktopmenu.php b/modules/blocktopmenu/blocktopmenu.php index dbb3ca1fa..b0aa07377 100644 --- a/modules/blocktopmenu/blocktopmenu.php +++ b/modules/blocktopmenu/blocktopmenu.php @@ -25,7 +25,7 @@ * International Registered Trademark & Property of PrestaShop SA */ -require dirname(__FILE__).'/menutoplinks.class.php'; +require (dirname(__FILE__).'/menutoplinks.class.php'); class Blocktopmenu extends Module { @@ -52,7 +52,7 @@ class Blocktopmenu extends Module { $this->name = 'blocktopmenu'; $this->tab = 'front_office_features'; - $this->version = 1.4; + $this->version = 1.5; $this->author = 'PrestaShop'; parent::__construct(); @@ -79,15 +79,15 @@ class Blocktopmenu extends Module `id_linksmenutop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `id_shop` INT UNSIGNED NOT NULL, `new_window` TINYINT( 1 ) NOT NULL, - `link` VARCHAR( 128 ) NOT NULL, INDEX (`id_shop`) ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;') && - Db::getInstance()->execute(' + Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmenutop_lang` ( `id_linksmenutop` INT NOT NULL, `id_lang` INT NOT NULL, `id_shop` INT NOT NULL, `label` VARCHAR( 128 ) NOT NULL , + `link` VARCHAR( 128 ) NOT NULL , INDEX ( `id_linksmenutop` , `id_lang`, `id_shop`) ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;')); } @@ -116,6 +116,7 @@ class Blocktopmenu extends Module $default_language = (int)Configuration::get('PS_LANG_DEFAULT'); $labels = Tools::getValue('label') ? array_filter(Tools::getValue('label'), 'strlen') : array(); + $links_label = Tools::getValue('link') ? array_filter(Tools::getValue('link'), 'strlen') : array(); $spacer = str_repeat(' ', $this->spacer_size); $divLangName = 'link_label'; @@ -130,9 +131,9 @@ class Blocktopmenu extends Module else if (Tools::isSubmit('submitBlocktopmenuLinks')) { - if ((Tools::getValue('link') == '') && (!count($labels))) + if ((!count($links_label)) && (!count($labels))) ; - else if (Tools::getValue('link') == '') + else if (!count($links_label)) $this->_html .= $this->displayError($this->l('Please, fill the "Link" field')); else if (!count($labels)) $this->_html .= $this->displayError($this->l('Please add a label')); @@ -151,6 +152,24 @@ class Blocktopmenu extends Module Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', str_replace(array('LNK'.$id_linksmenutop.',', 'LNK'.$id_linksmenutop), '', Configuration::get('MOD_BLOCKTOPMENU_ITEMS'))); $this->_html .= $this->displayConfirmation($this->l('The link has been removed')); } + else if (Tools::isSubmit('submitBlocktopmenuEdit')) + { + $id_linksmenutop = (int)Tools::getValue('id_linksmenutop', 0); + $id_shop = (int)Shop::getContextShopID(); + + if (!Tools::isSubmit('link')) + { + $tmp = MenuTopLinks::getLinkLang($id_linksmenutop, $id_shop); + $links_label_edit = $tmp['link']; + $labels_edit = $tmp['label']; + $new_window_edit = $tmp['new_window']; + } + else + { + MenuTopLinks::update(Tools::getValue('link'), Tools::getValue('label'), Tools::getValue('new_window', 0), (int)$id_shop, (int)$id_linksmenutop, (int)$id_linksmenutop); + $this->_html .= $this->displayConfirmation($this->l('The link has been edited')); + } + } $this->_html .= '
@@ -172,59 +191,59 @@ class Blocktopmenu extends Module
+ $this->makeMenuOption(); + $this->_html .= '

'.$this->l('Remove').' >>
+ $this->_html .= '

<< '.$this->l('Add').' @@ -289,29 +308,45 @@ class Blocktopmenu extends Module
'.$this->l('Add Menu Top Link').'
- -
'; - foreach ($languages as $language) - { - $this->_html .= ' - '; - } - $this->_html .= $this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true); - $this->_html .= '

- + '; + foreach ($languages as $language) + { + $this->_html .= ' + + '; + + $this->_html .= ' +
- +
-

+ '; + } + + $this->_html .= ' +

'.$this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true).'

'; + + $this->_html .= ' +
+ +
+
'; + + if (Tools::isSubmit('id_linksmenutop')) + $this->_html .= ''; + + if (Tools::isSubmit('submitBlocktopmenuEdit')) + $this->_html .= ''; + + $this->_html .= ' -

+
+

'; @@ -325,7 +360,7 @@ class Blocktopmenu extends Module '.$this->l('List Menu Top Link').' - + @@ -334,23 +369,24 @@ class Blocktopmenu extends Module '; - foreach ($links as $link) - { - $this->_html .= ' + foreach ($links as $link) + { + $this->_html .= ' - - - + + + '; - } - $this->_html .= ' + } + $this->_html .= '
'.$this->l('Id Link').' '.$this->l('Label').' '.$this->l('Link').'
'.$link['id_linksmenutop'].''.$link['label'].''.$link['link'].''.(int)$link['id_linksmenutop'].''.Tools::safeOutput($link['label']).''.Tools::safeOutput($link['link']).' '.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).' -
- + + +
'; return $this->_html; @@ -703,4 +739,4 @@ class Blocktopmenu extends Module return Db::getInstance()->executeS($sql); } -} \ No newline at end of file +} diff --git a/modules/blocktopmenu/menutoplinks.class.php b/modules/blocktopmenu/menutoplinks.class.php index 47e19926f..0d85a73ff 100644 --- a/modules/blocktopmenu/menutoplinks.class.php +++ b/modules/blocktopmenu/menutoplinks.class.php @@ -30,7 +30,7 @@ class MenuTopLinks public static function gets($id_lang, $id_linksmenutop = null, $id_shop) { return Db::getInstance()->executeS(' - SELECT l.id_linksmenutop, l.new_window, l.link, ll.label + SELECT l.id_linksmenutop, l.new_window, ll.link, ll.label FROM '._DB_PREFIX_.'linksmenutop l LEFT JOIN '._DB_PREFIX_.'linksmenutop_lang ll ON (l.id_linksmenutop = ll.id_linksmenutop AND ll.id_lang = '.(int)$id_lang.' AND ll.id_shop='.(int)$id_shop.') WHERE 1 @@ -44,34 +44,87 @@ class MenuTopLinks return self::gets($id_lang, $id_linksmenutop, $id_shop); } + public static function getLinkLang($id_linksmenutop, $id_shop) + { + $ret = Db::getInstance()->executeS(' + SELECT l.id_linksmenutop, l.new_window, ll.link, ll.label, ll.id_lang + FROM '._DB_PREFIX_.'linksmenutop l + LEFT JOIN '._DB_PREFIX_.'linksmenutop_lang ll ON (l.id_linksmenutop = ll.id_linksmenutop AND ll.id_shop='.(int)$id_shop.') + WHERE 1 + '.((!is_null($id_linksmenutop)) ? ' AND l.id_linksmenutop = "'.(int)$id_linksmenutop.'"' : '').' + AND l.id_shop IN (0, '.(int)$id_shop.') + '); + + $link = array(); + $label = array(); + $new_window = false; + foreach ($ret as $line) + { + $link[$line['id_lang']] = Tools::safeOutput($line['link']); + $label[$line['id_lang']] = Tools::safeOutput($line['label']); + $new_window = (bool)$line['new_window']; + } + + return array('link' => $link, 'label' => $label, 'new_window' => $new_window); + } + public static function add($link, $label, $newWindow = 0, $id_shop) { if(!is_array($label)) return false; + if(!is_array($link)) + return false; Db::getInstance()->insert( 'linksmenutop', array( 'new_window'=>(int)$newWindow, - 'link'=>pSQL($link), 'id_shop' => (int)$id_shop ) ); $id_linksmenutop = Db::getInstance()->Insert_ID(); - foreach($label as $id_lang=>$label) - { + foreach ($label as $id_lang=>$label) Db::getInstance()->insert( 'linksmenutop_lang', array( 'id_linksmenutop'=>(int)$id_linksmenutop, 'id_lang'=>(int)$id_lang, 'id_shop'=>(int)$id_shop, - 'label'=>pSQL($label) + 'label'=>pSQL($label), + 'link'=>pSQL($link[$id_lang]) ) ); - } } + public static function update($link, $labels, $newWindow = 0, $id_shop, $id_link) + { + if(!is_array($labels)) + return false; + if(!is_array($link)) + return false; + + Db::getInstance()->update( + 'linksmenutop', + array( + 'new_window'=>(int)$newWindow, + 'id_shop' => (int)$id_shop + ), + 'id_linksmenutop = '.(int)$id_link + ); + + foreach ($labels as $id_lang => $label) + Db::getInstance()->update( + 'linksmenutop_lang', + array( + 'id_shop'=>(int)$id_shop, + 'label'=>pSQL($label), + 'link'=>pSQL($link[$id_lang]) + ), + 'id_linksmenutop = '.(int)$id_link.' AND id_lang = '.(int)$id_lang + ); + } + + public static function remove($id_linksmenutop, $id_shop) { Db::getInstance()->delete('linksmenutop', 'id_linksmenutop = '.(int)$id_linksmenutop.' AND id_shop = '.(int)$id_shop);