[-] MO : #PSCFV-2415 : Add multilanguage support to blocktopmenu

This commit is contained in:
gCharmes
2012-05-21 03:08:05 +00:00
parent a7da37a65d
commit e6046abdc6
2 changed files with 172 additions and 83 deletions
+113 -77
View File
@@ -25,7 +25,7 @@
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*/ */
require dirname(__FILE__).'/menutoplinks.class.php'; require (dirname(__FILE__).'/menutoplinks.class.php');
class Blocktopmenu extends Module class Blocktopmenu extends Module
{ {
@@ -52,7 +52,7 @@ class Blocktopmenu extends Module
{ {
$this->name = 'blocktopmenu'; $this->name = 'blocktopmenu';
$this->tab = 'front_office_features'; $this->tab = 'front_office_features';
$this->version = 1.4; $this->version = 1.5;
$this->author = 'PrestaShop'; $this->author = 'PrestaShop';
parent::__construct(); parent::__construct();
@@ -79,15 +79,15 @@ class Blocktopmenu extends Module
`id_linksmenutop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `id_linksmenutop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_shop` INT UNSIGNED NOT NULL, `id_shop` INT UNSIGNED NOT NULL,
`new_window` TINYINT( 1 ) NOT NULL, `new_window` TINYINT( 1 ) NOT NULL,
`link` VARCHAR( 128 ) NOT NULL,
INDEX (`id_shop`) INDEX (`id_shop`)
) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;') && ) 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` ( CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmenutop_lang` (
`id_linksmenutop` INT NOT NULL, `id_linksmenutop` INT NOT NULL,
`id_lang` INT NOT NULL, `id_lang` INT NOT NULL,
`id_shop` INT NOT NULL, `id_shop` INT NOT NULL,
`label` VARCHAR( 128 ) NOT NULL , `label` VARCHAR( 128 ) NOT NULL ,
`link` VARCHAR( 128 ) NOT NULL ,
INDEX ( `id_linksmenutop` , `id_lang`, `id_shop`) INDEX ( `id_linksmenutop` , `id_lang`, `id_shop`)
) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;')); ) 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'); $default_language = (int)Configuration::get('PS_LANG_DEFAULT');
$labels = Tools::getValue('label') ? array_filter(Tools::getValue('label'), 'strlen') : array(); $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); $spacer = str_repeat(' ', $this->spacer_size);
$divLangName = 'link_label'; $divLangName = 'link_label';
@@ -130,9 +131,9 @@ class Blocktopmenu extends Module
else if (Tools::isSubmit('submitBlocktopmenuLinks')) 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')); $this->_html .= $this->displayError($this->l('Please, fill the "Link" field'));
else if (!count($labels)) else if (!count($labels))
$this->_html .= $this->displayError($this->l('Please add a label')); $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'))); 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')); $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 .= ' $this->_html .= '
<fieldset> <fieldset>
@@ -172,59 +191,59 @@ class Blocktopmenu extends Module
<tr> <tr>
<td> <td>
<select multiple="multiple" id="items" style="width: 300px; height: 160px;">'; <select multiple="multiple" id="items" style="width: 300px; height: 160px;">';
$this->makeMenuOption(); $this->makeMenuOption();
$this->_html .= '</select><br/> $this->_html .= '</select><br/>
<br/> <br/>
<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').' &gt;&gt;</a> <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').' &gt;&gt;</a>
</td> </td>
<td style="padding-left: 20px;"> <td style="padding-left: 20px;">
<select multiple="multiple" id="availableItems" style="width: 300px; height: 160px;">'; <select multiple="multiple" id="availableItems" style="width: 300px; height: 160px;">';
// BEGIN CMS // BEGIN CMS
$this->_html .= '<optgroup label="'.$this->l('CMS').'">'; $this->_html .= '<optgroup label="'.$this->l('CMS').'">';
$this->getCMSOptions(0, 1, $id_lang); $this->getCMSOptions(0, 1, $id_lang);
$this->_html .= '</optgroup>'; $this->_html .= '</optgroup>';
// BEGIN SUPPLIER // BEGIN SUPPLIER
$this->_html .= '<optgroup label="'.$this->l('Supplier').'">'; $this->_html .= '<optgroup label="'.$this->l('Supplier').'">';
$suppliers = Supplier::getSuppliers(false, $id_lang); $suppliers = Supplier::getSuppliers(false, $id_lang);
foreach ($suppliers as $supplier) foreach ($suppliers as $supplier)
$this->_html .= '<option value="SUP'.$supplier['id_supplier'].'">'.$spacer.$supplier['name'].'</option>'; $this->_html .= '<option value="SUP'.$supplier['id_supplier'].'">'.$spacer.$supplier['name'].'</option>';
$this->_html .= '</optgroup>'; $this->_html .= '</optgroup>';
// BEGIN Manufacturer // BEGIN Manufacturer
$this->_html .= '<optgroup label="'.$this->l('Manufacturer').'">'; $this->_html .= '<optgroup label="'.$this->l('Manufacturer').'">';
$manufacturers = Manufacturer::getManufacturers(false, $id_lang); $manufacturers = Manufacturer::getManufacturers(false, $id_lang);
foreach ($manufacturers as $manufacturer) foreach ($manufacturers as $manufacturer)
$this->_html .= '<option value="MAN'.$manufacturer['id_manufacturer'].'">'.$spacer.$manufacturer['name'].'</option>'; $this->_html .= '<option value="MAN'.$manufacturer['id_manufacturer'].'">'.$spacer.$manufacturer['name'].'</option>';
$this->_html .= '</optgroup>'; $this->_html .= '</optgroup>';
// BEGIN Categories // BEGIN Categories
$this->_html .= '<optgroup label="'.$this->l('Categories').'">'; $this->_html .= '<optgroup label="'.$this->l('Categories').'">';
$this->getCategoryOption(1, (int)$id_lang, (int)Shop::getContextShopID()); $this->getCategoryOption(1, (int)$id_lang, (int)Shop::getContextShopID());
$this->_html .= '</optgroup>'; $this->_html .= '</optgroup>';
// BEGIN Products // BEGIN Products
$this->_html .= '<optgroup label="'.$this->l('Products').'">'; $this->_html .= '<optgroup label="'.$this->l('Products').'">';
$this->_html .= '<option value="PRODUCT" style="font-style:italic">'.$spacer.$this->l('Choose ID product').'</option>'; $this->_html .= '<option value="PRODUCT" style="font-style:italic">'.$spacer.$this->l('Choose ID product').'</option>';
$this->_html .= '</optgroup>'; $this->_html .= '</optgroup>';
// BEGIN Menu Top Links // BEGIN Menu Top Links
$this->_html .= '<optgroup label="'.$this->l('Menu Top Links').'">'; $this->_html .= '<optgroup label="'.$this->l('Menu Top Links').'">';
$links = MenuTopLinks::gets($id_lang, null, (int)Shop::getContextShopID()); $links = MenuTopLinks::gets($id_lang, null, (int)Shop::getContextShopID());
foreach ($links as $link) foreach ($links as $link)
{ {
if ($link['label'] == '') if ($link['label'] == '')
{ {
$link = MenuTopLinks::get($link['id_linksmenutop'], $default_language, (int)Shop::getContextShopID()); $link = MenuTopLinks::get($link['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
$this->_html .= '<option value="LNK'.$link[0]['id_linksmenutop'].'">'.$spacer.$link[0]['label'].'</option>'; $this->_html .= '<option value="LNK'.(int)$link[0]['id_linksmenutop'].'">'.$spacer.$link[0]['label'].'</option>';
} }
else else
$this->_html .= '<option value="LNK'.$link['id_linksmenutop'].'">'.$spacer.$link['label'].'</option>'; $this->_html .= '<option value="LNK'.(int)$link['id_linksmenutop'].'">'.$spacer.$link['label'].'</option>';
} }
$this->_html .= '</optgroup>'; $this->_html .= '</optgroup>';
$this->_html .= '</select><br /> $this->_html .= '</select><br />
<br /> <br />
<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);">&lt;&lt; '.$this->l('Add').'</a> <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);">&lt;&lt; '.$this->l('Add').'</a>
</td> </td>
@@ -289,29 +308,45 @@ class Blocktopmenu extends Module
<fieldset> <fieldset>
<legend><img src="../img/admin/add.gif" alt="" title="" />'.$this->l('Add Menu Top Link').'</legend> <legend><img src="../img/admin/add.gif" alt="" title="" />'.$this->l('Add Menu Top Link').'</legend>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" id="form"> <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" id="form">
<label>'.$this->l('Label').'</label>
<div class="margin-form">';
foreach ($languages as $language)
{
$this->_html .= '
<div id="link_label_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang ? 'block' : 'none').';float: left;">
<input type="text" name="label['.$language['id_lang'].']" id="label_'.$language['id_lang'].'" size="70" value="'.(isset($labels[$language['id_lang']]) ? (int)$labels[$language['id_lang']] : '').'" />
</div>';
}
$this->_html .= $this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true);
$this->_html .= '</div><p class="clear"> </p> ';
<label>'.$this->l('Link').'</label> foreach ($languages as $language)
{
$this->_html .= '
<div id="link_label_'.(int)$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang ? 'block' : 'none').';">
<label>'.$this->l('Label').'</label>
<div class="margin-form"> <div class="margin-form">
<input type="text" name="link" value="'.Tools::getValue('link').'" size="70" /> <input type="text" name="label['.(int)$language['id_lang'].']" id="label_'.(int)$language['id_lang'].'" size="70" value="'.(isset($labels_edit[$language['id_lang']]) ? Tools::safeOutput($labels_edit[$language['id_lang']]) : '').'" />
</div> </div>
<label>'.$this->l('New Window').'</label> ';
$this->_html .= '
<label>'.$this->l('Link').'</label>
<div class="margin-form"> <div class="margin-form">
<input type="checkbox" name="new_window" value="1" '.(Tools::getValue('new_window') ? 'checked' : '').'/> <input type="text" name="link['.(int)$language['id_lang'].']" id="link_'.(int)$language['id_lang'].'" value="'.(isset($links_label_edit[$language['id_lang']]) ? Tools::safeOutput($links_label_edit[$language['id_lang']]) : '').'" size="70" />
</div> </div>
<p class="center"> </div>';
}
$this->_html .= '<label>'.$this->l('Language').'</label>
<div class="margin-form">'.$this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true).'</div><p style="clear: both;"> </p>';
$this->_html .= '<label style="clear: both;">'.$this->l('New Window').'</label>
<div class="margin-form">
<input style="clear: both;" type="checkbox" name="new_window" value="1" '.(isset($new_window_edit) && $new_window_edit ? 'checked' : '').'/>
</div>
<div class="margin-form">';
if (Tools::isSubmit('id_linksmenutop'))
$this->_html .= '<input type="hidden" name="id_linksmenutop" value="'.(int)Tools::getValue('id_linksmenutop').'" />';
if (Tools::isSubmit('submitBlocktopmenuEdit'))
$this->_html .= '<input type="submit" name="submitBlocktopmenuEdit" value="'.$this->l(' Edit ').'" class="button" />';
$this->_html .= '
<input type="submit" name="submitBlocktopmenuLinks" value="'.$this->l(' Add ').'" class="button" /> <input type="submit" name="submitBlocktopmenuLinks" value="'.$this->l(' Add ').'" class="button" />
</p> </div>
</form> </form>
</fieldset><br />'; </fieldset><br />';
@@ -325,7 +360,7 @@ class Blocktopmenu extends Module
<legend><img src="../img/admin/details.gif" alt="" title="" />'.$this->l('List Menu Top Link').'</legend> <legend><img src="../img/admin/details.gif" alt="" title="" />'.$this->l('List Menu Top Link').'</legend>
<table style="width:100%;"> <table style="width:100%;">
<thead> <thead>
<tr> <tr style="text-align: left;">
<th>'.$this->l('Id Link').'</th> <th>'.$this->l('Id Link').'</th>
<th>'.$this->l('Label').'</th> <th>'.$this->l('Label').'</th>
<th>'.$this->l('Link').'</th> <th>'.$this->l('Link').'</th>
@@ -334,23 +369,24 @@ class Blocktopmenu extends Module
</tr> </tr>
</thead> </thead>
<tbody>'; <tbody>';
foreach ($links as $link) foreach ($links as $link)
{ {
$this->_html .= ' $this->_html .= '
<tr> <tr>
<td>'.$link['id_linksmenutop'].'</td> <td>'.(int)$link['id_linksmenutop'].'</td>
<td>'.$link['label'].'</td> <td>'.Tools::safeOutput($link['label']).'</td>
<td><a href="'.$link['link'].'"'.(($link['new_window']) ? ' target="_blank"' : '').'>'.$link['link'].'</a></td> <td><a href="'.Tools::safeOutput($link['link']).'"'.(($link['new_window']) ? ' target="_blank"' : '').'>'.Tools::safeOutput($link['link']).'</a></td>
<td>'.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).'</td> <td>'.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).'</td>
<td> <td>
<form action="'.$_SERVER['REQUEST_URI'].'" method="post"> <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
<input type="hidden" name="id_linksmenutop" value="'.$link['id_linksmenutop'].'" /> <input type="hidden" name="id_linksmenutop" value="'.(int)$link['id_linksmenutop'].'" />
<input type="submit" name="submitBlocktopmenuEdit" value="'.$this->l('Edit').'" class="button" />
<input type="submit" name="submitBlocktopmenuRemove" value="'.$this->l('Remove').'" class="button" /> <input type="submit" name="submitBlocktopmenuRemove" value="'.$this->l('Remove').'" class="button" />
</form> </form>
</td> </td>
</tr>'; </tr>';
} }
$this->_html .= '</tbody> $this->_html .= '</tbody>
</table> </table>
</fieldset>'; </fieldset>';
return $this->_html; return $this->_html;
@@ -703,4 +739,4 @@ class Blocktopmenu extends Module
return Db::getInstance()->executeS($sql); return Db::getInstance()->executeS($sql);
} }
} }
+59 -6
View File
@@ -30,7 +30,7 @@ class MenuTopLinks
public static function gets($id_lang, $id_linksmenutop = null, $id_shop) public static function gets($id_lang, $id_linksmenutop = null, $id_shop)
{ {
return Db::getInstance()->executeS(' 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 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.') 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 WHERE 1
@@ -44,34 +44,87 @@ class MenuTopLinks
return self::gets($id_lang, $id_linksmenutop, $id_shop); 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) public static function add($link, $label, $newWindow = 0, $id_shop)
{ {
if(!is_array($label)) if(!is_array($label))
return false; return false;
if(!is_array($link))
return false;
Db::getInstance()->insert( Db::getInstance()->insert(
'linksmenutop', 'linksmenutop',
array( array(
'new_window'=>(int)$newWindow, 'new_window'=>(int)$newWindow,
'link'=>pSQL($link),
'id_shop' => (int)$id_shop 'id_shop' => (int)$id_shop
) )
); );
$id_linksmenutop = Db::getInstance()->Insert_ID(); $id_linksmenutop = Db::getInstance()->Insert_ID();
foreach($label as $id_lang=>$label) foreach ($label as $id_lang=>$label)
{
Db::getInstance()->insert( Db::getInstance()->insert(
'linksmenutop_lang', 'linksmenutop_lang',
array( array(
'id_linksmenutop'=>(int)$id_linksmenutop, 'id_linksmenutop'=>(int)$id_linksmenutop,
'id_lang'=>(int)$id_lang, 'id_lang'=>(int)$id_lang,
'id_shop'=>(int)$id_shop, '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) public static function remove($id_linksmenutop, $id_shop)
{ {
Db::getInstance()->delete('linksmenutop', 'id_linksmenutop = '.(int)$id_linksmenutop.' AND id_shop = '.(int)$id_shop); Db::getInstance()->delete('linksmenutop', 'id_linksmenutop = '.(int)$id_linksmenutop.' AND id_shop = '.(int)$id_shop);