PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/blockadvertising/blockadvertising.php

https://gitlab.com/brunorafael/enosis
PHP | 291 lines | 198 code | 40 blank | 53 comment | 40 complexity | 2c3cd2ac597244ea21e8d52d7ef8b48a MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2016 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-2016 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. if (!defined('_PS_VERSION_'))
  27. exit;
  28. class BlockAdvertising extends Module
  29. {
  30. /* Title associated to the image */
  31. public $adv_title;
  32. /* Link associated to the image */
  33. public $adv_link;
  34. /* Name of the image without extension */
  35. public $adv_imgname;
  36. /* Image path with extension */
  37. public $adv_img;
  38. public function __construct()
  39. {
  40. $this->name = 'blockadvertising';
  41. $this->tab = 'advertising_marketing';
  42. $this->version = '0.10.1';
  43. $this->author = 'PrestaShop';
  44. $this->need_instance = 0;
  45. $this->bootstrap = true;
  46. parent::__construct();
  47. $this->displayName = $this->l('Advertising block');
  48. $this->description = $this->l('Adds an advertisement block to selected sections of your e-commerce website.');
  49. $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.99.99');
  50. $this->initialize();
  51. }
  52. /*
  53. * Set the properties of the module, like the link to the image and the title (contextual to the current shop context)
  54. */
  55. protected function initialize()
  56. {
  57. $this->adv_imgname = 'advertising';
  58. if ((Shop::getContext() == Shop::CONTEXT_GROUP || Shop::getContext() == Shop::CONTEXT_SHOP)
  59. && file_exists(_PS_MODULE_DIR_.$this->name.'/img/'.$this->adv_imgname.'-g'.$this->context->shop->getContextShopGroupID().'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))
  60. )
  61. $this->adv_imgname .= '-g'.$this->context->shop->getContextShopGroupID();
  62. if (Shop::getContext() == Shop::CONTEXT_SHOP
  63. && file_exists(_PS_MODULE_DIR_.$this->name.'/img/'.$this->adv_imgname.'-s'.$this->context->shop->getContextShopID().'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))
  64. )
  65. $this->adv_imgname .= '-s'.$this->context->shop->getContextShopID();
  66. // If none of them available go default
  67. if ($this->adv_imgname == 'advertising')
  68. $this->adv_img = Tools::getMediaServer($this->name)._MODULE_DIR_.$this->name.'/img/fixtures/'.$this->adv_imgname.'.jpg';
  69. else
  70. $this->adv_img = Tools::getMediaServer($this->name)._MODULE_DIR_.$this->name.'/img/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT');
  71. $this->adv_link = htmlentities(Configuration::get('BLOCKADVERT_LINK'), ENT_QUOTES, 'UTF-8');
  72. $this->adv_title = htmlentities(Configuration::get('BLOCKADVERT_TITLE'), ENT_QUOTES, 'UTF-8');
  73. }
  74. public function install()
  75. {
  76. if (!parent::install())
  77. return false;
  78. // Hook the module either on the left or right column
  79. $theme = new Theme(Context::getContext()->shop->id_theme);
  80. if ((!$theme->default_left_column || !$this->registerHook('leftColumn'))
  81. && (!$theme->default_right_column || !$this->registerHook('rightColumn'))
  82. )
  83. {
  84. // If there are no colums implemented by the template, throw an error and uninstall the module
  85. $this->_errors[] = $this->l('This module needs to be hooked to a column, but your theme does not implement one');
  86. parent::uninstall();
  87. return false;
  88. }
  89. Configuration::updateGlobalValue('BLOCKADVERT_LINK', 'http://www.prestashop.com/');
  90. Configuration::updateGlobalValue('BLOCKADVERT_TITLE', 'PrestaShop');
  91. // Try to update with the extension of the image that exists in the module directory
  92. foreach (scandir(_PS_MODULE_DIR_.$this->name) as $file)
  93. if (in_array($file, array('advertising.jpg', 'advertising.gif', 'advertising.png')))
  94. Configuration::updateGlobalValue('BLOCKADVERT_IMG_EXT', substr($file, strrpos($file, '.') + 1));
  95. return true;
  96. }
  97. public function uninstall()
  98. {
  99. Configuration::deleteByName('BLOCKADVERT_LINK');
  100. Configuration::deleteByName('BLOCKADVERT_TITLE');
  101. Configuration::deleteByName('BLOCKADVERT_IMG_EXT');
  102. return (parent::uninstall());
  103. }
  104. /**
  105. * delete the contextual image (it is not allowed to delete the default image)
  106. *
  107. * @return void
  108. */
  109. private function _deleteCurrentImg()
  110. {
  111. // Delete the image file
  112. if ($this->adv_imgname != 'advertising' && file_exists(_PS_MODULE_DIR_.$this->name.'/img/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT')))
  113. unlink(_PS_MODULE_DIR_.$this->name.'/img/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'));
  114. // Update the extension to the global value or the shop group value if available
  115. Configuration::deleteFromContext('BLOCKADVERT_IMG_EXT');
  116. Configuration::updateValue('BLOCKADVERT_IMG_EXT', Configuration::get('BLOCKADVERT_IMG_EXT'));
  117. // Reset the properties of the module
  118. $this->initialize();
  119. }
  120. public function postProcess()
  121. {
  122. if (Tools::isSubmit('submitDeleteImgConf'))
  123. $this->_deleteCurrentImg();
  124. $errors = '';
  125. if (Tools::isSubmit('submitAdvConf'))
  126. {
  127. if (isset($_FILES['adv_img']) && isset($_FILES['adv_img']['tmp_name']) && !empty($_FILES['adv_img']['tmp_name']))
  128. {
  129. if ($error = ImageManager::validateUpload($_FILES['adv_img'], Tools::convertBytes(ini_get('upload_max_filesize'))))
  130. $errors .= $error;
  131. else
  132. {
  133. Configuration::updateValue('BLOCKADVERT_IMG_EXT', substr($_FILES['adv_img']['name'], strrpos($_FILES['adv_img']['name'], '.') + 1));
  134. // Set the image name with a name contextual to the shop context
  135. $this->adv_imgname = 'advertising';
  136. if (Shop::getContext() == Shop::CONTEXT_GROUP)
  137. $this->adv_imgname = 'advertising-g'.(int)$this->context->shop->getContextShopGroupID();
  138. elseif (Shop::getContext() == Shop::CONTEXT_SHOP)
  139. $this->adv_imgname = 'advertising-s'.(int)$this->context->shop->getContextShopID();
  140. // Copy the image in the module directory with its new name
  141. if (!move_uploaded_file($_FILES['adv_img']['tmp_name'], _PS_MODULE_DIR_.$this->name.'/img/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT')))
  142. $errors .= $this->l('File upload error.');
  143. }
  144. }
  145. // If the link is not set, then delete it in order to use the next default value (either the global value or the group value)
  146. if ($link = Tools::getValue('adv_link'))
  147. Configuration::updateValue('BLOCKADVERT_LINK', $link);
  148. elseif (Shop::getContext() == Shop::CONTEXT_SHOP || Shop::getContext() == Shop::CONTEXT_GROUP)
  149. Configuration::deleteFromContext('BLOCKADVERT_LINK');
  150. // If the title is not set, then delete it in order to use the next default value (either the global value or the group value)
  151. if ($title = Tools::getValue('adv_title'))
  152. Configuration::updateValue('BLOCKADVERT_TITLE', $title);
  153. elseif (Shop::getContext() == Shop::CONTEXT_SHOP || Shop::getContext() == Shop::CONTEXT_GROUP)
  154. Configuration::deleteFromContext('BLOCKADVERT_TITLE');
  155. // Reset the module properties
  156. $this->initialize();
  157. $this->_clearCache('blockadvertising.tpl');
  158. if (!$errors)
  159. Tools::redirectAdmin(AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&conf=6');
  160. echo $this->displayError($errors);
  161. }
  162. }
  163. /**
  164. * getContent used to display admin module form
  165. *
  166. * @return string content
  167. */
  168. public function getContent()
  169. {
  170. $this->postProcess();
  171. return $this->renderForm();
  172. }
  173. public function hookRightColumn($params)
  174. {
  175. if (!$this->isCached('blockadvertising.tpl', $this->getCacheId()))
  176. $this->smarty->assign(
  177. array(
  178. 'image' => $this->context->link->protocol_content.$this->adv_img,
  179. 'adv_link' => $this->adv_link,
  180. 'adv_title' => $this->adv_title,
  181. )
  182. );
  183. return $this->display(__FILE__, 'blockadvertising.tpl', $this->getCacheId());
  184. }
  185. public function hookLeftColumn($params)
  186. {
  187. return $this->hookRightColumn($params);
  188. }
  189. public function hookHeader($params)
  190. {
  191. $this->context->controller->addCSS($this->_path.'blockadvertising.css', 'all');
  192. }
  193. public function renderForm()
  194. {
  195. $fields_form = array(
  196. 'form' => array(
  197. 'legend' => array(
  198. 'title' => $this->l('Configuration'),
  199. 'icon' => 'icon-cogs'
  200. ),
  201. 'input' => array(
  202. array(
  203. 'type' => 'file',
  204. 'label' => $this->l('Image for the advertisement'),
  205. 'name' => 'adv_img',
  206. 'desc' => $this->l('By default the image will appear in the left column. The recommended dimensions are 155 x 163px.'),
  207. 'thumb' => $this->context->link->protocol_content.$this->adv_img,
  208. ),
  209. array(
  210. 'type' => 'text',
  211. 'label' => $this->l('Target link for the image'),
  212. 'name' => 'adv_link',
  213. ),
  214. array(
  215. 'type' => 'text',
  216. 'label' => $this->l('Title of the target link'),
  217. 'name' => 'adv_title',
  218. 'desc' => $this->l('This title will be displayed when you mouse over the advertisement block in your shop.')
  219. ),
  220. ),
  221. 'submit' => array(
  222. 'title' => $this->l('Save'),
  223. )
  224. ),
  225. );
  226. $helper = new HelperForm();
  227. $helper->show_toolbar = false;
  228. $helper->table = $this->table;
  229. $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  230. $helper->default_form_language = $lang->id;
  231. $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
  232. $this->fields_form = array();
  233. $helper->identifier = $this->identifier;
  234. $helper->submit_action = 'submitAdvConf';
  235. $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
  236. $helper->token = Tools::getAdminTokenLite('AdminModules');
  237. $helper->tpl_vars = array(
  238. 'fields_value' => $this->getConfigFieldsValues(),
  239. 'languages' => $this->context->controller->getLanguages(),
  240. 'id_language' => $this->context->language->id
  241. );
  242. return $helper->generateForm(array($fields_form));
  243. }
  244. public function getConfigFieldsValues()
  245. {
  246. return array(
  247. 'adv_link' => Tools::getValue('adv_link', Configuration::get('BLOCKADVERT_LINK')),
  248. 'adv_title' => Tools::getValue('adv_title', Configuration::get('BLOCKADVERT_TITLE')),
  249. );
  250. }
  251. }