<?php
include "include/session.php";
include "include/config.php";
include "include/opendb.php";
include "systemaccesscontrol.php";
if ($systemholiday=="SystemHoliday"){
    $agendaDate=array();
    $agendaDateOpen=array();
    $dBlock="";
    $dNoBlock="";
    $query = "SELECT
                date_format(holiday_date,\"%Y,%c,%e\") as dDate,
                date_format(holiday_date,\"y==%Y&&m==%c&&d==%e\") as openDate,
                date_format(holiday_date,\"%b %e, %Y\") as comDate,
                holiday_name
              FROM holiday_event
              ORDER BY holiday_date DESC";
    $result = mysqli_query($dbconn,$query) or die('Query Error : '.mysqli_error($dbconn));
    $num_rows = mysqli_num_rows($result);
    if ($num_rows > 0) {
        $dBlock="YES";
        $count=1;
        $abc="function fHoliday(y,m,d) {";
        array_push($agendaDate,$abc);
        array_push($agendaDateOpen,$abc);
        $abc="var rE=fGetEvent(y,m,d), r=null;";
        array_push($agendaDate,$abc);
        array_push($agendaDateOpen,$abc);
        while($line = mysql_fetch_assoc($result)) {
            $abc="fAddEvent(".$line['dDate'].", \"".$line['holiday_name']."\", null, \"#87ceeb\", \"dodgerblue\");";
            array_push($agendaDate,$abc);
            if ($count==1){
                $def="if (".$line['openDate'].")";
            }else{
                $def="else if (".$line['openDate'].")";
            }
            array_push($agendaDateOpen,$def);
            $def="r=[\"".$line['comDate']." ".$line['holiday_name']."\",gsAction,\"skyblue\",\"red\"]";
            array_push($agendaDateOpen,$def);
            $count=$count+1;
        }
        $abc="return rE?rE:r; // favor events over holidays";
        array_push($agendaDate,$abc);
        array_push($agendaDateOpen,$abc);
        $abc="}";
        array_push($agendaDate,$abc);
        array_push($agendaDateOpen,$abc);
    }
    if ($dBlock=="YES"){
        $filename="calendar/date/agenda_block.js";
        $option="w+";
        if (is_writable($filename)) {
            if (!$handle = fopen($filename, $option)) {
    //            echo "Cannot open file ($filename)";
                exit;
            }
            if (fwrite($handle, stripcslashes(implode("\n", $agendaDate))) === FALSE) {
    //            echo "Cannot write to file ($filename)";
                exit;
            }
    //        echo "Success, wrote to file ($filename)";
            $dNoBlock="YES";
            fclose($handle);
        } else {
    //        echo "The file $filename is not writable";
        }
    }
    if ($dNoBlock=="YES"){
        $filename1="calendar/date/agenda.js";
        $option1="w+";
        if (is_writable($filename1)) {
            if (!$handle1 = fopen($filename1, $option1)) {
    //             echo "Cannot open file ($filename1)";
                 exit;
            }
            if (fwrite($handle1, stripcslashes(implode("\n", $agendaDateOpen))) === FALSE) {
    //            echo "Cannot write to file ($filename1)";
                exit;
            }
//            echo "Success, wrote to file ($filename1)";
            fclose($handle1);
        } else {
    //        echo "The file $filename1 is not writable";
        }
    }
}else{
    //if not admin, do nothing
}
?>
