cvs: php-master-web /fetch events.php

From: Date: Sat, 03 May 2003 13:45:06 +0000
Subject: cvs: php-master-web /fetch events.php
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-17246@lists.php.net to get a copy of this message
goba Sat May 3 09:45:06 2003 EDT Modified files: /php-master-web/fetch events.php Log: Reformatting code to be closer to PEAR guidelines (and to readablity) Also modify the CVS outputting code to output the country code instead of the country name which simplifies the SQL query too. I have not found any code in phpweb using the country information, but now we need the country code to let the user see events in his country highlighted

Index: php-master-web/fetch/events.php diff -u php-master-web/fetch/events.php:1.5 php-master-web/fetch/events.php:1.6 --- php-master-web/fetch/events.php:1.5 Sun Apr 21 13:58:50 2002 +++ php-master-web/fetch/events.php Sat May 3 09:45:06 2003 @@ -7,33 +7,50 @@ @mysql_pconnect('localhost','nobody','') or exit; @mysql_select_db('php3') or exit; -$re = array(1=>'First',2=>'Second',3=>'Third',4=>'Fourth',-1=>'Last',-2=>'2nd Last',-3=>'3rd Last'); - - -if(!isset($a)) { - if(isset($HTTP_GET_VARS['a'])) $a = $HTTP_GET_VARS['a']; - else $a=0; +// Recurring possibilities +$re = array( + 1 => 'First', + 2 => 'Second', + 3 => 'Third', + 4 => 'Fourth', + -1 => 'Last', + -2 => '2nd Last', + -3 => '3rd Last' +); + +if (!isset($a)) { + if (isset($HTTP_GET_VARS['a'])) { $a = $HTTP_GET_VARS['a']; } + else { $a = 0; } } +// Set default values 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 = 3; +// Collect events for $nm number of months 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) { - echo "$i,$cm,$cy,".'"'.$row['cname'].'","'.addslashes($row['sdesc']).'",'.$row['id'].',"'.base64_encode($row['ldesc']).'","'.$row['url'].'",'.$row['recur'].','.$row['tipo'].','.$row['sdato'].','.$row['edato']."\n"; - } - } - $nm--; - $cd = 1; - if($nm) { - $cm++; - if($cm==13) { $cy++; $cm=1; } - } + $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) { + echo "$i,$cm,$cy," . '"' . $row['country'].'","' . + addslashes($row['sdesc']) . '",' . + $row['id'] . ',"' . base64_encode($row['ldesc']) . '","' . + $row['url'] . '",' . $row['recur'] . ',' . + $row['tipo'] . ',' . $row['sdato'] . ',' . + $row['edato'] . "\n"; + } + } + } + $nm--; + $cd = 1; + if ($nm) { + $cm++; + if ($cm == 13) { $cy++; $cm = 1; } + } } /* @@ -46,83 +63,121 @@ * -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_month($year, $month) { - $result = mysql_query("SELECT phpcal.*,country.name AS cname FROM phpcal LEFT JOIN country ON phpcal.country = country.id 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, MYSQL_ASSOC)) { - 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 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; +} + +// Get events for one month in one year to be listed +function load_month($year, $month) +{ + // Empty events array + $events = array(); + + // Get approved events starting or ending in the + // specified year/month, and all recurring events + $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" + ); + + // Cannot get results, return with event's not found + if (!$result) { echo mysql_error(); return array(); } + + // Go through found events + while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { + + switch($row['tipo']) { + + // One day event + case 1: + list(, , $dd) = explode('-', $row['sdato']); + $events[(int)$dd][] = $row; + break; + + // Multiple-day event + 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; + + // Recurring event + case 3: + list($which,$dd) = explode(':',$row['recur']); + $ts = weekday($year,$month,$dd,$which); + $events[(int)strftime('%d',$ts)][] = $row; + break; + } + } + + // Return events found + return($events); +} + +// Return the first day of a month +function start_month($year, $month) +{ + $ts = mktime(12, 0, 0, $month, 1, $year); + return strftime('%w', $ts); +} + +// Return the last day of a month +function last_day($year, $month) +{ + $ts = mktime(12, 0, 0, $month+1, 0, $year); + return strftime('%e', $ts); +} + +// Return array of months +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; }
« previous php.mirrors (#17246) next »