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

use DBI;
use CGI;

my %lex = ('Main' => 'Main',
	   'Error' => 'Error',
	   'Edit' => 'Edit',
	   'Date' => 'Date',
	   'StatHol' => 'StatHol',
	   'Inservice' => 'Inservice',
	   'Holiday' => 'Holiday',
	   'Other' => 'Other',
	   'Type' => 'Type',
	   'Description' => 'Description',
	   'Day In Cycle' => 'Day In Cycle',
	   'Fraction' => 'Fraction',
	   'Closed' => 'Closed',
	   'Yes' => 'Yes',
	   'No' => 'No',
	   'Update Record' => 'Update Record',
	   'Record(s) Updated' => 'Record(s) Updated',
	   'Contact' => 'Contact',
	   'Edit Another' => 'Edit Another',
	   'Eoy' => 'Eoy',
	   'Y' => 'Y',
	   'Out of Range' => 'Out of Range',

	   );

my $self = 'dated.pl';


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


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

print "$doctype\n<html><head><title>$lex{Edit} $lex{Date}</title>\n";
print "<link rel=\"stylesheet\" href=\"$css\" type=\"text/css\">\n";

print "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" ";
print "href=\"/js/calendar-blue.css\" title=\"blue\">\n";
print "<script type=\"text/javascript\" src=\"/js/calendar.js\"></script>\n";
print "<script type=\"text/javascript\" src=\"/js/lang/calendar-en.js\"></script>\n";
print "<script type=\"text/javascript\" src=\"/js/calendar-setup.js\"></script>\n";

print "$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{Edit} $lex{Date}</h1>\n";

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

if ( $arr{writeflag} ) {
    delete $arr{writeflag};
    updateRecord();
}


my $sth = $dbh->prepare("select * from dates where id = ?"); 
$sth->execute( $arr{id} ) ;
my ( $id, $date, $type, $desc1, $desc2, $dayincycle, $dayfraction ) = $sth->fetchrow;

if ($dayincycle eq $lex{Y} or $dayincycle eq $lex{Yes} or $dayincycle == 1 ) {
    $dayincycle = $lex{Yes};
} else {
    $dayincycle = $lex{No};
}


print "<form action=\"$self\" method=\"post\">\n";
print "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
print "<input type=\"hidden\" name=\"writeflag\" value=\"1\">\n";

print "<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\">\n";

# Date
print "<tr><td class=\"bra\">$lex{Date}</td>\n";
print "<td class=\"la\"><input type=\"text\" name=\"date\" id=\"date\" size=\"10\" value=\"$date\">
<button type=\"reset\" id=\"start_trigger\">...</button></td></tr>\n";

# Type
print "<tr><td class=\"bra\">$lex{Type}</td>\n";
print "<td class=\"la\"><select name=\"type\"><option>$type</option>\n";
print "<option>$lex{StatHol}</option><option>$lex{Inservice}</option>\n";
print "<option>$lex{Holiday}</option><option>$lex{Other}</option>\n";
print "</select></td></tr>\n";


# Description
print "<tr><td class=\"bra\">$lex{Description}</td>\n";
print "<td class=\"la\"><input type=\"text\" name=\"desc1\" size=\"30\" value=\"$desc1\"></td></tr>\n";

print "<tr><td class=\"bra\">$lex{'Day In Cycle'}?</td>\n";
print "<td class=\"la\"><select name=\"dayincycle\"><option>$dayincycle</option><option>$lex{No}</option>";
print "<option>$lex{Yes}</option></select></td></tr>\n";

print "<tr><td class=\"bra\">$lex{Fraction} $lex{Closed}</td>\n<td class=\"la\">";
print "<input type=\"text\" name=\"dayfraction\" size=\"6\" value=\"$dayfraction\"></td></tr>\n";


print "</table>\n";
print "<p><input type=\"submit\" value=\"". $lex{'Update Record'}. "\"></p>\n";
print "</form></center>\n";

print "<script type=\"text/javascript\">
  Calendar.setup({
    inputField     :    \"date\", // id of the input field
    ifFormat       :    \"%Y-%m-%d\", // format of the input field
    button         :    \"start_trigger\", // trigger for the calendar (button ID)
    singleClick    :    false,        // double-click mode
    step           :    1             // show all years in drop-down boxes (instead of every other year as default)
    });
</script>\n";

print "</body></html>\n";


#---------------
sub updateRecord {
#---------------

    #foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}<br>\n"; }

    if ( $arr{dayincycle} eq $lex{Yes} ) {
	$arr{dayincycle} = 1;
    } else { 
	$arr{dayincycle} = 0; 
    }


    if ( $arr{dayfraction} < 0 or $arr{dayfraction} > 1 ) {
	print "<h3>$lex{Fraction} $lex{'Out of Range'} (0-1)</h3>\n";
	print "</body></html>\n";
	exit;
    }


    my $sth = $dbh->prepare("update dates set date = ?, type = ?, desc1 = ?, 
      desc2 = ?, dayincycle = ?, dayfraction = ? where id = ?");

    $sth->execute( $arr{date}, $arr{type}, $arr{desc1}, $arr{desc2},
		   $arr{dayincycle}, $arr{dayfraction}, $arr{id} );

    if ( not $DBI::errstr ) {
	print "<h3>$lex{'Record(s) Updated'}</h3></p>";

    } else {
	print "<h3>$lex{Error}: $DBI::errstr;<br>\n";
	print "$lex{Contact} $adminname <a href=\"mailto:$adminemail\">$adminemail</a></h3>\n"; 
    }

    print "[ <a href=\"datedeled.pl\">$lex{'Edit Another'} $lex{Date}</a> |\n";
    print "<a href=\"$eoypage\">$lex{Eoy}</a> ]</p></body></html>\n";

    exit;

}
