1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?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;
}