Location: PHPKode > projects > O - OpenSource GroupWare > osgw/calendar/classes/class.calendar.php
<?php
/**************************************************************************
   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.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/
/*$Id: class.calendar.php,v 1.17 2004/05/29 07:15:27 rthomp Exp $*/

require("{$O->dir}/calendar/classes/class.format.php");
require("{$O->dir}/calendar/classes/class.events.php");
class calendar
{
	var $display_view;
	var $size;
	var $default_day;
	var $default_month;
	var $default_year;
	var $working_day;
	var $working_month;
	var $working_year;
	var $total_days;
	var $current_day;
	var $first_weekday;
	var $last_weekday;
	
	/*!
		@function calendar()
		@author Ryan Thompson
		@abstract Starts up a new calendar
		@version 0.1
		@param $view
		@param $day
		@param $month
		@param $year
		@return TRUE
		@since 07-04-2004
	*/
	function calendar($view, $day, $month, $year)
	{
		$this->display_view = $view;
			
		if(!isset($day))
		{
			$this->default_day = date('j');
			$this->default_month = date('n');
			$this->default_year = date('Y');
		} else {
			$this->default_day = $day;
			$this->default_month = $month;
			$this->default_year = $year;
		}

	}
	
	/*!
		@function default_view()
		@author Ryan Thompson
		@abstract Retrieves default view from preferences
		@version 0.1
		@return $default_view
		@since 05-04-2004
	*/	
	function default_view()
	{
		GLOBAL $user;
		
		return $user->get_preference('cl','view', $user->user_id);
		
	}
	/*!
		@function nav_buttons()
		@author Ryan Thompson
		@abstract Completes Navigation bar for Calendar
		@version 0.1
		@param $view
		@return $nav_buttons
		@since 06-04-2004
	*/	
	function nav_buttons($view)
	{
		GLOBAL $date, $layout, $lang, $html, $user;
		
		if(is_null($_GET['v']))
		{
			$day = date('j');
			$month = date('n');
			$year = date('Y');
			
			$view_name = ucfirst(basename($_SERVER['SCRIPT_FILENAME'], ".php"));
			$view_list = array_flip(explode(':',$lang->get_msg('views','cl')));
					
			$view_id = $view_list[$view_name];
		} else {
			$view_list = explode(':',$lang->get_msg('views','cl'));
			$view_name = $view_list[$view];
			$view_id = $view;
			$day = $_GET['d'];
			$month = $_GET['m'];
			$year = $_GET['y'];
		}
	
		$date_nav = "<input type=\"submit\" name=\"create\" value=\"". $lang->msgs['create_event'] ."\"
			class=\"button1\" onClick=\"window.location='create_event.php'\">&nbsp;";
		$date_nav .= "<input type=\"submit\" name=\"list\" value=\"". $lang->msgs['list_events'] ."\"
			class=\"button1\" onClick=\"window.location='event_list.php'\">";
		$date_nav .= $date->date_select('d', 'day', $day);
		$date_nav .= $date->date_select('m', 'month', $month);
		$date_nav .= $date->date_select('y', 'year', $year);
		
		$date_nav .= $html->select_box('v', 'array', explode(':', $lang->get_msg('views','cl')), $view_id);
		$date_nav .= "<input type=\"submit\" name=\"go\" value=\"". $lang->msgs['go'] ."\"
			class=\"button1\">";
		$date_nav .= "&nbsp;<input type=\"submit\" name=\"today\" value=\"{$lang->msgs['today']}\"
			class=\"button1\">";
		
		return  "<form action=\"index.php\" method=\"get\">". $layout->service_header($lang->get_msg('service_name','cl'), $date_nav, $lang->get_msg($view_name, 'cl')). "</form>";
	}
	
	/*!
		@function set_working_month()
		@author Ryan Thompson
		@abstract Sets the working month for a display
		@version 0.1
		@param $offset
		@return $working_month
		@since 07-04-2004
	*/	
	
	function set_working_month($offset)
	{

		$this->working_month = $this->default_month + $offset;
		$this->working_year = $this->default_year;
		if($this->working_month == 0)
		{
			$this->working_month = 12;
			$this->working_year = $this->default_year - 1;
			
		} elseif($this->working_month == 13)
		{
			$this->working_month = 1;
			$this->working_year = $this->default_year + 1;
		}
		//$this->working_timestamp = mktime(1,0,0, $this->working_month, 1, $year);
		
		//echo "<br>". $this->working_year . ' ' . $this->working_month;
		return $this->working_month;	
	}
	
