Location: PHPKode > projects > Horizon Blogging Software > calendar.php
<?php
$time_start = microtime(TRUE);

define('IN_HBS',TRUE);

include('inc/include.php');
$cfg = getConfig();

$vars['config'] = $cfg;
$vars['title'] = $cfg['hzn_title'];
show_page('header',$vars,$cfg['template']);
db();

function getEventDays($month, $year) 
{
	$days = array();
	$sql = mysql_query("SELECT DAY(event_date) AS day, COUNT(event_id) FROM" . $mysql["db_prefix"] . "calevents WHERE MONTH(event_date) = '$month' AND YEAR(event_date) = '$year' GROUP BY day");
	return $days;
}

function drawCalendar($month, $year) {
  // set variables we will need to help with layouts
  $first       = mktime(0,0,0,$month,1,$year); // timestamp for first of the month
  $offset      = date('w', $first); // what day of the week we start counting on
  $daysInMonth = date('t', $first);
  $vars['monthName']   = date('F', $first);
  $vars['weekDays']    = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa');
  $eventDays   = getEventDays($month, $year);
  
  // Start drawing calendar
  $vars['year'] = $year;
  
  $i = 0;
  for ($d = (1 - $offset); $d <= $daysInMonth; $d++) {
    if ($i % 7 == 0) $vars['day'][$d]['nl'] === TRUE;// Start new row
    if ($d < 1) $vars['day'][$d]['class'] = 'nonMonthDay';
    else {
	$vars['day'][$d]['class'] = 'monthDay';
	$vars['day'][$d]['id'] = $d;
      if (in_array($d, $eventDays)) {
        $vars['day'][$d]['event'] = TRUE;
	$vars['day'][$d]['year'] = $year;
	$vars['day'][$d]['month'] = $month;
      } 
    }
    ++$i; // Increment position counter
    if ($i % 7 == 0) $vars['day'][$d]['endl'] = TRUE; // End row on the 7th day
  }
  
  // Round out last row if we don't have a full week
  if ($i % 7 != 0) {
    for ($j = 0; $j < (7 - ($i % 7)); $j++) {
	$d++;
      $vars['day'][$d]['class'] = 'nonMonthDay';
    }
  }  
    return $vars;
}

$year  = (isset($_GET['year']) && ((int)$_GET['year'].''==$_GET['year'].'')) ? (int)$_GET['year'] : date('Y');
$month = (isset($_GET['month']) && ((int)$_GET['month'].''==$_GET['month'].'')) ? (int)$_GET['month'] : date('m');
$day = (isset($_GET['day']) && ((int)$_GET['month'].''==$_GET['month'].'')) ? (int)$_GET['day'] : date('d');

$vars = drawCalendar($month, $year);

// Previous month link
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month
$pMax = date('t', $prevTS);
$pDay = ($day > $pMax) ? $pMax : $day;
list($y, $m) = explode('-', date('Y-m', $prevTS));

$vars['prevYear'] = $y;
$vars['prevMonth'] = $m;
$vars['prevDay'] = $pDay;
// Next month link
$nextTS = strtotime("$year-$month-01 +1 month");
$nMax = date('t', $nextTS);
$nDay = ($day > $nMax) ? $nMax : $day;
list($y, $m) = explode('-', date('Y-m', $nextTS));

$vars['nYear'] = $y;
$vars['nMonth'] = $m;
$vars['nDay'] = $nDay;
$vars['draw'] = TRUE;
show_page('calendar',$vars,$cfg['template']);

// Calendar is done, let's list events for selected day
$sql = mysql_query("SELECT * FROM " . $mysql["db_prefix"] . "calevents WHERE event_date = '$year-$month-$day'");
if(!$sql)
{
	$vars['show_install'] = FALSE;
	$vars['error'] = 'HBS couldn\'t find the calevents table. If you cannot fix the problem, please go to the Edge Drive Community by clicking <a href="http://www.edgedrive.com/community">here</a>. Once there, be sure to include the error which is stated below.';
	$vars['sql_error'] = mysql_error();
	show_page('error',$vars,$cfg['template']);
}
if (@mysql_num_rows($sql) > 0) {
  while ($e = mysql_fetch_array($sql)) {
	$vars['list'] = TRUE;
	$vars['event_title'] = $e['event_title'];
    show_page('calendar',$vars,$cfg['template']);
  }
} else {
  $vars['empty'] = TRUE;
	show_page('calendar',$vars,$cfg['template']);
} 

$vars['version'] = getVersion();
show_page('footer',$vars,$cfg['template']);
$time_end = microtime(TRUE);
$time = $time_end - $time_start;
echo $time;
?>
Return current item: Horizon Blogging Software