PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/posters/posters.php

https://gitlab.com/sutrix.hoa.tran/Research-Prestashop
PHP | 286 lines | 223 code | 45 blank | 18 comment | 23 complexity | dff4340bc27c772dc57eb8cfb44c48bf MD5 | raw file
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. if (!defined('_PS_VERSION_'))
  8. exit;
  9. require_once(dirname(__FILE__) . '/classes/PostersModel.php');
  10. class Posters extends Module
  11. {
  12. public $rightColumn = 'right_column';
  13. public $leftColumn = 'left_column';
  14. public $header = 'header';
  15. public $top = 'top';
  16. public $footer = 'footer';
  17. public $home = 'home';
  18. public function __construct()
  19. {
  20. $this->name = 'posters';
  21. $this->tab = 'front_office_features';
  22. $this->version = '0.1';
  23. $this->author = 'Xuan Hoa';
  24. $this->displayName = $this->l('Sutrix Posters');
  25. $this->description = $this->l('This is module allow you add posters to your shop');
  26. parent::__construct();
  27. }
  28. public function install()
  29. {
  30. if(!parent::install() || !$this->createTables() ||
  31. !$this->registerHook('displayLeftColumn') ||
  32. !$this->registerHook('displayRightColumn') ||
  33. !$this->registerHook('displayFooter') ||
  34. !$this->registerHook('displayHeader') ||
  35. !$this->registerHook('displayHome') ||
  36. !$this->registerHook('displayTop')
  37. ){
  38. return false;
  39. }
  40. if(!$this->installTab('Home', 'AdminPosters', 'Posters')){
  41. return false;
  42. }
  43. if(Shop::isFeatureActive()){
  44. Shop::setContext(Shop::CONTEXT_ALL);
  45. }
  46. $this->img_folder = _PS_IMG_DIR_.'pos';
  47. if(!is_dir($this->img_folder) && !file_exists($this->img_folder))
  48. {
  49. mkdir($this->img_folder);
  50. chown($this->img_folder,fileowner(_PS_IMG_DIR_));
  51. chgrp($this->img_folder,filegroup(_PS_IMG_DIR_));
  52. chmod($this->img_folder,fileperms(_PS_IMG_DIR_));
  53. }
  54. return true;
  55. }
  56. public function getPostersByHook($hook)
  57. {
  58. if(!empty($hook)){
  59. $posters =array();
  60. $idLang = $this->context->language->id;
  61. $idShop = $this->context->shop->id;
  62. $posters = PostersModel::getAllPostersByHook($hook, $idLang, $idShop);
  63. if($posters)
  64. foreach ($posters as $i => $poster){
  65. if(!empty($poster['show_page'])){
  66. $controllerCurrent = Dispatcher::getInstance()->getController();
  67. $showPage = @unserialize($poster['show_page']);
  68. if(!in_array($controllerCurrent, (array)$showPage)){
  69. unset($posters[$i]);
  70. }
  71. }
  72. // $pageShowPosters = PostersModel::getPagesOfPosters($poster->id);
  73. }
  74. return $posters;
  75. }
  76. return false;
  77. }
  78. public function hookDisplayRightColumn($param)
  79. {
  80. $posters = $this->getPostersByHook($this->rightColumn);
  81. $controller = $this->getHookController('showPosters');
  82. return (!empty($posters))? $controller->run($param, $posters) : null;
  83. }
  84. public function hookDisplayFooter($param)
  85. {
  86. // $controller = Dispatcher::getInstance()->getController();
  87. //// $idCms = Tools::getValue('id_cms');
  88. // var_dump($controller);
  89. // exit;
  90. $posters = $this->getPostersByHook($this->footer);
  91. $controller = $this->getHookController('showPosters');
  92. return (!empty($posters))? $controller->run($param, $posters) : null;
  93. }
  94. public function hookDisplayHeader($param)
  95. {
  96. $posters = $this->getPostersByHook($this->header);
  97. $controller = $this->getHookController('showPosters');
  98. return (!empty($posters))? $controller->run($param, $posters) : null;
  99. }
  100. public function hookDisplayTop($param)
  101. {
  102. $posters = $this->getPostersByHook($this->top);
  103. $controller = $this->getHookController('showPosters');
  104. return (!empty($posters))? $controller->run($param, $posters) : null;
  105. }
  106. public function hookDisplayHome($param)
  107. {
  108. $posters = $this->getPostersByHook($this->home);
  109. $controller = $this->getHookController('showPosters');
  110. return (!empty($posters))? $controller->run($param, $posters) : null;
  111. }
  112. public function hookDisplayLeftColumn($param)
  113. {
  114. $posters = $this->getPostersByHook($this->leftColumn);
  115. $controller = $this->getHookController('showPosters');
  116. return (!empty($posters))? $controller->run($param, $posters) : null;
  117. }
  118. public function getHookController($hook_name)
  119. {
  120. // Include the controller file
  121. require_once(dirname(__FILE__) . '/controllers/hook/' . $hook_name . '.php');
  122. // Build the controller name dynamically
  123. $controller_name = $this->name . $hook_name . 'Controller';
  124. // Instantiate controller
  125. $controller = new $controller_name($this, __FILE__, $this->_path);
  126. // Return the controller
  127. return $controller;
  128. }
  129. public function dropTables()
  130. {
  131. $res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'posters`,`'._DB_PREFIX_.'posters_lang`,`'._DB_PREFIX_.'posters_shop`,`'._DB_PREFIX_.'type_posters`;');
  132. return $res;
  133. }
  134. public function createTables()
  135. {
  136. //Create table posters
  137. $result1 = Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'posters` (
  138. `id_posters` INT(11) NOT NULL AUTO_INCREMENT,
  139. `id_type_posters` INT(11) NOT NULL,
  140. `image` VARCHAR(100) NULL DEFAULT NULL,
  141. `target_link` VARCHAR(100) NULL DEFAULT NULL,
  142. `position_hook` VARCHAR(50) NOT NULL,
  143. `source_link` VARCHAR(100) NULL DEFAULT NULL,
  144. `width` INT(7) NULL DEFAULT NULL,
  145. `height` INT(7) NULL DEFAULT NULL,
  146. `active` TINYINT(1) NOT NULL DEFAULT \'1\',
  147. `show_page` VARCHAR(100) NULL DEFAULT NULL,
  148. `id_product` VARCHAR(100) NULL DEFAULT NULL,
  149. `id_category` INT(11) NULL DEFAULT NULL,
  150. `product_limit` INT(11) NULL DEFAULT NULL,
  151. `position` INT(11) NULL DEFAULT NULL,
  152. PRIMARY KEY (`id_posters`)) COLLATE=\'utf8_general_ci\' ENGINE=InnoDB;');
  153. //create posters_lang table
  154. $result2 = Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'posters_lang` (
  155. `id_posters` INT(11) NOT NULL,
  156. `id_lang` INT(11) NOT NULL,
  157. `name` VARCHAR(100) NOT NULL,
  158. `caption` VARCHAR(100) NULL DEFAULT NULL,
  159. `description` VARCHAR(100) NULL DEFAULT NULL,
  160. PRIMARY KEY (`id_posters`, `id_lang`) )COLLATE=\'utf8_general_ci\'ENGINE = InnoDB;');
  161. //create posters_shop table
  162. $result3 = Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'posters_shop` (
  163. `id_posters` INT(11) NOT NULL,
  164. `id_shop` INT(11) NOT NULL,
  165. PRIMARY KEY (`id_posters`, `id_shop`) ) COLLATE=\'utf8_general_ci\' ENGINE=InnoDB;');
  166. $result4 = Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'type_posters` (
  167. `id_type_posters` INT(7) NOT NULL AUTO_INCREMENT,
  168. `name` VARCHAR(100) NULL DEFAULT NULL,
  169. PRIMARY KEY (`id_type_posters`)
  170. )COLLATE= \'utf8_general_ci\'
  171. ENGINE=InnoDB
  172. AUTO_INCREMENT=1;');
  173. $result5 = Db::getInstance()->execute('INSERT INTO `prestashop`.`ps_type_posters` (`name`) VALUES (\'image\');
  174. INSERT INTO `prestashop`.`ps_type_posters` (`name`) VALUES (\'video\');
  175. INSERT INTO `prestashop`.`ps_type_posters` (`name`) VALUES (\'product\');
  176. INSERT INTO `prestashop`.`ps_type_posters` (`name`) VALUES (\'category\');
  177. INSERT INTO `prestashop`.`ps_type_posters` (`name`) VALUES (\'best_sale\');
  178. INSERT INTO `prestashop`.`ps_type_posters` (`name`) VALUES (\'most_view\');');
  179. return $result1 && $result2 && $result3 && $result4;
  180. }
  181. public function uninstall()
  182. {
  183. if (!parent::uninstall() ||
  184. !Configuration::deleteByName('posters')
  185. )
  186. return false;
  187. if (!$this->uninstallTab('AdminPosters') || !$this->dropTables())
  188. return false;
  189. return true;
  190. }
  191. public function uninstallTab($class_name)
  192. {
  193. $id_tab = (int)Tab::getIdFromClassName($class_name);
  194. $tab = new Tab((int)$id_tab);
  195. return $tab->delete();
  196. }
  197. public function installTab($parentTab, $class, $name)
  198. {
  199. $tab = new Tab();
  200. $tab->id_parent = (int)Tab::getIdFromClassName($parentTab);
  201. $tab->name = array();
  202. foreach (Language::getLanguages(true) as $lang ){
  203. $tab->name[$lang['id_lang']] = $name;
  204. }
  205. $tab->class_name = $class;
  206. $tab->module = $this->name;
  207. $tab->active = 1;
  208. return $tab->add();
  209. }
  210. public function templateAutocomplete($id_product = 0)
  211. {
  212. $this->context->controller->addCSS('/js/jquery/plugins/autocomplete/jquery.autocomplete.css', 'all');
  213. $this->context->controller->addJS($this->_path.'views/js/posters.js');
  214. $this->context->controller->addJS('/js/jquery/plugins/autocomplete/jquery.autocomplete.js');
  215. $my_associations = Posters::getAssociationsLight($this->context->language->id, $id_product); //function that will retrieve the array of all the product associated on my module table.
  216. $this->context->smarty->assign(array(
  217. 'my_associations' => $my_associations
  218. ));
  219. return $this->context->smarty->fetch(dirname(__FILE__).'/views/templates/admin/autocomplete.tpl');
  220. }
  221. public static function getAssociationsLight($id_lang, $idPosters)
  222. {
  223. $res =array();
  224. if(!empty($idPosters) && $idPosters > 0){
  225. $id_products = Db::getInstance()->getValue('SELECT p.id_product FROM '._DB_PREFIX_.'posters p WHERE p.id_posters = '.(int)$idPosters);
  226. // $id_products = explode(',', $id_products);
  227. $id_products = rtrim($id_products, ",") ;
  228. if(!empty($id_products) && $id_products != ""){
  229. $sql = 'SELECT p.`id_product`, p.`reference`, pl.`name`
  230. FROM `'._DB_PREFIX_.'product` p
  231. '.Shop::addSqlAssociation('product', 'p').'
  232. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
  233. p.`id_product` = pl.`id_product`
  234. AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
  235. ) WHERE p.`id_product` IN ('.$id_products.')';
  236. $res = Db::getInstance()->executeS($sql);
  237. }
  238. return $res;
  239. }
  240. }
  241. }