Location: PHPKode > projects > ACollab > include/lib/calendar.class.php
<?php 
/****************************************************************************************/
/* ACollab                                                                              */
/****************************************************************************************/
/* Copyright (c) 2002-2004  Adaptive Technology Resource Centre / University of Toronto */
/*                                                                                      */
/* http://atutor.ca/acollab                                                             */
/*                                                                                      */
/* This program is free software. You may 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 may access the GNU General Public License at:                                    */
/* http://www.opensource.org/licenses/gpl-license.php                                   */
/*                                                                                      */
/* You may contact the Adaptive Technology Resource Centre at                           */
/* Robarts Library, University of Toronto                                               */
/* 130 St. George Street, Toronto, Ontario, Canada M5S 1A5                              */
/* Further contact information is available at http://www.utoronto.ca/atrc/             */
/****************************************************************************************/
/* Programmer:                                                                          */
/* Joel Kronenberg - ATRC                                                               */
/****************************************************************************************/

if (!defined('AC_INCLUDE_PATH')) { exit; }

class Calendar {
	// today's real date
    var $current_year, $current_month, $current_day;

	// displayed date
	var $display_year, $display_month;

	// states:
	var $show_events, $show_entries, $show_mini;

	// intial:
	var $firstday;

	var $page_url;
	var $db;

	var $events_result, $event;
	var $entries_result, $entry;

	var $event_bullet_start = '<br /><small>+ ';
	var $event_bullet_end = '</small>';
	var $bullet_start = '<br /><small>&#149; ';
	var $bullet_end = '</small>';

	var $where_sql = '';

	/************************/
	/* store all the info!	*/
	var $events;
	var $entries;

	var $start_only; // to show all multiple date events or just starting date.

	/************************/
	/* constructor			*/
	function Calendar($year = '', $month = '') {
		$this->current_year = date('Y', time());
		$this->current_month = date('m', time());
		$this->current_day = date('j', time());

		if ($year == '') {
			$year = date('Y');
		}
		if ($month == '') {
			$month = date('m');
		}
		$this->display_year = $year;
		$this->display_month = $month;

		$this->show_events = TRUE;
		$this->show_entries = FALSE;

		// get the first column
		$this->firstday = date('w', mktime(0,0,0,$this->display_month,1,$this->display_year));
		$this->page_url = $_SERVER['PHP_SELF'];
		$this->show_mini= FALSE;
	}

	/********************/
	/* SetDB			*/
	function SetDB($db) {
		$this->db = $db;
	}

	function startOnly($value){
		if ($value === false){
			$this->start_only = 0;
		} else {
			$this->start_only = true;
		}
	}

	/************************/
	/* SetYearMonth			*/
	function SetYearMonth($year, $month) {
		$this->display_year = $year;
		$this->display_month = $month;
		$this->firstday = date('w', mktime(0,0,0,$this->display_month,1,$this->display_year));
	}

	/************************/
	/* PrintMiniCalendar	*/
	/* front end for the PrintCalendar() method	*/
	function PrintMiniCalendar(){
		echo '<small><a href="events/index.php?month='.$this->display_month.SEP.'year='.$this->display_year.'" onfocus="this.className=\'highlight\'" onblur="this.className=\'\'">';
		echo date('F', mktime(0,0,0,$this->display_month,1,$this->display_year));
		echo " $this->display_year</a></small>";

		$this->show_mini = true;
		$this->PrintCalendar();
		$this->show_mini = false;
	}

	/************************/
	/* PrintHeader			*/
	/* private				*/
	function PrintHeader(){
		if ($this->show_mini == true){
			echo '<table width="100" cellpadding="0" cellspacing="2" border="0" bgcolor="#B0DAF8">';
			echo '<tr>
					<th width="14%" class="box" scope="col"><small>S</small></th>
					<th width="14%" class="box" scope="col"><small>M</small></th>
					<th width="14%" class="box" scope="col"><small>T</small></th>
					<th width="14%" class="box" scope="col"><small>W</small></th>
					<th width="14%" class="box" scope="col"><small>T</small></th>
					<th width="14%" class="box" scope="col"><small>F</small></th>
					<th width="14%" class="box" scope="col"><small>S</small></th>
				  </tr>
				  <tr>';
		} else {
			echo '<table width="600" cellpadding="2" cellspacing="0" border="0" class="box2" align="center">';
			echo '<tr>
					<th width="14%" class="box" scope="col">'._AC('date_sun').'</th>
					<th width="14%" class="box" scope="col">'._AC('date_mon').'</th>
					<th width="14%" class="box" scope="col">'._AC('date_tue').'</th>
					<th width="14%" class="box" scope="col">'._AC('date_wed').'</th>
					<th width="14%" class="box" scope="col">'._AC('date_thu').'</th>
					<th width="14%" class="box" scope="col">'._AC('date_fri').'</th>
					<th width="14%" class="box" scope="col">'._AC('date_sat').'</th>
				  </tr>
				  <tr>';
		}
	} // end PrintHeader()


