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

Language PHP Lines 399
MD5 Hash 0aa74c99ab717f375f263fce4e144e72
Repository https://github.com/joebushi/joomla.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
 * @version		$Id$
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport( 'joomla.application.component.controller' );

/**
 * The Menu Item Controller
 *
 * @package		Joomla.Administrator
 * @subpackage	com_menus
 * @since		1.6
 */
class MenusControllerItem extends JController
{
	/**
	 * Constructor.
	 *
	 * @param	array An optional associative array of configuration settings.
	 * @see		JController
	 */
	public function __construct($config = array())
	{
		parent::__construct($config);

		// Register proxy tasks.
		$this->registerTask('save2copy',	'save');
		$this->registerTask('save2new',		'save');
		$this->registerTask('apply',		'save');
	}

	/**
	 * Dummy method to redirect back to standard controller
	 *
	 * @return	void
	 */
	public function display()
	{
		$this->setRedirect(JRoute::_('index.php?option=com_menus', false));
	}

	/**
	 * Method to add a new menu item.
	 *
	 * @return	void
	 */
	public function add()
	{
		// Initialise variables.
		$app = &JFactory::getApplication();

		// Clear the row edit information from the session.
		$app->setUserState('com_menus.edit.item.id',	null);
		$app->setUserState('com_menus.edit.item.data',	null);
		$app->setUserState('com_menus.edit.item.type',	null);
		$app->setUserState('com_menus.edit.item.link',	null);

		// Check if we are adding for a particular menutype
		$menuType = $app->getUserStateFromRequest($this->_context.'.filter.menutype', 'menutype', 'mainmenu');

		// Redirect to the edit screen.
		$this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit&menutype='.$menuType, false));
	}

	/**
	 * Method to edit an existing menu item.
	 *
	 * @return	void
	 */
	public function edit()
	{
		// Initialise variables.
		$app	= &JFactory::getApplication();
		$pks	= JRequest::getVar('cid', array(), '', 'array');

		// Get the id of the group to edit.
		$id		=  (empty($pks) ? JRequest::getInt('item_id') : (int) array_pop($pks));

		// Get the menu item model.
		$model	= &$this->getModel('Item');

		// Check that this is not a new item.
		if ($id > 0)
		{
			$item = $model->getItem($id);

			// If not already checked out, do so.
			if ($item->checked_out == 0)
			{
				if (!$model->checkout($id))
				{
					// Check-out failed, go back to the list and display a notice.
					$message = JText::sprintf('JError_Checkout_failed', $model->getError());
					//$this->setRedirect('index.php?option=com_menus&view=item&item_id='.$id, $message, 'error');
					return false;
				}
			}
		}

		// Push the new row id into the session.
		$app->setUserState('com_menus.edit.item.id',	$id);
		$app->setUserState('com_menus.edit.item.data',	null);
		$app->setUserState('com_menus.edit.item.type',	null);
		$app->setUserState('com_menus.edit.item.link',	null);

		$this->setRedirect('index.php?option=com_menus&view=item&layout=edit');

		return true;
	}

