| 1 |
<?php
|
| 2 |
/* $Id$ */
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
*
|
| 6 |
* This module is a workaround for the bug preventing comment links from
|
| 7 |
* always working. See http://drupal.org/node/26966 for more info.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/*
|
| 11 |
* Implementation of hook_help()
|
| 12 |
*/
|
| 13 |
function comment_redirect_help($section) {
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/help#comment_redirect':
|
| 16 |
$output .= '<p>' . t("There's a bug in drupal that causes comment links to go to the wrong node page on occasion. If you're not familiar with the issue, the discussion is at http://drupal.org/node/26966.") . '</p>';
|
| 17 |
$output .= '<p>' . t("As of this writing, the bug has been pushed off to drupal 7. I needed something sooner, so I took one of the patches (http://drupal.org/node/26966#comment-335956) and made it into a module. I'll be very happy when I can deprecate this module b/c it'll mean the bug'll finally be fixed.") . '</p>';
|
| 18 |
break;
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
/*
|
| 23 |
* Implementation of hook_menu()
|
| 24 |
*/
|
| 25 |
function comment_redirect_menu($may_cache) {
|
| 26 |
$items = array();
|
| 27 |
if ($may_cache) {
|
| 28 |
}
|
| 29 |
else {
|
| 30 |
if (arg(0) == "comment_redirect" && is_numeric(arg(1))) {
|
| 31 |
$items[] = array(
|
| 32 |
'path' => 'comment_redirect/' . arg(1),
|
| 33 |
'title' => t('Redirecting comment ' . arg(1)),
|
| 34 |
'access' => 'access comments',
|
| 35 |
'type' => MENU_CALLBACK,
|
| 36 |
'callback' => 'comment_redirect_redirect_comment',
|
| 37 |
'callback arguments' => array(arg(1)),
|
| 38 |
);
|
| 39 |
}
|
| 40 |
}
|
| 41 |
return $items;
|
| 42 |
}
|
| 43 |
|
| 44 |
|
| 45 |
/*
|
| 46 |
* Implementation of hook_views_tables_alter()
|
| 47 |
*/
|
| 48 |
function comment_redirect_views_tables_alter(&$tables) {
|
| 49 |
$tables['comments']['fields']['subject']['handler'] = array(
|
| 50 |
'views_handler_field_comment_redirect_link' => 'Normal',
|
| 51 |
'views_handler_field_comment_redirect_link_with_mark' => 'With Updated Mark',
|
| 52 |
);
|
| 53 |
}
|
| 54 |
|
| 55 |
/*
|
| 56 |
* Format a field as a link to a comment.
|
| 57 |
*/
|
| 58 |
function views_handler_field_comment_redirect_link($fieldinfo, $fielddata, $value, $data) {
|
| 59 |
if ($fielddata['options'] == 'nolink') {
|
| 60 |
return check_plain($value);
|
| 61 |
}
|
| 62 |
return l($value, "comment_redirect/" . $data->comments_cid);
|
| 63 |
}
|
| 64 |
|
| 65 |
/*
|
| 66 |
* Format a field as a link to a 'mark', stating whether or not the comment has
|
| 67 |
* updated since it was last viewed by the user.
|
| 68 |
*/
|
| 69 |
function views_handler_field_comment_redirect_link_with_mark($fieldinfo, $fielddata, $value, $data) {
|
| 70 |
if ($fielddata['options'] == 'nolink') {
|
| 71 |
$link = check_plain($value);
|
| 72 |
}
|
| 73 |
else {
|
| 74 |
$link = l($value, "comment_redirect/" . $data->comments_cid);
|
| 75 |
}
|
| 76 |
return $link .' '. theme('mark', node_mark($data->nid, $data->comments_timestamp));
|
| 77 |
}
|
| 78 |
|
| 79 |
/*
|
| 80 |
* This function generates the proper link to the comment
|
| 81 |
*/
|
| 82 |
function comment_redirect_redirect_comment($cid) {
|
| 83 |
global $user;
|
| 84 |
$comment = _comment_load($cid);
|
| 85 |
if (empty($comment)) {
|
| 86 |
drupal_not_found();
|
| 87 |
}
|
| 88 |
$order = _comment_get_display_setting('sort');
|
| 89 |
$mode = _comment_get_display_setting('mode');
|
| 90 |
$comments_per_page = _comment_get_display_setting('comments_per_page');
|
| 91 |
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
|
| 92 |
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
|
| 93 |
$sqland = ' AND c.timestamp > %d' ;
|
| 94 |
}
|
| 95 |
else {
|
| 96 |
$sqland = ' AND c.thread > "%s"' ;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
|
| 100 |
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
|
| 101 |
$sqland = ' AND c.timestamp < %d' ;
|
| 102 |
}
|
| 103 |
else {
|
| 104 |
$sqland = ' AND SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1)) < SUBSTRING("%s", 1, (LENGTH("%s") - 1))';
|
| 105 |
}
|
| 106 |
}
|
| 107 |
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
|
| 108 |
$commentresult = db_query('SELECT count(*) as cnt FROM {comments} c WHERE c.nid=' . $comment->nid . $sqland, $comment->timestamp );
|
| 109 |
}
|
| 110 |
else {
|
| 111 |
$commentresult = db_query('SELECT count(*) as cnt FROM {comments} c WHERE c.nid=' . $comment->nid . $sqland, $comment->thread, $comment->thread);
|
| 112 |
}
|
| 113 |
$commentcnt = db_fetch_object($commentresult);
|
| 114 |
$page = floor($commentcnt->cnt / _comment_get_display_setting('comments_per_page'));
|
| 115 |
$query = ($page > 0)?('page=' . $page) : '';
|
| 116 |
drupal_goto('node/' . $comment->nid, $query, 'comment-' . $comment->cid);
|
| 117 |
}
|