[CodeIgniter] カレンダークラスの拡張

application/libraries/MY_Calendar.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * MY Calendar Class
 *
 * カレンダーの拡張
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author
 * @link
 */
class MY_Calendar extends CI_Calendar {

	// ▼
	var $template_pattern = ''; // テンプレートのパターン ※MY_Calendar::set_template() 内にテンプレートを設定
	var $content_url_prefix = ''; // コンテンツのリンクURLの接頭辞
	var $content_url_flg = FALSE; // コンテンツのリンクを有効にするか否かの真偽値
	var $content_css = 'cal_content'; // コンテンツのCSS
	var $week_day_css_prefix = 'cal_week_day'; // 曜日を指定するCSSの接頭辞
	// ▲

	/**
	 * コンストラクタ
	 *
	 */
	// ▼
	public function __construct($config = array())
	{
		// 親クラスを継承
		parent::__construct($config);
	}
	// ▲

	// --------------------------------------------------------------------

	/**
	 * Generate the calendar
	 *
	 * @access	public
	 * @param	integer	the year
	 * @param	integer	the month
	 * @param	array	the data to be shown in the calendar cells
	 * @return	string
	 */
	function generate($year = '', $month = '', $data = array())
	{
		// Set and validate the supplied month/year
		if ($year == '')
			$year  = date("Y", $this->local_time);

		if ($month == '')
			$month = date("m", $this->local_time);

		if (strlen($year) == 1)
			$year = '200'.$year;

		if (strlen($year) == 2)
			$year = '20'.$year;

		if (strlen($month) == 1)
			$month = '0'.$month;

		$adjusted_date = $this->adjust_date($month, $year);

		$month	= $adjusted_date['month'];
		$year	= $adjusted_date['year'];

		// Determine the total days in the month
		$total_days = $this->get_total_days($month, $year);

		// Set the starting day of the week
		$start_days	= array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
		$start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];

		// Set the starting day number
		$local_date = mktime(12, 0, 0, $month, 1, $year);
		$date = getdate($local_date);
		$day  = $start_day + 1 - $date["wday"];

		while ($day > 1)
		{
			$day -= 7;
		}

		// Set the current month/year/day
		// We use this to determine the "today" date
		$cur_year	= date("Y", $this->local_time);
		$cur_month	= date("m", $this->local_time);
		$cur_day	= date("j", $this->local_time);

		$is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;

		// Generate the template data array
		$this->parse_template();

		// Begin building the calendar output
		$out = $this->temp['table_open'];
		$out .= "\n";

		$out .= "\n";
		$out .= $this->temp['heading_row_start'];
		$out .= "\n";

		// "previous" month link
		if ($this->show_next_prev == TRUE)
		{
			// Add a trailing slash to the  URL if needed
			$this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/",  $this->next_prev_url);

			$adjusted_date = $this->adjust_date($month - 1, $year);
			$out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
			$out .= "\n";
		}

		// Heading containing the month/year
		$colspan = ($this->show_next_prev == TRUE) ? 5 : 7;

