View source
<?php
define('CALENDAR_SHOW_ALL', 0);
define('CALENDAR_HIDE_ALL', -1);
define('CALENDAR_EMPTY_STRIPE', '#ffffff');
function calendar_menu() {
$items = array();
$items['admin/config/date/calendar'] = array(
'title' => 'Calendar',
'description' => 'Calendar administration.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'calendar_admin_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer site configuration',
),
);
return $items;
}
function calendar_admin_settings() {
$form = array();
$form['#prefix'] = '<h2>Calendar Administration</h2>';
$form['calendar_track_date'] = array(
'#title' => t('Track current date in session'),
'#type' => 'radios',
'#options' => array(
0 => t("Never"),
1 => t('For authenticated users'),
2 => t('For all users'),
),
'#default_value' => variable_get('calendar_track_date', 0),
'#description' => t("Store session information about the user's current date as they move back and forth through the calendar. Without this setting users will revert to the current day each time they choose a new calendar period (year, month, week, or day). With this option set they will move to a day that conforms to the time period they were viewing before they switched. Requires session tracking which is not ordinarily enabled for anonymous users."),
);
$form['calendar_add_colorbox'] = array(
'#title' => t('Add Colorbox settings to Node calendar templates'),
'#default_value' => variable_get('calendar_add_colorbox'),
'#type' => 'radios',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('To try the Colorbox settings, choose the option to add Colorbox settings to the calendar templates. Install and enable the Colorbox module, following the instructions on the Colorbox project page, then create a new calendar from a template using any date field in the Node base table. The calendar will be set up to display the calendar items in a Colorbox.'),
);
$form['calendar_provide_views_templates'] = array(
'#title' => t('Provide Views templates for calendar'),
'#type' => 'radios',
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => variable_get('calendar_provide_views_templates', 1),
'#description' => t("Views calendar templates are created for every date field by default, see !url. You can disable this to improve Views UI performance.", array(
'!url' => l(t('Views templates'), 'admin/structure/views/add-template'),
)),
);
return system_settings_form($form);
}
function calendar_preprocess_date_views_pager(&$vars) {
$view = $vars['plugin']->view;
if (empty($view->date_info) || empty($view->argument)) {
return;
}
$current_path = !empty($view->display_handler->options['path']) ? $view->display_handler->options['path'] : '';
if (empty($current_path)) {
return;
}
if ($view->display_handler->options['menu']['type'] == 'default tab') {
$parts = explode('/', $current_path);
array_pop($parts);
$current_path = implode('/', $parts);
}
if (!empty($view->date_info->calendar_date_link) && !empty($view->date_info->url) && (user_access("administer nodes") || user_access('create ' . $view->date_info->calendar_date_link . ' content'))) {
$name = node_type_get_name($view->date_info->calendar_date_link);
$href = 'node/add/' . str_replace('_', '-', $view->date_info->calendar_date_link);
$calendar_links[$current_path]['actions'][] = array(
'title' => t('Add @name', array(
'@name' => $name,
)),
'path' => $href,
);
}
drupal_alter('calendar_links', $calendar_links);
if (!empty($calendar_links[$current_path])) {
$_SESSION['calendar_links'][$current_path] = $calendar_links[$current_path];
}
}
function calendar_date_default_argument_alter(&$argument, &$value) {
global $user;
$style_options = $argument->view->display_handler
->get_option('style_options');
$tracking = variable_get('calendar_track_date', 0);
if (!empty($tracking) && ($tracking == 2 || !empty($user->uid))) {
if (!empty($_SESSION[$argument->view->name]['default_date'])) {
$default_date = $_SESSION[$argument->view->name]['default_date'];
}
else {
$default_date = date_now();
$_SESSION[$argument->view->name]['default_date'] = $default_date;
}
$value = $default_date
->format($argument->arg_format);
}
}
function calendar_views_pre_render(&$view) {
global $user;
$style_options = $view->display_handler
->get_option('style_options');
$tracking = variable_get('calendar_track_date', 0);
if (!empty($tracking) && ($tracking == 2 || !empty($user->uid))) {
foreach ($view->argument as $id => &$argument) {
if (date_views_handler_is_date($argument, 'argument') && empty($argument->is_default)) {
$date_range = $argument->date_handler
->arg_range($argument->argument);
$session_date = $date_range[0];
$days = intval(($date_range[1]
->format('U') - $date_range[0]
->format('U')) / 3600 / 24 / 2);
date_modify($session_date, "+{$days} days");
$_SESSION[$view->name]['default_date'] = $session_date;
}
}
}
}
function calendar_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if (!empty($_SESSION['calendar_links']) && array_key_exists($root_path, $_SESSION['calendar_links'])) {
$calendar_data = $_SESSION['calendar_links'][$root_path];
if (!empty($calendar_data['actions'])) {
foreach ($calendar_data['actions'] as $action) {
$item = menu_get_item($action['path']);
$item['title'] = $action['title'];
if (!isset($item['localized_options'])) {
$item['localized_options'] = array();
}
$item['localized_options'] += array(
'query' => array(),
);
$item['localized_options']['query'] += drupal_get_destination();
if (array_key_exists('access', $item) && $item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
}
return;
}
function calendar_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'calendar') . '/includes',
);
}
function calendar_display_types() {
return array(
'year' => t('Year'),
'month' => t('Month'),
'day' => t('Day'),
'week' => t('Week'),
);
}
function calendar_help($section, $arg) {
switch ($section) {
case 'admin/help#calendar':
return t("<p>View complete documentation at !link.</p>", array(
'!link' => l(t('Date and Calendar Documentation'), 'http://drupal.org/node/262062'),
));
}
}
function calendar_theme() {
$module_path = drupal_get_path('module', 'calendar');
$base = array(
'file' => 'theme.inc',
'path' => "{$module_path}/theme",
);
return array(
'calendar_item' => $base + array(
'template' => 'calendar-item',
'variables' => array(
'view' => NULL,
'rendered_fields' => NULL,
'item' => NULL,
),
),
'calendar_datebox' => $base + array(
'template' => 'calendar-datebox',
'variables' => array(
'date' => NULL,
'view' => NULL,
'items' => NULL,
'selected' => NULL,
),
),
'calendar_empty_day' => $base + array(
'variables' => array(
'curday' => NULL,
'view' => NULL,
),
),
'calendar_stripe_legend' => $base + array(
'variables' => array(
'view' => 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,
),
),
'calendar_month_multiple_entity' => $base + array(
'template' => 'calendar-month-multiple-entity',
'variables' => array(
'
curday' => NULL,
'count' => NULL,
'view' => NULL,
'ids' => NULL,
),
),
);
}
function calendar_node_view($node, $view_mode, $langcode) {
$path = variable_get('calendar_date_link_' . $node->type);
if (!empty($path)) {
$links['calendar_link'] = array(
'title' => t('Calendar'),
'href' => $path,
'attributes' => array(
'title' => t('View the calendar.'),
),
);
$node->content['links']['calendar'] = array(
'#theme' => 'links__node__calendar',
'#links' => $links,
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
);
}
}
function calendar_set_link($entity_type, $bundle, $path) {
switch ($entity_type) {
case 'node':
variable_set('calendar_date_link_' . $bundle, $path);
break;
default:
variable_set('calendar_date_link_' . $entity_type . '_' . $bundle, $path);
break;
}
}
function calendar_clear_link_bundle($bundle = NULL) {
if (empty($bundle)) {
$result = db_select('variable', 'v')
->fields('v', 'name')
->condition('name', 'calendar_date_link_%', 'LIKE')
->execute()
->fetchAll(PDO::FETCH_ASSOC);
}
else {
$result = db_select('variable', 'v')
->fields('v', 'name')
->condition('name', 'calendar_date_link_' . $bundle)
->execute()
->fetchAll(PDO::FETCH_ASSOC);
}
foreach ($result as $row) {
variable_del($row['name']);
}
}
function calendar_clear_link_path($path = NULL) {
$result = db_select('variable', 'v')
->fields('v', array(
'name',
'value',
))
->condition('name', 'calendar_date_link_%', 'LIKE')
->execute()
->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
if (empty($path) || unserialize($row['value']) == $path) {
variable_del($row['name']);
}
}
if (isset($_SESSION['calendar_links'][$path]['actions'])) {
unset($_SESSION['calendar_links'][$path]['actions']);
if (empty($_SESSION['calendar_links'][$path])) {
unset($_SESSION['calendar_links'][$path]);
}
}
}
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();
$full_translated_days = date_week_days_ordered(date_week_days(TRUE));
if ($len == 99) {
$translated_days = $full_translated_days;
}
else {
$translated_days = date_week_days_ordered(date_week_days_abbr(TRUE));
}
if ($with_week) {
$row[] = array(
'header' => TRUE,
'class' => "days week",
'data' => ' ',
'header_id' => 'Week',
);
}
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,
'header_id' => $full_translated_days[$delta],
);
}
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_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_list_views() {
$calendar_views = array();
$views = views_get_enabled_views();
foreach ($views as $view) {
$displays = array();
$view
->init_display();
foreach ($view->display as $display_id => $display) {
if ($display_id != 'default' && !empty($display->display_options['style_plugin']) && $display->display_options['style_plugin'] == 'calendar_style') {
$index = $view->name . ':' . $display_id;
$calendar_views[$index] = ucfirst($view->name) . ' ' . strtolower($view->display[$display_id]->display_title) . ' [' . $view->name . ':' . $display_id . ']';
}
}
}
return $calendar_views;
}
function calendar_block_info() {
$blocks['calendar_legend'] = array(
'info' => t('Calendar Legend'),
);
return $blocks;
}
function calendar_block_configure($delta = '') {
switch ($delta) {
case 'calendar_legend':
$options = calendar_list_views();
$form['calendar_legend_view'] = array(
'#type' => 'select',
'#title' => t('Legend View'),
'#description' => t('Choose the view display that contains the settings for the stripes that should be displayed in a legend in this block. Note that if you change the stripe values in that view you will need to clear the cache to pick up the new values in this block.'),
'#default_value' => variable_get('calendar_legend_view_' . $delta, ''),
'#options' => $options,
);
return $form;
}
}
function calendar_block_save($delta, $edit = array()) {
if ($delta == 'calendar_legend') {
variable_set('calendar_legend_view_' . $delta, $edit['calendar_legend_view']);
drupal_set_message(t('The view for the calendar legend has been set.'));
}
}
function calendar_block_view($delta = '') {
switch ($delta) {
case 'calendar_legend':
$view = variable_get('calendar_legend_view_' . $delta, '');
$content = theme('calendar_stripe_legend', array(
'view' => $view,
));
$block['subject'] = t('Calendar Legend');
$block['content'] = $content;
return $block;
}
}
function calendar_granularity_path(&$view, $granularity) {
$paths =& drupal_static(__FUNCTION__, array());
if (!array_key_exists($view->name, $paths) || !array_key_exists($granularity, $paths[$view->name])) {
$paths[$view->name][$granularity] = '';
foreach ($view->display as $id => $display) {
if (!empty($display->deleted) || empty($display->display_options['style_plugin']) || isset($display->display_options['enabled']) && $display->display_options['enabled'] == FALSE) {
continue;
}
if ($display->display_plugin != 'feed' && !empty($display->display_options['path']) && !empty($display->display_options['arguments'])) {
$type = 'month';
foreach ($display->display_options['arguments'] as $name => $argument) {
if (!empty($argument['granularity'])) {
$type = $argument['granularity'];
}
}
if ($type == $granularity) {
$part_path = $display->display_options['path'];
$parts = explode('/', $part_path);
if (in_array('%', $parts)) {
$current_path = parse_url($_GET['q']);
$current_parts = explode('/', $current_path['path']);
foreach ($parts as $key => $part) {
if ($part == '%' && !empty($current_parts[$key])) {
$parts[$key] = $current_parts[$key];
}
}
$part_path = implode('/', $parts);
}
$paths[$view->name][$granularity] = $part_path;
}
}
}
}
return $paths[$view->name][$granularity];
}
function calendar_validate_hex_color($element, &$form_state) {
if (!$element['#required'] && empty($element['#value'])) {
return;
}
if (!preg_match('/^#(?:(?:[a-f\\d]{3}){1,2})$/i', $element['#value'])) {
form_error($element, t("'@color' is not a valid hex color", array(
'@color' => $element['#value'],
)));
}
else {
form_set_value($element, $element['#value'], $form_state);
}
}