| 1 |
<?php
|
| 2 |
// $Id: talk.module,v 1.5 2008/03/24 02:50:57 cwgordon7 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Comments are displayed in a separate 'talk' tab, for node types you wish
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function talk_help($path, $arg) {
|
| 13 |
switch ($path) {
|
| 14 |
case 'admin/help#talk':
|
| 15 |
$output = '<p>'. t('The talk module gives you the option to display comments on a seperate tab. The option is per content type and can be set in the workflow options of a content type.') .'</p>';
|
| 16 |
return $output;
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_menu().
|
| 22 |
*/
|
| 23 |
function talk_menu() {
|
| 24 |
$items = array();
|
| 25 |
$items['node/%node/talk'] = array(
|
| 26 |
'title callback' => 'talk_menu_title',
|
| 27 |
'page callback' => 'talk_handle',
|
| 28 |
'page arguments' => array(1),
|
| 29 |
'access callback' => '_talk_access',
|
| 30 |
'access arguments' => array('access comments', 1),
|
| 31 |
'type' => MENU_LOCAL_TASK,
|
| 32 |
'weight' => 1,
|
| 33 |
);
|
| 34 |
$items['admin/settings/talk'] = array(
|
| 35 |
'title' => t('Talk page'),
|
| 36 |
'description' => t('Configure settings for the talk page.'),
|
| 37 |
'page callback' => 'drupal_get_form',
|
| 38 |
'page arguments' => array('talk_admin_form'),
|
| 39 |
);
|
| 40 |
return $items;
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Helper item for talk_menu: access callback.
|
| 45 |
*/
|
| 46 |
function _talk_access($perm, $node) {
|
| 47 |
return (user_access($perm) && talk_activated($node->type) && $node->nid && _talk_node_comment_value($node));
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Menu call back for admin form.
|
| 52 |
*/
|
| 53 |
function talk_admin_form() {
|
| 54 |
$form = array();
|
| 55 |
$form['talk_title'] = array(
|
| 56 |
'#type' => 'textfield',
|
| 57 |
'#title' => t('Title of the "talk" page'),
|
| 58 |
'#default_value' => talk_title(),
|
| 59 |
);
|
| 60 |
return system_settings_form($form);
|
| 61 |
}
|
| 62 |
|
| 63 |
function talk_title($title = NULL) {
|
| 64 |
if (is_null($title)) {
|
| 65 |
return variable_get('talk_title', t('Talk'));
|
| 66 |
}
|
| 67 |
variable_set('talk_title', $title);
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Menu callback for the title, as it would automatically pass args in, which would be bad.
|
| 72 |
*/
|
| 73 |
function talk_menu_title() {
|
| 74 |
return talk_title();
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Menu callback for talk page.
|
| 79 |
*/
|
| 80 |
function talk_handle($node) {
|
| 81 |
drupal_set_title($node->title);
|
| 82 |
$add_comments = _talk_node_comment_value($node) == COMMENT_NODE_READ_WRITE && user_access('post comments');
|
| 83 |
return theme('talkpage', $node, $add_comments);
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Implementation of hook_nodeapi().
|
| 88 |
*/
|
| 89 |
function talk_nodeapi(&$node, $op) {
|
| 90 |
switch ($op) {
|
| 91 |
case 'load':
|
| 92 |
if (talk_activated($node->type) && arg(0) == 'node' && !arg(2)) {
|
| 93 |
// Overwrite setting of comment module and set comments for this node to disabled.
|
| 94 |
// This prevents the comments of being displayed.
|
| 95 |
$output['comment_original_value'] = $node->comment;
|
| 96 |
$output['comment'] = 0;
|
| 97 |
return $output;
|
| 98 |
}
|
| 99 |
break;
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* Implementation of hook_link().
|
| 105 |
*/
|
| 106 |
function talk_link($type, $node = null, $teaser = false) {
|
| 107 |
if ($type == 'node' && talk_activated($node->type) && user_access('access comments')) {
|
| 108 |
$result = array();
|
| 109 |
if ($node->comment_count) {
|
| 110 |
$result['talk_view'] = array(
|
| 111 |
'title' => t('@title page (@nr comments)', array('@nr' => $node->comment_count, '@title' => talk_title())),
|
| 112 |
'href' => 'node/'. $node->nid .'/talk',
|
| 113 |
);
|
| 114 |
}
|
| 115 |
elseif (_talk_node_comment_value($node) == COMMENT_NODE_READ_WRITE) {
|
| 116 |
$result['talk_view'] = array(
|
| 117 |
'title' => t('Add new comment'),
|
| 118 |
'href' => "comment/reply/$node->nid",
|
| 119 |
);
|
| 120 |
}
|
| 121 |
return $result;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implementation of hook_form_alter().
|
| 127 |
*/
|
| 128 |
function talk_form_alter(&$form, $form_state, $form_id) {
|
| 129 |
// Add option to comment options of node types.
|
| 130 |
if ($form_id == 'node_type_form' && isset($form['identity']['type']) && module_exists('comment')) {
|
| 131 |
$form['workflow']['comment_talk'] = array(
|
| 132 |
'#type' => 'checkbox',
|
| 133 |
'#title' => t('Display comments on separate talk page'),
|
| 134 |
'#prefix' => '<strong>'. t('Talk pages:') .'</strong>',
|
| 135 |
'#weight' => 5,
|
| 136 |
'#default_value' => talk_activated($form['#node_type']->type),
|
| 137 |
);
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
/**
|
| 142 |
* Implementation of hook_link_alter().
|
| 143 |
*/
|
| 144 |
function talk_link_alter(&$links, $node) {
|
| 145 |
if (talk_activated($node->type)) {
|
| 146 |
unset($links['comment_add']);
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Implementation of hook_comment()
|
| 152 |
* Changing the destination to the talk page after posting a comment
|
| 153 |
*/
|
| 154 |
function talk_comment($a1, $op) {
|
| 155 |
if ($op == 'insert' || $op == 'update') {
|
| 156 |
$nid = $a1['nid'];
|
| 157 |
$node = node_load($nid);
|
| 158 |
if (talk_activated($node->type)) {
|
| 159 |
$_REQUEST['destination'] = 'node/'. $a1['nid'] .'/talk';
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
/**
|
| 164 |
* Is talk page option activated for node tpye?
|
| 165 |
*/
|
| 166 |
function talk_activated($node_type, $value = NULL) {
|
| 167 |
if (is_null($value)) {
|
| 168 |
return variable_get('comment_talk_'. $node_type, FALSE);
|
| 169 |
}
|
| 170 |
variable_set('comment_talk_'. $node_type, $value);
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Value of 'comment' of node.
|
| 175 |
*/
|
| 176 |
function _talk_node_comment_value(&$node) {
|
| 177 |
return isset($node->comment_original_value) ? $node->comment_original_value : $node->comment;
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Implementation of hook_theme().
|
| 182 |
*/
|
| 183 |
function talk_theme() {
|
| 184 |
return array(
|
| 185 |
'talkpage' => array(
|
| 186 |
'template' => 'talkpage',
|
| 187 |
'arguments' => array('node' => NULL, 'add_comments' => NULL),
|
| 188 |
),
|
| 189 |
);
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Theme talkpage for node
|
| 194 |
* @param $node
|
| 195 |
* node whose talk page is displayed
|
| 196 |
* @param $add_comments
|
| 197 |
* boolean which indicates if the adding of comments is allowed for current user
|
| 198 |
* @ingroup themeable
|
| 199 |
*/
|
| 200 |
function template_preprocess_talkpage(&$variables) {
|
| 201 |
$add_comments = $variables['add_comments'];
|
| 202 |
$node = $variables['node'];
|
| 203 |
$variables['title'] = talk_title();
|
| 204 |
$comment_link = '';
|
| 205 |
if ($add_comments) {
|
| 206 |
$comment_link = l(t('Add new comment'), "comment/reply/$node->nid");
|
| 207 |
}
|
| 208 |
$comments = comment_render($node, 0);
|
| 209 |
$redisplay = FALSE;
|
| 210 |
if ($node->comment_count > 1 && $add_comments) {
|
| 211 |
$redisplay = TRUE;
|
| 212 |
}
|
| 213 |
$variables['redisplay'] = $redisplay;
|
| 214 |
$variables['comment_link'] = $comment_link;
|
| 215 |
$variables['comments'] = $comments;
|
| 216 |
}
|