Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

UPDATED: Moved module to use yaml configuration rather than php code

  • Loading branch information...
commit 82bf854ca8eaca3ef76a38bf6dd2e477e6be8af6 1 parent 3dc8ef3
@tractorcow authored
View
2  _config.php
@@ -1,3 +1 @@
<?php
-
-CommentsNotifications::init();
View
17 _config/CommentsNotifications.yml
@@ -0,0 +1,17 @@
+CommentsNotifications:
+ recipient: 'SiteConfig'
+ only_unmoderated: false
+ email_template: 'CommentNotificationsEmail'
+ email_sender: 'noreply@silverstripe.org'
+ email_subject: 'New comment notification'
+CommentingController:
+ extensions:
+ - 'CommentingControllerNotificationsExtension'
+SiteConfig:
+ extensions:
+ - 'CommentsNotificationsExtension'
+ - 'SiteConfigCommentsNotificationsExtension'
+SiteTree:
+ extensions:
+ - 'CommentsNotificationsExtension'
+ - 'PageCommentsNotificationsExtension'
View
66 code/CommentsNotifications.php
@@ -7,63 +7,12 @@
*/
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 configure_siteconfig() {
+ return Config::inst()->get('CommentsNotifications', 'recipient') === 'SiteConfig';
+ }
- 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;
- }
+ public static function configure_page() {
+ return Config::inst()->get('CommentsNotifications', 'recipient') === 'Page';
}
/**
@@ -73,12 +22,13 @@ public static function init() {
* @return string Email address, if available
*/
public static function get_recipient($parent) {
- switch(self::$recipient) {
+ $recipient = Config::inst()->get('CommentsNotifications', 'recipient');
+ switch($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;
+ default: return $recipient;
}
}
}
View
13 code/extensions/CommentingControllerNotificationsExtension.php
@@ -18,24 +18,25 @@ public function onAfterPostComment(Comment $comment) {
if(empty($recipient)) return;
// Check moderation status
- if(CommentsNotifications::$only_unmoderated && $comment->Moderated) return;
+ if(Config::inst()->get('CommentsNotifications', 'only_unmoderated') && $comment->Moderated) return;
// Generate email
$email = new Email();
- $email->setSubject(CommentsNotifications::$email_subject);
+ $email->setSubject(Config::inst()->get('CommentsNotifications', 'email_subject'));
$email->setTo($recipient);
- $email->setTemplate(CommentsNotifications::$email_template);
+ $email->setTemplate(Config::inst()->get('CommentsNotifications', 'email_template'));
$email->populateTemplate($comment);
// Corretly set sender and from as per email convention
+ $sender = Config::inst()->get('CommentsNotifications', 'email_sender');
if(!empty($comment->Email)) {
$email->setFrom($comment->Email);
$email->addCustomHeader ('Reply-To', $comment->Email);
} else {
- $email->setFrom(CommentsNotifications::$email_sender);
+ $email->setFrom($sender);
}
- $email->addCustomHeader('X-Sender', CommentsNotifications::$email_sender);
- $email->addCustomHeader('Sender', CommentsNotifications::$email_sender);
+ $email->addCustomHeader('X-Sender', $sender);
+ $email->addCustomHeader('Sender', $sender);
// Send
$email->send();
View
10 code/extensions/PageCommentsNotificationsExtension.php
@@ -6,9 +6,11 @@
class PageCommentsNotificationsExtension extends SiteTreeExtension {
public function updateSettingsFields(FieldList $fields) {
- $fields->addFieldToTab(
- 'Root.Settings',
- new EmailField('CommentNotificationEmail', 'Comment Notification Email', null, 255)
- );
+ if(CommentsNotifications::configure_page()) {
+ $fields->addFieldToTab(
+ 'Root.Settings',
+ new EmailField('CommentNotificationEmail', 'Comment Notification Email', null, 255)
+ );
+ }
}
}
View
10 code/extensions/SiteConfigCommentsNotificationsExtension.php
@@ -6,9 +6,11 @@
class SiteConfigCommentsNotificationsExtension extends DataExtension {
public function updateCMSFields(FieldList $fields) {
- $fields->addFieldToTab(
- 'Root.CommentNotifications',
- new EmailField('CommentNotificationEmail', 'Comment Notification Email', null, 255)
- );
+ if(CommentsNotifications::configure_siteconfig()) {
+ $fields->addFieldToTab(
+ 'Root.CommentNotifications',
+ new EmailField('CommentNotificationEmail', 'Comment Notification Email', null, 255)
+ );
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.