<?php
$template = <<<END
<section class="calender">
<div class="calender_main">
<!--{def item/title}-->
<h2>{val item/title}</h2>
<!--{/def}-->
<!--{def item/week}-->
<table class="cal3">
<caption><a href="{val item/link}">{val item/m}月</a></caption>
<!--{each item/week}-->
<tr>
<th>{val item/week/d}({val item/week/w})</th>
<td>
<!--{each item/week/info}-->
<!--{def item/week/info/link}-->
<a href="{val item/week/info/link}">{val item/week/info/text}</a>
<!--{/def}-->
<!--{ndef item/week/info/link}-->
{val item/week/info/text}
<!--{/ndef}-->
<!--{/each}-->
</td>
</tr>
<!--{/each}-->
</table>
<ul class="cal btn-cal">
<li><a href="{val item/prev_ymd}">&lt; 前の週</a></li>
<li><a href="{val item/next_ymd}">次の週 &gt;</a></li>
</ul>
<!--{/def}-->
<!--{def item/month}-->
<table class="cal2">
<caption>{val item/y}年{val item/m}月</caption>
<tr>
<th>日</th>
<th>月</th>
<th>火</th>
<th>水</th>
<th>木</th>
<th>金</th>
<th>土</th>
</tr>
<!--{each item/month/week}-->
<tr>
<!--{each item/month/week/day}-->
<td>{val item/month/week/day/d}<br>
<!--{each item/month/week/day/info}-->
<!--{def item/month/week/day/info/link}-->
<a href="{val item/month/week/day/info/link}">{val item/month/week/day/info/text}</a>
<!--{/def}-->
<!--{ndef item/month/week/day/info/link}-->
{val item/month/week/day/info/text}
<!--{/ndef}-->
<!--{/each}-->
</td>
<!--{/each}-->
</tr>
<!--{/each}-->
</table>
<ul class="cal btn-cal">
<!--{def item/prev_ym}-->
<li><a href="{val item/prev_ym}">&lt; 前月</a></li>
<!--{/def}-->
<!--{def item/next_ym}-->
<li><a href="{val item/next_ym}">次月 &gt;</a></li>
<!--{/def}-->
</ul>
<!--{/def}-->
<!--{def item/year}-->
<!--{each item/year}-->
<div style="float:left;margin:20px;width:300px;height:270px">
<table class="cal1">
<caption><a href="{val item/year/link}">{val item/year/m}月</a></caption>
<tr>
<th>日</th>
<th>月</th>
<th>火</th>
<th>水</th>
<th>木</th>
<th>金</th>
<th>土</th>
</tr>
<!--{each item/year/month/week}-->
<tr>
<!--{each item/year/month/week/day}-->
<td>
<!--{def item/year/month/week/day/link}-->
<a href="{val item/year/month/week/day/link}">{val item/year/month/week/day/d}</a>
<!--{/def}-->
<!--{ndef item/year/month/week/day/link}-->
{val item/year/month/week/day/d}
<!--{/ndef}-->
</td>
<!--{/each}-->
</tr>
<!--{/each}-->
</table>
</div>
<!--{/each}-->
<!--{/def}-->
</div>
<!--/.main-->
</section>
<!-- ポップアップ要素 -->
<div class="popup-maru overlay" id="popup_{val id}" style="display: none;">
<div>
<h3 id="{val id}_title">タイトル</h3>
<p id="{val id}_contents">ポップアップの外側をクリックした場合も閉じるようにしました。</p>
<p><img src="" alt="" id="{val id}_image"></p>
<p class="c btn1-maru" id="{val id}_link"><a href="" id="{val id}_url">詳しくはこちら</a></p>
<i class="fa-solid fa-xmark close-maru"></i>
</div>
</div>
</section>
<script>
function popup(date, title, contents, url, image)
{
	$('#{val id}_title').html(date + "<br>" + title);
	$('#{val id}_contents').html(contents);
	if (url != '') {
		$('#{val id}_url').attr('href', url);
	} else {
		$('#{val id}_link').hide();
	}
	$('#{val id}_image').attr('src', image);
    $('#popup_{val id}').fadeIn(); // ポップアップを表示
}
</script>
END;

