[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_workflow/src/Model/ -> StageModel.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\Form\Form;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Log\Log;
  18  use Joomla\CMS\MVC\Model\AdminModel;
  19  use Joomla\String\StringHelper;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Model class for stage
  27   *
  28   * @since  4.0.0
  29   */
  30  class StageModel extends AdminModel
  31  {
  32      /**
  33       * Auto-populate the model state.
  34       *
  35       * Note. Calling getState in this method will result in recursion.
  36       *
  37       * @return  void
  38       *
  39       * @since   4.0.0
  40       */
  41      public function populateState()
  42      {
  43          parent::populateState();
  44  
  45          $app       = Factory::getApplication();
  46          $context   = $this->option . '.' . $this->name;
  47          $extension = $app->getUserStateFromRequest($context . '.filter.extension', 'extension', null, 'cmd');
  48  
  49          $this->setState('filter.extension', $extension);
  50      }
  51  
  52      /**
  53       * Method to change the title
  54       *
  55       * @param   integer  $categoryId  The id of the category.
  56       * @param   string   $alias       The alias.
  57       * @param   string   $title       The title.
  58       *
  59       * @return  array  Contains the modified title and alias.
  60       *
  61       * @since   4.0.0
  62       */
  63      protected function generateNewTitle($categoryId, $alias, $title)
  64      {
  65          // Alter the title & alias
  66          $table = $this->getTable();
  67  
  68          while ($table->load(array('title' => $title))) {
  69              $title = StringHelper::increment($title);
  70          }
  71  
  72          return array($title, $alias);
  73      }
  74  
  75      /**
  76       * Method to save the form data.
  77       *
  78       * @param   array  $data  The form data.
  79       *
  80       * @return   boolean  True on success.
  81       *
  82       * @since  4.0.0
  83       */
  84      public function save($data)
  85      {
  86          $table      = $this->getTable();
  87          $context    = $this->option . '.' . $this->name;
  88          $app        = Factory::getApplication();
  89          $user       = $app->getIdentity();
  90          $input      = $app->input;
  91          $workflowID = $app->getUserStateFromRequest($context . '.filter.workflow_id', 'workflow_id', 0, 'int');
  92  
  93          if (empty($data['workflow_id'])) {
  94              $data['workflow_id'] = $workflowID;
  95          }
  96  
  97          $workflow = $this->getTable('Workflow');
  98  
  99          $workflow->load($data['workflow_id']);
 100  
 101          $parts = explode('.', $workflow->extension);
 102  
 103          if (isset($data['rules']) && !$user->authorise('core.admin', $parts[0])) {
 104              unset($data['rules']);
 105          }
 106  
 107          // Make sure we use the correct extension when editing an existing workflow
 108          $key = $table->getKeyName();
 109          $pk  = (isset($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
 110  
 111          if ($pk > 0) {
 112              $table->load($pk);
 113  
 114              if ((int) $table->workflow_id) {
 115                  $data['workflow_id'] = (int) $table->workflow_id;
 116              }
 117          }
 118  
 119          if ($input->get('task') == 'save2copy') {
 120              $origTable = clone $this->getTable();
 121  
 122              // Alter the title for save as copy
 123              if ($origTable->load(['title' => $data['title']])) {
 124                  list($title) = $this->generateNewTitle(0, '', $data['title']);
 125                  $data['title'] = $title;
 126              }
 127  
 128              $data['published'] = 0;
 129              $data['default']   = 0;
 130          }
 131  
 132          return parent::save($data);
 133      }
 134  
 135      /**
 136       * Method to test whether a record can be deleted.
 137       *
 138       * @param   object  $record  A record object.
 139       *
 140       * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
 141       *
 142       * @since  4.0.0
 143       */
 144      protected function canDelete($record)
 145      {
 146          $table = $this->getTable('Workflow', 'Administrator');
 147  
 148          $table->load($record->workflow_id);
 149  
 150          if (empty($record->id) || $record->published != -2) {
 151              return false;
 152          }
 153  
 154          $app = Factory::getApplication();
 155          $extension = $app->getUserStateFromRequest('com_workflow.stage.filter.extension', 'extension', null, 'cmd');
 156  
 157          $parts = explode('.', $extension);
 158  
 159          $component = reset($parts);
 160  
 161          if (!Factory::getUser()->authorise('core.delete', $component . '.state.' . (int) $record->id) || $record->default) {
 162              $this->setError(Text::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
 163  
 164              return false;
 165          }
 166  
 167          return true;
 168      }
 169  
 170      /**
 171       * Method to test whether a record can have its state changed.
 172       *
 173       * @param   object  $record  A record object.
 174       *
 175       * @return  boolean  True if allowed to change the state of the record. Defaults to the permission set in the component.
 176       *
 177       * @since   4.0.0
 178       */
 179      protected function canEditState($record)
 180      {
 181          $user = Factory::getUser();
 182          $app = Factory::getApplication();
 183          $context = $this->option . '.' . $this->name;
 184          $extension = $app->getUserStateFromRequest($context . '.filter.extension', 'extension', null, 'cmd');
 185  
 186          if (!\property_exists($record, 'workflow_id')) {
 187              $workflowID          = $app->getUserStateFromRequest($context . '.filter.workflow_id', 'workflow_id', 0, 'int');
 188              $record->workflow_id = $workflowID;
 189          }
 190  
 191          // Check for existing workflow.
 192          if (!empty($record->id)) {
 193              return $user->authorise('core.edit.state', $extension . '.state.' . (int) $record->id);
 194          }
 195  
 196          // Default to component settings if workflow isn't known.
 197          return $user->authorise('core.edit.state', $extension);
 198      }
 199  
 200      /**
 201       * Abstract method for getting the form from the model.
 202       *
 203       * @param   array    $data      Data for the form.
 204       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 205       *
 206       * @return  Form|boolean A Form object on success, false on failure
 207       *
 208       * @since   4.0.0
 209       */
 210      public function getForm($data = array(), $loadData = true)
 211      {
 212          // Get the form.
 213          $form = $this->loadForm(
 214              'com_workflow.state',
 215              'stage',
 216              array(
 217                  'control' => 'jform',
 218                  'load_data' => $loadData
 219              )
 220          );
 221  
 222          if (empty($form)) {
 223              return false;
 224          }
 225  
 226          $id = $data['id'] ?? $form->getValue('id');
 227  
 228          $item = $this->getItem($id);
 229  
 230          $canEditState = $this->canEditState((object) $item);
 231  
 232          // Modify the form based on access controls.
 233          if (!$canEditState || !empty($item->default)) {
 234              if (!$canEditState) {
 235                  $form->setFieldAttribute('published', 'disabled', 'true');
 236                  $form->setFieldAttribute('published', 'required', 'false');
 237                  $form->setFieldAttribute('published', 'filter', 'unset');
 238              }
 239  
 240              $form->setFieldAttribute('default', 'disabled', 'true');
 241              $form->setFieldAttribute('default', 'required', 'false');
 242              $form->setFieldAttribute('default', 'filter', 'unset');
 243          }
 244  
 245          return $form;
 246      }
 247  
 248      /**
 249       * Method to get the data that should be injected in the form.
 250       *
 251       * @return mixed  The data for the form.
 252       *
 253       * @since  4.0.0
 254       */
 255      protected function loadFormData()
 256      {
 257          // Check the session for previously entered form data.
 258          $data = Factory::getApplication()->getUserState(
 259              'com_workflow.edit.state.data',
 260              array()
 261          );
 262  
 263          if (empty($data)) {
 264              $data = $this->getItem();
 265          }
 266  
 267          return $data;
 268      }
 269  
 270      /**
 271       * Method to change the home state of one or more items.
 272       *
 273       * @param   array    $pk     A list of the primary keys to change.
 274       * @param   integer  $value  The value of the home state.
 275       *
 276       * @return  boolean  True on success.
 277       *
 278       * @since  4.0.0
 279       */
 280      public function setDefault($pk, $value = 1)
 281      {
 282          $table = $this->getTable();
 283  
 284          if ($table->load($pk)) {
 285              if (!$table->published) {
 286                  $this->setError(Text::_('COM_WORKFLOW_ITEM_MUST_PUBLISHED'));
 287  
 288                  return false;
 289              }
 290          }
 291  
 292          if (empty($table->id) || !$this->canEditState($table)) {
 293              Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror');
 294  
 295              return false;
 296          }
 297  
 298          if ($value) {
 299              // Verify that the home page for this language is unique per client id
 300              if ($table->load(array('default' => '1', 'workflow_id' => $table->workflow_id))) {
 301                  $table->default = 0;
 302                  $table->store();
 303              }
 304          }
 305  
 306          if ($table->load($pk)) {
 307              $table->default = $value;
 308              $table->store();
 309          }
 310  
 311          // Clean the cache
 312          $this->cleanCache();
 313  
 314          return true;
 315      }
 316  
 317      /**
 318       * Method to change the published state of one or more records.
 319       *
 320       * @param   array    &$pks   A list of the primary keys to change.
 321       * @param   integer  $value  The value of the published state.
 322       *
 323       * @return  boolean  True on success.
 324       *
 325       * @since  4.0.0
 326       */
 327      public function publish(&$pks, $value = 1)
 328      {
 329          $table = $this->getTable();
 330          $pks   = (array) $pks;
 331          $app = Factory::getApplication();
 332          $extension = $app->getUserStateFromRequest('com_workflow.state.filter.extension', 'extension', null, 'cmd');
 333  
 334          // Default item existence checks.
 335          if ($value != 1) {
 336              foreach ($pks as $i => $pk) {
 337                  if ($table->load($pk) && $table->default) {
 338                      // Prune items that you can't change.
 339                      $app->enqueueMessage(Text::_('COM_WORKFLOW_MSG_DISABLE_DEFAULT'), 'error');
 340  
 341                      unset($pks[$i]);
 342                  }
 343              }
 344          }
 345  
 346          return parent::publish($pks, $value);
 347      }
 348  
 349      /**
 350       * Method to preprocess the form.
 351       *
 352       * @param   Form    $form  A Form object.
 353       * @param   mixed   $data  The data expected for the form.
 354       * @param   string  $group The name of the plugin group to import (defaults to "content").
 355       *
 356       * @return  void
 357       *
 358       * @since  4.0.0
 359       */
 360      protected function preprocessForm(Form $form, $data, $group = 'content')
 361      {
 362          $extension = Factory::getApplication()->input->get('extension');
 363  
 364          $parts = explode('.', $extension);
 365  
 366          $extension = array_shift($parts);
 367  
 368          // Set the access control rules field component value.
 369          $form->setFieldAttribute('rules', 'component', $extension);
 370  
 371          parent::preprocessForm($form, $data, $group);
 372      }
 373  }


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