PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/leoslideshow/controllers/admin/AdminLeoSlideshow.php

https://gitlab.com/ptisky/API_prestashop
PHP | 344 lines | 268 code | 46 blank | 30 comment | 41 complexity | 66954de63eb0cd1c6932bf439fc9f666 MD5 | raw file
  1. <?php
  2. /**
  3. * Leo Theme for Prestashop 1.6.x
  4. *
  5. * @author http://www.leotheme.com
  6. * @copyright Copyright (C) October 2013 LeoThemes.com <@emai:leotheme@gmail.com>
  7. * <info@leotheme.com>.All rights reserved.
  8. * @license GNU General Public License version 2
  9. */
  10. if (!defined('_PS_VERSION_'))
  11. exit;
  12. class AdminLeoSlideshowController extends ModuleAdminController
  13. {
  14. protected $max_image_size = null;
  15. public $theme_name;
  16. public $img_path;
  17. public $img_url;
  18. public function __construct()
  19. {
  20. $this->bootstrap = true;
  21. $this->max_image_size = (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE');
  22. parent::__construct();
  23. $this->theme_name = Context::getContext()->shop->getTheme();
  24. $this->img_path = _PS_ALL_THEMES_DIR_.$this->theme_name.'/img/modules/leoslideshow/';
  25. $this->img_url = __PS_BASE_URI__.'themes/'.$this->theme_name.'/img/modules/leoslideshow/';
  26. }
  27. public function setMedia()
  28. {
  29. $this->addCss(__PS_BASE_URI__.str_replace('//', '/', 'modules/leoslideshow').'/views/css/admin/admincontroller.css', 'all');
  30. //_PS_THEME_DIR_
  31. return parent::setMedia();
  32. }
  33. public function postProcess()
  34. {
  35. if (($img_name = Tools::getValue('imgName', false)) !== false)
  36. unlink($this->img_path.$img_name);
  37. //export process
  38. if (Tools::getValue('exportGroup'))
  39. {
  40. $group = LeoSlideshowGroup::getGroupByID(Tools::getValue('id_group'));
  41. $sliders = $this->getSlidesByGroup(Tools::getValue('id_group'));
  42. $language_field = array('title', 'link', 'image', 'thumbnail', 'video', 'layersparams');
  43. $languages = Language::getLanguages();
  44. $lang_list = array();
  45. foreach ($languages as $lang)
  46. $lang_list[$lang['id_lang']] = $lang['iso_code'];
  47. foreach ($sliders as $slider)
  48. {
  49. $curent_lang = 'en';
  50. foreach ($slider as $key => $value)
  51. {
  52. if ($key == 'id_lang')
  53. {
  54. $curent_lang = $lang_list[$value];
  55. continue;
  56. }
  57. if (in_array($key, $language_field))
  58. $group['sliders'][$slider['id']][$key][$curent_lang] = $value;
  59. else
  60. $group['sliders'][$slider['id']][$key] = $value;
  61. }
  62. }
  63. header('Content-Type: plain/text');
  64. header('Content-Disposition: Attachment; filename=export_group_'.Tools::getValue('id_group').'_'.time().'.txt');
  65. header('Pragma: no-cache');
  66. die(LeoSlideshowSlide::base64Encode(Tools::jsonEncode($group)));
  67. }
  68. parent::postProcess();
  69. }
  70. public function importGroup()
  71. {
  72. $type = Tools::strtolower(Tools::substr(strrchr($_FILES['import_file']['name'], '.'), 1));
  73. if (isset($_FILES['import_file']) && $type == 'txt' && isset($_FILES['import_file']['tmp_name']) && !empty($_FILES['import_file']['tmp_name']))
  74. {
  75. include_once(_PS_MODULE_DIR_.'leoslideshow/classes/LeoSlideshowGroup.php');
  76. include_once(_PS_MODULE_DIR_.'leoslideshow/classes/LeoSlideshowSlide.php');
  77. $content = Tools::file_get_contents($_FILES['import_file']['tmp_name']);
  78. $content = Tools::jsonDecode(LeoSlideshowSlide::base64Decode($content), true);
  79. $language_field = array('title', 'link', 'image', 'thumbnail', 'video', 'layersparams');
  80. $languages = Language::getLanguages();
  81. $lang_list = array();
  82. foreach ($languages as $lang)
  83. $lang_list[$lang['iso_code']] = $lang['id_lang'];
  84. $override_group = Tools::getValue('override_group');
  85. //override or edit
  86. if ($override_group && LeoSlideshowGroup::groupExists($content['id_leoslideshow_groups']))
  87. {
  88. $mod_group = new LeoSlideshowGroup($content['id_leoslideshow_groups']);
  89. //edit group
  90. $mod_group = $this->setDataForGroup($mod_group, $content);
  91. if (!$mod_group->update())
  92. return false;
  93. LeoSlideshowGroup::deleteAllSlider($content['id_leoslideshow_groups']);
  94. foreach ($content['sliders'] as $slider)
  95. {
  96. $mod_slide = new LeoSlideshowSlide();
  97. foreach ($slider as $key => $val)
  98. {
  99. if (in_array($key, $language_field))
  100. {
  101. foreach ($val as $key_lang => $val_lang)
  102. $mod_slide->{$key}[$lang_list[$key_lang]] = $val_lang;
  103. }
  104. else
  105. $mod_slide->{$key} = $val;
  106. }
  107. $mod_slide->id_group = $mod_group->id;
  108. if (isset($slider['id']) && $slider['id'] && LeoSlideshowSlide::sliderExist($slider['id']))
  109. $mod_slide->update();
  110. else
  111. $mod_slide->add();
  112. }
  113. }
  114. else
  115. {
  116. $mod_group = new LeoSlideshowGroup();
  117. $mod_group = $this->setDataForGroup($mod_group, $content);
  118. if (!$mod_group->add())
  119. return false;
  120. foreach ($content['sliders'] as $slider)
  121. {
  122. $mod_slide = new LeoSlideshowSlide();
  123. foreach ($slider as $key => $val)
  124. {
  125. if (in_array($key, $language_field))
  126. {
  127. foreach ($val as $key_lang => $val_lang)
  128. $mod_slide->{$key}[$lang_list[$key_lang]] = $val_lang;
  129. }
  130. else
  131. $mod_slide->{$key} = $val;
  132. }
  133. $mod_slide->id_group = $mod_group->id;
  134. $mod_slide->id = 0;
  135. $mod_slide->add();
  136. }
  137. }
  138. //add new
  139. //return true;
  140. }
  141. Tools::redirectAdmin('index.php?controller=AdminModules&token='.Tools::getAdminTokenLite('AdminModules').'&configure=leoslideshow&tab_module=leotheme&module_name=leoslideshow&conf=4');
  142. //return false;
  143. }
  144. public function setDataForGroup($group, $content)
  145. {
  146. $group->title = $content['title'];
  147. $group->id_shop = $this->context->shop->id;
  148. $group->hook = $content['hook'];
  149. $group->active = $content['active'];
  150. $group->params = $content['params'];
  151. $group->sliders = $content['sliders'];
  152. return $group;
  153. }
  154. /*
  155. * get all slider data
  156. */
  157. public function getSlidesByGroup($id_group)
  158. {
  159. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  160. SELECT lsll.`id_lang`, lsl.`id_leoslideshow_slides` as id,
  161. lsl.*,lsll.*
  162. FROM '._DB_PREFIX_.'leoslideshow_slides lsl
  163. LEFT JOIN '._DB_PREFIX_.'leoslideshow_slides_lang lsll ON (lsl.id_leoslideshow_slides = lsll.id_leoslideshow_slides)
  164. WHERE lsl.id_group = '.(int)$id_group.'
  165. ORDER BY lsl.position');
  166. }
  167. /**
  168. * renderForm contains all necessary initialization needed for all tabs
  169. *
  170. * @return void
  171. */
  172. public function renderList()
  173. {
  174. //this code for typo
  175. $typo = Tools::getValue('typo');
  176. if ($typo)
  177. {
  178. //check css file in theme
  179. if (file_exists(_PS_THEME_DIR_.'css/modules/leoslideshow/views/css/typo/typo.css'))
  180. $typo_dir = _THEME_DIR_.'css/'.str_replace('//', '/', 'modules/leoslideshow').'/views/css/typo/typo.css';
  181. else
  182. $typo_dir = __PS_BASE_URI__.str_replace('//', '/', 'modules/leoslideshow').'/views/css/typo/typo.css';
  183. $this->addCss($typo_dir, 'all');
  184. $this->addJS(__PS_BASE_URI__.'modules/leoslideshow/views/js/admin/jquery-ui-1.10.3.custom.min.js');
  185. $content = Tools::file_get_contents($this->context->link->getMediaLink($typo_dir));
  186. preg_match_all('#\.tp-caption\.(\w+)\s*{\s*#', $content, $matches);
  187. if (isset($matches[1]))
  188. $captions = $matches[1];
  189. $tpl = $this->createTemplate('typo.tpl');
  190. $tpl->assign(array(
  191. 'typoDir' => $typo_dir,
  192. 'captions' => $captions,
  193. 'field' => Tools::getValue('field')
  194. ));
  195. return $tpl->fetch();
  196. }
  197. //this code for select or upload IMG
  198. $tpl = $this->createTemplate('imagemanager.tpl');
  199. $sort_by = Tools::getValue('sortBy');
  200. $reload_slider_image = Tools::getValue('reloadSliderImage');
  201. $images = $this->getImageList($sort_by);
  202. $tpl->assign(array(
  203. 'images' => $images,
  204. 'reloadSliderImage' => $reload_slider_image,
  205. ));
  206. if ($reload_slider_image)
  207. die(Tools::jsonEncode($tpl->fetch()));
  208. $image_uploader = new HelperImageUploader('file');
  209. $image_uploader->setSavePath($this->img_path);
  210. $image_uploader->setMultiple(true)->setUseAjax(true)->setUrl(
  211. Context::getContext()->link->getAdminLink('AdminLeoSlideshow').'&ajax=1&action=addSliderImage');
  212. $tpl->assign(array(
  213. 'countImages' => count($images),
  214. 'images' => $images,
  215. 'max_image_size' => $this->max_image_size / 1024 / 1024,
  216. 'image_uploader' => $image_uploader->render(),
  217. 'imgManUrl' => Context::getContext()->link->getAdminLink('AdminLeoSlideshow'),
  218. 'token' => $this->token,
  219. 'imgUploadDir' => $this->img_path
  220. ));
  221. return $tpl->fetch();
  222. }
  223. public function getImageList($sort_by)
  224. {
  225. $path = $this->img_path;
  226. $this->createFolderUpImage();
  227. $images = glob($path.'/{*.jpeg,*.JPEG,*.jpg,*.JPG,*.gif,*.GIF,*.png,*.PNG}', GLOB_BRACE);
  228. if (!$images)
  229. $images = $this->getAllImage($path);
  230. if ($sort_by == 'name_desc')
  231. rsort($images);
  232. if ($sort_by == 'date' || $sort_by == 'date_desc')
  233. array_multisort(array_map('filemtime', $images), SORT_NUMERIC, SORT_DESC, $images);
  234. if ($sort_by == 'date_desc')
  235. rsort($images);
  236. $result = array();
  237. foreach ($images as &$file)
  238. {
  239. $file_info = pathinfo($file);
  240. $result[] = array('name' => $file_info['basename'], 'link' => $this->img_url.$file_info['basename']);
  241. }
  242. return $result;
  243. }
  244. public function getAllImage($path)
  245. {
  246. $images = array();
  247. foreach (scandir($path) as $d)
  248. if (preg_match('/(.*)\.(jpg|png|gif|jpeg)$/', $d))
  249. $images[] = $d;
  250. return $images;
  251. }
  252. public function ajaxProcessaddSliderImage()
  253. {
  254. if (isset($_FILES['file']))
  255. {
  256. $image_uploader = new HelperUploader('file');
  257. $this->createFolderUpImage();
  258. $image_uploader->setSavePath($this->img_path);
  259. $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size);
  260. $files = $image_uploader->process();
  261. $total_errors = array();
  262. foreach ($files as &$file)
  263. {
  264. $errors = array();
  265. // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
  266. if (!ImageManager::checkImageMemoryLimit($file['save_path']))
  267. $errors[] = Tools::displayError('Due to memory limit restrictions, this image cannot be loaded. Please increase your memory_limit value via your server\'s configuration settings. ');
  268. if (count($errors))
  269. $total_errors = array_merge($total_errors, $errors);
  270. //unlink($file['save_path']);
  271. //Necesary to prevent hacking
  272. unset($file['save_path']);
  273. //Add image preview and delete url
  274. }
  275. if (count($total_errors))
  276. $this->context->controller->errors = array_merge($this->context->controller->errors, $total_errors);
  277. $images = $this->getImageList('date');
  278. $tpl = $this->createTemplate('imagemanager.tpl');
  279. $tpl->assign(array(
  280. 'images' => $images,
  281. 'reloadSliderImage' => 1,
  282. 'link' => Context::getContext()->link
  283. ));
  284. die(Tools::jsonEncode($tpl->fetch()));
  285. }
  286. }
  287. public function createFolderUpImage()
  288. {
  289. if (!is_dir($this->img_path))
  290. {
  291. if (!is_dir(_PS_ALL_THEMES_DIR_.$this->theme_name.'/img'))
  292. mkdir(_PS_ALL_THEMES_DIR_.$this->theme_name.'/img', 0755);
  293. if (!is_dir(_PS_ALL_THEMES_DIR_.$this->theme_name.'/img/modules'))
  294. mkdir(_PS_ALL_THEMES_DIR_.$this->theme_name.'/img/modules', 0755);
  295. mkdir(_PS_ALL_THEMES_DIR_.$this->theme_name.'/img/modules/leoslideshow', 0755);
  296. }
  297. }
  298. }