PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/leoblog/controllers/admin/AdminLeoblogCategories.php

https://gitlab.com/ptisky/API_prestashop
PHP | 459 lines | 386 code | 33 blank | 40 comment | 30 complexity | c1d2afcaf9d851706640ec66d1b7a2de MD5 | raw file
  1. <?php
  2. /**
  3. * Leo Prestashop Blockleoblogs for Prestashop 1.6.x
  4. *
  5. * @package blockleoblogs
  6. * @version 3.0
  7. * @author http://www.leotheme.com
  8. * @copyright Copyright (C) October 2013 LeoThemes.com <@emai:leotheme@gmail.com>
  9. * <info@leotheme.com>.All rights reserved.
  10. * @license GNU General Public License version 2
  11. */
  12. include_once(_PS_MODULE_DIR_.'leoblog/loader.php');
  13. class AdminLeoblogCategoriesController extends AdminController
  14. {
  15. public $name = 'leoblog';
  16. protected $fields_form = array();
  17. public function __construct()
  18. {
  19. $this->bootstrap = true;
  20. $this->id_leoblogcat = true;
  21. $this->table = 'leoblogcat';
  22. $this->className = 'leoblogcat';
  23. $this->lang = true;
  24. $this->fields_options = array();
  25. $this->toolbar_title = $this->l('Categories Management');
  26. parent::__construct();
  27. }
  28. /**
  29. * Build List linked Icons Toolbar
  30. */
  31. public function initPageHeaderToolbar()
  32. {
  33. $this->context->controller->addJS(__PS_BASE_URI__.'js/jquery/ui/jquery.ui.sortable.min.js');
  34. if (file_exists(_PS_THEME_DIR_.'js/modules/leoblog/assets/admin/jquery.nestable.js'))
  35. $this->context->controller->addJS(__PS_BASE_URI__.'modules/leoblog/assets/admin/jquery.nestable.js');
  36. else
  37. $this->context->controller->addJS(__PS_BASE_URI__.'modules/leoblog/views/js/admin/jquery.nestable.js');
  38. if (file_exists(_PS_THEME_DIR_.'js/modules/leoblog/assets/admin/form.js'))
  39. $this->context->controller->addJS(__PS_BASE_URI__.'modules/leoblog/assets/admin/form.js');
  40. else
  41. $this->context->controller->addJS(__PS_BASE_URI__.'modules/leoblog/views/js/admin/form.js');
  42. $this->context->controller->addJS(__PS_BASE_URI__.'js/jquery/plugins/jquery.cookie-plugin.js');
  43. $this->context->controller->addJS(__PS_BASE_URI__.'js/jquery/ui/jquery.ui.tabs.min.js');
  44. $this->context->controller->addCss(__PS_BASE_URI__.'js/jquery/ui/themes/base/jquery.ui.tabs.css');
  45. if (file_exists(_PS_THEME_DIR_.'css/modules/leoblog/assets/admin/form.css'))
  46. $this->context->controller->addCss(__PS_BASE_URI__.'modules/leoblog/assets/admin/form.css');
  47. else
  48. $this->context->controller->addCss(__PS_BASE_URI__.'modules/leoblog/views/css/admin/form.css');
  49. if (empty($this->display))
  50. parent::initPageHeaderToolbar();
  51. }
  52. /**
  53. *
  54. */
  55. public function setMedia()
  56. {
  57. parent::setMedia();
  58. $this->addJqueryUi('ui.widget');
  59. $this->addJqueryPlugin('tagify');
  60. }
  61. /**
  62. * get live Edit URL
  63. */
  64. public function getLiveEditUrl($live_edit_params)
  65. {
  66. $url = $this->context->shop->getBaseURL().Dispatcher::getInstance()->createUrl('index', (int)$this->context->language->id, $live_edit_params);
  67. if (Configuration::get('PS_REWRITING_SETTINGS'))
  68. $url = str_replace('index.php', '', $url);
  69. return $url;
  70. }
  71. /**
  72. * add toolbar icons
  73. */
  74. public function initToolbar()
  75. {
  76. $this->context->smarty->assign('toolbar_scroll', 1);
  77. $this->context->smarty->assign('show_toolbar', 1);
  78. $this->context->smarty->assign('toolbar_btn', $this->toolbar_btn);
  79. $this->context->smarty->assign('title', $this->toolbar_title);
  80. }
  81. public function postProcess()
  82. {
  83. if (Tools::getValue('doupdatepos') && Tools::isSubmit('updatePosition'))
  84. {
  85. $list = Tools::getValue('list');
  86. $root = 1;
  87. $child = array();
  88. foreach ($list as $id => $parent_id)
  89. {
  90. if ($parent_id <= 0)
  91. {
  92. # validate module
  93. $parent_id = $root;
  94. }
  95. $child[$parent_id][] = $id;
  96. }
  97. $res = true;
  98. foreach ($child as $id_parent => $menus)
  99. {
  100. $i = 0;
  101. foreach ($menus as $id_leoblogcat)
  102. {
  103. $res &= Db::getInstance()->execute('
  104. UPDATE `'._DB_PREFIX_.'leoblogcat` SET `position` = '.(int)$i.', id_parent = '.(int)$id_parent.'
  105. WHERE `id_leoblogcat` = '.(int)$id_leoblogcat
  106. );
  107. $i++;
  108. }
  109. }
  110. die($this->l('Update Positions Done'));
  111. }
  112. /* delete megamenu item */
  113. if (Tools::getValue('dodel'))
  114. {
  115. $obj = new leoblogcat((int)Tools::getValue('id_leoblogcat'));
  116. $res = $obj->delete();
  117. Tools::redirectAdmin(AdminController::$currentIndex.'&token='.Tools::getValue('token'));
  118. }
  119. if (Tools::isSubmit('save'.$this->name) && Tools::isSubmit('active'))
  120. {
  121. if ($id_leoblogcat = Tools::getValue('id_leoblogcat'))
  122. {
  123. # validate module
  124. $megamenu = new leoblogcat((int)$id_leoblogcat);
  125. }
  126. else
  127. {
  128. # validate module
  129. $megamenu = new leoblogcat();
  130. }
  131. $this->copyFromPost($megamenu, $this->table);
  132. $megamenu->id_shop = $this->context->shop->id;
  133. if ($megamenu->validateFields(false) && $megamenu->validateFieldsLang(false))
  134. {
  135. $megamenu->save();
  136. if (isset($_FILES['image']) && isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name']))
  137. {
  138. if (ImageManager::validateUpload($_FILES['image']))
  139. return false;
  140. elseif (!($tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['image']['tmp_name'], $tmp_name))
  141. return false;
  142. elseif (!ImageManager::resize($tmp_name, _LEOBLOG_BLOG_IMG_DIR_.'c/'.$_FILES['image']['name']))
  143. return false;
  144. unlink($tmp_name);
  145. $megamenu->image = $_FILES['image']['name'];
  146. $megamenu->save();
  147. }
  148. Tools::redirectAdmin(AdminController::$currentIndex.'&saveleoblog&token='.Tools::getValue('token').'&id_leoblogcat='.$megamenu->id);
  149. }
  150. else
  151. {
  152. // validate module
  153. $this->_html .= '<div class="conf error alert alert-warning">'.$this->l('An error occurred while attempting to save.').'</div>';
  154. }
  155. }
  156. }
  157. /**
  158. *
  159. *
  160. */
  161. public function renderList()
  162. {
  163. $this->initToolbar();
  164. if (!$this->loadObject(true))
  165. return;
  166. // $id_lang = $this->context->language->id;
  167. // $id_leoblogcat = (int) (Tools::getValue('id_leoblogcat'));
  168. $obj = $this->object;
  169. $tree = $obj->getTree();
  170. $menus = $obj->getDropdown(null, $obj->id_parent);
  171. $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  172. $templates = LeoBlogHelper::getTemplates();
  173. $soption = array(
  174. array(
  175. 'id' => 'active_on',
  176. 'value' => 1,
  177. 'label' => $this->l('Enabled')
  178. ),
  179. array(
  180. 'id' => 'active_off',
  181. 'value' => 0,
  182. 'label' => $this->l('Disabled')
  183. )
  184. );
  185. $this->fields_form[0]['form'] = array(
  186. 'legend' => array(
  187. 'title' => $this->l('Category Form.'),
  188. ),
  189. 'input' => array(
  190. array(
  191. 'type' => 'hidden',
  192. 'label' => $this->l('Category ID'),
  193. 'name' => 'id_leoblogcat',
  194. 'default' => 0,
  195. ),
  196. array(
  197. 'type' => 'select',
  198. 'label' => $this->l('Theme - Template'),
  199. 'name' => 'template',
  200. 'options' => array('query' => $templates,
  201. 'id' => 'template',
  202. 'name' => 'template'),
  203. 'default' => 'default',
  204. ),
  205. array(
  206. 'type' => 'text',
  207. 'label' => $this->l('Meta title:'),
  208. 'default' => '',
  209. 'name' => 'title',
  210. 'id' => 'name', // for copyMeta2friendlyURL compatibility
  211. 'lang' => true,
  212. 'required' => true,
  213. 'class' => 'copyMeta2friendlyURL',
  214. 'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}'
  215. ),
  216. array(
  217. 'type' => 'text',
  218. 'label' => $this->l('Friendly URL'),
  219. 'name' => 'link_rewrite',
  220. 'required' => true,
  221. 'lang' => true,
  222. 'default' => '',
  223. 'hint' => $this->l('Only letters and the minus (-) character are allowed')
  224. ),
  225. array(
  226. 'type' => 'select',
  227. 'label' => $this->l('Parent ID'),
  228. 'name' => 'id_parent',
  229. 'options' => array('query' => $menus,
  230. 'id' => 'id',
  231. 'name' => 'title'),
  232. 'default' => 'url',
  233. ),
  234. array(
  235. 'type' => 'switch',
  236. 'label' => $this->l('Is Active'),
  237. 'name' => 'active',
  238. 'values' => $soption,
  239. 'default' => '1',
  240. ),
  241. array(
  242. 'type' => 'switch',
  243. 'label' => $this->l('Show Title'),
  244. 'name' => 'show_title',
  245. 'values' => $soption,
  246. 'default' => '1',
  247. ),
  248. array(
  249. 'type' => 'text',
  250. 'label' => $this->l('Addion Css Class'),
  251. 'name' => 'menu_class',
  252. 'display_image' => true,
  253. 'default' => ''
  254. ),
  255. array(
  256. 'type' => 'text',
  257. 'label' => $this->l('Menu Icon Class'),
  258. 'name' => 'icon_class',
  259. 'display_image' => true,
  260. 'default' => '',
  261. 'desc' => $this->l('The module integrated with FontAwesome').'. '
  262. .$this->l('Check list of icons and class name in here')
  263. .' <a href="http://fontawesome.io/" target="_blank">http://fontawesome.io/</a> or your icon class'
  264. ),
  265. array(
  266. 'type' => 'file',
  267. 'label' => $this->l('Image'),
  268. 'name' => 'image',
  269. 'display_image' => true,
  270. 'default' => '',
  271. 'desc' => $this->l(''),
  272. 'thumb' => '',
  273. 'title' => $this->l('Icon Preview'),
  274. ),
  275. array(
  276. 'type' => 'textarea',
  277. 'label' => $this->l('Content'),
  278. 'name' => 'content_text',
  279. 'lang' => true,
  280. 'default' => '',
  281. 'autoload_rte' => true
  282. ),
  283. ),
  284. 'submit' => array(
  285. 'title' => $this->l('Save'),
  286. 'class' => 'btn btn-large btn-danger'
  287. )
  288. );
  289. $this->fields_form[1]['form'] = array(
  290. 'legend' => array(
  291. 'title' => $this->l('SEO META'),
  292. ),
  293. 'input' => array(
  294. // custom template
  295. array(
  296. 'type' => 'textarea',
  297. 'label' => $this->l('Meta description'),
  298. 'name' => 'meta_description',
  299. 'lang' => true,
  300. 'cols' => 40,
  301. 'rows' => 10,
  302. 'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}',
  303. 'default' => ''
  304. ),
  305. array(
  306. 'type' => 'tags',
  307. 'label' => $this->l('Meta keywords'),
  308. 'name' => 'meta_keywords',
  309. 'lang' => true,
  310. 'default' => '',
  311. 'hint' => array(
  312. $this->l('Invalid characters:').' &lt;&gt;;=#{}',
  313. $this->l('To add "tags" click in the field, write something, and then press "Enter."')
  314. )
  315. ),
  316. ),
  317. 'submit' => array(
  318. 'title' => $this->l('Save'),
  319. 'class' => 'btn btn-large btn-danger'
  320. )
  321. );
  322. $helper = new HelperForm();
  323. $helper->module = $this;
  324. $helper->name_controller = $this->name;
  325. $helper->identifier = $this->identifier;
  326. $helper->token = Tools::getValue('token');
  327. foreach (Language::getLanguages(false) as $lang)
  328. $helper->languages[] = array(
  329. 'id_lang' => $lang['id_lang'],
  330. 'iso_code' => $lang['iso_code'],
  331. 'name' => $lang['name'],
  332. 'is_default' => ($default_lang == $lang['id_lang'] ? 1 : 0)
  333. );
  334. $helper->currentIndex = AdminController::$currentIndex;
  335. $helper->default_form_language = $default_lang;
  336. $helper->allow_employee_form_lang = $default_lang;
  337. $helper->toolbar_scroll = true;
  338. $helper->title = $this->l('Categories Management');
  339. $helper->submit_action = 'save'.$this->name;
  340. $helper->tpl_vars = array(
  341. 'fields_value' => $this->getConfigFieldsValues($obj),
  342. 'languages' => $this->context->controller->getLanguages(),
  343. 'id_language' => $this->context->language->id,
  344. 'PS_ALLOW_ACCENTED_CHARS_URL', (int)Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL')
  345. );
  346. $html = '
  347. <script type="text/javascript">
  348. var PS_ALLOW_ACCENTED_CHARS_URL = '.(int)Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL').';
  349. </script>
  350. ';
  351. $action = AdminController::$currentIndex.'&save'.$this->name.'&token='.Tools::getValue('token');
  352. $addnew = AdminController::$currentIndex.'&token='.Tools::getValue('token');
  353. $helper->toolbar_btn = false;
  354. $output = $html.'
  355. <div class="" id="megamenu">
  356. ';
  357. $output .= '<div class="col-md-4"><div class="panel panel-default"><h3 class="panel-title">'.$this->l('Tree Blog Categories Management').'</h3>'
  358. .'<div class="panel-content">'.$this->l('To sort orders or update parent-child, you drap and drop expected menu, then click to Update button to Save')
  359. .'<hr><p><input type="button" value="'.$this->l('New Category').'" id="addcategory" data-loading-text="'.$this->l('Processing ...').'" class="btn btn-danger" name="addcategory"></p><p><input type="button" value="'.$this->l('Update Positions').'" id="serialize" data-loading-text="'.$this->l('Processing ...').'" class="btn btn-danger" name="serialize"></p><hr>'.$tree.'</div></div></div>'
  360. .'<div class="col-md-8">'.$helper->generateForm($this->fields_form).'</div>'
  361. .'<script type="text/javascript"> var action="'.$action.'"; var addnew ="'.$addnew.'"; $("#content").PavMegaMenuList({action:action,addnew:addnew});</script>';
  362. $output .= '</div>';
  363. return $output;
  364. }
  365. /**
  366. * Asign value for each input of Data form
  367. */
  368. public function getConfigFieldsValues($obj)
  369. {
  370. $languages = Language::getLanguages(false);
  371. $fields_values = array();
  372. foreach ($this->fields_form as $k => $f)
  373. {
  374. foreach ($f['form']['input'] as $j => $input)
  375. {
  376. if (isset($obj->{trim($input['name'])}))
  377. {
  378. if (isset($obj->{trim($input['name'])}))
  379. $data = $obj->{trim($input['name'])};
  380. else
  381. $data = $input['default'];
  382. if ($input['name'] == 'image' && $data)
  383. {
  384. $thumb = __PS_BASE_URI__.'modules/'.$this->name.'/views/img/c/'.$data;
  385. $this->fields_form[$k]['form']['input'][$j]['thumb'] = $thumb;
  386. }
  387. if (isset($input['lang']))
  388. {
  389. foreach ($languages as $lang)
  390. {
  391. # validate module
  392. $fields_values[$input['name']][$lang['id_lang']] = isset($data[$lang['id_lang']]) ? $data[$lang['id_lang']] : $input['default'];
  393. }
  394. }
  395. else
  396. {
  397. # validate module
  398. $fields_values[$input['name']] = $data;
  399. }
  400. }
  401. else
  402. {
  403. if (isset($input['lang']))
  404. {
  405. foreach ($languages as $lang)
  406. {
  407. $v = Tools::getValue('title', Configuration::get($input['name'], $lang['id_lang']));
  408. $fields_values[$input['name']][$lang['id_lang']] = $v ? $v : $input['default'];
  409. }
  410. }
  411. else
  412. {
  413. $v = Tools::getValue($input['name'], Configuration::get($input['name']));
  414. $fields_values[$input['name']] = $v ? $v : $input['default'];
  415. }
  416. if ($input['name'] == $obj->type.'_type')
  417. {
  418. # validate module
  419. $fields_values[$input['name']] = $obj->item;
  420. }
  421. }
  422. }
  423. }
  424. return $fields_values;
  425. }
  426. }