Skip to content

Commit

Permalink
ADDED: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tractorcow committed Jan 31, 2013
1 parent 34d44ec commit fe4a10e
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 3 deletions.
37 changes: 34 additions & 3 deletions README.md
@@ -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!
3 changes: 3 additions & 0 deletions _config.php
@@ -0,0 +1,3 @@
<?php

CommentsNotifications::init();
84 changes: 84 additions & 0 deletions code/CommentsNotifications.php
@@ -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 code/extensions/CommentingControllerNotificationsExtension.php
@@ -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();
}
}
10 changes: 10 additions & 0 deletions code/extensions/CommentsNotificationsExtension.php
@@ -0,0 +1,10 @@
<?php

/**
* @author Damian Mooyman
*/
class CommentsNotificationsExtension extends DataExtension {
static $db = array(
'CommentNotificationEmail' => 'Varchar(255)'
);
}
14 changes: 14 additions & 0 deletions code/extensions/PageCommentsNotificationsExtension.php
@@ -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 code/extensions/SiteConfigCommentsNotificationsExtension.php
@@ -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)
);
}
}
31 changes: 31 additions & 0 deletions templates/CommentNotificationsEmail.ss
@@ -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>

0 comments on commit fe4a10e

Please sign in to comment.