#!/apps/bin/perl
#
# $Id: new_task.pl,v 1.3 2004/04/16 18:34:28 marki Exp $
##########################################################################
#   Brief Discription:
#
#   execution:  new_task
#
#   cgi script to add a new maintenance task to the maintenance 
#   scheduler and notifier.
#
#   created:  Fri Apr 17 20:22:24 EDT 1998
#
#   author of the script: arne 
#   
##########################################################################


use Time::CTime;
use Time::JulianDay;
use CGI qw(:all);
use DBI;

for ($i=1; $i<32; $i++) {
    $day_num[$i] = $i;
}
%month_labels = (
		 '0' =>'Jan',
		 '1' =>'Feb',
		 '2' =>'Mar',
		 '3' =>'Apr',
		 '4' =>'May',
		 '5' =>'Jun',
		 '6' =>'Jul',
		 '7' =>'Aug',
		 '8' =>'Sep',
		 '9' =>'Oct',
		 '10' =>'Nov',
		 '11' =>'Dec'
		 );
$cgi_query = new CGI;

print $cgi_query->header();
print $cgi_query->start_form();
print $cgi_query->start_html(-title=>'Hall D Maintenance Item Addition Form',
				 -BGCOLOR=>'#bbbbbb');
				 
$time_format="\%c";
$now = time();
$todays_day  =  strftime('%d',(localtime($now)));
$todays_month=  strftime('%m',(localtime($now)))-1;
$todays_year =  strftime('%Y',(localtime($now)));
if (param()) {
# header has already been filled out, loop through ALL the values 
# and see if any were marked....

    $password = $cgi_query->param('passwd');
    if ($password ne 'keepitrunning') {
	print $cgi_query->p("The password you entered is incorrect, please try again...");
    } else {

	$task = $cgi_query->param('task');   # you should check the task is unique!
	$email1 = $cgi_query->param('email1');
	$email2 = $cgi_query->param('email2');
	$period = $cgi_query->param('period');
	$last_day = $cgi_query->param('last_day');
	$last_month = $cgi_query->param('last_month');
	$last_year = $cgi_query->param('last_year');
	$long_task = $cgi_query->param('long_task');

	$period_type=1;

# convert last maintenance date into seconds since epoch
	$last_maintenance = jd_timelocal(0,0,8, $last_day, $last_month, $last_year);
# convert period into seconds
	$period = $period*86400.;
# calulate next maintenance date
	$next_maintenance = $last_maintenance + $period;

	print $cgi_query->p("task: $task was last performed $last_maintenance and needs to be performed again on $next_maintenance");

# create the SQL query and send it

	$sql_cmd = "INSERT into maintenance values \(";
	$sql_cmd .= "\'$task\', \'$email1\', \'$email2\', \'$long_task\',";
	$sql_cmd .= "$last_maintenance, $next_maintenance, $period, $period_type\)";

	print $sql_cmd;

	# Switched to localhost by M. Wise 3/4/2015 after switch to halldweb.jlab.org
	# $hostname = "halldweb1.jlab.org";
	$hostname = "localhost";
	$database = "maintenance";
	$user = "maintainer";
	$password = "";
	$dbh = DBI->connect("DBI:mysql:$database:$hostname", $user, $password);
	do_sql();

    }

} else { # give them the form to fill out

    print $cgi_query->p("Please enter a short UNIQUE task name",
			textfield(-name=>'task',
				  -size=>20,
				  -maxlength=>20));

    print $cgi_query->p("Enter the primary EMAIL contact address",
			textfield(-name=>'email1',
				  -size=>20,
				  -maxlength=>40));
    print $cgi_query->p("Enter the secondary EMAIL contact address",
			textfield(-name=>'email2',
				  -size=>20,
				  -maxlength=>40));
    print $cgi_query->p("How often is the maintenance to be done \(in days\)",
			textfield(-name=>'period',
				  -size=>5,
				  -maxlength=>10));

    print $cgi_query->p("When was the last maintenance performed?",
			popup_menu(-name=>'last_day',
				   -values=> \@day_num,
				   -default=>$todays_day),
			"-",
			popup_menu(-name=>'last_month',
				   -values => [sort numerically keys(%month_labels)],
				   -default =>$todays_month,
				   -labels  => \%month_labels),
			"-",
			popup_menu(-name=>'last_year',
				   -values=> ['2013', '2014', '2015', '2016', '2017', '2018', '2019', , '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028'],
				   -default=>$todays_year));


    print $cgi_query->p("Enter a concise description of the maintenance task");
    print $cgi_query->textarea(-name=>'long_task',
				  -rows=>20,
				  -columns=>50);
    print $cgi_query->p("Enter the Hall D Maintenance password",
			password_field(-name=>'passwd',
				  -size=>20,
				  -maxlength=>40));
    print $cgi_query->p(submit("SUBMIT"), reset("RESET FORM"));
}

print $cgi_query->endform;

sub numerically { $a >= $b; }

sub do_sql {
    $sth = $dbh -> prepare($sql_cmd)
        or die "Can't prepare $sql_cmd: $dbh->errstr\n";
    $rv = $sth -> execute
        or die "Can't execute the query $sql\n error: $sth->errstr\n";
    return;
}