$block_calender = array(
	"title" => "カレンダー",
	"name" => "calender",			// 名称、重複すると正しく動作しません
	"template" => $template,		// ブロックのテンプレート
	"css_file" => "calender.css",
	"input" => array(
		"help" => array(
			"title" => "使い方",
			"type" => "comment",
			"link" => DOCUMENT1 . "/?act=block_calender",
			"class" => "notice",
		),
		"title" => array(
			"title" => "タイトル",
			"type" => "text",
		),
		"kind" => array(
			"title" => "種類",
			"type" => "select",
			"list" => array(
				"ym" => "月間",
				"ymd" => "週間",
				"y" => "年間",
			),
		),
		"popup" => array(
			"title" => "ポップアップ",
			"type" => "select",
			"list" => array(
				"" => "使わない",
				"1" => "使う",
			),
		),
	),
	"repeat" => array(
//		"image", "link",
	),
	"func" => "func_calender",		// 処理プログラム（指定がない物は標準処理）
);

block_add($block_calender);

function func_calender($def, $info, $act="")
{
	$wstr = array("日", "月", "火", "水", "木", "金", "土"); 

	if (!$act) {
		$act = $_REQUEST["act"];
	}
	$y = $_REQUEST["y"];
	$m = $_REQUEST["m"];
	$d = $_REQUEST["d"];
	$kind = $_REQUEST["t"];

	if (!$kind) {
		$kind = $info["kind"];
	//	$kind = "y";	// 年間
	//	$kind = "ym";	// 月間
	//	$kind = "ymd";	// 週間
	}
	if (!$y) {
		$y = date('Y');
	}
	if (!$m) {
		if ($y == date('Y')) {
			$m = date('m');
		} else {
			$m = 1;
		}
	}
	if (!$d) {
		if (($y == date('Y'))&&($m == date('m'))) {
			$d = date('d');
		} else {
			$d = 1;
		}
	}
	$info["y"] = $y;
	$info["m"] = $m;
	$info["d"] = $d;
	// 月間
	if ($kind == "ym") {
		// ym
		$prev_t = mktime(0, 0, 0, $m - 1, 1, $y);	// 前月
		$next_t = mktime(0, 0, 0, $m + 1, 1, $y);	// 次月
		$info["prev_ym"] = "./?act={$act}&t={$kind}&y=" . date("Y", $prev_t) . "&m=" . date("m", $prev_t);
		$info["next_ym"] = "./?act={$act}&t={$kind}&y=" . date("Y", $next_t) . "&m=" . date("m", $next_t);
		//
		list($ym, $s, $e) = cal_month($y, $m);
		// 予定の埋め込み
		$list = get_newinfo(100, INFO_SCHEDULE, $s, $e);
		// カレンダーに予定を書き込む
		foreach ($ym["week"] as $wk => $week) {
			foreach ($week["day"] as $dk => $day) {
				foreach ($list as $val) {
					unset($v);
					if (strtotime($val["news_date"]) == strtotime($day["ymd"])) {
						if ($val["image"]) {
							$f = Image::getData($val["image"]);
							if ($f) {
								$val["image"] = "user_data/" . $f["save_name"];
							}
						}
						$v["text"] = $val["title"] . "\n";	// 見出し
						//if ($val["url"]) {
							if ($info["popup"]) {
								$v["link"] = "javascript:popup('{$val["news_date"]}', '{$val["title"]}', '{$val["contents"]}', '{$val["url"]}', '{$val["image"]}')";
							} else if ($val["url"]) {
								$v["link"] = $val["url"];
							}
						//}
					}
					if ($v) {
						$ym["week"][$wk]["day"][$dk]["info"][] = $v;
					}
				}
			}
		}
		$info["month"] = $ym;
	}
	// 週間
	if ($kind == "ymd") {
		list($week, $s, $e) = cal_week($y, $m, $d);
		$info["week"] = $week;
		$prev_t = mktime(0, 0, 0, $m, $d - 7, $y);	// 前週
		$next_t = mktime(0, 0, 0, $m, $d + 7, $y);	// 次週
		$info["prev_ymd"] = "./?act={$act}&t={$kind}&y=" . date("Y", $prev_t) . "&m=" . date("m", $prev_t) . "&d=" . date("d", $prev_t);
		$info["next_ymd"] = "./?act={$act}&t={$kind}&y=" . date("Y", $next_t) . "&m=" . date("m", $next_t) . "&d=" . date("d", $next_t);
		// 予定の埋め込み
		$list = get_newinfo(100, INFO_SCHEDULE, $s, $e);
		// カレンダーに予定を書き込む
		foreach ($week as $w => $day) {
			foreach ($list as $val) {
				unset($v);
				if (strtotime($val["news_date"]) == strtotime($day["ymd"])) {
					$v["text"] = $val["title"] . "\n";	// 見出し
					if ($val["image"]) {
						$f = Image::getData($val["image"]);
						if ($f) {
							$val["image"] = "user_data/" . $f["save_name"];
						}
					}
					$v["text"] = $val["title"] . "\n";	// 見出し
					//if ($val["url"]) {
						if ($info["popup"]) {
							$v["link"] = "javascript:popup('{$val["news_date"]}', '{$val["title"]}', '{$val["contents"]}', '{$val["url"]}', '{$val["image"]}')";
						} else if ($val["url"]) {
							$v["link"] = $val["url"];
						}
					//}
				}
				if ($v) {
					$week[$w]["info"][] = $v;
				}
			}
		}
		$info["week"] = $week;
		$info["link"] = "./?act={$act}&t=ym&y={$y}&m={$m}";
	}
	// 年間
	if ($kind == "y") {
		list($year, $s, $e) = cal_year($y, $m, $act);
		// 予定の埋め込み
		$list = get_newinfo(100, INFO_SCHEDULE, $s, $e);
		// 予定のある日一覧作成
		$sched = array();
		foreach ($list as $val) {
			$t = strtotime($val["news_date"]);
			$sched[$t] = $val;
		}
		// 予定のある日にリンクを設定
		foreach ($year as $y => $val) {
			foreach ($val["month"]["week"] as $w => $val2) {
				foreach ($val2["day"] as $d => $val3) {
					if ($sched[strtotime($val3["ymd"])]) {
						$t = strtotime($val3["ymd"]);
						$yy = date('Y', $t);
						$mm = date('m', $t);
						$dd = date('d', $t);
						$year[$y]["month"]["week"][$w]["day"][$d]["link"] = "./?act={$act}&t=ymd&y={$yy}&m={$mm}&d={$dd}";
					}
				}
			}
		}
		$info["year"] = $year;
	}
	return $info;
}

