[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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


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