Location: PHPKode > projects > Myphpim > MyPhPim-01.05/includes/cal_cl.inc
<?php

//****************************************************************************************
//                Copyright (C) 2000 Koen de Boeve
//
//        This program is free software; you can redistribute it and/or
//        modify it under the terms of the GNU General Public License
//        as published by the Free Software Foundation; either version 2
//        of the License, or (at your option) any later version.
//
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
//
//        You should have received a copy of the GNU General Public License
//        along with this program; if not, write to the Free Software
//        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//        Version : MyPhPim-01.05
//        Author : Koen de Boeve
//        Contact: hide@address.com
//**************************************************************************************** 
if ( file_exists ( "conf" ) ) { $conf = "conf"; }
if ( file_exists ( "../conf" ) ) { $conf = "../conf"; }
include $conf . "/config.inc";

// Class for Calendar functionality

class CalendarCl {


//###################################################################

	// Return quantity of days in month with month_id $month_id 
	
	Function Cal_ReturnQuantDaysInMonth($month_id,$year) {
		// make sure we don't miss the leap-years :-)
        	$tmp = $year/4;
        	$tmpnew = round($tmp);
        	switch ($month_id) {
                	case "01" : 	$days = "31"; break;;
                	case "02" : 	if ($tmp == $tmpnew) {
                                		$days = "29";
                           		} else {
                                		$days = "28";
                          		}
                          		break;;
                	case "03" : 	$days = "31"; break;;
                	case "04" : 	$days = "30"; break;;
                	case "05" : 	$days = "31"; break;;
                	case "06" : 	$days = "30"; break;;
                	case "07" : 	$days = "31"; break;;
                	case "08" : 	$days = "31"; break;;
                	case "09" : 	$days = "30"; break;;
                	case "10" : 	$days = "31"; break;;
                	case "11" : 	$days = "30"; break;;
                	case "12" : 	$days = "31"; break;;
        	}
        	return $days;
	}

//###################################################################

	// Well perhaps you allready figured out what this does huh?

	Function Cal_ReturnMonthName ( $month_id ) {
                switch ($month_id) {
                         case "1" :     $monthname="Januari"; break;;
                         case "2" :     $monthname="februari"; break;;
                         case "3" :     $monthname="March"; break;;
                         case "4" :     $monthname="April"; break;;
                         case "5" :     $monthname="May"; break;;
                         case "6" :     $monthname="June"; break;;
                         case "7" :     $monthname="July"; break;;
                         case "8" :     $monthname="August"; break;;
                         case "9" :     $monthname="September"; break;;
                         case "10" :     $monthname="October"; break;;
                         case "11" :     $monthname="November"; break;;
                         case "12" :     $monthname="December"; break;;
               }
		return $monthname;
        }

//###################################################################

	// Little pigs little pigs Let me in...

	Function Cal_returndayofweek ( $date ) {
		// MySQL has some neat functions to retrieve
		// information about a certain date, so why not use it? :-)
		$db = new DB;
		$query = "SELECT DAYOFWEEK('$date') as weekday";
		$result = $db->query ( $query );
		$weekday = $db->result ( $result, 0, "weekday" );
		return $weekday;
	}

//###################################################################

	// Not by the hair of my chinny chin chin...

	Function Cal_ReturnDayName ( $date ) {
		$weekday = $this->Cal_returndayofweek ( $date );
		switch ( $weekday ) {
			case "01": $dayname = "Sunday"; break;;
			case "02": $dayname = "Monday";	break;;
			case "03": $dayname = "Tuesday"; break;;
			case "04": $dayname = "Wednesday"; break;;
			case "05": $dayname = "Thursday"; break;;
			case "06": $dayname = "Friday";	break;;
			case "07": $dayname = "Saturday"; break;;
		}
		return $dayname;
	}

//###################################################################

	// Return the date as a string f.i.: 2001-03-16 becomes "Friday 16 March 2001"

	Function Cal_ShowDateAsString ( $date ) {
		//split up the date with seperator "-"
		if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
                        $dayofmonth = $regs[3];
                        $month_id = $regs[2];
                        $year = $regs[1];
                } else {
                        echo "wrong date format!<p>\n";
                }
                $month = $this->Cal_ReturnMonthName ( $month_id );
                $day = $this->Cal_ReturnDayName ( $date );
		$string = $day . " " . $dayofmonth . " " . $month . " " . $year;
		return $string;
	}

