[ 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\MVC\Model\AdminModel; 17 use Joomla\CMS\Plugin\PluginHelper; 18 use Joomla\Registry\Registry; 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 transition 27 * 28 * @since 4.0.0 29 */ 30 class TransitionModel 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 test whether a record can be deleted. 54 * 55 * @param object $record A record object. 56 * 57 * @return boolean True if allowed to delete the record. Defaults to the permission for the component. 58 * 59 * @since 4.0.0 60 */ 61 protected function canDelete($record) 62 { 63 if (empty($record->id) || $record->published != -2) { 64 return false; 65 } 66 67 $app = Factory::getApplication(); 68 $extension = $app->getUserStateFromRequest('com_workflow.transition.filter.extension', 'extension', null, 'cmd'); 69 70 return Factory::getUser()->authorise('core.delete', $extension . '.transition.' . (int) $record->id); 71 } 72 73 /** 74 * Method to test whether a record can have its state changed. 75 * 76 * @param object $record A record object. 77 * 78 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 79 * 80 * @since 4.0.0 81 */ 82 protected function canEditState($record) 83 { 84 $user = Factory::getUser(); 85 $app = Factory::getApplication(); 86 $context = $this->option . '.' . $this->name; 87 $extension = $app->getUserStateFromRequest($context . '.filter.extension', 'extension', null, 'cmd'); 88 89 if (!\property_exists($record, 'workflow_id')) { 90 $workflowID = $app->getUserStateFromRequest($context . '.filter.workflow_id', 'workflow_id', 0, 'int'); 91 $record->workflow_id = $workflowID; 92 } 93 94 // Check for existing workflow. 95 if (!empty($record->id)) { 96 return $user->authorise('core.edit.state', $extension . '.transition.' . (int) $record->id); 97 } 98 99 // Default to component settings if workflow isn't known. 100 return $user->authorise('core.edit.state', $extension); 101 } 102 103 /** 104 * Method to get a single record. 105 * 106 * @param integer $pk The id of the primary key. 107 * 108 * @return \Joomla\CMS\Object\CMSObject|boolean Object on success, false on failure. 109 * 110 * @since 4.0.0 111 */ 112 public function getItem($pk = null) 113 { 114 $item = parent::getItem($pk); 115 116 if (property_exists($item, 'options')) { 117 $registry = new Registry($item->options); 118 $item->options = $registry->toArray(); 119 } 120 121 return $item; 122 } 123 124 /** 125 * Method to save the form data. 126 * 127 * @param array $data The form data. 128 * 129 * @return boolean True on success. 130 * 131 * @since 4.0.0 132 */ 133 public function save($data) 134 { 135 $table = $this->getTable(); 136 $context = $this->option . '.' . $this->name; 137 $app = Factory::getApplication(); 138 $user = $app->getIdentity(); 139 $input = $app->input; 140 141 $workflowID = $app->getUserStateFromRequest($context . '.filter.workflow_id', 'workflow_id', 0, 'int'); 142 143 if (empty($data['workflow_id'])) { 144 $data['workflow_id'] = $workflowID; 145 } 146 147 $workflow = $this->getTable('Workflow'); 148 149 $workflow->load($data['workflow_id']); 150 151 $parts = explode('.', $workflow->extension); 152 153 if (isset($data['rules']) && !$user->authorise('core.admin', $parts[0])) { 154 unset($data['rules']); 155 } 156 157 // Make sure we use the correct workflow_id when editing an existing transition 158 $key = $table->getKeyName(); 159 $pk = (isset($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); 160 161 if ($pk > 0) { 162 $table->load($pk); 163 164 if ((int) $table->workflow_id) { 165 $data['workflow_id'] = (int) $table->workflow_id; 166 } 167 } 168 169 if ($input->get('task') == 'save2copy') { 170 $origTable = clone $this->getTable(); 171 172 // Alter the title for save as copy 173 if ($origTable->load(['title' => $data['title']])) { 174 list($title) = $this->generateNewTitle(0, '', $data['title']); 175 $data['title'] = $title; 176 } 177 178 $data['published'] = 0; 179 } 180 181 return parent::save($data); 182 } 183 184 /** 185 * Method to change the title 186 * 187 * @param integer $categoryId The id of the category. 188 * @param string $alias The alias. 189 * @param string $title The title. 190 * 191 * @return array Contains the modified title and alias. 192 * 193 * @since 4.0.0 194 */ 195 protected function generateNewTitle($categoryId, $alias, $title) 196 { 197 // Alter the title & alias 198 $table = $this->getTable(); 199 200 while ($table->load(array('title' => $title))) { 201 $title = StringHelper::increment($title); 202 } 203 204 return array($title, $alias); 205 } 206 207 /** 208 * Abstract method for getting the form from the model. 209 * 210 * @param array $data Data for the form. 211 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 212 * 213 * @return \Joomla\CMS\Form\Form|boolean A Form object on success, false on failure 214 * 215 * @since 4.0.0 216 */ 217 public function getForm($data = array(), $loadData = true) 218 { 219 // Get the form. 220 $form = $this->loadForm( 221 'com_workflow.transition', 222 'transition', 223 array( 224 'control' => 'jform', 225 'load_data' => $loadData 226 ) 227 ); 228 229 if (empty($form)) { 230 return false; 231 } 232 233 $id = $data['id'] ?? $form->getValue('id'); 234 235 $item = $this->getItem($id); 236 237 $canEditState = $this->canEditState((object) $item); 238 239 // Modify the form based on access controls. 240 if (!$canEditState) { 241 $form->setFieldAttribute('published', 'disabled', 'true'); 242 $form->setFieldAttribute('published', 'required', 'false'); 243 $form->setFieldAttribute('published', 'filter', 'unset'); 244 } 245 246 if (!empty($item->workflow_id)) { 247 $data['workflow_id'] = (int) $item->workflow_id; 248 } 249 250 if (empty($data['workflow_id'])) { 251 $context = $this->option . '.' . $this->name; 252 253 $data['workflow_id'] = (int) Factory::getApplication()->getUserStateFromRequest( 254 $context . '.filter.workflow_id', 255 'workflow_id', 256 0, 257 'int' 258 ); 259 } 260 261 $where = $this->getDatabase()->quoteName('workflow_id') . ' = ' . (int) $data['workflow_id']; 262 $where .= ' AND ' . $this->getDatabase()->quoteName('published') . ' = 1'; 263 264 $form->setFieldAttribute('from_stage_id', 'sql_where', $where); 265 $form->setFieldAttribute('to_stage_id', 'sql_where', $where); 266 267 return $form; 268 } 269 270 /** 271 * Method to get the data that should be injected in the form. 272 * 273 * @return mixed The data for the form. 274 * 275 * @since 4.0.0 276 */ 277 protected function loadFormData() 278 { 279 // Check the session for previously entered form data. 280 $data = Factory::getApplication()->getUserState( 281 'com_workflow.edit.transition.data', 282 array() 283 ); 284 285 if (empty($data)) { 286 $data = $this->getItem(); 287 } 288 289 return $data; 290 } 291 292 public function getWorkflow() 293 { 294 $app = Factory::getApplication(); 295 296 $context = $this->option . '.' . $this->name; 297 298 $workflow_id = (int) $app->getUserStateFromRequest($context . '.filter.workflow_id', 'workflow_id', 0, 'int'); 299 300 $workflow = $this->getTable('Workflow'); 301 302 $workflow->load($workflow_id); 303 304 return (object) $workflow->getProperties(); 305 } 306 307 /** 308 * Trigger the form preparation for the workflow group 309 * 310 * @param Form $form A Form object. 311 * @param mixed $data The data expected for the form. 312 * @param string $group The name of the plugin group to import (defaults to "content"). 313 * 314 * @return void 315 * 316 * @see FormField 317 * @since 4.0.0 318 * @throws \Exception if there is an error in the form event. 319 */ 320 protected function preprocessForm(Form $form, $data, $group = 'content') 321 { 322 $extension = Factory::getApplication()->input->get('extension'); 323 324 $parts = explode('.', $extension); 325 326 $extension = array_shift($parts); 327 328 // Set the access control rules field component value. 329 $form->setFieldAttribute('rules', 'component', $extension); 330 331 // Import the appropriate plugin group. 332 PluginHelper::importPlugin('workflow'); 333 334 parent::preprocessForm($form, $data, $group); 335 } 336 }
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 |