Jump to:

1036 Posts in 810 Topics by 315 members

Forum Module

SilverStripe Forums » Forum Module » Notifications of New Postings by Email - Module

Discuss the Forum Module.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 3750 Views
  • the-tech-guy
    Avatar
    Community Member
    13 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Hey great work Dave!

    Any update on this? I have been trying to install the module but keep getting the error:

    Fatal error: Class 'DataObjectDecorator' not found in /home/destinat/public_html/client-login/ForumModerationEmails/code/ForumEmailPostDecorator.php on line 2

  • davepolyester
    Avatar
    Community Member
    47 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Hey Tech-Guy,

    Thanks mate. Yeah I did make further progress on this for my own purposes but it required some significant changes to forum module itself to achieve what I wanted.

    In a nutshell, I replaced Moderators with Subscribers so a Forum Member can subscribe to any forum and receive notifications whenever a new thread is started or a reply is made. This means you can no longer have multiple individual moderators for various forums, only forum Admins who can moderate all forums. (In my situation this isn't a problem.)

    I've hesitated to post my work in the forum as I'm no expert and my work is very hacky, definitely not a clean extension of the forum module as it should be.

    Also, my work really only applies to SS2.4 which leads us to the issue you've encountered. My guess is that you're trying to use the ForumModerationEmails extension on an SS3 site, but I don't think the original extension is compatible with SS3.

    Other than that, I'm not sure what would be causing the problem based on the info you've provided.

  • davepolyester
    Avatar
    Community Member
    47 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Actually I may have just spotted your problem. I believe the root folder for the module should be SilverstripeForumModerationEmails, not ForumModerationEmails

  • Arbee
    Avatar
    Community Member
    18 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Dave, I'd be really interested in finding out more about how you coded the forum. My site is live but I have requests from users in wanting to get messages when both new posts and replies are added. I don't want to make them all moderators, in fact, it would be better to have no moderators. I would need to restrict users to only receive notice from forums they are allowed to access, is that a problem? All I really need is a subscribe button in each forum so an email notification is sent when any post is submitted within that forum. Make sense? or am I talking in circles?

  • the-tech-guy
    Avatar
    Community Member
    13 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Hi Dave,

    Thanks for the reply, I just checked and the root folder that I have the module installed in is named"ForumModerationEmails"

  • davepolyester
    Avatar
    Community Member
    47 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Thanks for your interest Arbee and Tech-Guy. Here's my further development work, even if it may now be redundant in the age of SS3.1.

    Since my last major code post, I've achieved the following:
    • Forum Community Members are able to subscribe/unsubscribe at will to any forum.
    • Notification emails are sent to all subscribers when a new post or reply is made to a forum.
    • Those with a forum rank of Administrator receive notification emails whenever any post is made to any forum, no subscription necessary.

    For starters, here's my latest revision of ForumEmailPostDecorator.php:

    class ForumEmailPostDecorator extends DataObjectDecorator {
       
       //Email all Forum Administrators and just the Subscribers to the particular forum when a post is made.
       //----------------------------------------------------------------------------------------------------
       function onAfterWrite() {
          // CHECK IF THIS IS A NEW POST. DON'T SEND EMAIL IF THE POST IS BEING MOVED OR EDITED!
          if(!$this->owner->LastEdited) {
             // CHECK IF THIS POST'S TOPIC ID MATCHES ITS OWN ID. THIS PREVENTS DUPLICATE EMAILS BEING SENT
             // WHEN A NEW TOPIC IS CREATED, CAUSED BY THE TWO WRITE STATEMENTS IN FORUM.PHP.
             if($this->owner->TopicID != $this->owner->ID) {
          
           // GET ALL MEMBERS WITH THE FORUMRANK OF "COMMUNITY MEMBER"
           $commembers = DataObject::get("Member","ForumRank = 'Community Member'", "", "");
           // GET ALL MEMBERS WITH THE FORUMRANK OF "ADMINISTRATOR"
           $administrators = DataObject::get("Member","ForumRank = 'Administrator'", "", "");
          
           // EMAIL THE COMMUNITY MEMBERS
           if($commembers) {
              foreach($commembers as $member) {
                 // ONLY EMAIL MEMBERS SUBSCRIBED TO THIS FORUM
                 if($member->isSubscribedToForum($this->owner->Forum())) {
                    $this->createEmail($member);
                 }
              }
              parent::onAfterWrite();
           }
          
          
           // EMAIL THE ADMINISTRATORS
           if($administrators) {
              foreach($administrators as $member) {
                    $this->createEmail($member);
                 }
              parent::onAfterWrite();
             }
          }
       }
       }
       
       //CREATE THE FORUM NOTIFICATION EMAIL
       function createEmail($member)
       {
          error_log("LOG MESSAGE FROM FORUM EMAIL POST DECORATOR LINK IS ".$this->owner->AbsoluteLink());
          $email = new Email();
          $email->setFrom('webmaster@yourdomain.co.nz');
          $email->addCustomHeader('Reply-To', 'no-reply@yourdomain.co.nz');
          $email->setTo($member->Email);
          $email->setSubject($this->owner->Title . ' | Your Website Name ' . $this->owner->Forum()->Title . ' Forum');
          $email->setTemplate('Forum_ModeratorNotificationV2');
          $email->populateTemplate(array(
             'Recipient' => $member->FirstName,
             'AbsoluteLink' => $this->owner->AbsoluteLink(),
             'Title' => $this->owner->Title,
             'Content' => $this->owner->Content,
             'Author' => $this->owner->Author()->Nickname,
             'Forum' => $this->owner->Forum()->Title
          ));
          $email->send();
       }
    }

    Next, the updated email template, Forum_ModeratorNotificationV2.ss

    <h3>Forum Post Notification: <em>$Forum</em></h3>

    <hr />
    <table cellpadding="10px">
    <tr>
    <td bgcolor="#EEE">
    <h4><a href="$AbsoluteLink" title="Go to the $Title page" class="$LinkingMode">$Title</a></h4>
    <pre>
    $Content
    </pre>
    <p>
    <b><em>$Author</em></b>
    </p>
    <h5><a href="$AbsoluteLink" title="Go to the $Title page" class="$LinkingMode">Click here to reply to this post</a></h5>
    </td>
    </tr>
    </table>

    <hr />
    <h6 style="font-size:0.8em;">NOTE: This is an automated email, please do not reply. You have received this email because you are subscribed to this forum ($Forum) or you are a Forums Administrator.</h6>

    Now to the changes to the Forum module itself. This is where things get really dirty, and I apologise in advance for any offense caused by my neolithic butchering. First some justification:
    I decided the easiest way to set up forum email subscriptions was to set up a many_many relationship of Subscribers to Forums, in exactly the same as the way Moderators are controlled. However I soon discovered a serious flaw in my plan: SS2.4 only allows one many_many relationship, so I couldn't just duplicate all the existing Moderators code and use that as the basis of my Subscribers system.
    I then attempted to use many_many_extraFields to get around the problem. The plan was to change Moderators to Subscribers, then add a Boolean extraField called Moderator so that anyone who subscribed to a Forum could then be made a Moderator for that forum. However, despite many hours and much lost hair, I could find no way to actually populate my extraField with any data. (Now would be a good time to point out the obvious: I'm no developer, just a designer who's keen and running blind!)
    Finally, I decided to ditch the Moderators thing altogether and replace it with Subscribers as the project I was doing this for was an internal company forum system that required very little active moderation, if any.

    So, excuses made and reasoning explained, here are my changes to the Forum Module code.

    In Forum.php:

    • Replace Moderator and Moderators with Subscriber and Subscribers in initial declarations:

    static $has_one = array(
          //"Moderator" => "Member",
          "Group" => "Group",
          "Category" => "ForumCategory",
          "Subscriber" => "Member"/*--- DBS - Subscriber to forum for emails. ---*/
       );
       
       static $many_many = array(
          //'Moderators' => 'Member',
          'Subscribers' => 'Member' /*--- DBS - Subscribers to forum for emails. ---*/
       );


    • Add this function before the Forum_Controller:

    //---------DBS: Is current member subscribed to this forum?---------
       
       function MemberIsSubscribed ($member = null) {
          if(!$member) $member = Member::CurrentMember();
          $idList = $this->Subscribers()->getIDList();
          return in_array($member->ID, $idList);
       }
    }


    • Add these functions to the end of the Forum_Controller:

    /* ---- DBS - Forum Subscription Code --- */
       function subscribeToForum() {
          $member = Member::CurrentMember();
          if(!$member) return false;
          $this->dataRecord->Subscribers()->add($member);
          Director::redirectBack();
          return;
       }
       
       function unsubscribeFromForum() {
          $member = Member::CurrentMember();
          if(!$member) return false;
          $this->dataRecord->Subscribers()->remove($member);
          Director::redirectBack();
          return;
       }

    In Post.php:

    • Add this function to Post (just a duplicate of existing IsModerator function):

    /* ---- DBS - Is Subscriber function ---*/
       function IsSubscriber(){
          return Member::currentUser()==$this->Forum()->Subscriber();
       }

    In ForumRole.php:

    • change the belongs_many_many declaration in extraStatistics function:

    'belongs_many_many' => array(
                //'ModeratedForums' => 'Forum',
                'SubscribedForums' => 'Forum' /*--- DBS - Forums that have been subscribed to. ---*/
             ),

    • replace the existing isModeratingForum function with a Subscriber version:

    /*--- DBS - Checks if current user is Subscribed to a forum -----------*/
       function isSubscribedToForum($forum) {
          $subscriberIds = $forum->Subscribers() ? $forum->Subscribers()->getIdList() : array();
          return in_array($this->owner->ID, $subscriberIds);
       }

    • replace the existing ModeratedForums tab with SubscribedForums in the updateCMSFields function:

    $fields->addFieldToTab('Root.SubscribedForums', new CheckboxSetField('SubscribedForums', 'Subscribed forums', ($allForums ? $allForums->map('ID', 'Title') : array())));

    Final step is to add some code to ForumHeader.ss so people can subscribe and unsubscribe with a single click:

    <% if CurrentMember %>
    <% if Categories %>
    <% else %>
    <p>
    <% if MemberIsSubscribed %>
    You are subscribed to email notifications for this forum.<br /><a href="{$Link}unsubscribeFromForum" class="unsubscribeEmail">Click here to unsubscribe.</a>
    <% else %>
    <a class="subscribeEmail" href="{$Link}subscribeToForum">Subscribe to email notifications for this forum.</a>
    <% end_if %>
    </p>
    <% end_if %>
    <% end_if %>


    (The <% if Categories %> conditional ensures the subscription link doesn't show up on ForumHolder pages, where it would be redundant.)

    Phew, I think that's everything! Please feel free to fix up my bodgy work and turn this into a proper module that extends the Forum module. There's a chance I may have missed a few of my mods so please ask if anything appears to be missing.

    I believe it should now be possible in SS3.1 to achieve this without replacing the Moderators funtionality as there is now better support for multiple many_many relationships.

    Have fun!

  • the-tech-guy
    Avatar
    Community Member
    13 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Wow this looks excellent Dave! really great work!

    I am using SS3.1 and I am going to try and implement the latest one tomorrow

  • davepolyester
    Avatar
    Community Member
    47 Posts

    Re: Notifications of New Postings by Email - Module Link to this post

    Awesome, TG, I'll be very interested to hear how you get on.

    I have no idea how well it will work with the SS3 version of the Forum module as I've not even looked at it to see the differences. I've seen the odd post around the Silverstripe forums that suggests SS3 has far better support for many_many relationships so hopefully instead of butchering the original code it will be much easier to simply extend the forum module properly.

    We're looking to update the site I did my work for to SS3.1 in the next few months so I may be forced to confront the issue myself!

    3750 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.