#------------------------------------- # 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=>';',COMPILE_EXT=>'c'}); my %db_info = (); my $page = ''; #------------------------------------- my $list_limit = ( exists $params{limit} and $params{limit} =~ /^(\d+)$/ ) ? $1 : 10; my $list_offset = ( exists $params{offset} and $params{offset} =~ /^(\d+)$/ ) ? $1 : 0; my $date_format = '%a %M %d, %Y | %l:%i %p'; for( 'log' ) # normally this is for( $params{view} ) { /log/ and do { $db_info{query} = "SELECT ID,DATE_FORMAT(Date_Added,'$date_format') as Created,DATE_FORMAT(Date_Modified,'$date_format') as Modified,Subject,Body FROM LogEntries WHERE Published='1' ORDER BY Created LIMIT $list_offset,$list_limit"; $db_info{fields} = [ qw( ID Date_Added Date_Modified Subject Body) ]; $db_info{table} = 'LogEntries'; $page = 'log/list.t'; last; }; /announcements/ and do { $db_info{query} = "SELECT ID,DATE_FORMAT(Date_Added,'$date_format') as Created,DATE_FORMAT(Date_Modified,'$date_format') as Modified,Body FROM Announcements WHERE Published='1' ORDER BY Created LIMIT $list_offset,$list_limit"; $db_info{fields} = [ qw(ID Date_Added Date_Modified Body) ]; $db_info{table} = 'Announcements'; $page = 'announcements/list.t'; last; }; /collections/ and do { $db_info{query} = "SELECT C.ID,DATE_FORMAT(C.Date_Added,'$date_format') as Created,DATE_FORMAT(C.Date_Modified,'$date_format') as Modified,C.Title,C.Summary,P.Filename as Image FROM PhotoCollections C LEFT JOIN Photos P ON C.Feature_Image = P.ID WHERE Published='1' ORDER BY Created DESC LIMIT $list_offset,$list_limit"; $db_info{fields} = [ qw(ID Date_Added Date_Modified Title Summary Image) ]; $db_info{table} = 'PhotoCollections'; $page = 'collections/list.t'; last; }; do{ print FrontPage(); exit(0); }; } #------------------------------------- if( $db_info{table} ) { my $sth = undef; #-- sanity check for any available records ---------------------- { $sth = QueryDB("SELECT COUNT(ID) from ".$db_info{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($db_info{query}); my($arrayref,%hash) = ( undef ); push @{$arrayref},{ %hash } while( @hash{ @{$db_info{fields}} } = $sth->fetchrow_array() ); #-- successfully retrieved records ------------------------------ if( scalar( @{$arrayref} ) ) { # DEBUG TEST ------------------------ print '
';
foreach( @{$arrayref} )
{
print "\n\n";
while( my( $key,$value ) = each %{$_} )
{
print "$key: $value\n";
}
}
print "\n";
# DEBUG TEST ------------------------
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;
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|