<?php

/**
* Removed comment id anchor Moved to _phptemplate_variables(). Modified for
* easy navigation of new comments. $comment->skip_links formatted for
* theme_links().
*
* @param $comment
* comment object
* @param $links
* (optional) Array of unproccessed link information.
* @param $visible
* (optional) Switch to fold or expand comments.
*/
function phptemplate_comment_view($comment, $links = array(), $visible = 1) {
static $counter = array('total new' => NULL, 'comments per page' => NULL, 'new pos' => 0, 'pos' => 0);

$counter['pos']++; // Part of To-do.
$output = '';

$comment->new = node_mark($comment->nid, $comment->timestamp);

if ($comment->new != MARK_READ) {

// Get number of new comments and the defaults per page.
if ($counter['total new'] == NULL) {
$counter['total new'] = comment_num_new($comment->nid);
// To-do: New comment navigation with paging support.
$counter['comments per page'] = variable_get('comment_default_per_page', 50);
}
// Sequentially add new comment id's for skip link targets starting from 2nd count.
// First target must have an id of "new" to cooperate with the rest of Drupal.
$target = (($counter['total new'] >= $counter['new pos']++) && ($counter['new pos'] != 1))
? 'new-'. $counter['new pos'] : 'new';

// Previews and folded comments do not get skip links.
if (!isset($comment->preview) && $visible == 1) {

// Set attributes for theme_links(). id set for link target.
$comment->skip_links['attr'] = array('id' => $target, 'class' => 'links skip-links');
// Mimicking theme_mark() to inherit CSS styles. This is just a label.
$comment->skip_links['links']['marker'] = array('title' => t('new'));

// Construct next links.
if ($counter['total new'] > $counter['new pos'] && $counter['total new'] != 1) {
$fragment = 'new-'. ($counter['new pos'] + 1);
$comment->skip_links['links']['next-link'] =
array('title' => t('next'), 'href' => $_GET['q'], 'fragment' => $fragment);
}
// Construct previous links.
if ($counter['total new'] >= $counter['new pos'] && $counter['new pos'] != 1) {
$fragment = $counter['new pos'] != 2 ? 'new-'. ($counter['new pos'] - 1) : 'new';
$comment->skip_links['links']['previous-link'] =
array('title' => t('previous'), 'href' => $_GET['q'], 'fragment' => $fragment);
}

}
}

// Switch to folded/unfolded view of the comment
if ($visible) {
$comment->comment = check_markup($comment->comment, $comment->format, FALSE);

// Comment API hook
comment_invoke_comment($comment, 'view');

$output .= theme('comment', $comment, $links);
}
else {
$output .= theme('comment_folded', $comment, $target);
}

return $output;
}

/**
* Minor adjustments to work with phptemplate_comment_view(). $target
* parameter added. Used for new comments.
*
* @param $comment
* comment object
* @param $target
* (optional) Used for link targeting.
*/
function phptemplate_comment_folded($comment, $target = NULL) {

$output = $target != NULL ?
"<div class=\"comment-folded new\">\n<a id=\"$target\"></a>" :
"<div class=\"comment-folded\">\n";
$output .= "<a id=\"comment-$comment->cid\"></a>";
$output .= '<span class="subject">'. l($comment->subject, comment_node_url() .'/'. $comment->cid, NULL, NULL, "comment-$comment->cid") . ' '. theme('mark', $comment->new) .'</span> ';
$output .= '<span class="credit">'. t('by') .' '. theme('username', $comment) ."</span>\n";
$output .= "</div>\n";

return $output;
}