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

Language PHP Lines 357
MD5 Hash 262132fc5c4e70b1ed79869fda7367a1 Estimated Cost $4,022 (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
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
<?php
/**
 * @version		$Id: link.php 390 2010-11-05 11:35:33Z 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.');

juimport('joomla.application.component.model16');
juimport('joomla.database.databasequery');

/**
 * The Redirect Link Model
 *
 * @package		NewLifeInIT
 * @subpackage	com_redirect
 * @version		1.0
 */
class RedirectModelLink extends JModel16
{
	/**
	 * Array of items for memory caching.
	 *
	 * @access	protected
	 * @var		array
	 */
	var $_items			= array();

	/**
	 * Auto-populate the model state.
	 *
	 * @return	void
	 */
	protected function populateState()
	{
		// Get the application object.
		$app = &JFactory::getApplication();

		// Attempt to auto-load the link id.
		$linkId = (int) $app->getUserState('redirect.edit.link.id');
		if (!$linkId) {
			$linkId = (int)JRequest::getInt('l_id');
		}

		// Only set the link id if there is a value.
		if ($linkId) {
			$this->setState('link.id', $linkId);
		}
	}

	/**
	 * Method to get a link item.
	 *
	 * @param	integer	The id of the link to get.
	 * @return	mixed	Link data object on success, false on failure.
	 * @since	1.0
	 */
	public function &getItem($linkId = null)
	{
		// Initialize variables.
		$linkId = (!empty($linkId)) ? $linkId : (int)$this->getState('link.id');
		$false	= false;

		// Get a link row instance.
		$table = &$this->getTable('Link', 'RedirectTable');

		// Attempt to load the row.
		$return = $table->load($linkId);

		// Check for a table object error.
		if ($return === false && $table->getError()) {
			$this->serError($table->getError());
			return $false;
		}

		// Check for a database error.
		if ($this->_db->getErrorNum()) {
			$this->setError($this->_db->getErrorMsg());
			return $false;
		}

		$value = JArrayHelper::toObject($table->getProperties(1), 'JObject');

		return $value;
	}

	/**
	 * Method to get the form object.
	 *
	 * @param	string	The type of form to load (view, model).
	 *
	 * @return	mixed	JXForm object on success, false on failure.
	 */
	public function &getForm($type = 'view')
	{
		// Initialize variables.
		$app	= JFactory::getApplication();
		$false	= false;

		// Get the form.
		juimport('jxtended.form.form');
		JForm::addFormPath(JPATH_COMPONENT.'/models/forms');
		JForm::addFieldPath(JPATH_COMPONENT.'/models/fields');
		$form = JForm::getInstance('link', 'jxform', true, array('array' => 'jxform'));

		// Check for an error.
		if (JError::isError($form)) {
			$this->setError($form->getMessage());
			return $false;
		}

		// Check the session for previously entered form data.
		$data = $app->getUserState('redirect.edit.link.data', array());

		// Bind the form data if present.
		if (!empty($data)) {
			$form->bind($data);
		}

		return $form;
	}

	/**
	 * Method to publish links.
	 *
	 * @param	array	The ids of the items to publish.
	 * @return	boolean	True on success.
	 * @since	1.0
	 */
	public function publish($linkId)
	{
		// Sanitize the ids.
		$linkId = (array) $linkId;
		JArrayHelper::toInteger($linkId);

		// Get the current user object.
		$user = &JFactory::getUser();

		// Get a link row instance.
		$table = &$this->getTable('Link', 'RedirectTable');

		// Attempt to publish the items.
		if (!$table->publish($linkId, 1, $user->get('id'))) {
			$this->setError($table->getError());
			return false;
		}

		return true;
	}

	/**
	 * Method to unpublish links.
	 *
	 * @param	array	The ids of the items to unpublish.
	 * @return	boolean	True on success.
	 * @since	1.0
	 */
	public function unpublish($linkId)
	{
		// Sanitize the ids.
		$linkId = (array) $linkId;
		JArrayHelper::toInteger($linkId);

		// Get the current user object.
		$user = &JFactory::getUser();

		// Get a link row instance.
		$table = &$this->getTable('Link', 'RedirectTable');

		// Attempt to unpublish the items.
		if (!$table->publish($linkId, 0, $user->get('id'))) {
			$this->setError($table->getError());
			return false;
		}

		return true;
	}

	/**
	 * Method to archive links.
	 *
	 * @param	array	The ids of the items to unpublish.
	 * @return	boolean	True on success.
	 * @since	1.0
	 */
	public function archive($linkId)
	{
		// Sanitize the ids.
		$linkId = (array) $linkId;
		JArrayHelper::toInteger($linkId);

		// Get the current user object.
		$user = &JFactory::getUser();

		// Get a link row instance.
		$table = &$this->getTable('Link', 'RedirectTable');

		// Attempt to unpublish the items.
		if (!$table->publish($linkId, 2, $user->get('id'))) {
			$this->setError($table->getError());
			return false;
		}

		return true;
	}

	/**
	 * Method to delete links.
	 *
	 * @param	array	An array of link ids.
	 * @return	boolean	Returns true on success, false on failure.
	 * @since	1.0
	 */
	public function delete($linkId)
	{
		// Sanitize the ids.
		$linkId = (array) $linkId;
		JArrayHelper::toInteger($linkId);

		// Get a link row instance.
		$table = &$this->getTable('Link', 'RedirectTable');

		// Iterate the links to delete each one.
		foreach ($linkId as $id)
		{
			$table->delete($id);
		}

		return true;
	}

	/**
	 * Method to activate links.
	 *
	 * @param	array	An array of link ids.
	 * @param	string	The new URL to set for the redirect.
	 * @param	string	A comment for the redirect links.
	 * @return	boolean	Returns true on success, false on failure.
	 * @since	1.0
	 */
	public function activate($linkId, $url, $comment=null)
	{
		// Sanitize the ids.
		$linkId = (array) $linkId;
		JArrayHelper::toInteger($linkId);

		// Populate default comment if necessary.
		$comment = (!empty($comment)) ? $comment : JText::sprintf('REDIRECTED_ON', JHtml::date(time()));

		if (!empty($linkId)) {

			// Implode the link ids.
			$linkId = implode(' OR `id` = ', $linkId);

			// Update the link rows.
			$this->_db->setQuery(
				'UPDATE `#__redirect_links`' .
				' SET `new_url` = '.$this->_db->Quote($url).', `published` = 1, `comment` = '.$this->_db->Quote($comment) .
				' WHERE `id` ='.$linkId
			);
			$this->_db->query();

			// Check for a database error.
			if ($this->_db->getErrorNum()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}

	/**
	 * Method to validate the form data.
	 *
	 * @param	array	The form data.
	 * @return	mixed	Array of filtered data if valid, false otherwise.
	 * @since	1.0
	 */
	public function validate($data)
	{
		// Get the form.
		$form = &$this->getForm('model');

		// Check for an error.
		if ($form === false) {
			return false;
		}
		// Filter and validate the form data.
		$form->filter($data);
		$return	= $form->validate($data);

		// Check for an error.
		if (JError::isError($return)) {
			$this->setError($return->getMessage());
			return false;
		}

		// Check the validation results.
		if ($return === false)
		{
			// Get the validation messages from the form.
			foreach ($form->getErrors() as $message) {
				$this->setError($message);
			}

			return false;
		}

		return $data;
	}

	/**
	 * Method to save the form data.
	 *
	 * @param	array	The form data.
	 * @return	boolean	True on success.
	 * @since	1.0
	 */
	public function save($data)
	{
		$linkId = (!empty($data['id'])) ? $data['id'] : (int)$this->getState('link.id');
		$isNew	= true;

		// Get a link row instance.
		$table = &$this->getTable('Link', 'RedirectTable');

		// Load the row if saving an existing item.
		if ($linkId > 0) {
			$table->load($linkId);
			$isNew = false;
		}

		// Bind the data.
		if (!$table->bind($data)) {
			$this->setError(JText::sprintf('REDIRECT_LINK_BIND_FAILED', $table->getError()));
			return false;
		}

		// Check the data.
		if (!$table->check()) {
			$this->setError($table->getError());
			return false;
		}

		// Store the data.
		if (!$table->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return $table->id;
	}
}
Back to Top