These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Symbiote\AdvancedWorkflow\Actions; |
||
4 | |||
5 | use SilverStripe\Forms\CheckboxField; |
||
6 | use SilverStripe\Forms\CheckboxSetField; |
||
7 | use SilverStripe\Forms\HeaderField; |
||
8 | use SilverStripe\Forms\TreeMultiselectField; |
||
9 | use SilverStripe\ORM\ArrayList; |
||
10 | use SilverStripe\ORM\DB; |
||
11 | use SilverStripe\Security\Group; |
||
12 | use SilverStripe\Security\Member; |
||
13 | use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction; |
||
14 | use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance; |
||
15 | |||
16 | /** |
||
17 | * A workflow action that allows additional users or groups to be assigned to |
||
18 | * the workflow part-way through the workflow path. |
||
19 | * |
||
20 | * @license BSD License (http://silverstripe.org/bsd-license/) |
||
21 | * @package advancedworkflow |
||
22 | * @subpackage actions |
||
23 | */ |
||
24 | class AssignUsersToWorkflowAction extends WorkflowAction |
||
25 | { |
||
26 | private static $db = array( |
||
27 | 'AssignInitiator' => 'Boolean', |
||
28 | ); |
||
29 | |||
30 | private static $many_many = array( |
||
31 | 'Users' => Member::class, |
||
32 | 'Groups' => Group::class, |
||
33 | ); |
||
34 | |||
35 | private static $icon = 'symbiote/silverstripe-advancedworkflow:images/assign.png'; |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
Loading history...
|
|||
36 | |||
37 | private static $table_name = 'AssignUsersToWorkflowAction'; |
||
38 | |||
39 | public function execute(WorkflowInstance $workflow) |
||
40 | { |
||
41 | $workflow->Users()->removeAll(); |
||
42 | //Due to http://open.silverstripe.org/ticket/8258, there are errors occuring if Group has been extended |
||
43 | //We use a direct delete query here before ticket 8258 fixed |
||
44 | //$workflow->Groups()->removeAll(); |
||
45 | $workflowID = $workflow->ID; |
||
46 | $query = <<<SQL |
||
47 | DELETE FROM "WorkflowInstance_Groups" WHERE ("WorkflowInstance_Groups"."WorkflowInstanceID" = '$workflowID'); |
||
48 | SQL; |
||
49 | DB::query($query); |
||
50 | $workflow->Users()->addMany($this->Users()); |
||
51 | $workflow->Groups()->addMany($this->Groups()); |
||
52 | if ($this->AssignInitiator) { |
||
53 | $workflow->Users()->add($workflow->Initiator()); |
||
54 | } |
||
55 | return true; |
||
56 | } |
||
57 | |||
58 | public function getCMSFields() |
||
59 | { |
||
60 | $fields = parent::getCMSFields(); |
||
61 | |||
62 | $cmsUsers = Member::mapInCMSGroups(); |
||
63 | |||
64 | $fields->addFieldsToTab('Root.Main', array( |
||
65 | new HeaderField('AssignUsers', $this->fieldLabel('AssignUsers')), |
||
66 | new CheckboxField('AssignInitiator', $this->fieldLabel('AssignInitiator')), |
||
67 | $users = CheckboxSetField::create('Users', $this->fieldLabel('Users'), $cmsUsers), |
||
68 | new TreeMultiselectField('Groups', $this->fieldLabel('Groups'), Group::class) |
||
69 | )); |
||
70 | |||
71 | // limit to the users which actually can access the CMS |
||
72 | $users->setSource(Member::mapInCMSGroups()); |
||
73 | |||
74 | return $fields; |
||
75 | } |
||
76 | |||
77 | public function fieldLabels($relations = true) |
||
78 | { |
||
79 | return array_merge(parent::fieldLabels($relations), array( |
||
80 | 'AssignUsers' => _t('AssignUsersToWorkflowAction.ASSIGNUSERS', 'Assign Users'), |
||
81 | 'Users' => _t('AssignUsersToWorkflowAction.USERS', 'Users'), |
||
82 | 'Groups' => _t('AssignUsersToWorkflowAction.GROUPS', 'Groups'), |
||
83 | 'AssignInitiator' => _t('AssignUsersToWorkflowAction.INITIATOR', 'Assign Initiator'), |
||
84 | )); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Returns a set of all Members that are assigned to this WorkflowAction subclass, either directly or via a group. |
||
89 | * |
||
90 | * @return ArrayList |
||
91 | */ |
||
92 | public function getAssignedMembers() |
||
93 | { |
||
94 | $members = $this->Users(); |
||
95 | $groups = $this->Groups(); |
||
96 | |||
97 | // Can't merge instances of DataList so convert to something where we can |
||
98 | $_members = ArrayList::create(); |
||
99 | $members->each(function ($item) use ($_members) { |
||
100 | $_members->push($item); |
||
101 | }); |
||
102 | |||
103 | $_groups = ArrayList::create(); |
||
104 | $groups->each(function ($item) use ($_groups) { |
||
105 | $_groups->push($item); |
||
106 | }); |
||
107 | |||
108 | foreach ($_groups as $group) { |
||
109 | $_members->merge($group->Members()); |
||
110 | } |
||
111 | |||
112 | $_members->removeDuplicates(); |
||
113 | return $_members; |
||
114 | } |
||
115 | } |
||
116 |