|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A workflow action that notifies users attached to the workflow path that they have a task awaiting them. |
|
4
|
|
|
* |
|
5
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
|
6
|
|
|
* @package advancedworkflow |
|
7
|
|
|
* @subpackage actions |
|
8
|
|
|
*/ |
|
9
|
|
|
class NotifyUsersWorkflowAction extends WorkflowAction { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var bool Should templates be constrained to just known-safe variables. |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $whitelist_template_variables = false; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
private static $db = array( |
|
|
|
|
|
|
17
|
|
|
'EmailSubject' => 'Varchar(100)', |
|
18
|
|
|
'EmailFrom' => 'Varchar(50)', |
|
19
|
|
|
'EmailTemplate' => 'Text' |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
public static $icon = 'advancedworkflow/images/notify.png'; |
|
23
|
|
|
|
|
24
|
|
|
public function getCMSFields() { |
|
25
|
|
|
$fields = parent::getCMSFields(); |
|
26
|
|
|
|
|
27
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
|
28
|
|
|
new HeaderField('NotificationEmail', $this->fieldLabel('NotificationEmail')), |
|
29
|
|
|
new LiteralField('NotificationNote', '<p>' . $this->fieldLabel('NotificationNote') . '</p>'), |
|
30
|
|
|
new TextField('EmailSubject', $this->fieldLabel('EmailSubject')), |
|
31
|
|
|
new TextField('EmailFrom', $this->fieldLabel('EmailFrom')), |
|
32
|
|
|
|
|
33
|
|
|
new TextareaField('EmailTemplate', $this->fieldLabel('EmailTemplate')), |
|
34
|
|
|
new ToggleCompositeField('FormattingHelpContainer', |
|
35
|
|
|
$this->fieldLabel('FormattingHelp'), new LiteralField('FormattingHelp', $this->getFormattingHelp())) |
|
|
|
|
|
|
36
|
|
|
)); |
|
37
|
|
|
|
|
38
|
|
|
$this->extend('updateNotifyUsersCMSFields', $fields); |
|
39
|
|
|
|
|
40
|
|
|
return $fields; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function fieldLabels($relations = true) { |
|
44
|
|
|
return array_merge(parent::fieldLabels($relations), array( |
|
45
|
|
|
'NotificationEmail' => _t('NotifyUsersWorkflowAction.NOTIFICATIONEMAIL', 'Notification Email'), |
|
46
|
|
|
'NotificationNote' => _t('NotifyUsersWorkflowAction.NOTIFICATIONNOTE', |
|
47
|
|
|
'All users attached to the workflow will be sent an email when this action is run.'), |
|
48
|
|
|
'EmailSubject' => _t('NotifyUsersWorkflowAction.EMAILSUBJECT', 'Email subject'), |
|
49
|
|
|
'EmailFrom' => _t('NotifyUsersWorkflowAction.EMAILFROM', 'Email from'), |
|
50
|
|
|
'EmailTemplate' => _t('NotifyUsersWorkflowAction.EMAILTEMPLATE', 'Email template'), |
|
51
|
|
|
'FormattingHelp' => _t('NotifyUsersWorkflowAction.FORMATTINGHELP', 'Formatting Help') |
|
52
|
|
|
)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function execute(WorkflowInstance $workflow) { |
|
56
|
|
|
$members = $workflow->getAssignedMembers(); |
|
57
|
|
|
|
|
58
|
|
|
if(!$members || !count($members)) { |
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$member = Member::currentUser(); |
|
63
|
|
|
$initiator = $workflow->Initiator(); |
|
64
|
|
|
|
|
65
|
|
|
$contextFields = $this->getContextFields($workflow->getTarget()); |
|
|
|
|
|
|
66
|
|
|
$memberFields = $this->getMemberFields($member); |
|
|
|
|
|
|
67
|
|
|
$initiatorFields = $this->getMemberFields($initiator); |
|
68
|
|
|
|
|
69
|
|
|
$variables = array(); |
|
70
|
|
|
|
|
71
|
|
|
foreach($contextFields as $field => $val) $variables["\$Context.$field"] = $val; |
|
72
|
|
|
foreach($memberFields as $field => $val) $variables["\$Member.$field"] = $val; |
|
73
|
|
|
foreach($initiatorFields as $field => $val) $variables["\$Initiator.$field"] = $val; |
|
74
|
|
|
|
|
75
|
|
|
$pastActions = $workflow->Actions()->sort('Created DESC'); |
|
|
|
|
|
|
76
|
|
|
$variables["\$CommentHistory"] = $this->customise(array( |
|
77
|
|
|
'PastActions'=>$pastActions, |
|
78
|
|
|
'Now'=>SS_Datetime::now() |
|
79
|
|
|
))->renderWith('CommentHistory'); |
|
80
|
|
|
|
|
81
|
|
|
$from = str_replace(array_keys($variables), array_values($variables), $this->EmailFrom); |
|
|
|
|
|
|
82
|
|
|
$subject = str_replace(array_keys($variables), array_values($variables), $this->EmailSubject); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if ($this->config()->whitelist_template_variables) { |
|
85
|
|
|
$item = new ArrayData(array( |
|
86
|
|
|
'Initiator' => new ArrayData($initiatorFields), |
|
87
|
|
|
'Member' => new ArrayData($memberFields), |
|
88
|
|
|
'Context' => new ArrayData($contextFields), |
|
89
|
|
|
'CommentHistory' => $variables["\$CommentHistory"] |
|
90
|
|
|
)); |
|
91
|
|
|
} |
|
92
|
|
|
else { |
|
93
|
|
|
$item = $workflow->customise(array( |
|
94
|
|
|
'Items' => $workflow->Actions(), |
|
|
|
|
|
|
95
|
|
|
'Member' => $member, |
|
96
|
|
|
'Context' => new ArrayData($contextFields), |
|
97
|
|
|
'CommentHistory' => $variables["\$CommentHistory"] |
|
98
|
|
|
)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$view = SSViewer::fromString($this->EmailTemplate); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$body = $view->process($item); |
|
105
|
|
|
|
|
106
|
|
|
foreach($members as $member) { |
|
107
|
|
|
if($member->Email) { |
|
108
|
|
|
$email = new Email; |
|
109
|
|
|
$email->setTo($member->Email); |
|
110
|
|
|
$email->setSubject($subject); |
|
111
|
|
|
$email->setFrom($from); |
|
112
|
|
|
$email->setBody($body); |
|
113
|
|
|
$email->send(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param DataObject $target |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getContextFields(DataObject $target) { |
|
125
|
|
|
$result = array(); |
|
126
|
|
|
if (!$target) { |
|
127
|
|
|
return $result; |
|
128
|
|
|
} |
|
129
|
|
|
$fields = $target->inheritedDatabaseFields(); |
|
130
|
|
|
|
|
131
|
|
|
foreach($fields as $field => $fieldDesc) { |
|
132
|
|
|
$result[$field] = $target->$field; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if($target instanceof CMSPreviewable) { |
|
136
|
|
|
$result['CMSLink'] = $target->CMSEditLink(); |
|
137
|
|
|
} else if ($target->hasMethod('WorkflowLink')) { |
|
138
|
|
|
$result['CMSLink'] = $target->WorkflowLink(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $result; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Builds an array with the member information |
|
146
|
|
|
* @param Member $member An optional member to use. If null, will use the current logged in member |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getMemberFields(Member $member = null) { |
|
150
|
|
|
if (!$member){ |
|
151
|
|
|
$member = Member::currentUser(); |
|
152
|
|
|
} |
|
153
|
|
|
$result = array(); |
|
154
|
|
|
|
|
155
|
|
|
if($member) foreach($member->summaryFields() as $field => $title) { |
|
156
|
|
|
$result[$field] = $member->$field; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if($member && !array_key_exists('Name', $result)) { |
|
160
|
|
|
$result['Name'] = $member->getName(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $result; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Returns a basic set of instructions on how email templates are populated with variables. |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getFormattingHelp() { |
|
173
|
|
|
$note = _t('NotifyUsersWorkflowAction.FORMATTINGNOTE', |
|
174
|
|
|
'Notification emails can contain HTML formatting. The following special variables are replaced with their |
|
175
|
|
|
respective values in the email subject, email from and template/body.'); |
|
176
|
|
|
$member = _t('NotifyUsersWorkflowAction.MEMBERNOTE', |
|
177
|
|
|
'These fields will be populated from the member that initiates the notification action. For example, |
|
178
|
|
|
{$Member.FirstName}.'); |
|
179
|
|
|
$initiator = _t('NotifyUsersWorkflowAction.INITIATORNOTE', |
|
180
|
|
|
'These fields will be populated from the member that initiates the workflow request. For example, |
|
181
|
|
|
{$Initiator.Email}.'); |
|
182
|
|
|
$context = _t('NotifyUsersWorkflowAction.CONTEXTNOTE', |
|
183
|
|
|
'Any summary fields from the workflow target will be available. For example, {$Context.Title}. |
|
184
|
|
|
Additionally, the {$Context.AbsoluteEditLink} variable will contain a link to edit the workflow target in |
|
185
|
|
|
the CMS (if it is a Page), and the {$Context.LinkToPendingItems} variable will generate a link to the CMS\' workflow admin, |
|
186
|
|
|
useful for allowing users to enact workflow transitions, directly from emails.'); |
|
187
|
|
|
$fieldName = _t('NotifyUsersWorkflowAction.FIELDNAME', 'Field name'); |
|
188
|
|
|
$commentHistory = _t('NotifyUsersWorkflowAction.COMMENTHISTORY', 'Comment history up to this notification.'); |
|
189
|
|
|
|
|
190
|
|
|
$memberFields = implode(', ', array_keys($this->getMemberFields())); |
|
191
|
|
|
|
|
192
|
|
|
return "<p>$note</p> |
|
193
|
|
|
<p><strong>{\$Member.($memberFields)}</strong><br>$member</p> |
|
194
|
|
|
<p><strong>{\$Initiator.($memberFields)}</strong><br>$initiator</p> |
|
195
|
|
|
<p><strong>{\$Context.($fieldName)}</strong><br>$context</p> |
|
196
|
|
|
<p><strong>{\$CommentHistory}</strong><br>$commentHistory</p>"; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.