[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_workflow/src/Controller/ -> WorkflowController.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\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\FormController;
  16  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  17  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  18  use Joomla\Database\ParameterType;
  19  use Joomla\Input\Input;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Workflow controller
  27   *
  28   * @since  4.0.0
  29   */
  30  class WorkflowController extends FormController
  31  {
  32      /**
  33       * The extension for which the workflows apply.
  34       *
  35       * @var    string
  36       * @since  4.0.0
  37       */
  38      protected $extension;
  39  
  40      /**
  41       * The section of the current extension
  42       *
  43       * @var    string
  44       * @since  4.0.0
  45       */
  46      protected $section;
  47  
  48      /**
  49       * Constructor.
  50       *
  51       * @param   array                $config   An optional associative array of configuration settings.
  52       * @param   MVCFactoryInterface  $factory  The factory.
  53       * @param   CMSApplication       $app      The Application for the dispatcher
  54       * @param   Input                $input    Input
  55       *
  56       * @since   4.0.0
  57       * @throws  \InvalidArgumentException when no extension is set
  58       */
  59      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  60      {
  61          parent::__construct($config, $factory, $app, $input);
  62  
  63          // If extension is not set try to get it from input or throw an exception
  64          if (empty($this->extension)) {
  65              $extension = $this->input->getCmd('extension');
  66  
  67              $parts = explode('.', $extension);
  68  
  69              $this->extension = array_shift($parts);
  70  
  71              if (!empty($parts)) {
  72                  $this->section = array_shift($parts);
  73              }
  74  
  75              if (empty($this->extension)) {
  76                  throw new \InvalidArgumentException(Text::_('COM_WORKFLOW_ERROR_EXTENSION_NOT_SET'));
  77              }
  78          }
  79      }
  80  
  81      /**
  82       * Method to check if you can add a new record.
  83       *
  84       * @param   array  $data  An array of input data.
  85       *
  86       * @return  boolean
  87       *
  88       * @since   4.0.0
  89       */
  90      protected function allowAdd($data = array())
  91      {
  92          return $this->app->getIdentity()->authorise('core.create', $this->extension);
  93      }
  94  
  95      /**
  96       * Method to check if you can edit a record.
  97       *
  98       * @param   array   $data  An array of input data.
  99       * @param   string  $key   The name of the key for the primary key.
 100       *
 101       * @return  boolean
 102       *
 103       * @since   4.0.0
 104       */
 105      protected function allowEdit($data = array(), $key = 'id')
 106      {
 107          $recordId = isset($data[$key]) ? (int) $data[$key] : 0;
 108          $user = $this->app->getIdentity();
 109  
 110          $record = $this->getModel()->getItem($recordId);
 111  
 112          if (empty($record->id)) {
 113              return false;
 114          }
 115  
 116          // Check "edit" permission on record asset (explicit or inherited)
 117          if ($user->authorise('core.edit', $this->extension . '.workflow.' . $recordId)) {
 118              return true;
 119          }
 120  
 121          // Check "edit own" permission on record asset (explicit or inherited)
 122          if ($user->authorise('core.edit.own', $this->extension . '.workflow.' . $recordId)) {
 123              return !empty($record) && $record->created_by == $user->id;
 124          }
 125  
 126          return false;
 127      }
 128  
 129      /**
 130       * Gets the URL arguments to append to an item redirect.
 131       *
 132       * @param   integer  $recordId  The primary key id for the item.
 133       * @param   string   $urlVar    The name of the URL variable for the id.
 134       *
 135       * @return  string  The arguments to append to the redirect URL.
 136       *
 137       * @since  4.0.0
 138       */
 139      protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
 140      {
 141          $append = parent::getRedirectToItemAppend($recordId);
 142          $append .= '&extension=' . $this->extension . ($this->section ? '.' . $this->section : '');
 143  
 144          return $append;
 145      }
 146  
 147      /**
 148       * Gets the URL arguments to append to a list redirect.
 149       *
 150       * @return  string  The arguments to append to the redirect URL.
 151       *
 152       * @since  4.0.0
 153       */
 154      protected function getRedirectToListAppend()
 155      {
 156          $append = parent::getRedirectToListAppend();
 157          $append .= '&extension=' . $this->extension . ($this->section ? '.' . $this->section : '');
 158  
 159          return $append;
 160      }
 161  
 162      /**
 163       * Function that allows child controller access to model data
 164       * after the data has been saved.
 165       *
 166       * @param   BaseDatabaseModel  $model      The data model object.
 167       * @param   array              $validData  The validated data.
 168       *
 169       * @return  void
 170       *
 171       * @since  4.0.0
 172       */
 173      public function postSaveHook(BaseDatabaseModel $model, $validData = array())
 174      {
 175          $task = $this->getTask();
 176  
 177          // The save2copy task needs to be handled slightly differently.
 178          if ($task === 'save2copy') {
 179              $table = $model->getTable();
 180  
 181              $key = $table->getKeyName();
 182  
 183              $recordId = (int) $this->input->getInt($key);
 184  
 185              // @todo Moves queries out of the controller.
 186              $db = $model->getDbo();
 187              $query = $db->getQuery(true);
 188  
 189              $query->select('*')
 190                  ->from($db->quoteName('#__workflow_stages'))
 191                  ->where($db->quoteName('workflow_id') . ' = :id')
 192                  ->bind(':id', $recordId, ParameterType::INTEGER);
 193  
 194              $statuses = $db->setQuery($query)->loadAssocList();
 195  
 196              $smodel = $this->getModel('Stage');
 197  
 198              $workflowID = (int) $model->getState($model->getName() . '.id');
 199  
 200              $mapping = [];
 201  
 202              foreach ($statuses as $status) {
 203                  $table = $smodel->getTable();
 204  
 205                  $oldID = $status['id'];
 206  
 207                  $status['workflow_id'] = $workflowID;
 208                  $status['id'] = 0;
 209  
 210                  unset($status['asset_id']);
 211  
 212                  $table->save($status);
 213  
 214                  $mapping[$oldID] = (int) $table->id;
 215              }
 216  
 217              $query = $db->getQuery(true)
 218                  ->select('*')
 219                  ->from($db->quoteName('#__workflow_transitions'))
 220                  ->where($db->quoteName('workflow_id') . ' = :id')
 221                  ->bind(':id', $recordId, ParameterType::INTEGER);
 222  
 223              $transitions = $db->setQuery($query)->loadAssocList();
 224  
 225              $tmodel = $this->getModel('Transition');
 226  
 227              foreach ($transitions as $transition) {
 228                  $table = $tmodel->getTable();
 229  
 230                  $transition['from_stage_id'] = $transition['from_stage_id'] != -1 ? $mapping[$transition['from_stage_id']] : -1;
 231                  $transition['to_stage_id'] = $mapping[$transition['to_stage_id']];
 232  
 233                  $transition['workflow_id'] = $workflowID;
 234                  $transition['id'] = 0;
 235  
 236                  unset($transition['asset_id']);
 237  
 238                  $table->save($transition);
 239              }
 240          }
 241      }
 242  }


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