[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_workflow/src/Model/ -> StagesModel.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 stages
  24   *
  25   * @since  4.0.0
  26   */
  27  class StagesModel 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', 's.id',
  42                  'title', 's.title',
  43                  'ordering','s.ordering',
  44                  'published', 's.published'
  45              );
  46          }
  47  
  48          parent::__construct($config);
  49      }
  50  
  51      /**
  52       * Method to auto-populate the model state.
  53       *
  54       * This method should only be called once per instantiation and is designed
  55       * to be called on the first call to the getState() method unless the model
  56       * configuration flag to ignore the request is set.
  57       *
  58       * Note. Calling getState in this method will result in recursion.
  59       *
  60       * @param   string  $ordering   An optional ordering field.
  61       * @param   string  $direction  An optional direction (asc|desc).
  62       *
  63       * @return  void
  64       *
  65       * @since  4.0.0
  66       */
  67      protected function populateState($ordering = 's.ordering', $direction = 'ASC')
  68      {
  69          $app = Factory::getApplication();
  70  
  71          $workflowID = $app->getUserStateFromRequest($this->context . '.filter.workflow_id', 'workflow_id', 1, 'int');
  72          $extension = $app->getUserStateFromRequest($this->context . '.filter.extension', 'extension', null, 'cmd');
  73  
  74          if ($workflowID) {
  75              $table = $this->getTable('Workflow', 'Administrator');
  76  
  77              if ($table->load($workflowID)) {
  78                  $this->setState('active_workflow', $table->title);
  79              }
  80          }
  81  
  82          $this->setState('filter.workflow_id', $workflowID);
  83          $this->setState('filter.extension', $extension);
  84  
  85          parent::populateState($ordering, $direction);
  86      }
  87  
  88      /**
  89       * A protected method to get a set of ordering conditions.
  90       *
  91       * @param   object  $table  A record object.
  92       *
  93       * @return  array  An array of conditions to add to ordering queries.
  94       *
  95       * @since   4.0.0
  96       */
  97      protected function getReorderConditions($table)
  98      {
  99          return [
 100              $this->getDatabase()->quoteName('workflow_id') . ' = ' . (int) $table->workflow_id,
 101          ];
 102      }
 103  
 104      /**
 105       * Method to get a table object, load it if necessary.
 106       *
 107       * @param   string  $type    The table name. Optional.
 108       * @param   string  $prefix  The class prefix. Optional.
 109       * @param   array   $config  Configuration array for model. Optional.
 110       *
 111       * @return  \Joomla\CMS\Table\Table  A Table object
 112       *
 113       * @since  4.0.0
 114       */
 115      public function getTable($type = 'Stage', $prefix = 'Administrator', $config = array())
 116      {
 117          return parent::getTable($type, $prefix, $config);
 118      }
 119  
 120      /**
 121       * Method to get the data that should be injected in the form.
 122       *
 123       * @return  string  The query to database.
 124       *
 125       * @since  4.0.0
 126       */
 127      public function getListQuery()
 128      {
 129          $db    = $this->getDatabase();
 130          $query = $db->getQuery(true);
 131  
 132          $query
 133              ->select(
 134                  [
 135                      $db->quoteName('s.id'),
 136                      $db->quoteName('s.title'),
 137                      $db->quoteName('s.ordering'),
 138                      $db->quoteName('s.default'),
 139                      $db->quoteName('s.published'),
 140                      $db->quoteName('s.checked_out'),
 141                      $db->quoteName('s.checked_out_time'),
 142                      $db->quoteName('s.description'),
 143                      $db->quoteName('uc.name', 'editor'),
 144                  ]
 145              )
 146              ->from($db->quoteName('#__workflow_stages', 's'))
 147              ->join('LEFT', $db->quoteName('#__users', 'uc'), $db->quoteName('uc.id') . ' = ' . $db->quoteName('s.checked_out'));
 148  
 149          // Filter by extension
 150          if ($workflowID = (int) $this->getState('filter.workflow_id')) {
 151              $query->where($db->quoteName('s.workflow_id') . ' = :id')
 152                  ->bind(':id', $workflowID, ParameterType::INTEGER);
 153          }
 154  
 155          $status = (string) $this->getState('filter.published');
 156  
 157          // Filter by publish state
 158          if (is_numeric($status)) {
 159              $status = (int) $status;
 160              $query->where($db->quoteName('s.published') . ' = :status')
 161                  ->bind(':status', $status, ParameterType::INTEGER);
 162          } elseif ($status === '') {
 163              $query->where($db->quoteName('s.published') . ' IN (0, 1)');
 164          }
 165  
 166          // Filter by search in title
 167          $search = $this->getState('filter.search');
 168  
 169          if (!empty($search)) {
 170              $search = '%' . str_replace(' ', '%', trim($search)) . '%';
 171              $query->where('(' . $db->quoteName('s.title') . ' LIKE :search1 OR ' . $db->quoteName('s.description') . ' LIKE :search2)')
 172                  ->bind([':search1', ':search2'], $search);
 173          }
 174  
 175          // Add the list ordering clause.
 176          $query->order($db->escape($this->getState('list.ordering', 's.ordering')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 177  
 178          return $query;
 179      }
 180  
 181      /**
 182       * Returns a workflow object
 183       *
 184       * @return  object  The workflow
 185       *
 186       * @since  4.0.0
 187       */
 188      public function getWorkflow()
 189      {
 190          $table = $this->getTable('Workflow', 'Administrator');
 191  
 192          $workflowId = (int) $this->getState('filter.workflow_id');
 193  
 194          if ($workflowId > 0) {
 195              $table->load($workflowId);
 196          }
 197  
 198          return (object) $table->getProperties();
 199      }
 200  }


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