View source
<?php
function calendar_get_calendar($view, $nodes, $module, $title = NULL, $params = array()) {
global $user;
calendar_load_date_api();
$today = calendar_user_date();
if ($params['limit'][0]) {
$min_date = $params['limit'][0];
$max_date = $params['limit'][1];
}
else {
$min_date = 0;
$max_date = 999999999999;
}
$data = array();
foreach ($nodes as $nid => $node) {
$node_start = date_gmmktime(array(
'mon' => calendar_event_date('m', $node->calendar_start, $node->start_offset),
'mday' => calendar_event_date('d', $node->calendar_start, $node->start_offset),
'year' => calendar_event_date('Y', $node->calendar_start, $node->start_offset),
));
if ($node->calendar_end) {
$node_end = date_gmmktime(array(
'mon' => calendar_event_date('m', $node->calendar_end, $node->end_offset),
'mday' => calendar_event_date('d', $node->calendar_end, $node->end_offset),
'year' => calendar_event_date('Y', $node->calendar_end, $node->end_offset),
));
}
else {
$node_end = $node_start;
}
if ($node_end < $min_date || $node_start > $max_date) {
continue;
}
if ($min_date >= $node_start) {
$node->calendar_start = $min_date;
}
if ($max_date <= $node_end) {
$node->calendar_end = $max_date;
}
$node->stripe = $params['stripe'] ? $node->{$params}['stripe'] : $node->nid;
$calendar = new stdClass();
$nodes[$node->nid] = $node;
$calendar->nid = $node->nid;
if ($node_end - $node_start < 86400) {
$calendar->calendar_state = 'singleday';
$calendar->stamp = $node_start;
$calendar->stripe = $node->stripe;
$data[date_format_date('Y', $node_start)][date_format_date('m', $node_start)][date_format_date('j', $node_start)][] = $calendar;
}
else {
for ($x = $node_start; $x <= $node_end; $x += 86400) {
if ($x == $node_end) {
$calendar = new StdClass();
$calendar->nid = $node->nid;
$calendar->stripe = $node->stripe;
$calendar->calendar_state = 'end';
$calendar->stamp = $x;
$data[date_format_date('Y', $x)][date_format_date('m', $x)][date_format_date('j', $x)][] = $calendar;
}
elseif ($x == $node_start) {
$calendar = new StdClass();
$calendar->nid = $node->nid;
$calendar->stripe = $node->stripe;
$calendar->calendar_state = 'start';
$calendar->stamp = $x;
$data[date_format_date('Y', $x)][date_format_date('m', $x)][date_format_date('j', $x)][] = $calendar;
}
else {
$calendar = new StdClass();
$calendar->nid = $node->nid;
$calendar->stripe = $node->stripe;
$calendar->calendar_state = 'ongoing';
$calendar->stamp = $x;
$data[date_format_date('Y', $x)][date_format_date('m', $x)][date_format_date('j', $x)][] = $calendar;
}
}
}
}
ksort($data, SORT_NUMERIC);
foreach ($data as $year => $months) {
ksort($data[$year], SORT_NUMERIC);
foreach ($data[$year] as $month => $days) {
ksort($data[$year][$month], SORT_NUMERIC);
}
}
$weekdays = calendar_week_days();
switch ($view) {
case 'day':
case 'table':
foreach ($data as $year => $months) {
if (count($data) > 1 && !$params['hide_header']) {
$rows[][] = array(
'class' => 'heading year',
'id' => 'year' . $year,
'data' => $year,
);
}
foreach ($months as $month => $days) {
foreach ($days as $day => $calendars) {
$content = theme('calendar_date_box', $year, $month, $day, 'table', $params['mini'], !empty($calendars), $params['url'], $params['append']);
foreach ($calendars as $calendar) {
if (!$month_name) {
$month_name = date_format_date('M', $calendar->stamp);
$dow = _calendar_day_of_week($calendar->stamp);
}
$node = $nodes[$calendar->nid];
$node->calendar_state = $calendar->calendar_state;
if ($output = module_invoke($module, 'calendar_node_' . $view, $node)) {
$content .= $output;
}
else {
$content .= theme('calendar_node_' . $view, $node);
}
}
$rows[][] = array(
'class' => strtolower("{$month_name} " . $weekdays[$dow]['day'] . ($calendar->stamp == $today ? ' today' : '')),
'id' => strtolower($month_name . $day),
'data' => $content,
);
$month_name = NULL;
}
}
}
break;
case 'week':
case 'month':
case 'year':
$colspan = $params['with_weekno'] ? '8' : '7';
foreach ($data as $year => $months) {
$month_rows[] = $title;
if (count($data) > 1 && !$params['hide_header']) {
$rows[][] = array(
'class' => 'heading year',
'id' => 'year' . $year,
'data' => $year,
'colspan' => $colspan,
);
}
foreach ($months as $month => $days) {
$curstamp = date_gmmktime(array(
'mon' => $month,
'mday' => 1,
'year' => $year,
));
$lastday = date_gmmktime(array(
'mon' => $month,
'mday' => date_format_date('t', $curstamp),
'year' => $year,
));
$row = array_fill(0, 6, theme('calendar_empty_day'));
$start = $offset = _calendar_day_of_week($curstamp);
$month_name = date_format_date('M', $curstamp);
$month_link = $params['url'] ? l($month_name, $params['url'] . '/' . $year . '/' . intval($month), NULL, $params['append']) : $month_name;
$week = 0;
if (!$params['hide_header']) {
$rows[][] = array(
'class' => 'heading month',
'id' => 'month' . $month,
'data' => $month_name,
'colspan' => $colspan,
);
}
$rows[] = calendar_week_header($params['mini'], $params['with_weekno']);
while ($curstamp <= $lastday) {
for ($x = $start; $x < 7; $x++) {
$cur_day = $week * 7 + ($x + 1) - $offset;
$selected = FALSE;
if (is_array($days[$cur_day])) {
foreach ($items = $days[$cur_day] as $item) {
if ($item->nid) {
$selected = TRUE;
}
}
}
$content = theme('calendar_date_box', $year, $month, $cur_day, $view, $params['mini'], $selected, $params['url'], $params['append']);
if (is_array($days[$cur_day]) && !$params['mini']) {
foreach ($days[$cur_day] as $calendar) {
$node = $nodes[$calendar->nid];
$node->calendar_state = $calendar->calendar_state;
if ($output = module_invoke($module, 'calendar_node_' . $view, $node)) {
$content .= $output;
}
elseif (!$params['mini'] && $node->nid != 0) {
$content .= theme('calendar_node_' . $view, $node);
}
}
}
$row[$x] = array(
'class' => strtolower("{$month_name} " . $weekdays[$x]['day'] . ($curstamp == $today ? ' today' : '') . ($params['mini'] ? ' mini' : '')),
'id' => strtolower($month_name . $cur_day),
'data' => $params['mini'] ? $content : '<div class="inner">' . $content . '</div>',
);
$curstamp += 86400;
if ($curstamp > $lastday) {
$x = 8;
}
}
$cur_week = date_format_date('W', $curstamp);
if (!($view == 'week' && $params['force_week'] && $params['force_week'] != date_format_date('W', $curstamp))) {
if ($params['with_weekno']) {
$week_row = array(
0 => array(
'class' => 'week',
'data' => l(sprintf('%02d', $cur_week), $params['url'] . '/' . $year . '/W' . $cur_week, NULL, $params['append']),
),
);
$rows[] = array_merge($week_row, array_pad($row, 7, theme('calendar_empty_day')));
}
else {
$rows[] = array_pad($row, 7, theme('calendar_empty_day'));
}
}
$week++;
$start = 0;
$row = array();
}
if ($view == 'year' && !$params['as_array']) {
$header = array(
$month_name ? array(
'class' => 'heading',
'data' => $month_link,
'colspan' => $colspan,
) : array(),
);
$output = theme('calendar_month', 'page', $header, $rows);
unset($rows);
$month_rows[] = $output;
}
}
}
break;
}
if ($params['as_array']) {
return $rows;
}
$header = $title ? array(
array(
'class' => 'heading',
'data' => $title,
'colspan' => $colspan,
),
) : array();
$op = $params['mini'] ? 'mini' : 'page';
return theme('calendar_' . $view, $op, $header, $rows, $month_rows);
}
function calendar_mktime($hour, $minute, $second, $month, $day, $year, $offset = NULL) {
global $user;
calendar_load_date_api();
$timestamp = date_gmmktime(array(
'hours' => $hour,
'minutes' => $minute,
'seconds' => $second,
'mon' => $month,
'day' => $day,
'year' => $year,
));
if (isset($offset)) {
return $timestamp - $offset;
}
elseif (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone) && variable_get('event_timezone_display', 'event') == 'user') {
return $timestamp - $user->timezone;
}
else {
return $timestamp - variable_get('date_default_timezone', 0);
}
}
function calendar_user_date($part = 'timestamp') {
global $user;
static $date;
calendar_load_date_api();
if (!$date) {
$now = date_time();
$date = date_gmmktime(array(
'mon' => date_format_date('m', $now),
'mday' => date_format_date('j', $now),
'year' => date_format_date('Y', $now),
));
}
switch ($part) {
case 'year':
return date_format_date('Y', $date);
case 'month':
return date_format_date('m', $date);
case 'day':
return date_format_date('j', $date);
case 'hour':
return date_format_date('H', $date);
case 'minute':
return date_format_date('i', $date);
case 'week':
return date_format_date('W', $date);
default:
return $date;
}
}
function calendar_event_date($format, $timestamp, $offset = null) {
global $user;
calendar_load_date_api();
if (isset($offset)) {
$timestamp += $offset;
}
elseif (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
$timestamp += $user->timezone;
}
else {
$timestamp += variable_get('date_default_timezone', 0);
}
if ($format == 'w') {
$result = calendar_day_of_week($timestamp);
}
else {
$result = date_format_date($format, $timestamp);
}
return $result;
}
function calendar_week_days() {
static $weekdays;
if (!$weekdays) {
switch (variable_get('date_first_day', 1)) {
case 1:
$weekdays = array(
array(
'day' => 'Mon',
't' => t('Mon'),
),
array(
'day' => 'Tue',
't' => t('Tue'),
),
array(
'day' => 'Wed',
't' => t('Wed'),
),
array(
'day' => 'Thu',
't' => t('Thu'),
),
array(
'day' => 'Fri',
't' => t('Fri'),
),
array(
'day' => 'Sat',
't' => t('Sat'),
),
array(
'day' => 'Sun',
't' => t('Sun'),
),
);
break;
case 2:
$weekdays = array(
array(
'day' => 'Tue',
't' => t('Tue'),
),
array(
'day' => 'Wed',
't' => t('Wed'),
),
array(
'day' => 'Thu',
't' => t('Thu'),
),
array(
'day' => 'Fri',
't' => t('Fri'),
),
array(
'day' => 'Sat',
't' => t('Sat'),
),
array(
'day' => 'Sun',
't' => t('Sun'),
),
array(
'day' => 'Mon',
't' => t('Mon'),
),
);
break;
case 3:
$weekdays = array(
array(
'day' => 'Wed',
't' => t('Wed'),
),
array(
'day' => 'Thu',
't' => t('Thu'),
),
array(
'day' => 'Fri',
't' => t('Fri'),
),
array(
'day' => 'Sat',
't' => t('Sat'),
),
array(
'day' => 'Sun',
't' => t('Sun'),
),
array(
'day' => 'Mon',
't' => t('Mon'),
),
array(
'day' => 'Tue',
't' => t('Tue'),
),
);
break;
case 4:
$weekdays = array(
array(
'day' => 'Thu',
't' => t('Thu'),
),
array(
'day' => 'Fri',
't' => t('Fri'),
),
array(
'day' => 'Sat',
't' => t('Sat'),
),
array(
'day' => 'Sun',
't' => t('Sun'),
),
array(
'day' => 'Mon',
't' => t('Mon'),
),
array(
'day' => 'Tue',
't' => t('Tue'),
),
array(
'day' => 'Wed',
't' => t('Wed'),
),
);
break;
case 5:
$weekdays = array(
array(
'day' => 'Fri',
't' => t('Fri'),
),
array(
'day' => 'Sat',
't' => t('Sat'),
),
array(
'day' => 'Sun',
't' => t('Sun'),
),
array(
'day' => 'Mon',
't' => t('Mon'),
),
array(
'day' => 'Tue',
't' => t('Tue'),
),
array(
'day' => 'Wed',
't' => t('Wed'),
),
array(
'day' => 'Thu',
't' => t('Thu'),
),
);
break;
case 6:
$weekdays = array(
array(
'day' => 'Sat',
't' => t('Sat'),
),
array(
'day' => 'Sun',
't' => t('Sun'),
),
array(
'day' => 'Mon',
't' => t('Mon'),
),
array(
'day' => 'Tue',
't' => t('Tue'),
),
array(
'day' => 'Wed',
't' => t('Wed'),
),
array(
'day' => 'Thu',
't' => t('Thu'),
),
array(
'day' => 'Fri',
't' => t('Fri'),
),
);
break;
default:
$weekdays = array(
array(
'day' => 'Sun',
't' => t('Sun'),
),
array(
'day' => 'Mon',
't' => t('Mon'),
),
array(
'day' => 'Tue',
't' => t('Tue'),
),
array(
'day' => 'Wed',
't' => t('Wed'),
),
array(
'day' => 'Thu',
't' => t('Thu'),
),
array(
'day' => 'Fri',
't' => t('Fri'),
),
array(
'day' => 'Sat',
't' => t('Sat'),
),
);
break;
}
}
return $weekdays;
}
function calendar_week_header($mini = FALSE, $with_week = TRUE) {
$days = calendar_week_days();
if ($with_week) {
$row[] = array(
'class' => "days week",
'data' => ' ',
);
}
foreach ($days as $day) {
$row[] = array(
'class' => strtolower("days " . $day['day']),
'data' => $mini ? drupal_substr($day['t'], 0, 1) : $day['t'],
);
}
return $row;
}
function _calendar_day_of_week($stamp) {
calendar_load_date_api();
$dow = date_format_date('w', $stamp);
if ($dow - variable_get('date_first_day', 1) >= 0) {
$dow = $dow - variable_get('date_first_day', 1);
}
else {
$dow = 7 - variable_get('date_first_day', 1);
}
return $dow;
}