#! /usr/bin/perl # Copyright 2001-2007 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', 'No Staff Found' => 'No Staff Found', 'View/Download' => 'View/Download', 'Custom Staff List' => 'Custom Staff List', 'Main' => 'Main', 'View Log File' => 'View Log File', 'Continue' => 'Continue', 'Homeroom' => 'Homeroom', 'Grade' => 'Grade', 'Cell Width' => 'Cell Width', 'Max Staff/Page' => 'Max Staff/Page', 'Select' => 'Select', 'Repeat Col Headings' => 'Repeat Col Headings', 'Staff List' => 'Staff List', 'Grade' => 'Grade', 'Room' => 'Room', 'Rm' => 'Rm', 'Students' => 'Students', 'Select Staff Positions' => 'Select Staff Positions', 'Select Sort Order' => 'Select Sort Order', 'Position' => 'Position', 'Name' => 'Name', 'Column Heading' => 'Column Heading', 'Other Settings' => 'Other Settings', 'Paper Size' => 'Paper Size', 'Letter' => 'Letter', 'A4' => 'A4', 'Legal' => 'Legal', 'No Positions Selected' => 'No Positions Selected', ); my $self = 'rptcustomstafflist.pl'; use DBI; use CGI; # Configurable settings my $maxstaff = 28; # maximum staff per page. my $maxstaffpositions = 12; # max type of staff types in admin.conf; my $width = 8; # default width of columns, 8mm; my $entrylimit = 15; # Max entries allowed for rotated text. my $maxentrysize = 14; # Maximum number of characters for the entry my $namewidth = 50; # Width of the first 'Name' column in mm. my $extracolwidth = 10; # Width of the second column in mm. Done in case I need this for another field later... my $margins = 26; # leave 0.5in (13mm) for each side margin. $q = new CGI; print $q->header; %arr = $q->Vars; unless (require "../etc/admin.conf"){ print "Cannot open admin.conf!"; die "Cannot open admin.conf!"; } unless (require "../lib/liblatex.pl"){ print "Cannot open liblatex.pl!"; die "Cannot open liblatex.pl!"; } # Date set after pulling in month and dow array. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $iddst) = localtime(time); $year = $year + 1900; $mon++; $wday++; my $currdate = "$s_dow[$wday], $s_month[$mon] $mday, $year"; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); print "$doctype\n". $lex{'Custom Staff List'}. " $chartype\n[ ". $lex{Main}. " ]\n"; print '

'. $lex{'Custom Staff List'}. "

\n"; # Get values for Customization if ( not $arr{flag} ){ printEntryPage(); } # Filter for Passed values that might crash LaTeX foreach $key (sort keys %arr){ #print "K:$key V:$arr{$key}
\n"; ( $arr{$key} ) = latex_filter( $arr{$key} ); } if ($arr{maxstudents}) { $maxlines = $arr{maxstaff}; } else { $maxlines = $maxstaff; } my $wi = $arr{width} - 1; # width of each col desired (in mm) # take away 1 for 1mm tabcolsep. # Get width for papersize # Letter paper size is 279mm x 216mm # Legal is 356mm x 216mm; a4 is 297 x 210 (skinny and longer) my $papersize; if ( $arr{papersize} eq $lex{Letter} ) { $papersize = 'letterpaper'; $paperwidth = 216; $textwidth = 206; $textheight = 269; } elsif ( $arr{papersize} eq $lex{A4} ) { $papersize = 'a4paper'; $paperwidth = 210; $textwidth = 200; $textheight = 287; } elsif ( $arr{papersize} eq $lex{Legal} ) { $papersize = 'legalpaper'; $paperwidth = 216; $textwidth = 200; $textheight = 346; } my $totalcolwidth = $paperwidth - $namewidth - $extracolwidth - $margins; my $colcount = $totalcolwidth / ($arr{width} + 1); $colcount = int $colcount; # truncate #print "Column Count: $colcount Total: $totalcolwidth \n"; # push entry text into array. for (1..$entrylimit){ my $entryname = 'entry'.$_; if ($arr{$entryname}){ # if it exists...push into array push @entry,$arr{$entryname}; } } my $entrysize = $#entry + 1; #print "Entry size: $entrysize\n"; if ( $arr{repeat} and $entrysize ){ $repeatcount = $colcount / $entrysize; $repeatcount = int $repeatcount; } elsif ($entrysize){ $repeatcount = 1; # only do it once if not repeat. } else { $repeatcount = 0;} $shortname = "stafflist$$"; $fileName = "$shortname.tex"; open(TEX,">$fileName") || die "Can't open tex file"; print TEX "\\documentclass[12pt,$papersize]{article} \\usepackage{array,newcent,rotating,colortbl,inputenc} \\inputencoding{latin1} \\pagestyle{empty} \\setlength{\\textwidth}{$textwidth mm} \\setlength{\\textheight}{$textheight mm} \\setlength{\\hoffset}{-1.4in} \\setlength{\\voffset}{-1.4in} \\addtolength{\\evensidemargin}{0in} \\addtolength{\\oddsidemargin}{0in} \\setlength{\\tabcolsep}{1mm} \\setlength{\\extrarowheight}{2mm} \\renewcommand{\\familydefault}{\\sfdefault} \\newcolumntype{G}{>{\\columncolor[gray]{1.00}}p{$namewidth mm}}\n"; # Note above... a columncolor of 1.00 means white, smaller number is darker. print TEX "\\begin{document}\n"; # Select the staff to include... my $select = 'where'; my $firstflag = 1; foreach my $idx ( 1..$maxstaffpositions ) { # max different types of staff positions my $field = 'position'. $idx; if ( $arr{$field} ) { $arr{$field} = $dbh->quote( $arr{$field} ); if ( not $firstflag ) { $select .= ' or '; } else { $firstflag = 0; } $select .= " position = $arr{$field} "; } } if ( $select eq 'where' ) { # nothing checked print "

