Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Blog Module

Discuss the Blog Module.

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

[Solved] PageCommentEmailNotification


Reply

5 Posts   662 Views

Avatar
borriej

2 March 2011 at 3:23am Community Member, 267 Posts

Hello,

I couldn't find the topic, so im posting it again:

Got this little script:

PageCommentEmailNotification.php

<?php

class PageCommentEmailNotification extends DataObjectDecorator {
function onAfterWrite() {
parent::onAfterWrite();
if ($this->NeedsModeration) {
$email = new Email();
$email->setTemplate('NewComment');
$email->setTo(Email::getAdminEmail());
$email->addCustomHeader('Reply-To', Member::currentUser()->Email);
$email->setSubject('New Comment ' . str_replace(array("http://", "https://"), array("", ""), Director::absoluteBaseURL()));
$email->populateTemplate(array(
'URL' => Director::absoluteBaseURL() . $this->owner->Parent()->URLSegment,
'PageTitle' => $this->owner->Parent()->Title,
'Comment' => $this->owner->Comment,
'Name' => $this->owner->Name,
));
$email->send();
}
}
}

It sends emails when new comments are posted on my blog, this works well when your logged in as a admin..

But when your not logged in: you get a pop-up with the html code of a error page. The message content:

   
      <h2>Server error</h2>
   
      <p>Sorry, there was a problem with handling your request.</p>

How do i fix this?

   

Avatar
Willr

2 March 2011 at 10:35pm Forum Moderator, 5511 Posts

Put the site into devmode. That will give you a better error message.

http://doc.silverstripe.org/sapphire/en/topics/debugging#environment-types

Avatar
borriej

2 March 2011 at 10:53pm Community Member, 267 Posts

Site is already in dev mode:
Director::set_environment_type("dev");

..

Avatar
borriej

2 March 2011 at 11:06pm Community Member, 267 Posts

im debugging it now by removing and adding line by line.

found out that the problem occurs when:

if ($this->NeedsModeration) {

}

Is added into this code:

class PageCommentEmailNotification extends DataObjectDecorator {
function onAfterWrite() {
parent::onAfterWrite();
if ($this->NeedsModeration) {

}

What to do now?s

Avatar
borriej

2 March 2011 at 11:11pm (Last edited: 2 March 2011 11:12pm), Community Member, 267 Posts

Solved!

For the people who are looking for this code:

config:

DataObject::add_extension('PageComment', 'PageCommentEmailNotification');

PageCommentEmailNotification.php

<?php

class PageCommentEmailNotification extends DataObjectDecorator {
function onAfterWrite() {
parent::onAfterWrite();

       $email = new Email();
$email->setTemplate('NewComment');
$email->setTo(Email::getAdminEmail());
// $email->addCustomHeader('Reply-To', Member::currentUser()->Email);
$email->setSubject('New Comment ' . str_replace(array("http://", "https://"), array("", ""), Director::absoluteBaseURL()));
$email->populateTemplate(array(
'URL' => Director::absoluteBaseURL() . $this->owner->Parent()->URLSegment,
'PageTitle' => $this->owner->Parent()->Title,
'Comment' => $this->owner->Comment,
'Name' => $this->owner->Name,
));
$email->send();
}
}