AdvancedWorkflowAdmin::columns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/**
3
 * @package advancedworkflow
4
 * @todo UI/UX needs looking at for when current user has no pending and/or submitted items, (Current implementation is bog-standard <p> text)
5
 */
6
class AdvancedWorkflowAdmin extends ModelAdmin {
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...
7
8
	private static $menu_title    = 'Workflows';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $menu_title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
	private static $menu_priority = -1;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $menu_priority is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
	private static $url_segment   = 'workflows';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $url_segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
	private static $menu_icon = "advancedworkflow/images/workflow-menu-icon.png";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $menu_icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
	
13
	/**
14
	 *
15
	 * @var array Allowable actions on this controller.
16
	 */
17
	private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
		'export',
19
		'ImportForm'
20
	);
21
	
22
	private static $url_handlers = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $url_handlers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
		'$ModelClass/export/$ID!' => 'export',
24
		'$ModelClass/$Action' => 'handleAction',
25
		'' => 'index'
26
	);
27
28
	private static $managed_models  = 'WorkflowDefinition';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $managed_models is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
30
	private static $model_importers = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $model_importers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
		'WorkflowDefinition' => 'WorkflowBulkLoader'
32
	);
33
34
	private static $dependencies = array(
0 ignored issues
show
Unused Code introduced by
The property $dependencies is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
		'workflowService'		=> '%$WorkflowService',
36
	);
37
38
	private static $fileEditActions = 'getCMSActions';
0 ignored issues
show
Unused Code introduced by
The property $fileEditActions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
39
40
	/**
41
	 * Defaults are set in {@link getEditForm()}.
42
	 * 
43
	 * @var array
44
	 */
45
	private static $fieldOverrides = array();
46
	
47
	/**
48
	 * @var WorkflowService
49
	 */
50
	public $workflowService;
51
	
52
	/**
53
	 * Initialise javascript translation files
54
	 * 
55
	 * @return void
56
	 */
57
	public function init() {
58
		parent::init();
59
		Requirements::add_i18n_javascript('advancedworkflow/javascript/lang');
60
		Requirements::javascript('advancedworkflow/javascript/WorkflowField.js');
61
		Requirements::javascript('advancedworkflow/javascript/WorkflowGridField.js');
62
		Requirements::css('advancedworkflow/css/WorkflowField.css');
63
		Requirements::css('advancedworkflow/css/WorkflowGridField.css');
64
	}	
65
66
	/*
67
	 * Shows up to x2 GridFields for Pending and Submitted items, dependent upon the current CMS user and that user's permissions
68
	 * on the objects showing in each field.
69
	 */
70
	public function getEditForm($id = null, $fields = null) {
71
		$form = parent::getEditForm($id, $fields);
72
		
73
		// Show items submitted into a workflow for current user to action
74
		$fieldName = 'PendingObjects';
75
		$pending = $this->userObjects(Member::currentUser(), $fieldName);
0 ignored issues
show
Documentation introduced by
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
		if(self::$fieldOverrides) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$fieldOverrides of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
			$displayFields = self::$fieldOverrides;
79
		} else {
80
			$displayFields = array(
81
				'Title'				=> _t('AdvancedWorkflowAdmin.Title', 'Title'),
82
				'LastEdited'		=> _t('AdvancedWorkflowAdmin.LastEdited', 'Changed'),
83
				'WorkflowTitle'		=> _t('AdvancedWorkflowAdmin.WorkflowTitle', 'Effective workflow'),
84
				'WorkflowStatus'	=> _t('AdvancedWorkflowAdmin.WorkflowStatus', 'Current action'),
85
			);
86
		}
87
88
		// Pending/Submitted items GridField Config
89
		$config = new GridFieldConfig_Base();
90
		$config->addComponent(new GridFieldEditButton());
91
		$config->addComponent(new GridFieldDetailForm());
92
		$config->getComponentByType('GridFieldPaginator')->setItemsPerPage(5);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setItemsPerPage() does only exist in the following implementations of said interface: GridFieldPaginator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
93
		$columns = $config->getComponentByType('GridFieldDataColumns');
94
		$columns->setFieldFormatting($this->setFieldFormatting($config));
95
96
		if($pending->count()) {
97
			$formFieldTop = GridField::create(
98
				$fieldName,
99
				$this->isAdminUser(Member::currentUser())?
0 ignored issues
show
Documentation introduced by
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
					_t(
101
						'AdvancedWorkflowAdmin.GridFieldTitleAssignedAll',
102
						'All pending items'
103
						):
104
					_t(
105
						'AdvancedWorkflowAdmin.GridFieldTitleAssignedYour',
106
						'Your pending items'),
107
				$pending,
108
				$config
109
			);
110
111
			$dataColumns = $formFieldTop->getConfig()->getComponentByType('GridFieldDataColumns');
112
			$dataColumns->setDisplayFields($displayFields);
113
114
			$formFieldTop->setForm($form);
115
			$form->Fields()->insertBefore($formFieldTop, 'WorkflowDefinition');
116
		}
