| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enables a dialogue between node author and one or more role groups.
|
| 7 |
*/
|
| 8 |
|
| 9 |
define('COMMENT_DIALOGUE_SUBJECT', 'Comment on [title] posted on [site-name]');
|
| 10 |
define('COMMENT_DIALOGUE_BODY', '
|
| 11 |
The following comment on [title] has been posted on [site-name] by [comment-author-name].
|
| 12 |
|
| 13 |
[comment-body]
|
| 14 |
|
| 15 |
|
| 16 |
The comment can be viewed here:
|
| 17 |
[comment-url]
|
| 18 |
|
| 19 |
|
| 20 |
-- [site-name] team');
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_help().
|
| 24 |
*/
|
| 25 |
function comment_dialogue_help($section) {
|
| 26 |
switch ($section) {
|
| 27 |
case 'admin/modules#description':
|
| 28 |
return t('Enables a dialogue between node author and one or more role groups.');
|
| 29 |
|
| 30 |
case 'admin/help#comment_dialogue':
|
| 31 |
return t('<p>Comment Dialogue facilitates a discussion between node authors and one or more role groups. The module sends an email notification to members of !selected_roles when a node author posts a comment to their own node and notification back to the node author when anyone else posts a comment on the node. Comment Dialogue can be enabled !per_node_type. There are no subscription options or the ability to "opt out" of notification. </p><p>It is recommended that access is restricted to just the node author and the role groups to view nodes and post comments where Comment Dialogue is enabled, for example with !Content_Access.</p>',
|
| 32 |
array(
|
| 33 |
'!selected_roles' => l('selected roles', 'admin/settings/comment_dialogue'),
|
| 34 |
'!per_node_type' => l('per nodetype', 'admin/content/type'),
|
| 35 |
'!Content_Access' => l('Content Access', 'http://drupal.org/project/content_Access')
|
| 36 |
));
|
| 37 |
|
| 38 |
case 'admin/settings/comment_dialogue':
|
| 39 |
return t('Comment Dialogue facilitates a discussion between node authors and one or more role groups. The module sends an email notification to members of roles selected below when a node author posts a comment to their own node and notification back to the node author when anyone else posts a comment on the node. Comment Dialogue can be enabled !per_node_type.', array('!per_node_type' => l('per nodetype', 'admin/content/type')));
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_menu().
|
| 45 |
*/
|
| 46 |
function comment_dialogue_menu($may_cache) {
|
| 47 |
$items = array();
|
| 48 |
if ($may_cache) {
|
| 49 |
$items[] = array(
|
| 50 |
'path' => 'admin/settings/comment_dialogue',
|
| 51 |
'title' => 'Comment Dialogue',
|
| 52 |
'description' => t('Enables a dialogue between node author and one or more role groups.'),
|
| 53 |
'callback' => 'drupal_get_form',
|
| 54 |
'callback arguments' => 'comment_dialogue_admin_settings',
|
| 55 |
'type' => MENU_NORMAL_ITEM,
|
| 56 |
'access' => user_access('administer site configuration'), //do we *really* need yet another permission??
|
| 57 |
);
|
| 58 |
}
|
| 59 |
return $items;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Menu callback
|
| 64 |
*
|
| 65 |
* @return
|
| 66 |
* array of form content.
|
| 67 |
*/
|
| 68 |
function comment_dialogue_admin_settings() {
|
| 69 |
$role_names = array();
|
| 70 |
$sql = "SELECT * FROM {role} r WHERE r.rid > 2 ORDER BY name";
|
| 71 |
$results = db_query($sql);
|
| 72 |
while ($result = db_fetch_object($results)) {
|
| 73 |
$role_names[$result->rid] = $result->name;
|
| 74 |
}
|
| 75 |
|
| 76 |
$form['comment_dialogue_roles'] = array(
|
| 77 |
'#type' => 'select',
|
| 78 |
'#title' => t('Dialogue Roles'),
|
| 79 |
'#options' => $role_names,
|
| 80 |
'#default_value' => variable_get('comment_dialogue_roles', NULL),
|
| 81 |
'#description' => t('Select roles to dialogue with node author.'),
|
| 82 |
'#multiple' => TRUE,
|
| 83 |
);
|
| 84 |
|
| 85 |
$form['comment_dialogue_msg'] = array(
|
| 86 |
'#type' => 'textfield',
|
| 87 |
'#size' => 100,
|
| 88 |
'#title' => t('Author Status Message'),
|
| 89 |
'#default_value' => variable_get('comment_dialogue_msg', t('Comment notification sent to members of !site-name !role-names.')),
|
| 90 |
'#description' => t('Node author will be shown this message when submitting a comment on their own node.'),
|
| 91 |
);
|
| 92 |
|
| 93 |
$form['email'] = array(
|
| 94 |
'#type' => 'fieldset',
|
| 95 |
'#title' => t('Notification email'),
|
| 96 |
);
|
| 97 |
|
| 98 |
$form['email']['comment_dialogue_subject'] = array(
|
| 99 |
'#type' => 'textfield',
|
| 100 |
'#size' => 100,
|
| 101 |
'#title' => t('Subject'),
|
| 102 |
'#default_value' => variable_get('comment_dialogue_subject', COMMENT_DIALOGUE_SUBJECT),
|
| 103 |
);
|
| 104 |
|
| 105 |
$form['email']['comment_dialogue_body'] = array(
|
| 106 |
'#type' => 'textarea',
|
| 107 |
'#rows' => 6,
|
| 108 |
'#cols' => 60,
|
| 109 |
'#title' => t('Body'),
|
| 110 |
'#default_value' => variable_get('comment_dialogue_body', COMMENT_DIALOGUE_BODY),
|
| 111 |
);
|
| 112 |
|
| 113 |
$form['email']['help'] = array(
|
| 114 |
'#type' => 'fieldset',
|
| 115 |
'#collapsible' => TRUE,
|
| 116 |
'#collapsed' => TRUE,
|
| 117 |
'#title' => t('Tokens'),
|
| 118 |
'#description' => t('The following tokens are available for the Notification subject and body:'),
|
| 119 |
);
|
| 120 |
|
| 121 |
if (module_exists('token')) {
|
| 122 |
$form['email']['help']['tokens'] = array(
|
| 123 |
'#value' => theme('token_help', 'comment'),
|
| 124 |
);
|
| 125 |
}
|
| 126 |
else {
|
| 127 |
$form['email']['help']['#value'] = t('You may use [site-name], [title], [author-name], [url], [comment-author-name], [comment-body], and [comment-url]. Additional tokens are available if the !token module is installed', array('!token' => l('Token', 'http://www.drupal.org/project/token')));
|
| 128 |
$form['email']['help']['#collapsible'] = FALSE;
|
| 129 |
$form['email']['help']['#collapsed'] = FALSE;
|
| 130 |
}
|
| 131 |
|
| 132 |
return system_settings_form($form);
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Implementation of hook_form_alter().
|
| 137 |
*/
|
| 138 |
function comment_dialogue_form_alter($form_id, &$form) {
|
| 139 |
// Add checkbox to activate per node type
|
| 140 |
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
|
| 141 |
// Developers: this will group all comment-related node content type
|
| 142 |
// settings. Copy this code block into your module and we can keep the form
|
| 143 |
// more intuitive until a standard arises.
|
| 144 |
if (!isset($form['comment_options'])) {
|
| 145 |
// create Comment Options fieldset
|
| 146 |
$form['comment_options'] = array(
|
| 147 |
'#type' => 'fieldset',
|
| 148 |
'#title' => t('Comment Options'),
|
| 149 |
'#collapsible' => TRUE,
|
| 150 |
'#weight' => 10 //place below the Workflow fieldset
|
| 151 |
);
|
| 152 |
// move default Comment setting to fieldset
|
| 153 |
if (isset($form['workflow']['comment'])) {
|
| 154 |
$form['comment_options']['comment'] = $form['workflow']['comment'];
|
| 155 |
$form['comment_options']['comment']['#weight'] = -10;
|
| 156 |
unset($form['workflow']['comment']);
|
| 157 |
}
|
| 158 |
}
|
| 159 |
// end comment option grouping
|
| 160 |
|
| 161 |
$form['comment_options']['comment_dialogue'] = array(
|
| 162 |
'#type' => 'checkbox',
|
| 163 |
'#title' => t('Enable Comment Dialogue for this node type'),
|
| 164 |
'#default_value' => variable_get('comment_dialogue_'. $form['#node_type']->type, 0),
|
| 165 |
'#options' => array(t('Disabled'), t('Enabled')),
|
| 166 |
'#description' => t('Check this box to enable email notification to one or more role groups when a node author posts a comment to their own node and notification back to the node author when anyone else posts a comment on the node.'),
|
| 167 |
);
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Implementation of hook_comment
|
| 173 |
*/
|
| 174 |
function comment_dialogue_comment($comment, $op) {
|
| 175 |
switch ($op) {
|
| 176 |
case 'insert':
|
| 177 |
if ((int)$comment['nid'] && $node = node_load($comment['nid'])) {
|
| 178 |
if (!variable_get('comment_dialogue_'. $node->type, 0)) return;
|
| 179 |
|
| 180 |
if ($node->uid && $node->uid == $comment['uid']) {
|
| 181 |
//notify roles
|
| 182 |
$dialogue_roles = array_keys((array)variable_get('comment_dialogue_roles', NULL));
|
| 183 |
if (!count($dialogue_roles)) {
|
| 184 |
watchdog('comment dialogue', t('No dialogue roles have been set.'), WATCHDOG_ERROR);
|
| 185 |
return;
|
| 186 |
}
|
| 187 |
|
| 188 |
$result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (%s)', implode(",", $dialogue_roles));
|
| 189 |
while ($account = db_fetch_object($result)) {
|
| 190 |
$to[] = $account->mail;
|
| 191 |
}
|
| 192 |
|
| 193 |
//get role names
|
| 194 |
$role_names = strtr(implode(" and ", $dialogue_roles), user_roles()) . ' ';
|
| 195 |
$role_names .= (count($dialogue_roles) == 1) ? t('role') : t('roles');
|
| 196 |
if (_comment_dialogue_notify($to, $node, $comment)) {
|
| 197 |
drupal_set_message(t(variable_get('comment_dialogue_msg', t('Comment notification sent to members of !site-name !role-names.')), array('!site-name' => variable_get('site_name', 'Drupal'), '!role-names' => $role_names)));
|
| 198 |
}
|
| 199 |
else drupal_set_message(t('A problem occurred sending comment notification to members of !role-names.', array('!role-names' => $role_names)), 'error');
|
| 200 |
}
|
| 201 |
else {
|
| 202 |
//notify author
|
| 203 |
if ($account = user_load(array('uid' => $node->uid))) {
|
| 204 |
$to[] = $account->mail;
|
| 205 |
if (_comment_dialogue_notify($to, $node, $comment)) {
|
| 206 |
drupal_set_message(t('Comment notification sent to %name.', array('%name' => $account->name)));
|
| 207 |
}
|
| 208 |
else drupal_set_message(t('A problem occurred sending comment notification to %name.', array('%name' => $account->name)), 'error');
|
| 209 |
}
|
| 210 |
}
|
| 211 |
}
|
| 212 |
break;
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Internal function.
|
| 218 |
*/
|
| 219 |
function _comment_dialogue_notify($to = array(), &$node, &$comment) {
|
| 220 |
global $base_url;
|
| 221 |
|
| 222 |
$site_name = variable_get('site_name', 'Drupal');
|
| 223 |
$from = "$site_name <" . variable_get('site_mail', ini_get('sendmail_from')) . '>';
|
| 224 |
$subject = variable_get('comment_dialogue_subject', COMMENT_DIALOGUE_SUBJECT);
|
| 225 |
$body = variable_get('comment_dialogue_body', COMMENT_DIALOGUE_BODY);
|
| 226 |
|
| 227 |
if (module_exists('token')) {
|
| 228 |
$subject = token_replace($subject, 'comment', $comment);
|
| 229 |
$body = token_replace($body, 'comment', $comment);
|
| 230 |
}
|
| 231 |
else {
|
| 232 |
$variables = array(
|
| 233 |
'[site-name]' => $site_name,
|
| 234 |
'[title]' => $node->title,
|
| 235 |
'[author-name]' => $node->name,
|
| 236 |
'[url]' => $base_url . '/node/' . $node->nid,
|
| 237 |
'[comment-author-name]' => $comment['author'],
|
| 238 |
'[comment-body]' => strip_tags($comment['comment'], '<p><br /><br>'), //replaced by linebreaks in processing
|
| 239 |
'[comment-url]' => $base_url . '/node/' . $node->nid . '#comment-' . $comment['cid'],
|
| 240 |
);
|
| 241 |
$subject = strtr($subject, $variables);
|
| 242 |
$body = strtr($body, $variables);
|
| 243 |
}
|
| 244 |
$subject = str_replace(array("\r", "\n"), '', $subject);
|
| 245 |
$body = str_replace(array('<p>', '<br />', '<br>'), "\r\n", $body);
|
| 246 |
|
| 247 |
$send_status = TRUE;
|
| 248 |
foreach ($to as $email) {
|
| 249 |
if (drupal_mail('comment_dialogue', $email, $subject, $body, $from)) {
|
| 250 |
watchdog('comment dialogue', t('Sent comment notification email to %recipient', array('%recipient' => $email)), WATCHDOG_NOTICE);
|
| 251 |
}
|
| 252 |
else {
|
| 253 |
watchdog('comment dialogue', t('Unable to send comment notification email to %recipient', array('%recipient' => $email)), WATCHDOG_ERROR);
|
| 254 |
$send_status = FALSE;
|
| 255 |
}
|
| 256 |
}
|
| 257 |
return $send_status;
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
* Implementation of hook_token_values()
|
| 262 |
*
|
| 263 |
* Default comment tokens are rather incomplete.
|
| 264 |
*/
|
| 265 |
function comment_dialogue_token_values($type, $object = NULL) {
|
| 266 |
$values = array();
|
| 267 |
switch ($type) {
|
| 268 |
case 'comment':
|
| 269 |
$comment = $object;
|
| 270 |
if ((int)$comment['nid'] && $node = node_load($comment['nid'])) {
|
| 271 |
global $base_url;
|
| 272 |
$values['comment-body'] = strip_tags($comment['comment'], '<p><br /><br>'); //replaced by linebreaks in processing
|
| 273 |
$values['comment-url'] = $base_url . '/node/' . $node->nid . '#comment-' . $comment['cid'];
|
| 274 |
$values['nid'] = $node->nid;
|
| 275 |
$values['type'] = $node->type;
|
| 276 |
$values['type-name'] = node_get_types('name', $node->type);
|
| 277 |
$values['title'] = strip_tags($node->title);
|
| 278 |
$values['title-raw'] = $node->title;
|
| 279 |
$values['author-uid'] = $node->uid;
|
| 280 |
$values['author-name'] = strip_tags($node->name);
|
| 281 |
$values['author-name-raw'] = $node->name;
|
| 282 |
$values['url'] = $base_url . '/node/' . $node->nid;
|
| 283 |
}
|
| 284 |
break;
|
| 285 |
}
|
| 286 |
|
| 287 |
return $values;
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Implementation of hook_token_list()
|
| 292 |
*/
|
| 293 |
function comment_dialogue_token_list($type = 'all') {
|
| 294 |
if ($type == 'comment') {
|
| 295 |
$tokens['comment']['comment-url'] = t("Complete URL to comment");
|
| 296 |
$tokens['comment']['nid'] = t("Node ID");
|
| 297 |
$tokens['comment']['type'] = t("Node type");
|
| 298 |
$tokens['comment']['type-name'] = t("Node type (user-friendly version)");
|
| 299 |
$tokens['comment']['title'] = t("Node title");
|
| 300 |
$tokens['comment']['title-raw'] = t("Unfiltered node title. WARNING - raw user input.");
|
| 301 |
$tokens['comment']['author-uid'] = t("Node author's user id");
|
| 302 |
$tokens['comment']['author-name'] = t("Node author's user name");
|
| 303 |
$tokens['comment']['author-name-raw'] = t("Node author's user name. WARNING - raw user input.");
|
| 304 |
$tokens['comment']['url'] = t("Complete URL");
|
| 305 |
}
|
| 306 |
|
| 307 |
return $tokens;
|
| 308 |
}
|