	/************************/
	/* PrintCalendar		*/
	function PrintCalendar() {
		if ($this->show_events == true) {
			if (!$this->db){
				echo 'The db connection is not set. Use the SetDB($db) method, with an initialized $db handler.';
				exit;
			}
			if ($this->start_only == false) {
				$lastday = 28; // we have atleast 28 days.
				while (checkdate($this->display_month,($lastday+1),$this->display_year)) {
				    $lastday++;
				}

				$sql = "SELECT 
					event_id, 
					start_date, 
					end_date, 
					title, 
					TO_DAYS(start_date) AS s_days, 
					TO_DAYS(end_date) AS e_days, 
					TO_DAYS('$this->display_year-$this->display_month-01') AS n_days 
				FROM ".TABLE_PREFIX."events WHERE 
				(
						(
							TO_DAYS(start_date)>=TO_DAYS('$this->display_year-$this->display_month-01') 
						AND
							TO_DAYS(start_date)<=TO_DAYS('$this->display_year-$this->display_month-$lastday')
						)
					OR
						(
							TO_DAYS(start_date) < TO_DAYS('$this->display_year-$this->display_month-01')
						AND
							TO_DAYS(end_date) > TO_DAYS('$this->display_year-$this->display_month-01')
						)
				)
				AND group_id=$_SESSION[group_id] 
				ORDER BY title";

				$result2 = mysql_query($sql,$this->db);

				while($row = mysql_fetch_assoc($result2)) {
					if (($row['start_date'] == $row['end_date']) || ($row['end_date'] == '0000-00-00 00:00:00')) {
						$dd	= substr($row['start_date'], 8, 2);
						$this->events[(int)$dd][] = $row;
					} else {
						list(,$mm,$dd) = explode('-',$row['start_date']);
						list(,$m2,$d2) = explode('-',$row['end_date']);

						if ((int)$mm == (int)$m2) {
							for($i=(int)$dd; $i<=(int)$d2; $i++) {
								$this->events[$i][] = $row;
							}
						} else if ($row['s_days'] < $row['n_days']) {
							/* this event started before this month */
							if ($m2 == $this->display_month) {
								/* this event ends this month */
								/* 1 till end day */
								for($i = 1; $i<=$d2; $i++) {
									$this->events[$i][] = $row;
								}
							} else {
								/* this event doesn't end in this month */
								/* 1 till end of month */
								for($i=1; $i<32; $i++) {
									$this->events[$i][] = $row;
								}
							}
						} else if ((int)$m2 > (int)$mm) {
							/* this event ends after this month */
							for($i = (int)$dd; $i<32; $i++) {
								$this->events[$i][] = $row;
							}
						}
						/*else if ((int)$mm == $this->display_month) {
							for($i=(int)$dd; $i<32; $i++) {
								$this->events[$i][] = $row;
							}
						} else {
							debug('xx');
							for($i=1; $i<=(int)$d2; $i++) {
								$this->events[$i][] = $row;
							}    
						}
						*/
		 
					} // end if
				}
			} else {
				$result2 = mysql_query("SELECT * FROM ".TABLE_PREFIX."events WHERE (MONTH(start_date)=$this->display_month) AND (YEAR(start_date)=$this->display_year) AND approved='y' $this->where_sql",$this->db);
				while($row = mysql_fetch_array($result2)) {
					list(,,$dd) = explode('-',$row[start_date]);
					$this->events[(int)$dd][] = $row;
				}
			}
		}
		
		if ($this->show_entries == true) {

			if (!$this->db){
				echo 'The db connection is not set. Use the SetDB($db) method, with an initialized $db handler.';
				exit;
			}
			if (!$member_id){
				echo 'The member_id is not set.';
			}

			$result3 = mysql_query("SELECT * FROM ".TABLE_PREFIX."calendar WHERE ((MONTH(start_date)=$this->display_month OR MONTH(end_date)=$this->display_month) AND (YEAR(start_date)=$this->display_year OR YEAR(end_date)=$this->display_year)) AND member_id=$member_id",$this->db);
			while($row3 = mysql_fetch_array($result3)) {
				if (($row3[start_date] == $row3[end_date]) || ($row3[end_date] == '0000-00-00 00:00:00'))
				{
					list(,,$dd) = explode('-',$row3[start_date]);
                    $this->entries[(int)$dd][] = $row3;
				} else {
					list(,$mm,$dd) = explode('-',$row3[start_date]);
                    list(,$m2,$d2) = explode('-',$row3[end_date]);
                    if((int)$mm==(int)$m2) {
						for($i=(int)$dd; $i<=(int)$d2; $i++) {
							$this->entries[$i][] = $row3;
						}
					} elseif((int)$mm==$this->display_month) {
						for($i=(int)$dd; $i<32; $i++) {
							$this->entries[$i][] = $row3;
						}
					} else {
						for($i=1; $i<=(int)$d2; $i++) {
							$this->entries[$i][] = $row3;
						}    
					}
				} // end if
            }

			$result2 = mysql_query("SELECT E.* FROM ".TABLE_PREFIX."events E, ".TABLE_PREFIX."member_events M WHERE M.member_id=$member_id AND M.event_id=E.event_id AND ((MONTH(start_date)=$this->display_month OR MONTH(end_date)=$this->display_month) AND (YEAR(start_date)=$this->display_year OR YEAR(end_date)=$this->display_year)) AND approved='y'",$this->db);
			while($row = mysql_fetch_array($result2)) {
				if (($row[start_date] == $row[end_date]) || ($row[end_date] == '0000-00-00'))
				{
					list(,,$dd) = explode('-',$row[start_date]);
                    $this->events[(int)$dd][] = $row;
				} else {
					list(,$mm,$dd) = explode('-',$row[start_date]);
                    list(,$m2,$d2) = explode('-',$row[end_date]);
                    if((int)$mm==(int)$m2) {
						for($i=(int)$dd; $i<=(int)$d2; $i++) {
							$this->events[$i][] = $row;
						}
					} elseif((int)$mm==$this->display_month) {
						for($i=(int)$dd; $i<32; $i++) {
							$this->events[$i][] = $row;
						}
					} else {
						for($i=1; $i<=(int)$d2; $i++) {
							$this->events[$i][] = $row;
						}    
					}
     
				} // end if
            }
		}

		$count=0; // first cell counter

		$this->PrintHeader();
		/************************/
		/* print the first row, cuz it's special! */
		for ($col=0; $col<7; $col++)
		{
			if ($col < $this->firstday) {
				// the empty cells before the calendar starts
				$count++;
			} else {
				// make the very first long cell.
				if ($count > 0)	{
					echo '<td colspan="'.$count.'" class="row1" bgcolor="#EEEEEE">&nbsp;</td>';
					$count=0; // only print it once.
				}

				$thisday = ($col+1)-$this->firstday;
				$this->PrintDay($thisday);
			}
		} // end for
		echo '</tr>';

		/* print the rest of this calendar */
		$nextday = 8 - $this->firstday; // day the rest starts at

		/************************/
		/* get the last day		*/
		$lastday = 28; // we have atleast 28 days.
		while (checkdate($this->display_month,$lastday,$this->display_year)) {
		    $lastday++;
		}

		$count =0; // final cell counter

		/* for each row, for each col */
		for ($row = 0; ($row<5) && ($nextday<$lastday); $row++) // j=row 4, k=col 7, should row<4 ?
		{
			echo '<tr>';
			for ($col = 0; $col<7; $col++) {
				if ($nextday < $lastday) {
					$this->PrintDay($nextday);
					$nextday++;
				} else {
					/* this is one of the very last cells after the lastday */
					$count++;
				}
			}

			/* make the very last long cell. only happens when we are past the lastday */
			if ($count > 0)	{
				echo '<td colspan="'.$count.'" class="row1" bgcolor="#EEEEEE">&nbsp;</td>';
			}
			echo '</tr>';
		}
		echo '</table>';
	} // end PrintCalendar()

