#!/usr/bin/perl
#
# Display an individual tip.
#
# Revision History
#
# 2003-MAR-30 DSM Initial revision.
$| = 1;
use strict;
use lib '/store/host/lists.evolt.org/includes';
use DBI;
use CGI qw(param escapeHTML);
use Date::Calc qw(Add_Delta_Days);
require 'evolt-lib.pl';
require 'evolt-db.pl';
my $body = ''; # Contains any dynamically generated content.
my ($syear, $smonth, $sday, $eyear, $emonth, $eday);
# Parameters passed in.
my $week = param('w') || '_';
my $id = param('id') || '';
my $orderby = param('orderby') || 'tip_date';
my $orderdir = param('orderdir') || 'DESC';
my $print = param('print') || 0;
$id = &random_tip() if (!defined($id) || $id !~ /^\d+$/);
if (!defined($id) || $id !~ /^\d+$/) {
$body .= "
Error: Tip ID not supplied
\n"
}
else {
($syear, $smonth, $sday) = ($week =~ /(\d{4})(\d{2})(\d{2})/)
if ($week ne '_');
($eyear, $emonth, $eday) = Add_Delta_Days($syear, $smonth, $sday, 7)
if ($week ne '_');
# Get a database handle for thetips database.
my $dbh = opendb('thetips') || ($body .= "Couldn't open database!
");
$body .= &tip_nav($dbh, $syear, $smonth, $sday, $eyear, $emonth, $eday,
$id, $orderby, $orderdir) if (!$print && $week ne '_');
$body .= &tip_detail($dbh, $id);
$body .= &tip_nav($dbh, $syear, $smonth, $sday, $eyear, $emonth, $eday,
$id, $orderby, $orderdir) if (!$print && $week ne '_');
# Done with the database connection.
$dbh->disconnect();
}
# Create the rest of the HTML that will be displayed.
my $print_link = ($print)
? ' '
: "Print Tip(s)";
my $week_of_link = ($week ne '_')
? "Week of Mon $syear/$smonth/$sday >"
: '';
my $html =<
tip
|
harvester
|
$print_link |
lists.evolt.org >
tip harvester >
$week_of_link
Tip $id.
|
|
$body
|
END
# Display the actual page.
print header($print);
if ($print) {
print "\n";
}
else {
print
body($html),
footer();
}
1;
########## Subroutines ##########
sub tip_nav {
my ($dbh, $syear, $smonth, $sday, $eyear, $emonth, $eday,
$id, $orderby, $orderdir) = @_;
my $html = '';
my ($prev_id, $next_id) = (0, 0);
my $sdate = sprintf "%04d%02d%02d", $syear, $smonth, $sday;
my $orderdir_sql = ($orderdir eq 'ASC') ? 'DESC' : 'ASC';
my $sql =<prepare($sql);
my $rev = $sth->execute();
while (my ($tipid) = $sth->fetchrow()) {
last if ($next_id > 0);
if ($next_id == -1) {
$next_id = $tipid;
last;
}
if ($tipid == $id) {
$next_id = -1;
}
else {
$prev_id = $tipid;
}
}
$sth->finish();
$html .=<
|
END
$html .= "Jump to:\n" if ($prev_id > 0 || $next_id > 0);
$html .= "Prev Tip\n" if ($prev_id > 0);
$html .= "Next Tip\n" if ($next_id > 0);
$html .= "
\n | \n
\n";
return $html;
}
sub tip_detail {
my ($dbh, $id) = @_;
my $sql =<prepare($sql);
my $rev = $sth->execute();
my ($tipd, $messid, $tip_date, $type, $author, $body) = $sth->fetchrow();
$sth->finish();
$body = &format($body);
return <
|
Date: |
$tip_date |
Author: |
$author |
Type: |
$type |
|
|
$body
|
END
}
sub format {
my $body = shift;
$body = escapeHTML($body);
$body =~ s/\r?\n/
\n/gs;
$body =~ s|<(https?://.+?)>|($1)|gs;
$body =~ s|(https?://[^<)\s]+)|$1|gs;
return $body;
}
sub random_tip {
my $id;
# Get a database handle for thetips database.
my $dbh = opendb('thetips') || ($body .= "Couldn't open database!
");
my $sql = 'SELECT max(tipid) FROM thetips';
my $sth = $dbh->prepare($sql);
my $rev = $sth->execute();
my $max = $sth->fetchrow();
$sth->finish();
if ($max > 0) {
while (!defined($id)) {
$id = int(rand($max + 1));
my $sql = "SELECT tipid FROM thetips WHERE tipid = $id";
my $sth = $dbh->prepare($sql);
my $rev = $sth->execute();
$id = $sth->fetchrow();
$sth->finish();
}
}
# Done with the database connection.
$dbh->disconnect();
return $id;
}