[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_users/src/Model/ -> NotesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_users
   6   *
   7   * @copyright   (C) 2011 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\Users\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\CMS\User\User;
  17  use Joomla\Database\DatabaseQuery;
  18  use Joomla\Database\ParameterType;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * User notes model class.
  26   *
  27   * @since  2.5
  28   */
  29  class NotesModel extends ListModel
  30  {
  31      /**
  32       * Constructor.
  33       *
  34       * @param   array                $config   An optional associative array of configuration settings.
  35       * @param   MVCFactoryInterface  $factory  The factory.
  36       *
  37       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  38       * @since   3.2
  39       */
  40      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  41      {
  42          // Set the list ordering fields.
  43          if (empty($config['filter_fields'])) {
  44              $config['filter_fields'] = array(
  45                  'id', 'a.id',
  46                  'user_id', 'a.user_id',
  47                  'u.name',
  48                  'subject', 'a.subject',
  49                  'catid', 'a.catid', 'category_id',
  50                  'state', 'a.state', 'published',
  51                  'c.title',
  52                  'review_time', 'a.review_time',
  53                  'publish_up', 'a.publish_up',
  54                  'publish_down', 'a.publish_down',
  55                  'level', 'c.level',
  56              );
  57          }
  58  
  59          parent::__construct($config, $factory);
  60      }
  61  
  62      /**
  63       * Build an SQL query to load the list data.
  64       *
  65       * @return  DatabaseQuery  A DatabaseQuery object to retrieve the data set.
  66       *
  67       * @since   2.5
  68       */
  69      protected function getListQuery()
  70      {
  71          $db = $this->getDatabase();
  72          $query = $db->getQuery(true);
  73  
  74          // Select the required fields from the table.
  75          $query->select(
  76              $this->getState(
  77                  'list.select',
  78                  'a.id, a.subject, a.checked_out, a.checked_out_time,' .
  79                  'a.catid, a.created_time, a.review_time,' .
  80                  'a.state, a.publish_up, a.publish_down'
  81              )
  82          );
  83          $query->from('#__user_notes AS a');
  84  
  85          // Join over the category
  86          $query->select('c.title AS category_title, c.params AS category_params')
  87              ->join('LEFT', '#__categories AS c ON c.id = a.catid');
  88  
  89          // Join over the users for the note user.
  90          $query->select('u.name AS user_name')
  91              ->join('LEFT', '#__users AS u ON u.id = a.user_id');
  92  
  93          // Join over the users for the checked out user.
  94          $query->select('uc.name AS editor')
  95              ->join('LEFT', '#__users AS uc ON uc.id = a.checked_out');
  96  
  97          // Filter by search in title
  98          $search = $this->getState('filter.search');
  99  
 100          if (!empty($search)) {
 101              if (stripos($search, 'id:') === 0) {
 102                  $search3 = (int) substr($search, 3);
 103                  $query->where($db->quoteName('a.id') . ' = :id');
 104                  $query->bind(':id', $search3, ParameterType::INTEGER);
 105              } elseif (stripos($search, 'uid:') === 0) {
 106                  $search4 = (int) substr($search, 4);
 107                  $query->where($db->quoteName('a.user_id') . ' = :id');
 108                  $query->bind(':id', $search4, ParameterType::INTEGER);
 109              } else {
 110                  $search = '%' . trim($search) . '%';
 111                  $query->where(
 112                      '(' . $db->quoteName('a.subject') . ' LIKE :subject'
 113                      . ' OR ' . $db->quoteName('u.name') . ' LIKE :name'
 114                      . ' OR ' . $db->quoteName('u.username') . ' LIKE :username)'
 115                  );
 116                  $query->bind(':subject', $search);
 117                  $query->bind(':name', $search);
 118                  $query->bind(':username', $search);
 119              }
 120          }
 121  
 122          // Filter by published state
 123          $published = $this->getState('filter.published');
 124  
 125          if (is_numeric($published)) {
 126              $query->where($db->quoteName('a.state') . ' = :state')
 127                  ->bind(':state', $published, ParameterType::INTEGER);
 128          } elseif ($published !== '*') {
 129              $query->whereIn($db->quoteName('a.state'), [0, 1]);
 130          }
 131  
 132          // Filter by a single category.
 133          $categoryId = (int) $this->getState('filter.category_id');
 134  
 135          if ($categoryId) {
 136              $query->where($db->quoteName('a.catid') . ' = :catid')
 137                  ->bind(':catid', $categoryId, ParameterType::INTEGER);
 138          }
 139  
 140          // Filter by a single user.
 141          $userId = (int) $this->getState('filter.user_id');
 142  
 143          if ($userId) {
 144              // Add the body and where filter.
 145              $query->select('a.body')
 146                  ->where($db->quoteName('a.user_id') . ' = :user_id')
 147                  ->bind(':user_id', $userId, ParameterType::INTEGER);
 148          }
 149  
 150          // Filter on the level.
 151          if ($level = $this->getState('filter.level')) {
 152              $level = (int) $level;
 153              $query->where($db->quoteName('c.level') . ' <= :level')
 154                  ->bind(':level', $level, ParameterType::INTEGER);
 155          }
 156  
 157          // Add the list ordering clause.
 158          $query->order($db->escape($this->getState('list.ordering', 'a.review_time')) . ' ' . $db->escape($this->getState('list.direction', 'DESC')));
 159  
 160          return $query;
 161      }
 162  
 163      /**
 164       * Method to get a store id based on model configuration state.
 165       *
 166       * This is necessary because the model is used by the component and
 167       * different modules that might need different sets of data or different
 168       * ordering requirements.
 169       *
 170       * @param   string  $id  A prefix for the store id.
 171       *
 172       * @return  string  A store id.
 173       *
 174       * @since   2.5
 175       */
 176      protected function getStoreId($id = '')
 177      {
 178          // Compile the store id.
 179          $id .= ':' . $this->getState('filter.search');
 180          $id .= ':' . $this->getState('filter.published');
 181          $id .= ':' . $this->getState('filter.category_id');
 182          $id .= ':' . $this->getState('filter.user_id');
 183          $id .= ':' . $this->getState('filter.level');
 184  
 185          return parent::getStoreId($id);
 186      }
 187  
 188      /**
 189       * Gets a user object if the user filter is set.
 190       *
 191       * @return  User  The User object
 192       *
 193       * @since   2.5
 194       */
 195      public function getUser()
 196      {
 197          $user = new User();
 198  
 199          // Filter by search in title
 200          $search = (int) $this->getState('filter.user_id');
 201  
 202          if ($search != 0) {
 203              $user->load((int) $search);
 204          }
 205  
 206          return $user;
 207      }
 208  
 209      /**
 210       * Method to auto-populate the model state.
 211       *
 212       * Note. Calling getState in this method will result in recursion.
 213       *
 214       * @param   string  $ordering   An optional ordering field.
 215       * @param   string  $direction  An optional direction (asc|desc).
 216       *
 217       * @return  void
 218       *
 219       * @since   1.6
 220       * @throws  \Exception
 221       */
 222      protected function populateState($ordering = 'a.review_time', $direction = 'desc')
 223      {
 224          // Adjust the context to support modal layouts.
 225          if ($layout = Factory::getApplication()->input->get('layout')) {
 226              $this->context .= '.' . $layout;
 227          }
 228  
 229          parent::populateState($ordering, $direction);
 230      }
 231  }


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