NEW Send one notification email per author #7

This commit is contained in:
Stig Lindqvist 2014-02-25 15:54:27 +13:00
parent b3c777a579
commit 6b337496cf
8 changed files with 271 additions and 297 deletions

View File

@ -1,154 +0,0 @@
<?php
/**
* Daily task to send emails to the owners of content items
* when the review date rolls around
*
*
* @todo create a page cache for the inherited so that we dont unneccesary need to look up parent pages
* @package contentreview
*/
class ContentReviewEmails extends BuildTask {
/**
* Holds a cached array for looking up members via their ID
*
* @var array
*/
protected static $member_cache = array();
/**
*
* @param SS_HTTPRequest $request
*/
public function run($request) {
// Disable subsite filter (if installed)
if (ClassInfo::exists('Subsite')) {
$oldSubsiteState = Subsite::$disable_subsite_filter;
Subsite::$disable_subsite_filter = true;
}
$overduePages = array();
$now = class_exists('SS_Datetime') ? SS_Datetime::now()->URLDate() : SSDatetime::now()->URLDate();
// First grab all the pages with a custom setting
//$customSettingsPages = Page::get('Page')
// ->leftJoin('Group_SiteTreeContentReview', '"SiteTree"."ID" = "OwnerGroups"."SiteTreeID"', 'OwnerGroups')
// ->leftJoin('Member_SiteTreeContentReview', '"SiteTree"."ID" = "OwnerUsers"."SiteTreeID"', "OwnerUsers")
// ->where('"SiteTree"."ContentReviewType" = \'Custom\' AND "SiteTree"."NextReviewDate" <= \''.$now.'\' AND' .
// ' ("OwnerGroups"."ID" IS NOT NULL OR "OwnerUsers"."ID" IS NOT NULL)')
//;
//$this->getOverduePagesForOwners($customSettingsPages, $overduePages);
// Then grab all the pages with that inherits their settings
//$inheritedSettingsPages = Page::get('Page')
// ->leftJoin('Group_SiteTreeContentReview', '"SiteTree"."ID" = "OwnerGroups"."SiteTreeID"', 'OwnerGroups')
// ->leftJoin('Member_SiteTreeContentReview', '"SiteTree"."ID" = "OwnerUsers"."SiteTreeID"', "OwnerUsers")
// ->where('"SiteTree"."ContentReviewType" = \'Inherit\'')
//;
$pages = Page::get();
$this->getOverduePagesForOwners($pages, $overduePages);
// Lets 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);
}
// Revert subsite filter (if installed)
if(ClassInfo::exists('Subsite')) {
Subsite::$disable_subsite_filter = $oldSubsiteState;
}
}
/**
*
* @param SS_list $pages
* @param array &$pages
* @return array
*/
protected function getOverduePagesForOwners(SS_list $pages, array &$overduePages) {
foreach($pages as $page) {
if(!$page->isContentReviewOverdue()) {
continue;
}
$option = $page->getOptions();
foreach($option->ContentReviewOwners() as $owner) {
if(!isset(self::$member_cache[$owner->ID])) {
self::$member_cache[$owner->ID] = $owner;
}
if(!isset($overduePages[$owner->ID])) {
$overduePages[$owner->ID] = array();
}
$overduePages[$owner->ID][] = $page;
}
}
return $overduePages;
}
/**
*
* @param int $owner
* @param array $pages
*/
protected function notifyOwner($ownerID, array $pages) {
$owner = self::$member_cache[$ownerID];
echo "----- ".$owner->Email." -----".PHP_EOL;
foreach($pages as $page) {
echo $page->Title.PHP_EOL;
}
}
/**
*
* @param SS_List $pages
* @return void
*/
protected function notify(SS_List $pages) {
if(!$pages) {
return;
}
if(!$pages->Count()) {
return;
}
foreach($pages as $page) {
// Resolve the content owner groups and members to a single list of members
$owners = $page->ContentReviewOwners();
if(!$owners->count()) {
continue;
}
$sender = Security::findAnAdministrator();
foreach($owners as $recipient) {
$subject = sprintf(_t('ContentReviewEmails.SUBJECT', 'Page %s due for content review'), $page->Title);
$email = new Email();
$email->setTo($recipient->Email);
$email->setFrom(($sender->Email) ? $sender->Email : Email::getAdminEmail());
$email->setTemplate('ContentReviewEmails');
$email->setSubject($subject);
$email->populateTemplate(array(
"PageCMSLink" => "admin/pages/edit/show/".$page->ID,
"Recipient" => $recipient,
"Sender" => $sender,
"Page" => $page,
"StageSiteLink" => Controller::join_links($page->Link(), "?stage=Stage"),
"LiveSiteLink" => Controller::join_links($page->Link(), "?stage=Live"),
));
//$email->send();
$message = '<strong>'._t('ContentReviewEmails.EMAIL_HEADING','Page due for review').'</strong><br/>'.
'The page "'.$page->Title.'" is due for review today by you.<br/>
<a href="admin/pages/edit/show/'.$page->ID.'">'. _t('ContentReviewEmails.REVIEWPAGELINK','Review the page in the CMS') .'</a> &mdash;
<a href="#">'. _t('ContentReviewEmails.VIEWPUBLISHEDLINK','View this page on the website') .'</a>';
if(class_exists('Notification')) {
// Notification::notify($recipient, $message);
}
// echo $page->Title.' - '.$recipient->Email.PHP_EOL;
}
}
}
}

