calendar_plugin_style.inc in Calendar 6.2
File
includes/calendar_plugin_style.inc
View source
<?php
class calendar_plugin_style extends views_plugin_style {
function init(&$view, &$display, $options = NULL) {
parent::init($view, $display, $options);
if (!isset($view->date_info)) {
$view->date_info = new StdClass();
}
$view->date_info->display_types = $this
->display_types();
}
function display_types($granularity = NULL, $option_type = 'names') {
$ids = array();
$names = array();
foreach (calendar_display_types() as $name => $type) {
foreach ($this->view->display as $id => $display) {
if ($display->display_plugin == 'calendar_period') {
if (!empty($display->display_options['calendar_type']) && $display->display_options['calendar_type'] == $name) {
$attachments = array_filter($display->display_options['displays']);
if (isset($attachments['calendar_1'])) {
$ids[$name] = $id;
$names[$name] = $display->display_title;
}
}
}
}
}
if ($granularity) {
return ${$option_type}[$granularity];
}
return ${$option_type};
}
function date_fields() {
$date_fields = array();
$calendar_fields = date_api_fields($this->view->base_table);
$arguments = $this->display->handler
->get_option('arguments');
foreach ($arguments as $name => $argument) {
if (isset($argument['date_fields'])) {
foreach ($argument['date_fields'] as $date_field) {
$field = $calendar_fields['name'][$date_field];
$handler = views_get_handler($field['table_name'], $field['field_name'], 'field');
if ($handler) {
$date_fields[$date_field] = $field;
$date_fields[$date_field]['name'] = $handler
->ui_name();
}
}
}
}
return $date_fields;
}
function validate() {
$errors = parent::validate();
if (empty($this->display->display_options['style_plugin'])) {
return $errors;
}
$style = $this->display->display_options['style_plugin'];
$arguments = $this->display->handler
->get_option('arguments');
if (!in_array('date_argument', array_keys($arguments))) {
if (empty($this->view->date_info->arg_missing)) {
$errors[$style] = t("The @style style requires a Date argument.", array(
'@style' => $style,
));
}
$this->view->date_info->arg_missing = TRUE;
$this->date_info->arg_fields = array();
}
else {
$this->date_info->arg_fields = $arguments['date_argument']['date_fields'];
if ($arguments['date_argument']['default_action'] != 'default' || $arguments['date_argument']['default_argument_type'] != 'date') {
if (empty($this->view->date_info->arg_missing_default)) {
$errors[] = calendar_errors('missing_argument_default');
}
$this->view->date_info->arg_missing_default = TRUE;
}
}
if ($style == 'calendar_style') {
$view_fields = date_api_fields($this->view->base_table);
$view_fields = $view_fields['name'];
$fields = $this->display->handler
->get_option('fields');
$has_fields = FALSE;
foreach ($fields as $column => $field) {
$field_name = $field['table'] . "." . $field['field'];
if (in_array($field_name, $this->date_info->arg_fields)) {
$has_fields = TRUE;
}
if (!empty($field['multiple']) && array_key_exists($field_name, $view_fields)) {
$cck_fields = content_fields();
$real_name = $view_fields[$field_name]['real_field_name'];
if ($cck_fields[$real_name]['multiple'] && !empty($field['multiple']['group'])) {
$errors[] = t("The date field '@field' used by the display '@display_title' cannot be set to 'Group multiple values'.", array(
'@field' => $view_fields[$field_name]['label'],
'@display_title' => $this->display->display_title,
));
}
}
}
if (!$has_fields) {
$errors[] = t('The date argument date fields must be added to this query. You can exclude them if you do not want them displayed in the calendar.');
}
}
return $errors;
}
function query() {
require_once './' . drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
$style_options = $this->view->style_plugin->options;
$i = 0;
foreach ($this->view->argument as $id => $argument) {
if ($argument->field == 'date_argument') {
if (empty($argument->min_date)) {
$value = $argument
->get_default_argument();
$range = $argument->date_handler
->arg_range($value);
$argument->min_date = $range[0];
$argument->max_date = $range[1];
}
$this->view->date_info->granularity = !empty($argument->granularity) ? $argument->granularity : $argument->options['granularity'];
$this->view->date_info->date_arg = !empty($this->view->args) && count($this->view->args) > $argument->position ? $this->view->args[$argument->position] : '';
$this->view->date_info->date_arg_pos = $i;
$this->view->date_info->year = isset($argument->year) ? $argument->year : NULL;
$this->view->date_info->month = isset($argument->month) ? $argument->month : NULL;
$this->view->date_info->day = isset($argument->day) ? $argument->day : NULL;
$this->view->date_info->week = isset($argument->week) ? $argument->week : NULL;
$this->view->date_info->min_date = $argument->min_date;
$this->view->date_info->max_date = $argument->max_date;
$this->view->date_info->min_date_date = date_format($this->view->date_info->min_date, DATE_FORMAT_DATE);
$this->view->date_info->max_date_date = date_format($this->view->date_info->max_date, DATE_FORMAT_DATE);
$this->view->date_info->forbid = isset($argument->forbid) ? $argument->forbid : FALSE;
break;
}
$i++;
}
$this->view->date_info->display_types = $this
->display_types();
$keys = drupal_map_assoc(array_keys(calendar_display_types()));
$this->view->date_info->calendar_type = $keys[$this->view->date_info->granularity];
if ($this->view->base_table == 'node') {
$this->view->query
->add_field('node', 'type');
}
parent::query();
}
function render() {
return theme($this
->theme_functions(), $this->view, $this->options, array());
}
}