// 1年分のカレンダー作成
function cal_year($y, $m = 1, $act="")
{
	$day_s = "";	// 開始日
	$day_e = "";	// 終了日
	$year = array();
	for ($i = 1; $i < 13; $i++) {
		list($item["month"], $s, $e) = cal_month($y, $m);
		if ($day_s == "") {
			$day_s = $s;
		}
		$day_e = $e;
		//
		$item["m"] = $m;
		$item["link"] = "./?act={$act}&t=ym&y={$y}&m={$m}";
		$year[] = $item;
		$m += 1;
		if ($m > 12) {
			$m = 1;
			$y += 1;
		}
	}
	return array($year, $day_s, $day_e);
}

// 1ヶ月分のカレンダー作成
function cal_month($y, $m)
{
	global $wstr;

	$cal = array();
	$day_s = "";	// 開始日
	$day_e = "";	// 終了日
	$t = mktime(0, 0, 0, $m, 1, $y);
	$w = date('w', $t);
	$t = mktime(0, 0, 0, $m, 1-$w, $y);	// 第一週日曜日
	$month = array();
	while (1) {
		$week = array();
		// 1週間
		for ($w = 0; $w < 7; $w++) {
			$n["y"] = date('Y', $t);
			$n["m"] = date('m', $t);
			$n["d"] = date('j', $t);
			$n["w"] = $wstr[$w];
			$n["ymd"] = date('Y-m-d', $t);
			if ($day_s == "") {
				$day_s = $n["ymd"];
			}
			$day_e = $n["ymd"];
			$t += 86400;
			$week["day"][$w] = $n;
		}
		$month["week"][] = $week;
		if ($m < date('m', $t)) break;	// 終わり
		if ($y != date('Y', $t)) break;
	}
	return array($month, $day_s, $day_e);
}

// 1週間のカレンダー作成($y/$m/$dを含む週)
function cal_week($y, $m, $d)
{
	$wstr = array("日", "月", "火", "水", "木", "金", "土"); 

	$day_s = "";	// 開始日
	$day_e = "";	// 終了日
	$cal = array();
	$t = mktime(0, 0, 0, $m, $d, $y);
	$w = date('w', $t);
	$t = mktime(0, 0, 0, $m, $d-$w, $y);
	for ($w = 0; $w < 7; $w++) {
		$n["y"] = date('Y', $t);
		$n["m"] = date('m', $t);
		$n["d"] = date('j', $t);
		$n["w"] = $wstr[$w];
		$n["ymd"] = date('Y-m-d', $t);
		if ($day_s == "") {
			$day_s = $n["ymd"];
		}
		$t += 86400;
		$cal[$w] = $n;
	}
	$day_e = $n["ymd"];
	return array($cal, $day_s, $day_e);
}
