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

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

View File

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

View File

@ -492,45 +492,7 @@ class SiteTreeContentReview extends DataExtension implements PermissionProvider
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.
*

View File

@ -18,6 +18,8 @@ class ContentReviewEmails extends BuildTask
$pages = Page::get()
->filter('NextReviewDate:LessThanOrEqual', $now->URLDate());
// Calculate whether today is the date a First or Second review should occur
$config = SiteConfig::current_site_config();
$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
// reminder should be sent, and if so, add it to the appropriate ArrayList
$pendingPagesFirstReminder = new ArrayList();
$pendingPagesSecondReminder = new ArrayList();
$firstReminderPages = new ArrayList();
$secondReminderPages = new ArrayList();
foreach ($pendingPages as $page) {
$notifyDate1 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $firstReview . ' days'));
$notifyDate2 = date('Y-m-d', strtotime($page->NextReviewDate . ' -' . $secondReview . ' days'));
if ($notifyDate1 == $now->URLDate()) {
$pendingPagesFirstReminder->push($page);
$firstReminderPages->push($page);
}
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.
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
foreach ($firstReminderPages as $memberID => $pages) {
$this->notifyOwner($memberID, $pages, "first");
}
// Send a separate email with a different subject line for the second reminder
foreach ($secondReminderPages as $memberID => $pages) {
$this->notifyOwner($memberID, $pages, "second");
}
// Send an email to the generic address with any first or second reminders
$this->notifyTeam($firstReminderPages, $secondReminderPages);
die();
ContentReviewCompatability::done($compatibility);
}
@ -71,28 +85,16 @@ class ContentReviewEmails extends BuildTask
*
* @return array
*/
protected function getNotifiablePagesForOwners(SS_list $pages, $type)
protected function getNotifiablePagesForOwners(SS_list $pages)
{
$overduePages = array();
foreach ($pages as $page) {
if ($type == "first") {
if (!$page->canRemind()) {
continue;
}
if (!$page->canRemind()) {
continue;
}
if ($type == "second") {
if (!$page->canRemindAgain()) {
continue;
}
}
if ($type == "due") {
if (!$page->canBeReviewedBy()) {
continue;
}
}
$option = $page->getOptions();
foreach ($option->ContentReviewOwners() as $owner) {
@ -105,6 +107,45 @@ class ContentReviewEmails extends BuildTask
}
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();
}
/**
@ -124,9 +165,7 @@ class ContentReviewEmails extends BuildTask
$email->setTo($owner->Email);
$email->setFrom($siteConfig->ReviewFrom);
if ($type == "first"){$subject = $siteConfig->ReviewSubjectFirstReminder;}
if ($type == "second"){$subject = $siteConfig->ReviewSubjectSecondReminder;}
if ($type == "due"){$subject = $siteConfig->ReviewSubject;}
$subject = $siteConfig->ReviewSubject;
$email->setSubject($subject);
// Get user-editable body
@ -155,10 +194,10 @@ class ContentReviewEmails extends BuildTask
*/
protected function getEmailBody($config, $variables, $type)
{
if ($type == "first") {
if ($type == "reminder1") {
$template = SSViewer::fromString($config->ReviewBodyFirstReminder);
}
if ($type == "second") {
if ($type == "reminder2") {
$template = SSViewer::fromString($config->ReviewBodySecondReminder);
}
if ($type == "due") {
@ -184,20 +223,29 @@ class ContentReviewEmails extends BuildTask
*
* @return array
*/
protected function getTemplateVariables($recipient, $config, $pages, $type)
protected function getTemplateVariables($recipient = null, $config, $pages)
{
if ($type == "first") {$subject = $config->ReviewSubjectFirstReminder;}
if ($type == "second") {$subject = $config->ReviewSubjectSecondReminder;}
if ($type == "due") {$subject = $config->ReviewSubject;}
if ($recipient != null) {
return array(
'Subject' => $config->ReviewSubject,
'PagesCount' => $pages->count(),
'FromEmail' => $config->ReviewFrom,
'ToFirstName' => $recipient->FirstName,
'ToSurname' => $recipient->Surname,
'ToEmail' => $recipient->Email
);
} else {
return array(
'Subject' => $config->ReviewSubjectReminder,
'FirstReminderPagesCount' => $pages->count(),
'SecondReminderPagesCount' => $pages->count(),
'FromEmail' => $config->ReviewFrom,
'ToEmail' => $config->ReviewReminderEmail
);
}
return array(
'Subject' => $subject,
'PagesCount' => $pages->count(),
'FromEmail' => $config->ReviewFrom,
'ToFirstName' => $recipient->FirstName,
'ToSurname' => $recipient->Surname,
'ToEmail' => $recipient->Email,
'Type' => $type
);
}
}

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.'
DISABLE: 'Disable content review'
EMAILFROM: 'From email address'
EMAILSUBJECTFIRSTREMINDER: 'Subject line - First reminder'
EMAILSUBJECTSECONDREMINDER: 'Subject line - Second reminder'
EMAILSUBJECTREMINDER: 'Subject line - Reminder'
EMAILSUBJECT: 'Subject line - Review due'
EMAILTEMPLATEFIRSTREMINDER: 'Email template - First reminder'
EMAILTEMPLATESECONDREMINDER: 'Email template - Second reminder'
EMAILTEMPLATE: 'Email template - Review due'
EMAILTEMPLATEFIRSTREMINDER: 'Email body - First reminder'
EMAILTEMPLATESECONDREMINDER: 'Email body - Second reminder'
EMAILTEMPLATE: 'Email body - Review due'
FIRSTREVIEWDAYSBEFORE: 'First review reminder # days before final review'
INHERIT: 'Inherit from parent page'
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>