//###################################################################

	// Make a small box to navigate easily through years and months.
	
	Function Cal_Showsmallyear ( $year, $month_id ) {
		GLOBAL $olddate;
		$nextyear = $year + 1;
		$prevyear = $year - 1;
		echo "<table bgcolor=darkseagreen border=0 cellpadding=2 cellspacing=0 width=90%>\n";
                echo "<tr bgcolor=black><td><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&month_id=$month_id&year=$prevyear><img src=pics/left.gif border=0></a></td><td colspan=1 align=center><font size=-1 color=white><b>$year</b></font></td><td align=right><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&month_id=$month_id&year=$nextyear><img src=pics/right.gif border=0></a></td></tr>";
		$m = array ( "01","02","03","04","05","06","07","08","09","10","11","12" );
		$count = 0;
		foreach ( $m as $month_id ) {
			if ( $count == 2 || $count == 5 || $count == 8 || $count == 11 ) {
				$month = $this->Cal_ReturnMonthName ( $month_id ); 
				echo "<td><font size=-2><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&monthview=1&month_id=$month_id&year=$year>$month</a></font></td></tr>";
			} else if ( $count == 0 || $count == 3 || $count == 6 ||  $count == 9 ) {
				$month = $this->Cal_ReturnMonthName ( $month_id ); 
				echo "<tr><td><font size=-2><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&monthview=1&month_id=$month_id&year=$year>$month</a></font></td>";
			} else {
				$month = $this->Cal_ReturnMonthName ( $month_id ); 
				echo "<td><font size=-2><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&monthview=1&month_id=$month_id&year=$year>$month</a></font></td>";
			}
			$count++;
		}		
				 
		echo "</table>";
	}	

