<?php
// cal.php
ini_set('display_errors', 1); 
$today = date("m/d/y");
start();
$mo = $_SESSION['mo'];
$YR = $_SESSION['YR'];
extract($_REQUEST);
if (isset($_REQUEST['decMo_y'])):
// decrease month
  if ($mo == 1):
    $mo= 12;
    $YR -=1;
  else:
    $mo -= 1;
  endif;
  $_SESSION['mo'] = $mo;  // current mo
  $_SESSION['YR'] = $YR;  // current YR
elseif (isset($_REQUEST['incMo_y'])):
// increase month
  if ($mo == 12):
    $mo= 1;
    $YR +=1;
  else:
    $mo += 1;
  endif;
  $_SESSION['mo'] = $mo;  // current mo
  $_SESSION['YR'] = $YR;  // current YR
  msgarr($_SESSION);
elseif (isset($_REQUEST['add'])):
// insert new item and get time and description
  $_SESSION['mo'] = $_REQUEST['mo'];
  $_SESSION['YR'] = $_REQUEST['YR'];
  $y=$_SESSION['YR']; $m=$_SESSION['mo'];
  $cDate = "$y-$m-$iDay";
  $cDate = "$m/$iDay/$y";
  $mDate = date('Y-m-d',strtotime($cDate));
  $date = dtoc(ctod($cDate));
  $sql = "insert into cal set date = '$mDate'";
  sql($sql);
  $Id = lastauto();
  $msg = "Enter new time and description";
  include ('header.inc');
  $time = ''; $descr = '';
  include ('detail.inc');
  exit;
elseif (isset($_REQUEST['update'])):
// display details and modify or delete
  extract(retreive('*','cal','Id',$Id));  
  $mDate = $date;
  $date = dtoc(ctod($date));
  $msg = "Modify event and click UPDATE<br> or DELETE<br>";
  include ('header.inc');
  $DoW = date("D",strtotime($date));
  include ('detail.inc');
  exit;
elseif (isset($_REQUEST['procMod'])):
  $_REQUEST['time'] = strtoupper($_REQUEST['time']);
  update ('cal',"Id=$Id");
elseif (isset($_REQUEST['delete'])):
  $sql = "delete from cal  where ID=$Id";
  sql($sql);
elseif (isset($_REQUEST['reportSel'])):
  $msg = "Select date for start and stop";
  $from = get1st();
  $to = DatePlus ($from,365);
  include ('header.inc');
  include ('reportSel.inc');
  exit;
elseif (isset($_REQUEST['procReport'])):
  $cal = "<style>td {font-weight: bold}</style>";
  $start = cdtom($from);
  $stop = cdtom($to);
  $cStart = dtoc(ctod($start));
  $cStop = dtoc(ctod($stop));
  $sql = "select * from cal where date>='$start' and date<='$stop' order by date";
  $sql = "select * from cal where date>='$start' and date<='$stop' and 
    time is not null and descr is not null order by date";
  $cur = sql($sql);
  $moP='z';
  while ($row=getRow($cur)):
    extract($row);
// get mo from mysql date
    $mo = substr($date,5,2); 
    $dDate = ctod($date);
    if ($mo <> $moP):
// new month
      $cMo = date('F',$dDate);
      $cYR = date('Y',$dDate);
      $cal .= "<tr><td colspan=5>$cMo $cYR</td></tr>";
      $moP = $mo;
    endif;
    $dow = date('D',$dDate);
    $day = date('j',$dDate);
    $cal .= "<tr><td></td><td>$dow</td><td>$day</td><td>$time</td><td>$descr</td></tr>";
  endwhile;
  include ('report.inc');
  exit;
elseif (isset($_REQUEST['selMo'])):
// returns 100*YR + mo
  $ip = strpos($selMo/100,'.');
  $mo = substr($selMo/100,$ip+1);
  //msg("$ip, $mo");
  // $mo = $selMo%100;
  $YR = substr($selMo/100,2,2);
  $_SESSION['mo'] = $mo;
  $_SESSION['YR'] = $YR;
  //msgarr($_SESSION);
