PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/blocklink/blocklink.php

http://marocmall.googlecode.com/
PHP | 387 lines | 325 code | 24 blank | 38 comment | 34 complexity | 6cec5ddeec50f8ab512221813b24c675 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 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 license@prestashop.com 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 <contact@prestashop.com>
  22. * @copyright 2007-2011 PrestaShop SA
  23. * @version Release: $Revision: 6594 $
  24. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. if (!defined('_CAN_LOAD_FILES_'))
  28. exit;
  29. class BlockLink extends Module
  30. {
  31. /* @var boolean error */
  32. protected $error = false;
  33. public function __construct()
  34. {
  35. $this->name = 'blocklink';
  36. $this->tab = 'front_office_features';
  37. $this->version = '1.4';
  38. $this->author = 'PrestaShop';
  39. $this->need_instance = 0;
  40. parent::__construct();
  41. $this->displayName = $this->l('Link block');
  42. $this->description = $this->l('Adds a block with additional links.');
  43. $this->confirmUninstall = $this->l('Are you sure you want to delete all your links ?');
  44. }
  45. public function install()
  46. {
  47. if (!parent::install() OR
  48. !$this->registerHook('leftColumn') OR
  49. !Db::getInstance()->Execute('
  50. CREATE TABLE '._DB_PREFIX_.'blocklink (
  51. `id_blocklink` int(2) NOT NULL AUTO_INCREMENT,
  52. `url` varchar(255) NOT NULL,
  53. `new_window` TINYINT(1) NOT NULL,
  54. PRIMARY KEY(`id_blocklink`))
  55. ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') OR
  56. !Db::getInstance()->Execute('
  57. CREATE TABLE '._DB_PREFIX_.'blocklink_lang (
  58. `id_blocklink` int(2) NOT NULL,
  59. `id_lang` int(2) NOT NULL,
  60. `text` varchar(64) NOT NULL,
  61. PRIMARY KEY(`id_blocklink`, `id_lang`))
  62. ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') OR
  63. !Configuration::updateValue('PS_BLOCKLINK_TITLE', array('1' => 'Block link', '2' => 'Bloc lien')))
  64. return false;
  65. return true;
  66. }
  67. public function uninstall()
  68. {
  69. if (!parent::uninstall() OR
  70. !Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'blocklink') OR
  71. !Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'blocklink_lang') OR
  72. !Configuration::deleteByName('PS_BLOCKLINK_TITLE') OR
  73. !Configuration::deleteByName('PS_BLOCKLINK_URL'))
  74. return false;
  75. return true;
  76. }
  77. public function hookLeftColumn($params)
  78. {
  79. global $cookie, $smarty;
  80. $links = $this->getLinks();
  81. $smarty->assign(array(
  82. 'blocklink_links' => $links,
  83. 'title' => Configuration::get('PS_BLOCKLINK_TITLE', $cookie->id_lang),
  84. 'url' => Configuration::get('PS_BLOCKLINK_URL'),
  85. 'lang' => 'text_'.$cookie->id_lang
  86. ));
  87. if (!$links)
  88. return false;
  89. return $this->display(__FILE__, 'blocklink.tpl');
  90. }
  91. public function hookRightColumn($params)
  92. {
  93. return $this->hookLeftColumn($params);
  94. }
  95. public function getLinks()
  96. {
  97. $result = array();
  98. /* Get id and url */
  99. 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' : '')))
  100. return false;
  101. $i = 0;
  102. foreach ($links AS $link)
  103. {
  104. $result[$i]['id'] = $link['id_blocklink'];
  105. $result[$i]['url'] = $link['url'];
  106. $result[$i]['newWindow'] = $link['new_window'];
  107. /* Get multilingual text */
  108. if (!$texts = Db::getInstance()->ExecuteS('SELECT `id_lang`, `text` FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink`='.(int)($link['id_blocklink'])))
  109. return false;
  110. foreach ($texts AS $text)
  111. $result[$i]['text_'.$text['id_lang']] = $text['text'];
  112. $i++;
  113. }
  114. return $result;
  115. }
  116. public function addLink()
  117. {
  118. /* Url registration */
  119. if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocklink VALUES (NULL, \''.pSQL($_POST['url']).'\', '.((isset($_POST['newWindow']) AND $_POST['newWindow']) == 'on' ? 1 : 0).')') OR !$lastId = mysql_insert_id())
  120. return false;
  121. /* Multilingual text */
  122. $languages = Language::getLanguages();
  123. $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
  124. if (!$languages)
  125. return false;
  126. foreach ($languages AS $language)
  127. if (!empty($_POST['text_'.$language['id_lang']]))
  128. {
  129. if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)($lastId).', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')'))
  130. return false;
  131. }
  132. else
  133. if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)($lastId).', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$defaultLanguage]).'\')'))
  134. return false;
  135. return true;
  136. }
  137. public function updateLink()
  138. {
  139. /* Url registration */
  140. if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'blocklink SET `url`=\''.pSQL($_POST['url']).'\', `new_window`='.(isset($_POST['newWindow']) ? 1 : 0).' WHERE `id_blocklink`='.(int)($_POST['id'])))
  141. return false;
  142. /* Multilingual text */
  143. $languages = Language::getLanguages();
  144. $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
  145. if (!$languages)
  146. return false;
  147. if (!Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink` = '.(int)($_POST['id'])))
  148. return false ;
  149. foreach ($languages AS $language)
  150. if (!empty($_POST['text_'.$language['id_lang']]))
  151. {
  152. if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)($_POST['id']).', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')'))
  153. return false;
  154. }
  155. else
  156. if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocklink_lang VALUES ('.(int)($_POST['id']).', '.$language['id_lang'].', \''.pSQL($_POST['text_'.$defaultLanguage]).'\')'))
  157. return false;
  158. return true;
  159. }
  160. public function deleteLink()
  161. {
  162. return (Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocklink WHERE `id_blocklink`='.(int)($_GET['id'])) AND Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocklink_lang WHERE `id_blocklink`='.(int)($_GET['id'])));
  163. }
  164. public function updateTitle()
  165. {
  166. $languages = Language::getLanguages();
  167. $result = array();
  168. foreach ($languages AS $language)
  169. $result[$language['id_lang']] = $_POST['title_'.$language['id_lang']];
  170. if (!Configuration::updateValue('PS_BLOCKLINK_TITLE', $result))
  171. return false;
  172. return Configuration::updateValue('PS_BLOCKLINK_URL', $_POST['title_url']);
  173. }
  174. public function getContent()
  175. {
  176. $this->_html = '<h2>'.$this->displayName.'</h2>
  177. <script type="text/javascript" src="'.$this->_path.'blocklink.js"></script>';
  178. /* Add a link */
  179. if (isset($_POST['submitLinkAdd']))
  180. {
  181. if (empty($_POST['text_'.Configuration::get('PS_LANG_DEFAULT')]) OR empty($_POST['url']))
  182. $this->_html .= $this->displayError($this->l('You must fill in all fields'));
  183. elseif (!Validate::isUrl(str_replace('http://', '', $_POST['url'])))
  184. $this->_html .= $this->displayError($this->l('Bad URL'));
  185. else
  186. if ($this->addLink())
  187. $this->_html .= $this->displayConfirmation($this->l('The link has been added.'));
  188. else
  189. $this->_html .= $this->displayError($this->l('An error occurred during link creation.'));
  190. }
  191. /* Update a link */
  192. elseif (isset($_POST['submitLinkUpdate']))
  193. {
  194. if (empty($_POST['text_'.Configuration::get('PS_LANG_DEFAULT')]) OR empty($_POST['url']))
  195. $this->_html .= $this->displayError($this->l('You must fill in all fields'));
  196. elseif (!Validate::isUrl(str_replace('http://', '', $_POST['url'])))
  197. $this->_html .= $this->displayError($this->l('Bad URL'));
  198. else
  199. if (empty($_POST['id']) OR !is_numeric($_POST['id']) OR !$this->updateLink())
  200. $this->_html .= $this->displayError($this->l('An error occurred during link updating.'));
  201. else
  202. $this->_html .= $this->displayConfirmation($this->l('The link has been updated.'));
  203. }
  204. /* Update the block title */
  205. elseif (isset($_POST['submitTitle']))
  206. {
  207. if (empty($_POST['title_'.Configuration::get('PS_LANG_DEFAULT')]))
  208. $this->_html .= $this->displayError($this->l('"title" field cannot be empty.'));
  209. elseif (!empty($_POST['title_url']) AND !Validate::isUrl(str_replace('http://', '', $_POST['title_url'])))
  210. $this->_html .= $this->displayError($this->l('The \'title\' field is invalid'));
  211. elseif (!Validate::isGenericName($_POST['title_'.Configuration::get('PS_LANG_DEFAULT')]))
  212. $this->_html .= $this->displayError($this->l('The \'title\' field is invalid'));
  213. elseif (!$this->updateTitle())
  214. $this->_html .= $this->displayError($this->l('An error occurred during title updating.'));
  215. else
  216. $this->_html .= $this->displayConfirmation($this->l('The block title has been updated.'));
  217. }
  218. /* Delete a link*/
  219. elseif (isset($_GET['id']))
  220. {
  221. if (!is_numeric($_GET['id']) OR !$this->deleteLink())
  222. $this->_html .= $this->displayError($this->l('An error occurred during link deletion.'));
  223. else
  224. $this->_html .= $this->displayConfirmation($this->l('The link has been deleted.'));
  225. }
  226. if (isset($_POST['submitOrderWay']))
  227. {
  228. if (Configuration::updateValue('PS_BLOCKLINK_ORDERWAY', (int)(Tools::getValue('orderWay'))))
  229. $this->_html .= $this->displayConfirmation($this->l('Sort order updated'));
  230. else
  231. $this->_html .= $this->displayError($this->l('An error occurred during sort order set-up.'));
  232. }
  233. $this->_displayForm();
  234. $this->_list();
  235. return $this->_html;
  236. }
  237. private function _displayForm()
  238. {
  239. global $cookie;
  240. /* Language */
  241. $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
  242. $languages = Language::getLanguages(false);
  243. $divLangName = 'text?title';
  244. /* Title */
  245. $title_url = Configuration::get('PS_BLOCKLINK_URL');
  246. $this->_html .= '
  247. <script type="text/javascript">
  248. id_language = Number('.$defaultLanguage.');
  249. </script>
  250. <fieldset>
  251. <legend><img src="'.$this->_path.'add.png" alt="" title="" /> '.$this->l('Add a new link').'</legend>
  252. <form method="post" action="'.$_SERVER['REQUEST_URI'].'">
  253. <label>'.$this->l('Text:').'</label>
  254. <div class="margin-form">';
  255. foreach ($languages as $language)
  256. $this->_html .= '
  257. <div id="text_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
  258. <input type="text" name="text_'.$language['id_lang'].'" id="textInput_'.$language['id_lang'].'" value="'.(($this->error AND isset($_POST['text_'.$language['id_lang']])) ? $_POST['text_'.$language['id_lang']] : '').'" /><sup> *</sup>
  259. </div>';
  260. $this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'text', true);
  261. $this->_html .= '
  262. <div class="clear"></div>
  263. </div>
  264. <label>'.$this->l('URL:').'</label>
  265. <div class="margin-form"><input type="text" name="url" id="url" value="'.(($this->error AND isset($_POST['url'])) ? $_POST['url'] : '').'" /><sup> *</sup></div>
  266. <label>'.$this->l('Open in a new window:').'</label>
  267. <div class="margin-form"><input type="checkbox" name="newWindow" id="newWindow" '.(($this->error AND isset($_POST['newWindow'])) ? 'checked="checked"' : '').' /></div>
  268. <div class="margin-form">
  269. <input type="hidden" name="id" id="id" value="'.($this->error AND isset($_POST['id']) ? $_POST['id'] : '').'" />
  270. <input type="submit" class="button" name="submitLinkAdd" value="'.$this->l('Add this link').'" />
  271. <input type="submit" class="button disable" name="submitLinkUpdate" value="'.$this->l('Edit this link').'" disabled="disbaled" id="submitLinkUpdate" />
  272. </div>
  273. </form>
  274. </fieldset>
  275. <fieldset class="space">
  276. <legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->l('Block title').'</legend>
  277. <form method="post" action="'.$_SERVER['REQUEST_URI'].'">
  278. <label>'.$this->l('Block title:').'</label>
  279. <div class="margin-form">';
  280. foreach ($languages as $language)
  281. $this->_html .= '
  282. <div id="title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
  283. <input type="text" name="title_'.$language['id_lang'].'" value="'.(($this->error AND isset($_POST['title'])) ? $_POST['title'] : Configuration::get('PS_BLOCKLINK_TITLE', $language['id_lang'])).'" /><sup> *</sup>
  284. </div>';
  285. $this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'title', true);
  286. $this->_html .= '
  287. <div class="clear"></div>
  288. </div>
  289. <label>'.$this->l('Block URL:').'</label>
  290. <div class="margin-form"><input type="text" name="title_url" value="'.(($this->error AND isset($_POST['title_url'])) ? $_POST['title_url'] : $title_url).'" /></div>
  291. <div class="margin-form"><input type="submit" class="button" name="submitTitle" value="'.$this->l('Update').'" /></div>
  292. </form>
  293. </fieldset>
  294. <fieldset class="space">
  295. <legend><img src="'.$this->_path.'prefs.gif" alt="" title="" /> '.$this->l('Settings').'</legend>
  296. <form method="post" action="'.$_SERVER['REQUEST_URI'].'">
  297. <label>'.$this->l('Order list:').'</label>
  298. <div class="margin-form">
  299. <select name="orderWay">
  300. <option value="0"'.(!Configuration::get('PS_BLOCKLINK_ORDERWAY') ? 'selected="selected"' : '').'>'.$this->l('by most recent links').'</option>
  301. <option value="1"'.(Configuration::get('PS_BLOCKLINK_ORDERWAY') ? 'selected="selected"' : '').'>'.$this->l('by oldest links').'</option>
  302. </select>
  303. </div>
  304. <div class="margin-form"><input type="submit" class="button" name="submitOrderWay" value="'.$this->l('Update').'" /></div>
  305. </form>
  306. </fieldset>';
  307. }
  308. private function _list()
  309. {
  310. $links = $this->getLinks();
  311. global $currentIndex, $cookie, $adminObj;
  312. $languages = Language::getLanguages();
  313. if ($links)
  314. {
  315. $this->_html .= '
  316. <script type="text/javascript">
  317. var currentUrl = \''.$currentIndex.'&configure='.$this->name.'\';
  318. var token=\''.$adminObj->token.'\';
  319. var links = new Array();';
  320. foreach ($links AS $link)
  321. {
  322. $this->_html .= 'links['.$link['id'].'] = new Array(\''.addslashes($link['url']).'\', '.$link['newWindow'];
  323. foreach ($languages AS $language)
  324. if (isset($link['text_'.$language['id_lang']]))
  325. $this->_html .= ', \''.addslashes($link['text_'.$language['id_lang']]).'\'';
  326. else
  327. $this->_html .= ', \'\'';
  328. $this->_html .= ');';
  329. }
  330. $this->_html .= '</script>';
  331. }
  332. $this->_html .= '
  333. <h3 class="blue space">'.$this->l('Link list').'</h3>
  334. <table class="table">
  335. <tr>
  336. <th>'.$this->l('ID').'</th>
  337. <th>'.$this->l('Text').'</th>
  338. <th>'.$this->l('URL').'</th>
  339. <th>'.$this->l('Actions').'</th>
  340. </tr>';
  341. if (!$links)
  342. $this->_html .= '
  343. <tr>
  344. <td colspan="3">'.$this->l('There are no links.').'</td>
  345. </tr>';
  346. else
  347. foreach ($links AS $link)
  348. $this->_html .= '
  349. <tr>
  350. <td>'.$link['id'].'</td>
  351. <td>'.$link['text_'.$cookie->id_lang].'</td>
  352. <td>'.$link['url'].'</td>
  353. <td>
  354. <img src="../img/admin/edit.gif" alt="" title="" onclick="linkEdition('.$link['id'].')" style="cursor: pointer" />
  355. <img src="../img/admin/delete.gif" alt="" title="" onclick="linkDeletion('.$link['id'].')" style="cursor: pointer" />
  356. </td>
  357. </tr>';
  358. $this->_html .= '
  359. </table>
  360. <input type="hidden" id="languageFirst" value="'.$languages[0]['id_lang'].'" />
  361. <input type="hidden" id="languageNb" value="'.sizeof($languages).'" />';
  362. }
  363. }