PageRenderTime 29ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/J2MTecnologia/joomla-3.x
PHP | 180 lines | 82 code | 21 blank | 77 comment | 8 complexity | f5b8229e7c113114cc800c163109924c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_redirect
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  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. {
  36. return false;
  37. }
  38. $user = JFactory::getUser();
  39. return $user->authorise('core.admin', 'com_redirect');
  40. }
  41. /**
  42. * Method to test whether a record can have its state edited.
  43. *
  44. * @param object $record A record object.
  45. *
  46. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  47. * @since 1.6
  48. */
  49. protected function canEditState($record)
  50. {
  51. $user = JFactory::getUser();
  52. // Check the component since there are no categories or other assets.
  53. return $user->authorise('core.admin', 'com_redirect');
  54. }
  55. /**
  56. * Returns a reference to the a Table object, always creating it.
  57. *
  58. * @param type The table type to instantiate
  59. * @param string A prefix for the table class name. Optional.
  60. * @param array Configuration array for model. Optional.
  61. * @return JTable A database object
  62. * @since 1.6
  63. */
  64. public function getTable($type = 'Link', $prefix = 'RedirectTable', $config = array())
  65. {
  66. return JTable::getInstance($type, $prefix, $config);
  67. }
  68. /**
  69. * Method to get the record form.
  70. *
  71. * @param array $data Data for the form.
  72. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  73. * @return JForm A JForm object on success, false on failure
  74. * @since 1.6
  75. */
  76. public function getForm($data = array(), $loadData = true)
  77. {
  78. // Get the form.
  79. $form = $this->loadForm('com_redirect.link', 'link', array('control' => 'jform', 'load_data' => $loadData));
  80. if (empty($form))
  81. {
  82. return false;
  83. }
  84. // Modify the form based on access controls.
  85. if ($this->canEditState((object) $data) != true)
  86. {
  87. // Disable fields for display.
  88. $form->setFieldAttribute('published', 'disabled', 'true');
  89. // Disable fields while saving.
  90. // The controller has already verified this is a record you can edit.
  91. $form->setFieldAttribute('published', 'filter', 'unset');
  92. }
  93. return $form;
  94. }
  95. /**
  96. * Method to get the data that should be injected in the form.
  97. *
  98. * @return mixed The data for the form.
  99. * @since 1.6
  100. */
  101. protected function loadFormData()
  102. {
  103. // Check the session for previously entered form data.
  104. $data = JFactory::getApplication()->getUserState('com_redirect.edit.link.data', array());
  105. if (empty($data))
  106. {
  107. $data = $this->getItem();
  108. }
  109. $this->preprocessData('com_redirect.link', $data);
  110. return $data;
  111. }
  112. /**
  113. * Method to activate links.
  114. *
  115. * @param array An array of link ids.
  116. * @param string The new URL to set for the redirect.
  117. * @param string A comment for the redirect links.
  118. * @return boolean Returns true on success, false on failure.
  119. * @since 1.6
  120. */
  121. public function activate(&$pks, $url, $comment = null)
  122. {
  123. $user = JFactory::getUser();
  124. $db = $this->getDbo();
  125. // Sanitize the ids.
  126. $pks = (array) $pks;
  127. JArrayHelper::toInteger($pks);
  128. // Populate default comment if necessary.
  129. $comment = (!empty($comment)) ? $comment : JText::sprintf('COM_REDIRECT_REDIRECTED_ON', JHtml::_('date', time()));
  130. // Access checks.
  131. if (!$user->authorise('core.admin', 'com_redirect'))
  132. {
  133. $pks = array();
  134. $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
  135. return false;
  136. }
  137. if (!empty($pks))
  138. {
  139. // Update the link rows.
  140. $query = $db->getQuery(true)
  141. ->update($db->quoteName('#__redirect_links'))
  142. ->set($db->quoteName('new_url') . ' = ' . $db->quote($url))
  143. ->set($db->quoteName('published') . ' = ' . $db->quote(1))
  144. ->set($db->quoteName('comment') . ' = ' . $db->quote($comment))
  145. ->where($db->quoteName('id') . ' IN (' . implode(',', $pks) . ')');
  146. $db->setQuery($query);
  147. try
  148. {
  149. $db->execute();
  150. }
  151. catch (RuntimeException $e)
  152. {
  153. $this->setError($e->getMessage());
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. }