<?php
class Calendar {
	var $res;
	var $curent_time;

        function Calendar($start=0, $end=0) {
		function f($x) {
			return ($x==0 ? 7 : $x);
		}

		global $link, $d, $m, $PATH;
		$array=array(
			1 => "Январь",
			2 => "Февраль",
			3 => "Март",
			4 => "Апрель",
			5 => "Май",
			6 => "Июнь",
			7 => "Июль",
			8 => "Август",
			9 => "Сентябрь",
			10 => "Октябрь",
			11 => "Ноябрь",
			12 => "Декабрь");
		$d=(!$d ? date("d") : intval($d));
		$cur=mktime(0, 0, 0, date("m")+$m, $d, date("Y"));
		$n=date("t", $cur);

		$page=new Template("templates/calendar/calendar.tpl");
		$page->dictionary("common/dict.ru");
		$page->assign(array(
			"LINK"			=> $link->get(),
			"y_left_link"		=> "d.$d/m.".($m-12)."/",
			"m_left_link"		=> "d.$d/m.".($m-1)."/",
			"m_right_link"		=> "d.$d/m.".($m+1)."/",
			"y_right_link"		=> "d.$d/m.".($m+12)."/",
			"calendar_day"		=> $array[date("n", $cur)].date(", Y", $cur),
			"calendar_curent"	=> date("d.m.Y"),
			"PATH"			=> $PATH,
		));
		$_page=new Template("templates/calendar/calendar_line.tpl");
		$_page->dictionary("common/dict.ru");
		$_page->assign(array(
			"LINK"		=> $link->get(),
			"PATH"		=> $PATH,
		));
		for ($i=1; $i<f(date("w", mktime(0, 0, 0, date("m")+$m, 1, date("Y")))); $i++)
			$_page->addrow("LINE", array());

		$w=date("W", mktime(0, 0, 0, date("m")+$m, 1, date("Y")));
		for ($i=1; $i<=$n; $i++) {
			$t=mktime(0, 0, 0, date("m")+$m, $i, date("Y"));
			if ($w!=date("W", $t)) {
				$w=date("W", $t);
				$page->addrow("LINE", array("line" => $_page->evaluate()));
				$_page=new Template("templates/calendar/calendar_line.tpl");
				$_page->dictionary("common/dict.ru");
				$_page->assign(array(
					"LINK"		=> $link->get(),
					"PATH"		=> $PATH,
				));
			}
			$_page->addrow("LINE", array(
				"selected"	=> ($t==$cur),
				"day"		=> $i,
				"link"		=> "d.".$i."/m.".$m."/",
				"disabled"	=> ($t<$start or ($end ? ($t>$end) : 0)),
				"t"		=> $t,
			));
		}
		for ($i=f(date("w", mktime(0,0,0,date("m")+$m,$n,date("Y"))));$i<7; $i++) $_page->addrow("LINE", array());
		$page->addrow("LINE", array("line" => $_page->evaluate()));
		$this->res=$page->evaluate();
		$this->curent_time=$cur;
	}

	function get() {
		return $this->res;
	}
}
?>