<?
require_once('include/global.php');
require_once('include/diary_lib.php');
require_once('include/pager.php');
require_once('conf.inc.php');


// set
	$CFG['TITLE'] = '<b>일정관리</b> >> <b>월간일정</b>';
	if ($G_SYS[DB] == 'oracle') 
	{
		$db_substr = 'SUBSTR';
	} else 
	{
		$db_substr = 'SUBSTRING';
	}	// if()

	// [2006-07-19] 공용일정 구분
	set_diary_no_isshare('', $_GET['share']);


// 달력계산
	$_today = time();

	if ((!$stat_year)&&(!$stat_month))
	{
		$stat_year = date('Y', $_today);
		$stat_month = date('m', $_today);
	} else
	{
		$stat_month = sprintf('%02d', $stat_month);
	}	// if()

	$now_date = date('Y-m-d', $_today);		// 오늘 날짜
	$now_day = date('d', $_today);		// 오늘 일자

	$today = mktime(0, 0, 0, $stat_month, 1, $stat_year);
	$month_last_day = date('t', $today);		// 이번달 마지막 일자
	$today_week = date('w', $today);
	$_month_last_day_v = $month_last_day + $today_week;
	$month_last_day_v = $_month_last_day_v + (7 - ($_month_last_day_v % 7));
	// [2013-07-08] 2013-08 에 마지막 빈라인이 추가되는 문제 해결.
	if ($_month_last_day_v <= 35 && $month_last_day_v == 42)
		$month_last_day_v = 35;
	else if ($_month_last_day_v <= 28 && $month_last_day_v == 35)
		$month_last_day_v = 28;

	$startdate = $stat_year . "-" . $stat_month . "-01";
	//echo "<HR>startdate - " . $startdate;
	$enddate = $stat_year . "-" . $stat_month . "-" . $month_last_day;
	//echo "<HR>enddate - " . $enddate;

	$startdate_lunar = substr(solartolunar($startdate), 5, 5);
	$enddate_lunar = substr(solartolunar($enddate), 5, 5);

	$startdate_md = substr($startdate, 5, 5);
	$enddate_md = substr($enddate, 5, 5);

	// 이번달에서 음력표시부분.
	$thismonth_lunar = db_get_rows("SELECT solar_date, lunar_date FROM " . $TB['DIARY_LUNAR'] . " WHERE solar_date >= '" . $startdate . "' AND solar_date <= '" . $enddate . "' AND $db_substr(lunar_date,9,2) IN ('01','15')");