//###################################################################
	
	// Show the calendar Items for this week
	
	Function Cal_Showsmallweek ( $date, $calid ) {
		// First lets find the starting point ( where does first week start? )
		 if ( ! $date ) {
			$date = date ('Y-m-d' );
		 }
		 if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
                        $dayofmonth = $regs[3];
                        $month_id = $regs[2];
                        $year = $regs[1];
                } else {
                        echo "wrong date format!<p>\n";
                }
		$db = new DB;
		// first we find out what the first day of $year is, so we can find the first day of that week more easily
		// for instance if 1 january 2001 is a monday, then the first week starts on sunday 31 December 2000 
		// yes , the first day of the week starts on sunday ! :p
		$start = $year . "-01-01"; 
		$result = $db->query ( "SELECT DAYOFWEEK('$start') AS firstday" );
		$first = $db->result ( $result, 0, "firstday" );
		$offset = $first - 1;
		if ( $offset == 0 ) {
			$fwstart = $start;
		} else {
			$prevy = $year - 1;
			$prevm = "12";
			$prevld = "31";
			$stday = $prevld - $offset + 1;
			$fwstart = $prevy . "-" . $prevm . "-" . $stday;
		}
		// lets see which daynr fwstart has...
		$result = $db->query ( "SELECT TO_DAYS('$fwstart') AS daynr" );
		$fwstartdaynr = $db->result ( $result, 0, "daynr" );
		// now lets find where first week ends...
		$result = $db->query ( "SELECT '$fwstart' + INTERVAL 6 DAY AS weekend" );
		$fwend = $db->result ( $result, 0, "weekend" );
		$result = $db->query ( "SELECT TO_DAYS('$fwend') AS daynr" );
		$fwenddaynr = $db->result ( $result, 0, "daynr" );
		

		// Now put all weeks in a startweek and endweek array 
		$weekstart[0] = "dummy";
		$weekend[0] = "dummy";
		$startnr[0] = "dummy";
		$stopnr[0] = "dummy";
		
		$weekstart[1] = $fwstart;
		$weekend[1] = $fwend;
		$startnr[1] = $fwstartdaynr;
		$stopnr[1] = $fwenddaynr;
		for ( $i = 2; $i < 53; $i++ ) {
			$prev = $i - 1;
			$result = $db->query ( "SELECT '$weekend[$prev]' + INTERVAL 1 DAY AS weekstart" );
			$weekstart[$i] = $db->result ( $result , 0, "weekstart" );
			$result = $db->query ( "SELECT TO_DAYS('$weekstart[$i]') AS daynr" ); 
			$startnr[$i] =  $db->result ( $result, 0, "daynr" );
			$result = $db->query ( "SELECT '$weekstart[$i]' + INTERVAL 6 DAY AS weekend" );
			$weekend[$i] = $db->result ( $result, 0, "weekend" );
			$result = $db->query ( "SELECT TO_DAYS('$weekend[$i]') AS daynr" );
                        $stopnr[$i] =  $db->result ( $result, 0, "daynr" );
		}
		// now lets find out in wich week $date is
		$result = $db->query ( "SELECT TO_DAYS('$date') AS daynr" );
                $daynr =  $db->result ( $result, 0, "daynr" );
		for ( $i = 1; $i < 53; $i++ ) {
			if ( $daynr <= $stopnr[$i] && $daynr >= $startnr[$i] ) {
				$weeknr = $i;
			} 
		}	
		$numcalids = count ( $calid );
		// Lets get the start and end of the week as strings
		$startdate = $this->Cal_ShowDateAsString ( $weekstart[$weeknr] ); 
		$enddate = $this->Cal_ShowDateAsString ( $weekend[$weeknr] ); 
		echo "<table border=0 bgcolor=dimgray width=100% cellpadding=0 cellspacing=0><tr><td align=center><font color=lightgrey><b>$startdate - $enddate</b></font></td></tr></table>\n"; 
		$thisday = $weekstart[$weeknr];
		$nextnr = $weeknr + 1;
		while ( $thisday != $weekstart[$nextnr] ) {
		$datestr = $this->Cal_ShowDateAsString ( $thisday );
		echo "<table border=0 bgcolor=lightgrey width=100% cellpadding=1 cellspacing=1><tr><td><font size=-1 color=dimgray><b>$datestr</b></font></td></tr>";
		echo "<tr bgcolor=white><td>";
		$l = 0;
		unset ( $vars );
		unset ( $entries );
                for ( $k = 0; $k < $numcalids; $k++ ) {
                	$repeat = $this->Cal_ReturnRepeatdays ( $calid[$k], $thisday );
                	if ( $repeat ) {
                	$vars[$l] = $this->Cal_EventSummary ( $calid[$k] );
                	$l++;
                	}
                } 
			$numvars = count ( $vars );
                	for ( $j = 0; $j < $numvars; $j++ ) {
                                $entries = $vars[$j];
                                $owner = $entries->owner;
                                $title = $entries->title;
                                $starttime = $entries->starttime;
                                if ( $starttime == 'no' ) {
                                        $starttime='00:00';
                                        $flag = 1;
                                } else { $flag = 0; }
                                $stoptime = $entries->stoptime;
                                if ( $flag == 0 ) {
                                        echo "<font size=-1><a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>$starttime - $stoptime </a>
						<a CLASS=blue href=calendar.php3?menu=detail&cal_id=$entries->cal_id><b>$title</b> </a>
						<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>[ $entries->owner ]</a></font>
						<font size=-2><br></font>";
                                } else {
                                        echo "<font size=-1><b><a CLASS=blue href=calendar.php3?menu=detail&cal_id=$entries->cal_id>$title</b></a> 
						<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>[ $entries->owner ]</a></font>
						<font size=-2><br></font>";
                                }
                	} 	
			$result = $db->query ( "SELECT '$thisday' + INTERVAL 1 DAY as nextday " );
			$thisday = $db->result ( $result , 0, "nextday" );
			echo"</td></tr>";
			echo "</table>";
		}
	}	

