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 😊
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…
Help on any of those is much appreciated.
TonyNiblles,
In backwards order:
<?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('[email protected]', 'Some Name');
$MAIL->to('[email protected]');
$MAIL->subject('Some Subject');
$MAIL->message('Some Message');
$MAIL->Send();
?>
You would use the insert_comment_end hook.
You have the member_id from the $SESS class. Just run a query against that for the member field you want to retrieve.
Jamie
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…
😊
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
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 = ''.$sn.' has commented on your profile.
To see the comment, view your profile now:<br><a href="http://www.mysite.com/account/edit_profile>rowusername" rel="noopener">.'" target="_blank">www.mysite.com/members/'.$query->row["username"].'</a>_ _ Or visit their profile to reply:<br><a >userdata['username'].'" target="_blank">www.mysite.com/members/'.$SESS->userdata['username'].'</a>_ --------------------------_ To stop receiving notifications for your profile, change your account settings:<br><a target="_blank"]www.mysite.com/account/edit_profile</a>';
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('[email protected]', 'My site');
$MAIL->to($query->row["email"]);
$MAIL->subject($sn.' has commented on your profile');
$MAIL->message($msg);
$MAIL->Send();
}
}
}
// END
}
// END Class
?>
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.
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
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! 😊
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
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!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.