	/**
	 * Method to cancel an edit
	 *
	 * Checks the item in, sets item ID in the session to null, and then redirects to the list page.
	 *
	 * @return	void
	 */
	public function cancel()
	{
		JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));

		// Initialise variables.

		$app	= &JFactory::getApplication();
		// Get the previous menu item id (if any) and the current menu item id.
		$previousId	= (int) $app->getUserState('com_content.edit.item.id');

		$model	= &$this->getModel('Item');




		// If rows ids do not match, checkin previous row.
		if (!$model->checkin($previousId)) {
		// Check-in failed, go back to the menu item and display a notice.
			$message = JText::sprintf('JError_Checkin_failed', $model->getError());
			$this->setRedirect('index.php?option=com_content&view=item&layout=edit', $message, 'error');
			return false;

		}

				// Clear the row edit information from the session.
		$app->setUserState('com_menus.edit.item.id',	null);
		$app->setUserState('com_menus.edit.item.data',	null);
		$app->setUserState('com_menus.edit.item.type',	null);
		$app->setUserState('com_menus.edit.item.link',	null);

	// Redirect to the list screen.
		$this->setRedirect(JRoute::_('index.php?option=com_menus&view=items', false));
	}
	/**
	 * Method to save a menu item.
	 *
	 * @return	void
	 */
	public function save()
	{
		// Check for request forgeries.
		JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));

		// Initialise variables.
		$app	= &JFactory::getApplication();
		$model	= &$this->getModel('Item');
		$task	= $this->getTask();

		// Get the posted values from the request.
		$iData	= JRequest::getVar('jform', array(), 'post', 'array');
		$pData	= JRequest::getVar('jformparams', array(), 'post', 'array');
		$map	= JRequest::getVar('menuid', array(), 'post', 'array');

		// Populate the row id from the session.
		$iData['id'] = (int) $app->getUserState('com_menus.edit.item.id');

		// The save2copy task needs to be handled slightly differently.
		if ($task == 'save2copy')
		{
			// Check-in the original row.
			if (!$model->checkin())
			{
				// Check-in failed, go back to the item and display a notice.
				$message = JText::sprintf('JError_Checkin_saved', $model->getError());
			//	$this->setRedirect('index.php?option=com_menus&view=item&layout=edit', $message, 'error');
				return false;
			}

			// Reset the ID and then treat the request as for Apply.
			$iData['id']	= 0;
			$task			= 'apply';
		}

		// Validate the posted data.
		// This post is made up of two forms, one for the item and one for params.
		$itemForm	= &$model->getForm();
		if (!$itemForm) {
			JError::raiseError(500, $model->getError());
			return false;
		}
		$iData	= $model->validate($itemForm, $iData);

		$paramsForm	= &$model->getParamsForm($iData['type'], $iData['link']);
		if (!$paramsForm) {
			JError::raiseError(500, $model->getError());
			return false;
		}
		$pData	= $model->validate($paramsForm, $pData);

		// Check for the special 'request' entry.
		if ($iData['type'] == 'component' && isset($pData['request']) && is_array($pData['request']) && !empty($pData['request']))
		{
			// Parse the submitted link arguments.
			$args = array();
			parse_str(parse_url($iData['link'], PHP_URL_QUERY), $args);

			// Merge in the user supplied request arguments.
			$args = array_merge($args, $pData['request']);
			$iData['link'] = 'index.php?'.http_build_query($args,'','&');
			unset($pData['request']);
		}

		// Params are validated so add them to the item data.
		$iData['params'] = $pData;

		// Push the menu id map back into the array
		$iData['map'] = &$map;

		// Check for validation errors.
		if ($iData === false)
		{
			// Get the validation messages.
			$errors	= $model->getErrors();

			// Push up to three validation messages out to the user.
			for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
			{
				if (JError::isError($errors[$i])) {
					$app->enqueueMessage($errors[$i]->getMessage(), 'notice');
				}
				else {
					$app->enqueueMessage($errors[$i], 'notice');
				}
			}

			// Save the data in the session.
			$app->setUserState('com_menus.edit.item.data', $iData);

			// Redirect back to the edit screen.
			$this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
			return false;
		}

		// Attempt to save the data.
		if (!$model->save($iData))
		{
			// Save the data in the session.
			$app->setUserState('com_menus.edit.item.data', $iData);

			// Redirect back to the edit screen.
			$this->setMessage(JText::sprintf('JError_Save_failed', $model->getError()), 'notice');
		//	$this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
			return false;
		}

		// Save succeeded, check-in the row.
		if (!$model->checkin())
		{
			// Check-in failed, go back to the row and display a notice.
			$message = JText::sprintf('JError_Checkin_saved', $model->getError());
		//	$this->setRedirect('index.php?option=com_menus&view=item&layout=edit', $message, 'error');
			return false;
		}

		$this->setMessage(JText::_('JController_Save_success'));

		// Redirect the user and adjust session state based on the chosen task.
		switch ($task)
		{
			case 'apply':
				// Set the row data in the session.
				$app->setUserState('com_menus.edit.item.id',	$model->getState('item.id'));
				$app->setUserState('com_menus.edit.item.data',	null);
				$app->setUserState('com_menus.edit.item.type',	null);
				$app->setUserState('com_menus.edit.item.link',	null);

				// Redirect back to the edit screen.
				$this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
				break;

			case 'save2new':
				// Clear the row id and data in the session.
				$app->setUserState('com_menus.edit.item.id',	null);
				$app->setUserState('com_menus.edit.item.data',	null);
				$app->setUserState('com_menus.edit.item.type',	null);
				$app->setUserState('com_menus.edit.item.link',	null);

				// Redirect back to the edit screen.
				$this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&layout=edit', false));
				break;

			default:
				// Clear the row id and data in the session.
				$app->setUserState('com_menus.edit.item.id',	null);
				$app->setUserState('com_menus.edit.item.data',	null);
				$app->setUserState('com_menus.edit.item.type',	null);
				$app->setUserState('com_menus.edit.item.link',	null);

				// Redirect to the list screen.
				$this->setRedirect(JRoute::_('index.php?option=com_menus&view=items', false));
				break;
		}
	}

	function setType()
	{
		// Initialise variables.
		$app	= &JFactory::getApplication();

		// Get the type.
		$type = JRequest::getVar('type');
		$type = json_decode(base64_decode($type));
		$title = isset($type->title) ? $type->title : null;
		if ($title != 'alias' && $title != 'separator' && $title != 'url') {
			$title = 'component';
		}
		$app->setUserState('com_menus.edit.item.type',	$title);
		if ($title=='component'){
			if (isset($type->request)) {
				if (isset($type->request->layout)) {
				$app->setUserState('com_menus.edit.item.link', 'index.php?option='.  $type->request->option.'&view='.
					$type->request->view.'&layout='.$type->request->layout);
				}
				else {
			$app->setUserState('com_menus.edit.item.link', 'index.php?option='.  $type->request->option.'&view='.
					$type->request->view);
				}
				//	$app->setUserState('com_menus.edit.item.id',	$model->getState('item.id'));
				//	$app->setUserState('com_menus.edit.item.data',	null);
				//$app->setUserState('com_menus.edit.item.type',	$type->type);
				//	$app->setUserState('com_menus.edit.item.link',	null);
			}
		}
		//If the type is alias you just need the item id from the menu item referenced.
		else if ($title=='alias'){
				$app->setUserState('com_menus.edit.item.link', 'index.php?Itemid=');

				//	$app->setUserState('com_menus.edit.item.id',	$model->getState('item.id'));
				//	$app->setUserState('com_menus.edit.item.data',	null);
				//$app->setUserState('com_menus.edit.item.type',	$type->type);
				//	$app->setUserState('com_menus.edit.item.link',	null);
		}
		//else if ($title=='url'){
			//	$app->setUserState('com_menus.edit.item.link', null );

				//	$app->setUserState('com_menus.edit.item.id',	$model->getState('item.id'));
				//	$app->setUserState('com_menus.edit.item.data',	null);
				//$app->setUserState('com_menus.edit.item.type',	$type->type);
				//	$app->setUserState('com_menus.edit.item.link',	null);
		//}

		$this->type=$type;
		$this->setRedirect('index.php?option=com_menus&view=item&layout=edit');
	}

	/**
	 * Method to run batch opterations.
	 *
	 * @return	void
	 */
	function batch()
	{
		JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));

		// Initialise variables.
		$app	= &JFactory::getApplication();
		$model	= &$this->getModel('Item');
		$vars	= JRequest::getVar('batch', array(), 'post', 'array');
		$cid	= JRequest::getVar('cid', array(), 'post', 'array');

		// Preset the redirect
		$this->setRedirect('index.php?option=com_menus&view=items');

		// Attempt to run the batch operation.
		if ($model->batch($vars, $cid))
		{
			$this->setMessage(JText::_('Menus_Batch_success'));
			return true;
		}
		else
		{
			$this->setMessage(JText::_(JText::sprintf('Menus_Error_Batch_failed', $model->getError())));
			return false;
		}
	}
}
Back to Top