[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> TransitionField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Field;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\HTML\HTMLHelper;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\Database\ParameterType;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Components Category field.
  23   *
  24   * @since  4.0.0
  25   */
  26  class TransitionField extends ListField
  27  {
  28      /**
  29       * The form field type.
  30       *
  31       * @var    string
  32       * @since  4.0.0
  33       */
  34      protected $type = 'Transition';
  35  
  36      /**
  37       * The component and section separated by ".".
  38       *
  39       * @var    string
  40       * @since  4.0.0
  41       */
  42      protected $extension;
  43  
  44      /**
  45       * The workflow stage to use.
  46       *
  47       * @var   integer
  48       */
  49      protected $workflowStage;
  50  
  51      /**
  52       * Method to setup the extension
  53       *
  54       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  55       * @param   mixed              $value    The form field value to validate.
  56       * @param   string             $group    The field name group control value. This acts as as an array container for the field.
  57       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  58       *                                       full field name would end up being "bar[foo]".
  59       *
  60       * @return  boolean  True on success.
  61       *
  62       * @since   4.0.0
  63       */
  64      public function setup(\SimpleXMLElement $element, $value, $group = null)
  65      {
  66          $result = parent::setup($element, $value, $group);
  67  
  68          if ($result) {
  69              $input = Factory::getApplication()->input;
  70  
  71              if (\strlen($element['extension'])) {
  72                  $this->extension = (string) $element['extension'];
  73              } else {
  74                  $this->extension = $input->getCmd('extension');
  75              }
  76  
  77              if (\strlen($element['workflow_stage'])) {
  78                  $this->workflowStage = (int) $element['workflow_stage'];
  79              } else {
  80                  $this->workflowStage = $input->getInt('id');
  81              }
  82          }
  83  
  84          return $result;
  85      }
  86  
  87      /**
  88       * Method to get a list of options for a list input.
  89       *
  90       * @return  array  An array of HTMLHelper options.
  91       *
  92       * @since  4.0.0
  93       */
  94      protected function getOptions()
  95      {
  96          // Let's get the id for the current item, either category or content item.
  97          $jinput = Factory::getApplication()->input;
  98  
  99          // Initialise variable.
 100          $db = $this->getDatabase();
 101          $extension = $this->extension;
 102          $workflowStage = (int) $this->workflowStage;
 103  
 104          $query = $db->getQuery(true)
 105              ->select(
 106                  [
 107                      $db->quoteName('t.id', 'value'),
 108                      $db->quoteName('t.title', 'text'),
 109                  ]
 110              )
 111              ->from(
 112                  [
 113                      $db->quoteName('#__workflow_transitions', 't'),
 114                      $db->quoteName('#__workflow_stages', 's'),
 115                      $db->quoteName('#__workflow_stages', 's2'),
 116                  ]
 117              )
 118              ->whereIn($db->quoteName('t.from_stage_id'), [-1, $workflowStage])
 119              ->where(
 120                  [
 121                      $db->quoteName('t.to_stage_id') . ' = ' . $db->quoteName('s.id'),
 122                      $db->quoteName('s.workflow_id') . ' = ' . $db->quoteName('s2.workflow_id'),
 123                      $db->quoteName('s.workflow_id') . ' = ' . $db->quoteName('t.workflow_id'),
 124                      $db->quoteName('s2.id') . ' = :stage1',
 125                      $db->quoteName('t.published') . ' = 1',
 126                      $db->quoteName('s.published') . ' = 1',
 127                  ]
 128              )
 129              ->bind(':stage1', $workflowStage, ParameterType::INTEGER)
 130              ->order($db->quoteName('t.ordering'));
 131  
 132          $items = $db->setQuery($query)->loadObjectList();
 133  
 134          Factory::getLanguage()->load('com_workflow', JPATH_ADMINISTRATOR);
 135  
 136          $parts = explode('.', $extension);
 137  
 138          $component = reset($parts);
 139  
 140          if (\count($items)) {
 141              $user = Factory::getUser();
 142  
 143              $items = array_filter(
 144                  $items,
 145                  function ($item) use ($user, $component) {
 146                      return $user->authorise('core.execute.transition', $component . '.transition.' . $item->value);
 147                  }
 148              );
 149  
 150              foreach ($items as $item) {
 151                  $item->text = Text::_($item->text);
 152              }
 153          }
 154  
 155          // Get workflow stage title
 156          $query = $db->getQuery(true)
 157              ->select($db->quoteName('title'))
 158              ->from($db->quoteName('#__workflow_stages'))
 159              ->where($db->quoteName('id') . ' = :stage')
 160              ->bind(':stage', $workflowStage, ParameterType::INTEGER);
 161  
 162          $workflowName = $db->setQuery($query)->loadResult();
 163  
 164          $default = [HTMLHelper::_('select.option', '', Text::_($workflowName))];
 165  
 166          $options = array_merge(parent::getOptions(), $items);
 167  
 168          if (\count($options)) {
 169              $default[] = HTMLHelper::_('select.option', '-1', '--------', ['disable' => true]);
 170          }
 171  
 172          // Merge with defaults
 173          return array_merge($default, $options);
 174      }
 175  }


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