endif;
displayMo($mo, $YR);
////////////////////////////////////////////////////////////////////
function displayMo($mo, $YR){
$moList = getMoList();
$cToday = dtoc(time());
$today = cdtom($cToday);
$cMo = date("F", mktime(0, 0, 0, $mo, 10));
// DoW 1st of month
$d1st = ctod("$mo/01/$YR");
$DoW1st = date('w',$d1st)+1; 
// days in month
$nDays = cal_days_in_month(CAL_GREGORIAN,$mo,$YR);
$msg = 'Select Month';
include ('header.inc');
// partial week, if any
$calDet =  '<tr>';
// first week blanks
for ($iDay=1; $iDay<=$DoW1st-1; $iDay++)
  $calDet .= '<td style="text-align: center; vertical-align: text-top">x</td>';
// first week dates
$numDays = 8-$DoW1st;
for ($iDay=1; $iDay<=$numDays; $iDay++):
  $mDate = cdtom("$mo/$iDay/$YR");
  $mo = $_SESSION['mo']; $YR = $_SESSION['YR'];
  if ($mDate == $today):
// pink background
    $calDet .=  "<td style=\"vertical-align: text-top; font-size: xx-large; font-weight: bold;
      background-color: pink\">
      <a href=\"cal.php?iDay=$iDay&mo=$mo&YR=$YR&add\">$iDay</a>";
  else:
      $calDet .= "<td style=\"vertical-align: text-top; font-size: xx-large; font-weight: bold;\">
      <a href=\"cal.php?iDay=$iDay&mo=$mo&YR=$YR&add\">$iDay</a>";
  endif;  
  $sql = "select * from cal where date = '$mDate'";
  $cur = sql($sql);
  $nRows = reccount();
  if ($nRows > 0):
    $i = 1;
    while ($row = getRow($cur,$i)): 
      extract($row);
      $calDet .=  "<a href=\"cal.php?Id=$Id&mo=$mo&YR=$YR&update\">
        <span style=\"color: red\">$time</span> $descr</a>";
      if ($i == $nRows)
        break;
      $i += 1;
    endwhile;
  endif;
endfor;
// rest of dates
$day1 = 9-$DoW1st;
for ($iDay=$day1; $iDay<=$nDays; $iDay++):
  $dow = date('w',ctod("$mo/$iDay/$YR"));  // 0=sunday
  if ($dow == 0)
// sunday, start new line
    $calDet .=  '</tr><tr>';
  $mDate = cdtom("$mo/$iDay/$YR");
  if ($mDate == $today)
// put red chk mk along side date 
    $calDet .=  "<td style=\"vertical-align: text-top; font-size: xx-large; font-weight: bold;
      background-color: pink\">
      <a href=\"cal.php?iDay=$iDay&mo=$mo&YR=$YR&add\">$iDay</a>";
  else
    $calDet .=  "<td style=\"vertical-align: text-top; font-size: xx-large; font-weight: bold;\">
    <a href=\"cal.php?iDay=$iDay&mo=$mo&YR=$YR&add\">$iDay</a>";
// loop thru apts for date 
  $sql = "select * from cal where date = '$mDate'";
  //order by     substring(REPLACE(concat('0',time),'12:','00:'),-7), substr(time,-2) ";
  $cur = sql($sql);
  $nRec = reccount();
  if ($nRec > 0):
    $i = 1;
    while ($row = getRow($cur,$i)): 
      extract($row);
      $calDet .=  "<a href=\"cal.php?Id=$Id&mo=$mo&YR=$YR&update\">";
      $calDet .= "<span style=\"color: red\"><br>$time</span> $descr</a>";
      if ($i == $nRec)
        break;
      $i += 1;
    endwhile;
  endif;
endfor;
// finish last line 
$nd = 6-$dow;
for ($j=1; $j<=$nd; $j++)  
  $calDet .=  '<td style="text-align: center; vertical-align: text-top  ;
    font-size: xx-large">x</td>';
$calDet .=  '</tr></form>';
include ('cal.inc');
//msgarr($_SESSION);
}
///////////////////////////////////////////////
function getMoList(){
  $mo = $_SESSION['mo'];
  $YR = $_SESSION['YR'];
//start 6 mo back, go for 13 mo = 19 mo
  $today = date('m/d/Y');
  $curMo = date('m',ctod($today));
  $curYR = date('Y',ctod($today));
  //$cDate = dateplus(dtoc($date),-181);
  $cDate = dateplus(dtoc(ctod($today)),-181);
  $mo = date('m',ctod($cDate));
  $YR = date('Y',ctod($cDate));
  $moList = '';
  for ($i=1; $i<=20; $i++):
    $val = 100*$YR+$mo;
    $disp = date('M',ctoD($cDate)).' '.$YR;
// select option with current date
    if ($mo == $curMo and $YR == $curYR) :
      $moList .= "<option onclick='this.form.submit()' value=$val
        selected>$disp</option>\n";
    else:
      $moList .= "<option onclick='this.form.submit()'value=$val>
        $disp</option>\n";
    endif;
    if ($mo == 12):
      $mo = 1;
      $YR += 1;
    else:
      $mo += 1;
    endif;
    $cDate = "$mo/1/$YR";
  endfor;
  return $moList;
}
////////////////////////
function start(){
// set error reporting and connects to mysql based on server, initializes mo, YR session vart
session_start();
$Server=substr($_SERVER['SERVER_SOFTWARE'],0,6);
$site = $_SERVER['SERVER_NAME'];
if ($Server=='Abyss/')
  error_reporting(E_ALL);  // testing only
else 
//  error_reporting(0);
  error_reporting(0);    // production
include ('../common/mysqli.php');
include ('../common/functions.php');
if ($Server=='Abyss/'):
  $host='localhost'; $user='php';$password = 'jack'; $db = 'cal';
//elseif (strpos($site,'iketrain.net') > 0):
// host-ed
// $host='localhost'; $user='biketra1_php'; //
// $password='s!vf~L_YP2d1{[S';
// $db = 'biketra1_cal';
else:
// hustly
  $host='localhost:3306'; $user = 'php1'; $password='Jackie$522543';
  $db = 'jobtrakliteh_cal';  
endif;
//msg("$host,$user,$password");
openDb($host,$user,$password);
$r = sql("use $db");
if (!isset($_SESSION['mo'])):
// not set, use current mo, YR;
// current yr
  $YR = date('Y',time());
// current month
  $mo = date('m',time());
  $_SESSION['mo'] = $mo;
  $_SESSION['YR'] = $YR;
endif;
}
////////////////////////////////////////////////////////////
function get1st(){
  $today = date('Y-m-d');
  $d = date("Y-m-01", strtotime($today));
  $d = ctod($d);
  return dtoc($d);
}
?>
