Email Notification for Commenting Form

I am running commenting 1.5, and I was hoping to add an email notification to admin users when a comment was submitted. Currently, we periodically check the admin side of the commenting plugin a few times a day to see if there are any comments. In the contribution plugin, there's a spot in admin where you can specify an email address for notifications of new contributions. Could I add something like that to the commenting plugin? Or if that's too complicated, can I just add an email address somewhere in the code of the plugin, so that when a comment is submitted, an email is also sent?

Thanks,
Andy

I see a couple different ways to attack this. I'll start with the most direct, which directly hacks the Commenting plugin. That's a little risky, since updates to the plugin would erase your additions, so after that I'll add in a less-risky, though a little more complicated, option.

The quick-and-dirty hack on the plugin would be to modify the models/Comment.php file to add a method that looks something like this (untested pseudocode):

public function afterSave()
{
    $email = new Zend_Mail;
    $bodyHtml = "<p>New comment on" . $this->record_id . "</p>";
    $email->setBodyHtml($bodyHtml);
    $email->setFrom("admin@yoursite.org");
    $email->addTo("moderator@yoursite.org");
    $email->send();

}

That, I think, would be the basic starts to that kind of hack.

The more complex, but stable approach would be to do the same thing, but via a new simple plugin that uses the after_save_<model> hook.

A plugin like that would just fire after any comment is saved, and do mostly the same code to send the email. That'd keep the notification functionality distinct from the basic plugin functionality, and I _think_ would still work with upgrades to the Commenting plugin.

I find that tastes vary about whether it's better to hack into the plugin, vs. adding functionality via new plugins.

Awesome. I went with the quick hack for right now and it works great. However, how does this work?

Is afterSave() a built in function to Omeka or Zend? How does it know to send this email after the comment is submitted?

Thanks again,
Andy

All the records inherit from an Omeka_Record (Omeka 1.x -- /application/libraries/Omeka/Record.php) or Omeka_Record_AbstractRecord (Omeka 2.x -- /application/libraries/Omeka/Record/AbstractRecord.php) class. They both have afterSave() methods on them that get run during the save process.

(This is actually one of the best/worst-kept secrets about Omeka hacking!)

So, it's a built in function on the superclass for the Comment, which is in Omeka. The save method just automatically runs any methods that are there, depending on Omeka version, things like beforeSave, afterSave, beforeInsert, beforeDelete etc.

Does that make sense?

I think it makes a lot more sense now. Thanks!

Here's what I ended up adding to the models/Comment.php

public function afterSave()
{
	$email = new Zend_Mail;
	$bodyHtml = '<p>New comment on ' . $this->record_id . '</p><p>'.substr(html_escape($this->body),0,40).'......<a href="https://www.mysite.edu/admin/commenting/comment/browse">See the rest of the comment</a></p>';
	$email->setBodyHtml($bodyHtml);
	$email->setFrom("myemail@myemail.com");
	$email->addTo("myemail@myemail.com");
	$email->addCc("mybossemail@myemail.com");
	$email->setSubject('New Comment');
	$email->send();
}

I added a bit so you can see a snippet of the comment, and a link to the commenting plugin in admin.