Source of file FileWorkflowApplicable.php
Size: 2,658 Bytes - Last Modified: 2021-12-24T06:44:28+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/FileWorkflowApplicable.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | <?php /** * WorkflowApplicable extension specifically for File objects, which don't have the same CMS * UI structure so need to be handled a little differently. Additionally, it doesn't really * work without custom code to handle the triggering of workflow, and in general is not * ready for production use just yet. * * @author marcus@silverstripe.com.au * @license BSD License (http://silverstripe.org/bsd-license/) * @package advancedworkflow */ use SilverStripe\ORM\DataExtension; use SilverStripe\Forms\FieldList; class FileWorkflowApplicable extends WorkflowApplicable { public function updateSummaryFields(&$fields) { $fields['ID'] = 'ID'; $fields['ParentID'] = 'ParentID'; } public function updateCMSFields(FieldList $fields) { if (!$this->owner->ID) { return $fields; } parent::updateCMSFields($fields); // add the workflow fields directly. It's a requirement of workflow on file objects // that CMS admins mark the workflow step as being editable for files to be administerable $active = $this->workflowService->getWorkflowFor($this->owner); if ($active) { $current = $active->CurrentAction(); $wfFields = $active->getWorkflowFields(); // loading data in a somewhat hack way $form = new Form($this, 'DummyForm', $wfFields, new FieldList()); $form->loadDataFrom($current); $fields->findOrMakeTab( 'Root.WorkflowActions', _t('Workflow.WorkflowActionsTabTitle', 'Workflow Actions') ); $fields->addFieldsToTab('Root.WorkflowActions', $wfFields); } } public function onAfterWrite() { parent::onAfterWrite(); $workflow = $this->workflowService->getWorkflowFor($this->owner); $rawData = $this->owner->toMap(); if ($workflow && $this->owner->TransitionID) { // we want to transition, so do so if that's a valid transition to take. $action = $workflow->CurrentAction(); if (!$this->canEditWorkflow()) { return; } $allowedFields = $workflow->getWorkflowFields()->saveableFields(); unset($allowedFields['TransitionID']); $allowed = array_keys($allowedFields); foreach ($allowed as $field) { if (isset($rawData[$field])) { $action->$field = $rawData[$field]; } } $action->write(); if (isset($rawData['TransitionID']) && $rawData['TransitionID']) { // unset the transition ID so this doesn't get re-executed $this->owner->TransitionID = null; $this->workflowService->executeTransition($this->owner, $rawData['TransitionID']); } else { // otherwise, just try to execute the current workflow to see if it // can now proceed based on user input $workflow->execute(); } } } } |