PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_redirect/models/link.php

https://bitbucket.org/ian_maclennan/joomla-cms
PHP | 173 lines | 71 code | 24 blank | 78 comment | 9 complexity | 5fb1f013130a4a3ff55811b890845819 MD5 | raw file
Possible License(s): JSON, GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access.
  8. defined('_JEXEC') or die;
  9. jimport('joomla.application.component.modeladmin');
  10. /**
  11. * Redirect link model.
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_redirect
  15. * @since 1.6
  16. */
  17. class RedirectModelLink extends JModelAdmin
  18. {
  19. /**
  20. * @var string The prefix to use with controller messages.
  21. * @since 1.6
  22. */
  23. protected $text_prefix = 'COM_REDIRECT';
  24. /**
  25. * Method to test whether a record can be deleted.
  26. *
  27. * @param object $record A record object.
  28. *
  29. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  30. * @since 1.6
  31. */
  32. protected function canDelete($record)
  33. {
  34. if ($record->published != -2) {
  35. return false;
  36. }
  37. $user = JFactory::getUser();
  38. return $user->authorise('core.admin', 'com_redirect');
  39. }
  40. /**
  41. * Method to test whether a record can have its state edited.
  42. *
  43. * @param object $record A record object.
  44. *
  45. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  46. * @since 1.6
  47. */
  48. protected function canEditState($record)
  49. {
  50. $user = JFactory::getUser();
  51. // Check the component since there are no categories or other assets.
  52. return $user->authorise('core.admin', 'com_redirect');
  53. }
  54. /**
  55. * Returns a reference to the a Table object, always creating it.
  56. *
  57. * @param type The table type to instantiate
  58. * @param string A prefix for the table class name. Optional.
  59. * @param array Configuration array for model. Optional.
  60. * @return JTable A database object
  61. * @since 1.6
  62. */
  63. public function getTable($type = 'Link', $prefix = 'RedirectTable', $config = array())
  64. {
  65. return JTable::getInstance($type, $prefix, $config);
  66. }
  67. /**
  68. * Method to get the record form.
  69. *
  70. * @param array $data Data for the form.
  71. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  72. * @return JForm A JForm object on success, false on failure
  73. * @since 1.6
  74. */
  75. public function getForm($data = array(), $loadData = true)
  76. {
  77. // Get the form.
  78. $form = $this->loadForm('com_redirect.link', 'link', array('control' => 'jform', 'load_data' => $loadData));
  79. if (empty($form)) {
  80. return false;
  81. }
  82. // Modify the form based on access controls.
  83. if ($this->canEditState((object) $data) != true) {
  84. // Disable fields for display.
  85. $form->setFieldAttribute('published', 'disabled', 'true');
  86. // Disable fields while saving.
  87. // The controller has already verified this is a record you can edit.
  88. $form->setFieldAttribute('published', 'filter', 'unset');
  89. }
  90. return $form;
  91. }
  92. /**
  93. * Method to get the data that should be injected in the form.
  94. *
  95. * @return mixed The data for the form.
  96. * @since 1.6
  97. */
  98. protected function loadFormData()
  99. {
  100. // Check the session for previously entered form data.
  101. $data = JFactory::getApplication()->getUserState('com_redirect.edit.link.data', array());
  102. if (empty($data)) {
  103. $data = $this->getItem();
  104. }
  105. return $data;
  106. }
  107. /**
  108. * Method to activate links.
  109. *
  110. * @param array An array of link ids.
  111. * @param string The new URL to set for the redirect.
  112. * @param string A comment for the redirect links.
  113. * @return boolean Returns true on success, false on failure.
  114. * @since 1.6
  115. */
  116. public function activate(&$pks, $url, $comment = null)
  117. {
  118. // Initialise variables.
  119. $user = JFactory::getUser();
  120. $db = $this->getDbo();
  121. // Sanitize the ids.
  122. $pks = (array) $pks;
  123. JArrayHelper::toInteger($pks);
  124. // Populate default comment if necessary.
  125. $comment = (!empty($comment)) ? $comment : JText::sprintf('COM_REDIRECT_REDIRECTED_ON', JHtml::_('date',time()));
  126. // Access checks.
  127. if (!$user->authorise('core.admin', 'com_redirect')) {
  128. $pks = array();
  129. $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
  130. return false;
  131. }
  132. if (!empty($pks)) {
  133. // Update the link rows.
  134. $db->setQuery(
  135. 'UPDATE `#__redirect_links`' .
  136. ' SET `new_url` = '.$db->Quote($url).', `published` = 1, `comment` = '.$db->Quote($comment) .
  137. ' WHERE `id` IN ('.implode(',', $pks).')'
  138. );
  139. $db->query();
  140. // Check for a database error.
  141. if ($error = $this->_db->getErrorMsg())
  142. {
  143. $this->setError($error);
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. }