How to: Send a notification to members with a favourite when a comment is posted on that item
Posted: 11 February 2008 04:43 PM   [ Ignore ]
Jr. Member
Avatar
RankRank
Total Posts:  56
Joined  2007-07-16

I’ve got the favourites module running in conjunction with a calendar blog, with events in.

When someone adds it as a ‘favourite’, they are set as an attendee of the event. They remove it as a favourite when they can no longer attend.

I wanted members to receive an email notification whenever someone comments on an event - just like FaceBook.

Incase anyone else would find this useful, here is how I’ve done it using an extension.

Receiving emails is defined as a custom member profile field preference, and weblog IDs are hard coded for the blog in question


<?php

if ( ! defined('EXT'))
{
exit('Invalid file request');
}


class Member_event_comment_notification
{
var $settings  = array();

var 
$name   'Member event comment notification';
var 
$version  '1.0.0';
var 
$description 'This will email a notification message to anyone attending an event which receives a comment, based on a custom member profile field preference.';
var 
$settings_exist '';
var 
$docs_url  '';

// -------------------------------
//   Constructor - Extensions use this for settings
// -------------------------------

function Member_event_comment_notification($settings='')
{
$this
->settings $settings;
}
// END


// --------------------------------
//  Activate Extension
// --------------------------------

function activate_extension()
{
global $DB;

$DB->query($DB->insert_string('exp_extensions',
array(
'extension_id' => '',
'class'  => "Member_event_comment_notification",
'method'    => "send_attendee_email",
'hook'   => "insert_comment_end",
'settings'  => "",
'priority'  => 10,
'version'   => $this->version,
'enabled'   => "y"
)
)
);
}
// END

// --------------------------------
//  Update Extension
// --------------------------------  

function update_extension($current='')
{
global $DB;

if (
$current == '' OR $current == $this->version)
{
return FALSE;
}

if ($current '1.0.0')
{
// Update queries for next version 1.0.1
}

$DB
->query("UPDATE exp_extensions 
SET version = '"
.$DB->escape_str($this->version)."' 
WHERE class = 'Member_event_comment_notification'"
);
}
// END


// --------------------------------
//  Send the email
// --------------------------------  

function send_attendee_email($data$comment_moderate$comment_id)
{   
global $DB$EXT$LOC$SESS;

$query $DB->query("SELECT exp_weblog_titles.url_title, exp_comments.entry_id FROM exp_comments LEFT JOIN exp_weblog_titles ON exp_weblog_titles.entry_id = exp_comments.entry_id WHERE exp_comments.comment_id = '"$comment_id."' AND exp_comments.weblog_id ='4'"); 
if (!empty(
$query->row["entry_id"]) && $query->row["entry_id"0{
$ei 
$query->row["entry_id"];
$ut $query->row["url_title"];
$query $DB->query("SELECT m.email, md.m_field_id_12 AS mprofileeventnotification, m.screen_name FROM exp_members AS m LEFT JOIN exp_member_data AS md ON md.member_id = m.member_id LEFT JOIN exp_favorites AS f ON m.member_id = f.member_id WHERE f.public = 'y' AND f.entry_id = '".$ei."' ORDER BY m.screen_name ASC");
if (
$query->num_rows 0{
$recipients 
= array();
foreach (
$query->result as $row{
if (!empty($row['email']) && $row["mprofileeventnotification"!= "No" && $row['email'!= $SESS->userdata['email']{
$recipients[] 
= array($row['email']$row["screen_name"]);       
}
}
if (sizeof($recipients) > 0{
if ( ! class_exists('EEmail')) {
require PATH_CORE.'core.email'.EXT;
}
$MAIL 
= new EEmail;
$MAIL->wordwrap 'y';
$MAIL->mailtype 'html';

foreach (
$recipients as $val{

$sn 
= (($SESS->userdata['screen_name'== "") ? "Someone" $SESS->userdata['screen_name']);  

$msg '<p>'.$sn.' has commented on an event you\'re attending.</p>

<
p>To see the commentview the event here:<br/><a href="http://www.mysite.com/events/info/'.$ut.'" target="_blank">www.mysite.com/members/'.$ut.'</a></p>

<
p>Or visit their profile to reply:<br/><a href="http://www.mysite.com/members/'.$SESS->userdata['username'].'" target="_blank">www.mysite.com/members/'.$SESS->userdata['username'].'</a></p>
<
p>--------------------------</p>
<
p>To stop receiving notifications for events you\'re attending, change your account settings:<br/><a href="http://www.mysite.com/account/edit_profile" target="_blank">www.mysite.com/account/edit_profile</a></p>';

$MAIL->initialize();
$MAIL->from('noreply@mysite.com''My site'); 
$MAIL->to($val[0]);
$MAIL->subject($sn." has commented on an Event you're attending"); 
$MAIL->message($msg);  
$MAIL->Send();

}
}     
}
}
}
// END

}
// END Class

?> 

Profile
 
 
Posted: 11 February 2008 07:00 PM   [ Ignore ]   [ # 1 ]
Administrator
Avatar
RankRankRankRankRank
Total Posts:  5680
Joined  2006-10-18

Thanks very much Tony! smile

 Signature 

aka “Pie Man”
Check out the new Solspace Blog!

Profile
 
 
Posted: 08 September 2008 01:50 PM   [ Ignore ]   [ # 2 ]
Jr. Member
RankRank
Total Posts:  98
Joined  2008-04-29

This is really cool idea. I don’t remember if I’ve ever hinted at this before on the board, but it would be really cool if we can get users notified when there are posts relating to a subject that’s been updated.

For example, if I “relate” an article (let’s say, news) to a topic (ie., Honda) - it would be very cool if to be able to have the user subscribed to “Honda” to be notified through email of an update on the site with news regarding the topic. =)


Cheers,
Danny

Profile