		$this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);

		// ▼ テーブルの見出し行を「M月YYYY」形式から「YYYY年M月」形式に変更
		// $this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)." ".$year, $this->temp['heading_title_cell']);
		$this->temp['heading_title_cell'] = str_replace('{heading}', $year.'年'.(int)$month.'月', $this->temp['heading_title_cell']);
		// ▲

		$out .= $this->temp['heading_title_cell'];
		$out .= "\n";

		// "next" month link
		if ($this->show_next_prev == TRUE)
		{
			$adjusted_date = $this->adjust_date($month + 1, $year);
			$out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
		}

		$out .= "\n";
		$out .= $this->temp['heading_row_end'];
		$out .= "\n";

		// Write the cells containing the days of the week
		$out .= "\n";
		$out .= $this->temp['week_row_start'];
		$out .= "\n";

		$day_names = $this->get_day_names();

		for ($i = 0; $i < 7; $i ++)
		{
			// ▼ 見出し行に曜日ごとのCSSを指定
			// $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
			// ↓
				$class_i = ($start_day + $i) %7;
				$out .= str_replace('{css_week_day}', $this->week_day_css_prefix.$class_i, str_replace('{week_day}', $day_names[$class_i], $this->temp['week_day_cell']));
			// ▲
		}

		$out .= "\n";
		$out .= $this->temp['week_row_end'];
		$out .= "\n";

		// Build the main body of the calendar
		while ($day <= $total_days)
		{
			$out .= "\n";
			$out .= $this->temp['cal_row_start'];
			$out .= "\n";

			for ($i = 0; $i < 7; $i++)
			{
				// ▼ 各セルに曜日ごとのCSSを指定
				// $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
				// ↓
				$class_i = ($start_day + $i) %7;
				if ($is_current_month == TRUE AND $day == $cur_day)
				{
					$out .= str_replace('{css_week_day}', $this->week_day_css_prefix.$class_i, $this->temp['cal_cell_start_today']);
				}
				else
				{
					$out .= str_replace('{css_week_day}', $this->week_day_css_prefix.$class_i, $this->temp['cal_cell_start']);
				}
				// ▲

				if ($day > 0 AND $day <= $total_days)
				{
					// ▼ 下記1行を追加し、日付をゼロ詰めに変換
					$day2 = sprintf('%02d', $day);
					// ▲

					if (isset($data[$day]))
					{
						// Cells with content
						$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];

						// ▼ URLの日付の箇所をゼロ詰めに置換
						//$out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
						// ↓
						$out .= str_replace('{day2}', $day2, str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp)));
						// ▲
					}
					else
					{
						// Cells with no content
						$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
						// ▼ URLの日付の箇所をゼロ詰めに置換
						// $out .= str_replace('{day}', $day, $temp);
						// ↓
						$out .= str_replace('{day2}', $day2, str_replace('{day}', $day, $temp));
						// ▲
					}
				}
				else
				{
					// Blank cells
					$out .= $this->temp['cal_cell_blank'];
				}
				$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
				$day++;
			}

			$out .= "\n";
			$out .= $this->temp['cal_row_end'];
			$out .= "\n";
		}

		$out .= "\n";
		$out .= $this->temp['table_close'];

		return $out;
	}

	// --------------------------------------------------------------------

	/**
	 * Set Template Data
	 *
	 * This is used in the event that the user has not created their own template
	 *
	 * @access	public
	 * @return array
	 */
	// ▼
	function set_template()
	{
		// コンテンツのURLの接頭辞が指定されている場合
		switch ($this->template_pattern)
		{
			case 1:
				$arr = array (
					'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
					'heading_row_start' => '<tr>',
					'heading_previous_cell' => '<th><a href="{previous_url}"><<</a></th>',
					'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
					'heading_next_cell' => '<th><a href="{next_url}">>></a></th>',
					'heading_row_end' => '</tr>',
					'week_row_start' => '<tr>',
					'week_day_cell' => '<th class="{css_week_day}">{week_day}</th>',
					'week_row_end' => '</tr>',
					'cal_row_start' => '<tr>',
					'cal_cell_start' => '<td class="{css_week_day}">',
					'cal_cell_start_today' => '<td class="{css_week_day}">',
					'cal_cell_content' => '<span class="'.$this->content_css.'">{day}<br />{content}</span>',
					'cal_cell_content_today' => '<span class="'.$this->content_css.'"><strong>{day}</strong><br />{content}</span>',
					'cal_cell_no_content' => '{day}',
					'cal_cell_no_content_today' => '<strong>{day}</strong>',
					'cal_cell_blank' => ' ',
					'cal_cell_end' => '</td>',
					'cal_cell_end_today' => '</td>',
					'cal_row_end' => '</tr>',
					'table_close' => '</table>'
				);
				$arr2 = array(
					'cal_cell_content' => '<span class="'.$this->content_css.'">{day}<br /><a href="'.$this->content_url_prefix.'{day2}">{content}</a></span>',
					'cal_cell_content_today' => '<strong><span class="'.$this->content_css.'">{day}</strong><br /><a href="'.$this->content_url_prefix.'{day2}">{content}</a></span>',
				);
				return (! empty($this->content_url_flg)) ? array_merge($arr, $arr2) : $arr;
				break;
			default:
				return  array (
					'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
					'heading_row_start' => '<tr>',
					'heading_previous_cell' => '<th><a href="{previous_url}"><<</a></th>',
					'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
					'heading_next_cell' => '<th><a href="{next_url}">>></a></th>',
					'heading_row_end' => '</tr>',
					'week_row_start' => '<tr>',
					'week_day_cell' => '<th class="{css_week_day}">{week_day}</th>',
					'week_row_end' => '</tr>',
					'cal_row_start' => '<tr>',
					'cal_cell_start' => '<td class="{css_week_day}">',
					'cal_cell_start_today' => '<td class="{css_week_day}">',
					'cal_cell_content' => '<a href="{content}">{day}</a>',
					'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
					'cal_cell_no_content' => '{day}',
					'cal_cell_no_content_today'	=> '<strong>{day}</strong>',
					'cal_cell_blank' => ' ',
					'cal_cell_end' => '</td>',
					'cal_cell_end_today' => '</td>',
					'cal_row_end' => '</tr>',
					'table_close' => '</table>'
				);
				break;
		}
	}

	// ▲

	// --------------------------------------------------------------------

	/**
	 * Parse Template
	 *
	 * Harvests the data within the template {pseudo-variables}
	 * used to display the calendar
	 *
	 * @access	public
	 * @return	void
	 */
	function parse_template()
	{

		// ▼ テンプレートを指定
		// $this->temp = $this->default_template();
		// ↓
		$this->temp = $this->set_template();
		// ▲

		if ($this->template == '')
		{
			return;
		}

		$today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');

		foreach (array('table_open', 'table_close', 'heading_row_start', 'heading_previous_cell', 'heading_title_cell', 'heading_next_cell', 'heading_row_end', 'week_row_start', 'week_day_cell', 'week_row_end', 'cal_row_start', 'cal_cell_start', 'cal_cell_content', 'cal_cell_no_content',  'cal_cell_blank', 'cal_cell_end', 'cal_row_end', 'cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today') as $val)
		{
			if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match))
			{
				$this->temp[$val] = $match['1'];
			}
			else
			{
				if (in_array($val, $today, TRUE))
				{
					$this->temp[$val] = $this->temp[str_replace('_today', '', $val)];
				}
			}
		}
	}

}