". $lex{'No Positions Selected'}. "

\n"; die; } my $sortorder; if ( $arr{sortorder} eq 'posname' ) { $sortorder = 'position, lastname, firstname'; } elsif ( $arr{sortorder} eq 'posgrade' ) { $sortorder = 'position, grade, lastname, firstname'; } else { $sortorder = 'lastname, firstname'; } $sth = $dbh->prepare("select distinct userid from staff $select order by $sortorder "); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die; } $rows = $sth->rows; if ($rows < 1){ # No staff found... print '

'. $lex{'No Staff Found'}. "!

\n"; print "\n"; system("rm -f $shortname.*"); #cleanup die; } prHeader(); # do the first header. while ( my $userid = $sth->fetchrow ) { $linecount++; if ($linecount > $maxlines) { # We'll print a new page header here # New Page Header $linecount = 0; print TEX "\\end{tabular}\\\\ \\newpage\n"; prHeader(); } prRecord( $userid ); } # End of Loop for all staff print TEX "\\end{tabular}\\\\ \n\\end{document}"; close TEX; system("$pdflatex $fileName >pdflog$$.txt"); system("mv $shortname.pdf $downloaddir"); system("mv pdflog$$.txt $downloaddir"); system("rm -f $shortname.*"); print "

"; print $lex{'View/Download'}. ' ', $lex{'Custom Staff List'}. "

\n"; print "

[ Main |\n "; print $lex{'View Log File'}. "\n ]

\n"; #----------------- sub printEntryPage { # Entry Values #----------------- print "
\n"; print "\n"; # Outermost Table print "
\n"; # First Cell as Table print "\n"; print "\n"; # Find the distinct positions in the staff table $sth = $dbh->prepare("select distinct position from staff where position is not NULL and position != '' order by position"); $sth->execute; if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } my $poscount; while ( my $position = $sth->fetchrow ) { $poscount++; print "\n"; } print "
". $lex{'Select Staff Positions'}. "
"; print ""; print " $position

\n"; # Start New Table for Sort Order Selection: Second Table as cell print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
". $lex{'Select Sort Order'}. "
"; print $lex{Name}. "
"; print $lex{Position}. ',' . $lex{Name}. "
"; print $lex{Position}. ',' . $lex{Grade}. "
\n"; print "
\n"; # table #3 # Now Set Width and Column Headings, print "\n"; print "\n"; print "\n\n"; print "\n\n"; print "\n\n\n\n"; for my $id ( 1..$entrylimit ) { my $entrytxt = "entry$id"; print "\n"; print "\n"; } print "\n"; print "
". $lex{'Other Settings'}. "
". $lex{'Cell Width'}. ""; print "mm
". $lex{'Paper Size'}. ""; print "
". $lex{'Max Staff/Page'}. ""; print ""; print "
". $lex{'Repeat Col Headings'}; print "?
". $lex{'Column Heading'}. " $id
"; print "
\n"; print "
\n"; # end of outer table. print "
\n"; exit; } #----------- sub prHeader { #----------- # Done at the start of a new page. print TEX "\\begin{tabular}{G|p{$extracolwidth mm}|"; for (1..$colcount){ print TEX "p{$wi mm}|"; } print TEX "}\n"; print TEX "\\raggedright\\bf $schoolname ". $lex{'Staff List'}; for (1..$colcount + 1){ print TEX "& ";} print TEX "\\\\ \n"; print TEX "\\small\\raggedright $currdate\n\\bigskip "; for (1..$colcount + 1){ print TEX "& ";} print TEX "\\\\ \n"; if ($arr{group} eq $lex{Grade} ){ print TEX "\\Large\\raggedright ". $lex{Grade}. " $curroom & "; } else { #print TEX "\\bf\\raggedright $teachername "; #print TEX $lex{Rm}. " $curroom & "; print TEX ' & '; } my $remainder = $colcount - ( $repeatcount * $entrysize ); if ( $repeatcount ) { for (1..$repeatcount){ # times to loop to print rotated text. foreach my $txt ( @entry ) { print TEX "&\\hfil\\rule{6pt}{0pt}\\begin{rotate}{90}$txt"; print TEX "\\end{rotate}\\hfil"; } } } for (1..$remainder){ print TEX "& ";} print TEX "\\\\ \\hline\n"; } # End of prHeader #----------- sub prRecord { # print normal record (ie. line) #----------- my $userid = shift; my $sth = $dbh->prepare("select lastname, firstname from staff where userid = ?"); $sth->execute( $userid ); if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; } my ( $lastname, $firstname ) = $sth->fetchrow; if ($alternatingcolor == 0){ print TEX "\\rowcolor[gray]{0.93}"; $alternatingcolor = 1; } else { $alternatingcolor = 0;} print TEX "\\raggedright {\\bf $lastname}, $firstname"; print TEX " & "; for (1..$colcount){ print TEX "& ";} print TEX "\\\\\\hline\n"; }