[% WRAPPER 'pagebase.t' title="| Mark's Log" %]
##
[% log.Subject %]
[% log.Date_Added %]
[% text.nl2br(log.Body) %]
[% IF log.cropped == 1 %]
[ more... ]
[% END %]
[% IF log.Date_Added != log.Date_Modified %]
last revised [% log.Date_Modified %]
[% END %]
####
[% view %] [% list.offset + 1 %] - [% IF list.offset + list.limit < list.total %][% list.offset + list.limit %][% ELSE %][% list.total %][% END %] of [% list.total %]
[% content %]
[% IF list.offset > 0 %]
[ < previous [% list.limit %] ]
[% END %]
[% IF list.offset + list.limit < list.total %]
[ next [% list.limit %] > ]
[% END %]
####
Content-Type: text/html; charset=UTF-8
Mark M. Tanny, Photographer [% title %]
[% content %]
####
#-------------------------------------
# PRAGMAS
use lib 'e:/web_projects/marksphotos/lib';
use strict;
use utf8;
#-------------------------------------
# IN HOUSE MODULES
#use Config;
use HTML::Tools;
use dbWrapper qw( QueryDB );
#-------------------------------------
# 3RD PARTY MODULES
use Template;
#==========================================================================
# MAIN
#==========================================================================
my %params = ParseQueryString();
my $template = Template->new({INCLUDE_PATH => 'e:/web_projects/marksphotos/templates/',DELIMITER=>';',CACHE_SIZE=>0});
my %db_info = ();
my $page = '';
for( $params{view} )
{
/log/ and do { $db_info{fields} = ['ID','Date_Added','Date_Modified','Subject','Body'];
$db_info{table} = 'LogEntries';
$db_info{fieldnames} = $db_info{fields};
$db_info{count_table} = 'LogEntries';
$page = 'log/list.t';
last;
};
/announcements/ and do { $db_info{fields} = ['ID','Date_Added','Date_Modified','Body'];
$db_info{table} = 'Announcements';
$db_info{fieldnames} = $db_info{fields};
$db_info{count_table} = 'Announcements';
$page = 'announcements/list.t';
last;
};
/collections/ and do { $db_info{fields} = ['C.ID','C.Date_Added','C.Date_Modified','C.Title','C.Summary','P.Filename as Image'];
$db_info{table} = 'PhotoCollections C LEFT JOIN Photos P ON C.Feature_Image = P.ID';
$db_info{fieldnames} = [ 'ID','Date_Added','Date_Modified','Title','Summary','Image' ];
$db_info{count_table} = 'PhotoCollections';
$page = 'collections/list.t';
last;
};
do{ print FrontPage(); exit(0); };
}
#-------------------------------------
my $list_limit = ( exists $params{limit} and $params{limit} =~ /^(\d+)$/ ) ? $1 : 10;
my $list_offset = ( exists $params{offset} and $params{offset} =~ /^(\d+)$/ ) ? $1 : 0;
#-------------------------------------
if( $db_info{table} )
{
my $sth = undef;
#-- sanity check for any available records ----------------------
{
$sth = QueryDB("SELECT COUNT(ID) from ".$db_info{count_table}." WHERE Published='1'");
}
my( $count ) = $sth->fetchrow_array;
if( $count > 0 )
#-- there are records to fetch ----------------------------------
{
if( $count > $list_offset )
#-- fetching a set of records in a valid range ------------------
{
$sth = QueryDB("SELECT ".join(',',@{$db_info{fields}})." FROM ".$db_info{table}." WHERE Published='1' ORDER BY Date_Added DESC LIMIT $list_offset,$list_limit");
my($arrayref,%hash) = ( undef );
push @{$arrayref},{ %hash } while( @hash{ @{$db_info{fieldnames}} } = $sth->fetchrow_array() );
#-- successfully retrieved records ------------------------------
if( scalar( @{$arrayref} ) )
{
my $result = '';
$template->process($page,{entries=>$arrayref,
list => { limit=>$list_limit,
offset=>$list_offset,
total=>$count
},
text => { crop => \&crop_text,
nl2br => \&nl2br
}
},
\$result
) or die $template->error();
print $result,$count and exit(0);
}
else
#-- couldn't retrieve records -----------------------------------
{
print ErrorMessage("There has been an internal error") and exit(0);
}
}
else
#-- tried to get set of records off the end of the table --------
{
print ErrorMessage("You've tried to get a list of entries that don't exist") and exit(0);
}
}
else
#-- there aren't any records to fetch ---------------------------
{
print ErrorMessage("There are no entries for display") and exit(0);
}
}
#==========================================================================
# SUBS
#==========================================================================
sub FrontPage
{
return ErrorMessage("welcome to the front page");
}
sub ErrorMessage
{
my $text = '';
$template->process('error.t',{message=>$_[0]},\$text ) or die $template->error();
return $text;
}
#==========================================================================
sub crop_text
#a second return value of '1' indicates that the string was truncated.
{
my($text,$limit) = @_;
$text =~ /^(.{1,$limit}\W)/s;
return ($text,0) if( $text eq $1 );
return ($1,1);
}
# convert newlines to line breaks
sub nl2br
{
my $text = $_[0];
$text =~ s|\n|
|g;
return $text
}