//###################################################################

	// this one makes a box where you can navigate through $month_id's days

	Function Cal_Showmsmallmonth ( $month_id , $year, $days) {
		GLOBAL $showweek, $user;
		$db = new DB;
		$owner_id = $db->UserId ( $user );
		$result = $db->query ( "SELECT cal_id FROM calendar where owner_id=$owner_id" );
		$numdates = $db->numrows ( $result );
		for ( $i = 0; $i < $numdates; $i++ ) {
			$ids[$i] = $db->result ( $result, $i, "cal_id" );
		}
		$numids = count ( $ids );
		if ( $showweek == "y" ) {
			$extrastr="showwk=y";
		} else {
			$extrastr="showd=y";
		}
		$today = date ( "Y-m-d" );
		$month = $this->Cal_ReturnMonthName ( $month_id );
		if ( $month_id == 12 ) {
			$nextmonth = "01";
			$nextyear = $year + 1;
			$prevmonth = $month_id - 1;
			$prevyear = $year;			
		} else if ( $month_id == "01" ) {
			$nextmonth = "02";
			$nextyear = $year;
			$prevmonth = "12";
			$prevyear = $year - 1;
		} else {
			$nextmonth = $month_id + 1;
			if ( $nextmonth < 10 ) {
				$nextmonth = "0" . $nextmonth;
			}
			$nextyear = $year;
			$prevmonth = $month_id - 1;
			if ( $prevmonth < 10 ) {
				$prevmonth = "0" . $prevmonth;
			}
			$prevyear = $year;
		}
		echo "<table bgcolor=darkseagreen border=0 cellpadding=2 cellspacing=0 width=90%>\n";
		echo "<tr bgcolor=black><td><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&month_id=$prevmonth&year=$prevyear><img src=pics/left.gif border=0></a></td><td colspan=5 align=center><font size=-1 color=white><b>$month $year</b></font></td><td align=right><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&month_id=$nextmonth&year=$nextyear><img src=pics/right.gif border=0></a></td></tr>";
		echo "<tr bgcolor=seagreen><td align=center><font size=-1 color=white>S</font></td><td align=center><font size=-1 color=white>M</font></td>
		      <td align=center><font size=-1 color=white>T</font></td><td align=center><font size=-1 color=white>W</font></td>
		      <td align=center><font size=-1 color=white>T</font></td><td align=center><font size=-1 color=white>F</font></td>
		      <td align=center><font size=-1 color=white>S</font></td></tr><tr>";
		$count = 1;
		for ( $i = 1; $i <= $days; $i++ ) {
			if ( $i < 10 ) {
				$date = $year . "-" . $month_id . "-" . "0" . $i;
			} else {
				$date = $year . "-" . $month_id . "-" . $i;
			}		
			$weekday = $this->Cal_returndayofweek ( $date );
			if ( $weekday == 1 && $i != 1 ) {
				echo "<tr>";
			}
			if ( $i == 1 ) {
				$k = 1;
				while ( $k != $weekday ) {
					echo "<td><font size=-1>&nbsp</font></td>";
					$count++;
					$k++;
				}
			}
			$fl = 0;
			for  ( $j = 0; $j < $numids; $j++ ) {
				$ret = $this->Cal_ReturnRepeatdays ( $ids[$j] , $date);	
				if ( $ret ) {
					$fl = 1;
				}
			}
			if ( $fl == 1 ) {
				$cl = "blue";
			} else {
				$cl = "black";
			}
			$fl = 0;
			if ( $today == $date ) {
				echo "<td align=center><font size=-1><a CLASS=red href=$GLOBALS[PHP_SELF]?menu=list&date=$date&keepday=y>$i</a></font></td>";
			} else {
				echo "<td align=center><font size=-1><a CLASS=$cl href=\"Javascript:onClick=parent.frames[2].location='calendar.php3?menu=main&date=$date'; onClick=parent.frames[3].location='calendar.php3?menu=list&date=$date&keepday=y';\">$i</a></font></td>";
			}
			if ( $weekday == 7 ) {
				echo "</tr>";
			}
			if ( $count == 7 ) {
				$count = 1;
			} else {
				$count++;
			}
		}
		for ( $j = $count ; $j < 8; $j++ ) {
			echo "<td><font size=-1>&nbsp</font></td>";
		}
		echo "</tr><tr><td></td></tr>";
		
		if ( $showweek == "y" ) {
			echo "<tr><td align=center colspan=7><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$date&showd=y><font size=-1>DAY VIEW</font></a></td></tr>";
		} else {
			echo "<tr><td align=center colspan=7><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$date&showwk=y><font size=-1>WEEK VIEW</font></a></td></tr>";
		}		
		echo "</table>";			
	}

