| 1 |
<?php
|
| 2 |
// $Id: comment_count_image.module,v 1.1 2008/10/12 21:04:03 jpetso Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Comment Count Image - Adds a dynamically-generated image to RSS feed items
|
| 6 |
* that shows the number of comments on that node.
|
| 7 |
*
|
| 8 |
* Copyright 2008 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_menu().
|
| 13 |
*/
|
| 14 |
function comment_count_image_menu() {
|
| 15 |
$items = array();
|
| 16 |
$items['system/files/comment-count-image/%node/node-comments.png'] = array(
|
| 17 |
'title' => 'Comment Count Image',
|
| 18 |
'page callback' => 'comment_count_image_download',
|
| 19 |
'page arguments' => array(3),
|
| 20 |
'access arguments' => array('access comments'),
|
| 21 |
'type' => MENU_CALLBACK,
|
| 22 |
);
|
| 23 |
$items['comment-count-image/go/node/%node'] = array(
|
| 24 |
'title' => 'Comments',
|
| 25 |
'page callback' => 'comment_count_image_goto',
|
| 26 |
'page arguments' => array(3),
|
| 27 |
'access arguments' => array('access comments'),
|
| 28 |
'type' => MENU_CALLBACK,
|
| 29 |
);
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Some feed readers, or at least Akregator (preferred feed reader of this
|
| 35 |
* module's initial author), don't open links with fragments. So instead of
|
| 36 |
* linking to the comments directly, we redirect to those.
|
| 37 |
* (Wordpress does it the same way, btw.)
|
| 38 |
*/
|
| 39 |
function comment_count_image_goto($node) {
|
| 40 |
return drupal_goto('node/'. $node->nid, NULL, 'comments');
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_nodeapi().
|
| 45 |
*/
|
| 46 |
function comment_count_image_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 47 |
switch ($op) {
|
| 48 |
case 'rss item':
|
| 49 |
if ($node->comment != COMMENT_NODE_DISABLED && user_access('access comments')) {
|
| 50 |
$image_menupath = 'system/files/comment-count-image/'. $node->nid .'/node-comments.png';
|
| 51 |
$image_url = url($image_menupath, array('absolute' => TRUE));
|
| 52 |
$image = theme('comment_count_image', $node, $image_url);
|
| 53 |
$node->teaser .= '<p>'. $image .'</p>';
|
| 54 |
$node->body .= '<p>'. $image .'</p>';
|
| 55 |
}
|
| 56 |
return;
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Implementation of hook_theme().
|
| 62 |
*/
|
| 63 |
function comment_count_image_theme($existing, $type, $theme, $path) {
|
| 64 |
return array(
|
| 65 |
'comment_count_image' => array(
|
| 66 |
'arguments' => array('node' => NULL, 'image_url' => NULL),
|
| 67 |
),
|
| 68 |
);
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Theme function, returning the complete markup (with links and stuff)
|
| 73 |
* of the comment count image.
|
| 74 |
*/
|
| 75 |
function theme_comment_count_image($node, $image_url) {
|
| 76 |
$image_attributes = array('rel' => 'nofollow', 'style' => 'margin-top: 0.5em;');
|
| 77 |
$image_link = l(
|
| 78 |
theme('image', $image_url, '', '', $image_attributes, FALSE),
|
| 79 |
'comment-count-image/go/node/'. $node->nid,
|
| 80 |
array('absolute' => TRUE, 'html' => TRUE)
|
| 81 |
);
|
| 82 |
// Indent the image a bit, in order to set it apart from the contents.
|
| 83 |
return '<span style="margin-left: 1.5em;"> </span>'. $image_link;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Download callback for the image containing the number of comments
|
| 88 |
* on the given node.
|
| 89 |
*/
|
| 90 |
function comment_count_image_download($node) {
|
| 91 |
//if (file_check_directory('comment-count-image/node')) {
|
| 92 |
$comment_count = empty($node->comment_count) ? 0 : $node->comment_count;
|
| 93 |
$image_path = comment_count_real_image_path($comment_count);
|
| 94 |
|
| 95 |
if (is_file($image_path)) {
|
| 96 |
$headers = array(
|
| 97 |
'Content-Type: '. mime_header_encode('image/png'),
|
| 98 |
'Content-Length: '. filesize($image_path),
|
| 99 |
);
|
| 100 |
file_transfer($image_path, $headers);
|
| 101 |
}
|
| 102 |
return drupal_not_found();
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Return the path of the comment count image, (re-)creating it if necessary.
|
| 107 |
* The image has the content type 'image/png'.
|
| 108 |
*/
|
| 109 |
function comment_count_real_image_path($comment_count) {
|
| 110 |
$image_path = file_directory_path() .'/comment-count-image/'. $comment_count .'.png';
|
| 111 |
if (!is_file($image_path)) {
|
| 112 |
comment_count_image_create($image_path, $comment_count);
|
| 113 |
}
|
| 114 |
return $image_path;
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Create an image at the given path containing the given number of comments.
|
| 119 |
* The image has the content type 'image/png'.
|
| 120 |
*/
|
| 121 |
function comment_count_image_create(&$image_path, $comment_count) {
|
| 122 |
if (!file_check_directory(dirname($image_path), FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
|
| 123 |
return FALSE;
|
| 124 |
}
|
| 125 |
// Create a (black) image.
|
| 126 |
$image = @imagecreatetruecolor(95, 15);
|
| 127 |
|
| 128 |
if (!$image) {
|
| 129 |
return FALSE;
|
| 130 |
}
|
| 131 |
imagecolortransparent($image, 0 /* black */); // Set black as transparent.
|
| 132 |
$text_color = imagecolorallocate($image, 50, 50, 200); // Blueish text color.
|
| 133 |
$comment_count = t('Comments: !n', array('!n' => $comment_count));
|
| 134 |
|
| 135 |
// Render and save the image.
|
| 136 |
imagestring($image, 2, 0, 0, $comment_count, $text_color);
|
| 137 |
imagepng($image, $image_path, 9 /* highest compression level, still looks good */);
|
| 138 |
imagedestroy($image);
|
| 139 |
return TRUE;
|
| 140 |
}
|