#! /usr/bin/perl # Copyright 2001-2010 Leslie Richardson # This file is part of Open Admin for Schools. # Open Admin for Schools is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. my %lex = ('Main' => 'Main', 'Template' => 'Template', 'Creator' => 'Creator', 'Description' => 'Description', 'Type' => 'Type', 'Select' => 'Select', 'Fields' => 'Fields', 'Error' => 'Error', 'Continue' => 'Continue', 'Column' => 'Column', 'Number' => 'Number', 'Width' => 'Width', 'Order' => 'Order', 'Tabular' => 'Tabular', 'Multicolumn' => 'Multicolumn', 'Reorder' => 'Reorder', 'Student Roster' => 'Student Roster', 'Report' => 'Report', 'Created' => 'Created', 'Delete' => 'Delete', 'Select' => 'Select', 'Deleted' => 'Deleted', 'Save' => 'Save', 'Drag' => 'Drag', 'Change' => 'Change', 'Order' => 'Order', 'View' => 'View', 'New' => 'New', ); my $self = 'templatecreator.pl'; my $templatepath = '../template/'; my $startTemplateDelete = 5; # Starting Number for User Created Templates (to delete) use DBI; use CGI; # Read config variables eval require "../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } my $q = new CGI; my %arr = $q->Vars; print $q->header( -charset, $charset ); # Page Header $title = "$lex{Template} $lex{Creator}"; print "$doctype\n$title \n"; if ( $arr{page} == 1 ) { # load jQuery libs print "\n"; print "\n"; } print "$chartype\n\n"; print "
[ $lex{Main} ]\n"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "

$title

\n"; # Setup Database access my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); $dbh->{mysql_enable_utf8} = 1; if ( not $arr{page} ) { showStartPage(); } elsif ( $arr{page} == 1 ) { delete $arr{page}; selectOptions(); } elsif ( $arr{page} == 2 ) { delete $arr{page}; createTemplate(); } elsif ( $arr{page} == 3 ) { delete $arr{page}; selectTemplateToDelete(); } elsif ( $arr{page} == 4 ) { delete $arr{page}; deleteTemplate(); } #----------------- sub deleteTemplate { #----------------- #print "\n"; #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } foreach my $key ( sort keys %arr ) { my $fullname = $templatepath . $key; my $result = unlink( $fullname ); if ( $result ) { print "

$key $lex{Deleted}

\n"; } } print "

[ $lex{'Student Roster'} $lex{Report} | \n"; print "$lex{Template} $lex{Creator} | \n"; print "$lex{Main} ]

\n"; print "\n"; exit; } #------------------------- sub selectTemplateToDelete { #------------------------- # Open the Report Templates my @files = glob($templatepath."rptstudrost*.tpl"); for my $tplfile ( @files ) { # read each label file and get description unless ( open (FH,"<$tplfile")) { print "$lex{Error} $lex{Template}: $!\n"; die "$lex{Error} $lex{Template}: $!\n"; } # read first 2 lines of the template only. my $desc = ; chomp $desc; $desc =~ s/\[//g; # strip any opening square labels, just in case. my $modeline = ; $modeline =~ s/^\s*//; # strip any leading spaces my ($mode, $fmtstart,$fmtend) = split /::/, $modeline; # ignore rest of the file, not required. $tplfile =~ s/^.*\///; # strip leading path push @desc, "$desc ($mode) [$tplfile]"; } print "

$lex{Delete} $lex{Template}