117
118
		// Show items submitted into a workflow by current user
119
		$fieldName = 'SubmittedObjects';
120
		$submitted = $this->userObjects(Member::currentUser(), $fieldName);
0 ignored issues
show
Documentation introduced by
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
		if($submitted->count()) {
122
			$formFieldBottom = GridField::create(
123
				$fieldName,
124
				$this->isAdminUser(Member::currentUser())?
0 ignored issues
show
Documentation introduced by
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
					_t(
126
						'AdvancedWorkflowAdmin.GridFieldTitleSubmittedAll',
127
						'All submitted items'
128
						):
129
					_t(
130
						'AdvancedWorkflowAdmin.GridFieldTitleSubmittedYour',
131
						'Your submitted items'),
132
				$submitted,
133
				$config
134
			);
135
136
			$dataColumns = $formFieldBottom->getConfig()->getComponentByType('GridFieldDataColumns');
137
			$dataColumns->setDisplayFields($displayFields);
138
139
			$formFieldBottom->setForm($form);
140
			$formFieldBottom->getConfig()->removeComponentsByType('GridFieldEditButton');
141
			$formFieldBottom->getConfig()->addComponent(new GridFieldWorkflowRestrictedEditButton());
142
			$form->Fields()->insertBefore($formFieldBottom, 'WorkflowDefinition');
143
		}
144
		
145
		$grid = $form->Fields()->dataFieldByName('WorkflowDefinition');
146
		if ($grid) {
147
			$grid->getConfig()->getComponentByType('GridFieldDetailForm')->setItemEditFormCallback(function ($form) {
148
				$record = $form->getRecord();
149
				if ($record) {
150
					$record->updateAdminActions($form->Actions());
151
				}
152
			});
153
			
154
			$grid->getConfig()->getComponentByType('GridFieldDetailForm')->setItemRequestClass('WorkflowDefinitionItemRequestClass');
155
			$grid->getConfig()->addComponent(new GridFieldExportAction());
156
			$grid->getConfig()->removeComponentsByType('GridFieldExportButton');
157
		}
158
		
159
		return $form;
160
	}
161
162
	/*
163
	 * @param Member $user
164
	 * @return boolean
165
	 */
166
	public function isAdminUser(Member $user) {
167
		if(Permission::checkMember($user, 'ADMIN')) {
168
			return true;
169
		}
170
		return false;
171
	}
172
173
	/*
174
	 * By default, we implement GridField_ColumnProvider to allow users to click through to the PagesAdmin.
175
	 * We would also like a "Quick View", that allows users to quickly make a decision on a given workflow-bound content-object
176
	 */
177
	public function columns() {
178
		$fields = array(
179
			'Title' => array(
180
				'link' => function($value, $item) {
181
					$pageAdminLink = singleton('CMSPageEditController')->Link('show');
182
					return sprintf('<a href="%s/%s">%s</a>',$pageAdminLink,$item->Link,$value);
183
				}
184
			),
185
			'WorkflowStatus' => array(
186
				'text' => function($value, $item) {
187
					return $item->WorkflowCurrentAction;
188
				}
189
			)
190
		);
191
		return $fields;
192
	}
193
194
	/*
195
	 * Discreet method used by both intro gridfields to format the target object's links and clickable text
196
	 *
197
	 * @param GridFieldConfig $config
198
	 * @return array $fieldFormatting
199
	 */
200
	public function setFieldFormatting(&$config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
		$fieldFormatting = array();
202
		// Parse the column information
203
		foreach($this->columns() as $source => $info) {
204
			if(isset($info['link']) && $info['link']) {
205
				$fieldFormatting[$source] = '<a href=\"$ObjectRecordLink\">$value</a>';
206
			}
207
			if(isset($info['text']) && $info['text']) {
208
				$fieldFormatting[$source] = $info['text'];
209
			}
210
		}
211
		return $fieldFormatting;
212
	}
213
214
	/**
215
	 * Get WorkflowInstance Target objects to show for users in initial gridfield(s)
216
	 *
217
	 * @param Member $member
0 ignored issues
show
Bug introduced by
There is no parameter named $member. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
218
	 * @param string $fieldName The name of the gridfield that determines which dataset to return
219
	 * @return DataList
220
	 * @todo Add the ability to see embargo/expiry dates in report-gridfields at-a-glance if QueuedJobs module installed
221
	 */