	/************************/
	/* PrintDay				*/
	function PrintDay($nextday) {
		if ($this->current_year > $this->display_year) {
			echo '<td valign="top" bgcolor="#dddddd" class="row1 ">';
		} else if ($this->current_month > $this->display_month && $this->current_year == $this->display_year) {       
			echo '<td valign="top" bgcolor="#dddddd" class="row1">';
		} else if ($this->current_month == $this->display_month && $this->current_day > $nextday && $this->current_year == $this->display_year) {
			echo '<td valign="top" bgcolor="#dddddd" class="row1">';
		} else {
			if (($nextday == $this->current_day) && ($this->display_month == $this->current_month) && ($this->display_year == $this->current_year)) {
				echo '<td valign="top" bgcolor="#8ED0FF" class="row1">';
			} else {
				echo '<td valign="top" bgcolor="white" class="row1">';
			}
		}

		if ($this->show_mini == true) {
			echo '<small>';
		}

		echo '<a href="events/events.php?d='.$nextday.SEP.'m='.$this->display_month.SEP.'y='.$this->display_year.'" title="Click to view events for this day" onfocus="this.className=\'highlight\'" onblur="this.className=\'\'">'.$nextday.'</a>';
		if ($this->show_mini == true) {
			echo '</small>';
		}

		if ($this->show_mini != true) {
			$count = $this->PrintSubjects ($nextday);
			/************************/
			/* put the extra spaces to make the cell square (only if not a mini calendar) */
			while ($count < 4) {
				echo '<br />';
				$count++;
			}
		}
		echo '</td>';
	}

