<?php
/*
TBS Calendar Application http://TomHenry.us/tbs3/
-------------------------------------------------
To Do List
beta testing
* add cookie 30 day sticky "lang" choice
* add cookie to make 'Month/Year' ddlb choice
* add highlight for current day box
* generalize internationalization
v 2.3 10Jan08
Dedicated db admin forms - source provided
English "Euro" style week begins with Monday
v 2.2 9Jan08
Partial internationalization (French only so far)
Thanks to Skrol29 for translating
v 2.1 9Jan08
Clean up rendering of unused table cells before & after cal days
Fixed cursor state for mouseovers
v 2.0 9Jan08
Add crude admin for add/edit/delete
v 1.5 8Jan08
Added calendar events data
Add CSS control of elements
v 1.0 7Jan08
Just the table layout done using serialize
*/
error_reporting(E_ERROR);
$debug=false; // do print_r()'s & concat into "$dumped" var, shows debug info at bottom of page
// ----NOTE---- TBS and ezSQL are auto_prepend'ed in my .htaccess file
// stop ezSQL query caching while debugging
$db->use_disk_cache = false;
$db->cache_queries = false;
$Months['En'] = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$Days['En'] = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); //start Sund
$Months['Fr'] = array('Janvier', 'Févier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
$Days['Fr'] = array('Lundi','Mardi','Mercredi', 'Jeudi', 'Vendredi','Samedi', 'Dimanche'); //start Mond
$Months['Eu'] = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$Days['Eu'] = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'); //start Mond
!isset($lang) ? $lang="En": NULL ; // Set DEFAULT $lang HERE
//$lang="Fr"; // override for testing
$MonthNames=$Months["$lang"];
$DayNames=$Days["$lang"] ;
$lang=="En" ? $langshift=1 : $langshift=2; // Monday 1st day-of-week for Euro
foreach($MonthNames AS $key=>$val){
$MonthName="MonthName".($key+1);
$$MonthName=$val;
}
foreach($DayNames AS $key=>$val){
$DayName="DayName".($key+1);
$$DayName=$val;
}
/* ======================================================================
Each month starts on the [day-of-week] table <td> element
$month_first_day_of_week = date('w', mktime(0, 0, 0, 1, 1, 2008));
Stuff table blocks_array with this months records -- order by 'event_date'
======================================================================== */
// let's examine the pre-existing cookie
$debug ? $dumped .= "\$_COOKIE= ".print_r($_COOKIE,true) : NULL ;
// set a LANGUAGE cookie to expire in 30 days
if($_GET['lang']){
setcookie("lang", $_GET['lang'], time()+60*60*24*30 ); // expire in 30 days
$lang=$_GET['lang'];
}else{
setcookie("lang", $lang, time()+60*60*24*30 );
}
// Add a COOKIE to hold active month/year
if($go=="Go"){
$dumped .= "GO= $go<br>";
setcookie("setmonth", $_POST['setmonth']);
setcookie("setyear", $_POST['setyear']);
$setmonth=$_POST['setmonth'];
$setyear=$_POST['setyear'];
}
if( !isset($setmonth) ){ // display the current month calendar
$displaymonth=(int)date("n"); // current month
$displayyear=date("Y"); // current year
$labelmonth=date('F', mktime(0, 0, 0, $displaymonth, 1, $displayyear));
$monthindex=$displaymonth-1;
$labelmonth=$MonthNames[$monthindex];
$labelyear=date("Y");
}else{ // month has been selected by user
$displaymonth=(int)date("n", mktime(0, 0, 0, $setmonth, 1, $setyear) );
$displayyear=date("Y", mktime(0, 0, 0, $setmonth, 1, $setyear));
$labelmonth=date('F', mktime(0, 0, 0, $displaymonth, 1, $displayyear));
$monthindex=$displaymonth-1;
$labelmonth=$MonthNames[$monthindex];
$labelyear=date("Y", mktime(0, 0, 0, $displaymonth, 1, $displayyear));
}
// ===TJH===
// I decided to do this application without doing TBS "sub-blocks" which
// would need to use sub-queries for each of the days in the month
// Instead - I'll use TBS "ondata function" feature which I like because
// of its flexibility and ease of use.
// ----------------------------------------------------------------------
// Create a dummy array called "dateblocks" that will be used by TBS to MergeBlock
// This array sets an element for each <td> in the table for each/all days of the month
// do some arithmetic to make empty blocks (<td>'s) before first day of month
$month_first_day_of_week = date('w', mktime(0, 0, 0, $displaymonth, 1, $displayyear));
$month_days_in_month = date('t', mktime(0, 0, 0, $displaymonth, 1, $displayyear));
$totalblocks=$month_first_day_of_week + $month_days_in_month;
for ($i = $langshift; $i <= $totalblocks; $i++) {
// Need "2007-01-23" style date format to be compatible with db content
// when we want to do an "INSERT" of new event into the db
//$YMD = $displayyear."-".$displaymonth."-".$i-$month_first_day_of_week;
$YMD = (string)$displayyear;
$YMD .="-".$displaymonth;
$D = (string)$i-$month_first_day_of_week;
$YMD = $YMD."-".$D;
settype($YMD,"string");
// now make the elements of the main array for the MergeBlock
$dateblocks[$i]=array(
'block'=>$i, // the 'block' refers to the <td>'s in the table
'ymd'=>$YMD,
'day'=>$i-$month_first_day_of_week); // the days of the month
}
// The db table being used here is from the very nice "LTW Calendar" application
// it contains records for each scheduled event
// Do query to get the "event" records --modify-- for your own database structure
$sql = "SELECT id,name,event_date, DAYOFMONTH(event_date) AS dayofmonth, start_time,end_time,description,location FROM ltw_eventsv4 WHERE MONTH(event_date)=$displaymonth AND YEAR(event_date)=$displayyear ORDER BY event_date,start_time";
$event = $db->get_results($sql);// use ezSQL
$debug ? $dumped .= "db query result = ".print_r($event,true)."<hr>" : NULL ; // for DEBUG
// Now, using the db results of events, we'll plug the data into the
// "event" array element to the relevant table blocks
foreach($event AS $key=>$val){
$day = $val->dayofmonth;
$dateblocks[$day+$month_first_day_of_week]['event'][]= // create an "event" array element for each db record
array(
'id'=>$val->id,
'daylabel'=>$val->dayofmonth,
// 'ymd'=>$val->ymd,
'ymd'=>$val->event_date,
'name'=>$val->name,
'description'=>$val->description,
'start_time'=>$val->start_time,
'location'=>$val->location
);
} // --end-- foreach(event)
// now we have the populated dateblocks array!!!
$debug ? $dumped .= "<hr>dateblocks =". print_r($dateblocks,true)."<hr>" : NULL ; // for DEBUG
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate(basename($_SERVER['SCRIPT_NAME'], ".php").".tpl") ;
//$TBS->LoadTemplate(basename($_SERVER['SCRIPT_NAME'], ".php").".html") ; //poor Apache index. setting
$TBS->MergeBlock('bx','array',$dateblocks);
$TBS->Show() ;
// ==========================================================================
// We need to handle multiple events in given day...
// the ondata function merges them into a single value
function event_block($BlockName,&$CurrRec,$RecNum){
if($CurrRec['day']>0){ // ONLY add "event" info to elements with real dates
$CurrRec['daylabel']=$CurrRec['day']; // add field for labelling the boxes
// Add highlight for current day's box
$CurrRec['ymd']==date("Y-n-j") ? $CurrRec['cellclass']="today" : $CurrRec['cellclass']="isday" ;
}else{
$CurrRec['cellclass']="notday"; // add a field for labelling the boxes
}
if(gettype($CurrRec['event'])=="array"){ // check db record to see if it has "event" array
foreach($CurrRec['event'] AS $key=>$val){ // create an output line for each event in db
$time = substr($val['start_time'],0,5); // trim off the seconds from the hh:mm:ss format in database
// stuff multiple lines into single field for output
$valstuff .= $time." <a class=mouseover href=\"\" LANGUAGE=\"Javascript\" onclick=\"openmenuwindow('http://tomhenry.us/tbs3/calendar_tbs/event_admin.php?action=edit&id=".$val['id']."&name=".$val['name']."')\" title=\"".htmlentities(stripslashes($val['description']))."\n".htmlentities(stripslashes($val['location']))."\r\n --Click to Edit Event--\">".$val['name']."</a><br>";
}
$CurrRec['valstuff']=$valstuff;
}
} // --end-- ondata function
?>