222
	public function userObjects(Member $user, $fieldName) {
223
		$list = new ArrayList();
224
		$userWorkflowInstances = $this->getFieldDependentData($user, $fieldName);
225
		foreach($userWorkflowInstances as $instance) {
0 ignored issues
show
Bug introduced by
The expression $userWorkflowInstances of type object<DataList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
226
			if(!$instance->TargetID || !$instance->DefinitionID) {
227
				continue;
228
			}
229
			// @todo can we use $this->getDefinitionFor() to fetch the "Parent" definition of $instance? Maybe define $this->workflowParent()
230
			$effectiveWorkflow = DataObject::get_by_id('WorkflowDefinition', $instance->DefinitionID);
231
			$target = $instance->getTarget();
232
			if(!is_object($effectiveWorkflow) || !$target) {
233
				continue;
234
			}
235
			$instance->setField('WorkflowTitle',$effectiveWorkflow->getField('Title'));
236
			$instance->setField('WorkflowCurrentAction',$instance->getCurrentAction());
237
			// Note the order of property-setting here, somehow $instance->Title is overwritten by the Target Title property..
238
			$instance->setField('Title',$target->getField('Title'));
239
			$instance->setField('LastEdited',$target->getField('LastEdited'));
240
			if (method_exists($target, 'CMSEditLink')) {
241
				$instance->setField('ObjectRecordLink', Controller::join_links(Director::absoluteBaseURL(), $target->CMSEditLink()));
242
			}
243
244
			$list->push($instance);
245
		}
246
		return $list;
247
	}
248
249
	/*
250
	 * Return content-object data depending on which gridfeld is calling for it
251
	 *
252
	 * @param Member $user
253
	 * @param string $fieldName
254
	 */
255
	public function getFieldDependentData(Member $user, $fieldName) {
256
		if($fieldName == 'PendingObjects') {
257
			return $this->workflowService->userPendingItems($user);
258
		}
259
		if($fieldName == 'SubmittedObjects') {
260
			return $this->workflowService->userSubmittedItems($user);
261
		}
262
	}
263
	
264
	/**
265
	 * Spits out an exported version of the selected WorkflowDefinition for download.
266
	 * 
267
	 * @param \SS_HTTPRequest $request
268
	 * @return \SS_HTTPResponse
269
	 */
270
	public function export(SS_HTTPRequest $request) {
271
		$url = explode('/', $request->getURL());
272
		$definitionID = end($url);
273
		if($definitionID && is_numeric($definitionID)) {
274
			$exporter = new WorkflowDefinitionExporter($definitionID);
275
			$exportFilename = WorkflowDefinitionExporter::$export_filename_prefix.'-'.$definitionID.'.yml';
276
			$exportBody = $exporter->export();
277
			$fileData = array(
278
				'name' => $exportFilename,
279
				'mime' => 'text/x-yaml',
280
				'body' => $exportBody,
281
				'size' => $exporter->getExportSize($exportBody)
0 ignored issues
show
Bug introduced by
It seems like $exportBody defined by $exporter->export() on line 276 can also be of type array<integer,string>; however, WorkflowDefinitionExporter::getExportSize() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
282
			);
283
			return $exporter->sendFile($fileData);
284
		}
285
	}	
286
	
287
	/**
288
	 * Required so we can simply change the visible label of the "Import" button and lose some redundant form-fields.
289
	 * 
290
	 * @return Form
291
	 */
292
	public function ImportForm() {
293
		$form = parent::ImportForm();
294
		if(!$form) {
295
			return;
296
		}
297
		
298
		$form->unsetAllActions();
299
		$newActionList = new FieldList(array(
300
			new FormAction('import', _t('AdvancedWorkflowAdmin.IMPORT', 'Import workflow'))
301
		));
302
		$form->Fields()->fieldByName('_CsvFile')->getValidator()->setAllowedExtensions(array('yml', 'yaml'));
303
		$form->Fields()->removeByName('EmptyBeforeImport');
304
		$form->setActions($newActionList);
305
		
306
		return $form;
307
	}	
308
}
309
310
class WorkflowDefinitionItemRequestClass extends GridFieldDetailForm_ItemRequest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
311
	public function updatetemplateversion($data, Form $form, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
312
		$record = $form->getRecord();
313
		if ($record) {
314
			$record->updateFromTemplate();
315
		}
316
		return $form->loadDataFrom($form->getRecord())->forAjaxTemplate();
0 ignored issues
show
Bug introduced by
It seems like $form->getRecord() can be null; however, loadDataFrom() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
317
	}
318
}