[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_checkin/src/Model/ -> CheckinModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_checkin
   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\Checkin\Administrator\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  15  use Joomla\CMS\MVC\Model\ListModel;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Checkin Model
  23   *
  24   * @since  1.6
  25   */
  26  class CheckinModel extends ListModel
  27  {
  28      /**
  29       * Count of the total items checked out
  30       *
  31       * @var  integer
  32       */
  33      protected $total;
  34  
  35      /**
  36       * Constructor.
  37       *
  38       * @param   array                $config   An optional associative array of configuration settings.
  39       * @param   MVCFactoryInterface  $factory  The factory.
  40       *
  41       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  42       * @since   3.2
  43       */
  44      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  45      {
  46          if (empty($config['filter_fields'])) {
  47              $config['filter_fields'] = array(
  48                  'table',
  49                  'count',
  50              );
  51          }
  52  
  53          parent::__construct($config, $factory);
  54      }
  55  
  56      /**
  57       * Method to auto-populate the model state.
  58       *
  59       * Note: Calling getState in this method will result in recursion.
  60       *
  61       * @param   string  $ordering   An optional ordering field.
  62       * @param   string  $direction  An optional direction (asc|desc).
  63       *
  64       * @return  void
  65       *
  66       * @since   1.6
  67       */
  68      protected function populateState($ordering = 'table', $direction = 'asc')
  69      {
  70          // List state information.
  71          parent::populateState($ordering, $direction);
  72      }
  73  
  74      /**
  75       * Checks in requested tables
  76       *
  77       * @param   array  $ids  An array of table names. Optional.
  78       *
  79       * @return  mixed  The database results or 0
  80       *
  81       * @since   1.6
  82       */
  83      public function checkin($ids = array())
  84      {
  85          $db = $this->getDatabase();
  86  
  87          if (!is_array($ids)) {
  88              return 0;
  89          }
  90  
  91          // This int will hold the checked item count.
  92          $results = 0;
  93  
  94          $app = Factory::getApplication();
  95  
  96          foreach ($ids as $tn) {
  97              // Make sure we get the right tables based on prefix.
  98              if (stripos($tn, $app->get('dbprefix')) !== 0) {
  99                  continue;
 100              }
 101  
 102              $fields = $db->getTableColumns($tn, false);
 103  
 104              if (!(isset($fields['checked_out']) && isset($fields['checked_out_time']))) {
 105                  continue;
 106              }
 107  
 108              $query = $db->getQuery(true)
 109                  ->update($db->quoteName($tn))
 110                  ->set($db->quoteName('checked_out') . ' = DEFAULT');
 111  
 112              if ($fields['checked_out_time']->Null === 'YES') {
 113                  $query->set($db->quoteName('checked_out_time') . ' = NULL');
 114              } else {
 115                  $nullDate = $db->getNullDate();
 116  
 117                  $query->set($db->quoteName('checked_out_time') . ' = :checkouttime')
 118                      ->bind(':checkouttime', $nullDate);
 119              }
 120  
 121              if ($fields['checked_out']->Null === 'YES') {
 122                  $query->where($db->quoteName('checked_out') . ' IS NOT NULL');
 123              } else {
 124                  $query->where($db->quoteName('checked_out') . ' > 0');
 125              }
 126  
 127              $db->setQuery($query);
 128  
 129              if ($db->execute()) {
 130                  $results = $results + $db->getAffectedRows();
 131                  $app->triggerEvent('onAfterCheckin', array($tn));
 132              }
 133          }
 134  
 135          return $results;
 136      }
 137  
 138      /**
 139       * Get total of tables
 140       *
 141       * @return  integer  Total to check-in tables
 142       *
 143       * @since   1.6
 144       */
 145      public function getTotal()
 146      {
 147          if (!isset($this->total)) {
 148              $this->getItems();
 149          }
 150  
 151          return $this->total;
 152      }
 153  
 154      /**
 155       * Get tables
 156       *
 157       * @return  array  Checked in table names as keys and checked in item count as values.
 158       *
 159       * @since   1.6
 160       */
 161      public function getItems()
 162      {
 163          if (!isset($this->items)) {
 164              $db     = $this->getDatabase();
 165              $tables = $db->getTableList();
 166              $prefix = Factory::getApplication()->get('dbprefix');
 167  
 168              // This array will hold table name as key and checked in item count as value.
 169              $results = array();
 170  
 171              foreach ($tables as $tn) {
 172                  // Make sure we get the right tables based on prefix.
 173                  if (stripos($tn, $prefix) !== 0) {
 174                      continue;
 175                  }
 176  
 177                  if ($this->getState('filter.search') && stripos($tn, $this->getState('filter.search')) === false) {
 178                      continue;
 179                  }
 180  
 181                  $fields = $db->getTableColumns($tn, false);
 182  
 183                  if (!(isset($fields['checked_out']) && isset($fields['checked_out_time']))) {
 184                      continue;
 185                  }
 186  
 187                  $query = $db->getQuery(true)
 188                      ->select('COUNT(*)')
 189                      ->from($db->quoteName($tn));
 190  
 191                  if ($fields['checked_out']->Null === 'YES') {
 192                      $query->where($db->quoteName('checked_out') . ' IS NOT NULL');
 193                  } else {
 194                      $query->where($db->quoteName('checked_out') . ' > 0');
 195                  }
 196  
 197                  $db->setQuery($query);
 198                  $count = $db->loadResult();
 199  
 200                  if ($count) {
 201                      $results[$tn] = $count;
 202                  }
 203              }
 204  
 205              $this->total = count($results);
 206  
 207              // Order items by table
 208              if ($this->getState('list.ordering') == 'table') {
 209                  if (strtolower($this->getState('list.direction')) == 'asc') {
 210                      ksort($results);
 211                  } else {
 212                      krsort($results);
 213                  }
 214              } else {
 215                  // Order items by number of items
 216                  if (strtolower($this->getState('list.direction')) == 'asc') {
 217                      asort($results);
 218                  } else {
 219                      arsort($results);
 220                  }
 221              }
 222  
 223              // Pagination
 224              $limit = (int) $this->getState('list.limit');
 225  
 226              if ($limit !== 0) {
 227                  $this->items = array_slice($results, $this->getState('list.start'), $limit);
 228              } else {
 229                  $this->items = $results;
 230              }
 231          }
 232  
 233          return $this->items;
 234      }
 235  }


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