1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Daily task to send emails to the owners of content items when the review date rolls around. |
5
|
|
|
*/ |
6
|
|
|
class ContentReviewEmails extends BuildTask |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @param SS_HTTPRequest $request |
10
|
|
|
*/ |
11
|
|
|
public function run($request) |
12
|
|
|
{ |
13
|
|
|
$compatibility = ContentReviewCompatability::start(); |
|
|
|
|
14
|
|
|
|
15
|
|
|
$now = SS_Datetime::now(); |
16
|
|
|
|
17
|
|
|
// First grab all the pages with a custom setting |
18
|
|
|
$pages = Page::get() |
19
|
|
|
->filter('NextReviewDate:LessThanOrEqual', $now->URLDate()); |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
// Calculate whether today is the date a First or Second review should occur |
24
|
|
|
$config = SiteConfig::current_site_config(); |
25
|
|
|
$firstReview = $config->FirstReviewDaysBefore; |
26
|
|
|
$secondReview = $config->SecondReviewDaysBefore; |
27
|
|
|
// Subtract the number of days prior to the review, from the current date |
28
|
|
|
|
29
|
|
|
// Get all pages where the NextReviewDate is still in the future |
30
|
|
|
$pendingPages = Page::get()->filter('NextReviewDate:GreaterThan', $now->URLDate()); |
31
|
|
|
|
32
|
|
|
// for each of these pages, check if today is the date the First or Second |
33
|
|
|
// reminder should be sent, and if so, add it to the appropriate ArrayList |
34
|
|
|
$firstReminderPages = new ArrayList(); |
35
|
|
|
$secondReminderPages = new ArrayList(); |
36
|
|
|
|
37
|
|
|
foreach ($pendingPages as $page) { |
38
|
|
|
$notifyDate1 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $firstReview . ' days')); |
39
|
|
|
$notifyDate2 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $secondReview . ' days')); |
40
|
|
|
|
41
|
|
|
if ($notifyDate1 == $now->URLDate()) { |
42
|
|
|
$firstReminderPages->push($page); |
43
|
|
|
} |
44
|
|
|
if ($notifyDate2 == $now->URLDate()) { |
45
|
|
|
$secondReminderPages->push($page); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$overduePages = $this->getNotifiablePagesForOwners($pages); |
50
|
|
|
|
51
|
|
|
// Send one email to one owner with all the pages in there instead of no of pages of emails. |
52
|
|
|
foreach ($overduePages as $memberID => $pages) { |
|
|
|
|
53
|
|
|
//$this->notifyOwner($memberID, $pages, "due"); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Send an email to the generic address with any first or second reminders |
57
|
|
|
$this->notifyTeam($firstReminderPages, $secondReminderPages); |
58
|
|
|
die(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
ContentReviewCompatability::done($compatibility); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param SS_list $pages |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function getNotifiablePagesForOwners(SS_list $pages) |
69
|
|
|
{ |
70
|
|
|
$overduePages = array(); |
71
|
|
|
|
72
|
|
|
foreach ($pages as $page) { |
73
|
|
|
|
74
|
|
|
if (!$page->canRemind()) { |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$option = $page->getOptions(); |
79
|
|
|
|
80
|
|
|
foreach ($option->ContentReviewOwners() as $owner) { |
81
|
|
|
if (!isset($overduePages[$owner->ID])) { |
82
|
|
|
$overduePages[$owner->ID] = new ArrayList(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$overduePages[$owner->ID]->push($page); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $overduePages; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Send an email to the configured team indicating which notices are at 'first reminder' or 'second reminder' status |
94
|
|
|
* The 'days before due' value for each of these is configurable in settings. |
95
|
|
|
* ie. a value of 30 for the 'first reminder' setting means a page with a review date exactly 30 days from due, will |
96
|
|
|
* be present in the email sent to the configured address. |
97
|
|
|
*/ |
98
|
|
|
protected function notifyTeam($firstReminderPages, $secondReminderPages) { |
99
|
|
|
// Prepare variables |
100
|
|
|
$siteConfig = SiteConfig::current_site_config(); |
101
|
|
|
$templateVariables1 = $this->getTemplateVariables('', $siteConfig, $firstReminderPages, 'reminder1'); |
|
|
|
|
102
|
|
|
$templateVariables2 = $this->getTemplateVariables('', $siteConfig, $secondReminderPages, 'reminder2'); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Build email |
105
|
|
|
$email = new Email(); |
106
|
|
|
$email->setTo($siteConfig->ReviewReminderEmail); |
107
|
|
|
$email->setFrom($siteConfig->ReviewFrom); |
108
|
|
|
|
109
|
|
|
$subject = $siteConfig->ReviewSubjectReminder; |
110
|
|
|
$email->setSubject($subject); |
111
|
|
|
|
112
|
|
|
// Get user-editable body |
113
|
|
|
$bodyFirstReminder = $this->getEmailBody($siteConfig, $templateVariables1, 'reminder1'); |
114
|
|
|
$bodySecondReminder = $this->getEmailBody($siteConfig, $templateVariables2, 'reminder2'); |
115
|
|
|
|
116
|
|
|
// Populate mail body with fixed template |
117
|
|
|
$email->setTemplate($siteConfig->config()->content_review_reminder_template); |
118
|
|
|
$email->populateTemplate($templateVariables1, $templateVariables2); |
|
|
|
|
119
|
|
|
$email->populateTemplate(array( |
120
|
|
|
'EmailBodyFirstReminder' => $bodyFirstReminder, |
121
|
|
|
'EmailBodySecondReminder' => $bodySecondReminder, |
122
|
|
|
'FirstReminderPages' => $firstReminderPages, |
123
|
|
|
'SecondReminderPages' => $secondReminderPages, |
124
|
|
|
)); |
125
|
|
|
|
126
|
|
|
$email->send(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param int $ownerID |
131
|
|
|
* @param array|SS_List $pages |
132
|
|
|
* @param string $type |
133
|
|
|
*/ |
134
|
|
|
protected function notifyOwner($ownerID, SS_List $pages, $type) |
135
|
|
|
{ |
136
|
|
|
// Prepare variables |
137
|
|
|
$siteConfig = SiteConfig::current_site_config(); |
138
|
|
|
$owner = Member::get()->byID($ownerID); |
139
|
|
|
$templateVariables = $this->getTemplateVariables($owner, $siteConfig, $pages, $type); |
|
|
|
|
140
|
|
|
|
141
|
|
|
// Build email |
142
|
|
|
$email = new Email(); |
143
|
|
|
$email->setTo($owner->Email); |
144
|
|
|
$email->setFrom($siteConfig->ReviewFrom); |
145
|
|
|
|
146
|
|
|
$subject = $siteConfig->ReviewSubject; |
147
|
|
|
$email->setSubject($subject); |
148
|
|
|
|
149
|
|
|
// Get user-editable body |
150
|
|
|
$body = $this->getEmailBody($siteConfig, $templateVariables, $type); |
151
|
|
|
|
152
|
|
|
// Populate mail body with fixed template |
153
|
|
|
$email->setTemplate($siteConfig->config()->content_review_template); |
154
|
|
|
$email->populateTemplate($templateVariables); |
155
|
|
|
$email->populateTemplate(array( |
156
|
|
|
'EmailBody' => $body, |
157
|
|
|
'Recipient' => $owner, |
158
|
|
|
'Pages' => $pages, |
159
|
|
|
)); |
160
|
|
|
|
161
|
|
|
$email->send(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get string value of HTML body with all variable evaluated. |
166
|
|
|
* |
167
|
|
|
* @param SiteConfig $config |
168
|
|
|
* @param array List of safe template variables to expose to this template |
169
|
|
|
* |
170
|
|
|
* @return HTMLText |
171
|
|
|
*/ |
172
|
|
|
protected function getEmailBody($config, $variables, $type) |
173
|
|
|
{ |
174
|
|
|
if ($type == "reminder1") { |
175
|
|
|
$template = SSViewer::fromString($config->ReviewBodyFirstReminder); |
176
|
|
|
} |
177
|
|
|
if ($type == "reminder2") { |
178
|
|
|
$template = SSViewer::fromString($config->ReviewBodySecondReminder); |
179
|
|
|
} |
180
|
|
|
if ($type == "due") { |
181
|
|
|
$template = SSViewer::fromString($config->ReviewBody); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$value = $template->process(new ArrayData($variables)); |
|
|
|
|
185
|
|
|
|
186
|
|
|
// Cast to HTML |
187
|
|
|
return DBField::create_field('HTMLText', (string) $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets list of safe template variables and their values which can be used |
192
|
|
|
* in both the static and editable templates. |
193
|
|
|
* |
194
|
|
|
* {@see ContentReviewAdminHelp.ss} |
195
|
|
|
* |
196
|
|
|
* @param Member $recipient |
197
|
|
|
* @param SiteConfig $config |
198
|
|
|
* @param SS_List $pages |
199
|
|
|
* @param string $type |
|
|
|
|
200
|
|
|
* |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
protected function getTemplateVariables($recipient = null, $config, $pages) |
204
|
|
|
{ |
205
|
|
|
if ($recipient != null) { |
206
|
|
|
return array( |
207
|
|
|
'Subject' => $config->ReviewSubject, |
208
|
|
|
'PagesCount' => $pages->count(), |
209
|
|
|
'FromEmail' => $config->ReviewFrom, |
210
|
|
|
'ToFirstName' => $recipient->FirstName, |
211
|
|
|
'ToSurname' => $recipient->Surname, |
212
|
|
|
'ToEmail' => $recipient->Email |
213
|
|
|
); |
214
|
|
|
} else { |
215
|
|
|
return array( |
216
|
|
|
'Subject' => $config->ReviewSubjectReminder, |
217
|
|
|
'FirstReminderPagesCount' => $pages->count(), |
218
|
|
|
'SecondReminderPagesCount' => $pages->count(), |
219
|
|
|
'FromEmail' => $config->ReviewFrom, |
220
|
|
|
'ToEmail' => $config->ReviewReminderEmail |
221
|
|
|
); |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
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.