View File

@ -0,0 +1,101 @@
<?php
/**
* Daily task to send emails to the owners of content items
* when the review date rolls around
*
*
* @todo create a page cache for the inherited so that we dont unneccesary need to look up parent pages
* @package contentreview
*/
class ContentReviewEmails extends BuildTask {
/**
* Holds a cached array for looking up members via their ID
*
* @var array
*/
protected static $member_cache = array();
/**
*
* @param SS_HTTPRequest $request
*/
public function run($request) {
// Disable subsite filter (if installed)
if (ClassInfo::exists('Subsite')) {
$oldSubsiteState = Subsite::$disable_subsite_filter;
Subsite::$disable_subsite_filter = true;
}
$overduePages = array();
$now = class_exists('SS_Datetime') ? SS_Datetime::now()->URLDate() : SSDatetime::now()->URLDate();
// First grab all the pages with a custom setting
$pages = Page::get('Page')->where('"SiteTree"."NextReviewDate" <= \''.$now.'\'');
$this->getOverduePagesForOwners($pages, $overduePages);
// Lets 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);
}
// Revert subsite filter (if installed)
if(ClassInfo::exists('Subsite')) {
Subsite::$disable_subsite_filter = $oldSubsiteState;
}
}
/**
*
* @param SS_list $pages
* @param array &$pages
* @return array
*/
protected function getOverduePagesForOwners(SS_list $pages, array &$overduePages) {
foreach($pages as $page) {
if(!$page->canBeReviewedBy()) {
continue;
}
$option = $page->getOptions();
foreach($option->ContentReviewOwners() as $owner) {
if(!isset(self::$member_cache[$owner->ID])) {
self::$member_cache[$owner->ID] = $owner;
}
if(!isset($overduePages[$owner->ID])) {
$overduePages[$owner->ID] = new ArrayList();
}
$overduePages[$owner->ID]->push($page);
}
}
return $overduePages;
}
/**
*
* @param int $owner
* @param array $pages
*/
protected function notifyOwner($ownerID, SS_List $pages) {
$owner = self::$member_cache[$ownerID];
$sender = Security::findAnAdministrator();
$senderEmail = ($sender->Email) ? $sender->Email : Config::inst()->get('Email', 'admin_email');
$subject = _t('ContentReviewEmails.SUBJECT', 'Page(s) are due for content review');
$email = new Email();
$email->setTo($owner->Email);
$email->setFrom($senderEmail);
$email->setTemplate('ContentReviewEmail');
$email->setSubject($subject);
$email->populateTemplate(array(
"Recipient" => $owner,
"Sender" => $sender,
"Pages" => $pages
));
$email->send();
}
}

View File

@ -0,0 +1,24 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head></head>
<body>
<table id="Content" cellspacing="1" cellpadding="10">
<tbody>
<tr>
<td scope="row" colspan="2" class="typography">
<h2><% _t('ContentReviewEmails.EMAIL_HEADING','Page(s) due for review') %></h2>
<p>There are $Pages.Count pages that are due for review today by you.</p>
</td>
</tr>
<% loop Pages %>
<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>

View File

@ -1,24 +0,0 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
</head>
<body>
<table id="Content" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td scope="row" colspan="2" class="typography">
<h2><% _t('ContentReviewEmails.EMAIL_HEADING','Page due for review') %></h2>
<p>The page $Page.Title is due for review today by you</p>
<h2>Actions</h2>
<ul>
<li><a href="$PageCMSLink"><% _t('ContentReviewEmails.REVIEWPAGELINK','Review the page in the CMS') %></a></li>
<li><a href="$LiveSiteLink"><% _t('ContentReviewEmails.VIEWPUBLISHEDLINK','View this page on the website') %></a></li>
</ul>
</td>
</tr>
</tbody>
</table>
</body>
</html>

View File

@ -2,14 +2,28 @@
class ContentReviewNotificationTest extends SapphireTest {
public static $fixture_file = 'contentreview/tests/ContentReviewTest.yml';
public function testContentReviewEmails() {
$this->markTestIncomplete();
SS_Datetime::set_mock_now('2010-02-14 12:00:00');
SS_Datetime::set_mock_now('2010-02-24 12:00:00');
// This propagates the next review date to 'contact-child' page from the parent page
$childParentPage = $this->objFromFixture('Page', 'contact');
$childParentPage->NextReviewDate = '2010-02-23';
$childParentPage->write();
$task = new ContentReviewEmails();
$task->run(new SS_HTTPRequest('GET', '/dev/tasks/ContentReviewEmails'));
$this->assertEmailSent('author@example.com', null, sprintf(_t('ContentReviewEmails.SUBJECT', 'Page %s due for content review'), 'Staff'));
$expectedSubject = _t('ContentReviewEmails.SUBJECT', 'Page(s) are due for content review');
$email = $this->findEmail('author@example.com', null, $expectedSubject);
$this->assertNotNull($email, 'Email haven\'t been sent.');
$this->assertContains('There are 3 pages that are due for review today by you.', $email['htmlContent']);
$this->assertContains('Staff', $email['htmlContent']);
$this->assertContains('Contact Us', $email['htmlContent']);
$this->assertContains('Contact Us Child', $email['htmlContent']);
SS_Datetime::clear_mock_now();
}

View File

@ -36,11 +36,13 @@ Member:
Email: visitor@example.com
Page:
# Cant be reviewed, no owners
home:
Title: Home
ContentReviewType: Custom
NextReviewDate: 2010-02-01
ReviewPeriodDays: 10
# Cant be reviewed, no owners
about:
Title: About Us
ContentReviewType: Custom
@ -62,10 +64,12 @@ Page:
Title: Contact Us Child
ContentReviewType: inherit
ParentID: =>Page.contact
# Cant be reviewed, no NextReviewDate
no-review:
Title: Page without review date
ContentReviewType: Custom
ContentReviewUsers: =>Member.author
# Cant be reviewed, no NextReviewDate
group-owned:
Title: Page owned by group
ContentReviewType: Custom

View File

@ -4,103 +4,109 @@ class SiteTreeContentReviewTest extends FunctionalTest {
public static $fixture_file = 'contentreview/tests/ContentReviewTest.yml';
// public function testPermissionsExists() {
// $perms = singleton('SiteTreeContentReview')->providePermissions();
// $this->assertTrue(isset($perms['EDIT_CONTENT_REVIEW_FIELDS']));
// }
//
// public function testUserWithPermissionCanEdit() {
// $editor = $this->objFromFixture('Member', 'editor');
// $this->logInAs($editor);
// $page = new Page();
// $fields = $page->getSettingsFields();
// $this->assertNotNull($fields->dataFieldByName('NextReviewDate'));
// }
//
// public function testUserWithoutPermissionCannotEdit() {
// $author = $this->objFromFixture('Member', 'author');
// $this->logInAs($author);
// $page = new Page();
// $fields = $page->getSettingsFields();
// $this->assertNull($fields->dataFieldByName('NextReviewDate'));
// }
//
// public function testAutomaticallyToNotSetReviewDate() {
// $editor = $this->objFromFixture('Member', 'editor');
// $this->logInAs($editor);
//
// $page = new Page();
// $page->ReviewPeriodDays = 10;
// $page->write();
// $this->assertTrue($page->doPublish());
// $this->assertEquals(null, $page->NextReviewDate);
// }
//
// public function testAddReviewNote() {
// $author = $this->objFromFixture('Member', 'author');
// $page = $this->objFromFixture('Page', 'home');
// $page->addReviewNote($author, 'This is a message');
//
// // Get the page again to make sure it's not only cached in memory
// $homepage = $this->objFromFixture('Page', 'home');
// $this->assertEquals(1, $homepage->ReviewLogs()->count());
// $this->assertEquals('This is a message', $homepage->ReviewLogs()->first()->Note);
// }
//
// public function testGetContentReviewOwners() {
// $page = $this->objFromFixture('Page', 'group-owned');
// $owners = $page->ContentReviewOwners();
// $this->assertEquals(1, $owners->count());
// $this->assertEquals('author@example.com', $owners->first()->Email);
// }
//
// public function testCanNotBeReviewBecauseNoReviewDate() {
// SS_Datetime::set_mock_now('2010-01-01 12:00:00');
// $author = $this->objFromFixture('Member', 'author');
// $page = $this->objFromFixture('Page', 'no-review');
// // page 'no-review' is owned by author, but there is no review date
// $this->assertFalse($page->canBeReviewedBy($author));
// }
//
// public function testCanNotBeReviewedBecauseInFuture() {
// SS_Datetime::set_mock_now('2010-01-01 12:00:00');
// $author = $this->objFromFixture('Member', 'author');
// $page = $this->objFromFixture('Page', 'staff');
// // page 'staff' is owned by author, but the review date is in the future
// $this->assertFalse($page->canBeReviewedBy($author));
// }
//
// public function testCanNotBeReviewedByUser() {
// SS_Datetime::set_mock_now('2010-03-01 12:00:00');
// $author = $this->objFromFixture('Member', 'author');
// $page = $this->objFromFixture('Page', 'home');
// // page 'home' doesnt have any owners
// $this->assertFalse($page->canBeReviewedBy($author));
// }
//
// public function testCanBeReviewedByUser() {
// SS_Datetime::set_mock_now('2010-03-01 12:00:00');
// $author = $this->objFromFixture('Member', 'author');
// $page = $this->objFromFixture('Page', 'staff');
// // page 'staff' is owned by author
// $this->assertTrue($page->canBeReviewedBy($author));
// }
//
// public function testCanNotBeReviewedByGroup() {
// SS_Datetime::set_mock_now('2010-03-01 12:00:00');
// $author = $this->objFromFixture('Member', 'editor');
// $page = $this->objFromFixture('Page', 'contact');
// // page 'contact' is owned by the authorgroup
// $this->assertFalse($page->canBeReviewedBy($author));
// }
//
// public function testCanBeReviewedByGroup() {
// SS_Datetime::set_mock_now('2010-03-01 12:00:00');
// $author = $this->objFromFixture('Member', 'author');
// $page = $this->objFromFixture('Page', 'contact');
// // page 'contact' is owned by the authorgroup
// $this->assertTrue($page->canBeReviewedBy($author));
// }
public function testPermissionsExists() {
$perms = singleton('SiteTreeContentReview')->providePermissions();
$this->assertTrue(isset($perms['EDIT_CONTENT_REVIEW_FIELDS']));
}
public function testUserWithPermissionCanEdit() {
$editor = $this->objFromFixture('Member', 'editor');
$this->logInAs($editor);
$page = new Page();
$fields = $page->getSettingsFields();
$this->assertNotNull($fields->dataFieldByName('NextReviewDate'));
}
public function testUserWithoutPermissionCannotEdit() {
$author = $this->objFromFixture('Member', 'author');
$this->logInAs($author);
$page = new Page();
$fields = $page->getSettingsFields();
$this->assertNull($fields->dataFieldByName('NextReviewDate'));
}
public function testAutomaticallyToNotSetReviewDate() {
$editor = $this->objFromFixture('Member', 'editor');
$this->logInAs($editor);
$page = new Page();
$page->ReviewPeriodDays = 10;
$page->write();
$this->assertTrue($page->doPublish());
$this->assertEquals(null, $page->NextReviewDate);
}
public function testAddReviewNote() {
$author = $this->objFromFixture('Member', 'author');
$page = $this->objFromFixture('Page', 'home');
$page->addReviewNote($author, 'This is a message');
// Get the page again to make sure it's not only cached in memory
$homepage = $this->objFromFixture('Page', 'home');
$this->assertEquals(1, $homepage->ReviewLogs()->count());
$this->assertEquals('This is a message', $homepage->ReviewLogs()->first()->Note);
}
public function testGetContentReviewOwners() {
$page = $this->objFromFixture('Page', 'group-owned');
$owners = $page->ContentReviewOwners();
$this->assertEquals(1, $owners->count());
$this->assertEquals('author@example.com', $owners->first()->Email);
}
public function testCanNotBeReviewBecauseNoReviewDate() {
SS_Datetime::set_mock_now('2010-01-01 12:00:00');
$author = $this->objFromFixture('Member', 'author');
$page = $this->objFromFixture('Page', 'no-review');
// page 'no-review' is owned by author, but there is no review date
$this->assertFalse($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
public function testCanNotBeReviewedBecauseInFuture() {
SS_Datetime::set_mock_now('2010-01-01 12:00:00');
$author = $this->objFromFixture('Member', 'author');
$page = $this->objFromFixture('Page', 'staff');
// page 'staff' is owned by author, but the review date is in the future
$this->assertFalse($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
public function testCanNotBeReviewedByUser() {
SS_Datetime::set_mock_now('2010-03-01 12:00:00');
$author = $this->objFromFixture('Member', 'author');
$page = $this->objFromFixture('Page', 'home');
// page 'home' doesnt have any owners
$this->assertFalse($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
public function testCanBeReviewedByUser() {
SS_Datetime::set_mock_now('2010-03-01 12:00:00');
$author = $this->objFromFixture('Member', 'author');
$page = $this->objFromFixture('Page', 'staff');
// page 'staff' is owned by author
$this->assertTrue($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
public function testCanNotBeReviewedByGroup() {
SS_Datetime::set_mock_now('2010-03-01 12:00:00');
$author = $this->objFromFixture('Member', 'editor');
$page = $this->objFromFixture('Page', 'contact');
// page 'contact' is owned by the authorgroup
$this->assertFalse($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
public function testCanBeReviewedByGroup() {
SS_Datetime::set_mock_now('2010-03-01 12:00:00');
$author = $this->objFromFixture('Member', 'author');
$page = $this->objFromFixture('Page', 'contact');
// page 'contact' is owned by the authorgroup
$this->assertTrue($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
public function testCanBeReviewedFromInheritedSetting() {
SS_Datetime::set_mock_now('2013-03-01 12:00:00');
@ -112,25 +118,28 @@ class SiteTreeContentReviewTest extends FunctionalTest {
$page = $this->objFromFixture('Page', 'contact-child');
$this->assertTrue($page->canBeReviewedBy($author));
SS_Datetime::clear_mock_now();
}
// public function testReviewActionVisibleForAuthor() {
// SS_Datetime::set_mock_now('2020-03-01 12:00:00');
// $page = $this->objFromFixture('Page', 'contact');
// $author = $this->objFromFixture('Member', 'author');
// $this->logInAs($author);
//
// $fields = $page->getCMSActions();
// $this->assertNotNull($fields->fieldByName('action_reviewed'));
// }
//
// public function testReviewActionNotVisibleForEditor() {
// SS_Datetime::set_mock_now('2020-03-01 12:00:00');
// $page = $this->objFromFixture('Page', 'contact');
// $author = $this->objFromFixture('Member', 'editor');
// $this->logInAs($author);
//
// $fields = $page->getCMSActions();
// $this->assertNull($fields->fieldByName('action_reviewed'));
// }
public function testReviewActionVisibleForAuthor() {
SS_Datetime::set_mock_now('2020-03-01 12:00:00');
$page = $this->objFromFixture('Page', 'contact');
$author = $this->objFromFixture('Member', 'author');
$this->logInAs($author);
$fields = $page->getCMSActions();
$this->assertNotNull($fields->fieldByName('action_reviewed'));
SS_Datetime::clear_mock_now();
}
public function testReviewActionNotVisibleForEditor() {
SS_Datetime::set_mock_now('2020-03-01 12:00:00');
$page = $this->objFromFixture('Page', 'contact');
$author = $this->objFromFixture('Member', 'editor');
$this->logInAs($author);
$fields = $page->getCMSActions();
$this->assertNull($fields->fieldByName('action_reviewed'));
SS_Datetime::clear_mock_now();
}
}