Description:
another iteration of recent_diaries, much more efficient and which properly handles anonymous viewing.
Box Code:
####
# New diary list
# by Randy, tweaked by steve@silicongoblin.com
#
# Vars:
# num_diaries: how many to show in the box
# trim_length: length of title to trim to for display
# (will display complete words up to this length)
# split_length: longest words allowed in title without
# splitting (I had a user title a diary entry
# "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
# which rather messed my columns up :P)
# split_with: what to split words longer than
# split_length with
#
# Warning: contains hacked, ugly HTML still
####
my ($content, $num_diaries, $trim_length) = ('', 10 , 62);
my ($split_length, $split_with) = (17, "<br>\n");
my $uid = $S->{UID};
# Get the last $num_diaries Diary entries, and information about how many comments
# are attached, and how many of these have been seen by the viewer.
my ($rv, $sth) = $S->db_select({
WHAT => q(s.sid, s.title, s.aid, s.tid, s.time, count(c.cid)),
FROM => qq(stories AS s
LEFT OUTER JOIN comments AS c ON c.sid = s.sid),
WHERE => q(s.section="Diary" AND s.displaystatus = 1),
GROUP_BY => q(s.sid),
ORDER_BY => q(s.time DESC),
LIMIT => $num_diaries
});
my $table = $sth->fetchall_arrayref;
# Now get the viewed_story information for each of the selected stories.
# Doing it this way is MUCH faster than doing two outer joins.
# Skip this step if the viewer isn't logged in.
my %lastseen = ();
if ($uid > 0) {
my $sids = join ',', map qq('$_->[0]'), @$table;
my ($rv, $sth) = $S->db_select({
WHAT => q(s.sid, v.lastseen),
FROM => qq(stories AS s, viewed_stories AS v),
WHERE => qq(v.sid = s.sid AND v.uid=$uid AND s.sid IN ($sids)),
});
my $table2 = $sth->fetchall_arrayref;
$lastseen{$_->[0]} = $_->[1] for @$table2;
}
for (@$table) {
my ($sid, $title, $aid, $tid, $time, $comments) = @$_;
# my $lastseen = exists $lastseen{$sid} ? $lastseen{$sid} : $comments;
my $lastseen = exists $lastseen{$sid} ? $lastseen{$sid} : 0;
if (length $title > $trim_length) {
$title = substr($title, 0, $trim_length);
$title =~ s/\s+\S+$//;
$title .= '...';
}
# my $new_text = (($uid < 1) || exists $lastseen{$sid}) ? '' : qq(<span style="color: red;">[!] </span>);
# my $new_text = (($uid < 1) || (exists $lastseen{$sid})) ? '' : qq(<span style="color: red;">[!] </span>);
my $new_text = exists $lastseen{$sid} ? '' : qq(<span style="color: red;">[!] </span>);
$new_text = '' unless $uid > 0;
# Split long words in the title, so users can't go "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah"
# and fsck up my columns anymore.
$title =~ s[(\S{$split_length,})][join $split_with, grep(/^./, split/(\S{$split_length})/, $1)]ge;
my $new_comments = $comments - $lastseen;
my $comments_text = '';
if ($comments) {
$comments_text = qq(<br>\n<b>$comments</b> comment) . ($comments == 1 ? '' : 's');
$comments_text .= qq( (<b><font color='red'>$new_comments</font> new</b>)) if $new_comments && ($uid > 0);
};
my $month = (qw/Null January February March April May June July August September October November December/)[substr($time, 5, 2)];
my $day = 0 + substr($time, 8, 2);
$content .= qq{<font face="arial, helvetica, sans-serif" size="-1">$new_text<b><a href="|rootdir|/?op=displaystory;sid=$sid">$title</a></b></font><br>
<font face="arial, helvetica, sans-serif" size="-2">by <a href="/?op=section;section=Diary;user=$tid">$aid</a> - $month $day$comments_text
</font><br><br>};
}
# Add the "More..." link for diaries
$content .= qq{
<BR><A HREf="|rootdir|/?op=search;type=diary;offset=$num_diaries">More Diaries...</a>
|smallfont_end|
};
# And return the content, like normal.
return {content => $content};
|