<?php


class Plugin_blogcalendar extends Plugin
{
	function do_block($pagename, $param1, $param2)
	{
		$prefix = trim($param1);
		
		$pattern = '^' . mb_ereg_quote($prefix) . '/(\d{1,4})-(\d{2})';
		$get = KinoWiki::getinstance()->getGET();
		if(mb_ereg($pattern, KinoWiki::getinstance()->getpagename(), $m)){
			$year = $m[1];
			$month = $m[2];
		}
		else if(isset($get['year']) && isset($get['month'])){
			$year = $get['year'];
			$month = $get['month'];
		}
		else{
			$year = date('Y');
			$month = date('n');
		}
		
		$smarty = new PluginSmarty('blogcalendar');
		$wiki = Wiki::getinstance();
		$day = 1;
		$last = date('t', mktime(0, 0, 0, $month, 1, $year));
		while($day <= $last){
			$line = array_fill(0, 7, '&nbsp;');
			$d = date('w', mktime(0, 0, 0, $month, $day, $year));
			for(; $d < 7 && $day <= $last; $d++, $day++){
				$page = sprintf('%s/%04d-%02d-%02d', $prefix, $year, $month, $day);
				$line[$d] = $wiki->ispage($page) ? makelink($page, $day) : $day;
			}
			$smarty->append('table', $line);
		}
		$smarty->assign('year', $year);
		$smarty->assign('month', $month);
		$smarty->assign('prefix', $prefix);
		$smarty->assign('page', KinoWiki::getinstance()->getpagename());
		return $smarty->fetch('blogcalendar.tpl.htm');
	}
}

?>