View source
<?php
define('CALENDAR_SHOW_ALL', 0);
define('CALENDAR_HIDE_ALL', -1);
function calendar_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'calendar') . '/includes',
);
}
function calendar_help($section, $arg) {
switch ($section) {
case 'admin/help#calendar':
return t("<p>View complete documentation at !link.</p>", array(
'!link' => 'http://drupal.org/node/120710',
));
}
}
function calendar_init() {
if (module_exists('calendar_multiday') || substr($_GET['q'], 0, 24) == 'admin/build/modules/list') {
return;
}
drupal_add_css('misc/farbtastic/farbtastic.css');
drupal_add_css(drupal_get_path('module', 'calendar') . '/calendar.css');
module_load_include('inc', 'calendar', 'theme/theme.inc');
}
function calendar_theme() {
if (module_exists('calendar_multiday')) {
return array();
}
$module_path = drupal_get_path('module', 'calendar');
$base = array(
'file' => 'theme.inc',
'path' => "{$module_path}/theme",
);
return array(
'calendar_day_node' => $base + array(
'template' => 'calendar-day-node',
'variables' => array(
'node' => NULL,
'view' => NULL,
),
),
'calendar_month_node' => $base + array(
'template' => 'calendar-month-node',
'variables' => array(
'node' => NULL,
'view' => NULL,
),
),
'calendar_week_node' => $base + array(
'template' => 'calendar-week-node',
'variables' => array(
'node' => NULL,
'view' => NULL,
),
),
'calendar_month_multiple_node' => $base + array(
'template' => 'calendar-month-multiple-node',
'variables' => array(
'curday' => NULL,
'count' => NULL,
'view' => NULL,
'types' => NULL,
),
),
'calendar_week_multiple_node' => $base + array(
'template' => 'calendar-week-multiple-node',
'variables' => array(
'curday' => NULL,
'count' => NULL,
'view' => NULL,
'types' => NULL,
),
),
'calendar_datebox' => $base + array(
'template' => 'calendar-datebox',
'variables' => array(
'date' => NULL,
'view' => NULL,
'items' => NULL,
'selected' => NULL,
),
),
'calendar_date_combo' => $base + array(
'variables' => array(
'node' => NULL,
'lable' => NULL,
'view' => NULL,
),
),
'calendar_empty_day' => $base + array(
'variables' => array(
'curday' => NULL,
'view' => NULL,
),
),
'calendar_stripe_legend' => $base + array(
'variables' => array(
'stripe_labels' => NULL,
),
),
'calendar_stripe_stripe' => $base + array(
'variables' => array(
'node' => NULL,
),
),
'calendar_time_row_heading' => $base + array(
'variables' => array(
'start_time' => NULL,
'next_start_time' => NULL,
'curday_date' => NULL,
),
),
'calendar_month_col' => $base + array(
'template' => 'calendar-month-col',
'variables' => array(
'item' => NULL,
),
),
'calendar_month_row' => $base + array(
'template' => 'calendar-month-row',
'variables' => array(
'inner' => NULL,
'class' => NULL,
'iehint' => NULL,
),
),
);
}
function calendar_offset($field_name) {
$default_offset = variable_get('date_default_timezone', 0);
$configurable_zones = variable_get('configurable_timezones', 1);
}
function calendar_part_is_valid($value, $type) {
if (!preg_match('/^[0-9]*$/', $value)) {
return FALSE;
}
$value = intval($value);
if ($value <= 0) {
return FALSE;
}
switch ($type) {
case 'year':
if ($value < DATE_MIN_YEAR) {
return FALSE;
}
break;
case 'month':
if ($value < 0 || $value > 12) {
return FALSE;
}
break;
case 'day':
if ($value < 0 || $value > 31) {
return FALSE;
}
break;
case 'week':
if ($value < 0 || $value > 53) {
return FALSE;
}
}
return TRUE;
}
function calendar_block_list($delta = 0) {
$blocks[0]['info'] = t('Calendar Legend.');
return $blocks;
}
function calendar_block_view($delta = 0) {
switch ($delta) {
case 0:
$block['subject'] = t('Calendar Legend');
$content = theme('calendar_stripe_legend');
$block['content'] = !empty($content) ? '<div class="calendar legend">' . $content . '</div>' : '';
return $block;
}
}
function calendar_display_types() {
return array(
'year' => t('Year'),
'month' => t('Month'),
'day' => t('Day'),
'week' => t('Week'),
);
}
function calendar_current_type($view) {
if (!is_object($view) || !isset($view->argument) || !is_array($view->argument)) {
if (!empty($view->date_info->default_display)) {
return $view->date_info->default_display;
}
return FALSE;
}
$i = 0;
$date_handler = new date_sql_handler();
foreach ($view->argument as $argument) {
if ($argument['id'] == 'date_argument') {
$parts = array_keys($date_handler
->arg_parts($view->args[$i]));
break;
}
$i++;
}
return array_pop($parts);
}
function calendar_node_stripe($view, &$node, $query_name, $delta, $stripe = NULL, $label = '') {
$colors = isset($view->date_info->calendar_colors) ? $view->date_info->calendar_colors : array();
if (empty($colors)) {
return;
}
$type_names = node_type_get_names();
$type = $node->raw->node_type;
if (!isset($node->stripe)) {
$node->stripe = array();
$node->stripe_label = array();
}
if (!$label && array_key_exists($type, $type_names)) {
$label = $type_names[$type];
}
if (!$stripe) {
if (array_key_exists($type, $colors)) {
$stripe = $colors[$type];
}
else {
$stripe = '';
}
}
$node->stripe[] = $stripe;
$node->stripe_label[] = $label;
$GLOBALS['calendar_stripe_labels'][][$type] = array(
'stripe' => $stripe,
'label' => $label,
);
return $stripe;
}
function calendar_node_taxonomy_stripe($view, &$node, $query_name, $delta, $stripe = NULL, $label = '') {
$colors_taxonomy = isset($view->date_info->calendar_colors_taxonomy) ? $view->date_info->calendar_colors_taxonomy : array();
if (empty($colors_taxonomy)) {
return;
}
if (empty($node->raw->taxonomy_term_data_tid)) {
return;
}
$node->vid = $node->raw->node_vid;
$terms_for_node = (array) $node->raw->taxonomy_term_data_tid;
if (!isset($node->stripe)) {
$node->stripe = array();
$node->stripe_label = array();
}
if (count($terms_for_node)) {
foreach ($terms_for_node as $tid) {
$term_for_node = taxonomy_term_load($tid);
if (!array_key_exists($term_for_node->tid, $colors_taxonomy)) {
continue;
}
$stripe = $colors_taxonomy[$term_for_node->tid];
$stripe_label = $term_for_node->name;
$node->stripe[] = $stripe;
$node->stripe_label[] = $stripe_label;
$GLOBALS['calendar_stripe_labels'][][$term_for_node->tid] = array(
'stripe' => $stripe,
'label' => $stripe_label,
);
}
}
else {
$node->stripe[] = '';
$node->stripe_label[] = '';
}
return;
}
function calendar_node_group_stripe($view, &$node, $query_name, $delta, $stripe = NULL, $label = '') {
$colors_group = isset($view->date_info->calendar_colors_group) ? $view->date_info->calendar_colors_group : array();
if (empty($colors_group)) {
return;
}
if (!function_exists('og_get_node_groups')) {
return;
}
$groups_for_node = og_get_node_groups($node);
if (!isset($node->stripe)) {
$node->stripe = array();
$node->stripe_label = array();
}
if (count($groups_for_node)) {
foreach ($groups_for_node as $gid => $group_name) {
if (!array_key_exists($gid, $colors_group)) {
continue;
}
$stripe = $colors_group[$gid];
$stripe_label = $group_name;
$node->stripe[] = $stripe;
$node->stripe_label[] = $stripe_label;
$GLOBALS['calendar_stripe_labels'][][$gid] = array(
'stripe' => $stripe,
'label' => $stripe_label,
);
}
}
else {
$node->stripe[] = '';
$node->stripe_label[] = '';
}
return $stripe;
}
function calendar_og_groups($view) {
if (!($groupnode = og_get_group_context())) {
global $user;
$groupnodes = array_keys($user->og_groups);
}
else {
$groupnodes = array(
$groupnode->nid,
);
}
return $groupnodes;
}
function calendar_date_select($view) {
return '<div class="calendar-date-select">' . drupal_render(drupal_get_form('calendar_date_select_form', $view)) . '</div>';
}
function calendar_date_select_form($form, &$form_state, $view) {
$format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), array(
'year',
'month',
'day',
));
$form['calendar_goto'] = array(
'#type' => module_exists('date_popup') ? 'date_popup' : 'date_select',
'#default_value' => date_format($view->date_info->min_date, 'Y-m-d'),
'#date_timezone' => date_default_timezone(),
'#date_format' => $format,
);
$form['calendar_type'] = array(
'#type' => 'hidden',
'#value' => $view->date_info->calendar_type,
);
$form['view_name'] = array(
'#type' => 'hidden',
'#value' => $view->name,
);
$form['view_url'] = array(
'#type' => 'hidden',
'#value' => $view
->get_url(),
);
$pos = calendar_arg_position($view);
$form['calendar_previous_arg'] = array(
'#type' => 'hidden',
'#value' => $view->args[$pos],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Change date'),
);
return $form;
}
function calendar_arg_position($view) {
$pos = 0;
foreach ($view->argument as $argument) {
if ($argument->definition['handler'] == 'date_views_argument_handler') {
return $pos;
}
$pos++;
}
}
function calendar_get_node_link($node, $default = NULL) {
if (isset($node->url)) {
return url($node->url, array(
'absolute' => TRUE,
));
}
elseif (empty($node->remote) && is_numeric($node->nid)) {
return url("node/{$node->nid}", array(
'absolute' => TRUE,
));
}
elseif (!empty($default)) {
return url($default, array(
'absolute' => TRUE,
));
}
}
function calendar_groupby_times($type = '') {
$times = array();
switch ($type) {
case 'hour':
for ($i = 0; $i <= 23; $i++) {
$times[] = date_pad($i) . ':00:00';
}
break;
case 'half':
for ($i = 0; $i <= 23; $i++) {
$times[] = date_pad($i) . ':00:00';
$times[] = date_pad($i) . ':30:00';
}
break;
default:
break;
}
return $times;
}
function calendar_errors($error) {
switch ($error) {
case 'missing_argument_default':
return t("The Date argument in this view must be set up to provide a default value set to the current date. Edit the argument, find 'Action to take if argument is not present.', choose 'Provide default argument', then select 'Current date'.");
}
}
function calendar_validate_hex_color($element) {
if (!$element['#required'] && empty($element['#value'])) {
return;
}
if (!preg_match('/^#(?:(?:[a-f\\d]{3}) {1,2})$/i', $element['#value'])) {
form_error($element, t('@value is not a valid hex color', array(
'@value' => check_plain($element['#value']),
)));
}
else {
form_set_value($element, $element['#value']);
}
}
function calendar_link($type, $object, $teaser = FALSE) {
if ($type == 'node' && !$teaser) {
$path = variable_get('calendar_date_link_' . $object->type);
if (!empty($path)) {
return array(
'calendar_link' => array(
'title' => t('Calendar'),
'href' => $path,
'attributes' => array(
'title' => t('View the calendar.'),
),
),
);
}
}
}
function calendar_remove($view_name) {
$calendar_options = variable_get('calendar_default_view_options', array());
if (array_key_exists($view_name, $calendar_options)) {
unset($calendar_options[$view_name]);
}
variable_set('calendar_default_view_options', $calendar_options);
if ($view = views_get_view($view_name)) {
$view
->delete();
}
views_invalidate_cache();
}
function calendar_week_header($view) {
$len = isset($view->date_info->style_name_size) ? $view->date_info->style_name_size : (!empty($view->date_info->mini) ? 1 : 3);
$with_week = !empty($view->date_info->style_with_weekno);
$untranslated_days = calendar_untranslated_days();
if ($len == 99) {
$translated_days = date_week_days_ordered(date_week_days(TRUE));
}
else {
$translated_days = date_week_days_ordered(date_week_days_abbr(TRUE));
}
if ($with_week) {
$row[] = array(
'header' => TRUE,
'class' => "days week",
'data' => ' ',
);
}
foreach ($untranslated_days as $delta => $day) {
$label = $len < 3 ? drupal_substr($translated_days[$delta], 0, $len) : $translated_days[$delta];
$row[] = array(
'header' => TRUE,
'class' => "days " . $day,
'data' => $label,
);
}
return $row;
}
function calendar_untranslated_days() {
$untranslated_days = date_week_days_ordered(date_week_days_untranslated());
foreach ($untranslated_days as $delta => $day) {
$untranslated_days[$delta] = strtolower(substr($day, 0, 3));
}
return $untranslated_days;
}
function calendar_build_nodes(&$view, &$items) {
if (empty($view->date_info->min_date) || empty($view->date_info->max_date)) {
return $items;
}
$display_timezone = date_timezone_get($view->date_info->min_date);
$display_timezone_name = timezone_name_get($display_timezone);
$min_utc = clone $view->date_info->min_date;
date_timezone_set($min_utc, timezone_open('UTC'));
$max_utc = clone $view->date_info->max_date;
date_timezone_set($max_utc, timezone_open('UTC'));
$min_zone_string = array();
$max_zone_string = array();
$view->date_info->nodes_per_page = 0;
$type_names = node_type_get_names();
$datefields = array();
$fields = date_views_fields();
if (!empty($view->filter['date_filter'])) {
$date_filter = $view->filter['date_filter'];
foreach ($view->filter['date_filter']->options['date_fields'] as $name) {
$datefields[] = $fields['name'][$name]['query_name'];
}
}
if (!empty($view->argument['date_argument'])) {
$date_filter = $view->argument['date_argument'];
foreach ($view->argument['date_argument']->options['date_fields'] as $name) {
$datefields[] = $fields['name'][$name]['query_name'];
}
}
$view_fields = date_views_views_fetch_fields('node', 'field');
$field_names = (array) array_keys($fields['name']);
$nodes = array();
$i = 0;
foreach ($date_filter->options['date_fields'] as $name) {
$field = $fields['name'][$name];
$field_type = strstr($field['type'], 'string') ? 'string' : 'timestamp';
$alias = $field['query_name'];
$field_name = $field['field_name'];
$fromto = $field['fromto'];
$tz_handling = $field['tz_handling'];
$label = isset($view->field[$name]) ? $view->field[$name]['label'] : $field['field_name'];
$tz_alias = str_replace('.', '_', $field['timezone_field']);
$db_tz = date_get_timezone_db($field['tz_handling']);
$local_tz = date_get_timezone($field['tz_handling'], 'date');
$field_name = $field['field_name'];
$real_field = $field['real_field_name'];
if (!isset($view->field[$field_name])) {
$format = variable_get('date_format_short', 'm/d/Y - H:i');
}
else {
if (strstr($field['type'], 'cck')) {
$formatter = $view->field[$field_name]->options['type'];
$format = date_formatter_format($formatter, $view->field[$field_name]->options['type']);
}
else {
$format = $view->field[$field_name]->options['date_format'];
switch ($format) {
case 'long':
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
case 'medium':
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
break;
case 'custom':
$format = $view->field[$field_name]->options['custom_date_format'];
break;
case 'time ago':
break;
default:
$format = variable_get('date_format_short', 'm/d/Y - H:i');
break;
}
}
}
$domain = check_plain($_SERVER['SERVER_NAME']);
$processed = array();
$rrule_processed = array();
foreach ($items as $pos => $item) {
$delta = !empty($field['delta_field']) && !empty($item->{$field['delta_field']}) ? $item->{$field['delta_field']} : 0;
$id = 'calendar.' . $item->id . '.' . $real_field . '.' . $delta;
if (strstr($view->current_display, '_ical')) {
if (!isset($rrule_processed[$item->id])) {
$rrule_processed[$item->id] = TRUE;
}
else {
continue;
}
}
if (!in_array($id, $processed) && isset($item->calendar_fields->{$alias})) {
$values[0] = $item->calendar_fields->{$fromto}[0];
$values[1] = $item->calendar_fields->{$fromto}[1];
$db_tz = date_get_timezone_db($tz_handling, isset($item->{$tz_alias}) ? $item->{$tz_alias} : $display_timezone_name);
$to_zone = date_get_timezone($tz_handling, isset($item->{$tz_alias}) ? $item->{$tz_alias} : $display_timezone_name);
$display_timezone_name = $to_zone;
$values_display = array();
$date = new DateObject($values[0], $db_tz);
if ($db_tz != $to_zone) {
date_timezone_set($date, timezone_open($to_zone));
}
$values[0] = date_format($date, DATE_FORMAT_DATETIME);
if ($display_timezone_name != $to_zone) {
date_timezone_set($date, $display_timezone);
$values_display[0] = date_format($date, DATE_FORMAT_DATETIME);
}
else {
$values_display[0] = $values[0];
}
$date = new DateObject($values[1], $db_tz);
if ($db_tz != $to_zone) {
date_timezone_set($date, timezone_open($to_zone));
}
$values[1] = date_format($date, DATE_FORMAT_DATETIME);
if ($display_timezone_name != $to_zone) {
date_timezone_set($date, $display_timezone);
$values_display[1] = date_format($date, DATE_FORMAT_DATETIME);
}
else {
$values_display[1] = $values[1];
}
if (!isset($min_zone_string[$display_timezone_name])) {
$date = clone $view->date_info->min_date;
date_timezone_set($date, $display_timezone);
$min_zone_string[$display_timezone_name] = date_format($date, DATE_FORMAT_DATE);
$date = clone $view->date_info->max_date;
date_timezone_set($date, $display_timezone);
$max_zone_string[$display_timezone_name] = date_format($date, DATE_FORMAT_DATE);
}
$now = max($min_zone_string[$display_timezone_name], substr($values_display[0], 0, 10));
$to = min($max_zone_string[$display_timezone_name], substr($values_display[1], 0, 10));
$next = new DateObject($now, $display_timezone);
if ($display_timezone_name != $to_zone) {
date_timezone_set($next, timezone_open($to_zone));
}
if (empty($to)) {
$to = $now;
}
while ($now <= $to) {
$node = clone $item;
if (isset($node->node_title) && !isset($node->title)) {
$node->title = $node->node_title;
}
if (isset($node->node_type) && !isset($node->type)) {
$node->type = $node->node_type;
}
$exceptions = array(
'format_interval',
'time ago',
);
$node->label = $label;
$node->format = $format;
if (!in_array($node->format, $exceptions)) {
if (!isset($formats[$format])) {
$formats[$format] = date_limit_format($format, array(
'hour',
'minute',
'second',
));
$node->format_time = $formats[$format];
}
}
else {
$node->format_time = '';
}
$node->url = calendar_get_node_link($node);
$node->datefield = $alias;
foreach ($node as $key => $val) {
if ($key != $alias && in_array($key, $datefields)) {
unset($node->{$key});
foreach ($fields['name'] as $other_fields) {
if ($other_fields['query_name'] == $key) {
foreach ($other_fields['related_fields'] as $related_field) {
$key2 = str_replace('.', '_', $related_field);
unset($node->{$key2});
}
}
}
}
}
$start = date_format($next, DATE_FORMAT_DATETIME);
date_modify($next, '+1 day');
date_modify($next, '-1 second');
$end = date_format($next, DATE_FORMAT_DATETIME);
$node->calendar_start = $values[0] < $start ? $start : $values[0];
$node->calendar_end = !empty($values[1]) ? $values[1] > $end ? $end : $values[1] : $node->calendar_start;
$node->date_start = date_create($values[0], timezone_open($to_zone));
$node->date_end = date_create(!empty($values[1]) ? $values[1] : $values[0], timezone_open($to_zone));
$node->date_start = date_create($values[0], timezone_open($to_zone));
$node->date_end = date_create(!empty($values[1]) ? $values[1] : $values[0], timezone_open($to_zone));
$node->calendar_start_date = date_create($node->calendar_start, timezone_open($to_zone));
$node->calendar_end_date = date_create($node->calendar_end, timezone_open($to_zone));
$node->calendar_start = date_format($node->calendar_start_date, DATE_FORMAT_DATETIME);
$node->calendar_end = date_format($node->calendar_end_date, DATE_FORMAT_DATETIME);
if (substr($real_field, 0, 6) == 'field_') {
$cck_field = field_info_field($real_field);
$instance = field_info_instance($view->base_table, $real_field, $node->type);
$granularity = $cck_field['settings']['granularity'];
$increment = $instance['widget']['settings']['increment'];
}
else {
$granularity = 'second';
$increment = 1;
}
$node->calendar_all_day = date_is_all_day($node->calendar_start, $node->calendar_end, $granularity, $increment);
$node->calendar_start = date_format($node->calendar_start_date, DATE_FORMAT_DATETIME);
$node->calendar_end = date_format($node->calendar_end_date, DATE_FORMAT_DATETIME);
unset($node->calendar_fields);
if (isset($node) && empty($node->calendar_start)) {
unset($node);
}
else {
calendar_node_stripe($view, $node, $alias, $alias);
calendar_node_taxonomy_stripe($view, $node, $alias, $alias);
calendar_node_group_stripe($view, $node, $alias, $alias);
$node->date_id = $id . '.' . $pos;
$nodes[] = $node;
unset($node);
}
date_modify($next, '+1 second');
$processed[] = $id;
$now = date_format($next, DATE_FORMAT_DATE);
}
}
}
}
return $nodes;
}