//###################################################################

	// Get the cal_id's from user $user on date $date

	Function Cal_GetCal_ids ( $user, $date ) { 
		$db = new DB;
		$query = "SELECT user_id FROM users WHERE user='$user'";
                $result = $db->query ( $query );
                $user_id = $db->result ( $result, 0, "user_id" );
		// First get all events from this user 
		$query = "SELECT cal_id FROM calendar WHERE owner_id=$user_id";
		$result = $db->query ( $query );
		$numrows = $db->numrows ( $result );
		for ( $i = 0; $i < $numrows; $i++ ) {
			$calids[$i] = $db->result ( $result, $i, "cal_id" );
		}
		$keep = $numrows;
		// lets see who shared his events with this user
		$query = "SELECT cal_id FROM calendar_shares WHERE shareusers=$user_id";
		$result = $db->query ( $query );
		$numrows = $db->numrows ( $result );
		$numrows = $numrows + $keep;
		$j = 0;
		for ( $i = $keep; $i < $numrows; $i++ ) {
			$calids[$i] = $db->result ( $result, $j, "cal_id" );
			$j++;
		}
		$keep = $numrows;
		// Lets see to which groups this user belongs...
		$query = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
		$result = $db->query ( $query );
		$rows = $db->numrows ( $result );
		for ( $i = 0; $i < $rows; $i++ ) {
			$groupids[$i] = $db->result ( $result , $i, "group_id" );
		}
		// Now we build a query to fetch the cal_ids shared with groups to whom this user belongs.

		$countgroups = count ( $groupids );
		if ( $countgroups > 0 ) {
			$query = "SELECT cal_id FROM calendar_shares WHERE sharegroups=";
			for ( $i = 0; $i < $countgroups; $i++ ) {
				if ( $i == 0 ) {
					$query = $query . $groupids[$i];
				} else {
					$query = $query . " OR sharegroups=$groupids[$i]";
				}
			}
			$result = $db->query ( $query );
			$numrows = $db->numrows ( $result );
			$numrows = $numrows + $keep;
			$j = 0;
			for ( $i = $keep; $i < $numrows; $i++ ) {
				$calids[$i] = $db->result ( $result, $j, "cal_id" );
				$j++;
			}
		}
		return $calids;
	}

//###################################################################

	// Return a few fields from an entry and return them as an object for later use
	// this is used for displaying a short description about the event

	Function Cal_EventSummary ( $cal_id ) {
		$db = new DB;
		$query = "SELECT owner_id, title, starttime, stoptime, date FROM calendar WHERE cal_id=$cal_id";
		$result = $db->query ( $query );
		$owner_id = $db->result ( $result, 0, "owner_id" );
		$var->title = $db->result ( $result, 0, "title" );
		$var->starttime = $db->result ( $result, 0, "starttime" );
		$var->stoptime = $db->result ( $result, 0, "stoptime" );
		$var->date = $db->result ( $result, 0, "date" );
		$var->owner = $db->UserName ( $owner_id );
		$var->cal_id = $cal_id;
		return $var;
	}

//###################################################################

	// We need to sort the events by starting hour
	
	Function Cal_SortByStartTime ( $calids ) {
		$db = new DB;
		$numcalids = count ( $calids );
		if ( $calids[0] ) {
			$query = "SELECT cal_id FROM calendar WHERE cal_id=$calids[0] ";
		} else {
			$query = "SELECT cal_id FROM calendar WHERE cal_id=NULL ";
		}	
		for ( $i = 1; $i < $numcalids; $i++ ) {
			$query = $query . "OR cal_id=$calids[$i] ";
		}
		$query = $query . "ORDER BY starttime";
		$result = $db->query ( $query );
		$numrows = $db->numrows ( $result );
		for ( $i = 0; $i < $numrows; $i++ ) {
			$value[$i] = $db->result ( $result, $i, "cal_id" );
		}
		return $value;
	} 

//###################################################################

	// start time and stop time , etc.. are stored as HH:MM in the database
	// Here we split that time and return the 2 as an array

	Function Cal_SplitTime ( $time ) {
		$arr = split ( ':', $time );
		$t->hour = $arr[0];
		$t->min = $arr[1];
		return $t;
	}

