further refactor to send emails to single address

This commit is contained in:
Julian 2016-08-30 17:01:35 +12:00
parent e02991ef05
commit 556f3c3126
6 changed files with 157 additions and 106 deletions

View File

@ -29,7 +29,6 @@ class ContentReviewCMSExtension extends LeftAndMainExtension
if (!$page->canEdit()) { if (!$page->canEdit()) {
return Security::permissionFailure($this->owner); return Security::permissionFailure($this->owner);
} }
$notes = (!empty($data["ReviewNotes"]) ? $data["ReviewNotes"] : _t("ContentReview.NOCOMMENTS", "(no comments)")); $notes = (!empty($data["ReviewNotes"]) ? $data["ReviewNotes"] : _t("ContentReview.NOCOMMENTS", "(no comments)"));
$page->addReviewNote(Member::currentUser(), $notes, $page->ReviewInfo); $page->addReviewNote(Member::currentUser(), $notes, $page->ReviewInfo);
$page->advanceReviewDate(); $page->advanceReviewDate();

View File

@ -17,11 +17,11 @@ class ContentReviewDefaultSettings extends DataExtension
'ReviewPeriodDays' => 'Int', 'ReviewPeriodDays' => 'Int',
'ReviewFrom' => 'Varchar(255)', 'ReviewFrom' => 'Varchar(255)',
'ReviewSubject' => 'Varchar(255)', 'ReviewSubject' => 'Varchar(255)',
'ReviewSubjectFirstReminder' => 'Varchar(255)', 'ReviewSubjectReminder' => 'Varchar(255)',
'ReviewSubjectSecondReminder' => 'Varchar(255)',
'ReviewBody' => 'HTMLText', 'ReviewBody' => 'HTMLText',
'ReviewBodyFirstReminder' => 'HTMLText', 'ReviewBodyFirstReminder' => 'HTMLText',
'ReviewBodySecondReminder' => 'HTMLText', 'ReviewBodySecondReminder' => 'HTMLText',
'ReviewReminderEmail' => 'Text',
'FirstReviewDaysBefore' => 'Int', 'FirstReviewDaysBefore' => 'Int',
'SecondReviewDaysBefore' => 'Int' 'SecondReviewDaysBefore' => 'Int'
); );
@ -32,12 +32,12 @@ class ContentReviewDefaultSettings extends DataExtension
* @var array * @var array
*/ */
private static $defaults = array( private static $defaults = array(
'ReviewSubjectFirstReminder' => 'Page(s) are 1 month from content review', 'ReviewSubjectReminder' => 'Page(s) are approaching content review date',
'ReviewSubjectSecondReminder' => 'Page(s) are 1 week from content review',
'ReviewSubject' => 'Page(s) are due for content review', 'ReviewSubject' => 'Page(s) are due for content review',
'ReviewBodyFirstReminder' => '<h2>Page(s) 1 month from review</h2><p>There are $PagesCount pages that are due for review by you 1 month from today.</p>', 'ReviewBodyFirstReminder' => '<h2>Page(s) 1 month from review</h2><p>There are $PagesCount pages that are due for review by you 1 month from today.</p>',
'ReviewBodySecondReminder' => '<h2>Page(s) 1 week from from review</h2><p>There are $PagesCount pages that are due for review by you 1 week from today.</p>', 'ReviewBodySecondReminder' => '<h2>Page(s) 1 week from from review</h2><p>There are $PagesCount pages that are due for review by you 1 week from today.</p>',
'ReviewBody' => '<h2>Page(s) due for review</h2><p>There are $PagesCount pages that are due for review today by you.</p>', 'ReviewBody' => '<h2>Page(s) due for review</h2><p>There are $PagesCount pages that are due for review today by you.</p>',
'ReviewReminderEmail' => 'govt.nz@dia.govt.nz',
'FirstReviewDaysBefore' => '30', 'FirstReviewDaysBefore' => '30',
'SecondReviewDaysBefore' => '7' 'SecondReviewDaysBefore' => '7'
); );
@ -62,6 +62,7 @@ class ContentReviewDefaultSettings extends DataExtension
* @var string * @var string
*/ */
private static $content_review_template = 'ContentReviewEmail'; private static $content_review_template = 'ContentReviewEmail';
private static $content_review_reminder_template = 'ContentReviewReminderEmail';
/** /**
* @return string * @return string
@ -175,12 +176,13 @@ class ContentReviewDefaultSettings extends DataExtension
array( array(
TextField::create('ReviewFrom', _t('ContentReview.EMAILFROM', 'From email address')) TextField::create('ReviewFrom', _t('ContentReview.EMAILFROM', 'From email address'))
->setRightTitle(_t('Review.EMAILFROM_RIGHTTITLE', 'e.g: do-not-reply@site.com')), ->setRightTitle(_t('Review.EMAILFROM_RIGHTTITLE', 'e.g: do-not-reply@site.com')),
TextField::create('ReviewSubjectFirstReminder', _t('ContentReview.EMAILSUBJECTFIRSTREMINDER', 'Subject line - First reminder')), TextField::create('ReviewReminderEmail','Review reminder email address')
TextField::create('ReviewSubjectSecondReminder', _t('ContentReview.EMAILSUBJECTSECONDREMINDER', 'Subject line - Second reminder')), ->setRightTitle('e.g: review.reminders@site.com'),
TextField::create('ReviewSubjectReminder', _t('ContentReview.EMAILSUBJECTREMINDER', 'Subject line - reminder')),
TextField::create('ReviewSubject', _t('ContentReview.EMAILSUBJECT', 'Subject line - Review due')), TextField::create('ReviewSubject', _t('ContentReview.EMAILSUBJECT', 'Subject line - Review due')),
TextAreaField::create('ReviewBodyFirstReminder', _t('ContentReview.EMAILTEMPLATEFIRSTREMINDER', 'Email template - First reminder')), TextAreaField::create('ReviewBodyFirstReminder', _t('ContentReview.EMAILTEMPLATEFIRSTREMINDER', 'Email body - First reminder')),
TextAreaField::create('ReviewBodySecondReminder', _t('ContentReview.EMAILTEMPLATESECONDREMINDER', 'Email template - Second reminder')), TextAreaField::create('ReviewBodySecondReminder', _t('ContentReview.EMAILTEMPLATESECONDREMINDER', 'Email body - Second reminder')),
TextAreaField::create('ReviewBody', _t('ContentReview.EMAILTEMPLATE', 'Email template - Review due')), TextAreaField::create('ReviewBody', _t('ContentReview.EMAILTEMPLATE', 'Email body - Review due')),
LiteralField::create('TemplateHelp', $this->owner->renderWith('ContentReviewAdminHelp')), LiteralField::create('TemplateHelp', $this->owner->renderWith('ContentReviewAdminHelp')),
) )
); );

View File

@ -493,44 +493,6 @@ class SiteTreeContentReview extends DataExtension implements PermissionProvider
return false; return false;
} }
public function canRemindAgain(Member $member = null) {
if (!$this->owner->obj("NextReviewDate")->exists()) {
return false;
}
// If today is not the date of the second reminder, return false
$config = SiteConfig::current_site_config();
$secondReview = $config->SecondReviewDaysBefore;
$now = SS_Datetime::now();
$notifyDate2 = date('Y-m-d', strtotime($this->owner->NextReviewDate . ' -' . $secondReview . ' days'));
if (!$notifyDate2 == $now->URLDate()) {
return false;
}
$options = $this->getOptions();
if (!$options) {
return false;
} elseif ($options->OwnerGroups()->count() == 0 && $options->OwnerUsers()->count() == 0) {
return false;
}
if (!$member) {
return true;
}
if ($member->inGroups($options->OwnerGroups())) {
return true;
}
if ($options->OwnerUsers()->find("ID", $member->ID)) {
return true;
}
return false;
}
/** /**
* Check if a review is due by a member for this owner. * Check if a review is due by a member for this owner.
* *

View File

@ -18,6 +18,8 @@ class ContentReviewEmails extends BuildTask
$pages = Page::get() $pages = Page::get()
->filter('NextReviewDate:LessThanOrEqual', $now->URLDate()); ->filter('NextReviewDate:LessThanOrEqual', $now->URLDate());
// Calculate whether today is the date a First or Second review should occur // Calculate whether today is the date a First or Second review should occur
$config = SiteConfig::current_site_config(); $config = SiteConfig::current_site_config();
$firstReview = $config->FirstReviewDaysBefore; $firstReview = $config->FirstReviewDaysBefore;
@ -29,39 +31,51 @@ class ContentReviewEmails extends BuildTask
// for each of these pages, check if today is the date the First or Second // for each of these pages, check if today is the date the First or Second
// reminder should be sent, and if so, add it to the appropriate ArrayList // reminder should be sent, and if so, add it to the appropriate ArrayList
$pendingPagesFirstReminder = new ArrayList(); $firstReminderPages = new ArrayList();
$pendingPagesSecondReminder = new ArrayList(); $secondReminderPages = new ArrayList();
foreach ($pendingPages as $page) { foreach ($pendingPages as $page) {
$notifyDate1 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $firstReview . ' days')); $notifyDate1 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $firstReview . ' days'));
$notifyDate2 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $secondReview . ' days')); $notifyDate2 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $secondReview . ' days'));
if ($notifyDate1 == $now->URLDate()) { if ($notifyDate1 == $now->URLDate()) {
$pendingPagesFirstReminder->push($page); $firstReminderPages->push($page);
} }
if ($notifyDate2 == $now->URLDate()) { if ($notifyDate2 == $now->URLDate()) {
$pendingPagesSecondReminder->push($page); $secondReminderPages->push($page);
} }
} }
$overduePages = $this->getNotifiablePagesForOwners($pages, "due");
$firstReminderPages = $this->getNotifiablePagesForOwners($pendingPagesFirstReminder, "first");
$secondReminderPages = $this->getNotifiablePagesForOwners($pendingPagesSecondReminder, "second");
Debug::show('====================================================================FIRST REMINDER');
foreach ($firstReminderPages as $p) {
Debug::show($p->Title);
}
Debug::show('====================================================================SECOND REMINDER');
foreach ($secondReminderPages as $p) {
Debug::show($p->Title);
}
Debug::show('====================================================================DUE/OVERDUE');
foreach ($pages as $p) {
Debug::show($p->Title);
}
//die();
$overduePages = $this->getNotifiablePagesForOwners($pages);
// Send one email to one owner with all the pages in there instead of no of pages of emails. // Send one email to one owner with all the pages in there instead of no of pages of emails.
foreach ($overduePages as $memberID => $pages) { foreach ($overduePages as $memberID => $pages) {
$this->notifyOwner($memberID, $pages, "due"); //$this->notifyOwner($memberID, $pages, "due");
} }
// Send a separate email with a different subject line for the first reminder // Send an email to the generic address with any first or second reminders
foreach ($firstReminderPages as $memberID => $pages) { $this->notifyTeam($firstReminderPages, $secondReminderPages);
$this->notifyOwner($memberID, $pages, "first"); die();
}
// Send a separate email with a different subject line for the second reminder
foreach ($secondReminderPages as $memberID => $pages) {
$this->notifyOwner($memberID, $pages, "second");
}
ContentReviewCompatability::done($compatibility); ContentReviewCompatability::done($compatibility);
} }
@ -71,27 +85,15 @@ class ContentReviewEmails extends BuildTask
* *
* @return array * @return array
*/ */
protected function getNotifiablePagesForOwners(SS_list $pages, $type) protected function getNotifiablePagesForOwners(SS_list $pages)
{ {
$overduePages = array(); $overduePages = array();
foreach ($pages as $page) { foreach ($pages as $page) {
if ($type == "first") {
if (!$page->canRemind()) { if (!$page->canRemind()) {
continue; continue;
} }
}
if ($type == "second") {
if (!$page->canRemindAgain()) {
continue;
}
}
if ($type == "due") {
if (!$page->canBeReviewedBy()) {
continue;
}
}
$option = $page->getOptions(); $option = $page->getOptions();
@ -107,6 +109,45 @@ class ContentReviewEmails extends BuildTask
return $overduePages; return $overduePages;
} }
protected function notifyTeam($firstReminderPages, $secondReminderPages) {
// Prepare variables
$siteConfig = SiteConfig::current_site_config();
$templateVariables1 = $this->getTemplateVariables('', $siteConfig, $firstReminderPages, 'reminder1');
$templateVariables2 = $this->getTemplateVariables('', $siteConfig, $secondReminderPages, 'reminder2');
// Build email
$email = new Email();
$email->setTo($siteConfig->Email);
$email->setFrom($siteConfig->ReviewFrom);
$subject = $siteConfig->ReviewSubject;
$email->setSubject($subject);
// Get user-editable body
$bodyFirstReminder = $this->getEmailBody($siteConfig, $templateVariables1, 'reminder1');
$bodySecondReminder = $this->getEmailBody($siteConfig, $templateVariables2, 'reminder2');
// Populate mail body with fixed template
$email->setTemplate($siteConfig->config()->content_review_reminder_template);
$email->populateTemplate($templateVariables1, $templateVariables2);
$email->populateTemplate(array(
'EmailBodyFirstReminder' => $bodyFirstReminder,
'EmailBodySecondReminder' => $bodySecondReminder,
'Recipient' => $siteConfig->ReviewReminderEmail,
'FirstReminderPages' => $firstReminderPages,
'SecondReminderPages' => $secondReminderPages,
));
Debug::show($email);
//$email->send();
}
/** /**
* @param int $ownerID * @param int $ownerID
* @param array|SS_List $pages * @param array|SS_List $pages
@ -124,9 +165,7 @@ class ContentReviewEmails extends BuildTask
$email->setTo($owner->Email); $email->setTo($owner->Email);
$email->setFrom($siteConfig->ReviewFrom); $email->setFrom($siteConfig->ReviewFrom);
if ($type == "first"){$subject = $siteConfig->ReviewSubjectFirstReminder;} $subject = $siteConfig->ReviewSubject;
if ($type == "second"){$subject = $siteConfig->ReviewSubjectSecondReminder;}
if ($type == "due"){$subject = $siteConfig->ReviewSubject;}
$email->setSubject($subject); $email->setSubject($subject);
// Get user-editable body // Get user-editable body
@ -155,10 +194,10 @@ class ContentReviewEmails extends BuildTask
*/ */
protected function getEmailBody($config, $variables, $type) protected function getEmailBody($config, $variables, $type)
{ {
if ($type == "first") { if ($type == "reminder1") {
$template = SSViewer::fromString($config->ReviewBodyFirstReminder); $template = SSViewer::fromString($config->ReviewBodyFirstReminder);
} }
if ($type == "second") { if ($type == "reminder2") {
$template = SSViewer::fromString($config->ReviewBodySecondReminder); $template = SSViewer::fromString($config->ReviewBodySecondReminder);
} }
if ($type == "due") { if ($type == "due") {
@ -184,20 +223,29 @@ class ContentReviewEmails extends BuildTask
* *
* @return array * @return array
*/ */
protected function getTemplateVariables($recipient, $config, $pages, $type) protected function getTemplateVariables($recipient = null, $config, $pages)
{ {
if ($type == "first") {$subject = $config->ReviewSubjectFirstReminder;} if ($recipient != null) {
if ($type == "second") {$subject = $config->ReviewSubjectSecondReminder;}
if ($type == "due") {$subject = $config->ReviewSubject;}
return array( return array(
'Subject' => $subject, 'Subject' => $config->ReviewSubject,
'PagesCount' => $pages->count(), 'PagesCount' => $pages->count(),
'FromEmail' => $config->ReviewFrom, 'FromEmail' => $config->ReviewFrom,
'ToFirstName' => $recipient->FirstName, 'ToFirstName' => $recipient->FirstName,
'ToSurname' => $recipient->Surname, 'ToSurname' => $recipient->Surname,
'ToEmail' => $recipient->Email, 'ToEmail' => $recipient->Email
'Type' => $type
); );
} else {
return array(
'Subject' => $config->ReviewSubjectReminder,
'FirstReminderPagesCount' => $pages->count(),
'SecondReminderPagesCount' => $pages->count(),
'FromEmail' => $config->ReviewFrom,
'ToEmail' => $config->ReviewReminderEmail
);
}
} }
} }

View File

@ -10,12 +10,11 @@ en:
DEFAULTSETTINGSHELP: 'These content review settings will apply to all pages that does not have specific Content Review schedule.' DEFAULTSETTINGSHELP: 'These content review settings will apply to all pages that does not have specific Content Review schedule.'
DISABLE: 'Disable content review' DISABLE: 'Disable content review'
EMAILFROM: 'From email address' EMAILFROM: 'From email address'
EMAILSUBJECTFIRSTREMINDER: 'Subject line - First reminder' EMAILSUBJECTREMINDER: 'Subject line - Reminder'
EMAILSUBJECTSECONDREMINDER: 'Subject line - Second reminder'
EMAILSUBJECT: 'Subject line - Review due' EMAILSUBJECT: 'Subject line - Review due'
EMAILTEMPLATEFIRSTREMINDER: 'Email template - First reminder' EMAILTEMPLATEFIRSTREMINDER: 'Email body - First reminder'
EMAILTEMPLATESECONDREMINDER: 'Email template - Second reminder' EMAILTEMPLATESECONDREMINDER: 'Email body - Second reminder'
EMAILTEMPLATE: 'Email template - Review due' EMAILTEMPLATE: 'Email body - Review due'
FIRSTREVIEWDAYSBEFORE: 'First review reminder # days before final review' FIRSTREVIEWDAYSBEFORE: 'First review reminder # days before final review'
INHERIT: 'Inherit from parent page' INHERIT: 'Inherit from parent page'
MARKREVIEWED: 'Mark as reviewed' MARKREVIEWED: 'Mark as reviewed'

View File

@ -0,0 +1,41 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head></head>
<body id="Content">
<table cellspacing="1" cellpadding="10">
<tbody>
<tr>
<td scope="row" colspan="2" class="typography">
$EmailBodyFirstReminder
</td>
</tr>
<% loop $FirstReminderPages %>
<tr>
<td valign="top">$Title</td>
<td><a href="{$BaseURL}admin/pages/edit/show/$ID"><% _t('ContentReviewEmails.REVIEWPAGELINK','Review the page in the CMS') %></a><br />
<a href="$AbsoluteLink"><% _t('ContentReviewEmails.VIEWPUBLISHEDLINK','View this page on the website') %></a>
</td>
</tr>
<% end_loop %>
</tbody>
</table>
<table id="Content" cellspacing="1" cellpadding="10">
<tbody>
<tr>
<td scope="row" colspan="2" class="typography">
$EmailBodySecondReminder
</td>
</tr>
<% loop $SecondReminderPages %>
<tr>
<td valign="top">$Title</td>
<td><a href="{$BaseURL}admin/pages/edit/show/$ID"><% _t('ContentReviewEmails.REVIEWPAGELINK','Review the page in the CMS') %></a><br />
<a href="$AbsoluteLink"><% _t('ContentReviewEmails.VIEWPUBLISHEDLINK','View this page on the website') %></a>
</td>
</tr>
<% end_loop %>
</tbody>
</table>
</body>
</html>