cvs: phpweb / cal.php
| From: | Gabor Hojtsy | Date: | Mon, 16 Dec 2002 17:03:25 +0000 |
| Subject: | cvs: phpweb / cal.php | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-14253@lists.php.net to get a copy of this message | ||
goba Mon Dec 16 12:03:25 2002 EDT
Modified files:
/phpweb cal.php
Log:
New, commented version of cal.php
- Indentation standardisation
- Added several comments
- Made some file reads and variable
checks more error-proof, so it gracely
degrades from more problematic situations
Still nothing done about year problems, I am on it...
Goba
Index: phpweb/cal.php diff -u phpweb/cal.php:1.35 phpweb/cal.php:1.36 --- phpweb/cal.php:1.35 Fri Nov 22 13:19:13 2002 +++ phpweb/cal.php Mon Dec 16 12:03:23 2002 @@ -1,89 +1,123 @@ <?php -require_once 'prepend.inc'; +include_once 'prepend.inc'; -# this serves three different forms of the calendar data: -# * a monthly view ($cm, $cy) -# * a daily view ($cm, $cd, $cy) -# * an individual item view ($id) -# if we encounter an error display an event or a day, we display the current -# month (or the month of the requested day) - -$begun = 0; -$id = isset($id) ? (int)$id : 0; -$cy = isset($cy) ? (int)$cy : 0; -$cm = isset($cm) ? (int)$cm : 0; -$cd = isset($cd) ? (int)$cd : 0; +/* + This script serves three different forms of the calendar data: + a monthly view ($cm, $cy) + a daily view ($cm, $cd, $cy) + an individual item view ($id) + For the two later, the month view is also displayed beneath the specifically + asked data. If we encounter an error, we have a fallback to display the + actual month/year. +*/ + +$begun = FALSE; $errors = array(); +$id = isset($id) ? (int) $id : 0; +$cy = isset($cy) ? (int) $cy : 0; +$cm = isset($cm) ? (int) $cm : 0; +$cd = isset($cd) ? (int) $cd : 0; +// We need to look up an event with an ID if ($id) { - if ($event = load_event($id)) { - commonHeader("Event: ".stripslashes(htmlentities($event['sdesc']))); - display_event($event, 0); - $begun++; - } - else { - $errors[] = "There is no event for specified id ('".htmlentities($id)."')"; - } + // Try to load event by ID and display header and info for that event + if ($event = load_event($id)) { + commonHeader("Event: " . stripslashes(htmlentities($event['sdesc']))); + display_event($event, 0); + $begun = TRUE; + } + // Unable to find event, put this to the error messages' list + else { + $errors[] = "There is no event for specified id ('".htmlentities($id)."')"; + } } + +// Year, month and day specified, display a daily view elseif ($cy && $cm && $cd) { - if (checkdate($cm,$cd,$cy)) { - $date = mktime(0,0,1,$cm,$cd,$cy); - if ($events = load_events($date)) { - commonHeader("Events: ".date("F j, Y", $date)); - echo "<h2>", date("F j, Y", $date), "</h2>\n"; - foreach ($events as $event) { - display_event($event, 0); - } - $begun++; - } - else { - $errors[] = "There are no events for the specified date (".date("F j, Y",$date).")."; - } - } - else { - $errors[] = "The specified date (".htmlentities("$cy/$cm/$cd").") was not valid."; - unset($cm); unset($cd); unset($cy); - } + + // Check if date is valid + if (checkdate($cm,$cd,$cy)) { + + // Date integer for that day + $date = mktime(0, 0, 1, $cm, $cd, $cy); + + // Try to load events for that day, and display them all + if ($events = load_events($date)) { + commonHeader("Events: ".date("F j, Y", $date)); + echo "<h2>", date("F j, Y", $date), "</h2>\n"; + foreach ($events as $event) { + display_event($event, 0); + echo "<br />"; + } + $begun = TRUE; + } + + // Unable to load events for that day + else { + $errors[] = "There are no events for the specified date (".date("F j, Y",$date).")."; + } + } + + // Wrong date specified + else { + $errors[] = "The specified date (".htmlentities("$cy/$cm/$cd").") was not valid."; + unset($cm); unset($cd); unset($cy); + } } +// Check if month and year is valid if ($cm && $cy && !checkdate($cm,1,$cy)) { - $errors[] = "The specified year and month (".htmlentities("$cy, $cm").") are not valid."; - unset($cm); unset($cy); + $errors[] = "The specified year and month (".htmlentities("$cy, $cm").") are not valid."; + unset($cm); unset($cy); } -if (!$cm) $cm = date("m"); -if (!$cy) $cy = date("Y"); +// Give defaults for the month and day values if they were invalid +if (!isset($cm) || $cm == 0) { $cm = date("m"); } +if (!isset($cy) || $cy == 0) { $cy = date("Y"); } -$date = mktime(0,0,1,$cm,1,$cy); +// Start of the month date +$date = mktime(0, 0, 1, $cm, 1, $cy); if (!$begun) { commonHeader("Events: ".date("F Y", $date)); ?> <div class="tip"> -<p>If you would like to suggest an upcoming event to be listed on this -calendar, you can use <a href="submit-event.php">our event submission -form</a>.</p> -<p>You can click on each of the events for details, or on the number for a day -to get the details for all of the events taking place that day.</p> + <p> + If you would like to suggest an upcoming event to be listed on this + calendar, you can use <a href="submit-event.php">our event submission + form</a>. + </p> + <p> + You can click on each of the events for details, or on the number for a day + to get the details for all of the events taking place that day. + </p> </div> <?php } -$events = load_events($date,1); +// Get events list for a whole month +$events = load_events($date, TRUE); + +// If there was an error, or there are no events, this is an error +if ($events === FALSE || count($events) == 0) { + $errors[] = "No events found for this month"; +} + +// If there were any error, display them +if (count($errors) > 0) { + display_errors($errors); + commonFooter(); + exit; +} + +// Beginning and end of this month +$bom = mktime(0, 0, 1, $cm, 1, $cy); +$eom = mktime(0, 0, 1, $cm+1, 0, $cy); -if (isset($errors) && $errors) { - display_errors($errors); - commonFooter(); - exit(0); -} - -# beginning and end of this month -$bom = mktime(0,0,1,$cm, 1,$cy); -$eom = mktime(0,0,1,$cm+1,0,$cy); - -# last month and next month -$lm = mktime(0,0,1,$cm,0,$cy); -$nm = mktime(0,0,1,$cm+1,1,$cy); +// Last month and next month +$lm = mktime(0, 0, 1, $cm, 0, $cy); +$nm = mktime(0, 0, 1, $cm+1, 1, $cy); +// Print out navigation links for previous and next month echo '<br /><table bgcolor="#d0d0d0" width="100%" border="0" cellspacing="0" cellpadding="3">', "\n<tr>", '<td align="left" width="33%"><a href="', $PHP_SELF, strftime('?cm=%m&cy=%Y">%B, %Y</a></td>', $lm), @@ -92,142 +126,208 @@ strftime('?cm=%m&cy=%Y">%B, %Y</a></td>', $nm), "</tr>\n</table>\n"; -# begin the calendar +// Begin the calendar table echo '<table id="cal" bgcolor="#f0f0f0" width="100%" border="1" cellspacing="0" cellpadding="3">', "\n",'<tr bgcolor="#a0a0a0">',"\n"; + +// Print out headers for weekdays for ($i = 0; $i < 7; $i++) { - echo '<th width="14%">', date("l",mktime(0,0,1,4,$i+1,2001)), "</th>\n"; + echo '<th width="14%">', date("l",mktime(0,0,1,4,$i+1,2001)), "</th>\n"; } echo "</tr>\n<tr>"; -# generate the requisite number of blank days to get things started +// Generate the requisite number of blank days to get things started for ($days = $i = date("w",$bom); $i > 0; $i--) { - echo '<td bgcolor="#d0d0d0"> </td>'; + echo '<td bgcolor="#d0d0d0"> </td>'; } +// Print out all the days in this month for ($i = 1; $i <= date("t",$bom); $i++) { - echo '<td valign="top"><a class="day" href="', - $PHP_SELF, "?cm=$cm&cd=$i&cy=$cy", - '">',$i,'</a>'; - display_events_for_day(date("Y-m-",$bom).sprintf("%02d",$i),$events); - echo '</td>'; - if (++$days % 7 == 0) echo "</tr>\n<tr>"; + + // Print out day number and all events for the day + echo '<td valign="top"><a class="day" href="', + $PHP_SELF, "?cm=$cm&cd=$i&cy=$cy", + '">',$i,'</a>'; + display_events_for_day(date("Y-m-",$bom).sprintf("%02d",$i), $events); + echo '</td>'; + + // Break HTML table row if at end of week + if (++$days % 7 == 0) echo "</tr>\n<tr>"; } -# generate the requisite number of blank days to wrap things up +// Generate the requisite number of blank days to wrap things up for (; $days % 7; $days++) { - echo '<td bgcolor="#d0d0d0"> </td>'; + echo '<td bgcolor="#d0d0d0"> </td>'; } + +// End HTML table of events echo "</tr>\n</table>\n"; +// Print out common footer commonFooter(); -# FUNCTIONS +// Generate the date on which a recurring event falls for a given month +// $bom and $eom are the first and last day of the month to look at +function date_for_recur($recur, $day, $bom, $eom) { + + // $day == 1 == 'Sunday' == date("w",'some sunday')+1 + + // ${recur}th $day of the month + if ($recur > 0) { + $bomd = date("w", $bom) + 1; + $days = (($day - $bomd + 7) % 7) + (($recur - 1) * 7); + return mktime(0,0,1, date("m",$bom), $days + 1, date("Y",$bom)); + } + + // ${recur}th to last $day of the month + else { + $eomd = date("w",$eom) + 1; + $days = (($eomd - $day + 7) % 7) + ((abs($recur) - 1) * 7); + return mktime(0,0,1, date("m",$bom)+1, -$days, date("Y",$bom)); + } +} -/* generate the date on which a recurring event falls for a given month */ -/* $bom and $eom are the first and last day of the month to look at */ -function date_for_recur($recur,$day,$bom,$eom) { - /* $day == 1 == 'Sunday' == date("w",'some sunday')+1 */ - - if ($recur > 0) { /* ${recur}th $day of the month */ - $bomd = date("w",$bom) + 1; - $days = (($day - $bomd + 7) % 7) + (($recur - 1) * 7); - - return mktime(0,0,1, date("m",$bom), $days + 1, date("Y",$bom)); - } - else { /* ${recur}th to last $day of the month */ - $eomd = date("w",$eom) + 1; - $days = (($eomd - $day + 7) % 7) + ((abs($recur) - 1) * 7); - - return mktime(0,0,1, date("m",$bom)+1, -$days, date("Y",$bom)); - } -} - -/* display a <div> for each of the events that fall on a given day */ -function display_events_for_day($day,$events) { - global $PHP_SELF,$cm,$cy; - foreach ($events as $event) { - if (($event['type'] == 2 && $event['start'] <= $day && $event['end'] >= $day) - || ($event['start'] == $day)) { - echo '<div class="event">', - '<a href="',$PHP_SELF,"?id=$event[id]&cm=$cm&cy=$cy",'">', - stripslashes(htmlentities($event['sdesc'])), - '</a></div>'; - } - } -} - -/* load a single event from the event list, by id */ -function load_event($id) { - $fp = @fopen("backend/events.csv",'r'); - if (!$fp) return; - while (!feof($fp)) { - $event = read_event($fp); - if ($event['id'] == $id) { - fclose($fp); - return $event; - } - } - fclose($fp); - return; -} - -/* load a list of events, either for a particular day or a whole month */ -function load_events($from, $whole_month=0) { - /* we'll take advantage of the equality behavior of this date format */ - $from_date = date("Y-m-d", $from); - $bom = mktime(0,0,1,date("m",$from),1,date("Y",$from)); - $eom = mktime(0,0,1,date("m",$from)+1,0,date("Y",$from)); - $to_date = date("Y-m-d", $whole_month ? $eom : $from); - - $events = $seen = array(); - $fp = @fopen("backend/events.csv",'r'); - if (!$fp) return; - - while (!feof($fp)) { - $event = read_event($fp); - - if(!isset($seen[$event['id']])) $seen[$event['id']] = 1; - else continue; - - switch ($event['type']) { - case 3: /* recurring event */ - $date = date_for_recur($event['recur'],$event['recur_day'],$bom,$eom); - $event['start'] = date("Y-m-d", $date); - /* falls through: now it is just like a single-day event */ - case 1: /* single-day event */ - if ($event['start'] >= $from_date && $event['start'] <= $to_date) { - $events[] = $event; - } - break; - case 2: /* multi-day event */ - if (($event['start'] >= $from_date && $event['start'] <= $to_date) - || ($event['end'] >= $from_date && $event['end'] <= $to_date) - || ($event['start'] <= $from_date && $event['end'] >= $to_date)) { - $events[] = $event; +// Display a <div> for each of the events that are on a given day +function display_events_for_day($day, $events) { + + // For preservation of state in the links + global $PHP_SELF, $cm, $cy; + + // For all events, try to find the events for this day + foreach ($events as $event) { + + // Multiday event, which still lasts, or the event starts today + if (($event['type'] == 2 && $event['start'] <= $day && $event['end'] >= $day) + || ($event['start'] == $day)) { + echo '<div class="event">', + '<a href="',$PHP_SELF,"?id=$event[id]&cm=$cm&cy=$cy",'">', + stripslashes(htmlentities($event['sdesc'])), + '</a></div>'; + } + } +} + +// Find a single event in the events file by ID +function load_event($id) +{ + // Open events CSV file, return on error + $fp = @fopen("backend/events.csv",'r'); + if (!$fp) { return FALSE; } + + // Read as we can, event by event + while (!feof($fp)) { + + $event = read_event($fp); + + // Return with the event, if it's ID is the one + // we search for (also close the file) + if ($event !== FALSE && $event['id'] == $id) { + fclose($fp); + return $event; } - break; } - } - fclose($fp); - return $events; -} - -/* read an event from the event listing */ -function read_event($fp) { - list(,,,,$sdesc,$id,$ldesc,$url,$recur,$tipo,$sdato,$edato) = fgetcsv($fp,8192); - @list($recur,$recur_day) = explode(":",$recur,2); - return array( - 'id' => $id, - 'type' => $tipo, - 'start' => $sdato, - 'end' => $edato, - 'recur' => $recur, - 'recur_day' => $recur_day, - 'sdesc' => $sdesc, - 'url' => $url, - 'ldesc' => base64_decode($ldesc), - ); + + // Close file, and return sign of failure + fclose($fp); + return FALSE; +} + +// Load a list of events. Either for a particular day ($from) or a whole +// month (if second parameter specified with TRUE) +function load_events($from, $whole_month = FALSE) +{ + // Take advantage of the equality behavior of this date format + $from_date = date("Y-m-d", $from); + $bom = mktime(0, 0, 1, date("m",$from), 1, date("Y",$from)); + $eom = mktime(0, 0, 1, date("m",$from) + 1, 0, date("Y",$from)); + $to_date = date("Y-m-d", $whole_month ? $eom : $from); + + // Set arrays to their default + $events = $seen = array(); + + // Try to open the events file for reading, return if unable to + $fp = @fopen("backend/events.csv",'r'); + if (!$fp) { return FALSE; } + + // For all events, read in the event and check it if fits our scope + while (!feof($fp)) { + + // Read the event data into $event, or continue with next + // line, if there was an error with this line + if (($event = read_event($fp)) === FALSE) { + continue; + } + + // Keep event's seen list up to date + // (for repeating events with the same ID) + if (!isset($seen[$event['id']])) { $seen[$event['id']] = 1; } + else { continue; } + + // Check if event is in our scope, depending on type + switch ($event['type']) { + + // Recurring event + case 3: + $date = date_for_recur($event['recur'], $event['recur_day'], $bom, $eom); + $event['start'] = date("Y-m-d", $date); + // Fall through. Now it is just like a single-day event + + // Single-day event + case 1: + if ($event['start'] >= $from_date && $event['start'] <= $to_date) { + $events[] = $event; + } + break; + + // Multi-day event + case 2: + if (($event['start'] >= $from_date && $event['start'] <= $to_date) + || ($event['end'] >= $from_date && $event['end'] <= $to_date) + || ($event['start'] <= $from_date && $event['end'] >= $to_date)) { + $events[] = $event; + } + break; + } + } + + // Close file and return with results + fclose($fp); + return $events; +} + +// Reads an event from the event listing +// Parameter: opened event listing file +function read_event($fp) +{ + // We were unable to read a line from the file, return + if (($linearr = fgetcsv($fp, 8192)) === FALSE) { + return FALSE; + } + + // Corrupt line in CSV file + if (count($linearr) < 12) { return FALSE; } + + // Put the array elements into variables + list( + , , , , + $sdesc, $id, $ldesc, $url, $recur, $tipo, $sdato, $edato + ) = $linearr; + + // Get info on recurring event + @list($recur, $recur_day) = explode(":", $recur, 2); + + // Return with SQL-resultset like array + return array( + 'id' => $id, + 'type' => $tipo, + 'start' => $sdato, + 'end' => $edato, + 'recur' => $recur, + 'recur_day' => $recur_day, + 'sdesc' => $sdesc, + 'url' => $url, + 'ldesc' => base64_decode($ldesc), + ); } ?>
Index: phpweb/cal.php diff -u phpweb/cal.php:1.35 phpweb/cal.php:1.36 --- phpweb/cal.php:1.35 Fri Nov 22 13:19:13 2002 +++ phpweb/cal.php Mon Dec 16 12:03:23 2002 @@ -1,89 +1,123 @@ <?php -require_once 'prepend.inc'; +include_once 'prepend.inc'; -# this serves three different forms of the calendar data: -# * a monthly view ($cm, $cy) -# * a daily view ($cm, $cd, $cy) -# * an individual item view ($id) -# if we encounter an error display an event or a day, we display the current -# month (or the month of the requested day) - -$begun = 0; -$id = isset($id) ? (int)$id : 0; -$cy = isset($cy) ? (int)$cy : 0; -$cm = isset($cm) ? (int)$cm : 0; -$cd = isset($cd) ? (int)$cd : 0; +/* + This script serves three different forms of the calendar data: + a monthly view ($cm, $cy) + a daily view ($cm, $cd, $cy) + an individual item view ($id) + For the two later, the month view is also displayed beneath the specifically + asked data. If we encounter an error, we have a fallback to display the + actual month/year. +*/ + +$begun = FALSE; $errors = array(); +$id = isset($id) ? (int) $id : 0; +$cy = isset($cy) ? (int) $cy : 0; +$cm = isset($cm) ? (int) $cm : 0; +$cd = isset($cd) ? (int) $cd : 0; +// We need to look up an event with an ID if ($id) { - if ($event = load_event($id)) { - commonHeader("Event: ".stripslashes(htmlentities($event['sdesc']))); - display_event($event, 0); - $begun++; - } - else { - $errors[] = "There is no event for specified id ('".htmlentities($id)."')"; - } + // Try to load event by ID and display header and info for that event + if ($event = load_event($id)) { + commonHeader("Event: " . stripslashes(htmlentities($event['sdesc']))); + display_event($event, 0); + $begun = TRUE; + } + // Unable to find event, put this to the error messages' list + else { + $errors[] = "There is no event for specified id ('".htmlentities($id)."')"; + } } + +// Year, month and day specified, display a daily view elseif ($cy && $cm && $cd) { - if (checkdate($cm,$cd,$cy)) { - $date = mktime(0,0,1,$cm,$cd,$cy); - if ($events = load_events($date)) { - commonHeader("Events: ".date("F j, Y", $date)); - echo "<h2>", date("F j, Y", $date), "</h2>\n"; - foreach ($events as $event) { - display_event($event, 0); - } - $begun++; - } - else { - $errors[] = "There are no events for the specified date (".date("F j, Y",$date).")."; - } - } - else { - $errors[] = "The specified date (".htmlentities("$cy/$cm/$cd").") was not valid."; - unset($cm); unset($cd); unset($cy); - } + + // Check if date is valid + if (checkdate($cm,$cd,$cy)) { + + // Date integer for that day + $date = mktime(0, 0, 1, $cm, $cd, $cy); + + // Try to load events for that day, and display them all + if ($events = load_events($date)) { + commonHeader("Events: ".date("F j, Y", $date)); + echo "<h2>", date("F j, Y", $date), "</h2>\n"; + foreach ($events as $event) { + display_event($event, 0); + echo "<br />"; + } + $begun = TRUE; + } + + // Unable to load events for that day + else { + $errors[] = "There are no events for the specified date (".date("F j, Y",$date).")."; + } + } + + // Wrong date specified + else { + $errors[] = "The specified date (".htmlentities("$cy/$cm/$cd").") was not valid."; + unset($cm); unset($cd); unset($cy); + } } +// Check if month and year is valid if ($cm && $cy && !checkdate($cm,1,$cy)) { - $errors[] = "The specified year and month (".htmlentities("$cy, $cm").") are not valid."; - unset($cm); unset($cy); + $errors[] = "The specified year and month (".htmlentities("$cy, $cm").") are not valid."; + unset($cm); unset($cy); } -if (!$cm) $cm = date("m"); -if (!$cy) $cy = date("Y"); +// Give defaults for the month and day values if they were invalid +if (!isset($cm) || $cm == 0) { $cm = date("m"); } +if (!isset($cy) || $cy == 0) { $cy = date("Y"); } -$date = mktime(0,0,1,$cm,1,$cy); +// Start of the month date +$date = mktime(0, 0, 1, $cm, 1, $cy); if (!$begun) { commonHeader("Events: ".date("F Y", $date)); ?> <div class="tip"> -<p>If you would like to suggest an upcoming event to be listed on this -calendar, you can use <a href="submit-event.php">our event submission -form</a>.</p> -<p>You can click on each of the events for details, or on the number for a day -to get the details for all of the events taking place that day.</p> + <p> + If you would like to suggest an upcoming event to be listed on this + calendar, you can use <a href="submit-event.php">our event submission + form</a>. + </p> + <p> + You can click on each of the events for details, or on the number for a day + to get the details for all of the events taking place that day. + </p> </div> <?php } -$events = load_events($date,1); +// Get events list for a whole month +$events = load_events($date, TRUE); + +// If there was an error, or there are no events, this is an error +if ($events === FALSE || count($events) == 0) { + $errors[] = "No events found for this month"; +} + +// If there were any error, display them +if (count($errors) > 0) { + display_errors($errors); + commonFooter(); + exit; +} + +// Beginning and end of this month +$bom = mktime(0, 0, 1, $cm, 1, $cy); +$eom = mktime(0, 0, 1, $cm+1, 0, $cy); -if (isset($errors) && $errors) { - display_errors($errors); - commonFooter(); - exit(0); -} - -# beginning and end of this month -$bom = mktime(0,0,1,$cm, 1,$cy); -$eom = mktime(0,0,1,$cm+1,0,$cy); - -# last month and next month -$lm = mktime(0,0,1,$cm,0,$cy); -$nm = mktime(0,0,1,$cm+1,1,$cy); +// Last month and next month +$lm = mktime(0, 0, 1, $cm, 0, $cy); +$nm = mktime(0, 0, 1, $cm+1, 1, $cy); +// Print out navigation links for previous and next month echo '<br /><table bgcolor="#d0d0d0" width="100%" border="0" cellspacing="0" cellpadding="3">', "\n<tr>", '<td align="left" width="33%"><a href="', $PHP_SELF, strftime('?cm=%m&cy=%Y">%B, %Y</a></td>', $lm), @@ -92,142 +126,208 @@ strftime('?cm=%m&cy=%Y">%B, %Y</a></td>', $nm), "</tr>\n</table>\n"; -# begin the calendar +// Begin the calendar table echo '<table id="cal" bgcolor="#f0f0f0" width="100%" border="1" cellspacing="0" cellpadding="3">', "\n",'<tr bgcolor="#a0a0a0">',"\n"; + +// Print out headers for weekdays for ($i = 0; $i < 7; $i++) { - echo '<th width="14%">', date("l",mktime(0,0,1,4,$i+1,2001)), "</th>\n"; + echo '<th width="14%">', date("l",mktime(0,0,1,4,$i+1,2001)), "</th>\n"; } echo "</tr>\n<tr>"; -# generate the requisite number of blank days to get things started +// Generate the requisite number of blank days to get things started for ($days = $i = date("w",$bom); $i > 0; $i--) { - echo '<td bgcolor="#d0d0d0"> </td>'; + echo '<td bgcolor="#d0d0d0"> </td>'; } +// Print out all the days in this month for ($i = 1; $i <= date("t",$bom); $i++) { - echo '<td valign="top"><a class="day" href="', - $PHP_SELF, "?cm=$cm&cd=$i&cy=$cy", - '">',$i,'</a>'; - display_events_for_day(date("Y-m-",$bom).sprintf("%02d",$i),$events); - echo '</td>'; - if (++$days % 7 == 0) echo "</tr>\n<tr>"; + + // Print out day number and all events for the day + echo '<td valign="top"><a class="day" href="', + $PHP_SELF, "?cm=$cm&cd=$i&cy=$cy", + '">',$i,'</a>'; + display_events_for_day(date("Y-m-",$bom).sprintf("%02d",$i), $events); + echo '</td>'; + + // Break HTML table row if at end of week + if (++$days % 7 == 0) echo "</tr>\n<tr>"; } -# generate the requisite number of blank days to wrap things up +// Generate the requisite number of blank days to wrap things up for (; $days % 7; $days++) { - echo '<td bgcolor="#d0d0d0"> </td>'; + echo '<td bgcolor="#d0d0d0"> </td>'; } + +// End HTML table of events echo "</tr>\n</table>\n"; +// Print out common footer commonFooter(); -# FUNCTIONS +// Generate the date on which a recurring event falls for a given month +// $bom and $eom are the first and last day of the month to look at +function date_for_recur($recur, $day, $bom, $eom) { + + // $day == 1 == 'Sunday' == date("w",'some sunday')+1 + + // ${recur}th $day of the month + if ($recur > 0) { + $bomd = date("w", $bom) + 1; + $days = (($day - $bomd + 7) % 7) + (($recur - 1) * 7); + return mktime(0,0,1, date("m",$bom), $days + 1, date("Y",$bom)); + } + + // ${recur}th to last $day of the month + else { + $eomd = date("w",$eom) + 1; + $days = (($eomd - $day + 7) % 7) + ((abs($recur) - 1) * 7); + return mktime(0,0,1, date("m",$bom)+1, -$days, date("Y",$bom)); + } +} -/* generate the date on which a recurring event falls for a given month */ -/* $bom and $eom are the first and last day of the month to look at */ -function date_for_recur($recur,$day,$bom,$eom) { - /* $day == 1 == 'Sunday' == date("w",'some sunday')+1 */ - - if ($recur > 0) { /* ${recur}th $day of the month */ - $bomd = date("w",$bom) + 1; - $days = (($day - $bomd + 7) % 7) + (($recur - 1) * 7); - - return mktime(0,0,1, date("m",$bom), $days + 1, date("Y",$bom)); - } - else { /* ${recur}th to last $day of the month */ - $eomd = date("w",$eom) + 1; - $days = (($eomd - $day + 7) % 7) + ((abs($recur) - 1) * 7); - - return mktime(0,0,1, date("m",$bom)+1, -$days, date("Y",$bom)); - } -} - -/* display a <div> for each of the events that fall on a given day */ -function display_events_for_day($day,$events) { - global $PHP_SELF,$cm,$cy; - foreach ($events as $event) { - if (($event['type'] == 2 && $event['start'] <= $day && $event['end'] >= $day) - || ($event['start'] == $day)) { - echo '<div class="event">', - '<a href="',$PHP_SELF,"?id=$event[id]&cm=$cm&cy=$cy",'">', - stripslashes(htmlentities($event['sdesc'])), - '</a></div>'; - } - } -} - -/* load a single event from the event list, by id */ -function load_event($id) { - $fp = @fopen("backend/events.csv",'r'); - if (!$fp) return; - while (!feof($fp)) { - $event = read_event($fp); - if ($event['id'] == $id) { - fclose($fp); - return $event; - } - } - fclose($fp); - return; -} - -/* load a list of events, either for a particular day or a whole month */ -function load_events($from, $whole_month=0) { - /* we'll take advantage of the equality behavior of this date format */ - $from_date = date("Y-m-d", $from); - $bom = mktime(0,0,1,date("m",$from),1,date("Y",$from)); - $eom = mktime(0,0,1,date("m",$from)+1,0,date("Y",$from)); - $to_date = date("Y-m-d", $whole_month ? $eom : $from); - - $events = $seen = array(); - $fp = @fopen("backend/events.csv",'r'); - if (!$fp) return; - - while (!feof($fp)) { - $event = read_event($fp); - - if(!isset($seen[$event['id']])) $seen[$event['id']] = 1; - else continue; - - switch ($event['type']) { - case 3: /* recurring event */ - $date = date_for_recur($event['recur'],$event['recur_day'],$bom,$eom); - $event['start'] = date("Y-m-d", $date); - /* falls through: now it is just like a single-day event */ - case 1: /* single-day event */ - if ($event['start'] >= $from_date && $event['start'] <= $to_date) { - $events[] = $event; - } - break; - case 2: /* multi-day event */ - if (($event['start'] >= $from_date && $event['start'] <= $to_date) - || ($event['end'] >= $from_date && $event['end'] <= $to_date) - || ($event['start'] <= $from_date && $event['end'] >= $to_date)) { - $events[] = $event; +// Display a <div> for each of the events that are on a given day +function display_events_for_day($day, $events) { + + // For preservation of state in the links + global $PHP_SELF, $cm, $cy; + + // For all events, try to find the events for this day + foreach ($events as $event) { + + // Multiday event, which still lasts, or the event starts today + if (($event['type'] == 2 && $event['start'] <= $day && $event['end'] >= $day) + || ($event['start'] == $day)) { + echo '<div class="event">', + '<a href="',$PHP_SELF,"?id=$event[id]&cm=$cm&cy=$cy",'">', + stripslashes(htmlentities($event['sdesc'])), + '</a></div>'; + } + } +} + +// Find a single event in the events file by ID +function load_event($id) +{ + // Open events CSV file, return on error + $fp = @fopen("backend/events.csv",'r'); + if (!$fp) { return FALSE; } + + // Read as we can, event by event + while (!feof($fp)) { + + $event = read_event($fp); + + // Return with the event, if it's ID is the one + // we search for (also close the file) + if ($event !== FALSE && $event['id'] == $id) { + fclose($fp); + return $event; } - break; } - } - fclose($fp); - return $events; -} - -/* read an event from the event listing */ -function read_event($fp) { - list(,,,,$sdesc,$id,$ldesc,$url,$recur,$tipo,$sdato,$edato) = fgetcsv($fp,8192); - @list($recur,$recur_day) = explode(":",$recur,2); - return array( - 'id' => $id, - 'type' => $tipo, - 'start' => $sdato, - 'end' => $edato, - 'recur' => $recur, - 'recur_day' => $recur_day, - 'sdesc' => $sdesc, - 'url' => $url, - 'ldesc' => base64_decode($ldesc), - ); + + // Close file, and return sign of failure + fclose($fp); + return FALSE; +} + +// Load a list of events. Either for a particular day ($from) or a whole +// month (if second parameter specified with TRUE) +function load_events($from, $whole_month = FALSE) +{ + // Take advantage of the equality behavior of this date format + $from_date = date("Y-m-d", $from); + $bom = mktime(0, 0, 1, date("m",$from), 1, date("Y",$from)); + $eom = mktime(0, 0, 1, date("m",$from) + 1, 0, date("Y",$from)); + $to_date = date("Y-m-d", $whole_month ? $eom : $from); + + // Set arrays to their default + $events = $seen = array(); + + // Try to open the events file for reading, return if unable to + $fp = @fopen("backend/events.csv",'r'); + if (!$fp) { return FALSE; } + + // For all events, read in the event and check it if fits our scope + while (!feof($fp)) { + + // Read the event data into $event, or continue with next + // line, if there was an error with this line + if (($event = read_event($fp)) === FALSE) { + continue; + } + + // Keep event's seen list up to date + // (for repeating events with the same ID) + if (!isset($seen[$event['id']])) { $seen[$event['id']] = 1; } + else { continue; } + + // Check if event is in our scope, depending on type + switch ($event['type']) { + + // Recurring event + case 3: + $date = date_for_recur($event['recur'], $event['recur_day'], $bom, $eom); + $event['start'] = date("Y-m-d", $date); + // Fall through. Now it is just like a single-day event + + // Single-day event + case 1: + if ($event['start'] >= $from_date && $event['start'] <= $to_date) { + $events[] = $event; + } + break; + + // Multi-day event + case 2: + if (($event['start'] >= $from_date && $event['start'] <= $to_date) + || ($event['end'] >= $from_date && $event['end'] <= $to_date) + || ($event['start'] <= $from_date && $event['end'] >= $to_date)) { + $events[] = $event; + } + break; + } + } + + // Close file and return with results + fclose($fp); + return $events; +} + +// Reads an event from the event listing +// Parameter: opened event listing file +function read_event($fp) +{ + // We were unable to read a line from the file, return + if (($linearr = fgetcsv($fp, 8192)) === FALSE) { + return FALSE; + } + + // Corrupt line in CSV file + if (count($linearr) < 12) { return FALSE; } + + // Put the array elements into variables + list( + , , , , + $sdesc, $id, $ldesc, $url, $recur, $tipo, $sdato, $edato + ) = $linearr; + + // Get info on recurring event + @list($recur, $recur_day) = explode(":", $recur, 2); + + // Return with SQL-resultset like array + return array( + 'id' => $id, + 'type' => $tipo, + 'start' => $sdato, + 'end' => $edato, + 'recur' => $recur, + 'recur_day' => $recur_day, + 'sdesc' => $sdesc, + 'url' => $url, + 'ldesc' => base64_decode($ldesc), + ); } ?>