\n"; print "\n"; print "\n"; print "\n"; print "\n"; foreach my $desc ( @desc ) { $desc =~ m/rptstudrost(.*)\.tpl/; my $num = $1; print "\n"; } print "\n"; print "
$lex{Template}$lex{Select}
$desc\n"; if ( $num >= $startTemplateDelete ) { print ""; } print "
"; print "
\n"; print "\n"; exit; } #----------------- sub createTemplate { #----------------- # Debug Option # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } my $reporttype = lc( $arr{tc_reporttype} ); delete $arr{tc_reporttype}; my $description = $arr{tc_description}; delete $arr{tc_description}; my $pdftype = lc( $arr{tc_pdftype} ); delete $arr{tc_pdftype}; my $colcount = $arr{tc_colcount}; delete $arr{tc_colcount}; my $filename; for my $i ( 0 .. 99 ) { if ( length $i == 1 ) { $i = '0'. $i; } # prepend a zero. $filename = 'rptstudrost'. $i. '.tpl'; my $fullname = $templatepath. $filename; if ( -e $fullname ) { next; } # skip last; # we have a filename to use. } # my @fields = split(",", $arr{sortorder} ); foreach my $fld ( @fields ) { $fld =~ s/s\_//; } delete $arr{sortorder}; # Extract the width keys my %width = (); foreach my $key ( keys %arr ) { if ( $key =~ m/wid\_(.*)/) { $width{ $1 } = $arr{$key}; delete $arr{$key}; } } # foreach my $key ( sort keys %arr ) { print "K:$key V: $arr{$key}
\n"; } # Open File unless ( open ( FH,">", $templatepath. $filename )) { print $lex{Error}. q{ }. $lex{Template}.": $!\n"; die $lex{Error}. q{ }. $lex{Template}.": $!\n"; } # Write Description line - Line 1 print FH $description. "\n"; if ( $reporttype eq 'pdf' ) { if ( $pdftype eq 'tabular' ) { # Write Type and Format Line - Line 2 print FH 'pdf::\begin{tabular}{|'; foreach my $fld ( @fields ) { print FH 'p{'. $width{ $fld }. 'mm}|'; } print FH '}\hline::\end{tabular}'. "\n"; # Write the header line - Line 3 my $first = 1; print FH '\rowcolor[gray]{0.85}'; foreach my $fld ( @fields ) { if ( not $first ) { print FH ' &'; } else { $first = 0; } print FH '{\bf <*'. $fld. '*>}'; } print FH '\\\\ \hline'. "\n"; print FH "\n"; # blank line 4 and 5 for future expansion. print FH "\n"; # Write the layout. my $first = 1; foreach my $fld ( @fields ) { if ( not $first ) { print FH ' &'; } else { $first = 0; } print FH '<@'. $fld. '@>'; } print FH '\\\\ \hline'. "\n"; } else { # pdftype is multicolumn # Write Type and Format Line - Line 2 print FH 'pdf::\begin{multicols}{'. $colcount. '}::\end{multicols}'. "\n"; # Write blank header line - Line 3 print FH "\n"; print FH "\n"; # blank line 4 and 5 for future expansion. print FH "\n"; # Write the layout. foreach my $fld ( @fields ) { print FH '<*'. $fld. '*> <@'. $fld. '@>\\\\'. "\n"; } } } else { # HTML report type # Write Type and Format Line - Line 2 print FH "html::"; print FH "::
::\n"; # no record count; # Write the header line - Line 3 print FH ''; foreach my $fld ( @fields ) { print FH '<*'. $fld. '*>'; } print FH ''. "\n"; print FH "\n"; # blank line 4 and 5 for future expansion. print FH "\n"; # Write the layout. print FH ''; foreach my $fld ( @fields ) { print FH '<@'. $fld. '@>'; } print FH ''. "\n"; } close FH; print "

$lex{Template}: $filename $lex{Created}

\n"; print "

[ $lex{View} $lex{New} $lex{Report} |\n"; print "$lex{Main} ]

\n"; print "\n"; exit; } #---------------- sub selectOptions { #---------------- #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}
\n"; } # Create hash for fieldnames from meta. my $sth = $dbh->prepare("select fieldid, fieldname from meta where tableid = ? order by arrayidx"); $sth->execute( 'student' ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my %fieldnames = (); while ( my ( $fieldid, $fieldname ) = $sth->fetchrow ) { $fieldnames{$fieldid} = $fieldname; } # Start Form print "
\n"; print "\n"; # put in hidden keys foreach my $key ( sort keys %arr ) { print "\n"; } if ( $arr{tc_reporttype} eq 'PDF' ) { # add more fields # Start Table print "\n"; if ( $arr{tc_pdftype} eq $lex{Tabular} ) { print "\n"; foreach my $key ( sort keys %arr ) { if ( $key =~ m/tc\_.*/ ) { next; } print "\n"; print "\n"; } } else { # multicol # get column count print "\n"; print "\n"; } # end of pdf - multicol print "
$lex{Fields}$lex{Width}
$key"; print " mm
$lex{Column} $lex{Number}\n"; print "
\n"; } # additional PDF options. # start of Sorting section. print "

$lex{Drag} $lex{Fields} = $lex{Change} $lex{Order}

\n"; print "
\n"; foreach my $key ( sort keys %arr ) { if ( $key =~ m/tc\_.*/ ) { next; } # skip non field values my $id = 's_'. $key; print "
"; print "$fieldnames{$key}
\n"; } print "
\n"; # end of Sorting section. # Save Button print "

\n"; print "\n"; print "
\n"; # Javscript Function for Sorting. print q{ }; print "\n"; exit; } # end of selectOptions #---------------- sub showStartPage { #---------------- # Start Form print "
\n"; print "\n"; print "\n"; # Continue Button print "\n"; # Description print "\n"; print "\n"; # Report Type: pdf or html print "\n"; print "\n"; # PDF Type: tabular or multicolumn print "\n"; print "\n"; # Create hash for fieldnames from meta. my $sth = $dbh->prepare("select fieldid, fieldname from meta where tableid = ? order by arrayidx"); $sth->execute( 'student' ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } # Select Fields to Show print "\n"; while ( my ( $fieldid, $fieldname ) = $sth->fetchrow ) { print "\n"; } print "\n"; # Continue Button print "\n"; print "
\n"; print "
$lex{Description}
$lex{Type}
PDF $lex{Type}
"; print "$lex{Select} $lex{Fields}
\n"; print " $fieldname

\n"; print "
\n"; print "\n"; exit; }