PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/homeslider/homeslider.php

https://github.com/Stigmi/PrestaShop-1.5
PHP | 777 lines | 599 code | 68 blank | 110 comment | 108 complexity | 4c1d0cb2ab44fe74e8e2cf12630372e9 MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-SA-3.0, LGPL-2.1
  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 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-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. * @since 1.5.0
  28. * @version 1.2 (2012-03-14)
  29. */
  30. if (!defined('_PS_VERSION_'))
  31. exit;
  32. include_once(_PS_MODULE_DIR_.'homeslider/HomeSlide.php');
  33. class HomeSlider extends Module
  34. {
  35. private $_html = '';
  36. public function __construct()
  37. {
  38. $this->name = 'homeslider';
  39. $this->tab = 'front_office_features';
  40. $this->version = '1.2.2';
  41. $this->author = 'PrestaShop';
  42. $this->need_instance = 0;
  43. $this->secure_key = Tools::encrypt($this->name);
  44. parent::__construct();
  45. $this->displayName = $this->l('Image slider for your homepage.');
  46. $this->description = $this->l('Adds an image slider to your homepage.');
  47. }
  48. /**
  49. * @see Module::install()
  50. */
  51. public function install()
  52. {
  53. /* Adds Module */
  54. if (parent::install() && $this->registerHook('displayHome') && $this->registerHook('actionShopDataDuplication'))
  55. {
  56. /* Sets up configuration */
  57. $res = Configuration::updateValue('HOMESLIDER_WIDTH', '535');
  58. $res &= Configuration::updateValue('HOMESLIDER_HEIGHT', '300');
  59. $res &= Configuration::updateValue('HOMESLIDER_SPEED', '500');
  60. $res &= Configuration::updateValue('HOMESLIDER_PAUSE', '3000');
  61. $res &= Configuration::updateValue('HOMESLIDER_LOOP', '1');
  62. /* Creates tables */
  63. $res &= $this->createTables();
  64. /* Adds samples */
  65. if ($res)
  66. $this->installSamples();
  67. return $res;
  68. }
  69. return false;
  70. }
  71. /**
  72. * Adds samples
  73. */
  74. private function installSamples()
  75. {
  76. $languages = Language::getLanguages(false);
  77. for ($i = 1; $i <= 5; ++$i)
  78. {
  79. $slide = new HomeSlide();
  80. $slide->position = $i;
  81. $slide->active = 1;
  82. foreach ($languages as $language)
  83. {
  84. $slide->title[$language['id_lang']] = 'Sample '.$i;
  85. $slide->description[$language['id_lang']] = 'This is a sample picture';
  86. $slide->legend[$language['id_lang']] = 'sample-'.$i;
  87. $slide->url[$language['id_lang']] = 'http://www.prestashop.com';
  88. $slide->image[$language['id_lang']] = 'sample-'.$i.'.jpg';
  89. }
  90. $slide->add();
  91. }
  92. }
  93. /**
  94. * @see Module::uninstall()
  95. */
  96. public function uninstall()
  97. {
  98. /* Deletes Module */
  99. if (parent::uninstall())
  100. {
  101. /* Deletes tables */
  102. $res = $this->deleteTables();
  103. /* Unsets configuration */
  104. $res &= Configuration::deleteByName('HOMESLIDER_WIDTH');
  105. $res &= Configuration::deleteByName('HOMESLIDER_HEIGHT');
  106. $res &= Configuration::deleteByName('HOMESLIDER_SPEED');
  107. $res &= Configuration::deleteByName('HOMESLIDER_PAUSE');
  108. $res &= Configuration::deleteByName('HOMESLIDER_LOOP');
  109. return $res;
  110. }
  111. return false;
  112. }
  113. /**
  114. * Creates tables
  115. */
  116. protected function createTables()
  117. {
  118. /* Slides */
  119. $res = (bool)Db::getInstance()->execute('
  120. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'homeslider` (
  121. `id_homeslider_slides` int(10) unsigned NOT NULL AUTO_INCREMENT,
  122. `id_shop` int(10) unsigned NOT NULL,
  123. PRIMARY KEY (`id_homeslider_slides`, `id_shop`)
  124. ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
  125. ');
  126. /* Slides configuration */
  127. $res &= Db::getInstance()->execute('
  128. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'homeslider_slides` (
  129. `id_homeslider_slides` int(10) unsigned NOT NULL AUTO_INCREMENT,
  130. `position` int(10) unsigned NOT NULL DEFAULT \'0\',
  131. `active` tinyint(1) unsigned NOT NULL DEFAULT \'0\',
  132. PRIMARY KEY (`id_homeslider_slides`)
  133. ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
  134. ');
  135. /* Slides lang configuration */
  136. $res &= Db::getInstance()->execute('
  137. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'homeslider_slides_lang` (
  138. `id_homeslider_slides` int(10) unsigned NOT NULL,
  139. `id_lang` int(10) unsigned NOT NULL,
  140. `title` varchar(255) NOT NULL,
  141. `description` text NOT NULL,
  142. `legend` varchar(255) NOT NULL,
  143. `url` varchar(255) NOT NULL,
  144. `image` varchar(255) NOT NULL,
  145. PRIMARY KEY (`id_homeslider_slides`,`id_lang`)
  146. ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
  147. ');
  148. return $res;
  149. }
  150. /**
  151. * deletes tables
  152. */
  153. protected function deleteTables()
  154. {
  155. $slides = $this->getSlides();
  156. foreach ($slides as $slide)
  157. {
  158. $to_del = new HomeSlide($slide['id_slide']);
  159. $to_del->delete();
  160. }
  161. return Db::getInstance()->execute('
  162. DROP TABLE IF EXISTS `'._DB_PREFIX_.'homeslider`, `'._DB_PREFIX_.'homeslider_slides`, `'._DB_PREFIX_.'homeslider_slides_lang`;
  163. ');
  164. }
  165. public function getContent()
  166. {
  167. $this->_html .= $this->headerHTML();
  168. $this->_html .= '<h2>'.$this->displayName.'.</h2>';
  169. /* Validate & process */
  170. if (Tools::isSubmit('submitSlide') || Tools::isSubmit('delete_id_slide') ||
  171. Tools::isSubmit('submitSlider') ||
  172. Tools::isSubmit('changeStatus'))
  173. {
  174. if ($this->_postValidation())
  175. $this->_postProcess();
  176. $this->_displayForm();
  177. }
  178. elseif (Tools::isSubmit('addSlide') || (Tools::isSubmit('id_slide') && $this->slideExists((int)Tools::getValue('id_slide'))))
  179. $this->_displayAddForm();
  180. else
  181. $this->_displayForm();
  182. return $this->_html;
  183. }
  184. private function _displayForm()
  185. {
  186. /* Gets Slides */
  187. $slides = $this->getSlides();
  188. /* Begin fieldset slider */
  189. $this->_html .= '
  190. <fieldset>
  191. <legend><img src="'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/logo.gif" alt="" /> '.$this->l('Slider configuration').'</legend>';
  192. /* Begin form */
  193. $this->_html .= '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">';
  194. /* Height field */
  195. $this->_html .= '
  196. <label>'.$this->l('Height:').'</label>
  197. <div class="margin-form">
  198. <input type="text" name="HOMESLIDER_HEIGHT" id="speed" size="3" value="'.Tools::safeOutput(Configuration::get('HOMESLIDER_HEIGHT')).'" /> px
  199. </div>';
  200. /* Width field */
  201. $this->_html .= '
  202. <label>'.$this->l('Width:').'</label>
  203. <div class="margin-form">
  204. <input type="text" name="HOMESLIDER_WIDTH" id="pause" size="3" value="'.Tools::safeOutput(Configuration::get('HOMESLIDER_WIDTH')).'" /> px
  205. </div>';
  206. /* Speed field */
  207. $this->_html .= '
  208. <label>'.$this->l('Speed:').'</label>
  209. <div class="margin-form">
  210. <input type="text" name="HOMESLIDER_SPEED" id="speed" size="3" value="'.Tools::safeOutput(Configuration::get('HOMESLIDER_SPEED')).'" /> ms
  211. </div>';
  212. /* Pause field */
  213. $this->_html .= '
  214. <label>'.$this->l('Pause:').'</label>
  215. <div class="margin-form">
  216. <input type="text" name="HOMESLIDER_PAUSE" id="pause" size="3" value="'.Tools::safeOutput(Configuration::get('HOMESLIDER_PAUSE')).'" /> ms
  217. </div>';
  218. /* Loop field */
  219. $this->_html .= '
  220. <label for="loop_on">'.$this->l('Loop:').'</label>
  221. <div class="margin-form">
  222. <img src="../img/admin/enabled.gif" alt="Yes" title="Yes" />
  223. <input type="radio" name="HOMESLIDER_LOOP" id="loop_on" '.(Configuration::get('HOMESLIDER_LOOP') == 1 ? 'checked="checked"' : '').' value="1" />
  224. <label class="t" for="loop_on">'.$this->l('Yes').'</label>
  225. <img src="../img/admin/disabled.gif" alt="No" title="No" style="margin-left: 10px;" />
  226. <input type="radio" name="HOMESLIDER_LOOP" id="loop_off" '.(Configuration::get('HOMESLIDER_LOOP') == 0 ? 'checked="checked" ' : '').' value="0" />
  227. <label class="t" for="loop_off">'.$this->l('No').'</label>
  228. </div>';
  229. /* Save */
  230. $this->_html .= '
  231. <div class="margin-form">
  232. <input type="submit" class="button" name="submitSlider" value="'.$this->l('Save').'" />
  233. </div>';
  234. /* End form */
  235. $this->_html .= '</form>';
  236. /* End fieldset slider */
  237. $this->_html .= '</fieldset>';
  238. $this->_html .= '<br /><br />';
  239. /* Begin fieldset slides */
  240. $this->_html .= '
  241. <fieldset>
  242. <legend><img src="'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/logo.gif" alt="" /> '.$this->l('Slides configuration').'</legend>
  243. <strong>
  244. <a href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&addSlide">
  245. <img src="'._PS_ADMIN_IMG_.'add.gif" alt="" /> '.$this->l('Add Slide').'
  246. </a>
  247. </strong>';
  248. /* Display notice if there are no slides yet */
  249. if (!$slides)
  250. $this->_html .= '<p style="margin-left: 40px;">'.$this->l('You have not yet added any slides.').'</p>';
  251. else /* Display slides */
  252. {
  253. $this->_html .= '
  254. <div id="slidesContent" style="width: 400px; margin-top: 30px;">
  255. <ul id="slides">';
  256. foreach ($slides as $slide)
  257. {
  258. $this->_html .= '
  259. <li id="slides_'.$slide['id_slide'].'">
  260. <strong>#'.$slide['id_slide'].'</strong> '.$slide['title'].'
  261. <p style="float: right">'.
  262. $this->displayStatus($slide['id_slide'], $slide['active']).'
  263. <a href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&id_slide='.(int)($slide['id_slide']).'" title="'.$this->l('Edit').'"><img src="'._PS_ADMIN_IMG_.'edit.gif" alt="" /></a>
  264. <a href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&delete_id_slide='.(int)($slide['id_slide']).'" title="'.$this->l('Delete').'"><img src="'._PS_ADMIN_IMG_.'delete.gif" alt="" /></a>
  265. </p>
  266. </li>';
  267. }
  268. $this->_html .= '</ul></div>';
  269. }
  270. // End fieldset
  271. $this->_html .= '</fieldset>';
  272. }
  273. private function _displayAddForm()
  274. {
  275. /* Sets Slide : depends if edited or added */
  276. $slide = null;
  277. if (Tools::isSubmit('id_slide') && $this->slideExists((int)Tools::getValue('id_slide')))
  278. $slide = new HomeSlide((int)Tools::getValue('id_slide'));
  279. /* Checks if directory is writable */
  280. if (!is_writable('.'))
  281. $this->adminDisplayWarning(sprintf($this->l('Modules %s must be writable (CHMOD 755 / 777)'), $this->name));
  282. /* Gets languages and sets which div requires translations */
  283. $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
  284. $languages = Language::getLanguages(false);
  285. $divLangName = 'image造title造url造legend造description';
  286. $this->_html .= '<script type="text/javascript">id_language = Number('.$id_lang_default.');</script>';
  287. /* Form */
  288. $this->_html .= '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">';
  289. /* Fieldset Upload */
  290. $this->_html .= '
  291. <fieldset class="width3">
  292. <br />
  293. <legend><img src="'._PS_ADMIN_IMG_.'add.gif" alt="" />1 - '.$this->l('Upload your slide').'</legend>';
  294. /* Image */
  295. $this->_html .= '<label>'.$this->l('Select a file:').' * </label><div class="margin-form">';
  296. foreach ($languages as $language)
  297. {
  298. $this->_html .= '<div id="image_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">';
  299. $this->_html .= '<input type="file" name="image_'.$language['id_lang'].'" id="image_'.$language['id_lang'].'" size="30" value="'.(isset($slide->image[$language['id_lang']]) ? $slide->image[$language['id_lang']] : '').'"/>';
  300. /* Sets image as hidden in case it does not change */
  301. if ($slide && $slide->image[$language['id_lang']])
  302. $this->_html .= '<input type="hidden" name="image_old_'.$language['id_lang'].'" value="'.($slide->image[$language['id_lang']]).'" id="image_old_'.$language['id_lang'].'" />';
  303. /* Display image */
  304. if ($slide && $slide->image[$language['id_lang']])
  305. $this->_html .= '<input type="hidden" name="has_picture" value="1" /><img src="'.__PS_BASE_URI__.'modules/'.$this->name.'/images/'.$slide->image[$language['id_lang']].'" width="'.(Configuration::get('HOMESLIDER_WIDTH')/2).'" height="'.(Configuration::get('HOMESLIDER_HEIGHT')/2).'" alt=""/>';
  306. $this->_html .= '</div>';
  307. }
  308. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'image', true);
  309. /* End Fieldset Upload */
  310. $this->_html .= '</fieldset><br /><br />';
  311. /* Fieldset edit/add */
  312. $this->_html .= '<fieldset class="width3">';
  313. if (Tools::isSubmit('addSlide')) /* Configure legend */
  314. $this->_html .= '<legend><img src="'._PS_ADMIN_IMG_.'add.gif" alt="" /> 2 - '.$this->l('Configure your slide').'</legend>';
  315. elseif (Tools::isSubmit('id_slide')) /* Edit legend */
  316. $this->_html .= '<legend><img src="'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/logo.gif" alt="" /> 2 - '.$this->l('Edit your slide').'</legend>';
  317. /* Sets id slide as hidden */
  318. if ($slide && Tools::getValue('id_slide'))
  319. $this->_html .= '<input type="hidden" name="id_slide" value="'.$slide->id.'" id="id_slide" />';
  320. /* Sets position as hidden */
  321. $this->_html .= '<input type="hidden" name="position" value="'.(($slide != null) ? ($slide->position) : ($this->getNextPosition())).'" id="position" />';
  322. /* Form content */
  323. /* Title */
  324. $this->_html .= '<br /><label>'.$this->l('Title:').' * </label><div class="margin-form">';
  325. foreach ($languages as $language)
  326. {
  327. $this->_html .= '
  328. <div id="title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
  329. <input type="text" name="title_'.$language['id_lang'].'" id="title_'.$language['id_lang'].'" size="30" value="'.(isset($slide->title[$language['id_lang']]) ? $slide->title[$language['id_lang']] : '').'"/>
  330. </div>';
  331. }
  332. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'title', true);
  333. $this->_html .= '</div><br /><br />';
  334. /* URL */
  335. $this->_html .= '<label>'.$this->l('URL:').' * </label><div class="margin-form">';
  336. foreach ($languages as $language)
  337. {
  338. $this->_html .= '
  339. <div id="url_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
  340. <input type="text" name="url_'.$language['id_lang'].'" id="url_'.$language['id_lang'].'" size="30" value="'.(isset($slide->url[$language['id_lang']]) ? $slide->url[$language['id_lang']] : '').'"/>
  341. </div>';
  342. }
  343. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'url', true);
  344. $this->_html .= '</div><br /><br />';
  345. /* Legend */
  346. $this->_html .= '<label>'.$this->l('Legend:').' * </label><div class="margin-form">';
  347. foreach ($languages as $language)
  348. {
  349. $this->_html .= '
  350. <div id="legend_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
  351. <input type="text" name="legend_'.$language['id_lang'].'" id="legend_'.$language['id_lang'].'" size="30" value="'.(isset($slide->legend[$language['id_lang']]) ? $slide->legend[$language['id_lang']] : '').'"/>
  352. </div>';
  353. }
  354. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'legend', true);
  355. $this->_html .= '</div><br /><br />';
  356. /* Description */
  357. $this->_html .= '
  358. <label>'.$this->l('Description:').' </label>
  359. <div class="margin-form">';
  360. foreach ($languages as $language)
  361. {
  362. $this->_html .= '<div id="description_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
  363. <textarea name="description_'.$language['id_lang'].'" rows="10" cols="29">'.(isset($slide->description[$language['id_lang']]) ? $slide->description[$language['id_lang']] : '').'</textarea>
  364. </div>';
  365. }
  366. $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'description', true);
  367. $this->_html .= '</div><div class="clear"></div><br />';
  368. /* Active */
  369. $this->_html .= '
  370. <label for="active_on">'.$this->l('Active:').'</label>
  371. <div class="margin-form">
  372. <img src="../img/admin/enabled.gif" alt="Yes" title="Yes" />
  373. <input type="radio" name="active_slide" id="active_on" '.(($slide && (isset($slide->active) && (int)$slide->active == 0)) ? '' : 'checked="checked" ').' value="1" />
  374. <label class="t" for="active_on">'.$this->l('Yes').'</label>
  375. <img src="../img/admin/disabled.gif" alt="No" title="No" style="margin-left: 10px;" />
  376. <input type="radio" name="active_slide" id="active_off" '.(($slide && (isset($slide->active) && (int)$slide->active == 0)) ? 'checked="checked" ' : '').' value="0" />
  377. <label class="t" for="active_off">'.$this->l('No').'</label>
  378. </div>';
  379. /* Save */
  380. $this->_html .= '
  381. <p class="center">
  382. <input style="min-height:26px" type="submit" class="button" name="submitSlide" value="'.$this->l('Save').'" />
  383. <a class="button" style="position:relative; padding:4px 3px;" href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'">'.$this->l('Cancel').'</a>
  384. </p>';
  385. /* End of fieldset & form */
  386. $this->_html .= '
  387. <p>*'.$this->l('Required fields').'</p>
  388. </fieldset>
  389. </form>';
  390. }
  391. private function _postValidation()
  392. {
  393. $errors = array();
  394. /* Validation for Slider configuration */
  395. if (Tools::isSubmit('submitSlider'))
  396. {
  397. if (!Validate::isInt(Tools::getValue('HOMESLIDER_SPEED')) || !Validate::isInt(Tools::getValue('HOMESLIDER_PAUSE')) ||
  398. !Validate::isInt(Tools::getValue('HOMESLIDER_WIDTH')) || !Validate::isInt(Tools::getValue('HOMESLIDER_HEIGHT')))
  399. $errors[] = $this->l('Invalid values');
  400. } /* Validation for status */
  401. elseif (Tools::isSubmit('changeStatus'))
  402. {
  403. if (!Validate::isInt(Tools::getValue('id_slide')))
  404. $errors[] = $this->l('Invalid slide');
  405. }
  406. /* Validation for Slide */
  407. elseif (Tools::isSubmit('submitSlide'))
  408. {
  409. /* Checks state (active) */
  410. if (!Validate::isInt(Tools::getValue('active_slide')) || (Tools::getValue('active_slide') != 0 && Tools::getValue('active_slide') != 1))
  411. $errors[] = $this->l('Invalid slide state');
  412. /* Checks position */
  413. if (!Validate::isInt(Tools::getValue('position')) || (Tools::getValue('position') < 0))
  414. $errors[] = $this->l('Invalid slide position');
  415. /* If edit : checks id_slide */
  416. if (Tools::isSubmit('id_slide'))
  417. {
  418. if (!Validate::isInt(Tools::getValue('id_slide')) && !$this->slideExists(Tools::getValue('id_slide')))
  419. $errors[] = $this->l('Invalid id_slide');
  420. }
  421. /* Checks title/url/legend/description/image */
  422. $languages = Language::getLanguages(false);
  423. foreach ($languages as $language)
  424. {
  425. if (Tools::strlen(Tools::getValue('title_'.$language['id_lang'])) > 255)
  426. $errors[] = $this->l('The title is too long.');
  427. if (Tools::strlen(Tools::getValue('legend_'.$language['id_lang'])) > 255)
  428. $errors[] = $this->l('The legend is too long.');
  429. if (Tools::strlen(Tools::getValue('url_'.$language['id_lang'])) > 255)
  430. $errors[] = $this->l('The URL is too long.');
  431. if (Tools::strlen(Tools::getValue('description_'.$language['id_lang'])) > 4000)
  432. $errors[] = $this->l('The description is too long.');
  433. if (Tools::strlen(Tools::getValue('url_'.$language['id_lang'])) > 0 && !Validate::isUrl(Tools::getValue('url_'.$language['id_lang'])))
  434. $errors[] = $this->l('The URL format is not correct.');
  435. if (Tools::getValue('image_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_'.$language['id_lang'])))
  436. $errors[] = $this->l('Invalid filename');
  437. if (Tools::getValue('image_old_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_old_'.$language['id_lang'])))
  438. $errors[] = $this->l('Invalid filename');
  439. }
  440. /* Checks title/url/legend/description for default lang */
  441. $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
  442. if (Tools::strlen(Tools::getValue('title_'.$id_lang_default)) == 0)
  443. $errors[] = $this->l('The title is not set.');
  444. if (Tools::strlen(Tools::getValue('legend_'.$id_lang_default)) == 0)
  445. $errors[] = $this->l('The legend is not set.');
  446. if (Tools::strlen(Tools::getValue('url_'.$id_lang_default)) == 0)
  447. $errors[] = $this->l('The URL is not set.');
  448. if (!Tools::isSubmit('has_picture') && (!isset($_FILES['image_'.$id_lang_default]) || empty($_FILES['image_'.$id_lang_default]['tmp_name'])))
  449. $errors[] = $this->l('The image is not set.');
  450. if (Tools::getValue('image_old_'.$id_lang_default) && !Validate::isFileName(Tools::getValue('image_old_'.$id_lang_default)))
  451. $errors[] = $this->l('The image is not set.');
  452. } /* Validation for deletion */
  453. elseif (Tools::isSubmit('delete_id_slide') && (!Validate::isInt(Tools::getValue('delete_id_slide')) || !$this->slideExists((int)Tools::getValue('delete_id_slide'))))
  454. $errors[] = $this->l('Invalid id_slide');
  455. /* Display errors if needed */
  456. if (count($errors))
  457. {
  458. $this->_html .= $this->displayError(implode('<br />', $errors));
  459. return false;
  460. }
  461. /* Returns if validation is ok */
  462. return true;
  463. }
  464. private function _postProcess()
  465. {
  466. $errors = array();
  467. /* Processes Slider */
  468. if (Tools::isSubmit('submitSlider'))
  469. {
  470. $res = Configuration::updateValue('HOMESLIDER_WIDTH', (int)Tools::getValue('HOMESLIDER_WIDTH'));
  471. $res &= Configuration::updateValue('HOMESLIDER_HEIGHT', (int)Tools::getValue('HOMESLIDER_HEIGHT'));
  472. $res &= Configuration::updateValue('HOMESLIDER_SPEED', (int)Tools::getValue('HOMESLIDER_SPEED'));
  473. $res &= Configuration::updateValue('HOMESLIDER_PAUSE', (int)Tools::getValue('HOMESLIDER_PAUSE'));
  474. $res &= Configuration::updateValue('HOMESLIDER_LOOP', (int)Tools::getValue('HOMESLIDER_LOOP'));
  475. $this->clearCache();
  476. if (!$res)
  477. $errors[] = $this->displayError($this->l('The configuration could not be updated.'));
  478. $this->_html .= $this->displayConfirmation($this->l('Configuration updated'));
  479. } /* Process Slide status */
  480. elseif (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_slide'))
  481. {
  482. $slide = new HomeSlide((int)Tools::getValue('id_slide'));
  483. if ($slide->active == 0)
  484. $slide->active = 1;
  485. else
  486. $slide->active = 0;
  487. $res = $slide->update();
  488. $this->clearCache();
  489. $this->_html .= ($res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayError($this->l('The configuration could not be updated.')));
  490. }
  491. /* Processes Slide */
  492. elseif (Tools::isSubmit('submitSlide'))
  493. {
  494. /* Sets ID if needed */
  495. if (Tools::getValue('id_slide'))
  496. {
  497. $slide = new HomeSlide((int)Tools::getValue('id_slide'));
  498. if (!Validate::isLoadedObject($slide))
  499. {
  500. $this->_html .= $this->displayError($this->l('Invalid id_slide'));
  501. return;
  502. }
  503. }
  504. else
  505. $slide = new HomeSlide();
  506. /* Sets position */
  507. $slide->position = (int)Tools::getValue('position');
  508. /* Sets active */
  509. $slide->active = (int)Tools::getValue('active_slide');
  510. /* Sets each langue fields */
  511. $languages = Language::getLanguages(false);
  512. foreach ($languages as $language)
  513. {
  514. $slide->title[$language['id_lang']] = Tools::getValue('title_'.$language['id_lang']);
  515. $slide->url[$language['id_lang']] = Tools::getValue('url_'.$language['id_lang']);
  516. $slide->legend[$language['id_lang']] = Tools::getValue('legend_'.$language['id_lang']);
  517. $slide->description[$language['id_lang']] = Tools::getValue('description_'.$language['id_lang']);
  518. /* Uploads image and sets slide */
  519. $type = strtolower(substr(strrchr($_FILES['image_'.$language['id_lang']]['name'], '.'), 1));
  520. $imagesize = array();
  521. $imagesize = @getimagesize($_FILES['image_'.$language['id_lang']]['tmp_name']);
  522. if (isset($_FILES['image_'.$language['id_lang']]) &&
  523. isset($_FILES['image_'.$language['id_lang']]['tmp_name']) &&
  524. !empty($_FILES['image_'.$language['id_lang']]['tmp_name']) &&
  525. !empty($imagesize) &&
  526. in_array(strtolower(substr(strrchr($imagesize['mime'], '/'), 1)), array('jpg', 'gif', 'jpeg', 'png')) &&
  527. in_array($type, array('jpg', 'gif', 'jpeg', 'png')))
  528. {
  529. $temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
  530. $salt = sha1(microtime());
  531. if ($error = ImageManager::validateUpload($_FILES['image_'.$language['id_lang']]))
  532. $errors[] = $error;
  533. elseif (!$temp_name || !move_uploaded_file($_FILES['image_'.$language['id_lang']]['tmp_name'], $temp_name))
  534. return false;
  535. elseif (!ImageManager::resize($temp_name, dirname(__FILE__).'/images/'.Tools::encrypt($_FILES['image_'.$language['id_lang']]['name'].$salt).'.'.$type, null, null, $type))
  536. $errors[] = $this->displayError($this->l('An error occurred during the image upload process.'));
  537. if (isset($temp_name))
  538. @unlink($temp_name);
  539. $slide->image[$language['id_lang']] = Tools::encrypt($_FILES['image_'.($language['id_lang'])]['name'].$salt).'.'.$type;
  540. }
  541. elseif (Tools::getValue('image_old_'.$language['id_lang']) != '')
  542. $slide->image[$language['id_lang']] = Tools::getValue('image_old_'.$language['id_lang']);
  543. }
  544. /* Processes if no errors */
  545. if (!$errors)
  546. {
  547. /* Adds */
  548. if (!Tools::getValue('id_slide'))
  549. {
  550. if (!$slide->add())
  551. $errors[] = $this->displayError($this->l('The slide could not be added.'));
  552. }
  553. /* Update */
  554. elseif (!$slide->update())
  555. $errors[] = $this->displayError($this->l('The slide could not be updated.'));
  556. $this->clearCache();
  557. }
  558. } /* Deletes */
  559. elseif (Tools::isSubmit('delete_id_slide'))
  560. {
  561. $slide = new HomeSlide((int)Tools::getValue('delete_id_slide'));
  562. $res = $slide->delete();
  563. $this->clearCache();
  564. if (!$res)
  565. $this->_html .= $this->displayError('Could not delete');
  566. else
  567. $this->_html .= $this->displayConfirmation($this->l('Slide deleted'));
  568. }
  569. /* Display errors if needed */
  570. if (count($errors))
  571. $this->_html .= $this->displayError(implode('<br />', $errors));
  572. elseif (Tools::isSubmit('submitSlide') && Tools::getValue('id_slide'))
  573. $this->_html .= $this->displayConfirmation($this->l('Slide updated'));
  574. elseif (Tools::isSubmit('submitSlide'))
  575. $this->_html .= $this->displayConfirmation($this->l('Slide added'));
  576. }
  577. private function _prepareHook()
  578. {
  579. if (!$this->isCached('homeslider.tpl', $this->getCacheId()))
  580. {
  581. $slider = array(
  582. 'width' => Configuration::get('HOMESLIDER_WIDTH'),
  583. 'height' => Configuration::get('HOMESLIDER_HEIGHT'),
  584. 'speed' => Configuration::get('HOMESLIDER_SPEED'),
  585. 'pause' => Configuration::get('HOMESLIDER_PAUSE'),
  586. 'loop' => Configuration::get('HOMESLIDER_LOOP'),
  587. );
  588. $slides = $this->getSlides(true);
  589. if (!$slides)
  590. return false;
  591. $this->smarty->assign('homeslider_slides', $slides);
  592. $this->smarty->assign('homeslider', $slider);
  593. }
  594. return true;
  595. }
  596. public function hookDisplayHome()
  597. {
  598. if(!$this->_prepareHook())
  599. return;
  600. // Check if not a mobile theme
  601. if ($this->context->getMobileDevice() != false)
  602. return false;
  603. $this->context->controller->addJS($this->_path.'js/jquery.bxSlider.min.js');
  604. $this->context->controller->addCSS($this->_path.'bx_styles.css');
  605. $this->context->controller->addJS($this->_path.'js/homeslider.js');
  606. return $this->display(__FILE__, 'homeslider.tpl', $this->getCacheId());
  607. }
  608. public function clearCache()
  609. {
  610. $this->_clearCache('homeslider.tpl');
  611. }
  612. public function hookActionShopDataDuplication($params)
  613. {
  614. Db::getInstance()->execute('
  615. INSERT IGNORE INTO '._DB_PREFIX_.'homeslider (id_homeslider_slides, id_shop)
  616. SELECT id_homeslider_slides, '.(int)$params['new_id_shop'].'
  617. FROM '._DB_PREFIX_.'homeslider
  618. WHERE id_shop = '.(int)$params['old_id_shop']);
  619. $this->clearCache();
  620. }
  621. public function headerHTML()
  622. {
  623. if (Tools::getValue('controller') != 'AdminModules' && Tools::getValue('configure') != $this->name)
  624. return;
  625. $this->context->controller->addJqueryUI('ui.sortable');
  626. /* Style & js for fieldset 'slides configuration' */
  627. $html = '
  628. <style>
  629. #slides li {
  630. list-style: none;
  631. margin: 0 0 4px 0;
  632. padding: 10px;
  633. background-color: #F4E6C9;
  634. border: #CCCCCC solid 1px;
  635. color:#000;
  636. }
  637. </style>
  638. <script type="text/javascript">
  639. $(function() {
  640. var $mySlides = $("#slides");
  641. $mySlides.sortable({
  642. opacity: 0.6,
  643. cursor: "move",
  644. update: function() {
  645. var order = $(this).sortable("serialize") + "&action=updateSlidesPosition";
  646. $.post("'.$this->context->shop->physical_uri.$this->context->shop->virtual_uri.'modules/'.$this->name.'/ajax_'.$this->name.'.php?secure_key='.$this->secure_key.'", order);
  647. }
  648. });
  649. $mySlides.hover(function() {
  650. $(this).css("cursor","move");
  651. },
  652. function() {
  653. $(this).css("cursor","auto");
  654. });
  655. });
  656. </script>';
  657. return $html;
  658. }
  659. public function getNextPosition()
  660. {
  661. $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  662. SELECT MAX(hss.`position`) AS `next_position`
  663. FROM `'._DB_PREFIX_.'homeslider_slides` hss, `'._DB_PREFIX_.'homeslider` hs
  664. WHERE hss.`id_homeslider_slides` = hs.`id_homeslider_slides` AND hs.`id_shop` = '.(int)$this->context->shop->id
  665. );
  666. return (++$row['next_position']);
  667. }
  668. public function getSlides($active = null)
  669. {
  670. $this->context = Context::getContext();
  671. $id_shop = $this->context->shop->id;
  672. $id_lang = $this->context->language->id;
  673. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  674. SELECT hs.`id_homeslider_slides` as id_slide,
  675. hssl.`image`,
  676. hss.`position`,
  677. hss.`active`,
  678. hssl.`title`,
  679. hssl.`url`,
  680. hssl.`legend`,
  681. hssl.`description`
  682. FROM '._DB_PREFIX_.'homeslider hs
  683. LEFT JOIN '._DB_PREFIX_.'homeslider_slides hss ON (hs.id_homeslider_slides = hss.id_homeslider_slides)
  684. LEFT JOIN '._DB_PREFIX_.'homeslider_slides_lang hssl ON (hss.id_homeslider_slides = hssl.id_homeslider_slides)
  685. WHERE (id_shop = '.(int)$id_shop.')
  686. AND hssl.id_lang = '.(int)$id_lang.
  687. ($active ? ' AND hss.`active` = 1' : ' ').'
  688. ORDER BY hss.position');
  689. }
  690. public function displayStatus($id_slide, $active)
  691. {
  692. $title = ((int)$active == 0 ? $this->l('Disabled') : $this->l('Enabled'));
  693. $img = ((int)$active == 0 ? 'disabled.gif' : 'enabled.gif');
  694. $html = '<a href="'.AdminController::$currentIndex.
  695. '&configure='.$this->name.'
  696. &token='.Tools::getAdminTokenLite('AdminModules').'
  697. &changeStatus&id_slide='.(int)$id_slide.'" title="'.$title.'"><img src="'._PS_ADMIN_IMG_.''.$img.'" alt="" /></a>';
  698. return $html;
  699. }
  700. public function slideExists($id_slide)
  701. {
  702. $req = 'SELECT hs.`id_homeslider_slides` as id_slide
  703. FROM `'._DB_PREFIX_.'homeslider` hs
  704. WHERE hs.`id_homeslider_slides` = '.(int)$id_slide;
  705. $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($req);
  706. return ($row);
  707. }
  708. }