[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_actionlogs/src/Model/ -> ActionlogsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_actionlogs
   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\Actionlogs\Administrator\Model;
  12  
  13  use DateTimeZone;
  14  use Exception;
  15  use Joomla\CMS\Component\ComponentHelper;
  16  use Joomla\CMS\Date\Date;
  17  use Joomla\CMS\Factory;
  18  use Joomla\CMS\Form\Form;
  19  use Joomla\CMS\Language\Text;
  20  use Joomla\CMS\MVC\Model\ListModel;
  21  use Joomla\Database\DatabaseIterator;
  22  use Joomla\Database\DatabaseQuery;
  23  use Joomla\Database\ParameterType;
  24  use Joomla\Utilities\ArrayHelper;
  25  use RuntimeException;
  26  
  27  // phpcs:disable PSR1.Files.SideEffects
  28  \defined('_JEXEC') or die;
  29  // phpcs:enable PSR1.Files.SideEffects
  30  
  31  /**
  32   * Methods supporting a list of article records.
  33   *
  34   * @since  3.9.0
  35   */
  36  class ActionlogsModel extends ListModel
  37  {
  38      /**
  39       * Constructor.
  40       *
  41       * @param   array  $config  An optional associative array of configuration settings.
  42       *
  43       * @since   3.9.0
  44       *
  45       * @throws  Exception
  46       */
  47      public function __construct($config = array())
  48      {
  49          if (empty($config['filter_fields'])) {
  50              $config['filter_fields'] = array(
  51                  'a.id', 'id',
  52                  'a.extension', 'extension',
  53                  'a.user_id', 'user',
  54                  'a.message', 'message',
  55                  'a.log_date', 'log_date',
  56                  'a.ip_address', 'ip_address',
  57                  'dateRange',
  58              );
  59          }
  60  
  61          parent::__construct($config);
  62      }
  63  
  64      /**
  65       * Method to auto-populate the model state.
  66       *
  67       * @param   string  $ordering   An optional ordering field.
  68       * @param   string  $direction  An optional direction (asc|desc).
  69       *
  70       * @return  void
  71       *
  72       * @since   3.9.0
  73       *
  74       * @throws  Exception
  75       */
  76      protected function populateState($ordering = 'a.id', $direction = 'desc')
  77      {
  78          parent::populateState($ordering, $direction);
  79      }
  80  
  81      /**
  82       * Build an SQL query to load the list data.
  83       *
  84       * @return  DatabaseQuery
  85       *
  86       * @since   3.9.0
  87       *
  88       * @throws  Exception
  89       */
  90      protected function getListQuery()
  91      {
  92          $db    = $this->getDatabase();
  93          $query = $db->getQuery(true)
  94              ->select('a.*')
  95              ->select($db->quoteName('u.name'))
  96              ->from($db->quoteName('#__action_logs', 'a'))
  97              ->join('LEFT', $db->quoteName('#__users', 'u') . ' ON ' . $db->quoteName('a.user_id') . ' = ' . $db->quoteName('u.id'));
  98  
  99          // Get ordering
 100          $fullorderCol = $this->state->get('list.fullordering', 'a.id DESC');
 101  
 102          // Apply ordering
 103          if (!empty($fullorderCol)) {
 104              $query->order($db->escape($fullorderCol));
 105          }
 106  
 107          // Get filter by user
 108          $user = $this->getState('filter.user');
 109  
 110          // Apply filter by user
 111          if (!empty($user)) {
 112              $user = (int) $user;
 113              $query->where($db->quoteName('a.user_id') . ' = :userid')
 114                  ->bind(':userid', $user, ParameterType::INTEGER);
 115          }
 116  
 117          // Get filter by extension
 118          $extension = $this->getState('filter.extension');
 119  
 120          // Apply filter by extension
 121          if (!empty($extension)) {
 122              $extension = $extension . '%';
 123              $query->where($db->quoteName('a.extension') . ' LIKE :extension')
 124                  ->bind(':extension', $extension);
 125          }
 126  
 127          // Get filter by date range
 128          $dateRange = $this->getState('filter.dateRange');
 129  
 130          // Apply filter by date range
 131          if (!empty($dateRange)) {
 132              $date = $this->buildDateRange($dateRange);
 133  
 134              // If the chosen range is not more than a year ago
 135              if ($date['dNow'] !== false && $date['dStart'] !== false) {
 136                  $dStart = $date['dStart']->format('Y-m-d H:i:s');
 137                  $dNow   = $date['dNow']->format('Y-m-d H:i:s');
 138                  $query->where(
 139                      $db->quoteName('a.log_date') . ' BETWEEN :dstart AND :dnow'
 140                  );
 141                  $query->bind(':dstart', $dStart);
 142                  $query->bind(':dnow', $dNow);
 143              }
 144          }
 145  
 146          // Filter the items over the search string if set.
 147          $search = $this->getState('filter.search');
 148  
 149          if (!empty($search)) {
 150              if (stripos($search, 'id:') === 0) {
 151                  $ids = (int) substr($search, 3);
 152                  $query->where($db->quoteName('a.id') . ' = :id')
 153                      ->bind(':id', $ids, ParameterType::INTEGER);
 154              } elseif (stripos($search, 'item_id:') === 0) {
 155                  $ids = (int) substr($search, 8);
 156                  $query->where($db->quoteName('a.item_id') . ' = :itemid')
 157                      ->bind(':itemid', $ids, ParameterType::INTEGER);
 158              } else {
 159                  $search = '%' . $search . '%';
 160                  $query->where($db->quoteName('a.message') . ' LIKE :message')
 161                      ->bind(':message', $search);
 162              }
 163          }
 164  
 165          return $query;
 166      }
 167  
 168      /**
 169       * Construct the date range to filter on.
 170       *
 171       * @param   string  $range  The textual range to construct the filter for.
 172       *
 173       * @return  array  The date range to filter on.
 174       *
 175       * @since   3.9.0
 176       *
 177       * @throws  Exception
 178       */
 179      private function buildDateRange($range)
 180      {
 181          // Get UTC for now.
 182          $dNow   = new Date();
 183          $dStart = clone $dNow;
 184  
 185          switch ($range) {
 186              case 'past_week':
 187                  $dStart->modify('-7 day');
 188                  break;
 189  
 190              case 'past_1month':
 191                  $dStart->modify('-1 month');
 192                  break;
 193  
 194              case 'past_3month':
 195                  $dStart->modify('-3 month');
 196                  break;
 197  
 198              case 'past_6month':
 199                  $dStart->modify('-6 month');
 200                  break;
 201  
 202              case 'past_year':
 203                  $dStart->modify('-1 year');
 204                  break;
 205  
 206              case 'today':
 207                  // Ranges that need to align with local 'days' need special treatment.
 208                  $offset = Factory::getApplication()->get('offset');
 209  
 210                  // Reset the start time to be the beginning of today, local time.
 211                  $dStart = new Date('now', $offset);
 212                  $dStart->setTime(0, 0, 0);
 213  
 214                  // Now change the timezone back to UTC.
 215                  $tz = new DateTimeZone('GMT');
 216                  $dStart->setTimezone($tz);
 217                  break;
 218          }
 219  
 220          return array('dNow' => $dNow, 'dStart' => $dStart);
 221      }
 222  
 223      /**
 224       * Get all log entries for an item
 225       *
 226       * @param   string   $extension  The extension the item belongs to
 227       * @param   integer  $itemId     The item ID
 228       *
 229       * @return  array
 230       *
 231       * @since   3.9.0
 232       */
 233      public function getLogsForItem($extension, $itemId)
 234      {
 235          $itemId = (int) $itemId;
 236          $db     = $this->getDatabase();
 237          $query  = $db->getQuery(true)
 238              ->select('a.*')
 239              ->select($db->quoteName('u.name'))
 240              ->from($db->quoteName('#__action_logs', 'a'))
 241              ->join('INNER', $db->quoteName('#__users', 'u') . ' ON ' . $db->quoteName('a.user_id') . ' = ' . $db->quoteName('u.id'))
 242              ->where($db->quoteName('a.extension') . ' = :extension')
 243              ->where($db->quoteName('a.item_id') . ' = :itemid')
 244              ->bind(':extension', $extension)
 245              ->bind(':itemid', $itemId, ParameterType::INTEGER);
 246  
 247          // Get ordering
 248          $fullorderCol = $this->getState('list.fullordering', 'a.id DESC');
 249  
 250          // Apply ordering
 251          if (!empty($fullorderCol)) {
 252              $query->order($db->escape($fullorderCol));
 253          }
 254  
 255          $db->setQuery($query);
 256  
 257          return $db->loadObjectList();
 258      }
 259  
 260      /**
 261       * Get logs data into Table object
 262       *
 263       * @param   integer[]|null  $pks  An optional array of log record IDs to load
 264       *
 265       * @return  array  All logs in the table
 266       *
 267       * @since   3.9.0
 268       */
 269      public function getLogsData($pks = null)
 270      {
 271          $db    = $this->getDatabase();
 272          $query = $this->getLogDataQuery($pks);
 273  
 274          $db->setQuery($query);
 275  
 276          return $db->loadObjectList();
 277      }
 278  
 279      /**
 280       * Get logs data as a database iterator
 281       *
 282       * @param   integer[]|null  $pks  An optional array of log record IDs to load
 283       *
 284       * @return  DatabaseIterator
 285       *
 286       * @since   3.9.0
 287       */
 288      public function getLogDataAsIterator($pks = null)
 289      {
 290          $db    = $this->getDatabase();
 291          $query = $this->getLogDataQuery($pks);
 292  
 293          $db->setQuery($query);
 294  
 295          return $db->getIterator();
 296      }
 297  
 298      /**
 299       * Get the query for loading logs data
 300       *
 301       * @param   integer[]|null  $pks  An optional array of log record IDs to load
 302       *
 303       * @return  DatabaseQuery
 304       *
 305       * @since   3.9.0
 306       */
 307      private function getLogDataQuery($pks = null)
 308      {
 309          $db    = $this->getDatabase();
 310          $query = $db->getQuery(true)
 311              ->select('a.*')
 312              ->select($db->quoteName('u.name'))
 313              ->from($db->quoteName('#__action_logs', 'a'))
 314              ->join('INNER', $db->quoteName('#__users', 'u') . ' ON ' . $db->quoteName('a.user_id') . ' = ' . $db->quoteName('u.id'));
 315  
 316          if (\is_array($pks) && \count($pks) > 0) {
 317              $pks = ArrayHelper::toInteger($pks);
 318              $query->whereIn($db->quoteName('a.id'), $pks);
 319          }
 320  
 321          return $query;
 322      }
 323  
 324      /**
 325       * Delete logs
 326       *
 327       * @param   array  $pks  Primary keys of logs
 328       *
 329       * @return  boolean
 330       *
 331       * @since   3.9.0
 332       */
 333      public function delete(&$pks)
 334      {
 335          $keys  = ArrayHelper::toInteger($pks);
 336          $db    = $this->getDatabase();
 337          $query = $db->getQuery(true)
 338              ->delete($db->quoteName('#__action_logs'))
 339              ->whereIn($db->quoteName('id'), $keys);
 340          $db->setQuery($query);
 341  
 342          try {
 343              $db->execute();
 344          } catch (RuntimeException $e) {
 345              $this->setError($e->getMessage());
 346  
 347              return false;
 348          }
 349  
 350          Factory::getApplication()->triggerEvent('onAfterLogPurge', array());
 351  
 352          return true;
 353      }
 354  
 355      /**
 356       * Removes all of logs from the table.
 357       *
 358       * @return  boolean result of operation
 359       *
 360       * @since   3.9.0
 361       */
 362      public function purge()
 363      {
 364          try {
 365              $this->getDatabase()->truncateTable('#__action_logs');
 366          } catch (Exception $e) {
 367              return false;
 368          }
 369  
 370          Factory::getApplication()->triggerEvent('onAfterLogPurge', array());
 371  
 372          return true;
 373      }
 374  
 375      /**
 376       * Get the filter form
 377       *
 378       * @param   array    $data      data
 379       * @param   boolean  $loadData  load current data
 380       *
 381       * @return  Form|boolean  The Form object or false on error
 382       *
 383       * @since   3.9.0
 384       */
 385      public function getFilterForm($data = array(), $loadData = true)
 386      {
 387          $form      = parent::getFilterForm($data, $loadData);
 388          $params    = ComponentHelper::getParams('com_actionlogs');
 389          $ipLogging = (bool) $params->get('ip_logging', 0);
 390  
 391          // Add ip sort options to sort dropdown
 392          if ($form && $ipLogging) {
 393              /* @var \Joomla\CMS\Form\Field\ListField $field */
 394              $field = $form->getField('fullordering', 'list');
 395              $field->addOption(Text::_('COM_ACTIONLOGS_IP_ADDRESS_ASC'), array('value' => 'a.ip_address ASC'));
 396              $field->addOption(Text::_('COM_ACTIONLOGS_IP_ADDRESS_DESC'), array('value' => 'a.ip_address DESC'));
 397          }
 398  
 399          return $form;
 400      }
 401  }


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