| 1 |
<?php
|
| 2 |
// vim: set filetype=php:
|
| 3 |
/**
|
| 4 |
* commentify.module
|
| 5 |
* Tie drupal content to external CMS via unique ID number (fid)
|
| 6 |
*
|
| 7 |
* This module handles the creation, linking and cleanup of nodes
|
| 8 |
* associated with an external CMS for the purpose of adding comments.
|
| 9 |
* The module itself only generates the backend data, modificiation
|
| 10 |
* of the commentify.js script is required to render comments to your
|
| 11 |
* specifications on the third party site.
|
| 12 |
*
|
| 13 |
*/
|
| 14 |
|
| 15 |
function commentify_menu($may_cache) {
|
| 16 |
$items = array();
|
| 17 |
if ($may_cache) {
|
| 18 |
$items[] = array(
|
| 19 |
'path' => 'admin/content/comments/commentify',
|
| 20 |
'title' => t('Commentify'),
|
| 21 |
'description' => t('Enables linking of comments to content on third party sites.'),
|
| 22 |
'callback' => 'drupal_get_form',
|
| 23 |
'callback arguments' => array('commentify_admin_settings'),
|
| 24 |
'access' => user_access('administer site configuration'),
|
| 25 |
'type' => MENU_NORMAL_ITEM
|
| 26 |
);
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'commentify/fid',
|
| 29 |
'title' => TRUE,
|
| 30 |
'callback' => 'commentify_display',
|
| 31 |
// 'callback arguments' => arg(2),
|
| 32 |
'access' => user_access('access content'),
|
| 33 |
'type' => MENU_CALLBACK
|
| 34 |
);
|
| 35 |
// Legacy path handler
|
| 36 |
$items[] = array(
|
| 37 |
'path' => 'workbench/fid',
|
| 38 |
'title' => TRUE,
|
| 39 |
'callback' => 'commentify_display',
|
| 40 |
// 'callback arguments' => arg(2),
|
| 41 |
'access' => user_access('access content'),
|
| 42 |
'type' => MENU_CALLBACK
|
| 43 |
);
|
| 44 |
} else {
|
| 45 |
$items[] = array(
|
| 46 |
'path' => 'commentify/add',
|
| 47 |
'title' => t('Add Comment'),
|
| 48 |
'callback' => 'commentify_checknode',
|
| 49 |
'access' => user_access("access content"),
|
| 50 |
'type' => MENU_CALLBACK
|
| 51 |
);
|
| 52 |
// Legacy path handler
|
| 53 |
$items[] = array(
|
| 54 |
'path' => 'workbench/add',
|
| 55 |
'title' => t('Add Comment'),
|
| 56 |
'callback' => 'commentify_checknode',
|
| 57 |
'access' => user_access("access content"),
|
| 58 |
'type' => MENU_CALLBACK
|
| 59 |
);
|
| 60 |
$items[] = array(
|
| 61 |
'path' => 'commentify/cleanup',
|
| 62 |
'title' => t('Cleanup'),
|
| 63 |
'callback' => 'commentify_cleanup',
|
| 64 |
'access' => TRUE,
|
| 65 |
'type' => MENU_CALLBACK
|
| 66 |
);
|
| 67 |
}
|
| 68 |
return $items;
|
| 69 |
} // function commentify_menu
|
| 70 |
|
| 71 |
function commentify_admin_settings() {
|
| 72 |
|
| 73 |
// Get all available node types
|
| 74 |
$types = array_map( create_function('$t', 'return $t->name;'), node_get_types());//node_get_types('name');
|
| 75 |
|
| 76 |
$form = array();
|
| 77 |
|
| 78 |
$form['commentify_nodesettings'] = array(
|
| 79 |
'#type' => 'fieldset',
|
| 80 |
'#title' => t('Node creation settings'),
|
| 81 |
'#weight' => -10,
|
| 82 |
'#collapsible' => TRUE,
|
| 83 |
);
|
| 84 |
|
| 85 |
$form['commentify_nodesettings']['commentify_content_type'] = array(
|
| 86 |
'#type' => 'select',
|
| 87 |
'#title' => t('Content Type'),
|
| 88 |
'#default_value' => variable_get('commentify_content_type','story'),
|
| 89 |
'#options' => $types,
|
| 90 |
'#description' => t('Content type to use for new nodes created by commentify module'),
|
| 91 |
);
|
| 92 |
|
| 93 |
$form['commentify_nodesettings']['commentify_site'] = array(
|
| 94 |
'#type' => 'textfield',
|
| 95 |
'#title' => t('Site URL'),
|
| 96 |
'#default_value' => variable_get('commentify_site','islandpacket.com'),
|
| 97 |
'#size' => 30,
|
| 98 |
'#maxlength' => 255,
|
| 99 |
'#description' => t('The URL of the site to accept submissions from'),
|
| 100 |
);
|
| 101 |
|
| 102 |
$form['commentify_nodesettings']['commentify_options'] = array(
|
| 103 |
'#type' => 'checkboxes',
|
| 104 |
'#title' => t('Default options'),
|
| 105 |
'#default_value' => variable_get('commentify_options',array()),
|
| 106 |
'#options' => array (
|
| 107 |
'status' => t('Published'),
|
| 108 |
'moderate' => t('In moderation queue'),
|
| 109 |
'promote' => t('Promoted to front page'),
|
| 110 |
'revision' => t('Create new revision'),
|
| 111 |
),
|
| 112 |
'#description' => t('Set default options for nodes published by commentify.module'),
|
| 113 |
);
|
| 114 |
|
| 115 |
$form['commentify_nodesettings']['commentify_comment_option'] = array(
|
| 116 |
'#type' => 'radios',
|
| 117 |
'#title' => 'Comment Options',
|
| 118 |
'#default_value' => variable_get('commentify_comment_option','Read/write'),
|
| 119 |
'#options' => array(t('Disabled'), t('Read only'), t('Read/write')),
|
| 120 |
);
|
| 121 |
|
| 122 |
$form['commentify_cleanup'] = array(
|
| 123 |
'#type' => 'fieldset',
|
| 124 |
'#title' => t('Cleanup settings'),
|
| 125 |
'#weight' => -5,
|
| 126 |
'#collapsible' => TRUE,
|
| 127 |
'#collapsed' => TRUE,
|
| 128 |
'#description' => 'Control automatic cleanup of uncommented nodes.'
|
| 129 |
);
|
| 130 |
|
| 131 |
$form['commentify_cleanup']['commentify_delete'] = array(
|
| 132 |
'#type' => 'checkbox',
|
| 133 |
'#title' => t('Delete nodes without comments automatically.'),
|
| 134 |
'#default_value' => variable_get('commentify_delete',TRUE),
|
| 135 |
);
|
| 136 |
|
| 137 |
$form['commentify_display'] = array(
|
| 138 |
'#type' => 'fieldset',
|
| 139 |
'#title' => t('Display settings'),
|
| 140 |
'#weight' => 0,
|
| 141 |
'#collapsible' => TRUE,
|
| 142 |
'#description' => t('Control the display of comments on third party site.'),
|
| 143 |
);
|
| 144 |
|
| 145 |
$form['commentify_display']['commentify_display_type'] = array(
|
| 146 |
'#type' => 'radios',
|
| 147 |
'#title' => 'Display type',
|
| 148 |
'#default_value' => variable_get('commentify_display_type',t('Text')),
|
| 149 |
'#options' => array(t('Text'),t('List')),
|
| 150 |
'#description' => t('How to display comments on 3rd party site - as text link, or list of comments.'),
|
| 151 |
);
|
| 152 |
|
| 153 |
// Settings for future use
|
| 154 |
$form['commentify_display']['commentify_display_count'] = array(
|
| 155 |
'#type' => 'textfield',
|
| 156 |
'#title' => 'Number of comments',
|
| 157 |
'#default_value' => variable_get('commentify_display_count',3),
|
| 158 |
'#size' => 2,
|
| 159 |
'#maxlength' => 3,
|
| 160 |
'#description' => t('How many comments should be shown on 3rd party page. Use 0 to show all.'),
|
| 161 |
// '#attributes' => array('disabled' => 'disabled'),
|
| 162 |
);
|
| 163 |
|
| 164 |
$form['commentify_display']['commentify_display_commentform'] = array(
|
| 165 |
'#type' => 'checkbox',
|
| 166 |
'#title' => 'Display comment form',
|
| 167 |
'#default_value' => variable_get('commentify_display_commentform',TRUE),
|
| 168 |
'#description' => t('If comments are displayed, show comment submission form also.'),
|
| 169 |
// '#attributes' => array('disabled' => 'disabled'),
|
| 170 |
);
|
| 171 |
|
| 172 |
return system_settings_form($form);
|
| 173 |
} // function commentify_settings
|
| 174 |
|
| 175 |
function commentify_createnode($headline,$body,$domain,$type) {
|
| 176 |
$options = variable_get('commentify_options',array());
|
| 177 |
$node->title = sprintf("%s", $headline);
|
| 178 |
$node->body = $body;
|
| 179 |
$node->type = $type;
|
| 180 |
$node->comment = variable_get('commentify_comment_option',FALSE);
|
| 181 |
foreach (array('status', 'moderate', 'promote', 'revision') as $key) {
|
| 182 |
if ($options[$key]) {
|
| 183 |
$node->$key = in_array($key, $options);
|
| 184 |
}
|
| 185 |
}
|
| 186 |
return $node;
|
| 187 |
} // function commentify_createnode
|
| 188 |
|
| 189 |
function commentify_checknode() {
|
| 190 |
$output = "";
|
| 191 |
|
| 192 |
$fid = $_POST['fid'];
|
| 193 |
$nid = db_result(db_query("SELECT nid from {commentify} where fid = %d",$fid));
|
| 194 |
|
| 195 |
$domain = variable_get('commentify_site','null');
|
| 196 |
$type = variable_get('commentify_content_type','null');
|
| 197 |
|
| 198 |
if ($nid) {
|
| 199 |
header("Location:/node/$nid");
|
| 200 |
return;
|
| 201 |
} elseif (!$nid && preg_match("/$domain/",$_SERVER['HTTP_REFERER'])) {
|
| 202 |
$node = commentify_createnode($_POST['headline'],$_POST['body'],$domain,$type);
|
| 203 |
node_save($node);
|
| 204 |
$nid = $node->nid;
|
| 205 |
db_query("INSERT INTO {commentify} (fid,nid) VALUES (%d,%d)",$fid,$nid);
|
| 206 |
header("Location:/node/$nid");
|
| 207 |
return;
|
| 208 |
} else {
|
| 209 |
$output .= "Sorry, that site not authorized to submit.";
|
| 210 |
return $output;
|
| 211 |
}
|
| 212 |
|
| 213 |
} // function commentify_checknode
|
| 214 |
|
| 215 |
function commentify_update_wb_table() {
|
| 216 |
// Cleans up commentify table to remove deleted nids
|
| 217 |
db_query("delete {commentify}.* from {commentify} left join {node} using (nid) where {node}.nid is null");
|
| 218 |
} // function commentify_update_wb_table
|
| 219 |
|
| 220 |
function commentify_cleanup() {
|
| 221 |
if (variable_get('commentify_delete',FALSE)) {
|
| 222 |
// Clean up nodes with no comments
|
| 223 |
$result = db_query("select {node}.nid from {node} left join {comments} on {node}.nid={comments}.nid where {comments}.nid is null and {node}.type='%s'",variable_get('commentify_content_type',FALSE));
|
| 224 |
$list = array();
|
| 225 |
|
| 226 |
while ($todelete = db_fetch_object($result)) {
|
| 227 |
array_push($list, $todelete->nid);
|
| 228 |
}
|
| 229 |
|
| 230 |
foreach ($list as $i) {
|
| 231 |
node_delete($i);
|
| 232 |
}
|
| 233 |
|
| 234 |
}
|
| 235 |
// Cleanup commentify table itself
|
| 236 |
$message = format_plural(count($list), "Commentify deleted 1 node.", "Commentify deleted @count nodes.");
|
| 237 |
echo $message;
|
| 238 |
watchdog("content", $message, $severity = WATCHDOG_NOTICE, $link = NULL);
|
| 239 |
commentify_update_wb_table();
|
| 240 |
} // function commentify_cleanup
|
| 241 |
|
| 242 |
function commentify_getnid($fid) {
|
| 243 |
$nid = db_result(db_query("SELECT nid FROM {commentify} WHERE fid=%d",$fid));
|
| 244 |
return $nid;
|
| 245 |
}
|
| 246 |
|
| 247 |
function commentify_getcommentcount($nid) {
|
| 248 |
$count = db_result(db_query("select count(c.cid) from {comments} c, {node} n where n.nid=c.nid and n.nid=%d group by c.nid",$nid));
|
| 249 |
return $count;
|
| 250 |
}
|
| 251 |
|
| 252 |
function commentify_getcomments($nid) {
|
| 253 |
$result = db_query("select c.subject, c.comment, c.timestamp, u.name from {comments} c, {node} n, {users} u where n.nid=c.nid and u.uid = c.uid and n.nid=%d limit %d",$nid,variable_get('commentify_display_count',5));
|
| 254 |
$comments = array();
|
| 255 |
while ($comment = db_fetch_array($result)) {
|
| 256 |
if (in_array("quote",module_list())) {
|
| 257 |
$comment['comment'] = _quote_filter_process($comment['comment']);
|
| 258 |
}
|
| 259 |
$comments[] = $comment;
|
| 260 |
}
|
| 261 |
return $comments;
|
| 262 |
}
|
| 263 |
|
| 264 |
function commentify_display($fid) {
|
| 265 |
$type = variable_get('commentify_display_type',t('Text'));
|
| 266 |
$output = '';
|
| 267 |
$count = '';
|
| 268 |
$nid = commentify_getnid($fid);
|
| 269 |
if (!$nid) {
|
| 270 |
$count = 0;
|
| 271 |
} else {
|
| 272 |
$count = commentify_getcommentcount($nid);
|
| 273 |
}
|
| 274 |
|
| 275 |
switch ($type) {
|
| 276 |
case 0:
|
| 277 |
// If display type is text link, create javascript
|
| 278 |
$string = $count;
|
| 279 |
$string .= " Comment";
|
| 280 |
if ($count != 1) { $string = $string.'s'; }
|
| 281 |
$output .= 'document.write("'.$string.'");';
|
| 282 |
break;
|
| 283 |
|
| 284 |
case 1:
|
| 285 |
// If display type is list, create json object
|
| 286 |
header('Content-Type: application/x-javascript; charset=utf-8');
|
| 287 |
$js = commentify_getcomments($nid);
|
| 288 |
$js = $_GET['callback'].'({"url":"'. url('node/'.$nid,null,null,true) .'","count":"'.$count.'","comments":'.drupal_to_js($js)."})";
|
| 289 |
// $js = str_replace(array('[',']'),array('(',')'),$js);
|
| 290 |
// $js = preg_replace('/"(\w+)":/','\1:',$js);
|
| 291 |
|
| 292 |
$output .= $js;
|
| 293 |
|
| 294 |
// $output .= var_dump($js);
|
| 295 |
|
| 296 |
break;
|
| 297 |
}
|
| 298 |
|
| 299 |
print $output;
|
| 300 |
}
|
| 301 |
|
| 302 |
function commentify_cron() {
|
| 303 |
commentify_cleanup();
|
| 304 |
}
|