[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_messages/src/Model/ -> MessagesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_messages
   6   *
   7   * @copyright   (C) 2008 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\Messages\Administrator\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  15  use Joomla\CMS\MVC\Model\ListModel;
  16  use Joomla\Database\ParameterType;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Messages Component Messages Model
  24   *
  25   * @since  1.6
  26   */
  27  class MessagesModel extends ListModel
  28  {
  29      /**
  30       * Override parent constructor.
  31       *
  32       * @param   array                $config   An optional associative array of configuration settings.
  33       * @param   MVCFactoryInterface  $factory  The factory.
  34       *
  35       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  36       * @since   3.2
  37       */
  38      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  39      {
  40          if (empty($config['filter_fields'])) {
  41              $config['filter_fields'] = array(
  42                  'message_id', 'a.id',
  43                  'subject', 'a.subject',
  44                  'state', 'a.state',
  45                  'user_id_from', 'a.user_id_from',
  46                  'user_id_to', 'a.user_id_to',
  47                  'date_time', 'a.date_time',
  48                  'priority', 'a.priority',
  49              );
  50          }
  51  
  52          parent::__construct($config, $factory);
  53      }
  54  
  55      /**
  56       * Method to auto-populate the model state.
  57       *
  58       * This method should only be called once per instantiation and is designed
  59       * to be called on the first call to the getState() method unless the model
  60       * configuration flag to ignore the request is set.
  61       *
  62       * Note. Calling getState in this method will result in recursion.
  63       *
  64       * @param   string  $ordering   An optional ordering field.
  65       * @param   string  $direction  An optional direction (asc|desc).
  66       *
  67       * @return  void
  68       *
  69       * @since   1.6
  70       */
  71      protected function populateState($ordering = 'a.date_time', $direction = 'desc')
  72      {
  73          // List state information.
  74          parent::populateState($ordering, $direction);
  75      }
  76  
  77      /**
  78       * Method to get a store id based on model configuration state.
  79       *
  80       * This is necessary because the model is used by the component and
  81       * different modules that might need different sets of data or different
  82       * ordering requirements.
  83       *
  84       * @param   string  $id  A prefix for the store id.
  85       *
  86       * @return  string    A store id.
  87       *
  88       * @since   1.6
  89       */
  90      protected function getStoreId($id = '')
  91      {
  92          // Compile the store id.
  93          $id .= ':' . $this->getState('filter.search');
  94          $id .= ':' . $this->getState('filter.state');
  95  
  96          return parent::getStoreId($id);
  97      }
  98  
  99      /**
 100       * Build an SQL query to load the list data.
 101       *
 102       * @return  \Joomla\Database\DatabaseQuery
 103       *
 104       * @since   1.6
 105       */
 106      protected function getListQuery()
 107      {
 108          // Create a new query object.
 109          $db = $this->getDatabase();
 110          $query = $db->getQuery(true);
 111          $user = Factory::getUser();
 112          $id   = (int) $user->get('id');
 113  
 114          // Select the required fields from the table.
 115          $query->select(
 116              $this->getState(
 117                  'list.select',
 118                  [
 119                      $db->quoteName('a') . '.*',
 120                      $db->quoteName('u.name', 'user_from'),
 121                  ]
 122              )
 123          );
 124          $query->from($db->quoteName('#__messages', 'a'));
 125  
 126          // Join over the users for message owner.
 127          $query->join(
 128              'INNER',
 129              $db->quoteName('#__users', 'u'),
 130              $db->quoteName('u.id') . ' = ' . $db->quoteName('a.user_id_from')
 131          )
 132              ->where($db->quoteName('a.user_id_to') . ' = :id')
 133              ->bind(':id', $id, ParameterType::INTEGER);
 134  
 135          // Filter by published state.
 136          $state = $this->getState('filter.state');
 137  
 138          if (is_numeric($state)) {
 139              $state = (int) $state;
 140              $query->where($db->quoteName('a.state') . ' = :state')
 141                  ->bind(':state', $state, ParameterType::INTEGER);
 142          } elseif ($state !== '*') {
 143              $query->whereIn($db->quoteName('a.state'), [0, 1]);
 144          }
 145  
 146          // Filter by search in subject or message.
 147          $search = $this->getState('filter.search');
 148  
 149          if (!empty($search)) {
 150              $search = '%' . str_replace(' ', '%', trim($search)) . '%';
 151              $query->extendWhere(
 152                  'AND',
 153                  [
 154                      $db->quoteName('a.subject') . ' LIKE :subject',
 155                      $db->quoteName('a.message') . ' LIKE :message',
 156                  ],
 157                  'OR'
 158              )
 159                  ->bind(':subject', $search)
 160                  ->bind(':message', $search);
 161          }
 162  
 163          // Add the list ordering clause.
 164          $query->order($db->escape($this->getState('list.ordering', 'a.date_time')) . ' ' . $db->escape($this->getState('list.direction', 'DESC')));
 165  
 166          return $query;
 167      }
 168  
 169      /**
 170       * Purge the messages table of old messages for the given user ID.
 171       *
 172       * @param   int  $userId  The user id
 173       *
 174       * @return  void
 175       *
 176       * @since   4.2.0
 177       */
 178      public function purge(int $userId): void
 179      {
 180          $db    = $this->getDatabase();
 181          $query = $db->getQuery(true)
 182              ->select($db->quoteName(['cfg_name', 'cfg_value']))
 183              ->from($db->quoteName('#__messages_cfg'))
 184              ->where(
 185                  [
 186                      $db->quoteName('user_id') . ' = :userId',
 187                      $db->quoteName('cfg_name') . ' = ' . $db->quote('auto_purge'),
 188                  ]
 189              )
 190              ->bind(':userId', $userId, ParameterType::INTEGER);
 191  
 192          $db->setQuery($query);
 193          $config = $db->loadObject();
 194  
 195          // Default is 7 days
 196          $purge = 7;
 197  
 198          // Check if auto_purge value set
 199          if (\is_object($config) && $config->cfg_name === 'auto_purge') {
 200              $purge = $config->cfg_value;
 201          }
 202  
 203          // If purge value is not 0, then allow purging of old messages
 204          if ($purge > 0) {
 205              // Purge old messages at day set in message configuration
 206              $past = Factory::getDate(time() - $purge * 86400)->toSql();
 207  
 208              $query = $db->getQuery(true)
 209                  ->delete($db->quoteName('#__messages'))
 210                  ->where(
 211                      [
 212                          $db->quoteName('date_time') . ' < :past',
 213                          $db->quoteName('user_id_to') . ' = :userId',
 214                      ]
 215                  )
 216                  ->bind(':past', $past)
 217                  ->bind(':userId', $userId, ParameterType::INTEGER);
 218  
 219              $db->setQuery($query);
 220              $db->execute();
 221          }
 222      }
 223  }


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