PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/opartslideshow/opartslideshow.php

https://gitlab.com/ptisky/API_prestashop
PHP | 347 lines | 207 code | 41 blank | 99 comment | 28 complexity | 509bda4d50cc405f6c31306143f14aca MD5 | raw file
  1. <?php
  2. // Security
  3. if (!defined('_PS_VERSION_'))
  4. exit;
  5. // Loading Models
  6. require_once(_PS_MODULE_DIR_ . 'opartslideshow/models/MySlideshow.php');
  7. require_once(_PS_MODULE_DIR_ . 'opartslideshow/models/MyImage.php');
  8. class Opartslideshow extends Module
  9. {
  10. public function __construct()
  11. {
  12. $this->name = 'opartslideshow';
  13. $this->tab = 'front_office_features';
  14. $this->version = '15-06-03';
  15. $this->author = 'Op\'art - Olivier CLEMENCE';
  16. $this->need_instance = 0;
  17. parent::__construct();
  18. $this->displayName = $this->l('Op\'art slideshow');
  19. $this->description = $this->l('multiple sideshow');
  20. $this->confirmUninstall = $this->l('Are you sure you want to delete this module ?');
  21. if ($this->active && Configuration::get('OPART_SLIDESHOW_CONF') == 'ok')
  22. $this->warning = $this->l('You have to configure your module');
  23. }
  24. public function install()
  25. {
  26. // Install SQL
  27. include(dirname(__FILE__).'/sql/install.php');
  28. foreach ($sql as $s)
  29. if (!Db::getInstance()->execute($s))
  30. return false;
  31. // Install Tabs
  32. $parent_tab = new Tab();
  33. //$parent_tab->name = 'Op\'art Slideshow';
  34. $parent_tab->name = array();
  35. foreach (Language::getLanguages() as $language)
  36. $parent_tab->name[$language['id_lang']] = 'Op\'art Slideshow';
  37. $parent_tab->class_name = 'AdminMainOpart';
  38. $parent_tab->id_parent = 0;
  39. $parent_tab->module = $this->name;
  40. $parent_tab->add();
  41. $tab1 = new Tab();
  42. //$tab1->name = 'Slideshow';
  43. $tab1->name = array();
  44. foreach (Language::getLanguages() as $language)
  45. $tab1->name[$language['id_lang']] = 'Slideshow';
  46. $tab1->class_name = 'AdminSlideshow';
  47. $tab1->id_parent = $parent_tab->id;
  48. $tab1->module = $this->name;
  49. $tab1->add();
  50. $tab2 = new Tab();
  51. //$tab2->name = 'Images';
  52. $tab2->name = array();
  53. foreach (Language::getLanguages() as $language)
  54. $tab2->name[$language['id_lang']] = 'Images';
  55. $tab2->class_name = 'AdminSlideshowImage';
  56. $tab2->id_parent = $parent_tab->id;
  57. $tab2->module = $this->name;
  58. $tab2->add();
  59. //Init
  60. Configuration::updateValue('OPART_SLIDESHOW_CONF', '');
  61. // Install Module
  62. if (
  63. parent::install() == false
  64. OR !$this->registerHook('displayTop')
  65. OR !$this->registerHook('displayLeftColumn')
  66. OR !$this->registerHook('displayRightColumn')
  67. OR !$this->registerHook('displayHome')
  68. OR !$this->registerHook('displayOpartSlideshowHook')
  69. OR !$this->registerHook('displayHeader')
  70. )
  71. return false;
  72. return true;
  73. }
  74. public function uninstall()
  75. {
  76. // Uninstall SQL
  77. include(dirname(__FILE__).'/sql/uninstall.php');
  78. foreach ($sql as $s)
  79. if (!Db::getInstance()->execute($s))
  80. return false;
  81. Configuration::deleteByName('OPART_SLIDESHOW_CONF');
  82. // Uninstall Tabs
  83. $tab = new Tab((int)Tab::getIdFromClassName('AdminSlideshow'));
  84. $tab->delete();
  85. $tab = new Tab((int)Tab::getIdFromClassName('AdminSlideshowImage'));
  86. $tab->delete();
  87. $tab = new Tab((int)Tab::getIdFromClassName('AdminMainOpart'));
  88. $tab->delete();
  89. //delete image
  90. $uploadDir=_PS_ROOT_DIR_.'/modules/opartslideshow/upload/';
  91. $photos=$this->searchPhoto($uploadDir);
  92. foreach($photos as $photo) {
  93. if(file_exists($uploadDir.$photo))
  94. unlink($uploadDir.$photo);
  95. }
  96. // Uninstall Module
  97. if (!parent::uninstall())
  98. return false;
  99. return true;
  100. }
  101. private function searchPhoto($dir){
  102. $photoArray=array();
  103. if (is_dir($dir)) {
  104. if ($dh = opendir($dir)) {
  105. while (($photo = readdir($dh)) !== false) {
  106. if($photo!="." && $photo!=".." && is_file($dir.'/'.$photo)) {
  107. $photoArray[]=$photo;
  108. }
  109. }
  110. closedir($dh);
  111. }
  112. }
  113. return $photoArray;
  114. }
  115. /*
  116. private function prepareHook($hookName) {
  117. //recup slides
  118. $sqlSlide='
  119. SELECT s.id_opartslideshow_slideshow,s.active,s.width,s.height,s.spw,s.sph,s.delay,s.sDelay,s.opacity,s.titleSpeed,s.effect,s.navigation,s.links,s.hoverpause,s.home,sl.name
  120. FROM '._DB_PREFIX_.'opartslideshow_slideshow s
  121. LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_lang sl ON (s.id_opartslideshow_slideshow=sl.id_opartslideshow_slideshow)
  122. WHERE s.active=1 AND hook="'.$hookName.'" AND sl.id_lang = '.(int)$this->context->language->id;
  123. if (!$results = Db::getInstance()->ExecuteS($sqlSlide))
  124. return false;
  125. if(get_class($this->context->controller)=="IndexController") {
  126. foreach($results as $result) {
  127. if($result['home']==1)
  128. $slides[$result['id_opartslideshow_slideshow']]=$result;
  129. }
  130. $showSlide=true;
  131. }
  132. if(get_class($this->context->controller)=="CategoryController") {
  133. $idCat=(int)Tools::getValue('id_category');
  134. print_r($result);
  135. echo "idCat=".$idCat;
  136. $sqlCat="SELECT id_opartslideshow_slideshow WHERE id_category=".$idCat;
  137. $slideCat = Db::getInstance()->ExecuteS($sqlSlide);
  138. foreach($slideCat as $idSlide) {
  139. if($result['id_opartslideshow_slideshow']==$idSlide)
  140. $slides[$result['id_opartslideshow_slideshow']]=$result;
  141. }
  142. if($result['id_category']==$idCat)
  143. $slides[$result['id_opartslideshow_slideshow']]=$result;
  144. $showSlide=true;
  145. }
  146. if(get_class($this->context->controller)=="ProductController") {
  147. $idProd=(int)Tools::getValue('id_product');
  148. foreach($results as $result) {
  149. if($result['id_product']==$idProd)
  150. $slides[$result['id_opartslideshow_slideshow']]=$result;
  151. }
  152. $showSlide=true;
  153. }
  154. if($showSlide==false)
  155. return false;
  156. $addToWhereImages="";
  157. $slides=array();
  158. foreach($results as $result) {
  159. $slides[$result['id_opartslideshow_slideshow']]=$result;
  160. $addToWhereImages.=($addToWhereImages=="")?"i.id_opartslideshow_slideshow=".$result['id_opartslideshow_slideshow']:" OR i.id_opartslideshow_slideshow=".$result['id_opartslideshow_slideshow'];
  161. }
  162. if($addToWhereImages!="")
  163. $addToWhereImages=" AND (".$addToWhereImages.")";
  164. $sqlImage='
  165. SELECT i.id_opartslideshow_slideshow,i.id_opartslideshow_slideshow_image,i.filename,il.name,il.targeturl,il.description
  166. FROM '._DB_PREFIX_.'opartslideshow_slideshow_image i
  167. LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_image_lang il ON (i.id_opartslideshow_slideshow_image=il.id_opartslideshow_slideshow_image)
  168. WHERE il.id_lang = '.(int)$this->context->language->id.' AND i.active=1'.$addToWhereImages.'
  169. ORDER BY i.position
  170. ';
  171. $results = Db::getInstance()->ExecuteS($sqlImage);
  172. foreach($results as $result) {
  173. $slides[$result['id_opartslideshow_slideshow']]['images'][]=$result;
  174. }
  175. $effectNames=array("random","swirl", "rain", "straight");
  176. $this->smarty->assign('effectNames', $effectNames);
  177. $this->smarty->assign('slides', $slides);
  178. return true;
  179. }
  180. */
  181. private function prepareHook($hookName) {
  182. $addToFrom="";
  183. $addToWhere="";
  184. $showSlide=false;
  185. if(get_class($this->context->controller)=="IndexController") {
  186. $addToWhere=' AND s.home=1';
  187. $showSlide=true;
  188. }
  189. if(get_class($this->context->controller)=="CategoryController") {
  190. $addToFrom='LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_category sc ON (s.id_opartslideshow_slideshow = sc.id_opartslideshow_slideshow)';
  191. $addToWhere=' AND (sc.id_category='.(int)Tools::getValue('id_category').' OR s.showOnCat=1)';
  192. $showSlide=true;
  193. }
  194. if(get_class($this->context->controller)=="ProductController") {
  195. $idProd=(int)Tools::getValue('id_product');
  196. $product=new Product($idProd);
  197. $idCat=$product->id_category_default;
  198. $addToFrom='LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_product sp ON (s.id_opartslideshow_slideshow = sp.id_opartslideshow_slideshow)';
  199. $addToFrom.=' LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_category sc ON (s.id_opartslideshow_slideshow = sc.id_opartslideshow_slideshow)';
  200. $addToWhere=' AND (sp.id_product='.$idProd.' OR sc.id_category='.$idCat.' OR s.showOnCat=1 OR s.showOnProd=1)';
  201. $showSlide=true;
  202. }
  203. if(get_class($this->context->controller)=="CmsController") {
  204. $addToFrom='LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_cms scm ON (s.id_opartslideshow_slideshow = scm.id_opartslideshow_slideshow)';
  205. $addToWhere=' AND (scm.id_cms='.(int)Tools::getValue('id_cms').' OR s.showOnCms=1)';
  206. $showSlide=true;
  207. }
  208. if($showSlide==false)
  209. return false;
  210. $sqlSlide='
  211. SELECT s.id_opartslideshow_slideshow,s.active,s.width,s.height,s.spw,s.sph,s.delay,s.sDelay,s.opacity,s.titleSpeed,s.effect,s.navigation,s.links,s.hoverpause,sl.name
  212. FROM '._DB_PREFIX_.'opartslideshow_slideshow s
  213. LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_lang sl ON (s.id_opartslideshow_slideshow=sl.id_opartslideshow_slideshow)
  214. '.$addToFrom.'
  215. WHERE s.active=1 AND hook="'.$hookName.'" AND sl.id_lang = '.(int)$this->context->language->id.$addToWhere.'
  216. ORDER BY sl.name
  217. ';
  218. //echo $sqlSlide."<br />";
  219. if (!$results = Db::getInstance()->ExecuteS($sqlSlide))
  220. return false;
  221. $addToWhereImages="";
  222. $slides=array();
  223. foreach($results as $result) {
  224. $slides[$result['id_opartslideshow_slideshow']]=$result;
  225. $addToWhereImages.=($addToWhereImages=="")?"i.id_opartslideshow_slideshow=".$result['id_opartslideshow_slideshow']:" OR i.id_opartslideshow_slideshow=".$result['id_opartslideshow_slideshow'];
  226. }
  227. if($addToWhereImages!="")
  228. $addToWhereImages=" AND (".$addToWhereImages.")";
  229. $sqlImage='
  230. SELECT i.id_opartslideshow_slideshow,i.id_opartslideshow_slideshow_image,i.filename,il.name,il.targeturl,il.description
  231. FROM '._DB_PREFIX_.'opartslideshow_slideshow_image i
  232. LEFT JOIN '._DB_PREFIX_.'opartslideshow_slideshow_image_lang il ON (i.id_opartslideshow_slideshow_image=il.id_opartslideshow_slideshow_image)
  233. WHERE il.id_lang = '.(int)$this->context->language->id.' AND i.active=1'.$addToWhereImages.'
  234. ORDER BY i.position
  235. ';
  236. $results = Db::getInstance()->ExecuteS($sqlImage);
  237. foreach($results as $result) {
  238. $slides[$result['id_opartslideshow_slideshow']]['images'][]=$result;
  239. }
  240. $effectNames=array("random","swirl", "rain", "straight");
  241. $this->smarty->assign('effectNames', $effectNames);
  242. $this->smarty->assign('slides', $slides);
  243. return true;
  244. }
  245. public function hookDisplayHeader() {
  246. $this->context->controller->addJS($this->_path.'js/coin-slider.js');
  247. $this->context->controller->addCSS($this->_path.'css/coin-slider-styles.css');
  248. }
  249. public function displayHook($tplFile='slideshow.tpl') {
  250. /*if ($this->context->getMobileDevice() != false)
  251. return false;*/
  252. //$this->context->controller->addJS(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js');
  253. /*$this->context->controller->addJS($this->_path.'js/coin-slider.js');
  254. $this->context->controller->addCSS($this->_path.'css/coin-slider-styles.css');*/
  255. //$this->displayHeader();
  256. return $this->display(__FILE__, $tplFile);
  257. }
  258. public function hookDisplayTop()
  259. {
  260. if(!$this->prepareHook('displayTop'))
  261. return false;
  262. return $this->displayHook('slideshowTop.tpl');
  263. }
  264. public function hookDisplayOpartSlideshowHook()
  265. {
  266. if(!$this->prepareHook('displayOpartSlideshowHook'))
  267. return false;
  268. return $this->displayHook();
  269. }
  270. public function hookDisplayHome()
  271. {
  272. if(!$this->prepareHook('displayHome'))
  273. return false;
  274. return $this->displayHook();
  275. }
  276. public function hookLeftColumn()
  277. {
  278. if(!$this->prepareHook('displayLeftColumn'))
  279. return false;
  280. return $this->displayHook();
  281. }
  282. public function hookRightColumn()
  283. {
  284. if(!$this->prepareHook('displayRightColumn'))
  285. return false;
  286. return $this->displayHook();
  287. }
  288. public function getContent()
  289. {
  290. $this->_html=$this->display(__FILE__, 'views/templates/admin/configure.tpl');
  291. return $this->_html;
  292. }
  293. }