//###################################################################

	// Print a Month view of the calendar

	Function Cal_PrintByMonth ( $vars ) {
		$numvars = count ( $vars );
		for ( $j = 0; $j < $numvars; $j++ ) {
                                $entries = $vars[$j];
                                $owner = $entries->owner;
                                $title = $entries->title;
                                $starttime = $entries->starttime;
                                if ( $starttime == 'no' || $starttime == '' ) {
                                        $starttime='00:00';
                                        $flag = 1;
                                } else { $flag = 0; }
                                $stoptime = $entries->stoptime;
                                if ( $flag == 0 ) {
					echo "<font size=-2><a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>$starttime - $stoptime</a> 
						<a CLASS=blue href=calendar.php3?menu=detail&cal_id=$entries->cal_id><b>$title</b></a> 
						<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>[ $entries->owner ]</b></font>
						<font size=-2><br></font>";
                                } else {
                                	echo "<font size=-2><a CLASS=blue href=calendar.php3?menu=detail&cal_id=$entries->cal_id><b>$title</b></a> 
						<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>[ $entries->owner ]</a></font>
						<font size=-2><br></font>";
                                }
 		}
	}

//###################################################################


	// Print An overview for 1 day

	Function Cal_PrintDayView ( $vars ) {
		$numvars = count ( $vars );
		echo "<table border=0 bgcolor=white width=100% cellspacing=1 cellpadding=0>\n";
		for ( $i = 0; $i < 24; $i++ ) {
			if ( $i < 10 ) {
				$hour = "0" . $i;
			} else {
				$hour = $i;
			} 
			$thishour = $hour;
			$nexthour = $i + 1;
			if ( $i < 10 ) {
				$nexthour = "0" . $nexthour;
			} 
			echo "<tr><td width=5%><font color=blue ><b>$hour 00 </b></font></td>";
			for ( $j = 0; $j < $numvars; $j++ ) {
				$entries = $vars[$j];
				$owner = $entries->owner;
				$title = $entries->title;
				$starttime = $entries->starttime;
				if ( $starttime == 'no' || $starttime == '') {
					$starttime='00:00';
					$flag = 1;
				} else { $flag = 0; }
				$stoptime = $entries->stoptime;
				$start = $this->Cal_SplitTime ( $starttime );				
				$stop = $this->Cal_SplitTime ( $stoptime );
				if ( $start->hour >= $thishour && $start->hour < $nexthour && $starttime != 'no') { 
					$stoptmp = $stop->hour;
					$starttmp = $start->hour;
					$span = $stoptmp - $starttmp;
					if ( $flag == 0 ) {
						echo "<td bgcolor=lightgrey rowspan=$span valign=top><font size=-1>
							<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>
							$start->hour:$start->min - $stop->hour:$stop->min</a>
							<a CLASS=blue href=calendar.php3?menu=detail&cal_id=$entries->cal_id><b>$title</b></a> 
							<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>[ $entries->owner ]</a>
							</font></td>";
					} else {
						echo "<td bgcolor=lightgrey rowspan=$span valign=top><font size=-1>
							<a CLASS=blue href=calendar.php3?menu=detail&cal_id=$entries->cal_id><b>$title</b></a> 
							<a CLASS=black href=calendar.php3?menu=detail&cal_id=$entries->cal_id>[ $entries->owner ]</a>
							</font></td>";
					}
						
				} 
			}
			echo "</tr>";
		}
		echo "</table>";
	}

//###################################################################

	// yabadabadoooooo

	Function Cal_Splitdate ( $date ) {
		ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs);
		return $regs;
	}

