[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_redirect/src/Model/ -> LinksModel.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\MVC\Factory\MVCFactoryInterface;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\Database\ParameterType;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Methods supporting a list of redirect links.
  25   *
  26   * @since  1.6
  27   */
  28  class LinksModel extends ListModel
  29  {
  30      /**
  31       * Constructor.
  32       *
  33       * @param   array                $config   An optional associative array of configuration settings.
  34       * @param   MVCFactoryInterface  $factory  The factory.
  35       *
  36       * @since   1.6
  37       */
  38      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  39      {
  40          if (empty($config['filter_fields'])) {
  41              $config['filter_fields'] = array(
  42                  'id', 'a.id',
  43                  'state', 'a.state',
  44                  'old_url', 'a.old_url',
  45                  'new_url', 'a.new_url',
  46                  'referer', 'a.referer',
  47                  'hits', 'a.hits',
  48                  'created_date', 'a.created_date',
  49                  'published', 'a.published',
  50                  'header', 'a.header', 'http_status',
  51              );
  52          }
  53  
  54          parent::__construct($config, $factory);
  55      }
  56      /**
  57       * Removes all of the unpublished redirects from the table.
  58       *
  59       * @return  boolean result of operation
  60       *
  61       * @since   3.5
  62       */
  63      public function purge()
  64      {
  65          $db = $this->getDatabase();
  66  
  67          $query = $db->getQuery(true);
  68  
  69          $query->delete('#__redirect_links')->where($db->quoteName('published') . '= 0');
  70  
  71          $db->setQuery($query);
  72  
  73          try {
  74              $db->execute();
  75          } catch (\Exception $e) {
  76              return false;
  77          }
  78  
  79          return true;
  80      }
  81  
  82      /**
  83       * Method to auto-populate the model state.
  84       *
  85       * Note. Calling getState in this method will result in recursion.
  86       *
  87       * @param   string  $ordering   An optional ordering field.
  88       * @param   string  $direction  An optional direction (asc|desc).
  89       *
  90       * @return  void
  91       *
  92       * @since   1.6
  93       */
  94      protected function populateState($ordering = 'a.old_url', $direction = 'asc')
  95      {
  96          // Load the parameters.
  97          $params = ComponentHelper::getParams('com_redirect');
  98          $this->setState('params', $params);
  99  
 100          // List state information.
 101          parent::populateState($ordering, $direction);
 102      }
 103  
 104      /**
 105       * Method to get a store id based on model configuration state.
 106       *
 107       * This is necessary because the model is used by the component and
 108       * different modules that might need different sets of data or different
 109       * ordering requirements.
 110       *
 111       * @param   string  $id  A prefix for the store id.
 112       *
 113       * @return  string  A store id.
 114       *
 115       * @since   1.6
 116       */
 117      protected function getStoreId($id = '')
 118      {
 119          // Compile the store id.
 120          $id .= ':' . $this->getState('filter.search');
 121          $id .= ':' . $this->getState('filter.state');
 122          $id .= ':' . $this->getState('filter.http_status');
 123  
 124          return parent::getStoreId($id);
 125      }
 126  
 127      /**
 128       * Build an SQL query to load the list data.
 129       *
 130       * @return  \Joomla\Database\DatabaseQuery
 131       *
 132       * @since   1.6
 133       */
 134      protected function getListQuery()
 135      {
 136          // Create a new query object.
 137          $db = $this->getDatabase();
 138          $query = $db->getQuery(true);
 139  
 140          // Select the required fields from the table.
 141          $query->select(
 142              $this->getState(
 143                  'list.select',
 144                  'a.*'
 145              )
 146          );
 147          $query->from($db->quoteName('#__redirect_links', 'a'));
 148  
 149          // Filter by published state
 150          $state = (string) $this->getState('filter.state');
 151  
 152          if (is_numeric($state)) {
 153              $state = (int) $state;
 154              $query->where($db->quoteName('a.published') . ' = :state')
 155                  ->bind(':state', $state, ParameterType::INTEGER);
 156          } elseif ($state === '') {
 157              $query->whereIn($db->quoteName('a.published'), [0,1]);
 158          }
 159  
 160          // Filter the items over the HTTP status code header.
 161          if ($httpStatusCode = $this->getState('filter.http_status')) {
 162              $httpStatusCode = (int) $httpStatusCode;
 163              $query->where($db->quoteName('a.header') . ' = :header')
 164                  ->bind(':header', $httpStatusCode, ParameterType::INTEGER);
 165          }
 166  
 167          // Filter the items over the search string if set.
 168          $search = $this->getState('filter.search');
 169  
 170          if (!empty($search)) {
 171              if (stripos($search, 'id:') === 0) {
 172                  $ids = (int) substr($search, 3);
 173                  $query->where($db->quoteName('a.id') . ' = :id');
 174                  $query->bind(':id', $ids, ParameterType::INTEGER);
 175              } else {
 176                  $search = '%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%');
 177                  $query->where(
 178                      '(' . $db->quoteName('old_url') . ' LIKE :oldurl'
 179                      . ' OR ' . $db->quoteName('new_url') . ' LIKE :newurl'
 180                      . ' OR ' . $db->quoteName('comment') . ' LIKE :comment'
 181                      . ' OR ' . $db->quoteName('referer') . ' LIKE :referer)'
 182                  )
 183                      ->bind(':oldurl', $search)
 184                      ->bind(':newurl', $search)
 185                      ->bind(':comment', $search)
 186                      ->bind(':referer', $search);
 187              }
 188          }
 189  
 190          // Add the list ordering clause.
 191          $query->order($db->escape($this->getState('list.ordering', 'a.old_url')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 192  
 193          return $query;
 194      }
 195  
 196      /**
 197       * Add the entered URLs into the database
 198       *
 199       * @param   array  $batchUrls  Array of URLs to enter into the database
 200       *
 201       * @return boolean
 202       */
 203      public function batchProcess($batchUrls)
 204      {
 205          $db    = $this->getDatabase();
 206          $query = $db->getQuery(true);
 207  
 208          $params  = ComponentHelper::getParams('com_redirect');
 209          $state   = (int) $params->get('defaultImportState', 0);
 210          $created = Factory::getDate()->toSql();
 211  
 212          $columns = [
 213              'old_url',
 214              'new_url',
 215              'referer',
 216              'comment',
 217              'hits',
 218              'published',
 219              'created_date',
 220              'modified_date',
 221          ];
 222  
 223          $values = [
 224              ':oldurl',
 225              ':newurl',
 226              $db->quote(''),
 227              $db->quote(''),
 228              0,
 229              ':state',
 230              ':created',
 231              ':modified',
 232          ];
 233  
 234          $query
 235              ->insert($db->quoteName('#__redirect_links'), false)
 236              ->columns($db->quoteName($columns))
 237              ->values(implode(', ', $values))
 238              ->bind(':oldurl', $old_url)
 239              ->bind(':newurl', $new_url)
 240              ->bind(':state', $state, ParameterType::INTEGER)
 241              ->bind(':created', $created)
 242              ->bind(':modified', $created);
 243  
 244          $db->setQuery($query);
 245  
 246          foreach ($batchUrls as $batch_url) {
 247              $old_url = $batch_url[0];
 248  
 249              // Destination URL can also be an external URL
 250              if (!empty($batch_url[1])) {
 251                  $new_url = $batch_url[1];
 252              } else {
 253                  $new_url = '';
 254              }
 255  
 256              $db->execute();
 257          }
 258  
 259          return true;
 260      }
 261  }


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