PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/joebushi/joomla
PHP | 398 lines | 211 code | 62 blank | 125 comment | 36 complexity | 0aa74c99ab717f375f263fce4e144e72 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. defined('_JEXEC') or die;
  8. jimport( 'joomla.application.component.controller' );
  9. /**
  10. * The Menu Item Controller
  11. *
  12. * @package Joomla.Administrator
  13. * @subpackage com_menus
  14. * @since 1.6
  15. */
  16. class MenusControllerItem extends JController
  17. {
  18. /**
  19. * Constructor.
  20. *
  21. * @param array An optional associative array of configuration settings.
  22. * @see JController
  23. */
  24. public function __construct($config = array())
  25. {
  26. parent::__construct($config);
  27. // Register proxy tasks.
  28. $this->registerTask('save2copy', 'save');
  29. $this->registerTask('save2new', 'save');
  30. $this->registerTask('apply', 'save');
  31. }
  32. /**
  33. * Dummy method to redirect back to standard controller
  34. *
  35. * @return void
  36. */
  37. public function display()
  38. {
  39. $this->setRedirect(JRoute::_('index.php?option=com_menus', false));
  40. }
  41. /**
  42. * Method to add a new menu item.
  43. *
  44. * @return void
  45. */
  46. public function add()
  47. {
  48. // Initialise variables.
  49. $app = &JFactory::getApplication();
  50. // Clear the row edit information from the session.
  51. $app->setUserState('com_menus.edit.item.id', null);
  52. $app->setUserState('com_menus.edit.item.data', null);
  53. $app->setUserState('com_menus.edit.item.type', null);
  54. $app->setUserState('com_menus.edit.item.link', null);
  55. // Check if we are adding for a particular menutype
  56. $menuType = $app->getUserStateFromRequest($this->_context.'.filter.menutype', 'menutype', 'mainmenu');
  57. // Redirect to the edit screen.
  58. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit&menutype='.$menuType, false));
  59. }
  60. /**
  61. * Method to edit an existing menu item.
  62. *
  63. * @return void
  64. */
  65. public function edit()
  66. {
  67. // Initialise variables.
  68. $app = &JFactory::getApplication();
  69. $pks = JRequest::getVar('cid', array(), '', 'array');
  70. // Get the id of the group to edit.
  71. $id = (empty($pks) ? JRequest::getInt('item_id') : (int) array_pop($pks));
  72. // Get the menu item model.
  73. $model = &$this->getModel('Item');
  74. // Check that this is not a new item.
  75. if ($id > 0)
  76. {
  77. $item = $model->getItem($id);
  78. // If not already checked out, do so.
  79. if ($item->checked_out == 0)
  80. {
  81. if (!$model->checkout($id))
  82. {
  83. // Check-out failed, go back to the list and display a notice.
  84. $message = JText::sprintf('JError_Checkout_failed', $model->getError());
  85. //$this->setRedirect('index.php?option=com_menus&view=item&item_id='.$id, $message, 'error');
  86. return false;
  87. }
  88. }
  89. }
  90. // Push the new row id into the session.
  91. $app->setUserState('com_menus.edit.item.id', $id);
  92. $app->setUserState('com_menus.edit.item.data', null);
  93. $app->setUserState('com_menus.edit.item.type', null);
  94. $app->setUserState('com_menus.edit.item.link', null);
  95. $this->setRedirect('index.php?option=com_menus&view=item&layout=edit');
  96. return true;
  97. }
  98. /**
  99. * Method to cancel an edit
  100. *
  101. * Checks the item in, sets item ID in the session to null, and then redirects to the list page.
  102. *
  103. * @return void
  104. */
  105. public function cancel()
  106. {
  107. JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));
  108. // Initialise variables.
  109. $app = &JFactory::getApplication();
  110. // Get the previous menu item id (if any) and the current menu item id.
  111. $previousId = (int) $app->getUserState('com_content.edit.item.id');
  112. $model = &$this->getModel('Item');
  113. // If rows ids do not match, checkin previous row.
  114. if (!$model->checkin($previousId)) {
  115. // Check-in failed, go back to the menu item and display a notice.
  116. $message = JText::sprintf('JError_Checkin_failed', $model->getError());
  117. $this->setRedirect('index.php?option=com_content&view=item&layout=edit', $message, 'error');
  118. return false;
  119. }
  120. // Clear the row edit information from the session.
  121. $app->setUserState('com_menus.edit.item.id', null);
  122. $app->setUserState('com_menus.edit.item.data', null);
  123. $app->setUserState('com_menus.edit.item.type', null);
  124. $app->setUserState('com_menus.edit.item.link', null);
  125. // Redirect to the list screen.
  126. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=items', false));
  127. }
  128. /**
  129. * Method to save a menu item.
  130. *
  131. * @return void
  132. */
  133. public function save()
  134. {
  135. // Check for request forgeries.
  136. JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));
  137. // Initialise variables.
  138. $app = &JFactory::getApplication();
  139. $model = &$this->getModel('Item');
  140. $task = $this->getTask();
  141. // Get the posted values from the request.
  142. $iData = JRequest::getVar('jform', array(), 'post', 'array');
  143. $pData = JRequest::getVar('jformparams', array(), 'post', 'array');
  144. $map = JRequest::getVar('menuid', array(), 'post', 'array');
  145. // Populate the row id from the session.
  146. $iData['id'] = (int) $app->getUserState('com_menus.edit.item.id');
  147. // The save2copy task needs to be handled slightly differently.
  148. if ($task == 'save2copy')
  149. {
  150. // Check-in the original row.
  151. if (!$model->checkin())
  152. {
  153. // Check-in failed, go back to the item and display a notice.
  154. $message = JText::sprintf('JError_Checkin_saved', $model->getError());
  155. // $this->setRedirect('index.php?option=com_menus&view=item&layout=edit', $message, 'error');
  156. return false;
  157. }
  158. // Reset the ID and then treat the request as for Apply.
  159. $iData['id'] = 0;
  160. $task = 'apply';
  161. }
  162. // Validate the posted data.
  163. // This post is made up of two forms, one for the item and one for params.
  164. $itemForm = &$model->getForm();
  165. if (!$itemForm) {
  166. JError::raiseError(500, $model->getError());
  167. return false;
  168. }
  169. $iData = $model->validate($itemForm, $iData);
  170. $paramsForm = &$model->getParamsForm($iData['type'], $iData['link']);
  171. if (!$paramsForm) {
  172. JError::raiseError(500, $model->getError());
  173. return false;
  174. }
  175. $pData = $model->validate($paramsForm, $pData);
  176. // Check for the special 'request' entry.
  177. if ($iData['type'] == 'component' && isset($pData['request']) && is_array($pData['request']) && !empty($pData['request']))
  178. {
  179. // Parse the submitted link arguments.
  180. $args = array();
  181. parse_str(parse_url($iData['link'], PHP_URL_QUERY), $args);
  182. // Merge in the user supplied request arguments.
  183. $args = array_merge($args, $pData['request']);
  184. $iData['link'] = 'index.php?'.http_build_query($args,'','&');
  185. unset($pData['request']);
  186. }
  187. // Params are validated so add them to the item data.
  188. $iData['params'] = $pData;
  189. // Push the menu id map back into the array
  190. $iData['map'] = &$map;
  191. // Check for validation errors.
  192. if ($iData === false)
  193. {
  194. // Get the validation messages.
  195. $errors = $model->getErrors();
  196. // Push up to three validation messages out to the user.
  197. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
  198. {
  199. if (JError::isError($errors[$i])) {
  200. $app->enqueueMessage($errors[$i]->getMessage(), 'notice');
  201. }
  202. else {
  203. $app->enqueueMessage($errors[$i], 'notice');
  204. }
  205. }
  206. // Save the data in the session.
  207. $app->setUserState('com_menus.edit.item.data', $iData);
  208. // Redirect back to the edit screen.
  209. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
  210. return false;
  211. }
  212. // Attempt to save the data.
  213. if (!$model->save($iData))
  214. {
  215. // Save the data in the session.
  216. $app->setUserState('com_menus.edit.item.data', $iData);
  217. // Redirect back to the edit screen.
  218. $this->setMessage(JText::sprintf('JError_Save_failed', $model->getError()), 'notice');
  219. // $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
  220. return false;
  221. }
  222. // Save succeeded, check-in the row.
  223. if (!$model->checkin())
  224. {
  225. // Check-in failed, go back to the row and display a notice.
  226. $message = JText::sprintf('JError_Checkin_saved', $model->getError());
  227. // $this->setRedirect('index.php?option=com_menus&view=item&layout=edit', $message, 'error');
  228. return false;
  229. }
  230. $this->setMessage(JText::_('JController_Save_success'));
  231. // Redirect the user and adjust session state based on the chosen task.
  232. switch ($task)
  233. {
  234. case 'apply':
  235. // Set the row data in the session.
  236. $app->setUserState('com_menus.edit.item.id', $model->getState('item.id'));
  237. $app->setUserState('com_menus.edit.item.data', null);
  238. $app->setUserState('com_menus.edit.item.type', null);
  239. $app->setUserState('com_menus.edit.item.link', null);
  240. // Redirect back to the edit screen.
  241. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
  242. break;
  243. case 'save2new':
  244. // Clear the row id and data in the session.
  245. $app->setUserState('com_menus.edit.item.id', null);
  246. $app->setUserState('com_menus.edit.item.data', null);
  247. $app->setUserState('com_menus.edit.item.type', null);
  248. $app->setUserState('com_menus.edit.item.link', null);
  249. // Redirect back to the edit screen.
  250. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
  251. break;
  252. default:
  253. // Clear the row id and data in the session.
  254. $app->setUserState('com_menus.edit.item.id', null);
  255. $app->setUserState('com_menus.edit.item.data', null);
  256. $app->setUserState('com_menus.edit.item.type', null);
  257. $app->setUserState('com_menus.edit.item.link', null);
  258. // Redirect to the list screen.
  259. $this->setRedirect(JRoute::_('index.php?option=com_menus&view=items', false));
  260. break;
  261. }
  262. }
  263. function setType()
  264. {
  265. // Initialise variables.
  266. $app = &JFactory::getApplication();
  267. // Get the type.
  268. $type = JRequest::getVar('type');
  269. $type = json_decode(base64_decode($type));
  270. $title = isset($type->title) ? $type->title : null;
  271. if ($title != 'alias' && $title != 'separator' && $title != 'url') {
  272. $title = 'component';
  273. }
  274. $app->setUserState('com_menus.edit.item.type', $title);
  275. if ($title=='component'){
  276. if (isset($type->request)) {
  277. if (isset($type->request->layout)) {
  278. $app->setUserState('com_menus.edit.item.link', 'index.php?option='. $type->request->option.'&view='.
  279. $type->request->view.'&layout='.$type->request->layout);
  280. }
  281. else {
  282. $app->setUserState('com_menus.edit.item.link', 'index.php?option='. $type->request->option.'&view='.
  283. $type->request->view);
  284. }
  285. // $app->setUserState('com_menus.edit.item.id', $model->getState('item.id'));
  286. // $app->setUserState('com_menus.edit.item.data', null);
  287. //$app->setUserState('com_menus.edit.item.type', $type->type);
  288. // $app->setUserState('com_menus.edit.item.link', null);
  289. }
  290. }
  291. //If the type is alias you just need the item id from the menu item referenced.
  292. else if ($title=='alias'){
  293. $app->setUserState('com_menus.edit.item.link', 'index.php?Itemid=');
  294. // $app->setUserState('com_menus.edit.item.id', $model->getState('item.id'));
  295. // $app->setUserState('com_menus.edit.item.data', null);
  296. //$app->setUserState('com_menus.edit.item.type', $type->type);
  297. // $app->setUserState('com_menus.edit.item.link', null);
  298. }
  299. //else if ($title=='url'){
  300. // $app->setUserState('com_menus.edit.item.link', null );
  301. // $app->setUserState('com_menus.edit.item.id', $model->getState('item.id'));
  302. // $app->setUserState('com_menus.edit.item.data', null);
  303. //$app->setUserState('com_menus.edit.item.type', $type->type);
  304. // $app->setUserState('com_menus.edit.item.link', null);
  305. //}
  306. $this->type=$type;
  307. $this->setRedirect('index.php?option=com_menus&view=item&layout=edit');
  308. }
  309. /**
  310. * Method to run batch opterations.
  311. *
  312. * @return void
  313. */
  314. function batch()
  315. {
  316. JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));
  317. // Initialise variables.
  318. $app = &JFactory::getApplication();
  319. $model = &$this->getModel('Item');
  320. $vars = JRequest::getVar('batch', array(), 'post', 'array');
  321. $cid = JRequest::getVar('cid', array(), 'post', 'array');
  322. // Preset the redirect
  323. $this->setRedirect('index.php?option=com_menus&view=items');
  324. // Attempt to run the batch operation.
  325. if ($model->batch($vars, $cid))
  326. {
  327. $this->setMessage(JText::_('Menus_Batch_success'));
  328. return true;
  329. }
  330. else
  331. {
  332. $this->setMessage(JText::_(JText::sprintf('Menus_Error_Batch_failed', $model->getError())));
  333. return false;
  334. }
  335. }
  336. }