// data

	// search
		$q_where = '';
		if ($_GET['s_word'])
		{
			set_q_search($_GET['s_word']);
			$q_where = " AND ( di_subject LIKE '%" . $_GET['s_word'] . "%' OR di_content LIKE '%" . $_GET['s_word'] . "%' ) ";
			$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
		}	// if()

		if ($_GET['s_di_kind'])
		{
			$q_where .= " AND di.di_kind = '" . $_GET['s_di_kind'] . "' ";
		}	// if()

		// [2006-06-20] 분류선택시 기념일이 보이지 않는 문제.
		if ($q_where != '')
		{
			$q_where = " AND ( ( 1=1 ".$q_where." ) OR di.di_content = '공휴일' OR di.di_content = '기념일' ) ";			
		}	// if()


	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();

		// 매년(음력)되풀이 일정중 12월~1월사이의 일정일때.
		if (substr($startdate_lunar, 0, 2) == '12' && substr($enddate_lunar, 0, 2) == '01')
		{
			#$is_lunar_12_1 = true;
			$q_repeat_l = "
							($db_substr(di.di_startdate,6,5) <= '$startdate_lunar' AND $db_substr(di.di_enddate,6,5) >= '$startdate_lunar')
							OR ($db_substr(di.di_startdate,6,5) <= '12-31' AND $db_substr(di.di_enddate,6,5) >= '12-31')
							OR ($db_substr(di.di_startdate,6,5) >= '$startdate_lunar' AND $db_substr(di.di_enddate,6,5) <= '12-31')
							OR ($db_substr(di.di_startdate,6,5) <= '01-01' AND $db_substr(di.di_enddate,6,5) >= '01-01')
							OR ($db_substr(di.di_startdate,6,5) <= '$enddate_lunar' AND $db_substr(di.di_enddate,6,5) >= '$enddate_lunar')
							OR ($db_substr(di.di_startdate,6,5) >= '01-01' AND $db_substr(di.di_enddate,6,5) <= '$enddate_lunar')
				";
		} else
		{
			#$is_lunar_12_1 = false;
			$q_repeat_l = "
							($db_substr(di.di_startdate,6,5) <= '$startdate_lunar' AND $db_substr(di.di_enddate,6,5) >= '$startdate_lunar')
							OR ($db_substr(di.di_startdate,6,5) <= '$enddate_lunar' AND $db_substr(di.di_enddate,6,5) >= '$enddate_lunar')
							OR (
								$db_substr(di.di_startdate,6,5) >= '$startdate_lunar' AND $db_substr(di.di_enddate,6,5) <= '$enddate_lunar'
								AND $db_substr(di.di_startdate,6,5) <= $db_substr(di.di_enddate,6,5)
							)
				";
		}	// if()


		// [2012-04-13] 윤달이 포함된 경우 매년(음력) 되풀이 일정이 제대로 보이지 않던 오류 수정.
		$q = "SELECT count(*) FROM " . $TB['DIARY_LUNAR'] . " WHERE solar_date >= '" . $startdate . "' AND solar_date <= '" . $enddate . "' AND yun = '1'";
		$_lunar_cnt = db_get_one($q);
		if ($_lunar_cnt > 0)
		{
			$_lunar = db_get_rows("SELECT $db_substr(lunar_date, 6, 5) md FROM " . $TB['DIARY_LUNAR'] . " WHERE solar_date >= '" . $startdate . "' AND solar_date <= '" . $enddate . "' AND yun = '0'", true);

			if (sizeof($_lunar['md']) > 0)
			{
				$_in = implode("','", $_lunar['md']);
				$q_repeat_l .= " OR $db_substr(di.di_startdate,6,5) IN ('".$_in."') ";
				$q_repeat_l .= " OR $db_substr(di.di_enddate,6,5) IN ('".$_in."') ";
			}	// if()
		}	// if()






		$sql = "
			SELECT
				*
			FROM
				" . $TB['DIARY'] . " di
			WHERE
				(
					(
						(
							(di.di_startdate <= '$startdate' AND di.di_enddate >= '$startdate')
							OR (di.di_startdate <= '$enddate' AND di.di_enddate >= '$enddate')
							OR (di.di_startdate >= '$startdate' AND di.di_enddate <= '$enddate')
						)
						AND di.di_repeat != 'Y'
						AND di.di_repeat != 'L'
					)
					OR
					(
						(
							($db_substr(di.di_startdate,6,5) <= '$startdate_md' AND $db_substr(di.di_enddate,6,5) >= '$startdate_md')
							OR ($db_substr(di.di_startdate,6,5) <= '$enddate_md' AND $db_substr(di.di_enddate,6,5) >= '$enddate_md')
							OR ($db_substr(di.di_startdate,6,5) >= '$startdate_md' AND $db_substr(di.di_enddate,6,5) <= '$enddate_md')
						)
						AND di.di_repeat = 'Y'
					)
					OR
					(
						(
							" . $q_repeat_l . "
						)
						AND di.di_repeat = 'L'
					)
				)
				AND ".get_diary_group_where_month()."
				" . $q_where . "
			ORDER BY
				di.di_startdate, di.di_starttime, di.di_enddate, di.di_endtime
			";
		#vd($sql);
		$rows = db_get_rows($sql, true);
		$rows_cnt = getarraycount($rows['di_subject']);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

	// data
		/*
		for ($i=0; $i<$rows_cnt; $i++)
		{
			$rows['di_content'][$i] = han_substr($rows['di_content'][$i], 60);
			$rows['di_starttime'][$i] = ' '.substr($rows['di_starttime'][$i], 0, 5);
			$rows['di_endtime'][$i] = ' '.substr($rows['di_endtime'][$i], 0, 5);
		}	// for()
		*/


	// set - 일별 예약내역
		$view_day_no = array();
		$view_day = array();
		$view_day_kind = array();
		$view_day_top = array();
		$view_isholiday = array();

		// 음력 1일, 15일 표시
		for ($i=0; $i<sizeof($thismonth_lunar); $i++)
		{
			$_day = str_replace('-', '', substr($thismonth_lunar[$i]['solar_date'], 0, 10));
			$view_day_top[$_day] = date('n.j', strtotime($thismonth_lunar[$i]['lunar_date']));
		}	// for()

		for ($i=0; $i<$rows_cnt; $i++)
		{
			#echo "<HR>__" . $rows['di_repeat'][$i] . '/' . $rows['di_startdate'][$i] . '/' . $rows['di_enddate'][$i];
			if ($rows['di_repeat'][$i] == 'L')		// 매년(음력) 되풀이일때 디비에 저장된 음력을 양력으로 변환후 계산함.
			{
				// [2006-12-08]
				$_sd_md = substr($rows['di_startdate'][$i], 5, 5);
				$rows['di_startdate'][$i] = lunartosolar(($stat_year - 1).'-'.$_sd_md);
				if (substr($rows['di_startdate'][$i], 0, 7) != $stat_year.'-'.$stat_month)
					$rows['di_startdate'][$i] = lunartosolar($stat_year.'-'.$_sd_md);

				$_ed_md = substr($rows['di_enddate'][$i], 5, 5);
				$rows['di_enddate'][$i] = lunartosolar(($stat_year - 1).'-'.$_ed_md);
				if (substr($rows['di_enddate'][$i], 0, 7) != $stat_year.'-'.$stat_month)
					$rows['di_enddate'][$i] = lunartosolar($stat_year.'-'.$_ed_md);
				/*
				if ($is_lunar_12_1 && substr($_sd_md, 0, 2) == '12')
				{
					$rows['di_startdate'][$i] = lunartosolar(($stat_year - 1).'-'.$_sd_md);
				} else
				{
					$rows['di_startdate'][$i] = lunartosolar($stat_year.'-'.$_sd_md);
				}	// if()

				if ($is_lunar_12_1 && substr($_ed_md, 0, 2) == '12')
				{
					$rows['di_enddate'][$i] = lunartosolar(($stat_year - 1).'-'.$_ed_md);
				} else
				{
					$rows['di_enddate'][$i] = lunartosolar($stat_year.'-'.$_ed_md);
				}	// if()
				*/
			} else if ($rows['di_repeat'][$i] == 'Y')		// 매년(양력) 되풀이일때 이번연도로 변환후 계산함.
			{
				$rows['di_startdate'][$i] = $stat_year.'-'.substr($rows['di_startdate'][$i], 5, 5);
				$rows['di_enddate'][$i] = $stat_year.'-'.substr($rows['di_enddate'][$i], 5, 5);
			}	// if()
			#echo "<HR>___" . $rows['di_repeat'][$i] . '/' . $rows['di_startdate'][$i] . '/' . $rows['di_enddate'][$i];

			$_sd_sp = split("-", $rows['di_startdate'][$i]);
			$_sd_ts = mktime(0, 0, 0, $_sd_sp[1], $_sd_sp[2], $_sd_sp[0]);

			$_ed_sp = split("-", $rows['di_enddate'][$i]);
			$_ed_ts = mktime(0, 0, 0, $_ed_sp[1], $_ed_sp[2], $_ed_sp[0]);

			// 일정기간
			$diaryday = (($_ed_ts - $_sd_ts) / (3600*24)) + 1;

			for ($j=1; $j<=$diaryday; $j++)
			{
				$_thisdate_ts = mktime(0, 0, 0, $_sd_sp[1], ($_sd_sp[2] + $j - 1), $_sd_sp[0]);
				$thisdate = date("Y-m-d", $_thisdate_ts);
				#$thisday = date("j", $_thisdate_ts);
				$thisday = date("Ymd", $_thisdate_ts);
				#$thisdayofweek = date("w", $_thisdate_ts);

				// 일정기간이 1일 이상일 경우, 선택된 달의 내역만 가져옴.
				if ( $diaryday == 1 ||  (($thisdate >= $startdate)&&($thisdate <= $enddate)) )
				{
					if ($rows['di_kind'][$i] == '0' && ($rows['di_content'][$i] == '공휴일' || $rows['di_content'][$i] == '기념일'))		// 공휴일/기념일일때
					{
						#echo "<HR>공휴일/기념일 : " . $thisday . '/' . $diaryday . '/' . $thisdate . '/' . $rows['di_subject'][$i];
						if ($rows['di_content'][$i] == '공휴일')
						{
							$view_isholiday[$thisday] = true;
						}	// if()
						if ($view_day_top[$thisday] != '')
							$view_day_top[$thisday] .= ' / ';
						$view_day_top[$thisday] .= $rows['di_subject'][$i];
					} else
					{
						$view_day_no[$thisday][] = $rows['di_no'][$i];
						$view_day[$thisday][] = $rows['di_subject'][$i];
						$view_day_kind[$thisday][] = $rows['di_kind'][$i];
					}	// if()
				}	// if()
			}	// for()
		}	// for()

		$back_url = urlencode($_SERVER[REQUEST_URI]);

	// 날짜선택
		$select_year = '';
		$opt_year = $stat_year - 5;
		for ($i=0; $i<=10; $i++)
		{
			$select_year .= '<option value="'.($opt_year + $i).'"';
			if ($opt_year + $i == $stat_year)
				$select_year .= ' selected';
			$select_year .= '>'.($opt_year + $i).'년</option>';
		}	// for()

		$select_month = '';
		for ($i=1; $i<=12; $i++)
		{
			$select_month .= '<option value="'.$i.'"';
			if ($i == $stat_month)
				$select_month .= ' selected';
			$select_month .= '>'.$i.'월</option>';
		}	// for()

		$_mkt = mktime(0, 0, 0, $stat_month-1, 1, $stat_year);
		$select_prev_y = date('Y', $_mkt);
		$select_prev_m = date('n', $_mkt);

		$_mkt = mktime(0, 0, 0, $stat_month+1, 1, $stat_year);
		$select_next_y = date('Y', $_mkt);
		$select_next_m = date('n', $_mkt);




// skin view
	nskinset('webmail', 'diary_month.html');
	define('NMAIL_SKIN_MAIN_TOP_FILE', nskindir('webmail').'/diary_top.inc.html');		// 상단메뉴
	include($NSKIN['view_layout']);


require_once('include/global_end.php');
?>