	/*!
		@function new_month()
		@author Ryan Thompson
		@abstract Begins a new month
		@version 0.1
		@param $size
		@param $offset
		@since 07-04-2004
	*/	
	function new_month($size, $offset)
	{
		
		$this->size = $size;
		$this->set_working_month($offset);
		$this->total_days = $this->set_total_days($this->working_month, $this->working_year);
		$this->current_day = 1;
		$this->first_weekday = $this->get_first_weekday($this->working_month, $this->working_year);
		$this->last_weekday = $this->get_last_weekday($this->working_month, $this->working_year);
		$format = new format($size, $this->working_month, $this->working_day, $this->working_year);
		if($this->size == 'large')
		{
			$links['next'] = $this->navigation_link('next', 'month');
			$links['prev'] = $this->navigation_link('prev', 'month');
		}
		$header = $format->calendar_header($links, "cal_header_{$this->size}.html", $this->display_view);
		
		$month = $this->create_month();
		
		
		$footer = $format->calendar_footer();
		
		
		$calendar = $header. $month. $footer;
		
		return $calendar;
	}
	
	
	/*!
		@function create_month()
		@author Ryan Thompson
		@abstract Create a month calendar
		@version 0.1
		@return $formatted_month
		@since 07-04-2004
	*/
	function create_month()
	{
		
		$format = new format($this->size, $this->working_month, $this->working_day, $this->working_year);
		$i = $this->first_weekday;
		$formatted_month = $format->weekdays();
		$formatted_month .= "</tr><tr>\n";
		
		$formatted_month .=	$format->fill_empty_cells($this->first_weekday);

		while($this->current_day < $this->total_days)
		{
			while($i < 7)
			{
				if($this->current_day > $this->total_days)
				{
					//Send the number of days it would take to fill from the cell
					//after the last day to Saturdays cell. 
					//This has to be 6 to account for weekdays counting 0 - 6.
					
					$formatted_month .=	$format->fill_empty_cells(6 - $this->last_weekday);
					break;
				} else {
					
					$events = new events();
					$start_time = date('U', mktime(0,0,0, $this->working_month, $this->current_day, $this->working_year));
					$finish_time = date('U', mktime(0,0,0, $this->working_month, $this->current_day + 1, $this->working_year));
					$holidays_list = $events->search_holidays($start_time, $finish_time);
					$event_list = $events->search_events($start_time, $finish_time);
					$birthdays = $events->search_birthdays($start_time);
					
					$formatted_month .= $format->format_cell($this->current_day, $event_list, $holidays_list, $birthdays);
				}
				$i++;	
				$this->current_day++;
		
			}
			$i = 0;
			$formatted_month .= "</tr><tr>\n";
		}
		
		
		
		return $formatted_month;
	}

	/*!
		@function get_first_weekday()
		@author Ryan Thompson
		@abstract Create a month calendar
		@version 0.1
		@params $month
		@params $year
		@since 07-04-2004
	*/
	function get_first_weekday($month, $year)
	{
		return date('w', mktime(0, 0, 0, $month, 1, $year));
	}
	
	/*!
		@function get_last_weekday()
		@author Ryan Thompson
		@abstract Get the last weekday of the month
		@version 0.1
		@params $month
		@params $year
		@since 07-04-2004
	*/
	function get_last_weekday($month, $year)
	{
		return date('w', mktime(0, 0, 0, $month, date('t', mktime(0,0,0, $month, 1, $year)), $year));
	}
	
	/*!
		@function set_total_days()
		@author Ryan Thompson
		@abstract Sets the total number of days in the working month
		@version 0.1
		@params $month
		@params $year
		@since 07-04-2004
	*/
	
	function set_total_days($month, $year)
	{
		return date('t', mktime(0,0,0, $month, 1, $year));
	}
	
	/*!
		@function navigation_link()
		@author Ryan Thompson
		@abstract Creates navigation link for next $item (day, month, year)
		@version 0.1
		@params $direction
		@params $item
		@return $next_link;
		@since 07-04-2004
	*/
	function navigation_link($direction, $item)
	{
		GLOBAL $O, $layout;
		
		if($direction == 'next')
		{
			$offset = +1;
			
		} else {
			$offset = -1;
		}	
			
		switch($item)
		{
			case 'day':
				$link = date('U', mktime(0,0,0, $this->default_month, $this->default_day+ $offset, $this->default_year));
			break;
			case 'week':
			
				$link = date('U', mktime(0,0,0, $this->working_month, $this->working_day+ ($offset * 7), $this->working_year));
			break;
			case 'month':
				$link = date('U', mktime(0,0,0, $this->default_month + $offset, $this->default_day, $this->default_year));
			break;
			case 'year':
				$link = date('U', mktime(0,0,0, $this->default_month, $this->default_day, $this->default_year +$offset));
			break;
		}
		
		$day = date('j', $link);
		$month = date('n', $link);
		$year = date('Y', $link);
			
		$image = $O->create_image("/calendar/themes/{$layout->user_theme}/$direction.png", $direction, "{$this->size}_header_cell");
		return $O->create_link("/calendar/$item.php?v={$_GET['v']}&d=$day&m=$month&y=$year", $image, "navigation");
			
	}
	
	/*!
		@function new_day()
		@author Ryan Thompson
		@abstract Creates a new day view
		@version 0.1
		@params $increment
		@return $formatted_day		
		@since 05-05-2004
	*/
	function new_day($increment = 30)
	{
		$this->size = 'large';

		$format = new format($this->size, $this->default_month, $this->default_day, $this->default_year);
		$links['next'] = $this->navigation_link('next', 'day');
		$links['prev'] = $this->navigation_link('prev', 'day');
		
		$header = $format->calendar_header($links, "day_header.html", $this->display_view);
		$this->working_day = $this->default_day;
		$this->working_month = $this->default_month;
		$this->working_year = $this->default_year;		
		$day = $this->create_day($increment, $format);
		
		$footer = $format->calendar_footer();
		$formatted_day = $header .$day. $footer;

		return $formatted_day;
		
	}

	/*!
		@function create_day()
		@author Ryan Thompson
		@abstract Creates a new day view
		@version 0.1
		@params $increment
		@params $format
		@return $time_sheet		
		@since 05-05-2004
	*/
function create_day($increment, $format)
{
	
	GLOBAL $layout;
		
	$events = new events();
	
	$time_sheet = NULL;	
	$i = 0;
	$events->day_events(
		mktime(0, 0, 0, $this->working_month, $this->working_day, $this->working_year),
		mktime(23, 59,59, $this->working_month, $this->working_day, $this->working_year));
			
	$cells_per_hour = 60 / $increment;
	$finish = 24 * $cells_per_hour;
	$fraction = 0;
	$j =0;
	while($i < $finish)
	{
		unset($printed);
	
		$start_time = mktime($j, $fraction, 0, $this->working_month, $this->working_day, $this->working_year);
		$time = date('H:i', $start_time);
		$time_sheet .= $format->create_time_cell($time);
		$end_time = mktime($i, $fraction + 29, 59, $this->working_month, $this->working_day, $this->working_year);
		if(is_array($events->event_list))
		{
			foreach($events->event_list as $key => $data)
			{
			
				$event_start = mktime(date('H', $data['evt_start']), date('i', $data['evt_start']), 0, $this->working_month, $this->working_day, $this->working_year);
			
				if($event_start == $start_time )
				{
					if(date('j', $data['evt_end']) != date('j', $data['evt_start']))
					{
						$u =  date('G', $event_start);
						$t = 24; //Finish on the 24th hour is the event spans
						
						$span = ($t - $u) * $cells_per_hour;
					} else {

						$u =  date('H', $event_start);
						$t = date('H', $data['evt_end']);
						$span = ($t - $u) * $cells_per_hour;
						
					}
					
				
					$time_sheet .= $format->create_event_cell($data, $span);
					$fraction = $fraction * $span;
					$printed = TRUE;
				}
			}
	
			if(!$printed)
			{
				$time_sheet .= $format->create_event_cell(NULL, 0);
			}
	
		} else {
			$time_sheet .= $format->create_event_cell(NULL, 0);
		}
			
		$fraction = $increment + $fraction;
		if($fraction == 60)
		{
			$fraction = 0;
			
			$j++;
		}

		$i++;
	}
		
	return $time_sheet;
		
}
	
	/*!
		@function new_week()
		@author Ryan Thompson
		@abstract Retrieves default view from preferences
		@version 0.1
		@return $week_view
		@since 25-05-2004
	*/
	function new_week()
	{
		$first_day = $this->get_first_day();
		
		
		$this->size = 'large';

		$format = new format($this->size, $this->default_month, $this->default_day, $this->default_year);
		$links['next'] = $this->navigation_link('next', 'week');
		$links['prev'] = $this->navigation_link('prev', 'week');
		$format->week_start = $first_day;
		$header = $format->calendar_header($links, "week_header.html", $this->display_view);
				
		return $header;
	}

	/*!
		@function get_first_day()
		@author Ryan Thompson
		@abstract Retrieves the first day of the week
		@version 0.1
		@return $week_view
		@since 25-05-2004
	*/
	function get_first_day()
	{
		GLOBAL $user;
		$week_start = $user->get_preference('cl', 'week_starts', $user->user_id);
		$current_weekday = date('w', mktime(0,0,0, $this->default_month, $this->default_day, $this->default_year));
		$today = mktime(0,0,0, $this->default_month, $this->default_day, $this->default_year);
		
		$first_day = $today - (DAY_SEC * ($current_weekday - $week_start));

		
		$this->working_day = date('j', $first_day);
		$this->working_year = date('Y', $first_day);
		$this->working_month = date('n', $first_day);
		
		return $first_day;
	}
		
}
Return current item: O - OpenSource GroupWare