[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_workflow/src/Table/ -> StageTable.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   */
  10  
  11  namespace Joomla\Component\Workflow\Administrator\Table;
  12  
  13  use Joomla\CMS\Access\Rules;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Table\Table;
  17  use Joomla\Database\DatabaseDriver;
  18  use Joomla\Database\ParameterType;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Stage table
  26   *
  27   * @since  4.0.0
  28   */
  29  class StageTable extends Table
  30  {
  31      /**
  32       * Indicates that columns fully support the NULL value in the database
  33       *
  34       * @var    boolean
  35       *
  36       * @since  4.0.0
  37       */
  38      protected $_supportNullValue = true;
  39  
  40      /**
  41       * @param   DatabaseDriver  $db  Database connector object
  42       *
  43       * @since  4.0.0
  44       */
  45      public function __construct(DatabaseDriver $db)
  46      {
  47          parent::__construct('#__workflow_stages', 'id', $db);
  48      }
  49  
  50      /**
  51       * Deletes workflow with transition and stages.
  52       *
  53       * @param   int  $pk  Extension ids to delete.
  54       *
  55       * @return  boolean  True on success.
  56       *
  57       * @since  4.0.0
  58       *
  59       * @throws  \UnexpectedValueException
  60       */
  61      public function delete($pk = null)
  62      {
  63          $db  = $this->getDbo();
  64          $app = Factory::getApplication();
  65          $pk  = (int) $pk;
  66  
  67          $query = $db->getQuery(true)
  68              ->select($db->quoteName('default'))
  69              ->from($db->quoteName('#__workflow_stages'))
  70              ->where($db->quoteName('id') . ' = :id')
  71              ->bind(':id', $pk, ParameterType::INTEGER);
  72  
  73          $isDefault = $db->setQuery($query)->loadResult();
  74  
  75          if ($isDefault) {
  76              $app->enqueueMessage(Text::_('COM_WORKFLOW_MSG_DELETE_IS_DEFAULT'), 'error');
  77  
  78              return false;
  79          }
  80  
  81          try {
  82              $query = $db->getQuery(true)
  83                  ->delete($db->quoteName('#__workflow_transitions'))
  84                  ->where(
  85                      [
  86                          $db->quoteName('to_stage_id') . ' = :idTo',
  87                          $db->quoteName('from_stage_id') . ' = :idFrom',
  88                      ],
  89                      'OR'
  90                  )
  91                  ->bind([':idTo', ':idFrom'], $pk, ParameterType::INTEGER);
  92  
  93              $db->setQuery($query)->execute();
  94  
  95              return parent::delete($pk);
  96          } catch (\RuntimeException $e) {
  97              $app->enqueueMessage(Text::sprintf('COM_WORKFLOW_MSG_WORKFLOWS_DELETE_ERROR', $e->getMessage()), 'error');
  98          }
  99  
 100          return false;
 101      }
 102  
 103      /**
 104       * Overloaded check function
 105       *
 106       * @return  boolean  True on success
 107       *
 108       * @see     Table::check()
 109       * @since   4.0.0
 110       */
 111      public function check()
 112      {
 113          try {
 114              parent::check();
 115          } catch (\Exception $e) {
 116              $this->setError($e->getMessage());
 117  
 118              return false;
 119          }
 120  
 121          if (trim($this->title) === '') {
 122              $this->setError(Text::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_STATE'));
 123  
 124              return false;
 125          }
 126  
 127          if (!empty($this->default)) {
 128              if ((int) $this->published !== 1) {
 129                  $this->setError(Text::_('COM_WORKFLOW_ITEM_MUST_PUBLISHED'));
 130  
 131                  return false;
 132              }
 133          } else {
 134              $db = $this->getDbo();
 135              $query = $db->getQuery(true);
 136  
 137              $query
 138                  ->select($db->quoteName('id'))
 139                  ->from($db->quoteName('#__workflow_stages'))
 140                  ->where(
 141                      [
 142                          $db->quoteName('workflow_id') . ' = :id',
 143                          $db->quoteName('default') . ' = 1',
 144                      ]
 145                  )
 146                  ->bind(':id', $this->workflow_id, ParameterType::INTEGER);
 147  
 148              $id = $db->setQuery($query)->loadResult();
 149  
 150              // If there is no default stage => set the current to default to recover
 151              if (empty($id)) {
 152                  $this->default = '1';
 153              } elseif ($id === $this->id) {
 154                  // This stage is the default, but someone has tried to disable it => not allowed
 155                  $this->setError(Text::_('COM_WORKFLOW_DISABLE_DEFAULT'));
 156  
 157                  return false;
 158              }
 159          }
 160  
 161          return true;
 162      }
 163  
 164      /**
 165       * Overloaded store function
 166       *
 167       * @param   boolean  $updateNulls  True to update fields even if they are null.
 168       *
 169       * @return  mixed  False on failure, positive integer on success.
 170       *
 171       * @see     Table::store()
 172       * @since   4.0.0
 173       */
 174      public function store($updateNulls = true)
 175      {
 176          $table = new StageTable($this->getDbo());
 177  
 178          if ($this->default == '1') {
 179              // Verify that the default is unique for this workflow
 180              if ($table->load(array('default' => '1', 'workflow_id' => (int) $this->workflow_id))) {
 181                  $table->default = 0;
 182                  $table->store();
 183              }
 184          }
 185  
 186          return parent::store($updateNulls);
 187      }
 188  
 189      /**
 190       * Method to bind an associative array or object to the Table instance.
 191       * This method only binds properties that are publicly accessible and optionally
 192       * takes an array of properties to ignore when binding.
 193       *
 194       * @param   array|object  $src     An associative array or object to bind to the Table instance.
 195       * @param   array|string  $ignore  An optional array or space separated list of properties to ignore while binding.
 196       *
 197       * @return  boolean  True on success.
 198       *
 199       * @since   4.0.0
 200       * @throws  \InvalidArgumentException
 201       */
 202      public function bind($src, $ignore = array())
 203      {
 204          // Bind the rules.
 205          if (isset($src['rules']) && \is_array($src['rules'])) {
 206              $rules = new Rules($src['rules']);
 207              $this->setRules($rules);
 208          }
 209  
 210          return parent::bind($src, $ignore);
 211      }
 212  
 213      /**
 214       * Method to compute the default name of the asset.
 215       * The default name is in the form table_name.id
 216       * where id is the value of the primary key of the table.
 217       *
 218       * @return  string
 219       *
 220       * @since  4.0.0
 221       */
 222      protected function _getAssetName()
 223      {
 224          $k = $this->_tbl_key;
 225          $workflow = new WorkflowTable($this->getDbo());
 226          $workflow->load($this->workflow_id);
 227  
 228          $parts = explode('.', $workflow->extension);
 229  
 230          $extension = array_shift($parts);
 231  
 232          return $extension . '.stage.' . (int) $this->$k;
 233      }
 234  
 235      /**
 236       * Method to return the title to use for the asset table.
 237       *
 238       * @return  string
 239       *
 240       * @since  4.0.0
 241       */
 242      protected function _getAssetTitle()
 243      {
 244          return $this->title;
 245      }
 246  
 247      /**
 248       * Get the parent asset id for the record
 249       *
 250       * @param   Table|null    $table  A Table object for the asset parent.
 251       * @param   integer|null  $id     The id for the asset
 252       *
 253       * @return  integer  The id of the asset's parent
 254       *
 255       * @since  4.0.0
 256       */
 257      protected function _getAssetParentId(Table $table = null, $id = null)
 258      {
 259          $asset = self::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo()));
 260  
 261          $workflow = new WorkflowTable($this->getDbo());
 262          $workflow->load($this->workflow_id);
 263  
 264          $parts = explode('.', $workflow->extension);
 265  
 266          $extension = array_shift($parts);
 267  
 268          $name = $extension . '.workflow.' . (int) $workflow->id;
 269  
 270          $asset->loadByName($name);
 271          $assetId = $asset->id;
 272  
 273          return !empty($assetId) ? $assetId : parent::_getAssetParentId($table, $id);
 274      }
 275  }


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