[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_workflow/src/Model/ -> WorkflowsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_workflow
   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   * @since       4.0.0
  10   */
  11  
  12  namespace Joomla\Component\Workflow\Administrator\Model;
  13  
  14  use Joomla\CMS\Factory;
  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   * Model class for workflows
  24   *
  25   * @since  4.0.0
  26   */
  27  class WorkflowsModel extends ListModel
  28  {
  29      /**
  30       * Constructor.
  31       *
  32       * @param   array  $config  An optional associative array of configuration settings.
  33       *
  34       * @see     JController
  35       * @since  4.0.0
  36       */
  37      public function __construct($config = array())
  38      {
  39          if (empty($config['filter_fields'])) {
  40              $config['filter_fields'] = array(
  41                  'id', 'w.id',
  42                  'title', 'w.title',
  43                  'published', 'w.published',
  44                  'created_by', 'w.created_by',
  45                  'created', 'w.created',
  46                  'ordering', 'w.ordering',
  47                  'modified', 'w.modified',
  48                  'description', 'w.description'
  49              );
  50          }
  51  
  52          parent::__construct($config);
  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  4.0.0
  70       */
  71      protected function populateState($ordering = 'w.ordering', $direction = 'asc')
  72      {
  73          $app = Factory::getApplication();
  74          $extension = $app->getUserStateFromRequest($this->context . '.filter.extension', 'extension', null, 'cmd');
  75  
  76          $this->setState('filter.extension', $extension);
  77          $parts = explode('.', $extension);
  78  
  79          // Extract the component name
  80          $this->setState('filter.component', $parts[0]);
  81  
  82          // Extract the optional section name
  83          $this->setState('filter.section', (count($parts) > 1) ? $parts[1] : null);
  84  
  85          parent::populateState($ordering, $direction);
  86      }
  87  
  88      /**
  89       * Method to get a table object, load it if necessary.
  90       *
  91       * @param   string  $type    The table name. Optional.
  92       * @param   string  $prefix  The class prefix. Optional.
  93       * @param   array   $config  Configuration array for model. Optional.
  94       *
  95       * @return  \Joomla\CMS\Table\Table  A Table object
  96       *
  97       * @since  4.0.0
  98       */
  99      public function getTable($type = 'Workflow', $prefix = 'Administrator', $config = array())
 100      {
 101          return parent::getTable($type, $prefix, $config);
 102      }
 103  
 104      /**
 105       * Method to get an array of data items.
 106       *
 107       * @return  mixed  An array of data items on success, false on failure.
 108       *
 109       * @since  4.0.0
 110       */
 111      public function getItems()
 112      {
 113          $items = parent::getItems();
 114  
 115          if ($items) {
 116              $this->countItems($items);
 117          }
 118  
 119          return $items;
 120      }
 121  
 122      /**
 123       * Get the filter form
 124       *
 125       * @param   array    $data      data
 126       * @param   boolean  $loadData  load current data
 127       *
 128       * @return  \Joomla\CMS\Form\Form|bool the Form object or false
 129       *
 130       * @since   4.0.0
 131       */
 132      public function getFilterForm($data = array(), $loadData = true)
 133      {
 134          $form = parent::getFilterForm($data, $loadData);
 135  
 136          if ($form) {
 137              $form->setValue('extension', null, $this->getState('filter.extension'));
 138          }
 139  
 140          return $form;
 141      }
 142  
 143      /**
 144       * Add the number of transitions and states to all workflow items
 145       *
 146       * @param   array  $items  The workflow items
 147       *
 148       * @return  mixed  An array of data items on success, false on failure.
 149       *
 150       * @since  4.0.0
 151       */
 152      protected function countItems($items)
 153      {
 154          $db = $this->getDatabase();
 155  
 156          $ids = [0];
 157  
 158          foreach ($items as $item) {
 159              $ids[] = (int) $item->id;
 160  
 161              $item->count_states = 0;
 162              $item->count_transitions = 0;
 163          }
 164  
 165          $query = $db->getQuery(true);
 166  
 167          $query->select(
 168              [
 169                  $db->quoteName('workflow_id'),
 170                  'COUNT(*) AS ' . $db->quoteName('count'),
 171              ]
 172          )
 173              ->from($db->quoteName('#__workflow_stages'))
 174              ->whereIn($db->quoteName('workflow_id'), $ids)
 175              ->where($db->quoteName('published') . ' >= 0')
 176              ->group($db->quoteName('workflow_id'));
 177  
 178          $status = $db->setQuery($query)->loadObjectList('workflow_id');
 179  
 180          $query = $db->getQuery(true);
 181  
 182          $query->select(
 183              [
 184                  $db->quoteName('workflow_id'),
 185                  'COUNT(*) AS ' . $db->quoteName('count'),
 186              ]
 187          )
 188              ->from($db->quoteName('#__workflow_transitions'))
 189              ->whereIn($db->quoteName('workflow_id'), $ids)
 190              ->where($db->quoteName('published') . ' >= 0')
 191              ->group($db->quoteName('workflow_id'));
 192  
 193          $transitions = $db->setQuery($query)->loadObjectList('workflow_id');
 194  
 195          foreach ($items as $item) {
 196              if (isset($status[$item->id])) {
 197                  $item->count_states = (int) $status[$item->id]->count;
 198              }
 199  
 200              if (isset($transitions[$item->id])) {
 201                  $item->count_transitions = (int) $transitions[$item->id]->count;
 202              }
 203          }
 204      }
 205  
 206      /**
 207       * Method to get the data that should be injected in the form.
 208       *
 209       * @return  string  The query to database.
 210       *
 211       * @since  4.0.0
 212       */
 213      public function getListQuery()
 214      {
 215          $db    = $this->getDatabase();
 216          $query = $db->getQuery(true);
 217  
 218          $query->select(
 219              [
 220                  $db->quoteName('w.id'),
 221                  $db->quoteName('w.title'),
 222                  $db->quoteName('w.created'),
 223                  $db->quoteName('w.modified'),
 224                  $db->quoteName('w.published'),
 225                  $db->quoteName('w.checked_out'),
 226                  $db->quoteName('w.checked_out_time'),
 227                  $db->quoteName('w.ordering'),
 228                  $db->quoteName('w.default'),
 229                  $db->quoteName('w.created_by'),
 230                  $db->quoteName('w.description'),
 231                  $db->quoteName('u.name'),
 232                  $db->quoteName('uc.name', 'editor'),
 233              ]
 234          )
 235              ->from($db->quoteName('#__workflows', 'w'))
 236              ->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('w.created_by'))
 237              ->join('LEFT', $db->quoteName('#__users', 'uc'), $db->quoteName('uc.id') . ' = ' . $db->quoteName('w.checked_out'));
 238  
 239          // Filter by extension
 240          if ($extension = $this->getState('filter.extension')) {
 241              $query->where($db->quoteName('extension') . ' = :extension')
 242                  ->bind(':extension', $extension);
 243          }
 244  
 245          $status = (string) $this->getState('filter.published');
 246  
 247          // Filter by status
 248          if (is_numeric($status)) {
 249              $status = (int) $status;
 250              $query->where($db->quoteName('w.published') . ' = :published')
 251                  ->bind(':published', $status, ParameterType::INTEGER);
 252          } elseif ($status === '') {
 253              $query->where($db->quoteName('w.published') . ' IN (0, 1)');
 254          }
 255  
 256          // Filter by search in title
 257          $search = $this->getState('filter.search');
 258  
 259          if (!empty($search)) {
 260              $search = '%' . str_replace(' ', '%', trim($search)) . '%';
 261              $query->where('(' . $db->quoteName('w.title') . ' LIKE :search1 OR ' . $db->quoteName('w.description') . ' LIKE :search2)')
 262                  ->bind([':search1', ':search2'], $search);
 263          }
 264  
 265          // Add the list ordering clause.
 266          $orderCol  = $this->state->get('list.ordering', 'w.ordering');
 267          $orderDirn = strtoupper($this->state->get('list.direction', 'ASC'));
 268  
 269          $query->order($db->escape($orderCol) . ' ' . ($orderDirn === 'DESC' ? 'DESC' : 'ASC'));
 270  
 271          return $query;
 272      }
 273  }


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