<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://gekkoware.sourceforge.net
	*/

	if (!defined("IN-BLOCK")) die();

	/*
	<%%>
	<?xml version="1.0" encoding="UTF-8"?>
	<blockscript>
		<info lang="en">
			<title>Calendar</title>
			<description>Shows a calendar with relevant links to information for the given month</description>
			<author><![CDATA[xiam &lt;<a href="mailto:xiam@users.sourceforge.net">xiam@users.sourceforge.net</a>&gt;]]></author>
			<homepage><![CDATA[<a href="http://www.gekkoware.org">http://www.gekkoware.org</a>]]></homepage>
		</info>
		<info lang="es">
			<title>Calendario</title>
			<description><![CDATA[Muestra un calendario con enlaces relevantes para el mes dado]]></description>
		</info>
		<variable name="simple">0</variable>
		<variable name="month">0</variable>
		<variable name="year">0</variable>
	</blockscript>
	<%%>
	*/

	require_once (GEKKO_ROOT."lib/calendar.lib.php");

	$date = explode(".", preg_replace("/[^0-9.]/", "",_ext("date")));
	if (count($date) > 1) {
		$year = intval($date[0]);
		$month = intval($date[1]);
	}

	if ($month <= 0 || $month > 12) $month = date("m");
	if ($year <= 0) $year = date("Y");

	$cal = new Calendar($month, $year);

	if (!$simple) {

		/* lower and upper timestamp limits for this month */
		$lower_ts = mktime(0, 0, 0, $month, 1, $year);
		$upper_ts = mktime(23, 59, 59, $month, date("t", $lower_ts), $year);

		/* events counter */
		$events = Array();

		/* fetching all information we can for the current module and the selected month/year */
		switch (MAIN_MODULE) {

			case "blog": case "news":

				/* those modules are very similar */

				require_once GEKKO_ROOT."modules/categories/main.lib.php";
				$cat_lock = getAllowedCategories(true);

				$q = $GLOBALS["db"]->query("SELECT ID, Title, CreationTime
				FROM ".db_table(MAIN_MODULE)." WHERE Status = '1' AND CreationTime >= '{$lower_ts}' AND CreationTime <= '{$upper_ts}'",
				__FILE__, __LINE__, true);

				while ($row = $GLOBALS["db"]->fetch_row($q)) {
					saneHTML($row);

					$day = date("j", $row["CreationTime"]);

					/* events counter */
					$events[$day] = isset($events[$day]) ? $events[$day]+1 : 1;

					if ($events[$day] > 0) {
						/* an event has already counted today! */
						$cal->set_html($day, "<a href=\""._l("C_SITE.REL_URL")."index.php/".MAIN_MODULE."?date=$year.$month.$day\" title=\"(".$events[$day].")\">$day</a>");
					} else {
						/* this is the first event for today */
						$cal->set_html($day, "<a href=\""._l("C_SITE.REL_URL")."index.php/".MAIN_MODULE."/view/".urlencode($row["Title"])."?id={$row["ID"]}\" title=\"".$row["Title"]."\">$day</a>");
					}
				}
			break;
		}
	}

	$return = $cal->make();

?>