t('Calendar')); foreach (module_implements('_calendar_add_types') as $module) { $function = $module .'_calendar_add_types'; $types += $function($view); } } return $types; } /** * Function to get information about all views that have calendar components. * * @return * array with views that use calendar plugins or have calendar arguments. */ function _calendar_info() { $cid = 'calendar_views'; cache_clear_all($cid, 'cache_views'); $calendar_views = array(); $calendar_types = calendar_view_types(); $result = db_query("SELECT vid, name FROM {view_view} ORDER BY name"); while ($v = db_fetch_object($result)) { $view = views_get_view($v->vid); $additions = array(); $additions['vid'] = $view->vid; $additions['name'] = $view->name; $additions['url'] = $view->url; $additions['args'] = array(); $additions['page'] = array_key_exists($view->page_type, $calendar_types); $additions['block'] = array_key_exists($view->block_type, $calendar_types); foreach ((array) $view->argument as $delta => $argument) { if (in_array($argument['type'], calendar_args())) { $additions['args'][$delta] = $argument['id']; } } if (!empty($additions['args']) || $additions['page'] || $additions['block']) { $calendar_views[$view->name] = $additions; } } views_load_cache(); foreach (_views_get_default_views() as $view) { if (empty($view->disabled)) { $additions = array(); $additions['vid'] = $view->vid; $additions['name'] = $view->name; $additions['url'] = $view->url; $additions['args'] = array(); $additions['page'] = array_key_exists($view->page_type, $calendar_types); $additions['block'] = array_key_exists($view->block_type, $calendar_types); foreach ((array) $view->argument as $delta => $argument) { if (in_array($argument['type'], calendar_args())) { $additions['args'][$delta] = $argument['id']; } } if (!empty($additions['args']) || $additions['page'] || $additions['block']) { $calendar_views[$view->name] = $additions; } } } cache_set($cid, 'cache_views', serialize($calendar_views)); return $calendar_views; } /** * Identify all potential date/timestamp fields. * * @return * array with fieldname, type, and table */ function _calendar_fields() { $cid = 'calendar_fields'; cache_clear_all($cid, 'cache_views'); $delta = 0; $event_fields_processed = array(); views_load_cache(); foreach (_views_get_fields() as $name => $val) { $timestamp_fromto = array(); $string_fromto = array(); $tmp = explode('.', $name); $field_name = $val['content_field']['field_name'] ? $val['content_field']['field_name'] : $tmp[1]; // We need to treat event_start and event_end as a single date, all other fields have // the same field_name for both start and end dates. $processed_name = strstr($field_name, 'event_') ? 'event' : $field_name; $type = ''; // for cck fields, get the date type if ($val['content_field']['type'] == 'date' || $val['content_field']['type'] == 'datestamp') { $type = $val['content_field']['type'] == 'date' ? 'cck_string' : 'cck_timestamp'; } // all other fields that use the views date handler are timestamps elseif ($val['handler'] == views_handler_field_dates()) { $type = 'timestamp'; } // don't do anything if this is not a date field if ($type) { // dates with from and to dates need to handle both fields as one // add the from and to dates to the first one found and ignore the second // Handling for event dates if (module_exists('event') && !in_array($processed_name, $event_fields_processed) && ($name == 'event.event_start' || $name == 'event.event_end')) { $timestamp_fromto = array('event.event_start', 'event.event_end'); $offset_field = 'event.timezone'; $tz_handling = variable_get('event_timezone_display', 'site'); $event_fields_processed[] = $processed_name; $related_fields = array( 'event.event_start', 'event.event_end', 'event.timezone', ); $timezone_field = 'event.timezone'; } // Handling for content field dates elseif ($val['content_field']['tz_handling']) { $tz_handling = $val['content_field']['tz_handling']; if ($tz_handling == 'date') { $offset_field = $val['table'] .'.'. $val['content_db_info']['columns']['offset']['column']; } $related_fields = array( $val['table'] .'.'. $field_name .'_value', $val['table'] .'.'. $field_name .'_value2', $val['table'] .'.'. $field_name .'_timezone', $val['table'] .'.'. $field_name .'_offset', $val['table'] .'.'. $field_name .'_offset2', ); $timezone_field = $val['table'] .'.'. $field_name .'_timezone'; } // Handling for simple timestamp fields else { $timestamp_fromto = array($name, $name); $tz_handling = 'site'; $related_fields = array(); $timezone_field = ''; } // Handling for cck fromto dates if (!in_array($processed_name, $event_fields_processed)) { switch ($val['content_field']['type']) { case 'datestamp': $timestamp_fromto = array( $val['table'] .'.'. $field_name .'_value', $val['table'] .'.'. ($val['content_field']['todate'] ? $field_name .'_value2' : $field_name .'_value'), ); break; case 'date': $string_fromto = array( $val['table'] .'.'. $field_name .'_value', $val['table'] .'.'. ($val['content_field']['todate'] ? $field_name .'_value2' : $field_name .'_value'), ); break; } $event_fields_processed[] = $processed_name; } if (is_array($val['content_field']['granularity'])) { $granularity = (array) array_keys($val['content_field']['granularity']); } else { $granularity = array('Y', 'M', 'D', 'H', 'N'); } // skip this step on second occurance of fromto date fields, if more than one exists in view if (!in_array($processed_name, $event_fields_processed) || $timestamp_fromto || $string_fromto) { // cck fields append a column name to the field, others do not // need a real field_name with no column name appended for cck date formatters $fields[$tmp[1]] = array( 'type' => $type, 'delta' => $delta, 'label' => $val['name'], 'granularity' => $granularity, 'fullname' => $name, 'table' => $tmp[0], 'field' => $tmp[1], 'field_name' => $field_name, 'query_name' => str_replace('.', '_', $name), 'timestamp_fromto' => $timestamp_fromto, 'string_fromto' => $string_fromto, 'tz_handling' => $tz_handling, 'offset_field' => $offset_field, 'timezone_field' => $timezone_field, 'related_fields' => $related_fields, ); } } } cache_set($cid, 'cache_views', serialize($fields)); return $fields; } /** * Validate a view. */ function _calendar_views_validate($type, $view, $form) { // list (and table) modes require there to be at least 1 field active. if (is_array($view['field'])) { $fields = array_filter(array_keys($view['field']), 'is_numeric'); } if (!$fields) { form_error($form["$type-info"][$type .'_type'], t('The Calendar View requires at least one field.')); } if (isset($view['field']['count'])) { $defaultsort = false; for ($i = 0; $i < $view['field']['count']; $i++) { if ($view['field'][$i]['defaultsort']) { if ($defaultsort) { form_error($form['field'][$i]['defaultsort'], t('You can only set on Default Sort on one field.')); break; } $defaultsort = true; } } } // Make sure all arguments are set to 'Display all values'. $arg_types = array(); $cal_args = calendar_args(); foreach ($view['argument'] as $delta => $argument) { if (in_array($argument['type'], $cal_args)) { $view['argument'][$delta]['argdefault'] = 2; if ($argument['argdefault'] != 2) { form_error($form['argument'][$delta]['argdefault'], t('Calendar arguments must be set to \'Display All Values\'.')); } $arg_types[] = $argument['type']; } } // Must have Year, Month, and Day or Year and Week calendar arguments. if (!in_array('calendar_year', $arg_types) && ((!in_array('calendar_month', $arg_types) && !in_array('calendar_day', $arg_types) || !in_array('calendar_week', $arg_types)))) { form_error($form['argument'], t('The Calendar requres as arguments Calendar: Year, Calendar: Month, and Calendar: Day, or Calendar: Year and Calendar: Week')); } // CCK date fields cannot use grouped handler. $calendar_fields = array_keys(calendar_fields()); foreach ($view['field'] as $delta => $field) { if (in_array($field['field'], $calendar_fields) && $field['handler'] == 'content_views_field_handler_group') { form_error($form['field'][$delta]['handler'], t('Calendar CCK Date fields must be set to \'Do not group multiple values\'.')); } } } /** * Setup Calendar parameters. */ function _calendar_setup_form($view_name) { $view = views_load_view($view_name); $form = array(); $time = mktime(1, 15, 0, 1, 1, date('Y', time())); $time_options = array( 'G:i' => date('G:i', $time), 'g:ia' => date('g:ia', $time), 'g:iA' => date('g:iA', $time), 'g:i a' => date('g:i a', $time), 'g:i A' => date('g:i A', $time), 'H:i' => date('H:i', $time), 'h:ia' => date('h:ia', $time), 'h:iA' => date('h:iA', $time), 'h:i a' => date('h:i a', $time), 'h:i A' => date('h:i A', $time), ); $form['markup'] = array( '#type' => 'markup', '#value' => '