cvs: phpweb / cal.php cal.sql events.php index.php style.css submit-event.php /include layout.inc
| From: | jim winstead | Date: | Tue, 05 Mar 2002 02:54:21 +0000 |
| Subject: | cvs: phpweb / cal.php cal.sql events.php index.php style.css submit-event.php /include layout.inc | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-8968@lists.php.net to get a copy of this message | ||
jimw Mon Mar 4 21:54:21 2002 EDT
Removed files:
/phpweb cal.sql events.php
Modified files:
/phpweb cal.php index.php style.css submit-event.php
/phpweb/include layout.inc
Log:
rewrote cal.php to get data from backend/events.csv. phpweb is once again
mysql-free. also got rid of events.php, since cal.php can now display info
about particular events. the calendar should look essentially the same, modulo
smaller default text, and some small css tweaks (mainly to put a divider
between events -- i could never tell them apart before).
one small thing to add would be a remove button to events a-la the
manual notes when the magic cookie is set.
Index: phpweb/cal.php diff -u phpweb/cal.php:1.30 phpweb/cal.php:1.31 --- phpweb/cal.php:1.30 Mon Mar 4 16:22:08 2002 +++ phpweb/cal.php Mon Mar 4 21:54:18 2002 @@ -1,331 +1,225 @@ <?php - include_once 'prepend.inc'; +require_once 'prepend.inc'; - // The calendar is hosted at php2.chek.com only! - if (!is_primary_site()) { - /* - if (is_backup_primary()) { - commonHeader("Service Unavailable");?> -<p>Sorry, the event calendar is temporarily unavailable.</p> -<?php - commonFooter(); - exit; +# 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; + +if ($id) { + if ($event = load_event($id)) { + commonHeader("Event: ".htmlentities($event['sdesc'])); + display_event($event, 0); + $begun++; + } + else { + $errors[] = "There is no event for specified id ('".htmlentities($id)."')"; + } +} +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).")."; } - */ - header("Location: http://php2.chek.com/cal.php"); - exit; } - if (!isset($format)) { - $format = 'html'; + else { + $errors[] = "The specified date (".htmlentities("$cy/$cm/$cd").") was not valid."; + unset($cm); unset($cd); unset($cy); } +} - if ($format == 'html') { - commonHeader("Event Calendar",1); +if ($cm && $cy && !checkdate($cm,1,$cy)) { + $errors[] = "The specified year and month (".htmlentities("$cy, $cm").") are not valid."; + unset($cm); unset($cy); +} - include_once 'cvs-auth.inc'; - if (isset($save) && isset($pw)) { # non-developers don't have $user set - setcookie("MAGIC_COOKIE",base64_encode("$user:$pw"),time()+3600*24*20,'/'); - } - if (isset($MAGIC_COOKIE) && !isset($user) && !isset($pw)) { - list($user,$pw) = explode(":", base64_decode($MAGIC_COOKIE)); - } - } +if (!$cm) $cm = date("m"); +if (!$cy) $cy = date("Y"); - @mysql_connect('localhost') or die('unable to connect to database'); - @mysql_select_db('php3'); +$date = mktime(0,0,1,$cm,1,$cy); - $re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); -/* -CREATE TABLE phpcal ( - id int(8) NOT NULL, - sdato date, - edato date, - recur char(12), - sdesc char(16) NOT NULL, - url varchar(128), - ldesc text, - tipo int(1) NOT NULL, - approved int(1) NOT NULL DEFAULT 0, - app_by char(16), - PRIMARY KEY(id), - INDEX (sdato), - INDEX (edato) -) -*/ - /* - * Find the first, second, third, last, second-last etc. weekday of a month - * - * args: day 1 = Monday - * which 1 = first - * 2 = second - * 3 = third - * -1 = last - * -2 = second-last - */ - function weekday($year, $month, $day, $which) { - $ts = mktime(12,0,0,$month+(($which>0)?0:1),($which>0)?1:0,$year); - $done = false; - $match = 0; - $inc = 3600*24; - while(!$done) { - if(strftime('%w',$ts)==$day-1) { - $match++; - } - if($match==abs($which)) $done=true; - else $ts += (($which>0)?1:-1)*$inc; - } - return $ts; - } - - function load_unapproved_events() { - global $re; - $days = days(); - $result = mysql_query("select phpcal.*, country.name as name from phpcal, country where phpcal.country=country.id and approved=0 order by sdato"); - if(!$result) echo mysql_error(); - else { - while($row = mysql_fetch_array($result)) { - switch($row['tipo']) { - case 1: - $events[] = array($row['id'],$row['sdato'],$row['sdesc'],$row['ldesc'],$row['url'], $row['name'], $row['category']); - break; - case 2: - $events[] = array($row['id'],$row['sdato'].' to '.$row['edato'],$row['sdesc'],$row['ldesc'],$row['url'], $row['name'], $row['category']); - break; - case 3: - list($which,$day) = explode(':',$row['recur']); - $events[] = array($row['id'],'Every '.$re[(int)$which].' '.$days[$day],$row['sdesc'],$row['ldesc'],$row['url'], $row['name'], $row['category']); - break; - } - } - } - return $events; - } - - function load_month($year, $month) { - $result = mysql_query("select * from phpcal where (((MONTH(sdato)=$month or MONTH(edato)=$month) and (YEAR(sdato)=$year or YEAR(edato)=$year) and tipo<3) or tipo=3) and approved=1"); - if(!$result) echo mysql_error(); - else { - while($row = mysql_fetch_array($result)) { - switch($row['tipo']) { - case 1: - list(,,$dd) = explode('-',$row['sdato']); - $events[(int)$dd][] = $row; - break; - case 2: - list(,$mm,$dd) = explode('-',$row['sdato']); - list(,$m2,$d2) = explode('-',$row['edato']); - if((int)$mm==(int)$m2) { - for($i=(int)$dd; $i<=(int)$d2; $i++) { - $events[$i][] = $row; - } - } elseif((int)$mm==$month) { - for($i=(int)$dd; $i<32; $i++) { - $events[$i][] = $row; - } - } else { - for($i=1; $i<=(int)$d2; $i++) { - $events[$i][] = $row; - } - } - break; - case 3: - list($which,$dd) = explode(':',$row['recur']); - $ts = weekday($year,$month,$dd,$which); - $events[(int)strftime('%d',$ts)][] = $row; - break; - } - } - } - return($events); - } - - function start_month($year, $month) { - $ts = mktime(12,0,0,$month,1,$year); - return strftime('%w',$ts); - } - - function last_day($year,$month) { - $ts = mktime(12,0,0,$month+1,0,$year); - return strftime('%e',$ts); - } - - function months() { - static $months=NULL; - - if(!$months) for($i=1;$i<=12;$i++) { - $months[$i] = strftime('%B',mktime(12,0,0,$i,1)); - } - return $months; - } - - /* returns array of Days starting with 1 = Sunday */ - function days() { - static $days=NULL; - if(!$days) for($i=1;$i<=7;$i++) { - $days[$i] = strftime('%A',mktime(12,0,0,4,$i,2001)); - } - return $days; - } - - function draw_cal($y,$m) { - global $ev, $cm, $cy; - - $months = months(); - $m=(int)$m; - $month=$months[$m]; - $events = load_month($y,$m); - $pm=$m-1; - $nm=$m+1; - $py = $ny = $y; - if($m==1) { - $pm = 12; - $py = $y-1; - } else { - $pm = $m-1; - } - if($m==12) { - $nm = 1; - $ny = $y+1; - } - ?> -<table border=0 cellspacing=0 cellpadding=3 width=100%><tr bgcolor=#d0d0d0> -<th align=left> -<table width=100% border=0 -<tr><td align=left><a href="cal.php?cm=<?php echo $pm?>&cy=<?php echo $py?>"><?php echo $months[$pm].', '.$py?></a></td> -<td align=center><b><?php echo $month,', '.$y?></b></td> -<td align=right><a href="cal.php?cm=<?php echo $nm?>&cy=<?php echo $ny?>"><?php echo $months[$nm].', '.$ny?></a></td></tr></table></th> -</tr> -<tr bgcolor=#d0d0d0><td> -<table border=1 cellspacing=0 cellpadding=3 width=100%> -<?php - $days = days(); - $start = start_month($y,$m); - $last = last_day($y,$m); - if(($start==5 && $last>30) || ($start==6 && $last>29)) $rows=7; else $rows=6; - for($j=0;$j<$rows; $j++) { - echo '<tr>'; - for($i=1; $i<=7; $i++) { - if($j==0) echo '<th width=14.2% bgcolor=#a0a0a0>'.$days[$i]."</th>\n"; - else { - if($j==1 && ($i-1)==$start) $day = 1; - if($day && $day<=$last) { - $label=$day; - $col = '#f0f0f0'; - $bcol=$col; - $data = ' '; - if(is_array($events[$day])) { - $data = ''; - foreach($events[$day] as $row) { - if($data) $data .= "<br>\n"; - $data .= "<a href=\"cal.php?cm=$cm&cy=$cy&ev=".$row['id']."\">".$row['sdesc'].'</a>'; - } - } - } - else { $label=''; $data=''; $col='#d0d0d0'; $bcol=$col; } - echo "<td bgcolor=$bcol><table border=0 width=100% bgcolor=\"$col\" cellspacing=0><tr><td align=right>$label</td></tr>"; - echo "<tr><td>$data</td></tr></table></td>\n"; - } - if($day) $day++; - } - echo "</tr>\n"; - } +if (!$begun) { + commonHeader("Events: ".date("F Y", $date)); ?> -</table> -<?php if($ev) draw_event($ev) ?> +<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> +</div> <?php -echo "</table>\n"; } -function draw_event($ev) { - global $re; - $stmt = "select phpcal.*,country.name as cname from phpcal,country where phpcal.country = country.id and phpcal.id=$ev"; - $result = mysql_query($stmt); - if(!$result) echo mysql_error(); - else $event = mysql_fetch_array($result); - $days=days(); - $cat = $event['category']; - switch($cat) { - case 1: - $category = "regional"; - break; - case 2: - $category = "national"; - break; - case 3: - $category = "international"; - break; - default: - $category = "unknown"; - } -?> -<tr bgcolor=#d0d0d0><td> -<table border=0 cellspacing=0 cellpadding=3 width=100%> -<tr bgcolor=#a0a0a0><th><?php echo $event['sdesc']?></th> -<td bgcolor=#f0f0f0 rowspan=3 width=80%> -<?php +$events = load_events($date,1); -echo ini_get('magic_quotes_gpc') ? stripslashes($event['ldesc']) : $event['ldesc'] ; +if ($errors) display_errors($errors); -echo ((!$event['country']) || ($event['country'] != 'XXX')) ? "<br><b>Country</b>: ". $event['cname'] : "" ; +# 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); + +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), + '<td align="center" width="33%">', strftime('<b>%B, %Y</b></td>', $bom), + '<td align="right" width="33%"><a href="', $PHP_SELF, + strftime('?cm=%m&cy=%Y">%B, %Y</a></td>', $nm), + "</tr>\n</table>\n"; + +# begin the calendar +echo '<table id="cal" bgcolor="#f0f0f0" width="100%" border="1" cellspacing="0" cellpadding="3">', + "\n",'<tr bgcolor="#a0a0a0">',"\n"; +for ($i = 0; $i < 7; $i++) { + echo '<th width="14%">', date("l",mktime(0,0,1,4,$i+1,2001)), "</th>\n"; +} +echo "</tr>\n<tr>"; -echo ((!isset($category)) || ($category != "unknown")) ? "<br><b>Event Type</b>: ". $category : "" ; +# 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>'; +} -if(strlen($event['url'])) { - echo "<br><b>URL</b>: <a href=\"".$event['url']."\">".$event['url']."</a>\n"; +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>"; } -?> -</td></tr> -<tr bgcolor=#a0a0a0><th> -<?php switch($event['tipo']) { - case 1: - echo $event['sdato']; - break; - case 2: - echo $event['sdato'].' to '.$event['edato']; - break; - case 3: - list($which,$day) = explode(':',$event['recur']); - echo 'Every '.$re[(int)$which].' '.$days[$day]; - break; + +# generate the requisite number of blank days to wrap things up +for (; $days % 7; $days++) { + echo '<td bgcolor="#d0d0d0"> </td>'; +} +echo "</tr>\n</table>\n"; + +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 */ + + 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)); } -?> -</th></tr> -<tr bgcolor=#a0a0a0><th>Approved by: <?php echo $event['app_by']?></th></tr> -</table> -</td></tr> -<?php } - if (!isset($cm)) $cm = (int)strftime('%m'); - if (!isset($cy)) $cy = (int)strftime('%Y'); - if (!isset($cd)) $cd = (int)strftime('%d'); - if (!isset($nm)) $nm = 1; - - switch($format) { - case 'html': - echo "<h1>Event Calendar</h1>"; - draw_cal($cy,$cm); - echo "<br>"; - commonFooter(); - break; - - case 'csv': - while($nm) { - $entries = load_month($cy,$cm); - $last = last_day($cy,$cm); - for($i=$cd; $i<=$last; $i++) { - if(is_array($entries[$i])) foreach($entries[$i] as $row) { - if($data) $data.="\n"; - $data .= "$i,$cm,$cy,\"http://php.net/cal.php?cm=$cm&cy=$cy&ev=".$row['id']."\",\"".addslashes($row['sdesc']).'"'; - } - } - $nm--; - $cd = 1; - if($nm) { - $cm++; - if($cm==13) { $cy++; $cm=1; } - } - } - echo $data; - break; + +/* 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",'">', + 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 ($seen[$event['id']]++) continue; # only want each event once! + + 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; + } + 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), + ); +} + ?> Index: phpweb/index.php diff -u phpweb/index.php:1.201 phpweb/index.php:1.202 --- phpweb/index.php:1.201 Mon Mar 4 16:13:22 2002 +++ phpweb/index.php Mon Mar 4 21:54:18 2002 @@ -57,7 +57,7 @@ if($fp) { $cm=0; while(!feof($fp)) { - list($d,$m,$y,$url,$desc) = fgetcsv($fp,8192); + list($d,$m,$y,$url,$desc,$id) = fgetcsv($fp,8192); if($cm!=(int)$m) { if($cm) $RSIDEBAR_DATA.= "<br />\n"; else $RSIDEBAR_DATA.='<h3>Upcoming Events<br /> <a href="submit-event.php">[add event]</a></h3>'; @@ -66,7 +66,7 @@ unset($seen); } if(!$seen[$desc]) { - $RSIDEBAR_DATA .= "$d. <a href=\"$url\">$desc</a><br>\n"; + $RSIDEBAR_DATA .= "$d. <a href=\"cal.php?id=$id\">$desc</a><br>\n"; $seen[$desc] = true; } } Index: phpweb/style.css diff -u phpweb/style.css:1.26 phpweb/style.css:1.27 --- phpweb/style.css:1.26 Sun Mar 3 15:47:07 2002 +++ phpweb/style.css Mon Mar 4 21:54:19 2002 @@ -129,3 +129,26 @@ background: #eef; padding: 0px 8px; } + +table#cal { + font-size: smaller; +} + +table#cal a { + text-decoration: none; +} +table#cal a:hover { + text-decoration: underline; +} + +table#cal a.day { + float: right; + font-weight: bold; + color: #666; +} + +table#cal div.event { + margin-bottom: 2px; + padding-bottom: 2px; + border-bottom: 1px dashed #000; +} Index: phpweb/submit-event.php diff -u phpweb/submit-event.php:1.4 phpweb/submit-event.php:1.5 --- phpweb/submit-event.php:1.4 Sun Mar 3 21:59:37 2002 +++ phpweb/submit-event.php Mon Mar 4 21:54:19 2002 @@ -111,27 +111,10 @@ $re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); -if (isset($in)) {?> -<p><b>Preview:</b></p> -<table border="0" cellspacing="0" cellpadding="3" width="100%"> - <tr bgcolor="#dddddd"><td> - <b><?php echo date("F j, Y", mktime(0,0,1,$in['smonth'],$in['sday'],$in['syear']))?></b> -<?php - if ($in['url']) echo '<a href="', htmlentities($in['url']),'">'; - echo htmlentities($in['sdesc']); - if ($in['url']) echo "</a>"; - switch ($in['type']) { - case 'multi': - echo " ($in[syear]-$in[smonth]-$in[sday] to $in[eyear]-$in[emonth]-$in[eday])"; - break; - case 'recur': - echo " (Every ", $re[$in['recur']], " ", $days[$in['recur_day']], " of the month)"; - break; - } +if (isset($in)) { + echo "<p><b>Preview:</b></p>\n"; + display_event($in); ?> - </td></tr> - <tr bgcolor="#eeeeee"><td><?php echo $in['ldesc']?></td></tr> -</table> <p><b>Change:</b></p> <?php } Index: phpweb/include/layout.inc diff -u phpweb/include/layout.inc:1.75 phpweb/include/layout.inc:1.76 --- phpweb/include/layout.inc:1.75 Sun Mar 3 15:47:07 2002 +++ phpweb/include/layout.inc Mon Mar 4 21:54:19 2002 @@ -1,5 +1,5 @@ <?php -/* $Id: layout.inc,v 1.75 2002/03/03 20:47:07 jimw Exp $ */ +/* $Id: layout.inc,v 1.76 2002/03/05 02:54:19 jimw Exp $ */ # spacer() # print a IMG tag for a sized spacer GIF @@ -392,6 +392,43 @@ echo $errors[0]; } echo '</div>'; +} + +function display_event($event,$include_date=1) +{ + for ($i = 0; $i < 7; $i++) { + $days[$i+1] = strftime('%A',mktime(12,0,0,4,$i,2001)); + } + + $re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); + + $sday = $event['start'] ? strtotime($event['start']) + : mktime(0,0,1,$event['smonth'],$event['sday'],$event['syear']); + $eday = $event['end'] ? strtotime($event['end']) + : mktime(0,0,1,$event['emonth'],$event['eday'],$event['eyear']); +?> +<table border="0" cellspacing="0" cellpadding="3" width="100%"> + <tr bgcolor="#dddddd"><td> +<?php + if ($include_date && ($event['start'] || $event['smonth'])) echo "<b>", date("F j, Y", $sday), "</b>\n"; + if ($event['url']) echo '<a href="', htmlentities($event['url']),'">'; + echo "<b>", htmlentities($event['sdesc']), "</b>"; + if ($event['url']) echo "</a>"; + switch ($event['type']) { + case 2: + case 'multi': + echo " (", date("Y-m-d",$sday), " to ", date("Y-m-d",$eday), ")"; + break; + case 3: + case 'recur': + echo " (Every ", $re[$event['recur']], " ", $days[$event['recur_day']], " of the month)"; + break; + } +?> + </td></tr> + <tr bgcolor="#eeeeee"><td><?php echo $event['ldesc']?></td></tr> +</table> +<?php } ?>
Index: phpweb/cal.php diff -u phpweb/cal.php:1.30 phpweb/cal.php:1.31 --- phpweb/cal.php:1.30 Mon Mar 4 16:22:08 2002 +++ phpweb/cal.php Mon Mar 4 21:54:18 2002 @@ -1,331 +1,225 @@ <?php - include_once 'prepend.inc'; +require_once 'prepend.inc'; - // The calendar is hosted at php2.chek.com only! - if (!is_primary_site()) { - /* - if (is_backup_primary()) { - commonHeader("Service Unavailable");?> -<p>Sorry, the event calendar is temporarily unavailable.</p> -<?php - commonFooter(); - exit; +# 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; + +if ($id) { + if ($event = load_event($id)) { + commonHeader("Event: ".htmlentities($event['sdesc'])); + display_event($event, 0); + $begun++; + } + else { + $errors[] = "There is no event for specified id ('".htmlentities($id)."')"; + } +} +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).")."; } - */ - header("Location: http://php2.chek.com/cal.php"); - exit; } - if (!isset($format)) { - $format = 'html'; + else { + $errors[] = "The specified date (".htmlentities("$cy/$cm/$cd").") was not valid."; + unset($cm); unset($cd); unset($cy); } +} - if ($format == 'html') { - commonHeader("Event Calendar",1); +if ($cm && $cy && !checkdate($cm,1,$cy)) { + $errors[] = "The specified year and month (".htmlentities("$cy, $cm").") are not valid."; + unset($cm); unset($cy); +} - include_once 'cvs-auth.inc'; - if (isset($save) && isset($pw)) { # non-developers don't have $user set - setcookie("MAGIC_COOKIE",base64_encode("$user:$pw"),time()+3600*24*20,'/'); - } - if (isset($MAGIC_COOKIE) && !isset($user) && !isset($pw)) { - list($user,$pw) = explode(":", base64_decode($MAGIC_COOKIE)); - } - } +if (!$cm) $cm = date("m"); +if (!$cy) $cy = date("Y"); - @mysql_connect('localhost') or die('unable to connect to database'); - @mysql_select_db('php3'); +$date = mktime(0,0,1,$cm,1,$cy); - $re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); -/* -CREATE TABLE phpcal ( - id int(8) NOT NULL, - sdato date, - edato date, - recur char(12), - sdesc char(16) NOT NULL, - url varchar(128), - ldesc text, - tipo int(1) NOT NULL, - approved int(1) NOT NULL DEFAULT 0, - app_by char(16), - PRIMARY KEY(id), - INDEX (sdato), - INDEX (edato) -) -*/ - /* - * Find the first, second, third, last, second-last etc. weekday of a month - * - * args: day 1 = Monday - * which 1 = first - * 2 = second - * 3 = third - * -1 = last - * -2 = second-last - */ - function weekday($year, $month, $day, $which) { - $ts = mktime(12,0,0,$month+(($which>0)?0:1),($which>0)?1:0,$year); - $done = false; - $match = 0; - $inc = 3600*24; - while(!$done) { - if(strftime('%w',$ts)==$day-1) { - $match++; - } - if($match==abs($which)) $done=true; - else $ts += (($which>0)?1:-1)*$inc; - } - return $ts; - } - - function load_unapproved_events() { - global $re; - $days = days(); - $result = mysql_query("select phpcal.*, country.name as name from phpcal, country where phpcal.country=country.id and approved=0 order by sdato"); - if(!$result) echo mysql_error(); - else { - while($row = mysql_fetch_array($result)) { - switch($row['tipo']) { - case 1: - $events[] = array($row['id'],$row['sdato'],$row['sdesc'],$row['ldesc'],$row['url'], $row['name'], $row['category']); - break; - case 2: - $events[] = array($row['id'],$row['sdato'].' to '.$row['edato'],$row['sdesc'],$row['ldesc'],$row['url'], $row['name'], $row['category']); - break; - case 3: - list($which,$day) = explode(':',$row['recur']); - $events[] = array($row['id'],'Every '.$re[(int)$which].' '.$days[$day],$row['sdesc'],$row['ldesc'],$row['url'], $row['name'], $row['category']); - break; - } - } - } - return $events; - } - - function load_month($year, $month) { - $result = mysql_query("select * from phpcal where (((MONTH(sdato)=$month or MONTH(edato)=$month) and (YEAR(sdato)=$year or YEAR(edato)=$year) and tipo<3) or tipo=3) and approved=1"); - if(!$result) echo mysql_error(); - else { - while($row = mysql_fetch_array($result)) { - switch($row['tipo']) { - case 1: - list(,,$dd) = explode('-',$row['sdato']); - $events[(int)$dd][] = $row; - break; - case 2: - list(,$mm,$dd) = explode('-',$row['sdato']); - list(,$m2,$d2) = explode('-',$row['edato']); - if((int)$mm==(int)$m2) { - for($i=(int)$dd; $i<=(int)$d2; $i++) { - $events[$i][] = $row; - } - } elseif((int)$mm==$month) { - for($i=(int)$dd; $i<32; $i++) { - $events[$i][] = $row; - } - } else { - for($i=1; $i<=(int)$d2; $i++) { - $events[$i][] = $row; - } - } - break; - case 3: - list($which,$dd) = explode(':',$row['recur']); - $ts = weekday($year,$month,$dd,$which); - $events[(int)strftime('%d',$ts)][] = $row; - break; - } - } - } - return($events); - } - - function start_month($year, $month) { - $ts = mktime(12,0,0,$month,1,$year); - return strftime('%w',$ts); - } - - function last_day($year,$month) { - $ts = mktime(12,0,0,$month+1,0,$year); - return strftime('%e',$ts); - } - - function months() { - static $months=NULL; - - if(!$months) for($i=1;$i<=12;$i++) { - $months[$i] = strftime('%B',mktime(12,0,0,$i,1)); - } - return $months; - } - - /* returns array of Days starting with 1 = Sunday */ - function days() { - static $days=NULL; - if(!$days) for($i=1;$i<=7;$i++) { - $days[$i] = strftime('%A',mktime(12,0,0,4,$i,2001)); - } - return $days; - } - - function draw_cal($y,$m) { - global $ev, $cm, $cy; - - $months = months(); - $m=(int)$m; - $month=$months[$m]; - $events = load_month($y,$m); - $pm=$m-1; - $nm=$m+1; - $py = $ny = $y; - if($m==1) { - $pm = 12; - $py = $y-1; - } else { - $pm = $m-1; - } - if($m==12) { - $nm = 1; - $ny = $y+1; - } - ?> -<table border=0 cellspacing=0 cellpadding=3 width=100%><tr bgcolor=#d0d0d0> -<th align=left> -<table width=100% border=0 -<tr><td align=left><a href="cal.php?cm=<?php echo $pm?>&cy=<?php echo $py?>"><?php echo $months[$pm].', '.$py?></a></td> -<td align=center><b><?php echo $month,', '.$y?></b></td> -<td align=right><a href="cal.php?cm=<?php echo $nm?>&cy=<?php echo $ny?>"><?php echo $months[$nm].', '.$ny?></a></td></tr></table></th> -</tr> -<tr bgcolor=#d0d0d0><td> -<table border=1 cellspacing=0 cellpadding=3 width=100%> -<?php - $days = days(); - $start = start_month($y,$m); - $last = last_day($y,$m); - if(($start==5 && $last>30) || ($start==6 && $last>29)) $rows=7; else $rows=6; - for($j=0;$j<$rows; $j++) { - echo '<tr>'; - for($i=1; $i<=7; $i++) { - if($j==0) echo '<th width=14.2% bgcolor=#a0a0a0>'.$days[$i]."</th>\n"; - else { - if($j==1 && ($i-1)==$start) $day = 1; - if($day && $day<=$last) { - $label=$day; - $col = '#f0f0f0'; - $bcol=$col; - $data = ' '; - if(is_array($events[$day])) { - $data = ''; - foreach($events[$day] as $row) { - if($data) $data .= "<br>\n"; - $data .= "<a href=\"cal.php?cm=$cm&cy=$cy&ev=".$row['id']."\">".$row['sdesc'].'</a>'; - } - } - } - else { $label=''; $data=''; $col='#d0d0d0'; $bcol=$col; } - echo "<td bgcolor=$bcol><table border=0 width=100% bgcolor=\"$col\" cellspacing=0><tr><td align=right>$label</td></tr>"; - echo "<tr><td>$data</td></tr></table></td>\n"; - } - if($day) $day++; - } - echo "</tr>\n"; - } +if (!$begun) { + commonHeader("Events: ".date("F Y", $date)); ?> -</table> -<?php if($ev) draw_event($ev) ?> +<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> +</div> <?php -echo "</table>\n"; } -function draw_event($ev) { - global $re; - $stmt = "select phpcal.*,country.name as cname from phpcal,country where phpcal.country = country.id and phpcal.id=$ev"; - $result = mysql_query($stmt); - if(!$result) echo mysql_error(); - else $event = mysql_fetch_array($result); - $days=days(); - $cat = $event['category']; - switch($cat) { - case 1: - $category = "regional"; - break; - case 2: - $category = "national"; - break; - case 3: - $category = "international"; - break; - default: - $category = "unknown"; - } -?> -<tr bgcolor=#d0d0d0><td> -<table border=0 cellspacing=0 cellpadding=3 width=100%> -<tr bgcolor=#a0a0a0><th><?php echo $event['sdesc']?></th> -<td bgcolor=#f0f0f0 rowspan=3 width=80%> -<?php +$events = load_events($date,1); -echo ini_get('magic_quotes_gpc') ? stripslashes($event['ldesc']) : $event['ldesc'] ; +if ($errors) display_errors($errors); -echo ((!$event['country']) || ($event['country'] != 'XXX')) ? "<br><b>Country</b>: ". $event['cname'] : "" ; +# 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); + +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), + '<td align="center" width="33%">', strftime('<b>%B, %Y</b></td>', $bom), + '<td align="right" width="33%"><a href="', $PHP_SELF, + strftime('?cm=%m&cy=%Y">%B, %Y</a></td>', $nm), + "</tr>\n</table>\n"; + +# begin the calendar +echo '<table id="cal" bgcolor="#f0f0f0" width="100%" border="1" cellspacing="0" cellpadding="3">', + "\n",'<tr bgcolor="#a0a0a0">',"\n"; +for ($i = 0; $i < 7; $i++) { + echo '<th width="14%">', date("l",mktime(0,0,1,4,$i+1,2001)), "</th>\n"; +} +echo "</tr>\n<tr>"; -echo ((!isset($category)) || ($category != "unknown")) ? "<br><b>Event Type</b>: ". $category : "" ; +# 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>'; +} -if(strlen($event['url'])) { - echo "<br><b>URL</b>: <a href=\"".$event['url']."\">".$event['url']."</a>\n"; +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>"; } -?> -</td></tr> -<tr bgcolor=#a0a0a0><th> -<?php switch($event['tipo']) { - case 1: - echo $event['sdato']; - break; - case 2: - echo $event['sdato'].' to '.$event['edato']; - break; - case 3: - list($which,$day) = explode(':',$event['recur']); - echo 'Every '.$re[(int)$which].' '.$days[$day]; - break; + +# generate the requisite number of blank days to wrap things up +for (; $days % 7; $days++) { + echo '<td bgcolor="#d0d0d0"> </td>'; +} +echo "</tr>\n</table>\n"; + +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 */ + + 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)); } -?> -</th></tr> -<tr bgcolor=#a0a0a0><th>Approved by: <?php echo $event['app_by']?></th></tr> -</table> -</td></tr> -<?php } - if (!isset($cm)) $cm = (int)strftime('%m'); - if (!isset($cy)) $cy = (int)strftime('%Y'); - if (!isset($cd)) $cd = (int)strftime('%d'); - if (!isset($nm)) $nm = 1; - - switch($format) { - case 'html': - echo "<h1>Event Calendar</h1>"; - draw_cal($cy,$cm); - echo "<br>"; - commonFooter(); - break; - - case 'csv': - while($nm) { - $entries = load_month($cy,$cm); - $last = last_day($cy,$cm); - for($i=$cd; $i<=$last; $i++) { - if(is_array($entries[$i])) foreach($entries[$i] as $row) { - if($data) $data.="\n"; - $data .= "$i,$cm,$cy,\"http://php.net/cal.php?cm=$cm&cy=$cy&ev=".$row['id']."\",\"".addslashes($row['sdesc']).'"'; - } - } - $nm--; - $cd = 1; - if($nm) { - $cm++; - if($cm==13) { $cy++; $cm=1; } - } - } - echo $data; - break; + +/* 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",'">', + 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 ($seen[$event['id']]++) continue; # only want each event once! + + 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; + } + 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), + ); +} + ?> Index: phpweb/index.php diff -u phpweb/index.php:1.201 phpweb/index.php:1.202 --- phpweb/index.php:1.201 Mon Mar 4 16:13:22 2002 +++ phpweb/index.php Mon Mar 4 21:54:18 2002 @@ -57,7 +57,7 @@ if($fp) { $cm=0; while(!feof($fp)) { - list($d,$m,$y,$url,$desc) = fgetcsv($fp,8192); + list($d,$m,$y,$url,$desc,$id) = fgetcsv($fp,8192); if($cm!=(int)$m) { if($cm) $RSIDEBAR_DATA.= "<br />\n"; else $RSIDEBAR_DATA.='<h3>Upcoming Events<br /> <a href="submit-event.php">[add event]</a></h3>'; @@ -66,7 +66,7 @@ unset($seen); } if(!$seen[$desc]) { - $RSIDEBAR_DATA .= "$d. <a href=\"$url\">$desc</a><br>\n"; + $RSIDEBAR_DATA .= "$d. <a href=\"cal.php?id=$id\">$desc</a><br>\n"; $seen[$desc] = true; } } Index: phpweb/style.css diff -u phpweb/style.css:1.26 phpweb/style.css:1.27 --- phpweb/style.css:1.26 Sun Mar 3 15:47:07 2002 +++ phpweb/style.css Mon Mar 4 21:54:19 2002 @@ -129,3 +129,26 @@ background: #eef; padding: 0px 8px; } + +table#cal { + font-size: smaller; +} + +table#cal a { + text-decoration: none; +} +table#cal a:hover { + text-decoration: underline; +} + +table#cal a.day { + float: right; + font-weight: bold; + color: #666; +} + +table#cal div.event { + margin-bottom: 2px; + padding-bottom: 2px; + border-bottom: 1px dashed #000; +} Index: phpweb/submit-event.php diff -u phpweb/submit-event.php:1.4 phpweb/submit-event.php:1.5 --- phpweb/submit-event.php:1.4 Sun Mar 3 21:59:37 2002 +++ phpweb/submit-event.php Mon Mar 4 21:54:19 2002 @@ -111,27 +111,10 @@ $re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); -if (isset($in)) {?> -<p><b>Preview:</b></p> -<table border="0" cellspacing="0" cellpadding="3" width="100%"> - <tr bgcolor="#dddddd"><td> - <b><?php echo date("F j, Y", mktime(0,0,1,$in['smonth'],$in['sday'],$in['syear']))?></b> -<?php - if ($in['url']) echo '<a href="', htmlentities($in['url']),'">'; - echo htmlentities($in['sdesc']); - if ($in['url']) echo "</a>"; - switch ($in['type']) { - case 'multi': - echo " ($in[syear]-$in[smonth]-$in[sday] to $in[eyear]-$in[emonth]-$in[eday])"; - break; - case 'recur': - echo " (Every ", $re[$in['recur']], " ", $days[$in['recur_day']], " of the month)"; - break; - } +if (isset($in)) { + echo "<p><b>Preview:</b></p>\n"; + display_event($in); ?> - </td></tr> - <tr bgcolor="#eeeeee"><td><?php echo $in['ldesc']?></td></tr> -</table> <p><b>Change:</b></p> <?php } Index: phpweb/include/layout.inc diff -u phpweb/include/layout.inc:1.75 phpweb/include/layout.inc:1.76 --- phpweb/include/layout.inc:1.75 Sun Mar 3 15:47:07 2002 +++ phpweb/include/layout.inc Mon Mar 4 21:54:19 2002 @@ -1,5 +1,5 @@ <?php -/* $Id: layout.inc,v 1.75 2002/03/03 20:47:07 jimw Exp $ */ +/* $Id: layout.inc,v 1.76 2002/03/05 02:54:19 jimw Exp $ */ # spacer() # print a IMG tag for a sized spacer GIF @@ -392,6 +392,43 @@ echo $errors[0]; } echo '</div>'; +} + +function display_event($event,$include_date=1) +{ + for ($i = 0; $i < 7; $i++) { + $days[$i+1] = strftime('%A',mktime(12,0,0,4,$i,2001)); + } + + $re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); + + $sday = $event['start'] ? strtotime($event['start']) + : mktime(0,0,1,$event['smonth'],$event['sday'],$event['syear']); + $eday = $event['end'] ? strtotime($event['end']) + : mktime(0,0,1,$event['emonth'],$event['eday'],$event['eyear']); +?> +<table border="0" cellspacing="0" cellpadding="3" width="100%"> + <tr bgcolor="#dddddd"><td> +<?php + if ($include_date && ($event['start'] || $event['smonth'])) echo "<b>", date("F j, Y", $sday), "</b>\n"; + if ($event['url']) echo '<a href="', htmlentities($event['url']),'">'; + echo "<b>", htmlentities($event['sdesc']), "</b>"; + if ($event['url']) echo "</a>"; + switch ($event['type']) { + case 2: + case 'multi': + echo " (", date("Y-m-d",$sday), " to ", date("Y-m-d",$eday), ")"; + break; + case 3: + case 'recur': + echo " (Every ", $re[$event['recur']], " ", $days[$event['recur_day']], " of the month)"; + break; + } +?> + </td></tr> + <tr bgcolor="#eeeeee"><td><?php echo $event['ldesc']?></td></tr> +</table> +<?php } ?>