Author Comment Notification per Weblog Entry
Posted: 18 January 2008 03:57 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  94
Joined  06-10-2007

Hello!

I’ve got a problem I’m trying to solve and I am hoping you can help me.

When a member registers on my site (using Solspaces’ User module) they are automatically given a weblog entry, for which they are the author (via a little extension I made).

This enables the member profiles to have their own ‘Comments wall’, which is rather a nice thing.

I’d like to let people choose whether or not they receive an email notifcation from their weblog entry when someone posts a comment.
There is a preference ‘Notify authors of new comments’ in the weblog prefs - but of course - this applies globally, and I’d like to make this a preference that applies to each individual weblog.

I’m seeking technical assistance to do this smile

From what I can see, I might be able to run an extension to do this (from a hook when a comment is posted).

There are a few things I need to know first though…

* What is the best way of retrieving a single custom member field value from member_id and the field name?
* Which extension hook is the appropriate one to use in this case?
* How can I send a custom email or send an email based on the built-in comment notification one?

Help on any of those is much appreciated.

Profile
 
 
Posted: 24 January 2008 12:31 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1367
Joined  02-12-2003

TonyNiblles,

In backwards order:

1. You would call the email class to send an email:

<?php

if ( ! class_exists('EEmail'))
{
    
require PATH_CORE.'core.email'.EXT;
}

$MAIL
= new EEmail;
$MAIL->wordwrap    = 'y';
$MAIL->mailtype    = 'html'; // OR 'text'
                        
$MAIL->initialize();
$MAIL->from('some_email@address.com', 'Some Name');    
$MAIL->to('some_email@address.com');
$MAIL->subject('Some Subject');    
$MAIL->message('Some Message');        
$MAIL->Send();

?>

2. You would use the insert_comment_end hook.

3. You have the member_id from the $SESS class.  Just run a query against that for the member field you want to retrieve.

Jamie

Profile
 
 
Posted: 24 January 2008 12:49 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  94
Joined  06-10-2007

Wow! That is fantastic, that you so much for your help. You *officially* kick ass!

This should be relatively simple then.

The one remaining hairy bit for me is the custom weblog field. I get extremely confused when I look over the piece of code to pull out that data…

// Custom weblog fields    
$fields = array();
$query = $DB->query("SELECT m_field_id, m_field_name, m_field_fmt FROM exp_member_fields");
if (
$query->num_rows > 0) {
    
foreach ($query->result as $row) {
        $fields[$row[
'm_field_name']] = array($row['m_field_id'], $row['m_field_fmt']);
    
}
}
$query
= $DB->query("SELECT * FROM exp_member_data WHERE member_id = '".$member_id."'");    
if (
$query->num_rows == 0) {
    
foreach ($fields as $key => $val) {
        $text
= $TMPL->swap_var_single($key, '', $text);
    
}   
}

foreach ($query->result as $row) {
    $cond
= array();
    foreach(
$fields as $key =>  $value) {
        $cond[$key]
= $this->TYPE->parse_type($row['m_field_id_'.$value['0']],
                                        array(
                                              
'text_format'   => $value['1'],
                                              
'html_format'   => 'safe',
                                              
'auto_links'    => 'y',
                                              
'allow_img_url' => 'n'
                                             
)
                                      );    
    
}
    $text
= $FNS->prep_conditionals($text, $cond);
    foreach (
$TMPL->var_single as $key => $val) {
        
if ( isset($fields[$val]) AND isset($row['m_field_id_'.$fields[$val]['0']])) {
            $text
= $TMPL->swap_var_single(
                                                
$val,
                                                
$this->TYPE->parse_type(
                                                                        
$row['m_field_id_'.$fields[$val]['0']],
                                                                        array(
                                                                                
'text_format'   => $fields[$val]['1'],
                                                                                
'html_format'   => 'safe',
                                                                                
'auto_links'    => 'y',
                                                                                
'allow_img_url' => 'n'
                                                                              
)
                                                                      ),
                                                
$text
                                              
);
        
}
    }
}

...at least, that’s the code I am currently using in another plugin of mine.

If we know the member_id and the custom field name - is there a ‘proper’ way of grabbing that data?

I’m sure this is a simple little query, but this is the second time I am building my community site and if I can understand the more professional ways of doing things, then I won’t be so scared to look over my old code in 6 months time. I just want to do it right…

smile

Profile
 
 
Posted: 24 January 2008 12:58 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1367
Joined  02-12-2003

Tony,

Its more a matter of scale.  You really only need one field right?  So no reason to over complicate it.

A simple query like this will grab the data you need:

$query = $DB->query("SELECT m_field_id_# AS receive_comment_emails FROM exp_member_data WHERE member_id = '".$member_id."'");

Where m_field_id_# is the column with field you need. 

Jamie

Profile
 
 
Posted: 24 January 2008 01:06 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  94
Joined  06-10-2007

Best to be safe smile

Thank you SO much for the black’n'white reply. Just means I can know I’m going about this the right way smile

I’ll probably go on to make two or three more extensions to fire off emails for other similar things.

Profile
 
 
Posted: 11 February 2008 05:34 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  94
Joined  06-10-2007