// END MY_Calendar class

/* End of file MY_Calendar.php */
/* Location: ./application/libraries/MY_Calendar.php */

application/controllers/event.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Event extends CI_Controller {

	/**
	 * コンストラクタ
	 */
	function __construct()
	{
		// 親クラスの継承
		parent::__construct();
		// データベースクラスの読み込み
		$this->load->database();
		// URLヘルパの読み込み
		$this->load->helper(array('url'));
	}

	/**
	 * 月別イベントカレンダー
	 */
	function month($year = '', $month = '')
	{
		// 西暦年と月を取得
		if ($year == '' OR $month == '' OR ! checkdate((int)$month, 1, (int)$year))
		{
			$year = date('Y');
			$month = date('m');
		}

		// DBからイベントを取得
		$events = array();
		$this->db->query('SELECT * FROM event WHERE year = '.$this->db->escape($year).' AND month = '.$this->db->escape($month).' AND status = 1');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$events[$row['date']] = $row['title'];
			}
		}

		// カレンダーの設定
		$prefs = array(

			// CodeIgniterの既存の設定項目
			'template' => '', // テンプレート
			'next_prev_url' => '/event/month/', //
			'show_next_prev' => TRUE,
			'start_day' => 'sunday',

			// 拡張クラスの設定項目
			'template_pattern' => 1, // テンプレートのバターンを指定。テンプレートのパターンを増やす場合は、MY_Calendar::set_template() に記述。
			'content_url_prefix' => base_url()."event/day/{$year}/{$month}/", // コンテンツのリンクURLの接頭辞
			'content_url_flg' => TRUE, // コンテンツのリンクを有効にするか否かの真偽値
			'content_css' => 'event', // コンテンツのCSS
			'week_day_css_prefix' => 'week_day', // 曜日ごとのCSSの接頭辞

		);

		// カレンダーの生成
		$this->load->library('calendar', $prefs);
		$calendar = $this->calendar->generate($year, $month, $events);
		$this->load->view('event_month', array('calendar' => $calendar));
	}
}

/* End of file event.php */
/* Location: ./application/controllers/event.php */

application/views/event_month.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>カレンダー</title>
<style type="text/css">

a {
	color: #003399;
	background-color: transparent;
	font-weight: normal;
}
table {
	border-collapse: collapse;
	border: 1px solid #CCC;
}
th, td {
	text-align: center;
	border: 1px solid #CCC;
	width: 100px;
}
td {
	vertical-align: top;
	height: 100px;
}
.week_day6 {
	color: #06F;
}
.week_day0, .holiday {
	color: #F00;
}
span.event {
	color: #F00;
}

</style>
</head>
<body>

<h1>カレンダー</h1>
<?php echo $calendar; ?>

</body>
</html>
こちらもあわせてどうぞ
  1. [CodeIgniter] 国民の休日 & 振替休日 & 定休日 を取得するクラス
  2. [PHP] 日付・時刻用の関数
  3. [PHP] お天気WEBサービス
  4. [CodeIgniter] TinyMCEヘルパ(TinyMCE3.4.3.2 jQuery版用)
  5. [PHP] 改行コードの置換・削除
  6. [PHP] SEO検索順位
  7. [PHP] Ping送信
  8. [PHP] お天気サービス用の関数
HatenaGoogle BookmarksYahoo BookmarksFacebook

コメントする

*