//###################################################################
	
	// A bit of a misleading function name here.. 
	// this only shows a grey bar with the date as string and 2 navigation buttons to scroll through the dates.

	Function Cal_ShowDayview ( $user, $date ) {
		$db = new DB;
		$today = date ( "Y-m-d" );
		if ( ! $date ) {
			$date = $today;
		}
		if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
			$dayofmonth = $regs[3];
			$month_id = $regs[2];
			$year = $regs[1];
		} else {
			echo "wrong date format!<p>\n";
		}
		$month = $this->Cal_ReturnMonthName ( $month_id );
		$day = $this->Cal_ReturnDayName ( $date );
		$cal_ids = $this->Cal_GetCal_ids ( $user, $date );
		$result = $db->query ( " SELECT '$date' - INTERVAL 1 DAY as yesterday " );
		$yesterday = $db->result ( $result, 0, "yesterday" );
		$result = $db->query ( " SELECT '$date' + INTERVAL 1 DAY as tomorrow " );
		$tomorrow = $db->result ( $result, 0, "tomorrow" );
		echo "<table border=0 bgcolor=dimgray width=100% cellpadding=0 cellspacing=0><tr><td align=center><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$yesterday&keepday=y><img src=pics/left.gif border=0></a><font color=lightgrey><b>$day $dayofmonth, $month $year</b></font><a CLASS=blue href=$GLOBALS[PHP_SELF]?menu=list&date=$tomorrow&keepday=y><img src=pics/right.gif border=0></a></td></tr></table><table><tr>\n";
		echo "</tr></table>\n";
	}

//###################################################################

	// return the daynr counted from start of day 0 (epoch )
	// for easier calculation of dates
	Function Cal_DayNr ( $date ) {
		$db = new DB;
		$query = "SELECT TO_DAYS( '$date' ) AS daynr";
		$result = $db->query ( $query );
		$daynr = $db->result ( $result, 0, "daynr" );
		return $daynr;
	}	

//###################################################################
	

	// This Function returns the next date according to a supplied date and interval
	// e.g. : '2000-12-01' , 2 , MONTH returns newdate: '2001-02-01'
	Function Cal_ReturnInterval ( $date, $interval, $type ) {
		if ( $type == "WEEK" ) {
			$interval = $interval * 7;
			$type = "DAY";
		}
		$db = new DB;
		$query = "SELECT DATE_ADD( '$date', INTERVAL $interval $type ) AS newdate";
		$result = $db->query ( $query );
		$newdate = $db->result ( $result, 0, "newdate" );
		return $newdate;
	}



//###################################################################

	// Get Events For user on date $date in a specific period
	Function Cal_GetEvents ( $user, $date, $time1, $time2 ) {
		$db = new DB;
		$query = "SELECT user_id FROM users WHERE user='$user'";
		$result = $db->query ( $query );
		$user_id = $db->result ( $result, 0, "user_id" );
		$query = "SELECT * FROM calendar WHERE owner_id=$user_id AND date='$date' AND starttime >= $time1 AND starttime <= $time2 ORDER BY starttime";
		$result = $db->query ( $query );
		$numrows = $db->numrows ( $result );
		for ( $i = 0; $i < $numrows; $i++ ) {
			$tmp->cal_id 		= $db->result ( $result, $i, "cal_id" );
			$tmp->owner_id 		= $db->result ( $result, $i, "owner_id" );
			$tmp->title 		= $db->result ( $result, $i, "title" );
			$tmp->note		= $db->result ( $result, $i, "note" );
			$tmp->date		= $db->result ( $result, $i, "date" );
			$tmp->starttime 	= $db->result ( $result, $i, "starttime" );		
			$tmp->stoptime		= $db->result ( $result, $i, "stoptime" );		
			$tmp->repeat		= $db->result ( $result, $i, "repeat" );		
			$tmp->interv 		= $db->result ( $result, $i, "interv" );		
			$tmp->repeattype 	= $db->result ( $result, $i, "repeattype" );		
			$tmp->until 		= $db->result ( $result, $i, "until" );		
			$tmp->lastupdate 	= $db->result ( $result, $i, "lastupdate" );		
			$tmp->updateby 		= $db->result ( $result, $i, "updateby" );
			$entry[$i] = $tmp;
		}
		return $entry;
	}		
	


