PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminCmsContentController.php

https://gitlab.com/jslee1/PrestaShop
PHP | 316 lines | 242 code | 36 blank | 38 comment | 68 complexity | 347a026d3703056f0a1898cad220adf3 MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 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/osl-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-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @property CMS $object
  28. */
  29. class AdminCmsContentControllerCore extends AdminController
  30. {
  31. /** @var object adminCMSCategories() instance */
  32. protected $admin_cms_categories;
  33. /** @var object adminCMS() instance */
  34. protected $admin_cms;
  35. /** @var object Category() instance for navigation*/
  36. protected static $category = null;
  37. public function __construct()
  38. {
  39. $this->bootstrap = true;
  40. /* Get current category */
  41. $id_cms_category = (int)Tools::getValue('id_cms_category', Tools::getValue('id_cms_category_parent', 1));
  42. self::$category = new CMSCategory($id_cms_category);
  43. if (!Validate::isLoadedObject(self::$category)) {
  44. die('Category cannot be loaded');
  45. }
  46. $this->table = 'cms';
  47. $this->className = 'CMS';
  48. $this->bulk_actions = array(
  49. 'delete' => array(
  50. 'text' => $this->l('Delete selected'),
  51. 'confirm' => $this->l('Delete selected items?'),
  52. 'icon' => 'icon-trash'
  53. )
  54. );
  55. $this->admin_cms_categories = new AdminCmsCategoriesController();
  56. $this->admin_cms_categories->init();
  57. $this->admin_cms = new AdminCmsController();
  58. $this->admin_cms->init();
  59. parent::__construct();
  60. }
  61. /**
  62. * Return current category
  63. *
  64. * @return object
  65. */
  66. public static function getCurrentCMSCategory()
  67. {
  68. return self::$category;
  69. }
  70. public function viewAccess($disable = false)
  71. {
  72. $result = parent::viewAccess($disable);
  73. $this->admin_cms_categories->tabAccess = $this->tabAccess;
  74. $this->admin_cms->tabAccess = $this->tabAccess;
  75. return $result;
  76. }
  77. public function initContent()
  78. {
  79. $this->initTabModuleList();
  80. $this->renderPageHeaderToolbar();
  81. $this->admin_cms_categories->token = $this->token;
  82. $this->admin_cms->token = $this->token;
  83. if ($this->display == 'edit_category') {
  84. $this->content .= $this->admin_cms_categories->renderForm();
  85. } elseif ($this->display == 'edit_page') {
  86. $this->content .= $this->admin_cms->renderForm();
  87. } elseif ($this->display == 'view_page') {
  88. $fixme = 'fixme';
  89. }// @FIXME
  90. else {
  91. $id_cms_category = (int)Tools::getValue('id_cms_category');
  92. if (!$id_cms_category) {
  93. $id_cms_category = 1;
  94. }
  95. // CMS categories breadcrumb
  96. $cms_tabs = array('cms_category', 'cms');
  97. // Cleaning links
  98. $cat_bar_index = self::$currentIndex;
  99. foreach ($cms_tabs as $tab) {
  100. if (Tools::getValue($tab.'Orderby') && Tools::getValue($tab.'Orderway')) {
  101. $cat_bar_index = preg_replace('/&'.$tab.'Orderby=([a-z _]*)&'.$tab.'Orderway=([a-z]*)/i', '', self::$currentIndex);
  102. }
  103. }
  104. $this->context->smarty->assign(array(
  105. 'cms_breadcrumb' => getPath($cat_bar_index, $id_cms_category, '', '', 'cms'),
  106. 'page_header_toolbar_btn' => $this->page_header_toolbar_btn,
  107. 'page_header_toolbar_title' => $this->toolbar_title,
  108. ));
  109. $this->content .= $this->admin_cms_categories->renderList();
  110. $this->admin_cms->id_cms_category = $id_cms_category;
  111. $this->content .= $this->admin_cms->renderList();
  112. }
  113. $this->context->smarty->assign(array(
  114. 'content' => $this->content
  115. ));
  116. }
  117. public function renderPageHeaderToolbar()
  118. {
  119. $id_cms_category = (int)Tools::getValue('id_cms_category');
  120. $id_cms_page = Tools::getValue('id_cms');
  121. if (!$id_cms_category) {
  122. $id_cms_category = 1;
  123. }
  124. $cms_category = new CMSCategory($id_cms_category);
  125. if ($this->display == 'edit_category') {
  126. if (Tools::getValue('addcms_category') !== false) {
  127. $this->toolbar_title[] = $this->l('Add new');
  128. } else {
  129. $this->toolbar_title[] = sprintf($this->l('Edit: %s'), $cms_category->name[$this->context->employee->id_lang]);
  130. }
  131. } elseif ($this->display == 'edit_page') {
  132. $this->toolbar_title[] = $cms_category->name[$this->context->employee->id_lang];
  133. if (Tools::getValue('addcms') !== false) {
  134. $this->toolbar_title[] = $this->l('Add new');
  135. } elseif ($id_cms_page) {
  136. $cms_page = new CMS($id_cms_page);
  137. $this->toolbar_title[] = sprintf($this->l('Edit: %s'), $cms_page->meta_title[$this->context->employee->id_lang]);
  138. }
  139. } else {
  140. $this->toolbar_title[] = $this->l('CMS');
  141. }
  142. if ($this->display == 'list') {
  143. $this->page_header_toolbar_btn['new_cms_category'] = array(
  144. 'href' => self::$currentIndex.'&addcms_category&token='.$this->token,
  145. 'desc' => $this->l('Add new CMS category', null, null, false),
  146. 'icon' => 'process-icon-new'
  147. );
  148. $this->page_header_toolbar_btn['new_cms_page'] = array(
  149. 'href' => self::$currentIndex.'&addcms&id_cms_category='.(int)$id_cms_category.'&token='.$this->token,
  150. 'desc' => $this->l('Add new CMS page', null, null, false),
  151. 'icon' => 'process-icon-new'
  152. );
  153. }
  154. $this->page_header_toolbar_title = implode(' '.Configuration::get('PS_NAVIGATION_PIPE').' ', $this->toolbar_title);
  155. if (is_array($this->page_header_toolbar_btn)
  156. && $this->page_header_toolbar_btn instanceof Traversable
  157. || trim($this->page_header_toolbar_title) != '') {
  158. $this->show_page_header_toolbar = true;
  159. }
  160. $template = $this->context->smarty->createTemplate(
  161. $this->context->smarty->getTemplateDir(0).DIRECTORY_SEPARATOR
  162. .'page_header_toolbar.tpl', $this->context->smarty);
  163. $this->context->smarty->assign(array(
  164. 'show_page_header_toolbar' => $this->show_page_header_toolbar,
  165. 'title' => $this->page_header_toolbar_title,
  166. 'toolbar_btn' => $this->page_header_toolbar_btn,
  167. 'page_header_toolbar_btn' => $this->page_header_toolbar_btn,
  168. 'page_header_toolbar_title' => $this->toolbar_title,
  169. ));
  170. }
  171. public function postProcess()
  172. {
  173. $this->admin_cms->postProcess();
  174. $this->admin_cms_categories->postProcess();
  175. parent::postProcess();
  176. if (((Tools::isSubmit('submitAddcms_category') || Tools::isSubmit('submitAddcms_categoryAndStay')) && count($this->admin_cms_categories->errors))
  177. || Tools::isSubmit('updatecms_category')
  178. || Tools::isSubmit('addcms_category')) {
  179. $this->display = 'edit_category';
  180. } elseif (((Tools::isSubmit('submitAddcms') || Tools::isSubmit('submitAddcmsAndStay')) && count($this->admin_cms->errors))
  181. || Tools::isSubmit('updatecms')
  182. || Tools::isSubmit('addcms')) {
  183. $this->display = 'edit_page';
  184. } else {
  185. $this->display = 'list';
  186. $this->id_cms_category = (int)Tools::getValue('id_cms_category');
  187. }
  188. if (isset($this->admin_cms->errors)) {
  189. $this->errors = array_merge($this->errors, $this->admin_cms->errors);
  190. }
  191. if (isset($this->admin_cms_categories->errors)) {
  192. $this->errors = array_merge($this->errors, $this->admin_cms_categories->errors);
  193. }
  194. }
  195. public function setMedia()
  196. {
  197. parent::setMedia();
  198. $this->addJqueryUi('ui.widget');
  199. $this->addJqueryPlugin('tagify');
  200. }
  201. public function ajaxProcessUpdateCmsPositions()
  202. {
  203. if ($this->tabAccess['edit'] === '1') {
  204. $id_cms = (int)Tools::getValue('id_cms');
  205. $id_category = (int)Tools::getValue('id_cms_category');
  206. $way = (int)Tools::getValue('way');
  207. $positions = Tools::getValue('cms');
  208. if (is_array($positions)) {
  209. foreach ($positions as $key => $value) {
  210. $pos = explode('_', $value);
  211. if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category && $pos[2] == $id_cms)) {
  212. $position = $key;
  213. break;
  214. }
  215. }
  216. }
  217. $cms = new CMS($id_cms);
  218. if (Validate::isLoadedObject($cms)) {
  219. if (isset($position) && $cms->updatePosition($way, $position)) {
  220. die(true);
  221. } else {
  222. die('{"hasError" : true, "errors" : "Can not update cms position"}');
  223. }
  224. } else {
  225. die('{"hasError" : true, "errors" : "This cms can not be loaded"}');
  226. }
  227. }
  228. }
  229. public function ajaxProcessUpdateCmsCategoriesPositions()
  230. {
  231. if ($this->tabAccess['edit'] === '1') {
  232. $id_cms_category_to_move = (int)Tools::getValue('id_cms_category_to_move');
  233. $id_cms_category_parent = (int)Tools::getValue('id_cms_category_parent');
  234. $way = (int)Tools::getValue('way');
  235. $positions = Tools::getValue('cms_category');
  236. if (is_array($positions)) {
  237. foreach ($positions as $key => $value) {
  238. $pos = explode('_', $value);
  239. if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_cms_category_parent && $pos[2] == $id_cms_category_to_move)) {
  240. $position = $key;
  241. break;
  242. }
  243. }
  244. }
  245. $cms_category = new CMSCategory($id_cms_category_to_move);
  246. if (Validate::isLoadedObject($cms_category)) {
  247. if (isset($position) && $cms_category->updatePosition($way, $position)) {
  248. die(true);
  249. } else {
  250. die('{"hasError" : true, "errors" : "Can not update cms categories position"}');
  251. }
  252. } else {
  253. die('{"hasError" : true, "errors" : "This cms category can not be loaded"}');
  254. }
  255. }
  256. }
  257. public function ajaxProcessPublishCMS()
  258. {
  259. if ($this->tabAccess['edit'] === '1') {
  260. if ($id_cms = (int)Tools::getValue('id_cms')) {
  261. $bo_cms_url = _PS_BASE_URL_.__PS_BASE_URI__.basename(_PS_ADMIN_DIR_).'/index.php?tab=AdminCmsContent&id_cms='.(int)$id_cms.'&updatecms&token='.$this->token;
  262. if (Tools::getValue('redirect')) {
  263. die($bo_cms_url);
  264. }
  265. $cms = new CMS((int)(Tools::getValue('id_cms')));
  266. if (!Validate::isLoadedObject($cms)) {
  267. die('error: invalid id');
  268. }
  269. $cms->active = 1;
  270. if ($cms->save()) {
  271. die($bo_cms_url);
  272. } else {
  273. die('error: saving');
  274. }
  275. } else {
  276. die('error: parameters');
  277. }
  278. }
  279. }
  280. }