Source of file FrontEndWorkflowController.php
Size: 6,766 Bytes - Last Modified: 2021-12-23T10:49:16+00:00
/var/www/docs.ssmods.com/process/src/src/Controllers/FrontEndWorkflowController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 | <?php namespace Symbiote\AdvancedWorkflow\Controllers; use Exception; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Forms\Form; use SilverStripe\ORM\DataObject; use Symbiote\AdvancedWorkflow\DataObjects\WorkflowTransition; use Symbiote\AdvancedWorkflow\Forms\FrontendWorkflowForm; use Symbiote\AdvancedWorkflow\Services\WorkflowService; /** * Provides a front end Form view of the defined Workflow Actions and Transitions * * @author rodney@symbiote.com.au shea@symbiote.com.au * @license BSD License (http://silverstripe.org/bsd-license/) * @package advancedworkflow */ abstract class FrontEndWorkflowController extends Controller { protected $transitionID; protected $contextObj; /** * The title to be displayed on the page * @var string */ public $Title; /** * @return string ClassName of object that Workflow is applied to */ abstract public function getContextType(); /** * @return object Context Object */ public function getContextObject() { if (!$this->contextObj) { if ($id = $this->getContextID()) { $cType = $this->getContextType(); $cObj = DataObject::get_by_id($cType, $id); if ($cObj) { $this->contextObj = $cObj->canView() ? $cObj : null; } } } return $this->contextObj; } /** * @return int ID of Context Object */ protected function getContextID() { $id = $this->contextObj ? $this->contextObj->ID : null; if (!$id) { if ($this->request->param('ID')) { $id = (int) $this->request->param('ID'); } elseif ($this->request->requestVar('ID')) { $id = (int) $this->request->requestVar('ID'); } } return $id; } /** * Specifies the Workflow Definition to be used, * ie. retrieve from SiteConfig - or wherever it's defined * * @return WorkflowDefinition */ abstract public function getWorkflowDefinition(); /** * Handle the Form Action * - FrontEndWorkflowForm contains the logic for this * * @param SS_HTTPRequest $request * @todo - is this even required??? */ public function handleAction($request, $action) { return parent::handleAction($request, $action); } /** * Create the Form containing: * - fields from the Context Object * - required fields from the Context Object * - Actions from the connected WorkflowTransitions * * @return Form */ public function Form() { $svc = singleton(WorkflowService::class); $active = $svc->getWorkflowFor($this->getContextObject()); if (!$active) { return; //throw new Exception('Workflow not found, or not specified for Context Object'); } $wfFields = $active->getFrontEndWorkflowFields(); $wfActions = $active->getFrontEndWorkflowActions(); $wfValidator = $active->getFrontEndRequiredFields(); //Get DataObject for Form (falls back to ContextObject if not defined in WorkflowAction) $wfDataObject = $active->getFrontEndDataObject(); // set any requirements spcific to this contextobject $active->setFrontendFormRequirements(); // hooks for decorators $this->extend('updateFrontEndWorkflowFields', $wfFields); $this->extend('updateFrontEndWorkflowActions', $wfActions); $this->extend('updateFrontEndRequiredFields', $wfValidator); $this->extend('updateFrontendFormRequirements'); $form = new FrontendWorkflowForm($this, 'Form/' . $this->getContextID(), $wfFields, $wfActions, $wfValidator); $form->addExtraClass("fwf"); if ($wfDataObject) { $form->loadDataFrom($wfDataObject); } return $form; } /** * @return WorkflowTransition */ public function getCurrentTransition() { $trans = null; if ($this->transitionID) { $trans = DataObject::get_by_id(WorkflowTransition::class, $this->transitionID); } return $trans; } /** * Save the Form Data to the defined Context Object * * @param array $data * @param Form $form * @param SS_HTTPRequest $request * @throws Exception */ public function doFrontEndAction(array $data, Form $form, HTTPRequest $request) { if (!$obj = $this->getContextObject()) { throw new Exception( _t( 'FrontEndWorkflowController.FRONTENDACTION_CONTEXT_EXCEPTION', 'Context Object Not Found' ) ); } if (!$this->getCurrentTransition()->canExecute($this->contextObj->getWorkflowInstance())) { throw new Exception( _t( 'FrontEndWorkflowController.FRONTENDACTION_TRANSITION_EXCEPTION', 'You do not have permission to execute this action' ) ); } //Only Save data when Transition is 'Active' if ($this->getCurrentTransition()->Type == 'Active') { //Hand off to WorkflowAction to perform Save $svc = singleton(WorkflowService::class); $active = $svc->getWorkflowFor($obj); $active->doFrontEndAction($data, $form, $request); } //run execute on WorkflowInstance instance $action = $this->contextObj->getWorkflowInstance()->currentAction(); $action->BaseAction()->execute($this->contextObj->getWorkflowInstance()); //get valid transitions $transitions = $action->getValidTransitions(); //tell instance to execute transition if it's in the permitted list if ($transitions->find('ID', $this->transitionID)) { $this->contextObj->getWorkflowInstance()->performTransition($this->getCurrentTransition()); } } /** * checks to see if there is a title set on the current workflow action * uses that or falls back to controller->Title */ public function Title() { if (!$this->Title) { if ($this->getContextObject()) { if ($workflow = $this->contextObj->getWorkflowInstance()) { $this->Title = $workflow->currentAction()->BaseAction()->PageTitle ? $workflow->currentAction()->BaseAction()->PageTitle : $workflow->currentAction()->Title; } } } return $this->Title; } } |