getColumnAttributes()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.2
cc 4
eloc 10
nc 3
nop 3
1
<?php
2
/**
3
 *
4
 * @package advancedworkflow
5
 */
6
class GridFieldWorkflowRestrictedEditButton implements GridField_ColumnProvider {
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
	/**
9
	 * Add a column
10
	 * 
11
	 * @param type $gridField
12
	 * @param array $columns 
13
	 */
14
	public function augmentColumns($gridField, &$columns) {
15
		if(!in_array('Actions', $columns))
16
			$columns[] = 'Actions';
17
	}
18
	
19
	/**
20
	 * Append a 'disabled' CSS class to GridField rows whose WorkflowInstance records are not viewable/editable
21
	 * by the current user. 
22
	 * 
23
	 * This is used to visually "grey out" records and it's leveraged in some overriding JavaScript, to maintain an ability
24
	 * to click the target object's hyperlink.
25
	 *
26
	 * @param GridField $gridField
27
	 * @param DataObject $record
28
	 * @param string $columnName
29
	 * @return array
30
	 */
31
	public function getColumnAttributes($gridField, $record, $columnName) {
32
		$defaultAtts = array('class' => 'col-buttons');
33
		if($record instanceof WorkflowInstance) {
34
			$isAdmin = Permission::check('ADMIN');
35
			$isAssigned = $record->getAssignedMembers()->find('ID', Member::currentUserID());
36
			if(!$isAdmin && !$isAssigned) {
37
				$atts['class'] = $defaultAtts['class'].' disabled';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$atts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $atts = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
38
				return $atts;
39
			}
40
			return $defaultAtts;
41
		}
42
		return $defaultAtts;
43
	}
44
	
45
	/**
46
	 * Add the title 
47
	 * 
48
	 * @param GridField $gridField
49
	 * @param string $columnName
50
	 * @return array
51
	 */
52
	public function getColumnMetadata($gridField, $columnName) {
53
		if($columnName == 'Actions') {
54
			return array('title' => '');
55
		}
56
	}
57
	
58
	/**
59
	 * Which columns are handled by this component
60
	 * 
61
	 * @param type $gridField
62
	 * @return type 
63
	 */
64
	public function getColumnsHandled($gridField) {
65
		return array('Actions');
66
	}
67
	
68
	/**
69
	 * @param GridField $gridField
70
	 * @param DataObject $record
71
	 * @param string $columnName
72
	 *
73
	 * @return string - the HTML for the column 
74
	 */
75
	public function getColumnContent($gridField, $record, $columnName) {
76
		$data = new ArrayData(array(
77
			'Link' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit')
78
		));
79
		return $data->renderWith('GridFieldEditButton');
80
	}
81
}
82