[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_redirect/src/Model/ -> LinkModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_redirect
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Redirect\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Model\AdminModel;
  18  use Joomla\Utilities\ArrayHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Redirect link model.
  26   *
  27   * @since  1.6
  28   */
  29  class LinkModel extends AdminModel
  30  {
  31      /**
  32       * @var        string    The prefix to use with controller messages.
  33       * @since   1.6
  34       */
  35      protected $text_prefix = 'COM_REDIRECT';
  36  
  37      /**
  38       * Method to test whether a record can be deleted.
  39       *
  40       * @param   object  $record  A record object.
  41       *
  42       * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  43       *
  44       * @since   1.6
  45       */
  46      protected function canDelete($record)
  47      {
  48          if ($record->published != -2) {
  49              return false;
  50          }
  51  
  52          return parent::canDelete($record);
  53      }
  54  
  55      /**
  56       * Method to get the record form.
  57       *
  58       * @param   array    $data      Data for the form.
  59       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  60       *
  61       * @return  \Joomla\CMS\Form\Form A JForm object on success, false on failure
  62       *
  63       * @since   1.6
  64       */
  65      public function getForm($data = array(), $loadData = true)
  66      {
  67          // Get the form.
  68          $form = $this->loadForm('com_redirect.link', 'link', array('control' => 'jform', 'load_data' => $loadData));
  69  
  70          if (empty($form)) {
  71              return false;
  72          }
  73  
  74          // Modify the form based on access controls.
  75          if ($this->canEditState((object) $data) != true) {
  76              // Disable fields for display.
  77              $form->setFieldAttribute('published', 'disabled', 'true');
  78  
  79              // Disable fields while saving.
  80              // The controller has already verified this is a record you can edit.
  81              $form->setFieldAttribute('published', 'filter', 'unset');
  82          }
  83  
  84          // If in advanced mode then we make sure the new URL field is not compulsory and the header
  85          // field compulsory in case people select non-3xx redirects
  86          if (ComponentHelper::getParams('com_redirect')->get('mode', 0) == true) {
  87              $form->setFieldAttribute('new_url', 'required', 'false');
  88              $form->setFieldAttribute('header', 'required', 'true');
  89          }
  90  
  91          return $form;
  92      }
  93  
  94      /**
  95       * Method to get the data that should be injected in the form.
  96       *
  97       * @return  mixed  The data for the form.
  98       *
  99       * @since   1.6
 100       */
 101      protected function loadFormData()
 102      {
 103          // Check the session for previously entered form data.
 104          $data = Factory::getApplication()->getUserState('com_redirect.edit.link.data', array());
 105  
 106          if (empty($data)) {
 107              $data = $this->getItem();
 108          }
 109  
 110          $this->preprocessData('com_redirect.link', $data);
 111  
 112          return $data;
 113      }
 114  
 115      /**
 116       * Method to activate links.
 117       *
 118       * @param   array   &$pks     An array of link ids.
 119       * @param   string  $url      The new URL to set for the redirect.
 120       * @param   string  $comment  A comment for the redirect links.
 121       *
 122       * @return  boolean  Returns true on success, false on failure.
 123       *
 124       * @since   1.6
 125       */
 126      public function activate(&$pks, $url, $comment = null)
 127      {
 128          $user = Factory::getUser();
 129          $db = $this->getDatabase();
 130  
 131          // Sanitize the ids.
 132          $pks = (array) $pks;
 133          $pks = ArrayHelper::toInteger($pks);
 134  
 135          // Populate default comment if necessary.
 136          $comment = (!empty($comment)) ? $comment : Text::sprintf('COM_REDIRECT_REDIRECTED_ON', HTMLHelper::_('date', time()));
 137  
 138          // Access checks.
 139          if (!$user->authorise('core.edit', 'com_redirect')) {
 140              $pks = array();
 141              $this->setError(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
 142  
 143              return false;
 144          }
 145  
 146          if (!empty($pks)) {
 147              // Update the link rows.
 148              $query = $db->getQuery(true)
 149                  ->update($db->quoteName('#__redirect_links'))
 150                  ->set($db->quoteName('new_url') . ' = :url')
 151                  ->set($db->quoteName('published') . ' = 1')
 152                  ->set($db->quoteName('comment') . ' = :comment')
 153                  ->whereIn($db->quoteName('id'), $pks)
 154                  ->bind(':url', $url)
 155                  ->bind(':comment', $comment);
 156              $db->setQuery($query);
 157  
 158              try {
 159                  $db->execute();
 160              } catch (\RuntimeException $e) {
 161                  $this->setError($e->getMessage());
 162  
 163                  return false;
 164              }
 165          }
 166  
 167          return true;
 168      }
 169  
 170      /**
 171       * Method to batch update URLs to have new redirect urls and comments. Note will publish any unpublished URLs.
 172       *
 173       * @param   array   &$pks     An array of link ids.
 174       * @param   string  $url      The new URL to set for the redirect.
 175       * @param   string  $comment  A comment for the redirect links.
 176       *
 177       * @return  boolean  Returns true on success, false on failure.
 178       *
 179       * @since   3.6.0
 180       */
 181      public function duplicateUrls(&$pks, $url, $comment = null)
 182      {
 183          $user = Factory::getUser();
 184          $db = $this->getDatabase();
 185  
 186          // Sanitize the ids.
 187          $pks = (array) $pks;
 188          $pks = ArrayHelper::toInteger($pks);
 189  
 190          // Access checks.
 191          if (!$user->authorise('core.edit', 'com_redirect')) {
 192              $pks = array();
 193              $this->setError(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
 194  
 195              return false;
 196          }
 197  
 198          if (!empty($pks)) {
 199              $date = Factory::getDate()->toSql();
 200  
 201              // Update the link rows.
 202              $query = $db->getQuery(true)
 203                  ->update($db->quoteName('#__redirect_links'))
 204                  ->set($db->quoteName('new_url') . ' = :url')
 205                  ->set($db->quoteName('modified_date') . ' = :date')
 206                  ->set($db->quoteName('published') . ' = 1')
 207                  ->whereIn($db->quoteName('id'), $pks)
 208                  ->bind(':url', $url)
 209                  ->bind(':date', $date);
 210  
 211              if (!empty($comment)) {
 212                  $query->set($db->quoteName('comment') . ' = ' . $db->quote($comment));
 213              }
 214  
 215              $db->setQuery($query);
 216  
 217              try {
 218                  $db->execute();
 219              } catch (\RuntimeException $e) {
 220                  $this->setError($e->getMessage());
 221  
 222                  return false;
 223              }
 224          }
 225  
 226          return true;
 227      }
 228  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer