-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34d44ec
commit fe4a10e
Showing
8 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,35 @@ | |||
silverstripe-comments-notifications | # Page comments email notification module for Silverstripe | ||
=================================== | |||
|
|
||
Simple email notifications for comments posted against the Comments module | This mobule provides simple email notifications for page comments | ||
|
|||
## Credits and Authors | |||
|
|||
* Damian Mooyman - <https://github.com/tractorcow/silverstripe-comments-notifications> | |||
|
|||
## License | |||
|
|||
* TODO | |||
|
|||
## Requirements | |||
|
|||
* SilverStripe 3.1 | |||
* PHP 5.3 | |||
* Comments module - <https://github.com/silverstripe/silverstripe-comments> | |||
|
|||
## Installation Instructions | |||
|
|||
* Extract all files into the 'comments-notifications' folder under your Silverstripe root. | |||
|
|||
## Configuration | |||
|
|||
Set CommentsNotifications::$recipient to one of | |||
|
|||
* SiteConfig (configure the email under settings globally) | |||
* Page (configure the recipient per page) | |||
* Admin (uses the admin email address) | |||
* Disabled | |||
* any email address (use this explicit email address) | |||
|
|||
## Need more help? | |||
|
|||
Message or email me at damian.mooyman@gmail.com or, well, read the code! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
<?php | |||
|
|||
CommentsNotifications::init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,84 @@ | |||
<?php | |||
|
|||
/** | |||
* Bootstrapping / configuration class for comments notification | |||
* | |||
* @author Damo | |||
*/ | |||
class CommentsNotifications { | |||
|
|||
/** | |||
* Flag indicating whether or not to autoload this module with its | |||
* required extensions | |||
* | |||
* @var boolean | |||
*/ | |||
public static $enabled = true; | |||
|
|||
/** | |||
* Determine the recipient of the notification emails. One of: | |||
* <ul> | |||
* <li>SiteConfig - Set email in SiteConfig</li> | |||
* <li>Page - Set email per page</li> | |||
* <li>Disabled - No emails</li> | |||
* <li>Admin - Use admin email</li> | |||
* <li>An email address - Hard coded value</li> | |||
* </ul> | |||
* | |||
* @var string | |||
*/ | |||
public static $recipient = 'SiteConfig'; | |||
// Probably could put this into yaml config files, but the manifest isn't | |||
// loaded at the correct time. | |||
|
|||
/** | |||
* Flag indicating whether we should notify the user only of unmoderated | |||
* entries, or all entries. | |||
* | |||
* @var boolean | |||
*/ | |||
public static $only_unmoderated = false; | |||
|
|||
public static $email_template = 'CommentNotificationsEmail'; | |||
|
|||
public static $email_sender = 'noreply@silverstripe.org'; | |||
|
|||
public static $email_subject = 'New comment notification'; | |||
|
|||
public static function init() { | |||
if(!self::$enabled) return; | |||
|
|||
if(!class_exists('CommentingController')) { | |||
user_error('Please install the comments module - https://github.com/silverstripe/silverstripe-comments', E_USER_ERROR); | |||
} | |||
|
|||
CommentingController::add_extension('CommentingControllerNotificationsExtension'); | |||
|
|||
switch(self::$recipient) { | |||
case 'SiteConfig': | |||
SiteConfig::add_extension('CommentsNotificationsExtension'); | |||
SiteConfig::add_extension('SiteConfigCommentsNotificationsExtension'); | |||
break; | |||
case 'Page': | |||
Page::add_extension('CommentsNotificationsExtension'); | |||
Page::add_extension('PageCommentsNotificationsExtension'); | |||
break; | |||
} | |||
} | |||
|
|||
/** | |||
* Returns the email address that should be notified of comments to the given page | |||
* | |||
* @param DataObject $parent Parent object | |||
* @return string Email address, if available | |||
*/ | |||
public static function get_recipient($parent) { | |||
switch(self::$recipient) { | |||
case 'Disabled': return null; | |||
case 'SiteConfig': return SiteConfig::current_site_config()->CommentNotificationEmail; | |||
case 'Page': return $parent->CommentNotificationEmail; | |||
case 'Admin': return Email::getAdminEmail(); | |||
default: return self::$recipient; | |||
} | |||
} | |||
} |
43 changes: 43 additions & 0 deletions
43
code/extensions/CommentingControllerNotificationsExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
<?php | |||
|
|||
/** | |||
* @author Damian Mooyman | |||
* @see CommentingController | |||
*/ | |||
class CommentingControllerNotificationsExtension extends Extension { | |||
|
|||
/** | |||
* Notify admin of new comment | |||
* | |||
* @param Comment $comment | |||
*/ | |||
public function onAfterPostComment(Comment $comment) { | |||
|
|||
// Determine recipient | |||
$recipient = CommentsNotifications::get_recipient($comment->getParent()); | |||
if(empty($recipient)) return; | |||
|
|||
// Check moderation status | |||
if(CommentsNotifications::$only_unmoderated && $comment->Moderated) return; | |||
|
|||
// Generate email | |||
$email = new Email(); | |||
$email->setSubject(CommentsNotifications::$email_subject); | |||
$email->setTo($recipient); | |||
$email->setTemplate(CommentsNotifications::$email_template); | |||
$email->populateTemplate($comment); | |||
|
|||
// Corretly set sender and from as per email convention | |||
if(!empty($comment->Email)) { | |||
$email->setFrom($comment->Email); | |||
$email->addCustomHeader ('Reply-To', $comment->Email); | |||
} else { | |||
$email->setFrom(CommentsNotifications::$email_sender); | |||
} | |||
$email->addCustomHeader('X-Sender', CommentsNotifications::$email_sender); | |||
$email->addCustomHeader('Sender', CommentsNotifications::$email_sender); | |||
|
|||
// Send | |||
$email->send(); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
<?php | |||
|
|||
/** | |||
* @author Damian Mooyman | |||
*/ | |||
class CommentsNotificationsExtension extends DataExtension { | |||
static $db = array( | |||
'CommentNotificationEmail' => 'Varchar(255)' | |||
); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,14 @@ | |||
<?php | |||
|
|||
/** | |||
* @author Damian Mooyman | |||
*/ | |||
class PageCommentsNotificationsExtension extends SiteTreeExtension { | |||
|
|||
public function updateSettingsFields(FieldList $fields) { | |||
$fields->addFieldToTab( | |||
'Root.Settings', | |||
new EmailField('CommentNotificationEmail', 'Comment Notification Email', null, 255) | |||
); | |||
} | |||
} |
14 changes: 14 additions & 0 deletions
14
code/extensions/SiteConfigCommentsNotificationsExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,14 @@ | |||
<?php | |||
|
|||
/** | |||
* @author Damian Mooyman | |||
*/ | |||
class SiteConfigCommentsNotificationsExtension extends DataExtension { | |||
|
|||
public function updateCMSFields(FieldList $fields) { | |||
$fields->addFieldToTab( | |||
'Root.CommentNotifications', | |||
new EmailField('CommentNotificationEmail', 'Comment Notification Email', null, 255) | |||
); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head></head> | |||
<body> | |||
<p>A new comment has been posted.</p> | |||
<% if Link %> | |||
<p><a href="$Link">Click here to view this entry</a></p> | |||
<% end_if %> | |||
<p>Comment details</p> | |||
<dl> | |||
<dt>Date Posted:</dt> | |||
<dd>$Created.Nice</dd> | |||
<% if Name %> | |||
<dt>Name:</dt> | |||
<dd>$Name.XML</dd> | |||
<% end_if %> | |||
<% if Email %> | |||
<dt>Email:</dt> | |||
<dd>$Email.XML</dd> | |||
<% end_if %> | |||
<% if URL %> | |||
<dt>URL:</dt> | |||
<dd>$URL.XML</dd> | |||
<% end_if %> | |||
<% if Comment %> | |||
<dt>Comment:</dt> | |||
<dd>$Comment.XML</dd> | |||
<% end_if %> | |||
</dl> | |||
</body> | |||
</html> |