Thanks for the help!

I have this working beautifully now, as I originally intended.

Should anyone else wish to do this, here was my finished code for my extension:

<?php

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


class Member_wall_comment_notification
{
    
var $settings        = array();
    
    var
$name            = 'Member wall comment notification';
    var
$version        = '1.0.0';
    var
$description    = 'This will email a notification message to someone when they receive a wall comment, based on a custom member profile field preference.';
    var
$settings_exist    = '';
    var
$docs_url        = '';
    
    
// -------------------------------
    //   Constructor - Extensions use this for settings
    // -------------------------------
    
    
function Member_wall_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_wall_comment_notification",
                                            
'method'       => "send_member_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_wall_comment_notification'"
);
    
}
    
// END
    
    
    // --------------------------------
    //  Send the email
    // --------------------------------  
    
    
function send_member_email($data, $comment_moderate, $comment_id)
    
{   
        
global $DB, $EXT, $LOC, $SESS;
        
        
//$msg = "data:".print_r($data)."<br/>comment_moderate:".$comment_moderate."<br/>comment_id:".$comment_id;
        
        // First, get member preference
        
$query = $DB->query("SELECT m_field_id_11 AS receive_comment_emails FROM exp_member_data WHERE member_id = '".$SESS->userdata['member_id']."'");

        
// Default to being ON (option is either Yes or No)
        
if ($query->row["receive_comment_emails"] != "No") {
            
            
// Next, work out the email address of this author                 
            
$query = $DB->query("SELECT exp_members.email AS email, exp_members.username AS username FROM exp_comments LEFT JOIN exp_weblog_titles ON exp_weblog_titles.entry_id = exp_comments.entry_id LEFT JOIN exp_members ON exp_members.member_id = exp_weblog_titles.author_id WHERE exp_comments.weblog_id = '3' AND exp_comments.comment_id = '". $comment_id."'");
            if (!empty(
$query->row["email"])) {
                            
                $sn
= (($SESS->userdata['screen_name'] == "") ? "Someone" : $SESS->userdata['screen_name']);            
                            
                
$msg = '<p>'.$sn.' has commented on your profile.</p>
                
                <p>To see the comment, view your profile now:<br/><a href="http://www.mysite.com/members/'
.$query->row["username"].'" target="_blank">www.mysite.com/members/'.$query->row["username"].'</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 your profile, change your account settings:<br/><a href="http://www.mysite.com/account/edit_profile" target="_blank">www.mysite.com/account/edit_profile</a></p>'
;
            
                if ( !
class_exists('EEmail'))
                
{
                    
require PATH_CORE.'core.email'.EXT;
                
}
                
                $MAIL
= new EEmail;
                
$MAIL->wordwrap    = 'y';
                
$MAIL->mailtype    = 'html'; // OR 'text'
                                        
                
$MAIL->initialize();
                
$MAIL->from('noreply@mysite.com', 'My site');    
                
$MAIL->to($query->row["email"]);
                
$MAIL->subject($sn.' has commented on your profile');    
                
$MAIL->message($msg);        
                
$MAIL->Send();
            
}
        }
    }
    
// END

}
// END Class

?>
Profile
 
 
Posted: 14 February 2008 07:16 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
RankRankRank
Total Posts:  348
Joined  07-04-2007

Would you consider releasing the code for automatically creating the comment wall weblog entry? It would be useful for people wanting to store member data in weblogs (profile data, comments, tags etc).

Thanks

Profile
 
 
Posted: 15 February 2008 03:38 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  94
Joined  06-10-2007

Sure thing, here it is:

<?php

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


class Member_blog_entry
{
    
var $settings        = array();
    
