#!/usr/bin/perl
#  Copyright 2001-2009 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.

# TODO : Get rid of the ugliness and use the latest templates instead!

my %lex = ('Main' => 'Main',
	   'Eoy' => 'Eoy',
	   'Error' => 'Error',
	   'View Preregistered Students' => 'View Preregistered Students',
	   'Name' => 'Name',
	   'Description' => 'Description',
	   'Parent 1' => 'Parent 1',
	   'Parent 2' => 'Parent 2',
	   'Emer/Rel' => 'Emer/Rel',

	   );


use DBI;
use CGI;

eval require "../../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. ": $@<br>\n";
    die $lex{Error}. ": $@\n";
}


my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, 
    $iddst) = localtime(time);
$year = $year + 1900;
$mon++; $wday++;
$currdate = "$dow[$wday], $month[$mon] $mday, $year";

my $q = new CGI;
print $q->header( -charset, $charset );
%arr = $q->Vars;

my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);


my $select;
if ( $arr{homeroom} ) {
    my $homeroom = $dbh->quote( $arr{homeroom} );
    $select = "where homeroom = $homeroom ";
}

my $sortorder = "lastname, firstname";
if ($arr{sortorder} eq "birthdate") {
    $sortorder = "birthdate";
} 

my $sth = $dbh->prepare("select * from prereg $select order by $sortorder");
$sth->execute;
$rows = $sth->rows;

print "$doctype\n<html><head><title>". $lex{'View Preregistered Students'}. "</title>
<link rel=\"stylesheet\" href=\"$css\" type=\"text/css\">
$chartype\n</head><body>\n";

print "<a href=\"$homepage\">". $lex{Main}. "</a> |\n";
print "<a href=\"$eoypage\">". $lex{Eoy}. "</a> |\n";

print "<center><h1>". $lex{'View Preregistered Students'}. "</h1><h3>$currdate</h3>\n";

print "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
print "<tr><th>". $lex{Name}. "</th><th>". $lex{Description}. "</th><th>". $lex{'Parent 1'};
print "</th><th>". $lex{'Parent 2'}. "</th><th>". $lex{'Emer/Rel'}. "</th></tr>\n";

for ( my $i=1; $i <= $rows; ++$i) {
    @arr = $sth->fetchrow;

    print <<"EOF";

   <tr>
   <td valign="top">
      <table><tr><td colspan="2"><b> $arr[2] $arr[3] $arr[1]</b></td></tr>
      <tr><td align="right"><em>Studnum:</em></td><td> $arr[4]</td></tr>
      <tr><td align="right"><em>Grade:</em></td><td> $arr[5]</td></tr>
      <tr><td align="right"><em>Form:</em></td><td> $arr[6]</td></tr>
      <tr><td align="right"><em>Sex:</em></td><td> $arr[7]</td></tr>
      <tr><td align="right"><em>Bdate:</em></td><td> $arr[8]</td></tr>
      <tr><td align="right"><em>ProvNum:</em></td><td> $arr[57]</td></tr>
      <tr><td align="right"><em>House:</em></td><td> $arr[9]</td></tr>
      <tr><td align="right"><em>Bus:</em></td><td> $arr[56] </td></tr>
      <tr>
        <td align="right"><em>Emerg:</em></td>
        <td>$arr[49] $arr[50] $arr[51]</td>
      </tr>
    
    <tr><td align="right"> </td>
        <td> $arr[52]  $arr[53] </td>
    </tr>
      
    <tr><td align="right"><em>Doctor:</em></td>
       <td> $arr[54]  $arr[55] </td>
    </tr>
      </table>
   </td>

  <td valign="top">
      <table>
      <tr><td align="right"><em>HealthID:</em></td><td> $arr[10]</td></tr>
      <tr><td align="right"><em>Entry:</em></td><td> $arr[11]</td></tr>
      <tr><td align="right"><em>Ethnic:</em></td><td> $arr[12]</td></tr>
      <tr><td align="right"><em>SPar:</em></td><td> $arr[13]</td></tr> 
      <tr><td align="right"><em>Internet:</em></td><td> $arr[14]</td></tr> 
      <tr><td align="right"><em>Youngest:</em></td><td> $arr[15]</td></tr>
      <tr><td align="right"><em>Owing:</em></td><td> $arr[16]</td></tr>
      <tr><td align="right"><em>Band:</em></td><td> $arr[58]</td></tr>
      <tr><td align="right"><em>Band #:</em></td><td> $arr[59]</td></tr>
      <tr><td align="right"><em>Medical:</em></td><td>$arr[18]</td></tr>
      </table>
  </td>
  
  
  <td valign="top">
   <table>
    <tr><td align="right"><em>Par:</em></td><td>$arr[27]</td></tr>
    <tr><td align="right"><em>Con:</em></td><td>$arr[28]</td></tr>
    <tr><td align="right"><em>Name:</em></td><td>$arr[29]</td></tr>
    <tr><td align="right"><em>HPh:</em></td><td> $arr[30]</td></tr>
    <tr><td align="right"><em>WPh:</em></td><td> $arr[31]</td></tr>
    <tr><td align="right"><em>Cel:</em></td><td> $arr[32]</td></tr>
    <tr><td align="right"><em>Fax:</em></td><td> $arr[33]</td></tr>
    <tr><td align="right"><em>Adr:</em></td><td> $arr[34]</td></tr>
    <tr><td align="right"><em>City:</em></td><td> $arr[35]</td></tr>
    <tr><td align="right"><em>Pcode:</em></td><td> $arr[36]</td></tr>
    <tr><td align="right"><em>Work:</em></td><td> $arr[37]</td></tr>
   </table>
  </td>


  <td valign="top">
    <table>
    <tr><td align="right"><em>Par:</em></td><td>$arr[38]</td></tr>
    <tr><td align="right"><em>Con:</em></td><td> $arr[39]</td></tr>
    <tr><td align="right"><em>Name:</em></td><td> $arr[40]</td></tr>
    <tr><td align="right"><em>HPh:</em></td><td> $arr[41]</td></tr>
    <tr><td align="right"><em>WPh:</em></td><td> $arr[42]</td></tr>
    <tr><td align="right"><em>Cel:</em></td><td> $arr[43]</td></tr>
    <tr><td align="right"><em>Fax:</em></td><td> $arr[44]</td></tr>
    <tr><td align="right"><em>Adr:</em></td><td> $arr[45]</td></tr>
    <tr><td align="right"><em>City:</em></td><td> $arr[46]</td></tr>
    <tr><td align="right"><em>Pcode:</em></td><td> $arr[47]</td></tr>
    <tr><td align="right"><em>Work:</em></td><td> $arr[48]</td></tr>
    </table>
  </td>

  <td valign"top">
    <table>
    <tr><td align="right">Rel:</td><td> $arr[19]</td></tr>
    <tr><td align="right">Mom:</td><td> $arr[20]</td></tr>
    <tr><td align="right">Dad:</td><td> $arr[21]</td></tr>
    <tr><td align="right">Parish:</td><td> $arr[22]</td></tr>
    <tr><td align="right">Bap:</td><td> $arr[60]</td></tr>
    <tr><td align="right">Comm:</td><td> $arr[23]</td></tr>
    <tr><td align="right">Recon:</td><td> $arr[24]</td></tr>
    <tr><td align="right">Conf:</td><td> $arr[25]</td></tr>
    <tr><td align="right">Maiden:</td><td> $arr[26]</td></tr>
    </table>
  </td>
 </tr>
  
EOF

}

print "</table></center></body></html>";
