the-art-of-joomla-archive /redirect/code/trunk/administrator/components/com_redirect/controllers/link.php

Language PHP Lines 329
MD5 Hash ec4946a194620dd807da25c61ba87784 Estimated Cost $3,454 (why?)
Repository https://bitbucket.org/eddieajau/the-art-of-joomla-archive 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
<?php
/**
 * @version		$Id: link.php 391 2010-11-05 11:40:55Z eddieajau $
 * @package		NewLifeInIT
 * @subpackage	com_redirect
 * @copyright	Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

defined('_JEXEC') or die('Invalid Request.');

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

/**
 * The Redirect Link Controller
 *
 * @package		Redirect
 * @subpackage	com_redirect
 * @version		1.0
 */
class RedirectControllerLink extends JController
{
	/**
	 * Overridden constructor to register alternate tasks
	 *
	 * @access	protected
	 * @return	void
	 * @since	1.0
	 */
	function __construct()
	{
		parent::__construct();

		// Map the save tasks.
		$this->registerTask('save2new',		'save');
		$this->registerTask('apply',		'save');

		// Map the publishing state tasks.
		$this->registerTask('unpublish',	'publish');
		$this->registerTask('archive',		'publish');
	}

	/**
	 * Dummy method to redirect back to standard controller
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function display()
	{
		$this->setRedirect('index.php?option=com_redirect');
	}

	/**
	 * Method to add a new redirect link.
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function add()
	{
		// Initialize variables.
		$app = &JFactory::getApplication();

		// Clear the link id from the session.
		$app->setUserState('redirect.edit.link.id', null);

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

	/**
	 * Method to setup a redirect link for editing and redirect to the edit form.
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function edit()
	{
		// Initialize variables.
		$app	= &JFactory::getApplication();
		$cid	= JRequest::getVar('cid', array(), '', 'array');

		// Get the previous link id (if any) and the current link id.
		$previousId = (int) $app->getUserState('redirect.edit.link.id');
		$linkId		= (int) (count($cid) ? $cid[0] : JRequest::getInt('l_id'));

		// Set the category id for the category to edit in the session.
		$app->setUserState('redirect.edit.link.id', $linkId);

		// Get the model.
		$model = &$this->getModel('Link', 'RedirectModel');

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

	/**
	 * Method to cancel an edit
	 *
	 * Sets item id in the session to null, and then redirects to the list screen.
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function cancel()
	{
		// Initialize variables.
		$app = &JFactory::getApplication();

		// Clear the link id from the session.
		$app->setUserState('redirect.edit.link.id', null);

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

	/**
	 * Method to delete redirect links.
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function delete()
	{
		// Check for request forgeries.
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Get and sanitize the items to delete.
		$cid = JRequest::getVar('cid', null, 'post', 'array');
		JArrayHelper::toInteger($cid);

		// Get the model.
		$model = &$this->getModel('Link', 'RedirectModel');

		// Attempt to delete the item(s).
		if (!$model->delete($cid)) {
			$this->setMessage(JText::sprintf('REDIRECT_LINK_DELETE_FAILED', $model->getError()), 'notice');
		}
		else {
			$this->setMessage(JText::sprintf('REDIRECT_LINK_DELETE_SUCCESS', count($cid)));
		}

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

	/**
	 * Method to save a redirect link.
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function save()
	{
		// Check for request forgeries.
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialize variables.
		$app = &JFactory::getApplication();

		// Get the posted values from the request.
		$data = JRequest::getVar('jxform', array(), 'post', 'array');

		// Populate the row id from the session.
		$data['id'] = (int) $app->getUserState('redirect.edit.link.id');

		// Get the model and attempt to validate the posted data.
		$model = &$this->getModel('Link', 'RedirectModel');
		$return	= $model->validate($data);

		// Check for validation errors.
		if ($return === 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('redirect.edit.link.data', $data);

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

		// Attempt to save the data.
		$return	= $model->save($data);

		// Check for errors.
		if ($return === false)
		{
			// Save the data in the session.
			$app->setUserState('redirect.edit.link.data', $data);

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

		// Redirect the user and adjust session state based on the chosen task.
		switch ($this->_task)
		{
			case 'apply':
				// Redirect back to the edit screen.
				$this->setMessage(JText::_('REDIRECT_LINK_SAVE_SUCCESS'));
				$this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
				break;

			case 'save2new':
				// Clear the link id from the session.
				$app->setUserState('redirect.edit.link.id', null);

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

			default:
				// Clear the link id from the session.
				$app->setUserState('redirect.edit.link.id', null);

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

		// Flush the data from the session.
		$app->setUserState('redirect.edit.link.data', null);
	}

	/**
	 * Method to activate a list of links
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function activate()
	{
		// Check for request forgeries.
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Get and sanitize the items to delete.
		$cid = JRequest::getVar('cid', null, 'post', 'array');
		JArrayHelper::toInteger($cid);

		// Get the model.
		$model = &$this->getModel('Link', 'RedirectModel', array('ignore_request' => true));

		$new_url = JRequest::getString('new_url');
		$comment = JRequest::getString('comment');

		// Attempt to activate the links
		$result = $model->activate($cid, $new_url, $comment);

		// If there is a problem, set the error message.
		if (!$result) {
			$this->setMessage($model->getError());
		}

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

	/**
	 * Method to set the published state of links.
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function publish()
	{
		// Check for request forgeries.
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Get and sanitize the items to delete.
		$cid = JRequest::getVar('cid', null, 'post', 'array');
		JArrayHelper::toInteger($cid);

		// Get the model.
		$model = &$this->getModel('Link', 'RedirectModel', array('ignore_request' => true));

		// Attempt to set the publishing state for the links.
		switch ($this->getTask())
		{
			case 'publish':
				$result = $model->publish($cid);
				break;

			case 'unpublish':
				$result = $model->unpublish($cid);
				break;

			case 'archive':
				$result = $model->archive($cid);
				break;
		}

		// If there is a problem, set the error message.
		if (!$result) {
			$this->setMessage($model->getError());
		}

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