[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_privacy/src/Model/ -> ConsentsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_privacy
   6   *
   7   * @copyright   (C) 2018 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\Privacy\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\MVC\Model\ListModel;
  15  use Joomla\Database\DatabaseQuery;
  16  use Joomla\Database\Exception\ExecutionFailureException;
  17  use Joomla\Database\ParameterType;
  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   * Consents management model class.
  26   *
  27   * @since  3.9.0
  28   */
  29  class ConsentsModel extends ListModel
  30  {
  31      /**
  32       * Constructor.
  33       *
  34       * @param   array  $config  An optional associative array of configuration settings.
  35       *
  36       * @since   3.9.0
  37       */
  38      public function __construct($config = [])
  39      {
  40          if (empty($config['filter_fields'])) {
  41              $config['filter_fields'] = [
  42                  'id', 'a.id',
  43                  'user_id', 'a.user_id',
  44                  'subject', 'a.subject',
  45                  'created', 'a.created',
  46                  'username', 'u.username',
  47                  'name', 'u.name',
  48                  'state', 'a.state',
  49              ];
  50          }
  51  
  52          parent::__construct($config);
  53      }
  54  
  55      /**
  56       * Method to get a DatabaseQuery object for retrieving the data set from a database.
  57       *
  58       * @return  DatabaseQuery
  59       *
  60       * @since   3.9.0
  61       */
  62      protected function getListQuery()
  63      {
  64          // Create a new query object.
  65          $db    = $this->getDatabase();
  66          $query = $db->getQuery(true);
  67  
  68          // Select the required fields from the table.
  69          $query->select($this->getState('list.select', 'a.*'));
  70          $query->from($db->quoteName('#__privacy_consents', 'a'));
  71  
  72          // Join over the users for the username and name.
  73          $query->select($db->quoteName('u.username', 'username'))
  74              ->select($db->quoteName('u.name', 'name'));
  75          $query->join('LEFT', $db->quoteName('#__users', 'u') . ' ON u.id = a.user_id');
  76  
  77          // Filter by search in email
  78          $search = $this->getState('filter.search');
  79  
  80          if (!empty($search)) {
  81              if (stripos($search, 'id:') === 0) {
  82                  $ids = (int) substr($search, 3);
  83                  $query->where($db->quoteName('a.id') . ' = :id')
  84                      ->bind(':id', $ids, ParameterType::INTEGER);
  85              } elseif (stripos($search, 'uid:') === 0) {
  86                  $uid = (int) substr($search, 4);
  87                  $query->where($db->quoteName('a.user_id') . ' = :uid')
  88                      ->bind(':uid', $uid, ParameterType::INTEGER);
  89              } elseif (stripos($search, 'name:') === 0) {
  90                  $search = '%' . substr($search, 5) . '%';
  91                  $query->where($db->quoteName('u.name') . ' LIKE :search')
  92                      ->bind(':search', $search);
  93              } else {
  94                  $search = '%' . $search . '%';
  95                  $query->where('(' . $db->quoteName('u.username') . ' LIKE :search)')
  96                      ->bind(':search', $search);
  97              }
  98          }
  99  
 100          $state = $this->getState('filter.state');
 101  
 102          if ($state != '') {
 103              $state = (int) $state;
 104              $query->where($db->quoteName('a.state') . ' = :state')
 105                  ->bind(':state', $state, ParameterType::INTEGER);
 106          }
 107  
 108          // Handle the list ordering.
 109          $ordering  = $this->getState('list.ordering');
 110          $direction = $this->getState('list.direction');
 111  
 112          if (!empty($ordering)) {
 113              $query->order($db->escape($ordering) . ' ' . $db->escape($direction));
 114          }
 115  
 116          return $query;
 117      }
 118  
 119      /**
 120       * Method to get a store id based on model configuration state.
 121       *
 122       * This is necessary because the model is used by the component and
 123       * different modules that might need different sets of data or different
 124       * ordering requirements.
 125       *
 126       * @param   string  $id  A prefix for the store id.
 127       *
 128       * @return  string
 129       *
 130       * @since   3.9.0
 131       */
 132      protected function getStoreId($id = '')
 133      {
 134          // Compile the store id.
 135          $id .= ':' . $this->getState('filter.search');
 136  
 137          return parent::getStoreId($id);
 138      }
 139  
 140      /**
 141       * Method to auto-populate the model state.
 142       *
 143       * Note. Calling getState in this method will result in recursion.
 144       *
 145       * @param   string  $ordering   An optional ordering field.
 146       * @param   string  $direction  An optional direction (asc|desc).
 147       *
 148       * @return  void
 149       *
 150       * @since   3.9.0
 151       */
 152      protected function populateState($ordering = 'a.id', $direction = 'desc')
 153      {
 154          // Load the filter state.
 155          $this->setState(
 156              'filter.search',
 157              $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search')
 158          );
 159  
 160          $this->setState(
 161              'filter.subject',
 162              $this->getUserStateFromRequest($this->context . '.filter.subject', 'filter_subject')
 163          );
 164  
 165          $this->setState(
 166              'filter.state',
 167              $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_state')
 168          );
 169  
 170          // Load the parameters.
 171          $this->setState('params', ComponentHelper::getParams('com_privacy'));
 172  
 173          // List state information.
 174          parent::populateState($ordering, $direction);
 175      }
 176  
 177      /**
 178       * Method to invalidate specific consents.
 179       *
 180       * @param   array  $pks  The ids of the consents to invalidate.
 181       *
 182       * @return  boolean  True on success.
 183       */
 184      public function invalidate($pks)
 185      {
 186          // Sanitize the ids.
 187          $pks = (array) $pks;
 188          $pks = ArrayHelper::toInteger($pks);
 189  
 190          try {
 191              $db = $this->getDatabase();
 192              $query = $db->getQuery(true)
 193                  ->update($db->quoteName('#__privacy_consents'))
 194                  ->set($db->quoteName('state') . ' = -1')
 195                  ->whereIn($db->quoteName('id'), $pks)
 196                  ->where($db->quoteName('state') . ' = 1');
 197              $db->setQuery($query);
 198              $db->execute();
 199          } catch (ExecutionFailureException $e) {
 200              $this->setError($e->getMessage());
 201  
 202              return false;
 203          }
 204  
 205          return true;
 206      }
 207  
 208      /**
 209       * Method to invalidate a group of specific consents.
 210       *
 211       * @param   array  $subject  The subject of the consents to invalidate.
 212       *
 213       * @return  boolean  True on success.
 214       */
 215      public function invalidateAll($subject)
 216      {
 217          try {
 218              $db = $this->getDatabase();
 219              $query = $db->getQuery(true)
 220                  ->update($db->quoteName('#__privacy_consents'))
 221                  ->set($db->quoteName('state') . ' = -1')
 222                  ->where($db->quoteName('subject') . ' = :subject')
 223                  ->where($db->quoteName('state') . ' = 1')
 224                  ->bind(':subject', $subject);
 225              $db->setQuery($query);
 226              $db->execute();
 227          } catch (ExecutionFailureException $e) {
 228              $this->setError($e->getMessage());
 229  
 230              return false;
 231          }
 232  
 233          return true;
 234      }
 235  }


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