Location: PHPKode > scripts > Kemana Directory > module/kcalendar/main.php
<?php
// --- calendar
// get event for a date (2007-12-25)
function get_event ($date)
{
	global $event_cache, $tpl_section;

	if (array_key_exists ($date, $event_cache))
	{
		$tmp = array ();
		$tmp['cid'] = random_str (3);
		$tmp['events'] = $event_cache[$date];
		$events = quick_tpl ($tpl_section['kcal_event'], $tmp);
		return $events;
	}
	else
		return '';
}

$sql_today = date ('Y-m-d');
$foo = date_param ('dt', 'get');
$dd = '01';
$mm = substr ($foo, 5, 2);
$yy = substr ($foo, 0, 4);

if (!checkdate ($mm, $dd, $yy))
{
	$foo = $sql_today;
	$mm = substr ($foo, 5, 2);
	$yy = substr ($foo, 0, 4);
}
$timestamp = mktime (0, 0, 0, $mm, $dd, $yy);

// cached for better perfomance
$output = qcache_get ('kcalendar_'.$timestamp);
if (!$output)
{
	// tpl
	load_section ('mod', 'module_kcalendar.tpl');
	$tpl = load_tpl ('var', $tpl_section['kcalendar']);
	$txt['block_date'] = '';

	// data definition
	$day_name_def[0] = $lang['datetime']['Sunday'];
	$day_name_def[1] = $lang['datetime']['Monday'];
	$day_name_def[2] = $lang['datetime']['Tuesday'];
	$day_name_def[3] = $lang['datetime']['Wednesday'];
	$day_name_def[4] = $lang['datetime']['Thursday'];
	$day_name_def[5] = $lang['datetime']['Friday'];
	$day_name_def[6] = $lang['datetime']['Saturday'];

	$idx_def[0] = 'sun';
	$idx_def[1] = 'mon';
	$idx_def[2] = 'tue';
	$idx_def[3] = 'wed';
	$idx_def[4] = 'thu';
	$idx_def[5] = 'fri';
	$idx_def[6] = 'sat';

	// init
	$day_one_idx = date ('w', mktime (0, 0, 0, $mm, 1, $yy));
	$day_of_year = date ('z', $timestamp);
	$num_day = date ('t', $timestamp);
	$month_name = date ('F', $timestamp);
	if (strlen ($mm) < 2) $mm = "0$mm";

	// prev days
	if ($mm == 1)
		// even though PHP recognize month -1 as 12 previous year, we need to be cautious
		$num_day_prev = date ('t', mktime (0, 0, 0, 12, $dd, $yy-1));
	else
		$num_day_prev = date ('t', mktime (0, 0, 0, $mm-1, $dd, $yy));

	// events
	$event_cache = $tmp = array ();

	// 1. Find cf with 'event' field type
	$res = sql_query ("SELECT dir_id, field_id FROM ".$db_prefix."cf_define WHERE field_type='event'");
	while ($row = sql_fetch_array ($res))
	{
		// -- build field name, end date, recur type fields, start date, end date
		$fid = $row['field_id'];
		$eid = $row['field_id'].'_end';
		$rid = $row['field_id'].'_recur';
		$s1 = "$yy-$mm-01";
		$f1 = "$yy-$mm-$num_day";
		
		// -- find events belongs to this month, and events span several months and include this month
		$sql = "($fid >= '$s1' AND $fid <= '$f1')";
		$sql .= " OR "; 	// events belong to this month
		$sql .= "(($eid >= '$s1') AND ($fid <= '$f1'))";		// events from previous months that still ongoing (end this month or later) AND events for next months that start from now
		$res2 = sql_query ("SELECT item_id, $fid, $eid, $rid FROM ".$db_prefix."cf_value AS t1 JOIN ".$db_prefix."link AS t2 USING (item_id) WHERE ($fid != '0000-00-00') AND ($sql) AND t2.status='O'");
		while ($row2 = sql_fetch_array ($res2)) 
		{
			// get recur type, start date, end date
			$recur = $row2[$rid];
			$sd = $row2[$fid];
			$fd = $row2[$eid];
			$stmp = $ftmp = FALSE;

			switch ($recur)
			{
				// get yearly!
				case 'y':
					// create event date with current year as event
					if (substr ($sd, 5, 2) == $mm) $stmp = $ftmp = "$yy-".substr ($sd, 5, 2)."-".substr ($sd, -2);
				break;
				
				// get monthly!
				case 'm':
					// create event date with current year & month as event
					$stmp = $ftmp = "$yy-$mm-".substr ($sd, -2);
				break;
				
				// get weekly!
				case 'w':
					// first, find what day is it? mon, sat, wed?
					$d = $sd;
					$ts = mktime (0, 0, 0, substr ($d, 5, 2), substr ($d, -2), substr ($d, 0, 4));
					$ww = date ('w', $ts);
					$www = $ww - $day_one_idx + 1;

					// then create event starting from the date to end of month
					if (($yy == substr ($sd, 0, 4)) && ($mm == substr ($sd, 5, 2))) $j = substr ($d, -2); else $j = $www;
					if (($yy == substr ($fd, 0, 4)) && ($mm == substr ($fd, 5, 2))) $k = substr ($fd, -2); else $k = $num_day;
					for ($i = $j; $i <= $k; $i = $i + 7) 
					{
						if (strlen ($i) == 1) $d = '0'.$i; else $d = $i;
						$tmp[] = $row2['item_id'].','."$yy-$mm-$d";
					}
				break;
			
				// get daily!
				case 'd':
					// this is pretty easy
					$stmp = $sd;
					$ftmp = $fd;
				break;
				
				// get once!
				case 'x':
					if (substr ($sd, 5, 2) == $mm) $stmp = $ftmp = $sd;
				break;
			}
			
			// --- create tmp
			// --- this is pretty easy
			if ($stmp)
			{
				if (($yy == substr ($stmp, 0, 4)) && ($mm == substr ($stmp, 5, 2))) $j = substr ($stmp, -2); else $j = 1;
				if (($yy == substr ($ftmp, 0, 4)) && ($mm == substr ($ftmp, 5, 2))) $k = substr ($ftmp, -2); else $k = $num_day;
				for ($i = $j; $i <= $k; $i++) 
				{
					if (strlen ($i) == 1) $d = '0'.$i; else $d = $i;
					$tmp[] = $row2['item_id'].','."$yy-$mm-$d";
				}
			}
		}
	}

	// 2. Remove duplicate entries, and create event list (above lines are only to create date & idx)
	$tmp = array_unique ($tmp);
	foreach ($tmp as $k => $v)
	{
		$f = explode (',', $v);
		$item_id = $f[0];
		$d = $f[1];
		if (!isset ($event_cache[$d])) $event_cache[$d] = '';
		$res3 = sql_query ("SELECT item_id, title, short_note FROM ".$db_prefix."link WHERE item_id='$item_id' AND status='O' LIMIT 1");
		while ($row3 = sql_fetch_array ($res3))
		{
			$foo = array ();
			$foo['item_id'] = $row3['item_id'];
			$foo['title'] = $row3['title'];
			$foo['summary'] = $row3['short_note'];
			if ($config['enable_adp'] == 3) $foo['item_id'] = create_seo_url ($foo['item_id'], $foo['title']);
			$event_cache[$d] .= quick_tpl ($tpl_section['kcal_event_item'], $foo);
		}
	}

	// 3. create previous month events
	$start = $num_day_prev - $day_one_idx;
	for ($f = 0; $f <= $day_one_idx; $f++)
	{
		$start++;
		$ee = $idx_def[$f].'_event';
		$row[$idx_def[$f]] = "<span class=\"prevnext_date\">$start</span>";	
		$row[$ee] = '';
	}

	// 4. create current month events
	for ($i = 1; $i <= $num_day; $i++)
	{
		// $j = day index (0..6); $i = day (date?)
		$j = ($day_one_idx + $i - 1) % 7;
		$row[$idx_def[$j]] = $i;
		
		// today
		if (strlen ($i) < 2) $dd = "0$i"; else $dd = $i;

		$foo = "$yy-$mm-$dd";;
		if ($foo == $sql_today) $row[$idx_def[$j]] = "<span style=\"color:#00f; text-decoration:underline; font-weight:bolder\">$i</span>";
		
		// event
		$ee = $idx_def[$j].'_event';
		$row[$ee] = get_event ($foo);
		
		// if $j = saturday -> full block
		if ($j == 6) 
		{
			$txt['block_date'] .= quick_tpl ($tpl_block['date'], $row);
			for ($f = 0; $f <= 6; $f++) $row[$idx_def[$f]] = '';
		}
	}

	// 5. create next month events
	if ($j < 6) 
	{
		// next month
		$doh = 0; $j++;
		for ($i = $j; $i <= 6; $i++)
		{
			$doh++;
			$ee = $idx_def[$i].'_event';
			$row[$idx_def[$i]] = "<span class=\"prevnext_date\">$doh</span>";
			$row[$ee] = '';		
		}
		$txt['block_date'] .= quick_tpl ($tpl_block['date'], $row);
	}

	// 6. flush
	$txt['mm_name'] = $lang['datetime'][$month_name];
	$txt['yy_name'] = $yy;
	$txt['mm_today'] = substr ($sql_today, 5, 2);
	$txt['yy_today'] = substr ($sql_today, 0, 4);
	$txt['mm_next'] = ($mm == 12) ? 1 : $mm + 1;
	$txt['yy_next'] = ($mm == 12) ? $yy + 1 : $yy;
	$txt['mm_prev'] = ($mm == 1) ? 12 : $mm - 1;
	$txt['yy_prev'] = ($mm == 1) ? $yy - 1 : $yy;
	$txt['date_select'] = date_form ('dt', 2000, 1, 0, "$yy-$mm-$dd", 10);
	$output = quick_tpl ($tpl, $txt);
	
	// 7. save to cache
	qcache_update ('kcalendar_'.$timestamp, $output);
}

echo $output; die;
?>
Return current item: Kemana Directory