//###################################################################

	// See if date $date is a repeated event from reference 

	Function Cal_IsThisARepeat ( $date, $reference, $interval, $type, $enddate ) {
	 		
		$nowday = $this->Cal_DayNr ( $date ); 
		$stopday = $this->Cal_DayNr ( $enddate );
		if ( $interval == 0 || $interval == "" || !interval ) {
			return FALSE;
		} elseif ( $nowday > $stopday && $enddate && $enddate != "0000-00-00" ) {
			return FALSE;
		} else {
			switch ( $type ) {
				case "DAY" :
						$refday = $this->Cal_DayNr ( $reference );
						$day = $this->Cal_DayNr ( $date ); 
						$tmp = $day - $refday ;
						if ( $tmp < 0 ) {
							return FALSE;
						}
						$sum = $tmp / $interval;
						$round = round ( $sum );
						if ( $sum == $round ) {
							return TRUE;
						} else {
							return FALSE;
						}
						break;;
				case "WEEK" :
						$interval = $interval * 7;
						$refday = $this->Cal_DayNr ( $reference );
                               		        $day = $this->Cal_DayNr ( $date );
                                       		$tmp = $day - $refday ;
                                        	if ( $tmp < 0 ) {
                                                	return FALSE;
                                        	}
                                        	$sum = $tmp / $interval;
                                        	$round = round ( $sum );
                                        	if ( $sum == $round ) {
                                                	return TRUE;
                                        	} else {
                                                	return FALSE;
                                        	}
                                        	break;; 
				case "YEAR" :
						$date_arr = $this->Cal_Splitdate( $date );
						$ref_arr = $this->Cal_Splitdate( $reference );
						$ref_year = $ref_arr[1];
						$date_year = $date_arr[1];
						$tmp = $date_year - $ref_year;
						if ( $tmp < 0 ) {
                                                	return FALSE;
                                        	}
						$sum = $tmp / $interval;
                                        	$round = round ( $sum );
						if ( $sum == $round ) {
					 		$refmd = $ref_arr[2] . "-" . $ref_arr[3];
							$newmd = $date_arr[2] . "-" . $date_arr[3];
							if ( $refmd == $newmd ) {	
                                                		return TRUE;
							} else {
								return FALSE;
							}
                                        	} else {
                                                	return FALSE;
                                        	}
                                        	break;;
				case "MONTH" :
						$date_arr = $this->Cal_Splitdate( $date );
						$ref_arr = $this->Cal_Splitdate( $reference );
						$refmonth = $ref_arr[2];
						$refyear = $ref_arr[1];
						$month = $date_arr[2];
						$year = $date_arr[1];
						$tmpref = $refyear . "-" . $refmonth . "-01";
						$refday = $this->Cal_DayNr ( $tmpref );
						$tmpday = $year . "-" . $month . "-01";
						$tmp = $interval;
						$day = $this->Cal_DayNr ( $tmpday );
						while ( $day > $refday ) {
							if ( $interval < $month ) {
								$month = $month - $interval;
							} else {
								while ( $tmp > 12 ) {
									$tmp = $tmp - 12;
									$year--;
								}
								if ( $month < $tmp ) {
									$tmp = $tmp - $month;
									$month = 12 - $tmp;
									$year--;
								} elseif ( $month == $tmp ) {
									$month = 12;
									$year--;
								} else {
									$month = $month - $tmp + 1;
								}
							}
							if ( $month < 10 ) {
								$month = "0" . $month;
							}
							if ( $month == "00" ) {
								$month = "12";
							}
							$tmpday = $year . "-" . $month . "-01";
							$day = $this->Cal_DayNr ( $tmpday );
							$tmp = $interval;
						}
						$newdate = $year . "-" . $month . "-" . $date_arr[3];
						if ( $newdate == $reference ) {
							return TRUE;
						} else {
							return FALSE;
						}
						break;;
						
			}
		}

	}

//###################################################################

	// Get All Dates on which an event should take place 

	Function Cal_ReturnRepeatdays ( $cal_id , $date) {
		$db = new DB; 
		$query ="SELECT date, until, interv, repeattype FROM calendar WHERE cal_id=$cal_id";
		$result = $db->query ( $query );
		$reference = $db->result ( $result, 0, "date" );
		$enddate = $db->result ( $result, 0, "until" );
		$interval = $db->result ( $result, 0, "interv" );
		$type = $db->result ( $result, 0, "repeattype" );
		$answer = $this->Cal_IsThisARepeat ( $date, $reference, $interval, $type , $enddate);
		if ( $answer ) {
			return TRUE;
		} elseif ( $date == $reference ) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

		
}


?>
Return current item: Myphpim