Source of file AdvancedWorkflowExtension.php
Size: 4,621 Bytes - Last Modified: 2021-12-24T06:44:28+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/AdvancedWorkflowExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 | <?php /** * Handles interactions triggered by users in the backend of the CMS. Replicate this * type of functionality wherever you need UI interaction with workflow. * * @author marcus@silverstripe.com.au * @license BSD License (http://silverstripe.org/bsd-license/) * @package advancedworkflow */ use SilverStripe\Admin\LeftAndMainExtension; use SilverStripe\View\Requirements; class AdvancedWorkflowExtension extends LeftAndMainExtension { private static $allowed_actions = array( 'updateworkflow', 'startworkflow' ); public function startworkflow($data, $form, $request) { $item = $form->getRecord(); $workflowID = isset($data['TriggeredWorkflowID']) ? intval($data['TriggeredWorkflowID']) : 0; if (!$item || !$item->canEdit()) { return; } // Save a draft, if the user forgets to do so $this->saveAsDraftWithAction($form, $item); $svc = singleton('WorkflowService'); $svc->startWorkflow($item, $workflowID); return $this->returnResponse($form); } /** * Need to update the edit form AFTER it's been transformed to read only so that the workflow stuff is still * allowed to be added with 'write' permissions * * @param Form $form */ public function updateEditForm(SilverStripe\Forms\Form $form) { Requirements::javascript(ADVANCED_WORKFLOW_DIR . '/javascript/advanced-workflow-cms.js'); $svc = singleton('WorkflowService'); $p = $form->getRecord(); $active = $svc->getWorkflowFor($p); if ($active) { $fields = $form->Fields(); $current = $active->CurrentAction(); $wfFields = $active->getWorkflowFields(); $allowed = array_keys($wfFields->saveableFields()); $data = array(); foreach ($allowed as $fieldName) { $data[$fieldName] = $current->$fieldName; } $fields->findOrMakeTab( 'Root.WorkflowActions', _t('Workflow.WorkflowActionsTabTitle', 'Workflow Actions') ); $fields->addFieldsToTab('Root.WorkflowActions', $wfFields); $form->loadDataFrom($data); if (!$p->canEditWorkflow()) { $form->makeReadonly(); } $this->owner->extend('updateWorkflowEditForm', $form); } } public function updateItemEditForm($form) { $record = $form->getRecord(); if ($record && $record->hasExtension('WorkflowApplicable')) { $actions = $form->Actions(); $record->extend('updateCMSActions', $actions); $this->updateEditForm($form); } } /** * Update a workflow based on user input. * * @todo refactor with WorkflowInstance::updateWorkflow * * @param array $data * @param Form $form * @param SS_HTTPRequest $request * @return String */ public function updateworkflow($data, Form $form, $request) { $svc = singleton('WorkflowService'); $p = $form->getRecord(); $workflow = $svc->getWorkflowFor($p); if (!$workflow) { return; } $action = $workflow->CurrentAction(); if (!$p || !$p->canEditWorkflow()) { return; } $allowedFields = $workflow->getWorkflowFields()->saveableFields(); unset($allowedFields['TransitionID']); $allowed = array_keys($allowedFields); if (count($allowed)) { $form->saveInto($action, $allowed); $action->write(); } if (isset($data['TransitionID']) && $data['TransitionID']) { $svc->executeTransition($p, $data['TransitionID']); } else { // otherwise, just try to execute the current workflow to see if it // can now proceed based on user input $workflow->execute(); } return $this->returnResponse($form); } protected function returnResponse($form) { if ($this->owner instanceof GridFieldDetailForm_ItemRequest) { $record = $form->getRecord(); if ($record && $record->exists()) { return $this->owner->edit($this->owner->getRequest()); } } $negotiator = method_exists($this->owner, 'getResponseNegotiator') ? $this->owner->getResponseNegotiator() : Controller::curr()->getResponseNegotiator(); return $negotiator->respond($this->owner->getRequest()); } /** * Ocassionally users forget to apply their changes via the standard CMS "Save Draft" button, * and select the action button instead - losing their changes. * Calling this from a controller method saves a draft automatically for the user, whenever a workflow action is run. * See: #72 and #77 * * @param \Form $form * @param \DataObject $item * @return void */ protected function saveAsDraftWithAction(Form $form, DataObject $item) { $form->saveInto($item); $item->write(); } } |