	/************************/
	/* decides what gets printed */
	function PrintSubjects($nextday){
		$count=0;
		//if ($this->show_events == true) {
		//	$count = $this->PrintEvents($nextday);
		//}

		if ($this->show_entries == true) {
			$count = $this->PrintEntries($nextday) + $count;
		}

		return $count;
	}

	/************************/
	/* PrintEvents			*/
	function PrintEvents($thisday) {
		global $city, $province, $country;

		$count=0;
		if(is_array($this->events[$thisday])) {
            foreach($this->events[$thisday] as $row) {
				$count++;
				echo $this->event_bullet_start;
				echo '<a href="events/events.php?event_id='.$row['event_id'].SEP.'d='.$thisday.SEP.'m='.$this->display_month.SEP.'y='.$this->display_year.'#'.$row['event_id'].'" class="calendarevent" onfocus="this.className=\'highlight\'" onblur="this.className=\'\'">';
				echo $row['title'];
				echo '</a>';
				echo $this->event_bullet_end;
			}
		}

		return $count;
	} // end PrintEvents
}

/************************************************************************/
/* persona lcalendar */
class PersonalCalendar extends Calendar {

	/************************/
	/* decides what gets printed */
	function PrintSubjects($nextday){
		$count = 0;
		$count = $this->PrintEvents($nextday);
		$count = $this->PrintEntries($nextday) + $count;

		return $count;
	}

	/************************/
	/* PrintEntries			*/
	function PrintEntries($thisday) {
		$count = 0;
		$link = '<a href="events/events.php?d='.$thisday.SEP.'m='.$this->display_month.SEP.'y='.$this->display_year.'" onfocus="this.className=\'highlight\'" onblur="this.className=\'\'">';

		if(is_array($this->entries[$thisday]))
		{
            foreach($this->entries[$thisday] as $row) {
				$count++;
				echo $this->bullet_start;
				echo '<a href="events/events.php?entry_id='.$row[0].SEP.'d='.$thisday.SEP.'m='.$this->display_month.SEP.'y='.$this->display_year.'"  onfocus="this.className=\'highlight\'" onblur="this.className=\'\'">';
				echo $row['subject'];
				echo '</a>';
				echo $this->bullet_end;
			}
		}
		return $count;
	} // end PrintEntries
}


/********************************************************************************/
/* events calendar		*/
class EventCalendar extends Calendar {
	/************************/
	/* restrict this sql to my city */
	function eventCity($my_city_id) {
		if ($my_city_id){
			$this->where_sql .= " AND city_id=$my_city_id";
		}
	}

	/************************/
	/* restrict this sql to my province */
	function eventProvince($my_province_id) {
		if ($my_province_id){
			$this->where_sql .= " AND province_id=$my_province_id";
		}
	}

	/************************/
	/* restrict this sql to my province */
	function eventCountry($my_country_id) {
		if ($my_country_id){
			$this->where_sql .= " AND country_id=$my_country_id";
		}
	}

	/************************/
	/* PrintMiniCalendar	*/
	/* front end for the PrintCalendar() method	*/
	function PrintMiniCalendar(){
		global $city, $province, $country;
		echo '<small><a href="events/?m='.$this->display_month.SEP.'y='.$this->display_year.'" onfocus="this.className=\'highlight\'" onblur="this.className=\'\'">';
		echo date('F', mktime(0,0,0,$this->display_month,1,$this->display_year));
		echo " $this->display_year</a></small>";

		$this->show_mini = TRUE;
		$this->PrintCalendar();
		$this->show_mini = FALSE;
	}


	/************************/
	/* decides what gets printed */
	function PrintSubjects($nextday){
		$count = 0;
		if ($this->show_events == TRUE) {
			$count = $this->PrintEvents($nextday);
		}

		return $count;
	}
}
?>
Return current item: ACollab