<?php
/********************************************************************
Product    : MiniCalendar
Author     : Chris Gaebler
Date       : 8 October 2010
Version    : 2.03
Copyright  : Les Arbres Design 2010
Contact    : http://extensions.lesarbresdesign.info
Licence    : GNU General Public License
Description: Displays a calendar in a module position
*********************************************************************/

defined('_JEXEC') or die('Restricted access');

//---------------------------------------------------------------------------------------------
// Draw a calendar for one month in any language
// $link is blank for no links, or a url ready to append 'p' for previous or 'n' for next
//
function make_calendar($year, $month, $link = '', $encoding, $day_name_length, $start_day, $weekHdr)
{
	$current_year = date('Y');
	$current_month = date('m');
	$current_day = date('d');
	$num_columns = 7;										// without week numbers, we have 7 columns
	if (($weekHdr != '') and ($start_day == 1))				// only show week numbers if start day is Monday
		$num_columns = 8;
	else
		$weekHdr = '';										// if start day not Monday, don't do week numbers
	
	echo "\n".'<table class="mod_minical_table" align="center" cellspacing="0">'."\n";
	echo "\n<tr>";
	
// draw the month and year heading in the current language

	$month_time = mktime(0,0,0,$month,1,$year);
	echo '<th colspan="'.$num_columns.'">';
	if ($link != '')
		echo '<a href="'.$link.'p"><span class="mod_minical_left">&laquo;</span></a>&nbsp;&nbsp;';
	$month_string = strftime('%B %Y',$month_time);
	if ($encoding)
		echo utf8_encode($month_string);
	else
		echo $month_string;
	if ($link != '')
		echo '&nbsp;&nbsp;<a href="'.$link.'n"><span class="mod_minical_right">&raquo;</span></a>';
	echo '</th>';
	echo '</tr>';
	
// draw the day names heading in the current language

	if ($day_name_length > 0)
		{
		echo "\n<tr>";
		if ($weekHdr != '')
			echo "<th>".$weekHdr."</th>";
		for ($i = 0; $i < 7; $i++)
			{
			$day = $i + $start_day;
			$day_time = ($day + 3) * 86400;			// zero time was Thursday 1st of January 1970
			$day_name = gmstrftime('%A',$day_time);	// use GMT to get the day names
			if ($encoding)
				$day_name = utf8_encode($day_name);
			if (function_exists('mb_substr'))
				$day_short_name = mb_substr($day_name,0,$day_name_length,'UTF-8');	// prefer this if available
			else
				$day_short_name = substr($day_name,0,$day_name_length);		// use this if no mbstring library
			echo "<th>$day_short_name</th>";
			}
		echo '</tr>';
		}
	
// draw the days

	$day_time = gmmktime(0,0,0,$month,1,$year);			// GMT of first day of month
	$first_weekday = gmstrftime("%w",$day_time);		// 0 = Sunday ... 6 = Saturday
	$weeknumber = gmstrftime("%V",$day_time);			// first week number
	$first_column = ($first_weekday + 7 - $start_day) % 7; 	// column for first day
	$days_in_month = cal_days_in_month(CAL_GREGORIAN,$month,$year);
	echo '<tr>';
	if ($weekHdr != '')
		echo '<td class="mod_minical_weekno">'.$weeknumber.'</td>';
	for ($i = 0; $i < $first_column; $i++)
		echo '<td></td>';
	$column_count = $first_column;
	for ($day = 1; $day <= $days_in_month; $day++)
		{
		if ($column_count == 7)
			{
			echo "</tr>\n<tr>";
			$column_count = 0;
			if ($weekHdr != '')
				{
				$day_time = strtotime(strftime('%Y-%m-%d',$day_time).' + 1 week');
				$weeknumber = gmstrftime("%V",$day_time);	// week number
				echo '<td class="mod_minical_weekno">'.$weeknumber.'</td>';
				}
			}
		if (($year == $current_year) and ($month == $current_month) and ($day == $current_day))
			echo '<td id="mod_minical_today"'.'>'.$day.'</td>';	// highlight today's date
		else
			echo  '<td>'.$day.'</td>';
		$column_count ++;
		}
	for ($i = $column_count; $i < 7; $i++)
		echo '<td></td>';
	echo "</tr></table>\n";
}
