| 1 |
<?php
|
| 2 |
// $Id: comment_subscribe.module $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*
|
| 7 |
* This module provides comment followup e-mail notification for
|
| 8 |
* anonymous as well as registered users.
|
| 9 |
*/
|
| 10 |
//The mail to be send for the users
|
| 11 |
define('DEFAULT_MAILTEXT','
|
| 12 |
Hi !name,
|
| 13 |
|
| 14 |
!commname has commented on: "!node_title"
|
| 15 |
|
| 16 |
The post is about
|
| 17 |
----
|
| 18 |
!node_teaser
|
| 19 |
----
|
| 20 |
|
| 21 |
You can view the comment at the following url
|
| 22 |
!comment_url
|
| 23 |
|
| 24 |
You can stop receiving emails when someone replies to this blog post,
|
| 25 |
by going to !link1
|
| 26 |
|
| 27 |
You can stop receiving emails when someone replies to your comment,
|
| 28 |
by going to !link2
|
| 29 |
|
| 30 |
If you have an account at the site and if you have auto-blog-following enabled in your account, you will receive emails like this for all replies to a blog post you commented on. You can disable this unchecking the flag at the time you post the comment.
|
| 31 |
|
| 32 |
|
| 33 |
Thanks for your feedback,
|
| 34 |
|
| 35 |
Webmaster of !site
|
| 36 |
!mission
|
| 37 |
!uri
|
| 38 |
');
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Implementation of hook_help().
|
| 42 |
*/
|
| 43 |
function comment_subscribe_help($path, $arg) {
|
| 44 |
switch ( $path ) {
|
| 45 |
case 'admin/modules#description':
|
| 46 |
return t('Comment follow-up e-mail notification for anonymous as well as registered users.');
|
| 47 |
break;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
/**
|
| 51 |
* Implementation of hook_menu().
|
| 52 |
*/
|
| 53 |
function comment_subscribe_menu() {
|
| 54 |
$items = array();
|
| 55 |
global $user;
|
| 56 |
$items['admin/settings/comment_subscribe'] = array(
|
| 57 |
'title' => t('Comment Subscribe'),
|
| 58 |
'description' => t('Comment follow-up mail subscription.'),
|
| 59 |
'page callback' => 'drupal_get_form',
|
| 60 |
'page arguments' => array('comment_subscribe_settings'),
|
| 61 |
'access arguments' => array('administer comment_subscribe'),
|
| 62 |
'type' => MENU_NORMAL_ITEM,
|
| 63 |
'file' => 'comment_subscribe.admin.inc'
|
| 64 |
);
|
| 65 |
$items['comment_subscribe'] = array(
|
| 66 |
'title' => t('Comment Subscribe') ,
|
| 67 |
'page callback' => 'comment_subscribe_page',
|
| 68 |
'access arguments' => array(1),
|
| 69 |
'type' => MENU_CALLBACK
|
| 70 |
);
|
| 71 |
return $items;
|
| 72 |
}
|
| 73 |
/**
|
| 74 |
* Insert our checkbox, and populate fields.
|
| 75 |
* set validation hook.
|
| 76 |
*/
|
| 77 |
function comment_subscribe_form_alter(&$form, $form_state, $form_id) {
|
| 78 |
global $user;
|
| 79 |
//Checking whether the form is comment_form or not.
|
| 80 |
if ( ($form_id != 'comment_form')) {
|
| 81 |
return;
|
| 82 |
}
|
| 83 |
$op = isset($_POST['op']) ? $_POST['op'] : '';
|
| 84 |
if ($op == t('Preview')) {
|
| 85 |
drupal_set_message(t('ATTENTION: your comment is NOT YET posted -
|
| 86 |
please click the post button to confirm your post' )
|
| 87 |
);
|
| 88 |
//extra submit button on top
|
| 89 |
if (!form_get_errors() && ((variable_get('comment_preview',
|
| 90 |
COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL)
|
| 91 |
|| ($op == t('Preview')) || ($op == t('Save')))) {
|
| 92 |
$form['submitextra'] = array(
|
| 93 |
'#type' => 'fieldset',
|
| 94 |
'#title' => t('Comment is not posted yet - please click
|
| 95 |
post button to confirm your post'),
|
| 96 |
'#weight' => -99,
|
| 97 |
'#collapsible' => FALSE
|
| 98 |
);
|
| 99 |
$form['submitextra']['submit'] = array('#type' => 'submit',
|
| 100 |
'#value' => t('Save'), '#weight' => -20);
|
| 101 |
}
|
| 102 |
}
|
| 103 |
if(variable_get("comment_subscribe_node_alert",FALSE)) {
|
| 104 |
$form['commentsubscribenode'] = array(
|
| 105 |
'#type' => 'checkbox',
|
| 106 |
'#title' => t('Email me about all replies to this article.'),
|
| 107 |
'#default_value' => ($user->uid != 0)?$user->comment_subscribe_mailalert:variable_get
|
| 108 |
("comment_subscribe_default_anon_mailalert", FALSE)
|
| 109 |
);
|
| 110 |
}
|
| 111 |
else if ( ($user->uid == 0) || ( variable_get(
|
| 112 |
"comment_subscribe_regged_checkbox", TRUE) ) ) {
|
| 113 |
$form['commentsubscribe'] = array(
|
| 114 |
'#type' => 'checkbox',
|
| 115 |
'#title' => t('Email me about replies to this comment.'),
|
| 116 |
'#default_value' => ($user->uid != 0)?$user->comment_subscribe_mailalert:variable_get
|
| 117 |
("comment_subscribe_default_anon_mailalert", TRUE)
|
| 118 |
);
|
| 119 |
}
|
| 120 |
else {
|
| 121 |
$form['commentsubscribe'] = array(
|
| 122 |
'#type' => 'hidden',
|
| 123 |
'#title' => t('Mail me updates to this comment.'),
|
| 124 |
'#default_value' =>$user->comment_subscribe_mailalert
|
| 125 |
);
|
| 126 |
}
|
| 127 |
if( $form['cid']['#value'] != '' ) {
|
| 128 |
//Get the subscribe status of current comment from z_commentsubscribe table
|
| 129 |
$subsrberslt = db_fetch_object(db_query("SELECT subscribe,subscribenode FROM
|
| 130 |
{z_commentsubscribe}
|
| 131 |
WHERE cid = %d", arg(2)));
|
| 132 |
$subscribestatus = $subsrberslt->subscribe;
|
| 133 |
$subscribenodestatus = $subsrberslt->subscribenode;
|
| 134 |
$form['commentsubscribe']['#default_value'] = $subscribestatus;
|
| 135 |
$form['commentsubscribenode']['#default_value'] = $subscribenodestatus;
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Implementation of hook_user().
|
| 141 |
*/
|
| 142 |
function comment_subscribe_user($type, &$edit, &$user, $category = NULL) {
|
| 143 |
switch ($type) {
|
| 144 |
|
| 145 |
case 'form':
|
| 146 |
if ( $category == 'account' ) {
|
| 147 |
$form = array();
|
| 148 |
$form['comment_subscribe_settings'] = array(
|
| 149 |
'#type' => 'fieldset',
|
| 150 |
'#title' => t('Comment follow up subscription settings'),
|
| 151 |
'#weight' => 4,
|
| 152 |
'#collapsible' => TRUE
|
| 153 |
);
|
| 154 |
$form['comment_subscribe_settings']['comment_subscribe_mailalert'] = array(
|
| 155 |
'#type' => 'checkbox',
|
| 156 |
'#title' => t('Receive comment follow-up notification e-mails'),
|
| 157 |
'#default_value' => isset($edit['comment_subscribe_mailalert']) ? $edit['comment_subscribe_mailalert'] : 1,
|
| 158 |
'#description' => t('Check this box to receive e-mail notification for follow-up comments to comments you
|
| 159 |
posted. ')
|
| 160 |
);
|
| 161 |
return $form;
|
| 162 |
}
|
| 163 |
break;
|
| 164 |
}
|
| 165 |
}
|
| 166 |
/**
|
| 167 |
* Implement comment hook and check the publish status
|
| 168 |
*/
|
| 169 |
function comment_subscribe_comment($comment, $op) {
|
| 170 |
global $user;
|
| 171 |
switch($op) {
|
| 172 |
case 'publish':
|
| 173 |
//Converting array to object
|
| 174 |
if( is_array( $comment ) ) {
|
| 175 |
|
| 176 |
$comment = (object)$comment;
|
| 177 |
}
|
| 178 |
_comment_subscribe_mailalert($comment);
|
| 179 |
break;
|
| 180 |
case 'update':
|
| 181 |
//If node subscription is selected
|
| 182 |
if(variable_get("comment_subscribe_node_alert",FALSE)) {
|
| 183 |
$sql = "update {z_commentsubscribe} set subscribenode =%d where
|
| 184 |
cid = %d";
|
| 185 |
db_query($sql, $comment['commentsubscribenode'], $comment['cid']);
|
| 186 |
}
|
| 187 |
//If comment reply subscription is selected
|
| 188 |
else {
|
| 189 |
$sql = "update {z_commentsubscribe} set subscribe = %d where
|
| 190 |
cid = %d";
|
| 191 |
db_query($sql, $comment['commentsubscribe'], $comment['cid']);
|
| 192 |
}
|
| 193 |
break;
|
| 194 |
case 'insert':
|
| 195 |
case 'Save':
|
| 196 |
//Get pid from comment for current cid ,get the parents for
|
| 197 |
//this cid
|
| 198 |
$notifyval = 0;
|
| 199 |
$parentrslt = db_fetch_object(db_query("SELECT pid FROM
|
| 200 |
{comments}
|
| 201 |
WHERE cid = %d", $comment['cid']));
|
| 202 |
$strparents = $parentrslt->pid.',';
|
| 203 |
$prvparnt = db_fetch_object(db_query("SELECT parents FROM
|
| 204 |
{z_commentsubscribe}
|
| 205 |
WHERE cid = %d", $parentrslt->pid));
|
| 206 |
$parents = $prvparnt->parents.$strparents;
|
| 207 |
//Checking which option is selected ie checking
|
| 208 |
//whether node reply or comment reply is chosen.
|
| 209 |
//According to the selection flag subscription is saved.
|
| 210 |
if(variable_get("comment_subscribe_node_alert",FALSE)) {
|
| 211 |
$sql = "insert into {z_commentsubscribe} (cid, parents, subscribenode)
|
| 212 |
values(%d, '%s', %d)";
|
| 213 |
db_query($sql, $comment['cid'], $parents, $comment['commentsubscribenode']);
|
| 214 |
}
|
| 215 |
else {
|
| 216 |
$sql = "insert into {z_commentsubscribe} (cid,parents,subscribe)
|
| 217 |
values(%d, '%s', %d)";
|
| 218 |
db_query($sql, $comment['cid'], $parents, $comment['commentsubscribe']);
|
| 219 |
}
|
| 220 |
/* if(user_access('administer comment_subscribe')){
|
| 221 |
$comment = db_fetch_object(db_query("SELECT * FROM {comments}
|
| 222 |
WHERE cid = %d", $comment['cid']));
|
| 223 |
_comment_subscribe_mailalert($comment);
|
| 224 |
}*/
|
| 225 |
break;
|
| 226 |
case 'delete':
|
| 227 |
$sql = "DELETE FROM {z_commentsubscribe} WHERE cid = %d or
|
| 228 |
instr(parents, '%s,') ";
|
| 229 |
db_query($sql, $comment->cid,$comment->cid);
|
| 230 |
break;
|
| 231 |
}
|
| 232 |
}
|
| 233 |
/**
|
| 234 |
* Implementation of hook_mail().
|
| 235 |
*/
|
| 236 |
function comment_subscribe_mail($key, &$message, $params) {
|
| 237 |
$language = $message['language'];
|
| 238 |
$variables = user_mail_tokens($params['account'], $language);
|
| 239 |
$message['subject'] .= $_SESSION['subject'];
|
| 240 |
$message['body'][] = $_SESSION['message'] ;
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
*Function to send mail to subscribed users if any comment is submitted
|
| 245 |
*/
|
| 246 |
function _comment_subscribe_mailalert( $comment) {
|
| 247 |
global $base_url;
|
| 248 |
$nid = $comment->nid;
|
| 249 |
$cid = $comment->cid;
|
| 250 |
$commname= $comment->name;
|
| 251 |
$commtext= $comment->comment;
|
| 252 |
$commsubj= $comment->subject;
|
| 253 |
//Get the parents and mailid of a particular comment
|
| 254 |
$parentrslt = db_fetch_object(db_query("SELECT parents,
|
| 255 |
IF(length(c.mail)<1, ifnull(u.mail,u.init),
|
| 256 |
c.mail) mail FROM {z_commentsubscribe} zc
|
| 257 |
LEFT JOIN {comments} c on zc.cid = c.cid
|
| 258 |
LEFT JOIN {users} u on u.uid = c.uid
|
| 259 |
WHERE zc.cid = %d", $cid));
|
| 260 |
$arrparents =explode(',',$parentrslt->parents);
|
| 261 |
$parents .= $arrparents[0];
|
| 262 |
for($i = 1; $i< count($arrparents)-1 ; $i++) {
|
| 263 |
$parents .=',';
|
| 264 |
$parents .= $arrparents[$i];
|
| 265 |
}
|
| 266 |
$currmailid = $parentrslt->mail;
|
| 267 |
$node = node_load($nid);
|
| 268 |
$from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 269 |
//Get the mailid of users who subscribed for the comment replies
|
| 270 |
//or node comments.
|
| 271 |
$result = db_query('SELECT DISTINCT c.cid,u.init,c.uid,c.name,c.nid
|
| 272 |
, IF(length(c.mail)<1, ifnull(u.mail,u.init),
|
| 273 |
c.mail) mail
|
| 274 |
FROM {comments} c LEFT OUTER JOIN {users} u ON u.uid=c.uid
|
| 275 |
LEFT JOIN {z_commentsubscribe} zc on zc.cid=c.cid
|
| 276 |
WHERE nid=%d AND ((zc.cid IN (%s) AND subscribe=1) OR subscribenode=1 )AND c.status=0
|
| 277 |
AND LENGTH(IF(LENGTH(c.mail)<1, ifnull(u.mail,u.init),c.mail))>1
|
| 278 |
AND IF(LENGTH(c.mail)<1, ifnull(u.mail,u.init),c.mail) like
|
| 279 |
\'%@%.%\'
|
| 280 |
AND IF(LENGTH(c.mail)<1, ifnull(u.mail,u.init),c.mail) <> \'%s\'
|
| 281 |
GROUP BY IF(LENGTH(c.mail)<1, ifnull(u.mail,u.init),c.mail) ORDER
|
| 282 |
BY mail DESC',
|
| 283 |
$nid, $parents, $currmailid
|
| 284 |
);
|
| 285 |
$count=0;
|
| 286 |
while ($alert = db_fetch_object($result))
|
| 287 |
{
|
| 288 |
$mailid = $alert->mail;
|
| 289 |
$subject = t('!site :: new comment for your post.', array(
|
| 290 |
'!site' => variable_get('site_name', 'drupal')));
|
| 291 |
$message = t((variable_get('comment_subscribe_default_mailtext',
|
| 292 |
DEFAULT_MAILTEXT)),
|
| 293 |
array(
|
| 294 |
'!commname' => $commname,
|
| 295 |
'!commtext' => $commtext,
|
| 296 |
'!commsubj' => $commsubj,
|
| 297 |
'!comment_url' => url('node/'.$nid,array('absolute' => TRUE))."#comment-".
|
| 298 |
$cid,
|
| 299 |
'!node_title' => $node->title,
|
| 300 |
'!node_teaser' => $node->teaser,
|
| 301 |
'!mission' => variable_get('site_mission', ''),
|
| 302 |
'!node_body' => $node->body,
|
| 303 |
'!name' => $alert->name,
|
| 304 |
'!site' => variable_get('site_name', 'drupal'),
|
| 305 |
'!uri' => $base_url,
|
| 306 |
'!uri_brief' => substr($base_url, strlen('http://')),
|
| 307 |
'!date' => format_date(time()),
|
| 308 |
'!login_uri' => url('user', array('absolute' => TRUE)),
|
| 309 |
'!edit_uri' => url('user/'. $alert->uid .'/edit', array('absolute' => TRUE)
|
| 310 |
),
|
| 311 |
'!link1' => url('comment_subscribe/disable/nodepost/'.$nid.'/'.
|
| 312 |
md5($mailid), array('absolute' => TRUE)),
|
| 313 |
'!link2' => url('comment_subscribe/disable/postrply/'.$alert->
|
| 314 |
cid.'/'.md5($mailid), array('absolute' => TRUE)),
|
| 315 |
)
|
| 316 |
);
|
| 317 |
$_SESSION['subject'] = $subject;
|
| 318 |
$_SESSION['message'] = $message;
|
| 319 |
$mail = drupal_mail('comment_subscribe', 'comment_subscribe_mail', $alert->mail, language_default());
|
| 320 |
$count++;
|
| 321 |
if ( ($alert->uid)!=0 )
|
| 322 |
$mylink =l( $mailid , 'user/'. $alert->uid .'/edit') ;
|
| 323 |
else
|
| 324 |
$mylink =$mailid;
|
| 325 |
// Add an entry to the watchdog log.
|
| 326 |
watchdog('comment_subscribe', t('Subscribe ') . $mylink,
|
| 327 |
WATCHDOG_NOTICE, l(t('source comment'), 'node/'. $nid, array('fragment' => 'comment-'. $alert->cid)));
|
| 328 |
// revert to previous (site default) locale
|
| 329 |
$locale = $initial_locale;
|
| 330 |
}
|
| 331 |
if( $count > 0 )
|
| 332 |
drupal_set_message ("Thanks for your comment! I just sent out
|
| 333 |
$count follow-up mail notifications!");
|
| 334 |
|
| 335 |
}
|
| 336 |
/**
|
| 337 |
* Function to unsubscribe the mail
|
| 338 |
*/
|
| 339 |
function comment_subscribe_page() {
|
| 340 |
global $user;
|
| 341 |
$breadcrumb = NULL;
|
| 342 |
$op = $_POST["op"];
|
| 343 |
$edit = $_POST["edit"];
|
| 344 |
$page_content=" ";
|
| 345 |
if (empty($op)) {
|
| 346 |
$op = arg(1);
|
| 347 |
}
|
| 348 |
$arg = arg(3);
|
| 349 |
switch ($op) {
|
| 350 |
case 'disable':
|
| 351 |
//Type of unsubscription
|
| 352 |
$type = arg(2);
|
| 353 |
$key=$arg;
|
| 354 |
//Disable followup comments for a particular node
|
| 355 |
if(strcmp($type,'nodepost')==0) {
|
| 356 |
db_query("UPDATE {comments} c , {users} u ,
|
| 357 |
{z_commentsubscribe} cs
|
| 358 |
SET cs.subscribe = 0, cs.subscribenode = 0
|
| 359 |
WHERE c.cid=cs.cid AND c.uid = u.uid
|
| 360 |
AND c.nid = %d AND md5(IF( LENGTH( c.mail ) <1, u.mail,
|
| 361 |
c.mail ) ) = '%s'",$arg,arg(4));
|
| 362 |
drupal_set_message(t("Your comment follow up subscription for
|
| 363 |
this node was disabled. Thanks"));
|
| 364 |
$title = t('Disabled comment follow up subscription feature
|
| 365 |
for this node');
|
| 366 |
}
|
| 367 |
//Disable followup comments for a particular comment
|
| 368 |
else {
|
| 369 |
//Get the maximum cid for a particular comment of a givenid
|
| 370 |
$lstcid = db_fetch_object(db_query("SELECT max(zc.cid) as
|
| 371 |
maxcid FROM {z_commentsubscribe} zc
|
| 372 |
LEFT JOIN {comments} c on c.cid = zc.cid
|
| 373 |
LEFT OUTER JOIN {users} u ON u.uid=c.uid
|
| 374 |
WHERE subscribe = 1 AND instr(parents ,'%s') AND md5(IF(
|
| 375 |
LENGTH( c.mail ) <1, u.mail, c.mail ) ) = '%s'",
|
| 376 |
$arg,arg(4)));
|
| 377 |
if($lstcid->maxcid)
|
| 378 |
$cid = $lstcid->maxcid;
|
| 379 |
else
|
| 380 |
$cid = $arg;
|
| 381 |
db_query("UPDATE {comments} c , {users} u ,
|
| 382 |
{z_commentsubscribe} cs
|
| 383 |
SET cs.subscribe = 0, cs.subscribenode = 0
|
| 384 |
WHERE c.cid=cs.cid AND u.uid=c.uid
|
| 385 |
AND c.cid = %d AND md5(IF( LENGTH( c.mail ) <1, u.mail,
|
| 386 |
c.mail ) ) = '%s' ",$cid,arg(4));
|
| 387 |
drupal_set_message(t("Your comment follow up subscription
|
| 388 |
for this post was disabled. Thanks"));
|
| 389 |
$title = t('Disabled comment follow up subscription feature
|
| 390 |
for this comment');
|
| 391 |
}
|
| 392 |
break;
|
| 393 |
default;
|
| 394 |
$title = t('Comment Subscribe');
|
| 395 |
break;
|
| 396 |
}
|
| 397 |
drupal_set_title($title);
|
| 398 |
drupal_set_breadcrumb($breadcrumb);
|
| 399 |
print theme("page", $page_content);
|
| 400 |
}
|