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

/administrator/components/com_menus/controllers/item.php

https://bitbucket.org/eternaware/joomus
PHP | 345 lines | 188 code | 54 blank | 103 comment | 32 complexity | 64fdd7bf06b0ae2a27ff632df7b5c185 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_menus
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * The Menu Item Controller
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_menus
  15. * @since 1.6
  16. */
  17. class MenusControllerItem extends JControllerForm
  18. {
  19. /**
  20. * Method to add a new menu item.
  21. *
  22. * @return mixed True if the record can be added, a JError object if not.
  23. *
  24. * @since 1.6
  25. */
  26. public function add()
  27. {
  28. $app = JFactory::getApplication();
  29. $context = 'com_menus.edit.item';
  30. $result = parent::add();
  31. if ($result)
  32. {
  33. $app->setUserState($context . '.type', null);
  34. $app->setUserState($context . '.link', null);
  35. $menuType = $app->getUserStateFromRequest($this->context . '.filter.menutype', 'menutype', 'mainmenu', 'cmd');
  36. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&menutype=' . $menuType . $this->getRedirectToItemAppend(), false));
  37. }
  38. return $result;
  39. }
  40. /**
  41. * Method to run batch operations.
  42. *
  43. * @param object $model The model.
  44. *
  45. * @return boolean True if successful, false otherwise and internal error is set.
  46. *
  47. * @since 1.6
  48. */
  49. public function batch($model = null)
  50. {
  51. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  52. $model = $this->getModel('Item', '', array());
  53. // Preset the redirect
  54. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=items' . $this->getRedirectToListAppend(), false));
  55. return parent::batch($model);
  56. }
  57. /**
  58. * Method to cancel an edit.
  59. *
  60. * @param string $key The name of the primary key of the URL variable.
  61. *
  62. * @return boolean True if access level checks pass, false otherwise.
  63. *
  64. * @since 1.6
  65. */
  66. public function cancel($key = null)
  67. {
  68. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  69. $app = JFactory::getApplication();
  70. $context = 'com_menus.edit.item';
  71. $result = parent::cancel();
  72. if ($result)
  73. {
  74. // Clear the ancillary data from the session.
  75. $app->setUserState($context . '.type', null);
  76. $app->setUserState($context . '.link', null);
  77. }
  78. }
  79. /**
  80. * Method to edit an existing record.
  81. *
  82. * @param string $key The name of the primary key of the URL variable.
  83. * @param string $urlVar The name of the URL variable if different from the primary key
  84. * (sometimes required to avoid router collisions).
  85. *
  86. * @return boolean True if access level check and checkout passes, false otherwise.
  87. *
  88. * @since 1.6
  89. */
  90. public function edit($key = null, $urlVar = null)
  91. {
  92. $app = JFactory::getApplication();
  93. $result = parent::edit();
  94. if ($result)
  95. {
  96. // Push the new ancillary data into the session.
  97. $app->setUserState('com_menus.edit.item.type', null);
  98. $app->setUserState('com_menus.edit.item.link', null);
  99. }
  100. return true;
  101. }
  102. /**
  103. * Method to save a record.
  104. *
  105. * @param string $key The name of the primary key of the URL variable.
  106. * @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  107. *
  108. * @return boolean True if successful, false otherwise.
  109. *
  110. * @since 1.6
  111. */
  112. public function save($key = null, $urlVar = null)
  113. {
  114. // Check for request forgeries.
  115. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  116. $app = JFactory::getApplication();
  117. $model = $this->getModel('Item', '', array());
  118. $data = $this->input->post->get('jform', array(), 'array');
  119. $task = $this->getTask();
  120. $context = 'com_menus.edit.item';
  121. $recordId = $this->input->getInt('id');
  122. if (!$this->checkEditId($context, $recordId))
  123. {
  124. // Somehow the person just went to the form and saved it - we don't allow that.
  125. $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $recordId));
  126. $this->setMessage($this->getError(), 'error');
  127. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=items' . $this->getRedirectToListAppend(), false));
  128. return false;
  129. }
  130. // Populate the row id from the session.
  131. $data['id'] = $recordId;
  132. // The save2copy task needs to be handled slightly differently.
  133. if ($task == 'save2copy')
  134. {
  135. // Check-in the original row.
  136. if ($model->checkin($data['id']) === false)
  137. {
  138. // Check-in failed, go back to the item and display a notice.
  139. $this->setMessage(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'warning');
  140. return false;
  141. }
  142. // Reset the ID and then treat the request as for Apply.
  143. $data['id'] = 0;
  144. $data['associations'] = array();
  145. $task = 'apply';
  146. }
  147. // Validate the posted data.
  148. // This post is made up of two forms, one for the item and one for params.
  149. $form = $model->getForm($data);
  150. if (!$form)
  151. {
  152. JError::raiseError(500, $model->getError());
  153. return false;
  154. }
  155. $data = $model->validate($form, $data);
  156. // Check for the special 'request' entry.
  157. if ($data['type'] == 'component' && isset($data['request']) && is_array($data['request']) && !empty($data['request']))
  158. {
  159. // Parse the submitted link arguments.
  160. $args = array();
  161. parse_str(parse_url($data['link'], PHP_URL_QUERY), $args);
  162. // Merge in the user supplied request arguments.
  163. $args = array_merge($args, $data['request']);
  164. $data['link'] = 'index.php?' . urldecode(http_build_query($args, '', '&'));
  165. unset($data['request']);
  166. }
  167. // Check for validation errors.
  168. if ($data === false)
  169. {
  170. // Get the validation messages.
  171. $errors = $model->getErrors();
  172. // Push up to three validation messages out to the user.
  173. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
  174. {
  175. if ($errors[$i] instanceof Exception)
  176. {
  177. $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
  178. }
  179. else
  180. {
  181. $app->enqueueMessage($errors[$i], 'warning');
  182. }
  183. }
  184. // Save the data in the session.
  185. $app->setUserState('com_menus.edit.item.data', $data);
  186. // Redirect back to the edit screen.
  187. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId), false));
  188. return false;
  189. }
  190. // Attempt to save the data.
  191. if (!$model->save($data))
  192. {
  193. // Save the data in the session.
  194. $app->setUserState('com_menus.edit.item.data', $data);
  195. // Redirect back to the edit screen.
  196. $this->setMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'warning');
  197. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId), false));
  198. return false;
  199. }
  200. // Save succeeded, check-in the row.
  201. if ($model->checkin($data['id']) === false)
  202. {
  203. // Check-in failed, go back to the row and display a notice.
  204. $this->setMessage(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'warning');
  205. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId), false));
  206. return false;
  207. }
  208. $this->setMessage(JText::_('COM_MENUS_SAVE_SUCCESS'));
  209. // Redirect the user and adjust session state based on the chosen task.
  210. switch ($task)
  211. {
  212. case 'apply':
  213. // Set the row data in the session.
  214. $recordId = $model->getState($this->context . '.id');
  215. $this->holdEditId($context, $recordId);
  216. $app->setUserState('com_menus.edit.item.data', null);
  217. $app->setUserState('com_menus.edit.item.type', null);
  218. $app->setUserState('com_menus.edit.item.link', null);
  219. // Redirect back to the edit screen.
  220. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId), false));
  221. break;
  222. case 'save2new':
  223. // Clear the row id and data in the session.
  224. $this->releaseEditId($context, $recordId);
  225. $app->setUserState('com_menus.edit.item.data', null);
  226. $app->setUserState('com_menus.edit.item.type', null);
  227. $app->setUserState('com_menus.edit.item.link', null);
  228. $app->setUserState('com_menus.edit.item.menutype', $model->getState('item.menutype'));
  229. // Redirect back to the edit screen.
  230. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(), false));
  231. break;
  232. default:
  233. // Clear the row id and data in the session.
  234. $this->releaseEditId($context, $recordId);
  235. $app->setUserState('com_menus.edit.item.data', null);
  236. $app->setUserState('com_menus.edit.item.type', null);
  237. $app->setUserState('com_menus.edit.item.link', null);
  238. // Redirect to the list screen.
  239. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
  240. break;
  241. }
  242. }
  243. /**
  244. * Sets the type of the menu item currently being edited.
  245. *
  246. * @return void
  247. *
  248. * @since 1.6
  249. */
  250. public function setType()
  251. {
  252. $app = JFactory::getApplication();
  253. // Get the posted values from the request.
  254. $data = $this->input->post->get('jform', array(), 'array');
  255. $recordId = $this->input->getInt('id');
  256. // Get the type.
  257. $type = $data['type'];
  258. $type = json_decode(base64_decode($type));
  259. $title = isset($type->title) ? $type->title : null;
  260. $recordId = isset($type->id) ? $type->id : 0;
  261. if ($title != 'alias' && $title != 'separator' && $title != 'url')
  262. {
  263. $title = 'component';
  264. }
  265. $app->setUserState('com_menus.edit.item.type', $title);
  266. if ($title == 'component')
  267. {
  268. if (isset($type->request))
  269. {
  270. $component = JComponentHelper::getComponent($type->request->option);
  271. $data['component_id'] = $component->id;
  272. $app->setUserState('com_menus.edit.item.link', 'index.php?' . JURI::buildQuery((array) $type->request));
  273. }
  274. }
  275. // If the type is alias you just need the item id from the menu item referenced.
  276. elseif ($title == 'alias')
  277. {
  278. $app->setUserState('com_menus.edit.item.link', 'index.php?Itemid=');
  279. }
  280. unset($data['request']);
  281. $data['type'] = $title;
  282. if ($this->input->get('fieldtype') == 'type')
  283. {
  284. $data['link'] = $app->getUserState('com_menus.edit.item.link');
  285. }
  286. //Save the data in the session.
  287. $app->setUserState('com_menus.edit.item.data', $data);
  288. $this->type = $type;
  289. $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId), false));
  290. }
  291. }