[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
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 workflow 27 * 28 * @since 4.0.0 29 */ 30 class WorkflowModel 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 $app = Factory::getApplication(); 88 $user = $app->getIdentity(); 89 $input = $app->input; 90 $context = $this->option . '.' . $this->name; 91 $extension = $app->getUserStateFromRequest($context . '.filter.extension', 'extension', null, 'cmd'); 92 $data['extension'] = !empty($data['extension']) ? $data['extension'] : $extension; 93 94 // Make sure we use the correct extension when editing an existing workflow 95 $key = $table->getKeyName(); 96 $pk = (isset($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); 97 98 if ($pk > 0) { 99 $table->load($pk); 100 101 $data['extension'] = $table->extension; 102 } 103 104 if (isset($data['rules']) && !$user->authorise('core.admin', $data['extension'])) { 105 unset($data['rules']); 106 } 107 108 if ($input->get('task') == 'save2copy') { 109 $origTable = clone $this->getTable(); 110 111 // Alter the title for save as copy 112 if ($origTable->load(['title' => $data['title']])) { 113 list($title) = $this->generateNewTitle(0, '', $data['title']); 114 $data['title'] = $title; 115 } 116 117 // Unpublish new copy 118 $data['published'] = 0; 119 $data['default'] = 0; 120 } 121 122 $result = parent::save($data); 123 124 // Create default stage for new workflow 125 if ($result && $input->getCmd('task') !== 'save2copy' && $this->getState($this->getName() . '.new')) { 126 $workflow_id = (int) $this->getState($this->getName() . '.id'); 127 128 $table = $this->getTable('Stage'); 129 130 $table->id = 0; 131 $table->title = 'COM_WORKFLOW_BASIC_STAGE'; 132 $table->description = ''; 133 $table->workflow_id = $workflow_id; 134 $table->published = 1; 135 $table->default = 1; 136 137 $table->store(); 138 } 139 140 return $result; 141 } 142 143 /** 144 * Abstract method for getting the form from the model. 145 * 146 * @param array $data Data for the form. 147 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 148 * 149 * @return \Joomla\CMS\Form\Form|boolean A Form object on success, false on failure 150 * 151 * @since 4.0.0 152 */ 153 public function getForm($data = array(), $loadData = true) 154 { 155 // Get the form. 156 $form = $this->loadForm( 157 'com_workflow.workflow', 158 'workflow', 159 array( 160 'control' => 'jform', 161 'load_data' => $loadData 162 ) 163 ); 164 165 if (empty($form)) { 166 return false; 167 } 168 169 $id = $data['id'] ?? $form->getValue('id'); 170 171 $item = $this->getItem($id); 172 173 $canEditState = $this->canEditState((object) $item); 174 175 // Modify the form based on access controls. 176 if (!$canEditState || !empty($item->default)) { 177 if (!$canEditState) { 178 $form->setFieldAttribute('published', 'disabled', 'true'); 179 $form->setFieldAttribute('published', 'required', 'false'); 180 $form->setFieldAttribute('published', 'filter', 'unset'); 181 } 182 183 $form->setFieldAttribute('default', 'disabled', 'true'); 184 $form->setFieldAttribute('default', 'required', 'false'); 185 $form->setFieldAttribute('default', 'filter', 'unset'); 186 } 187 188 $form->setFieldAttribute('created', 'default', Factory::getDate()->format('Y-m-d H:i:s')); 189 $form->setFieldAttribute('modified', 'default', Factory::getDate()->format('Y-m-d H:i:s')); 190 191 return $form; 192 } 193 194 /** 195 * Method to get the data that should be injected in the form. 196 * 197 * @return mixed The data for the form. 198 * 199 * @since 4.0.0 200 */ 201 protected function loadFormData() 202 { 203 // Check the session for previously entered form data. 204 $data = Factory::getApplication()->getUserState( 205 'com_workflow.edit.workflow.data', 206 array() 207 ); 208 209 if (empty($data)) { 210 $data = $this->getItem(); 211 } 212 213 return $data; 214 } 215 216 /** 217 * Method to preprocess the form. 218 * 219 * @param Form $form Form object. 220 * @param mixed $data The data expected for the form. 221 * @param string $group The name of the plugin group to import (defaults to "content"). 222 * 223 * @return void 224 * 225 * @since 4.0.0 226 */ 227 protected function preprocessForm(Form $form, $data, $group = 'content') 228 { 229 $extension = Factory::getApplication()->input->get('extension'); 230 231 $parts = explode('.', $extension); 232 233 $extension = array_shift($parts); 234 235 // Set the access control rules field component value. 236 $form->setFieldAttribute('rules', 'component', $extension); 237 238 parent::preprocessForm($form, $data, $group); 239 } 240 241 /** 242 * A protected method to get a set of ordering conditions. 243 * 244 * @param object $table A record object. 245 * 246 * @return array An array of conditions to add to ordering queries. 247 * 248 * @since 4.0.0 249 */ 250 protected function getReorderConditions($table) 251 { 252 $db = $this->getDatabase(); 253 254 return [ 255 $db->quoteName('extension') . ' = ' . $db->quote($table->extension), 256 ]; 257 } 258 259 /** 260 * Method to change the default state of one item. 261 * 262 * @param array $pk A list of the primary keys to change. 263 * @param integer $value The value of the home state. 264 * 265 * @return boolean True on success. 266 * 267 * @since 4.0.0 268 */ 269 public function setDefault($pk, $value = 1) 270 { 271 $table = $this->getTable(); 272 273 if ($table->load($pk)) { 274 if ($table->published !== 1) { 275 $this->setError(Text::_('COM_WORKFLOW_ITEM_MUST_PUBLISHED')); 276 277 return false; 278 } 279 } 280 281 if (empty($table->id) || !$this->canEditState($table)) { 282 Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror'); 283 284 return false; 285 } 286 287 $date = Factory::getDate()->toSql(); 288 289 if ($value) { 290 // Unset other default item 291 if ( 292 $table->load( 293 [ 294 'default' => '1', 295 'extension' => $table->get('extension') 296 ] 297 ) 298 ) { 299 $table->default = 0; 300 $table->modified = $date; 301 $table->store(); 302 } 303 } 304 305 if ($table->load($pk)) { 306 $table->modified = $date; 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 test whether a record can be deleted. 319 * 320 * @param object $record A record object. 321 * 322 * @return boolean True if allowed to delete the record. Defaults to the permission for the component. 323 * 324 * @since 4.0.0 325 */ 326 protected function canDelete($record) 327 { 328 if (empty($record->id) || $record->published != -2) { 329 return false; 330 } 331 332 return Factory::getUser()->authorise('core.delete', $record->extension . '.workflow.' . (int) $record->id); 333 } 334 335 /** 336 * Method to test whether a record can have its state changed. 337 * 338 * @param object $record A record object. 339 * 340 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 341 * 342 * @since 4.0.0 343 */ 344 protected function canEditState($record) 345 { 346 $user = Factory::getUser(); 347 348 // Check for existing workflow. 349 if (!empty($record->id)) { 350 return $user->authorise('core.edit.state', $record->extension . '.workflow.' . (int) $record->id); 351 } 352 353 // Default to component settings if workflow isn't known. 354 return $user->authorise('core.edit.state', $record->extension); 355 } 356 357 /** 358 * Method to change the published state of one or more records. 359 * 360 * @param array &$pks A list of the primary keys to change. 361 * @param integer $value The value of the published state. 362 * 363 * @return boolean True on success. 364 * 365 * @since 4.0.0 366 */ 367 public function publish(&$pks, $value = 1) 368 { 369 $table = $this->getTable(); 370 $pks = (array) $pks; 371 372 $date = Factory::getDate()->toSql(); 373 374 // Default workflow item check. 375 foreach ($pks as $i => $pk) { 376 if ($table->load($pk) && $value != 1 && $table->default) { 377 // Prune items that you can't change. 378 Factory::getApplication()->enqueueMessage(Text::_('COM_WORKFLOW_UNPUBLISH_DEFAULT_ERROR'), 'error'); 379 unset($pks[$i]); 380 break; 381 } 382 } 383 384 // Clean the cache. 385 $this->cleanCache(); 386 387 // Ensure that previous checks don't empty the array. 388 if (empty($pks)) { 389 return true; 390 } 391 392 $table->load($pk); 393 $table->modified = $date; 394 $table->store(); 395 396 return parent::publish($pks, $value); 397 } 398 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |