Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
Kalens - Blob - ViewGit - Blue Static
<?php
/*=====================================================================*\
|| ###################################################################
|| # Kalens [#]version[#]
|| # Copyright ©2002-[#]year[#] Iris Studios, Inc.
|| #
|| # 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; version [#]gpl[#] of the License.
|| #
|| # 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.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/

// ######################### REQUIRE BACKEND #########################
define('SVN', '$Id$');

$apptemplates = array(
	// Month view
	'MONTHVIEW',
	'monthview_daybit',
	'monthview_eventbit',
	'monthview_weekbit',
	'editor_dropdown_year',
	'editor_dropdown_month',

	// Print month
	'MONTHVIEW_PRINT',
	'monthview_print_weekbit',
	'monthview_print_daybit',
	'monthview_print_eventbit',

	// Week view
	'WEEKVIEW',
	'weekview_daybit',
	'weekview_eventbit',

	// Print week
	'WEEKVIEW_PRINT',
	'weekview_print_daybit',
	'weekview_print_eventbit',

	// Small calendar
	'smallcalendar',
	'smallcalendar_weekbit',
	'smallcalendar_daybit'
);

require_once('./global.php');
require_once('./includes/date.php');
require_once('./includes/event.php');

// ######################## START MAIN SCRIPT ########################

// No calendar request has been made
if (empty($_REQUEST['id']))
{
	print_error($lang->string('The calendar you specified is invalid.'));
}

$kalens->input_clean('id', TYPE_UINT);

$calendar = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "calendar WHERE calendarid = " . $kalens->in['id']);

// The user is a guest, but the calendar is not public so give them a no permission error
if (!verify_calendar_permissions($kalens->in['id'], 0))
{
	print_no_permission();
}

/* Big query to pull everything out at once (not needed, atm):

SELECT c.*, c.title AS calendar_title, s.*, e.*
FROM eventstore s
LEFT JOIN event e ON (e.eventid = s.groupid)
LEFT JOIN calendar c ON (e.calendarid = e.calendarid)
WHERE c.calendarid = $id

*/

/*

Dev notes: $info is used in the templates
$render is used for creating the calendar

*/

// ########################## REDEFINE DAYS ##########################
$system['cdays'] = array(
	'Sunday' => 1,
	'Monday' => 2,
	'Tuesday' => 3,
	'Wednesday' => 4,
	'Thursday' => 5,
	'Friday' => 6,
	'Saturday' => 7,

	1 => 'Sunday',
	2 => 'Monday',
	3 => 'Tuesday',
	4 => 'Wednesday',
	5 => 'Thursday',
	6 => 'Friday',
	7 => 'Saturday'
);

// ########################### MONTHLY VIEW ##########################
if ($_REQUEST['view'] == 'month' OR empty($_REQUEST['view']))
{
	$kalens->input_clean_array(array(
		'month'		=> TYPE_UINT,
		'year'		=> TYPE_UINT
	));

	store_date_args();

	require_once('./includes/defined.php');
	$info['year'] = (!$kalens->in['year'] ? pcdate('Y', TIMENOW) : $kalens->in['year']);
	$info['month'] = (!$kalens->in['month'] ? pcdate('n', TIMENOW) : $kalens->in['month']);
	$info['monthname'] = $system['months']["$info[month]"];
	$info['nextmon'] = $info['month'] + 1;
	$info['prevmon'] = $info['month'] - 1;
	$info['prevyear'] = ($info['prevmon'] == 0 ? $info['year'] - 1 : $info['year']);
	$info['nextyear'] = ($info['nextmon'] == 13 ? $info['year'] + 1 : $info['year']);
	$info['nextmon'] = ($info['nextmon'] == 13 ? 1 : $info['nextmon']);
	$info['prevmon'] = ($info['prevmon'] == 0 ? 12 : $info['prevmon']);

	// Month jump
	$yearlist = construct_year_list((!$kalens->in['year'] ? pcdate('Y', TIMENOW) : $kalens->in['year']));
	$monthlist = construct_month_list((!$kalens->in['month'] ? pcdate('m', TIMENOW) : $kalens->in['month']), $system['months']);

	store_date_args();
	$calendar['dateargs'] = unstore_date_args('url');

	$templates = array();
	if ($_REQUEST['print'])
	{
		$templates['monthview_daybit'] = 'monthview_print_daybit';
		$templates['monthview_eventbit'] = 'monthview_print_eventbit';
		$templates['monthview_weekbit'] = 'monthview_print_weekbit';
		$templates['MONTHVIEW'] = 'MONTHVIEW_PRINT';
	}
	else
	{
		$templates['monthview_daybit'] = 'monthview_daybit';
		$templates['monthview_eventbit'] = 'monthview_eventbit';
		$templates['monthview_weekbit'] = 'monthview_weekbit';
		$templates['MONTHVIEW'] = 'MONTHVIEW';
	}

	// Start day of the month
	$today = pcgetdate(TIMENOW, false);
	$start = gmmktime(00, 00, 00, ($kalens->in['month'] ? $kalens->in['month'] : $today['mon']), 01, ($kalens->in['year'] ? $kalens->in['year'] : $today['year']));
	$render['start'] = pcgetdate($start, true);

	// End day of the month
	if ($render['start']['mon'] == 12)
	{
		$render['end'] = gmmktime(00, 00, 00, 1, 01, $render['start']['year'] + 1);
	}
	else
	{
		$render['end'] = gmmktime(00, 00, 00, $render['start']['mon'] + 1, 01, $render['start']['year']);
	}

	// The numerical format of the last day of the month (total days)
	$render['totaldays'] = pcdate('d', $render['end']);
	if ($render['totaldays'] < 20)
	{
		$render['totaldays'] = pcdate('d', $render['end'] - SECONDS_IN_DAY);
	}

	$render['start'] = convert_date_to_gmt($render['start']['0']);
	$render['end'] = convert_date_to_gmt($render['end']);

	// The name of the day that on which the first day of the month falls
	$render['startdow'] = pcdate('l', $render['start']);

	// If the month starts on any day but Sunday, put in blank days to cover
	// for it
	$show['blank'] = true;
	foreach ($system['cdays'] AS $_key => $_value)
	{
		if (is_int($_value))
		{
			if ($render['startdow'] != $system['cdays']["$_value"])
			{
				eval('$calendar[weekbits] .= "' . $template->fetch($templates['monthview_daybit']) . '";');
			}
			else
			{
				break;
			}
		}
	}
	$show['blank'] = false;

	// Get the numerical day code
	$day_counter = $system['cdays']["$render[startdow]"];

	// Fetch all of the events from the database and put them in an array for evaluation in the calendar rednering
	$get_events = $db->query("
		SELECT
			event.startline as event_startline,
			event.endline as event_endline,
			event.*,
			eventstore.*
		FROM " . TABLE_PREFIX . "event AS event LEFT JOIN " . TABLE_PREFIX . "eventstore AS eventstore ON (eventstore.groupid = event.eventid)
		WHERE event.calendarid = " . $kalens->in['id'] . "
		AND eventstore.startline >= " . convert_gmt_to_user($render['start']) . "
		AND eventstore.endline <= " . convert_gmt_to_user($render['end']) . "
		ORDER BY event.startline, event.stime ASC"
	);

	while ($event = $db->fetch_array($get_events))
	{
		$eventlist[] = $event;
	}

	// Create the main calendar display
	for ($i = 1; $i <= $render['totaldays']; $i++)
	{
		// Eval() the days of the week
		$monthview['date'] = $i;

		// Make sure we don't have any events going into this
		unset($eventbits);

		if (is_array($eventlist))
		{
			foreach ($eventlist AS $eventinfo)
			{
				$eventinfo['startstamp'] = process_user_date($eventinfo['startline'], $eventinfo['stime']);
				$eventinfo['time'] = pcdate($kalens->options['timeformat'], $eventinfo['startstamp']);
				$eventinfo['eventid'] = $eventinfo['groupid'];

				if (pcdate('d', $eventinfo['startstamp']) == $i AND pcdate('m', $eventinfo['startstamp']) == pcdate('m', $render['start']) AND pcdate('Y', $eventinfo['startstamp']) == pcdate('Y', $render['start']))
				{
					if ($eventinfo['type'] == '2' AND pcdate('d', $eventinfo['event_endline']) < $i AND pcdate('m', $eventinfo['event_endline']) == pcdate('m', $render['start']))
					{
						// There's no way around this other than to have this blank
					}
					else
					{
						$eventinfo['title'] = fetch_trimmed_title($eventinfo['title']);
						if ($eventinfo['allday'])
						{
							$eventinfo['time'] = $lang->string('All Day');
						}
						eval('$eventbits .= "' . $template->fetch($templates['monthview_eventbit']) . '";');
					}
				}
			}
		}

		store_date_args(false, $i);
		$info['dateargs'] = unstore_date_args('uri');

		if (pcdate('d', TIMENOW) == $monthview['date'] AND pcdate('m', TIMENOW) == $info['month'] AND pcdate('Y', TIMENOW) == $info['year'])
		{
			$show['highlight'] = true;
		}
		else
		{
			$show['highlight'] = false;
		}

		eval('$daybits = "' . $template->fetch($templates['monthview_daybit']) . '";');
		eval('$calendar[weekbits] .= "' . addslashes($daybits) . '";');

		if ($day_counter == 7 OR $i == $render['totaldays'])
		{
			$show['blank'] = true;
			// If this is the end of the month and the last day isn't Saturday, create blanks
			if ($day_counter != 7 AND $i == $render['totaldays'])
			{
				for ($n = $day_counter; $n < 7; $n++)
				{
					eval('$calendar[weekbits] .= "' . $template->fetch($templates['monthview_daybit']) . '";');
				}
			}

			// Finish rendering the week
			if (!$initweek)
			{
				$calendar['day'] = 1;
			}

			$calendar['month'] = (!$kalens->in['month'] ? pcdate('m', TIMENOW) : $kalens->in['month']);
			$calendar['year'] = (!$kalens->in['year'] ? pcdate('Y', TIMENOW) : $kalens->in['year']);
			eval('$calendar[monthbits] .= "' . $template->fetch($templates['monthview_weekbit']) . '";');
			unset($calendar['weekbits']);
			$day_counter = 0;
			$show['blank'] = false;

			$initweek = 1;

			if ($initweek)
			{
				$calendar['day'] = $i + 1;
			}
		}

		$day_counter++;
	}

	unset($render);

	//$curmonth = construct_small_month($info['month'], $info['year']);
	$nextmonthcal = construct_small_month($info['nextmon'], $info['nextyear']);
	$prevmonthcal = construct_small_month($info['prevmon'], $info['prevyear']);

	$navbits = construct_navbar(array($lang->string('Calendar') => "showcalendar.php?id=$calendar[calendarid]", $calendar['title']));
	eval('$navbar = "' . $template->fetch('navbar') . '";');
	eval('$template->flush("' . $template->fetch($templates['MONTHVIEW']) . '");');
}

// ########################### WEEKLY VIEW ###########################
if ($_REQUEST['view'] == 'week')
{
	$kalens->input_clean_array(array(
		'month'		=> TYPE_UINT,
		'day'		=> TYPE_UINT,
		'year'		=> TYPE_UINT
	));

	store_date_args(false, -1);
	$info['monthdateargs'] = unstore_date_args('url');

	store_date_args();

	$templates = array();
	if ($_REQUEST['print'])
	{
		$templates['weekview_eventbit'] = 'weekview_print_eventbit';
		$templates['weekview_daybit'] = 'weekview_print_daybit';
		$templates['WEEKVIEW'] = 'WEEKVIEW_PRINT';
	}
	else
	{
		$templates['weekview_eventbit'] = 'weekview_eventbit';
		$templates['weekview_daybit'] = 'weekview_daybit';
		$templates['WEEKVIEW'] = 'WEEKVIEW';
	}

	// Take the GET vars and turn them into a time stamp
	$render['start'] = gmmktime(00, 00, 00, $kalens->in['month'], $kalens->in['day'], $kalens->in['year']);

	// GMT may set this over the edge, so fixy it
	if (pcdate('d', $render['start']) < $kalens->in['day'] OR pcdate('m', $render['start']) != $kalens->in['month'])
	{
		$render['start'] += SECONDS_IN_DAY;
	}
	else if (pcdate('d', $render['start']) > $kalens->in['day'])
	{
		$render['start'] -= SECONDS_IN_DAY;
	}

	// Get the end of the week by just going until we hit Saturday and making that a timestamp
	$render['end'] = $render['start'];
	$day_of_week = pcdate('w', $render['start']) + 1;
	while ($day_of_week < $system['cdays']['Saturday'] AND pcdate('n', $render['end'] + SECONDS_IN_DAY) == $kalens->in['month'])
	{
		$render['end'] += SECONDS_IN_DAY;
		$day_of_week++;
	}

	$info['weekstart'] = pcdate('d', $render['start']);
	$info['month'] = $kalens->in['month'];
	$info['monthname'] = $system['months']["$info[month]"];
	$info['year'] = pcdate('Y', $render['start']);
	$info['day'] = pcdate('d', $render['start']);
	$info['dateargs'] = unstore_date_args('url');

	// Pad the week with any days that aren't in the month
	$render['startdow'] = pcdate('w', $render['start']) + 1;
	if ($render['startdow'] != $system['cdays']['Sunday'])
	{
		$temp_start_dow = $render['startdow'];

		$show['blank'] = true;
		foreach ($system['cdays'] AS $_key => $_value)
		{
			if (is_int($_value))
			{
				if ($temp_start_dow > $system['cdays']['Sunday'])
				{
					eval('$calendar[daybits] .= "' . $template->fetch($templates['weekview_daybit']) . '";');
					$temp_start_dow--;
				}
				else
				{
					break;
				}
			}
		}
		$show['blank'] = false;

		$render['daystorender'] = 8 - $render['startdow'];
	}
	else
	{
		$render['daystorender'] = 7;
	}

	// Query the database for the events
	$get_events = $db->query("
		SELECT
			event.startline as event_startline,
			event.endline as event_endline,
			event.*,
			eventstore.*
		FROM " . TABLE_PREFIX . "event AS event LEFT JOIN " . TABLE_PREFIX . "eventstore AS eventstore ON (eventstore.groupid = event.eventid)
		WHERE event.calendarid = " . $kalens->in['id'] . "
		AND eventstore.startline >= " . convert_gmt_to_user($render['start'] - SECONDS_IN_DAY) . "
		AND eventstore.endline <= " . convert_gmt_to_user($render['end'] + SECONDS_IN_DAY) . "
		ORDER BY event.startline, event.stime ASC"
	);

	while ($event = $db->fetch_array($get_events))
	{
		$eventlist[] = $event;
	}

	// Plot the events
	for ($i = 0; $i < $render['daystorender']; $i++)
	{
		unset($eventbits);

		if (is_array($eventlist))
		{
			foreach ($eventlist AS $eventinfo)
			{
				$eventinfo['startstamp'] = process_user_date($eventinfo['startline'], $eventinfo['stime']);
				$eventinfo['time'] = pcdate($kalens->options['timeformat'], $eventinfo['startstamp']);
				$eventinfo['eventid'] = $eventinfo['groupid'];

				if (pcdate('d', $eventinfo['startstamp']) == pcdate('d', $render['start'] + (SECONDS_IN_DAY * $i)) AND pcdate('d', $eventinfo['startstamp']) <= pcdate('d', $eventinfo['event_endline']))
				{
					$eventinfo['title'] = fetch_trimmed_title($eventinfo['title']);
					if ($eventinfo['allday'])
					{
						$eventinfo['time'] = $lang->string('All Day');
					}
					eval('$eventbits .= "' . $template->fetch($templates['weekview_eventbit']) . '";');
				}
			}
		}

		$weekview['timecode'] = $render['start'] + (SECONDS_IN_DAY * $i);

		if (pcdate('n', $weekview['timecode']) == $kalens->in['month'] AND pcdate('Y', $weekview['timecode'] == $kalens->in['year']))
		{
			$weekview['date'] = pcdate('d', $weekview['timecode']);
			$show['blank'] = false;
		}
		else
		{
			$show['blank'] = true;
		}

		if (pcdate('d', TIMENOW) == $weekview['date'] AND pcdate('m', TIMENOW) == $info['month'] AND pcdate('Y', TIMENOW) == $info['year'])
		{
			$show['highlight'] = true;
		}
		else
		{
			$show['highlight'] = false;
		}

		store_date_args(false, $weekview['date']);
		$info['dateargs'] = unstore_date_args('uri');

		eval('$calendar[daybits] .= "' . $template->fetch($templates['weekview_daybit']) . '";');

		$show['blank'] = false;
	}

	store_date_args();

	unset($render);

	$navbits = construct_navbar(array($calendar['title'] => "showcalendar.php?id=$calendar[calendarid]&amp;$info[monthdateargs]", sprintf($lang->string('Week of %1$s %2$s, %3$s'), $info['monthname'], $info['weekstart'], $info['year'])));
	eval('$navbar = "' . $template->fetch('navbar') . '";');
	eval('$template->flush("' . $template->fetch($templates['WEEKVIEW']) . '");');
}

/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>