FrontEndWorkflowController::getContextObject()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 8.8571
cc 5
eloc 8
nc 5
nop 0
1
<?php
2
/**
3
 * Provides a front end Form view of the defined Workflow Actions and Transitions 
4
 *
5
 * @author  [email protected] [email protected]
6
 * @license BSD License (http://silverstripe.org/bsd-license/)
7
 * @package advancedworkflow
8
 */
9
abstract class FrontEndWorkflowController extends Controller {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
	protected	$transitionID;
12
	protected 	$contextObj;
13
14
	/**
15
	 * The title to be displayed on the page
16
	 * @var string
17
	 */
18
	public $Title;
19
20
	
21
	/**
22
	 * @return string ClassName of object that Workflow is applied to
23
	 */
24
	abstract function getContextType();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
	
26
	/**
27
	 * @return object Context Object
28
	 */
29
	public function getContextObject() {
30
		if (!$this->contextObj) {
31
			if ($id = $this->getContextID()) {
32
				$cType = $this->getContextType();
33
				$cObj = DataObject::get_by_id($cType, $id);
34
				if ($cObj) {
35
					$this->contextObj = $cObj->canView() ? $cObj : null;
36
				}
37
			}
38
		}		
39
		return $this->contextObj;
40
	}
41
	
42
	/**
43
	 * @return int ID of Context Object
44
	 */
45
	protected function getContextID() {
46
		$id = $this->contextObj ? $this->contextObj->ID : null;
47
		if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
48
			if ($this->request->param('ID')) {
49
				$id = (int) $this->request->param('ID');
50
			} else if ($this->request->requestVar('ID')) {
51
				$id = (int) $this->request->requestVar('ID');
52
			}
53
		}
54
		return $id;
55
	}
56
		
57
	/**
58
	 * Specifies the Workflow Definition to be used, 
59
	 * ie. retrieve from SiteConfig - or wherever it's defined
60
	 * 
61
	 * @return WorkflowDefinition
62
	 */
63
	 abstract function getWorkflowDefinition();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
		
65
	/**
66
	 * Handle the Form Action
67
	 * - FrontEndWorkflowForm contains the logic for this
68
	 * 
69
	 * @param SS_HTTPRequest $request
70
	 * @todo - is this even required???
71
	 */
72
	public function handleAction($request, $action){
73
		return parent::handleAction($request, $action);
74
	}
75
	
76
	/**
77
	 * Create the Form containing:
78
	 * - fields from the Context Object
79
	 * - required fields from the Context Object
80
	 * - Actions from the connected WorkflowTransitions
81
	 * 
82
	 * @return Form
83
	 */
84
	public function Form(){
85
		
86
		$svc 			= singleton('WorkflowService');
87
		$active 		= $svc->getWorkflowFor($this->getContextObject());
88
89
		if (!$active){
90
			return;
91
			//throw new Exception('Workflow not found, or not specified for Context Object');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92
		}
93
		
94
		$wfFields 		= $active->getFrontEndWorkflowFields();
95
		$wfActions 		= $active->getFrontEndWorkflowActions();
96
		$wfValidator 	= $active->getFrontEndRequiredFields();
97
		
98
		//Get DataObject for Form (falls back to ContextObject if not defined in WorkflowAction)
99
		$wfDataObject	= $active->getFrontEndDataObject();
100
						
101
		// set any requirements spcific to this contextobject
102
		$active->setFrontendFormRequirements();
103
				
104
		// hooks for decorators
105
		$this->extend('updateFrontEndWorkflowFields', $wfFields);
106
		$this->extend('updateFrontEndWorkflowActions', $wfActions);
107
		$this->extend('updateFrontEndRequiredFields', $wfValidator);
108
		$this->extend('updateFrontendFormRequirements');
109
	   
110
		$form = new FrontendWorkflowForm($this, 'Form/' . $this->getContextID(), $wfFields, $wfActions, $wfValidator);
111
		
112
		$form->addExtraClass("fwf");
113
		
114
		if($wfDataObject) {
115
			$form->loadDataFrom($wfDataObject);
116
		}
117
	
118
		return $form;
119
	}
120
	
121
	/**
122
	 * @return WorkflowTransition
123
	 */
124
	public function getCurrentTransition() {
125
		$trans = null;
126
		if ($this->transitionID) {
127
			$trans = DataObject::get_by_id('WorkflowTransition',$this->transitionID);
128
		}
129
		return $trans;
130
	}
131
	
132
	/**
133
	 * Save the Form Data to the defined Context Object
134
	 * 
135
	 * @param array $data
136
	 * @param Form $form
137
	 * @param SS_HTTPRequest $request
138
	 * @throws Exception
139
	 */
140
	public function doFrontEndAction(array $data, Form $form, SS_HTTPRequest $request) {
141
		if (!$obj = $this->getContextObject()) {
142
			throw new Exception(
143
				_t(
144
					'FrontEndWorkflowController.FRONTENDACTION_CONTEXT_EXCEPTION',
145
					'Context Object Not Found'
146
					)
147
			);
148
		}
149
150
		if(!$this->getCurrentTransition()->canExecute($this->contextObj->getWorkflowInstance())){
151
			throw new Exception(
152
				_t(
153
					'FrontEndWorkflowController.FRONTENDACTION_TRANSITION_EXCEPTION',
154
					'You do not have permission to execute this action'
155
					)
156
			);
157
		}
158
		
159
		//Only Save data when Transition is 'Active'
160
		if ($this->getCurrentTransition()->Type == 'Active') {
0 ignored issues
show
Documentation introduced by
The property Type does not exist on object<WorkflowTransition>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
161
			//Hand off to WorkflowAction to perform Save
162
			$svc 			= singleton('WorkflowService');
163
			$active 		= $svc->getWorkflowFor($obj);
164
			
165
			$active->doFrontEndAction($data, $form, $request);
166
		}
167
		
168
		//run execute on WorkflowInstance instance		
169
		$action = $this->contextObj->getWorkflowInstance()->currentAction();
170
		$action->BaseAction()->execute($this->contextObj->getWorkflowInstance());
171
		
172
		//get valid transitions
173
		$transitions = $action->getValidTransitions();
174
		
175
		//tell instance to execute transition if it's in the permitted list
176
		if ($transitions->find('ID',$this->transitionID)) {
177
			$this->contextObj->getWorkflowInstance()->performTransition($this->getCurrentTransition());
178
		}
179
	}	
180
181
	/**
182
	 * checks to see if there is a title set on the current workflow action
183
	 * uses that or falls back to controller->Title
184
	 */
185
	public function Title(){
186
		if (!$this->Title) {
187
			if($this->getContextObject()){
188
				if($workflow = $this->contextObj->getWorkflowInstance()){
189
					$this->Title = $workflow->currentAction()->BaseAction()->PageTitle ? $workflow->currentAction()->BaseAction()->PageTitle : $workflow->currentAction()->Title;	
190
				}
191
			}
192
		}
193
		return $this->Title;
194
	}
195
		
196
}