[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_workflow/src/Table/ -> WorkflowTable.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   * Workflow table
  26   *
  27   * @since  4.0.0
  28   */
  29  class WorkflowTable 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          $this->typeAlias = '{extension}.workflow';
  48  
  49          parent::__construct('#__workflows', 'id', $db);
  50      }
  51  
  52      /**
  53       * Deletes workflow with transition and states.
  54       *
  55       * @param   int  $pk  Extension ids to delete.
  56       *
  57       * @return  boolean
  58       *
  59       * @since  4.0.0
  60       *
  61       * @throws  \Exception on ACL error
  62       */
  63      public function delete($pk = null)
  64      {
  65          $db  = $this->getDbo();
  66          $app = Factory::getApplication();
  67          $pk  = (int) $pk;
  68  
  69          // Gets the workflow information that is going to be deleted.
  70          $query = $db->getQuery(true)
  71              ->select($db->quoteName('default'))
  72              ->from($db->quoteName('#__workflows'))
  73              ->where($db->quoteName('id') . ' = :id')
  74              ->bind(':id', $pk, ParameterType::INTEGER);
  75  
  76          $isDefault = $db->setQuery($query)->loadResult();
  77  
  78          if ($isDefault) {
  79              $app->enqueueMessage(Text::_('COM_WORKFLOW_MSG_DELETE_DEFAULT'), 'error');
  80  
  81              return false;
  82          }
  83  
  84          // Delete the workflow states, then transitions from all tables.
  85          try {
  86              $query = $db->getQuery(true)
  87                  ->delete($db->quoteName('#__workflow_stages'))
  88                  ->where($db->quoteName('workflow_id') . ' = :id')
  89                  ->bind(':id', $pk, ParameterType::INTEGER);
  90  
  91              $db->setQuery($query)->execute();
  92  
  93              $query = $db->getQuery(true)
  94                  ->delete($db->quoteName('#__workflow_transitions'))
  95                  ->where($db->quoteName('workflow_id') . ' = :id')
  96                  ->bind(':id', $pk, ParameterType::INTEGER);
  97  
  98              $db->setQuery($query)->execute();
  99  
 100              return parent::delete($pk);
 101          } catch (\RuntimeException $e) {
 102              $app->enqueueMessage(Text::sprintf('COM_WORKFLOW_MSG_WORKFLOWS_DELETE_ERROR', $e->getMessage()), 'error');
 103  
 104              return false;
 105          }
 106      }
 107  
 108      /**
 109       * Overloaded check function
 110       *
 111       * @return  boolean  True on success
 112       *
 113       * @see     Table::check()
 114       * @since   4.0.0
 115       */
 116      public function check()
 117      {
 118          try {
 119              parent::check();
 120          } catch (\Exception $e) {
 121              $this->setError($e->getMessage());
 122  
 123              return false;
 124          }
 125  
 126          if (trim($this->title) === '') {
 127              $this->setError(Text::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_WORKFLOW'));
 128  
 129              return false;
 130          }
 131  
 132          if (!empty($this->default)) {
 133              if ((int) $this->published !== 1) {
 134                  $this->setError(Text::_('COM_WORKFLOW_ITEM_MUST_PUBLISHED'));
 135  
 136                  return false;
 137              }
 138          } else {
 139              $db    = $this->getDbo();
 140              $query = $db->getQuery(true);
 141  
 142              $query
 143                  ->select($db->quoteName('id'))
 144                  ->from($db->quoteName('#__workflows'))
 145                  ->where($db->quoteName('default') . ' = 1');
 146  
 147              $id = $db->setQuery($query)->loadResult();
 148  
 149              // If there is no default workflow => set the current to default to recover
 150              if (empty($id)) {
 151                  $this->default = '1';
 152              } elseif ($id === $this->id) {
 153                  // This workflow is the default, but someone has tried to disable it => not allowed
 154                  $this->setError(Text::_('COM_WORKFLOW_DISABLE_DEFAULT'));
 155  
 156                  return false;
 157              }
 158          }
 159  
 160          return true;
 161      }
 162  
 163      /**
 164       * Overloaded store function
 165       *
 166       * @param   boolean  $updateNulls  True to update fields even if they are null.
 167       *
 168       * @return  mixed  False on failure, positive integer on success.
 169       *
 170       * @see     Table::store()
 171       * @since   4.0.0
 172       */
 173      public function store($updateNulls = true)
 174      {
 175          $date = Factory::getDate();
 176          $user = Factory::getUser();
 177  
 178          $table = new WorkflowTable($this->getDbo());
 179  
 180          if ($this->id) {
 181              // Existing item
 182              $this->modified_by = $user->id;
 183              $this->modified    = $date->toSql();
 184          } else {
 185              $this->modified_by = 0;
 186          }
 187  
 188          if (!(int) $this->created) {
 189              $this->created = $date->toSql();
 190          }
 191  
 192          if (empty($this->created_by)) {
 193              $this->created_by = $user->id;
 194          }
 195  
 196          if (!(int) $this->modified) {
 197              $this->modified = $this->created;
 198          }
 199  
 200          if (empty($this->modified_by)) {
 201              $this->modified_by = $this->created_by;
 202          }
 203  
 204          if ((int) $this->default === 1) {
 205              // Verify that the default is unique for this workflow
 206              if (
 207                  $table->load(
 208                      [
 209                      'default' => '1',
 210                      'extension' => $this->extension
 211                      ]
 212                  )
 213              ) {
 214                  $table->default = 0;
 215                  $table->store();
 216              }
 217          }
 218  
 219          return parent::store($updateNulls);
 220      }
 221  
 222      /**
 223       * Method to bind an associative array or object to the Table instance.
 224       * This method only binds properties that are publicly accessible and optionally
 225       * takes an array of properties to ignore when binding.
 226       *
 227       * @param   array|object  $src     An associative array or object to bind to the Table instance.
 228       * @param   array|string  $ignore  An optional array or space separated list of properties to ignore while binding.
 229       *
 230       * @return  boolean  True on success.
 231       *
 232       * @since   4.0.0
 233       * @throws  \InvalidArgumentException
 234       */
 235      public function bind($src, $ignore = array())
 236      {
 237          // Bind the rules.
 238          if (isset($src['rules']) && \is_array($src['rules'])) {
 239              $rules = new Rules($src['rules']);
 240              $this->setRules($rules);
 241          }
 242  
 243          return parent::bind($src, $ignore);
 244      }
 245  
 246      /**
 247       * Method to compute the default name of the asset.
 248       * The default name is in the form table_name.id
 249       * where id is the value of the primary key of the table.
 250       *
 251       * @return  string
 252       *
 253       * @since  4.0.0
 254       */
 255      protected function _getAssetName()
 256      {
 257          $k = $this->_tbl_key;
 258  
 259          $parts = explode('.', $this->extension);
 260  
 261          $extension = array_shift($parts);
 262  
 263          return $extension . '.workflow.' . (int) $this->$k;
 264      }
 265  
 266      /**
 267       * Method to return the title to use for the asset table.
 268       *
 269       * @return  string
 270       *
 271       * @since  4.0.0
 272       */
 273      protected function _getAssetTitle()
 274      {
 275          return $this->title;
 276      }
 277  
 278      /**
 279       * Get the parent asset id for the record
 280       *
 281       * @param   Table    $table  A Table object for the asset parent.
 282       * @param   integer  $id     The id for the asset
 283       *
 284       * @return  integer  The id of the asset's parent
 285       *
 286       * @since  4.0.0
 287       */
 288      protected function _getAssetParentId(Table $table = null, $id = null)
 289      {
 290          $assetId = null;
 291  
 292          $parts = explode('.', $this->extension);
 293  
 294          $extension = array_shift($parts);
 295  
 296          // Build the query to get the asset id for the parent category.
 297          $query = $this->getDbo()->getQuery(true)
 298              ->select($this->getDbo()->quoteName('id'))
 299              ->from($this->getDbo()->quoteName('#__assets'))
 300              ->where($this->getDbo()->quoteName('name') . ' = :extension')
 301              ->bind(':extension', $extension);
 302  
 303          // Get the asset id from the database.
 304          $this->getDbo()->setQuery($query);
 305  
 306          if ($result = $this->getDbo()->loadResult()) {
 307              $assetId = (int) $result;
 308          }
 309  
 310          // Return the asset id.
 311          if ($assetId) {
 312              return $assetId;
 313          } else {
 314              return parent::_getAssetParentId($table, $id);
 315          }
 316      }
 317  }


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