    var
$name            = 'Member blog entry';
    var
$version        = '1.0.0';
    var
$description    = 'This will insert a blog entry for a newly registered member, making it possible to have a comments form on their member profile.';
    var
$settings_exist    = '';
    var
$docs_url        = '';
    
    
// -------------------------------
    //   Constructor - Extensions use this for settings
    // -------------------------------
    
    
function Member_blog_entry($settings='')
    
{
        $this
->settings = $settings;
    
}
    
// END
    
    
    // --------------------------------
    //  Activate Extension
    // --------------------------------
    
    
function activate_extension()
    
{
        
global $DB;
        
        
$DB->query($DB->insert_string('exp_extensions',
                                      array(
                                            
'extension_id' => '',
                                            
'class'        => "Member_blog_entry",
                                            
'method'       => "add_member_blog",
                                            
'hook'         => "user_register_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_blog_entry'"
);
    
}
    
// END
    
    
    // --------------------------------
    //  Add the member blog
    // --------------------------------  
    
    
function add_member_blog($which, $member_id)
    
{    
        
global $DB, $EXT, $LOC;
        
// Fetch the freshly registered member data
        
$results = $DB->query("SELECT username, screen_name FROM exp_members WHERE member_id = ".$member_id."");
        
$entry_date = date('Y-m-d H:i A');
        
$entry_date = $LOC->set_human_time($LOC->now);
        
$entry_date = $LOC->now;
        
// Add the member blog title (make a readable Title and a url_title with their ID, followed by _comment)
        
$qry = "INSERT INTO exp_weblog_titles (entry_id, weblog_id, author_id, site_id, ip_address, title, url_title, entry_date, edit_date, versioning_enabled, year, month, day, expiration_date, comment_expiration_date, sticky, status, allow_comments, allow_trackbacks, forum_topic_id, dst_enabled)
        VALUES
            ('',
            '3',
            '$member_id',
            '1',
            '"
.$_SERVER['REMOTE_ADDR']."',
            '"
.$results->row['screen_name']." comments',
            '"
.$member_id."_comments',
            '"
.$entry_date."',
            NOW(),
            'n',
            '"
.date('Y')."',
            '"
.date('m')."',
            '"
.date('d')."',
            '0',
            '0',
            'n',
            'open',
            'y',
            'n',
            '0',
            'n')"
;
        
$DB->query($qry);
        
// Get entry ID
        
$entry_id = $DB->insert_id;
        
// Add to weblog data
        
$qry = "INSERT INTO `exp_weblog_data` (`entry_id`, `site_id`, `weblog_id`) VALUES ('".$entry_id."', '1', '3')";
        
$DB->query($qry);
        
// Update total entries
        
$DB->query("UPDATE exp_members set total_entries = '1', last_entry_date = NOW() WHERE member_id = '$member_id'");
    
}
    
// END

}
// END Class

?>

It will create an entry with the url_title made from their ID and then “_comments”, eg: someone with a user id of 1 would have a blog entry URL title of “1_comments”.

This works for all new registrations on a site, naturally, older members would have to be manually added.

Profile
 
 
Posted: 15 February 2008 04:26 AM   [ Ignore ]   [ # 8 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9246
Joined  04-15-2006

Hi Tony,

I just happened across this code and it seems really good what you can do with it. I realise that we would probably need to change a few variables in the extension to say which weblog to post to but I tried placing the code above into an extension file and whilst it shows up and installs correctly when I register any new people to the site nothing gets added to the database as a new entry.

Would love to get this working. Do you have any ideas what it might be?

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 17 February 2008 11:17 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
RankRankRank
Total Posts:  348
Joined  07-04-2007

Mark, I havent tried the posted code out yet but “user_register_end” hook relates to Solspace’s User Module.  So you will need to have that installed.

Profile
 
 
Posted: 17 February 2008 11:51 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
RankRankRank
Total Posts:  348
Joined  07-04-2007

-

Profile
 
 
Posted: 17 February 2008 12:59 PM   [ Ignore ]   [ # 11 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9246
Joined  04-15-2006
Joobs - 17 February 2008 11:17 AM

Mark, I havent tried the posted code out yet but “user_register_end” hook relates to Solspace’s User Module.  So you will need to have that installed.

Aha, that would make sense! grin

I should have read the post more carefully!!

Sorry about that. Will probably take a look into the module at some point.

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 23 February 2008 11:24 AM   [ Ignore ]   [ # 12 ]  
Research Assistant
RankRankRank
Total Posts:  348
Joined  07-04-2007

Mark, I’m pretty sure the hook “member_member_register” could be used instead and would have the same effect when a user registers through the normal registration page.

Profile
 
 
Posted: 26 February 2008 01:31 PM   [ Ignore ]   [ # 13 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9246
Joined  04-15-2006

Hiya,

Thanks for that. Will probably give that a try myself at some point as it does seem like a really great idea.

Thanks again.

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 13 October 2008 02:51 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-18-2003

This functionality is exactly what I’m looking - unfortunately, I can’t get this extension to load, I’m getting

Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION

from the Extensions Manager on line 11 which is:

var $settings        = array();

Anyone any ideas? This is on my local installation on my Mac running Php 5.2.6.

Alternatively, anyone know of any other ways of achieving the same thing: user registers (most likely through Solspace’s User Module) and their ID is submitted to a weblog entry which functions as their Profile.

Many thanks!

 Signature 

“There are a number of us these days who do not seek deliberately to go to prison but cherish a dream of being sent there to enjoy, paradoxically, true freedom.” Anthony Burgess

Books | Design

Profile
 
 
Posted: 13 October 2008 05:12 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
RankRankRank
Total Posts:  348
Joined  07-04-2007

I’ve used the posted code in an extension before and got it to work.  Although i did modify it a little bit.

I’m not sure what’s causing the error.  Is the filename exactly the same as the class name? ext.Member_blog_entry.php

Profile
 
 
Posted: 13 October 2008 11:04 AM   [ Ignore ]   [ # 16 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-18-2003

Yes, it’s the same…still can’t get to the bottom of it but am working on a slightly different solution so may not be necessary. Thanks for replying though…

 Signature 

“There are a number of us these days who do not seek deliberately to go to prison but cherish a dream of being sent there to enjoy, paradoxically, true freedom.” Anthony Burgess

Books | Design

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 77482 Total Logged-in Users: 32
Total Topics: 101518 Total Anonymous Users: 21
Total Replies: 544254 Total Guests: 225
Total Posts: 645772    
Members ( View Memberlist )