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
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?
...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…
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
// 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"])) {
$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
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).
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
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.
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?
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.
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.
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…