1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Forms\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Forms\GridField\GridField_ColumnProvider; |
7
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
8
|
|
|
use SilverStripe\Security\Permission; |
9
|
|
|
use SilverStripe\Security\Security; |
10
|
|
|
use SilverStripe\View\ArrayData; |
11
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @package advancedworkflow |
16
|
|
|
*/ |
17
|
|
|
class GridFieldWorkflowRestrictedEditButton implements GridField_ColumnProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Add a column |
21
|
|
|
* |
22
|
|
|
* @param type $gridField |
23
|
|
|
* @param array $columns |
24
|
|
|
*/ |
25
|
|
|
public function augmentColumns($gridField, &$columns) |
26
|
|
|
{ |
27
|
|
|
if (!in_array('Actions', $columns)) { |
28
|
|
|
$columns[] = 'Actions'; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Append a 'disabled' CSS class to GridField rows whose WorkflowInstance records are not viewable/editable |
34
|
|
|
* by the current user. |
35
|
|
|
* |
36
|
|
|
* This is used to visually "grey out" records and it's leveraged in some overriding JavaScript, to maintain |
37
|
|
|
* an ability to click the target object's hyperlink. |
38
|
|
|
* |
39
|
|
|
* @param GridField $gridField |
40
|
|
|
* @param DataObject $record |
41
|
|
|
* @param string $columnName |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function getColumnAttributes($gridField, $record, $columnName) |
45
|
|
|
{ |
46
|
|
|
$defaultAtts = array('class' => 'col-buttons'); |
47
|
|
|
if ($record instanceof WorkflowInstance) { |
48
|
|
|
$isAdmin = Permission::check('ADMIN'); |
49
|
|
|
$isAssigned = $record->getAssignedMembers()->find('ID', Security::getCurrentUser()->ID); |
50
|
|
|
if (!$isAdmin && !$isAssigned) { |
51
|
|
|
$atts['class'] = $defaultAtts['class'].' disabled'; |
|
|
|
|
52
|
|
|
return $atts; |
53
|
|
|
} |
54
|
|
|
return $defaultAtts; |
55
|
|
|
} |
56
|
|
|
return $defaultAtts; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add the title |
61
|
|
|
* |
62
|
|
|
* @param GridField $gridField |
63
|
|
|
* @param string $columnName |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getColumnMetadata($gridField, $columnName) |
67
|
|
|
{ |
68
|
|
|
if ($columnName == 'Actions') { |
69
|
|
|
return array('title' => ''); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Which columns are handled by this component |
75
|
|
|
* |
76
|
|
|
* @param type $gridField |
77
|
|
|
* @return type |
78
|
|
|
*/ |
79
|
|
|
public function getColumnsHandled($gridField) |
80
|
|
|
{ |
81
|
|
|
return array('Actions'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param GridField $gridField |
86
|
|
|
* @param DataObject $record |
87
|
|
|
* @param string $columnName |
88
|
|
|
* |
89
|
|
|
* @return string - the HTML for the column |
90
|
|
|
*/ |
91
|
|
|
public function getColumnContent($gridField, $record, $columnName) |
92
|
|
|
{ |
93
|
|
|
$data = new ArrayData(array( |
94
|
|
|
'Link' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit') |
95
|
|
|
)); |
96
|
|
|
return $data->renderWith(GridFieldEditButton::class); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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:
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 thebar
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.