This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/comment/theme.php

243 lines
7.1 KiB
PHP
Raw Normal View History

<?php
class CommentListTheme extends Themelet {
2009-08-24 02:43:10 +00:00
var $comments_shown = 0;
var $show_anon_id = false;
2010-02-02 18:12:54 +00:00
var $anon_id = 1;
2012-02-11 08:18:51 +00:00
var $anon_cid = 0;
var $anon_map = array();
var $ct = null;
private function get_anon_colour($ip) {
if(is_null($this->ct)) {
$this->ct = hsl_rainbow();
2012-02-11 08:18:51 +00:00
}
if(!array_key_exists($ip, $this->anon_map)) {
$this->anon_map[$ip] = $this->ct[$this->anon_cid++ % count($this->ct)];
}
return $this->anon_map[$ip];
}
2009-08-24 02:43:10 +00:00
2009-08-04 16:45:09 +00:00
/**
* Display a page with a list of images, and for each image,
* the image's comments
*/
2009-08-04 16:45:09 +00:00
public function display_comment_list($images, $page_number, $total_pages, $can_post) {
global $config, $page, $user;
// aaaaaaargh php
assert(is_array($images));
2009-08-19 04:53:27 +00:00
assert(is_numeric($page_number));
assert(is_numeric($total_pages));
assert(is_bool($can_post));
2009-08-04 16:45:09 +00:00
// parts for the whole page
$prev = $page_number - 1;
$next = $page_number + 1;
$h_prev = ($page_number <= 1) ? "Prev" :
'<a href="'.make_link('comment/list/'.$prev).'">Prev</a>';
$h_index = "<a href='".make_link()."'>Index</a>";
$h_next = ($page_number >= $total_pages) ? "Next" :
'<a href="'.make_link('comment/list/'.$next).'">Next</a>';
$nav = $h_prev.' | '.$h_index.' | '.$h_next;
2009-01-04 19:18:37 +00:00
$page->set_title("Comments");
$page->set_heading("Comments");
$page->add_block(new Block("Navigation", $nav, "left"));
$this->display_paginator($page, "comment/list", null, $page_number, $total_pages);
2009-08-04 16:45:09 +00:00
// parts for each image
$position = 10;
$comment_limit = $config->get_int("comment_list_count", 10);
$comment_captcha = $config->get_bool('comment_captcha');
2009-08-04 16:45:09 +00:00
foreach($images as $pair) {
$image = $pair[0];
$comments = $pair[1];
$thumb_html = $this->build_thumb_html($image);
$comment_html = "";
$comment_count = count($comments);
if($comment_limit > 0 && $comment_count > $comment_limit) {
$hidden = $comment_count - $comment_limit;
$comment_html .= '<p>showing '.$comment_limit.' of '.$comment_count.' comments</p>';
$comments = array_slice($comments, -$comment_limit);
$this->show_anon_id = false;
2012-02-11 08:18:51 +00:00
}
else {
$this->show_anon_id = true;
}
$this->anon_id = 1;
2009-08-04 16:45:09 +00:00
foreach($comments as $comment) {
$comment_html .= $this->comment_to_html($comment);
}
if(!$user->is_anonymous()) {
if($can_post) {
$comment_html .= $this->build_postbox($image->id);
}
} else {
if ($can_post) {
if(!$comment_captcha) {
$comment_html .= $this->build_postbox($image->id);
}
else {
$comment_html .= "<a href='".make_link("post/view/".$image->id)."'>Add Comment</a>";
}
}
2009-08-04 16:45:09 +00:00
}
$html = '
<table class="comment_list_table"><tr>
<td width="220">'.$thumb_html.'</td>
<td>'.$comment_html.'</td>
2009-08-04 16:45:09 +00:00
</tr></table>
';
2009-08-04 16:45:09 +00:00
$page->add_block(new Block( $image->id.': '.$image->get_tag_list(), $html, "main", $position++));
2009-08-04 16:45:09 +00:00
}
}
2009-08-04 16:45:09 +00:00
/**
* Add some comments to the page, probably in a sidebar
*
* $comments = an array of Comment objects to be shown
*/
2009-08-04 16:45:09 +00:00
public function display_recent_comments($comments) {
global $page;
$this->show_anon_id = false;
2009-08-04 16:45:09 +00:00
$html = "";
foreach($comments as $comment) {
$html .= $this->comment_to_html($comment, true);
}
$html .= "<p><a class='more' href='".make_link("comment/list")."'>Full List</a>";
$page->add_block(new Block("Comments", $html, "left"));
}
2009-08-04 16:45:09 +00:00
/**
* Show comments for an image
*/
2009-08-04 16:45:09 +00:00
public function display_image_comments(Image $image, $comments, $postbox) {
global $page;
$this->show_anon_id = true;
2009-08-04 16:45:09 +00:00
$html = "";
foreach($comments as $comment) {
$html .= $this->comment_to_html($comment);
}
2009-08-04 16:45:09 +00:00
if($postbox) {
$html .= $this->build_postbox($image->id);
}
2009-07-28 00:09:00 +00:00
$page->add_block(new Block("Comments", $html, "main", 30));
}
2011-12-31 13:54:32 +00:00
/**
* Show comments made by a user
*/
public function display_user_comments($comments) {
global $page;
$html = "";
foreach($comments as $comment) {
$html .= $this->comment_to_html($comment, true);
}
2012-01-09 03:18:17 +00:00
if(empty($html)) {
$html = '<p>No comments by this user.</p>';
}
2011-12-31 13:54:32 +00:00
$page->add_block(new Block("Comments", $html, "left", 70));
}
protected function comment_to_html($comment, $trim=false) {
2012-02-11 08:18:51 +00:00
global $config, $user;
$tfe = new TextFormattingEvent($comment->comment);
send_event($tfe);
$i_uid = int_escape($comment->owner_id);
$h_name = html_escape($comment->owner_name);
$h_poster_ip = html_escape($comment->poster_ip);
2010-04-07 12:25:46 +00:00
$h_timestamp = autodate($comment->posted);
$h_comment = ($trim ? substr($tfe->stripped, 0, 50) . (strlen($tfe->stripped) > 50 ? "..." : "") : $tfe->formatted);
$i_comment_id = int_escape($comment->comment_id);
$i_image_id = int_escape($comment->image_id);
2012-02-11 08:18:51 +00:00
if($i_uid == $config->get_int("anon_id")) {
$anoncode = "";
$style = "";
$anoncode2 = "";
if($this->show_anon_id) {
$anoncode = '<sup>'.$this->anon_id.'</sup>';
}
2012-02-11 08:18:51 +00:00
if($user->can("view_ip")) {
#$style = " style='color: ".$this->get_anon_colour($comment->poster_ip).";'";
if(!array_key_exists($comment->poster_ip, $this->anon_map)) {
$this->anon_map[$comment->poster_ip] = $this->anon_id;
}
if($this->anon_map[$comment->poster_ip] != $this->anon_id) {
$anoncode2 = '<sup>('.$this->anon_map[$comment->poster_ip].')</sup>';
}
2012-02-11 08:18:51 +00:00
}
$h_userlink = "<span class='username'$style>" . $h_name . $anoncode . $anoncode2 . "</span>";
$this->anon_id++;
}
else {
$h_userlink = '<a class="username" href="'.make_link('user/'.$h_name).'">'.$h_name.'</a>';
2010-02-02 18:12:54 +00:00
}
2009-08-04 16:45:09 +00:00
$stripped_nonl = str_replace("\n", "\\n", substr($tfe->stripped, 0, 50));
2008-12-27 10:56:51 +00:00
$stripped_nonl = str_replace("\r", "\\r", $stripped_nonl);
2009-08-04 16:45:09 +00:00
if($trim) {
return '
'.$h_userlink.': '.$h_comment.'
<a href="'.make_link('post/view/'.$i_image_id).'">&gt;&gt;&gt;</a>
';
2009-08-04 16:45:09 +00:00
}
else {
2012-02-06 14:42:38 +00:00
$avatar = "";
if(!empty($comment->owner_email)) {
$hash = md5(strtolower($comment->owner_email));
$avatar = "<img src=\"http://www.gravatar.com/avatar/$hash.jpg\"><br>";
}
$h_reply = " - <a href='javascript: replyTo($i_image_id, $i_comment_id)'>Reply</a>";
2012-02-10 01:55:33 +00:00
$h_ip = $user->can("view_ip") ? "<br>".show_ip($comment->poster_ip, "Comment posted {$comment->posted}") : "";
2012-02-07 15:15:18 +00:00
$h_del = $user->can("delete_comment") ?
2012-02-06 14:42:38 +00:00
' - <a onclick="return confirm(\'Delete comment by '.$h_name.':\\n'.$stripped_nonl.'\');" '.
'href="'.make_link('comment/delete/'.$i_comment_id.'/'.$i_image_id).'">Del</a>' : '';
return '
<a name="'.$i_comment_id.'"></a>
2012-02-06 12:24:13 +00:00
<div class="comment">
2012-02-06 14:42:38 +00:00
<div class="info">
'.$avatar.'
'.$h_timestamp.$h_reply.$h_ip.$h_del.'
</div>
'.$h_userlink.': '.$h_comment.'
2009-08-04 16:45:09 +00:00
</div>
';
2009-08-04 16:45:09 +00:00
}
2012-02-06 14:42:38 +00:00
return "";
}
protected function build_postbox($image_id) {
global $config;
2009-11-10 03:01:20 +00:00
$i_image_id = int_escape($image_id);
$hash = CommentList::get_hash();
$captcha = $config->get_bool("comment_captcha") ? captcha_get_html() : "";
2009-11-10 03:55:17 +00:00
return '
'.make_form(make_link("comment/add")).'
<input type="hidden" name="image_id" value="'.$i_image_id.'" />
<input type="hidden" name="hash" value="'.$hash.'" />
2012-02-06 14:42:38 +00:00
<textarea id="comment_on_'.$i_image_id.'" name="comment" rows="5" cols="50"></textarea>
'.$captcha.'
<br><input type="submit" value="Post Comment" />
</form>
';
}
}
?>