VEKIA

block links prestashop with supported footer hook

Nov 11th, 2013
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.16 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2013 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to [email protected] so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <[email protected]>
  22. * @copyright 2007-2013 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26.  
  27. if (!defined('_PS_VERSION_'))
  28. exit;
  29.  
  30. class BlockLink extends Module
  31. {
  32. /* @var boolean error */
  33. protected $error = false;
  34.  
  35. public function __construct()
  36. {
  37. $this->name = 'blocklink';
  38. $this->tab = 'front_office_features';
  39. $this->version = '1.5';
  40. $this->author = 'PrestaShop';
  41. $this->need_instance = 0;
  42.  
  43. parent::__construct();
  44.  
  45. $this->displayName = $this->l('Link block');
  46. $this->description = $this->l('Adds a block with additional links.');
  47. $this->confirmUninstall = $this->l('Are you sure you want to delete all your links ?');
  48. }
  49.  
  50. public function install()
  51. {
  52. if (!parent::install() ||
  53. !$this->registerHook('leftColumn') || !$this->registerHook('header') ||
  54. !Db::getInstance()->execute('
  55. CREATE TABLE '._DB_PREFIX_.'blocklink (
  56. `id_blocklink` int(2) NOT NULL AUTO_INCREMENT,
  57. `url` varchar(255) NOT NULL,
  58. `new_window` TINYINT(1) NOT NULL,
  59. PRIMARY KEY(`id_blocklink`))
  60. ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') ||
  61. !Db::getInstance()->execute('
  62. CREATE TABLE '._DB_PREFIX_.'blocklink_shop (
  63. `id_blocklink` int(2) NOT NULL AUTO_INCREMENT,
  64. `id_shop` int(2) NOT NULL,
  65. PRIMARY KEY(`id_blocklink`, `id_shop`))
  66. ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') ||
  67. !Db::getInstance()->execute('
  68. CREATE TABLE '._DB_PREFIX_.'blocklink_lang (
  69. `id_blocklink` int(2) NOT NULL,
  70. `id_lang` int(2) NOT NULL,
  71. `text` varchar(64) NOT NULL,
  72. PRIMARY KEY(`id_blocklink`, `id_lang`))
  73. ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') ||
  74. !Configuration::updateValue('PS_BLOCKLINK_TITLE', array('1' => 'Block link', '2' => 'Bloc lien')))
  75. return false;
  76. return true;
  77. }
  78.  
  79. public function uninstall()
  80. {
  81. if (!parent::uninstall() ||
  82. !Db::getInstance()->execute('DROP TABLE '._DB_PREFIX_.'blocklink') ||
  83. !Db::getInstance()->execute('DROP TABLE '._DB_PREFIX_.'blocklink_lang') ||
  84. !Db::getInstance()->execute('DROP TABLE '._DB_PREFIX_.'blocklink_shop') ||
  85. !Configuration::deleteByName('PS_BLOCKLINK_TITLE') ||
  86. !Configuration::deleteByName('PS_BLOCKLINK_URL'))
  87. return false;
  88. return true;
  89. }
  90.  
  91. public function hookLeftColumn($params)
  92. {
  93. $links = $this->getLinks();
  94.  
  95. $this->smarty->assign(array(
  96. 'blocklink_links' => $links,
  97. 'title' => Configuration::get('PS_BLOCKLINK_TITLE', $this->context->language->id),
  98. 'url' => Configuration::get('PS_BLOCKLINK_URL'),
  99. 'lang' => 'text_'.$this->context->language->id
  100. ));
  101. if (!$links)
  102. return false;
  103. return $this->display(__FILE__, 'blocklink.tpl');
  104. }
  105.  
  106. public function hookRightColumn($params)
  107. {
  108. return $this->hookLeftColumn($params);
  109. }
  110. public function hookFooter($params)
  111. {
  112. return $this->hookLeftColumn($params);
  113. }
  114.  
  115. public function hookHeader($params)
  116. {
  117. $this->context->controller->addCSS($this->_path.'blocklink.css', 'all');
  118. }
  119.  
  120. public function getLinks()
  121. {
  122. $result = array();
  123. // Get id and url
  124.  
  125. $sql = 'SELECT b.`id_blocklink`, b.`url`, b.`new_window`
  126. FROM `'._DB_PREFIX_.'blocklink` b';
  127. if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL)
  128. $sql .= ' JOIN `'._DB_PREFIX_.'blocklink_shop` bs ON b.`id_blocklink` = bs.`id_blocklink` AND bs.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).') ';
  129. $sql .= (int)Configuration::get('PS_BLOCKLINK_ORDERWAY') == 1 ? ' ORDER BY `id_blocklink` DESC' : '';
  130.  
  131. if (!$links = Db::getInstance()->executeS($sql))
  132. return false;
  133. $i = 0;
  134. foreach ($links as $link)
  135. {
  136. $result[$i]['id'] = $link['id_blocklink'];
  137. $result[$i]['url'] = $link['url'];
  138. $result[$i]['newWindow'] = $link['new_window'];
  139. // Get multilingual text
  140. if (!$texts = Db::getInstance()->executeS('SELECT `id_lang`, `text`
  141. FROM '._DB_PREFIX_.'blocklink_lang
  142. WHERE `id_blocklink`='.(int)$link['id_blocklink']))
  143. return false;
  144. foreach ($texts as $text)
  145. $result[$i]['text_'.$text['id_lang']] = $text['text'];
  146. $i++;
  147. }
  148. return $result;
  149. }
  150.  
  151. public function addLink()
  152. {
  153. if (!($languages = Language::getLanguages()))
  154. return false;
  155. $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
  156.  
  157. if ($id_link = Tools::getValue('id_link'))
  158. {
  159. 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))
  160. return false;
  161. if (!Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink` = '.(int)$id_link))
  162. return false;
  163.  
  164. foreach ($languages as $language)
  165. if (!empty($_POST['text_'.$language['id_lang']]))
  166. {
  167. if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')'))
  168. return false;
  169. }
  170. else
  171. if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.$language['id_lang'].', \''.pSQL($_POST['text_'.$id_lang_default]).'\')'))
  172. return false;
  173. }
  174. else
  175. {
  176. if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink
  177. VALUES (NULL, \''.pSQL($_POST['url']).'\', '.((isset($_POST['newWindow']) && $_POST['newWindow']) == 'on' ? 1 : 0).')') ||
  178. !$id_link = Db::getInstance()->Insert_ID())
  179. return false;
  180.  
  181. foreach ($languages as $language)
  182. if (!empty($_POST['text_'.$language['id_lang']]))
  183. {
  184. if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang
  185. VALUES ('.(int)$id_link.', '.(int)$language['id_lang'].', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')'))
  186. return false;
  187. }
  188. else
  189. if (!Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)$id_link.', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$id_lang_default]).'\')'))
  190. return false;
  191. }
  192.  
  193. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_shop WHERE id_blocklink='.(int)$id_link);
  194.  
  195. if (!Shop::isFeatureActive())
  196. {
  197. Db::getInstance()->insert('blocklink_shop', array(
  198. 'id_blocklink' => (int)$id_link,
  199. 'id_shop' => (int)Context::getContext()->shop->id,
  200. ));
  201. }
  202. else
  203. {
  204. $assos_shop = Tools::getValue('checkBoxShopAsso_blocklink');
  205. if (empty($assos_shop))
  206. return false;
  207. foreach ($assos_shop as $id_shop => $row)
  208. Db::getInstance()->insert('blocklink_shop', array(
  209. 'id_blocklink' => (int)$id_link,
  210. 'id_shop' => (int)$id_shop,
  211. ));
  212. }
  213. return true;
  214. }
  215.  
  216. public function deleteLink()
  217. {
  218. return (Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink WHERE `id_blocklink` = '.(int)$_GET['id']) &&
  219. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_shop WHERE `id_blocklink` = '.(int)$_GET['id']) &&
  220. Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink` = '.(int)$_GET['id']));
  221. }
  222.  
  223. public function updateTitle()
  224. {
  225. $languages = Language::getLanguages();
  226. $result = array();
  227. foreach ($languages as $language)
  228. $result[$language['id_lang']] = $_POST['title_'.$language['id_lang']];
  229. if (!Configuration::updateValue('PS_BLOCKLINK_TITLE', $result))
  230. return false;
  231. return Configuration::updateValue('PS_BLOCKLINK_URL', $_POST['title_url']);
  232. }
  233.  
  234. public function getContent()
  235. {
  236. $this->_html = '<h2>'.$this->displayName.'</h2>
  237. <script type="text/javascript" src="'.$this->_path.'blocklink.js"></script>';
  238.  
  239. // Add a link
  240. if (isset($_POST['submitLinkAdd']))
  241. {
  242. if (empty($_POST['text_'.Configuration::get('PS_LANG_DEFAULT')]) || empty($_POST['url']))
  243. $this->_html .= $this->displayError($this->l('You must fill in all fields'));
  244. elseif (!Validate::isUrl(str_replace('http://', '', $_POST['url'])))
  245. $this->_html .= $this->displayError($this->l('Bad URL'));
  246. else
  247. if ($this->addLink())
  248. $this->_html .= $this->displayConfirmation($this->l('The link has been added.'));
  249. else
  250. $this->_html .= $this->displayError($this->l('An error occurred during link creation.'));
  251. }
  252. // Update the block title
  253. elseif (isset($_POST['submitTitle']))
  254. {
  255.  
  256. if (empty($_POST['title_'.Configuration::get('PS_LANG_DEFAULT')]))
  257. $this->_html .= $this->displayError($this->l('"title" field cannot be empty.'));
  258. elseif (!empty($_POST['title_url']) && !Validate::isUrl(str_replace('http://', '', $_POST['title_url'])))
  259. $this->_html .= $this->displayError($this->l('The \'title\' field is invalid'));
  260. elseif (!Validate::isGenericName($_POST['title_'.Configuration::get('PS_LANG_DEFAULT')]))
  261. $this->_html .= $this->displayError($this->l('The \'title\' field is invalid'));
  262. elseif (!$this->updateTitle())
  263. $this->_html .= $this->displayError($this->l('An error occurred during title updating.'));
  264. else
  265. $this->_html .= $this->displayConfirmation($this->l('The block title has been updated.'));
  266. }
  267. // Delete a link
  268. elseif (Tools::getValue('delete_link') && isset($_GET['id']))
  269. {
  270.  
  271. if (!is_numeric($_GET['id']) || !$this->deleteLink())
  272. $this->_html .= $this->displayError($this->l('An error occurred during link deletion.'));
  273. else
  274. $this->_html .= $this->displayConfirmation($this->l('The link has been deleted.'));
  275. }
  276.  
  277. if (isset($_POST['submitOrderWay']))
  278. {
  279. if (Configuration::updateValue('PS_BLOCKLINK_ORDERWAY', (int)(Tools::getValue('orderWay'))))
  280. $this->_html .= $this->displayConfirmation($this->l('Sort order updated'));
  281. else
  282. $this->_html .= $this->displayError($this->l('An error occurred during sort order set-up.'));
  283. }
  284.  
  285. $this->_displayForm();
  286. $this->_list();
  287.  
  288. return $this->_html;
  289. }
  290.  
  291. private function _displayForm()
  292. {
  293. /* Language */
  294. $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
  295. $languages = Language::getLanguages(false);
  296. $divLangName = 'text¤title';
  297. /* Title */
  298. $title_url = Configuration::get('PS_BLOCKLINK_URL');
  299. if (!Tools::isSubmit('submitLinkAdd'))
  300. {
  301. if ($id_link = (int)Tools::getValue('id_link'))
  302. {
  303. $res = Db::getInstance()->executeS('
  304. SELECT *
  305. FROM '._DB_PREFIX_.'blocklink b
  306. LEFT JOIN '._DB_PREFIX_.'blocklink_lang bl ON (b.id_blocklink = bl.id_blocklink)
  307. WHERE b.id_blocklink='.(int)$id_link);
  308. if ($res)
  309. foreach ($res as $row)
  310. {
  311. $links['text'][(int)$row['id_lang']] = $row['text'];
  312. $links['url'] = $row['url'];
  313. $links['new_window'] = $row['new_window'];
  314. }
  315. }
  316. }
  317. $this->_html .= '
  318. <script type="text/javascript">
  319. id_language = Number('.(int)$id_lang_default.');
  320. </script>
  321. <fieldset>
  322. <legend><img src="'.$this->_path.'add.png" alt="" title="" /> '.$this->l('Add a new link').'</legend>
  323. <form method="post" action="index.php?controller=adminmodules&configure='.Tools::safeOutput(Tools::getValue('configure')).'&token='.Tools::safeOutput(Tools::getValue('token')).'&tab_module='.Tools::safeOutput(Tools::getValue('tab_module')).'&module_name='.Tools::safeOutput(Tools::getValue('module_name')).'">
  324. <input type="hidden" name="id_link" value="'.(int)Tools::getValue('id_link').'" />
  325. <label>'.$this->l('Text:').'</label>
  326. <div class="margin-form">';
  327. foreach ($languages as $language)
  328. $this->_html .= '
  329. <div id="text_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').'; float: left;">
  330. <input type="text" name="text_'.$language['id_lang'].'" id="textInput_'.$language['id_lang'].'" value="'.((isset($links) && isset($links['text'][$language['id_lang']])) ? $links['text'][$language['id_lang']] : '').'" /><sup> *</sup>
  331. </div>';
  332. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'text', true);
  333. $this->_html .= '
  334. <div class="clear"></div>
  335. </div>
  336. <label>'.$this->l('URL:').'</label>
  337. <div class="margin-form"><input type="text" name="url" id="url" value="'.(isset($links) && isset($links['url']) ? Tools::safeOutput($links['url']) : '').'" /><sup> *</sup></div>
  338. <label>'.$this->l('Open in a new window:').'</label>
  339. <div class="margin-form"><input type="checkbox" name="newWindow" id="newWindow" '.((isset($links) && $links['new_window']) ? 'checked="checked"' : '').' /></div>';
  340. $shops = Shop::getShops(true, null, true);
  341. if (Shop::isFeatureActive() && count($shops) > 1)
  342. {
  343. $helper = new HelperForm();
  344. $helper->id = (int)Tools::getValue('id_link');
  345. $helper->table = 'blocklink';
  346. $helper->identifier = 'id_blocklink';
  347.  
  348. $this->_html .= '<label for="shop_association">'.$this->l('Shop association:').'</label><div id="shop_association" class="margin-form">'.$helper->renderAssoShop().'</div>';
  349. }
  350. $this->_html .= '
  351. <div class="margin-form">
  352. <input type="submit" class="button" name="submitLinkAdd" value="'.$this->l('Add this link').'" />
  353. </div>
  354. </form>
  355. </fieldset>
  356. <fieldset class="space">
  357. <legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->l('Block title').'</legend>
  358. <form method="post" action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'">
  359. <label>'.$this->l('Block title:').'</label>
  360. <div class="margin-form">';
  361. foreach ($languages as $language)
  362. $this->_html .= '
  363. <div id="title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').'; float: left;">
  364. <input type="text" name="title_'.$language['id_lang'].'" value="'.Tools::safeOutput(($this->error && isset($_POST['title'])) ? $_POST['title'] : Configuration::get('PS_BLOCKLINK_TITLE', $language['id_lang'])).'" /><sup> *</sup>
  365. </div>';
  366. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'title', true);
  367. $this->_html .= '
  368. <div class="clear"></div>
  369. </div>
  370. <label>'.$this->l('Block URL:').'</label>
  371. <div class="margin-form"><input type="text" name="title_url" value="'.Tools::safeOutput(($this->error && isset($_POST['title_url'])) ? $_POST['title_url'] : $title_url).'" /></div>
  372. <div class="margin-form"><input type="submit" class="button" name="submitTitle" value="'.$this->l('Update').'" /></div>
  373. </form>
  374. </fieldset>
  375. <fieldset class="space">
  376. <legend><img src="'.$this->_path.'prefs.gif" alt="" title="" /> '.$this->l('Settings').'</legend>
  377. <form method="post" action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'">
  378. <label>'.$this->l('Order list:').'</label>
  379. <div class="margin-form">
  380. <select name="orderWay">
  381. <option value="0"'.(!Configuration::get('PS_BLOCKLINK_ORDERWAY') ? 'selected="selected"' : '').'>'.$this->l('by most recent links').'</option>
  382. <option value="1"'.(Configuration::get('PS_BLOCKLINK_ORDERWAY') ? 'selected="selected"' : '').'>'.$this->l('by oldest links').'</option>
  383. </select>
  384. </div>
  385. <div class="margin-form"><input type="submit" class="button" name="submitOrderWay" value="'.$this->l('Update').'" /></div>
  386. </form>
  387. </fieldset>';
  388. }
  389.  
  390. private function _list()
  391. {
  392. $links = $this->getLinks();
  393. $languages = Language::getLanguages();
  394. $token = Tools::safeOutput(Tools::getValue('token'));
  395. if (!Validate::isCleanHtml($token))
  396. $token = '';
  397. if ($links)
  398. {
  399. $this->_html .= '
  400. <script type="text/javascript">
  401. var currentUrl = \''.Tools::safeOutput($_SERVER['REQUEST_URI']).'\';
  402. var token=\''.$token.'\';
  403. var links = new Array();';
  404. foreach ($links as $link)
  405. {
  406. $this->_html .= 'links['.$link['id'].'] = new Array(\''.addslashes($link['url']).'\', '.$link['newWindow'];
  407. foreach ($languages as $language)
  408. if (isset($link['text_'.$language['id_lang']]))
  409. $this->_html .= ', \''.addslashes($link['text_'.$language['id_lang']]).'\'';
  410. else
  411. $this->_html .= ', \'\'';
  412. $this->_html .= ');';
  413. }
  414. $this->_html .= '</script>';
  415. }
  416. $this->_html .= '
  417. <h3 class="blue space">'.$this->l('Link list').'</h3>
  418. <table class="table">
  419. <tr>
  420. <th>'.$this->l('ID').'</th>
  421. <th>'.$this->l('Text').'</th>
  422. <th>'.$this->l('URL').'</th>
  423. <th>'.$this->l('Actions').'</th>
  424. </tr>';
  425.  
  426. if (!$links)
  427. $this->_html .= '
  428. <tr>
  429. <td colspan="3">'.$this->l('There are no links.').'</td>
  430. </tr>';
  431. else
  432. foreach ($links as $link)
  433. $this->_html .= '
  434. <tr>
  435. <td>'.(int)$link['id'].'</td>
  436. <td>'.Tools::safeOutput($link['text_'.$this->context->language->id]).'</td>
  437. <td>'.Tools::safeOutput($link['url']).'</td>
  438. <td>
  439. <a href="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'&id_link='.(int)$link['id'].'"><img src="../img/admin/edit.gif" alt="" title="" style="cursor: pointer" /></a>
  440. <a href="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'&id='.(int)$link['id'].'&delete_link=1"><img src="../img/admin/delete.gif" alt="" title="" style="cursor: pointer" /></a>
  441. </td>
  442. </tr>';
  443. $i = 0;
  444. $nb = count($languages);
  445. $idLng = 0;
  446. while ($i < $nb)
  447. {
  448. if ($languages[$i]['id_lang'] == (int)Configuration::get('PS_LANG_DEFAULT'))
  449. $idLng = $i;
  450. $i++;
  451. }
  452. $this->_html .= '
  453. </table>
  454. <input type="hidden" id="languageFirst" value="'.(int)$languages[0]['id_lang'].'" />
  455. <input type="hidden" id="languageNb" value="'.count($languages).'" />';
  456. }
  457. }
Advertisement
Add Comment
Please, Sign In to add comment