<?php
/**
* 月をあらわすクラス
*
* PHP version 5
*
* @package    components.charcoal
* @author     stk2k <stk2k@sazysoft.com>
* @copyright  2008 stk2k, sazysoft
*/

class Month
{
	var $year;
	var $month;

	/*
	 *    コンストラクタ
	 */
	public function __construct( $year, $month )
	{
		$this->year = $year ? $year : intval(date("Y"));
		$this->month = $month ? $month : intval(date("n"));
	}

	/*
	 * 年を取得
	 */
	public function getYear(){
		return $this->year;
	}

	/*
	 * 月を取得
	 */
	public function getMonth(){
		return $this->month;
	}

	/*
	 * 次の月を取得
	 */
	public function next(){
		$year = $this->year;
		$month = $this->month + 1;

		if ( $month > 12 ){
			$year ++;
			$month = 1;
		}

		return new Month( $year, $month );
	}

	/*
	 * 前の月を取得
	 */
	public function prev(){
		$year = $this->year;
		$month = $this->month - 1;

		if ( $month < 1 ){
			$year --;
			$month = 12;
		}

		return new Month( $year, $month );
	}


}

/**
* カレンダーコンポーネント
*
* PHP version 5
*
* @package    components/charcoal
* @author     stk2k <stk2k@sazysoft.com>
* @copyright  2008 stk2k, sazysoft
*/

class Calendar implements IComponent
{

	/*
	 *    コンストラクタ
	 */
	public function __construct()
	{
	}

	/*
	 * コンポーネント名を取得
	 */
	public function getComponentName(){
		return "Calendar Component";
	}

	/*
	 * コンポーネントを初期化する
	 */
	public function initComponent( $component_config ){

	}

	/*
	 * カレンダーを作成
	 */
	public function createCalendar( Month $month ){


//print "year:$year month:$month<BR>";

		// 月の初め
		$month_start_day = mktime( 0, 0, 0, $month->getMonth(), 1, $month->getYear() );

		// 来月の初め
		$next_month_start_day = strtotime("+1 month",$month_start_day);

//print "month_start_day:$month_start_day next_month_start_day:$next_month_start_day<BR>";

		// １週〜５週まで
		$week = array();
		$week_array = array();
		for ( $day = $month_start_day; $day < $next_month_start_day; $day++ ){
//print "day:$day next_month_start_day:$next_month_start_day<BR>";
			// 曜日
			$w = date( "w", $day );
			// 日を追加
			$week[$w] = array(
						"text" => date("j",$day),
						"holiday" => FALSE
					);
			// 土曜日なら、次の週を追加
			if ( $w == 6 ){
				$week_array[] = $week;
				$week = array();
			}
			// 次の日
			$day = strtotime("+1 day",$day);
		}
		$week_array[] = $week;


		return $week_array;
	}


}

?>
