* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7077 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; class BlockLink extends Module { /* @var boolean error */ protected $error = false; public function __construct() { $this->name = 'blocklink'; $this->tab = 'front_office_features'; $this->version = '1.5'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Link block'); $this->description = $this->l('Adds a block with additional links.'); $this->confirmUninstall = $this->l('Are you sure you want to delete all your links ?'); } public function install() { if (!parent::install() || !$this->registerHook('leftColumn') || !Db::getInstance()->execute(' CREATE TABLE '._DB_PREFIX_.'blocklink ( `id_blocklink` int(2) NOT NULL AUTO_INCREMENT, `url` varchar(255) NOT NULL, `new_window` TINYINT(1) NOT NULL, PRIMARY KEY(`id_blocklink`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') || !Db::getInstance()->execute(' CREATE TABLE '._DB_PREFIX_.'blocklink_shop ( `id_blocklink` int(2) NOT NULL AUTO_INCREMENT, `id_shop` varchar(255) NOT NULL, PRIMARY KEY(`id_blocklink`, `id_shop`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') || !Db::getInstance()->execute(' CREATE TABLE '._DB_PREFIX_.'blocklink_lang ( `id_blocklink` int(2) NOT NULL, `id_lang` int(2) NOT NULL, `text` varchar(64) NOT NULL, PRIMARY KEY(`id_blocklink`, `id_lang`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') || !Configuration::updateValue('PS_BLOCKLINK_TITLE', array('1' => 'Block link', '2' => 'Bloc lien'))) return false; return true; } public function uninstall() { if (!parent::uninstall() || !Db::getInstance()->execute('DROP TABLE '._DB_PREFIX_.'blocklink') || !Db::getInstance()->execute('DROP TABLE '._DB_PREFIX_.'blocklink_lang') || !Db::getInstance()->execute('DROP TABLE '._DB_PREFIX_.'blocklink_shop') || !Configuration::deleteByName('PS_BLOCKLINK_TITLE') || !Configuration::deleteByName('PS_BLOCKLINK_URL')) return false; return true; } public function hookLeftColumn($params) { $links = $this->getLinks(); $this->smarty->assign(array( 'blocklink_links' => $links, 'title' => Configuration::get('PS_BLOCKLINK_TITLE', $this->context->language->id), 'url' => Configuration::get('PS_BLOCKLINK_URL'), 'lang' => 'text_'.$this->context->language->id )); if (!$links) return false; return $this->display(__FILE__, 'blocklink.tpl'); } public function hookRightColumn($params) { return $this->hookLeftColumn($params); } public function getLinks() { $result = array(); // Get id and url if (!$links = Db::getInstance()->executeS('SELECT `id_blocklink`, `url`, `new_window` FROM '._DB_PREFIX_.'blocklink'. ((int)Configuration::get('PS_BLOCKLINK_ORDERWAY') == 1 ? ' ORDER BY `id_blocklink` DESC' : ''))) return false; $i = 0; foreach ($links as $link) { $result[$i]['id'] = $link['id_blocklink']; $result[$i]['url'] = $link['url']; $result[$i]['newWindow'] = $link['new_window']; // Get multilingual text if (!$texts = Db::getInstance()->executeS('SELECT `id_lang`, `text` FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink`='.(int)$link['id_blocklink'])) return false; foreach ($texts as $text) $result[$i]['text_'.$text['id_lang']] = $text['text']; $i++; } return $result; } public function addLink() { if ($id_link = Tools::getValue('id_link')) { // Url registration if (!Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'blocklink SET `url`=\''.pSQL($_POST['url']).'\', `new_window`='.(isset($_POST['newWindow']) ? 1 : 0).' WHERE `id_blocklink`='.(int)$id_link)) return false; // Multilingual text $languages = Language::getLanguages(); $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT')); if (!$languages) return false; if (!Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink` = '.(int)$id_link)) return false; foreach ($languages as $language) if (!empty($_POST['text_'.$language['id_lang']])) { if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')')) return false; } else if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.$language['id_lang'].', \''.pSQL($_POST['text_'.$defaultLanguage]).'\')')) return false; } else { // Url registration if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink VALUES (NULL, \''.pSQL($_POST['url']).'\', '.((isset($_POST['newWindow']) && $_POST['newWindow']) == 'on' ? 1 : 0).')') || !$id_link = Db::getInstance()->Insert_ID()) return false; // Multilingual text $languages = Language::getLanguages(); $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT')); if (!$languages) return false; foreach ($languages as $language) if (!empty($_POST['text_'.$language['id_lang']])) { if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.(int)$language['id_lang'].', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')')) return false; } else if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$defaultLanguage]).'\')')) return false; } $assos_shop = Tools::getValue('checkBoxShopAsso_blocklink'); Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocklink_shop WHERE id_blocklink='.(int)$id_link); foreach ($assos_shop as $asso) foreach ($asso as $id_shop => $row) Db::getInstance()->insert('blocklink_shop', array( 'id_blocklink' => (int)$id_link, 'id_shop' => (int)$id_shop, )); return true; } public function deleteLink() { return (Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink WHERE `id_blocklink`='.(int)($_GET['id'])) && Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_shop WHERE `id_blocklink`='.(int)($_GET['id'])) && Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink`='.(int)($_GET['id']))); } public function updateTitle() { $languages = Language::getLanguages(); $result = array(); foreach ($languages as $language) $result[$language['id_lang']] = $_POST['title_'.$language['id_lang']]; if (!Configuration::updateValue('PS_BLOCKLINK_TITLE', $result)) return false; return Configuration::updateValue('PS_BLOCKLINK_URL', $_POST['title_url']); } public function getContent() { $this->_html = '
| '.$this->l('ID').' | '.$this->l('Text').' | '.$this->l('URL').' | '.$this->l('Actions').' |
|---|---|---|---|
| '.$this->l('There are no links.').' | |||
| '.$link['id'].' | '.$link['text_'.$this->context->language->id].' | '.$link['url'].' |
|