Now all modules are in core modules folder

This commit is contained in:
Manuel Cillero 2017-08-08 12:14:45 +02:00
parent 5ba1cdfa0b
commit 05b6a91b0c
1907 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,266 @@
<?php
/**
* @file
* Defines date-related Views data and plugins:
*
* Date argument:
* A generic date argument that has an option to select one or more
* Views date fields to filter on, automatically adds them to the view,
* and then filters the view by the value of the selected field(s).
* The flexible argument will accept and evaluate most ISO date
* and period formats, like 2009-05-01, 2008-W25, P1W.
*
* Date filter:
* A generic date filter that has an option to select a
* Views date field to filter on, with a choice of a widget to use
* for the filter form and an option to set the default value to
* a set date or something like 'now +90 days'. If the operator is
* set to 'between' or 'not between' you can set a default value for
* both the from and to dates.
*
* Current date argument default
* Adds a default option to set the argument to the current date
* when the argument is empty.
*
* Date navigation attachment
* Navigation that can be attached to any display to create back/next
* links by date, requires the date argument and uses the current
* date argument default to set a starting point for the view.
*/
/**
* Implementation of hook_views_handlers() to register all of the basic handlers
* views uses.
*/
function date_api_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'date_api') .'/includes',
),
'handlers' => array(
'date_api_argument_handler' => array(
'parent' => 'views_handler_argument_date',
),
'date_api_filter_handler' => array(
'parent' => 'views_handler_filter_numeric',
),
),
);
}
/**
* Implementation of hook_views_plugins().
*/
function date_api_views_plugins() {
$path = drupal_get_path('module', 'date_api');
$views_path = drupal_get_path('module', 'views');
require_once "./$path/theme/theme.inc";
return array(
'module' => 'date_api', // This just tells our themes are elsewhere.
'display' => array(
// Parents are not really displays, just needed so the files can
// be included.
'parent' => array(
'no ui' => TRUE,
'handler' => 'views_plugin_display',
'path' => "$views_path/plugins",
'parent' => '',
),
'attachment' => array(
'no ui' => TRUE,
'handler' => 'views_plugin_display_attachment',
'path' => "$views_path/plugins",
'parent' => 'parent',
),
// Display plugin for date navigation.
'date_nav' => array(
'title' => t('Date browser'),
'help' => t('Date back/next navigation to attach to other displays. Requires the Date argument.'),
'handler' => 'date_plugin_display_attachment',
'parent' => 'attachment',
'path' => "$path/includes",
'theme' => 'views_view',
'use ajax' => TRUE,
'admin' => t('Date browser'),
'help topic' => 'date-browser',
),
),
'style' => array(
'parent' => array(
// this isn't really a display but is necessary so the file can
// be included.
'no ui' => TRUE,
'handler' => 'views_plugin_style',
'path' => "$views_path/plugins",
'theme file' => 'theme.inc',
'theme path' => "$views_path/theme",
'parent' => '',
),
'date_nav' => array(
'title' => t('Date browser style'),
'help' => t('Creates back/next navigation.'),
'handler' => 'date_navigation_plugin_style',
'path' => "$path/includes",
'parent' => 'parent',
'theme' => 'date_navigation',
'theme file' => 'theme.inc',
'theme path' => "$path/theme",
'uses row plugin' => FALSE,
'uses fields' => FALSE,
'uses options' => TRUE,
'type' => 'date_nav',
'even empty' => TRUE,
),
),
);
}
/**
* Implementation of hook_views_data().
*/
function date_api_views_data() {
$data = array();
$tables = module_invoke_all('date_api_tables');
foreach ($tables as $base_table) {
// The flexible date argument.
$data[$base_table]['date_argument'] = array(
'group' => t('Date'),
'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
'help' => t('Filter any Views !base_table date field by a date argument, using any common ISO date/period format (i.e. YYYY, YYYY-MM, YYYY-MM-DD, YYYY-W99, YYYY-MM-DD--P3M, P90D, etc).', array('!base_table' => $base_table)),
'argument' => array(
'handler' => 'date_api_argument_handler',
'empty field name' => t('Undated'),
'base' => $base_table,
),
);
// The flexible date filter.
$data[$base_table]['date_filter'] = array(
'group' => t('Date'),
'title' => t('Date (!base_table)', array('!base_table' => $base_table)),
'help' => t('Filter any Views !base_table date field.', array('!base_table' => $base_table)),
'filter' => array(
'handler' => 'date_api_filter_handler',
'empty field name' => t('Undated'),
'base' => $base_table,
),
);
}
return $data;
}
/**
* Identify all potential date/timestamp fields and cache the data.
*/
function date_api_fields($base = 'node', $reset = FALSE) {
static $fields = array();
$empty = array('name' => array(), 'alias' => array());
require_once('./'. drupal_get_path('module', 'date_api') .'/includes/date_api_fields.inc');
if (empty($fields[$base]) || $reset) {
$cid = 'date_api_fields_'. $base;
if (!$reset && $cached = cache_get($cid, 'cache_views')) {
$fields[$base] = $cached->data;
}
else {
$fields[$base] = _date_api_fields($base);
}
}
// Make sure that empty values will be arrays in he expected format.
return !empty($fields) && !empty($fields[$base]) ? $fields[$base] : $empty;
}
/**
* Central function for setting up the right timezone values
* in the SQL date handler.
*
* The date handler will use this information to decide if the
* database value needs a timezone conversion.
*
* In Views, we will always be comparing to a local date value,
* so the goal is to convert the database value to the right
* value to compare to the local value.
*/
function date_views_set_timezone(&$date_handler, &$view, $field) {
$tz_handling = $field['tz_handling'];
switch ($tz_handling) {
case 'date' :
$date_handler->db_timezone = 'UTC';
$date_handler->local_timezone_field = $field['timezone_field'];
$date_handler->offset_field = $field['offset_field'];
break;
case 'none':
$date_handler->db_timezone = date_default_timezone_name();
$date_handler->local_timezone = date_default_timezone_name();
break;
case 'utc':
$date_handler->db_timezone = 'UTC';
$date_handler->local_timezone = 'UTC';
break;
default :
$date_handler->db_timezone = 'UTC';
$date_handler->local_timezone = date_default_timezone_name();
break;
}
}
function date_views_querystring($view, $extra_params = array()) {
$query_params = array_merge($_GET, $extra_params);
// Allow NULL params to be removed from the query string.
foreach ($extra_params AS $key => $value) {
if (!isset($value)) {
unset($query_params[$key]);
}
}
// Filter the special "q" and "view" variables out of the query string.
$exclude = array('q');
// If we don't explicitly add a value for "view", filter it out.
if (empty($extra_params['view'])) {
$exclude[] = 'view';
}
$query = drupal_query_string_encode($query_params, $exclude);
// To prevent an empty query string from adding a "?" on to the end of a URL,
// we return NULL.
return !empty($query) ? $query : NULL;
}
/**
* Identify the base url of the page,
* needed when the calendar is embedded so we
* don't set the url to the calendar url.
*/
function date_views_page_url($view) {
if ($view->build_type == 'page') {
return date_views_real_url($view, $view->date_info->real_args);
}
else {
$block_identifier = isset($view->date_info->block_identifier) ? $view->date_info->block_identifier : 'mini';
return url($_GET['q'], date_views_querystring($view, array($block_identifier => NULL)), NULL, TRUE);
}
}
/**
* Figure out what the URL of the calendar view we're currently looking at is.
*/
function date_views_real_url($view, $args) {
if (empty($args)) {
return $view->date_info->url;
}
// Add non-calendar arguments to the base url.
$parts = explode('/', $view->date_info->url);
$bump = 0;
foreach ($parts as $delta => $part) {
// If one of the args is buried in the url, add it here and adjust
// the delta values we'll compare the calendar arg positions to.
if (drupal_substr($part, 0, 1) == '$') {
$parts[$delta] = array_shift($args);
$bump++;
}
}
foreach ($args as $delta => $arg) {
if (!in_array($delta + $bump, calendar_arg_positions($view)) && !empty($arg)) {
array_push($parts, $arg);
}
}
return implode('/', $parts);
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @file
* Default Views.
*/
function date_api_views_default_views() {
$view = new view;
$view->name = 'date_browser';
$view->description = 'Browse through nodes by year, month, day, or week. Date browser attachment adds back/next navigation to the top of the page.';
$view->tag = 'Date';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = TRUE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('arguments', array(
'date_argument' => array(
'default_action' => 'default',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'date',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'date_fields' => array(
'node.changed' => 'node.changed',
),
'year_range' => '-3:+3',
'date_method' => 'OR',
'granularity' => 'month',
'id' => 'date_argument',
'table' => 'node',
'field' => 'date_argument',
'relationship' => 'none',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(),
'validate_argument_type' => 'tid',
'validate_argument_php' => '',
'default_options_div_prefix' => '',
'validate_argument_signup_status' => 'any',
'validate_argument_signup_node_access' => 0,
'override' => array(
'button' => 'Override',
),
),
));
$handler->override_option('access', array(
'type' => 'none',
'role' => array(),
'perm' => '',
));
$handler->override_option('items_per_page', 0);
$handler->override_option('use_pager', '1');
$handler->override_option('row_plugin', 'node');
$handler->override_option('row_options', array(
'teaser' => 1,
'links' => 1,
'comments' => 0,
));
$handler = $view->new_display('page', 'Page', 'page');
$handler->override_option('path', 'date-browser');
$handler->override_option('menu', array(
'type' => 'none',
'title' => '',
'weight' => 0,
'name' => 'navigation',
));
$handler->override_option('tab_options', array(
'type' => 'none',
'title' => '',
'weight' => 0,
));
$handler = $view->new_display('date_nav', 'Date browser', 'date_nav_1');
$handler->override_option('style_plugin', 'date_nav');
$handler->override_option('row_plugin', 'fields');
$handler->override_option('row_options', array());
$handler->override_option('attachment_position', 'before');
$handler->override_option('inherit_arguments', TRUE);
$handler->override_option('inherit_exposed_filters', TRUE);
$handler->override_option('displays', array(
'page' => 'page',
'default' => 0,
));
$views[$view->name] = $view;
return $views;
}

View file

@ -0,0 +1,398 @@
<?php
/**
* @file
* Views argument handler.
*/
/**
* Date API argument handler.
*/
class date_api_argument_handler extends views_handler_argument_date {
var $offset = NULL;
function construct() {
parent::construct();
require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_sql.inc');
$this->date_handler = new date_sql_handler();
$this->date_handler->construct();
}
/**
* Get granularity and use it to create the formula and a format
* for the results.
*/
function init(&$view, $options) {
parent::init($view, $options);
// Identify the type of display we're using.
$this->display_handler = $view->display_handler->definition['handler'];
// Add a date handler to the display.
$date_handler = $this->date_handler;
$date_handler->granularity = $this->options['granularity'];
$this->format = $date_handler->views_formats($date_handler->granularity, 'display');
$this->sql_format = $date_handler->views_formats($date_handler->granularity, 'sql');
if (empty($this->view->date_info)) $this->view->date_info = new stdClass();
// Set the view range, do this only if not already set in case there are multiple date arguments.
if (empty($this->view->date_info->min_allowed_year)) {
$range = date_range_years($this->options['year_range']);
$this->view->date_info->min_allowed_year = !empty($range) && is_array($range) ? $range[0] : variable_get('min_allowed_year', 100);
$this->view->date_info->max_allowed_year = !empty($range) && is_array($range) ? $range[1] : variable_get('max_allowed_year', 4000);
}
if (empty($this->view->date_info->date_fields)) {
$this->view->date_info->date_fields = array();
}
$this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
}
/**
* Default value for the date_fields option.
*/
function option_definition() {
$options = parent::option_definition();
$options['date_fields'] = array('default' => array());
$options['year_range'] = array('default' => '-3:+3', 'export' => 'export_plugin');
$options['date_method'] = array('default' => 'OR', 'export' => 'export_plugin');
$options['granularity'] = array('default' => 'month', 'export' => 'export_plugin');
$options['default_argument_type'] = array('default' => 'date', 'export' => 'export_plugin');
return $options;
}
/**
* Make sure our custom options get exported.
* Handle the options we know about, pass the rest to the parent plugin.
*/
function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
$output = '';
if (in_array($option, array('year_range', 'granularity', 'default_argument_type', 'date_method'))) {
$name = $this->options[$option];
$output .= $indent . $prefix . "['$option'] = '$name';\n";
return $output;
}
return parent::export_plugin($indent, $prefix, $storage, $option, $definition, $parents);
}
/**
* Add a form element to select date_fields for this argument.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$options = $this->date_handler->date_parts();
unset($options['second'], $options['minute']);
$options += array('week' => date_t('Week', 'datetime'));
$form['granularity'] = array(
'#title' => t('Granularity'),
'#type' => 'radios',
'#options' => $options,
'#default_value' => $this->options['granularity'],
'#multiple' => TRUE,
'#description' => t("Select the type of date value to be used in defaults, summaries, and navigation. For example, a granularity of 'month' will set the default date to the current month, summarize by month in summary views, and link to the next and previous month when using date navigation."),
);
$form['year_range'] = array(
'#title' => t('Date year range'),
'#type' => 'textfield',
'#default_value' => $this->options['year_range'],
'#description' => t("Set the allowable minimum and maximum year range for this argument, either a -X:+X offset from the current year, like '-3:+3' or an absolute minimum and maximum year, like '2005:2010'. When the argument is set to a date outside the range, the page will be returned as 'Page not found (404)'."),
);
$fields = date_api_fields($this->definition['base']);
$options = array();
foreach ($fields['name'] as $name => $field) {
$options[$name] = $field['label'];
}
// If this argument was added as a CCK field argument and no other date field
// has been chosen, update the default with the right date.
if (empty($this->options['date_fields']) && $this->field != 'date_argument') {
$this->options['date_fields'] = array($this->table .'.'. $this->field);
}
$form['date_fields'] = array(
'#title' => t('Date field(s)'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $this->options['date_fields'],
'#multiple' => TRUE,
'#description' => t("Select one or more date fields to filter with this argument. Do not select both the 'From date' and 'To date' for CCK date fields, only one of them is needed."),
);
$form['date_method'] = array(
'#title' => t('Method'),
'#type' => 'radios',
'#options' => array('OR' => t('OR'), 'AND' => t('AND')),
'#default_value' => $this->options['date_method'],
'#description' => t('Method of handling multiple date fields in the same query. Return items that have any matching date field (date = field_1 OR field_2), or only those with matches in all selected date fields (date = field_1 AND field_2).'),
);
}
function options_validate($form, &$form_state) {
// It is very important to call the parent function here:
parent::options_validate($form, $form_state);
if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
$check_fields = array_filter($form_state['values']['options']['date_fields']);
if (empty($check_fields)) {
form_error($form['date_fields'], t('You must select at least one date field for this argument.'));
}
if (!preg_match('/^(?:\-[0-9]{1,4}|[0-9]{4}):(?:[\+|\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) {
form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9'));
}
}
}
function options_submit($form, &$form_state) {
// It is very important to call the parent function here:
parent::options_submit($form, $form_state);
if ($form_state['values']['form_id'] == 'views_ui_config_item_form') {
$form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
}
}
// Update the summary values to show selected granularity.
function admin_summary() {
$fields = date_api_fields($this->definition['base']);
if (!empty($this->options['date_fields'])) {
$output = array();
foreach ($this->options['date_fields'] as $field) {
$output[] = $fields['name'][$field]['label'];
}
return implode('<br />'. $this->options['date_method'] .' ', $output);
}
else {
return parent::admin_summary();
}
}
/**
* Set the empty argument value to the current date,
* formatted appropriately for this argument.
*/
function get_default_argument($raw = FALSE) {
$granularity = $this->options['granularity'];
if (!$raw && $this->options['default_argument_type'] == 'date') {
if ($granularity == 'week') {
$now = date_now();
$week = date_week(date_format($now, 'Y-m-d'));
return date_format($now, 'Y') .'-W'. $week;
}
else {
return date($this->format(), time());
}
}
else {
return parent::get_default_argument($raw);
}
}
function format() {
if (!empty($this->options['granularity'])) {
$date_handler = new date_sql_handler();
return $date_handler->views_formats($this->options['granularity']);
}
else {
return !empty($this->options[$this->option_name]) ? $this->options[$this->option_name] : 'Y-m';
}
}
/**
* Provide a link to the next level of the view from the summary.
*/
function summary_name($data) {
$format = $this->date_handler->views_formats($this->options['granularity'], 'display');
$value = $data->{$this->name_alias};
$range = $this->date_handler->arg_range($value);
return date_format_date($range[0], 'custom', $format);
}
/**
* Provide the argument to use to link from the summary to the next level;
* this will be called once per row of a summary, and used as part of
* $view->get_url().
*
* @param $data
* The query results for the row.
*/
function summary_argument($data) {
$format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
$value = $data->{$this->name_alias};
$range = $this->date_handler->arg_range($value);
return date_format_date($range[0], 'custom', $format);
}
/**
* Provide a link to the next level of the view from the argument.
*/
function title() {
$format = $this->date_handler->views_formats($this->options['granularity'], 'display');
$range = $this->date_handler->arg_range($this->argument);
return date_format_date($range[0], 'custom', $format);
}
/**
* Create a summary query that matches the granularity.
*
* Needed or Views will do a groupby on the complete date instead
* of only the part of the date actually used in the argument.
*/
function summary_query() {
$this->get_query_fields();
// No way to do summaries on more than one field at a time.
if (count($this->query_fields) > 1) {
return;
}
// Cause query->ensure_table to perform the correct join.
$this->table = $this->query_fields[0]['field']['table_name'];
$this->ensure_my_table();
$field = $this->query_fields[0]['field'];
$date_handler = $this->query_fields[0]['date_handler'];
// Get the SQL format for this granularity, like Y-m,
// and use that as the grouping value.
$format = $date_handler->views_formats($this->options['granularity'], 'sql');
$this->formula = $date_handler->sql_format($format, $date_handler->sql_field($field['fullname']));
// Add the computed field.
$this->base_alias = $this->name_alias = $this->query->add_field(NULL, $this->formula, $field['query_name']);
return $this->summary_basics(FALSE);
}
function get_query_fields() {
$fields = date_api_fields($this->definition['base']);
$fields = $fields['name'];
$min_date = isset($this->min_date) ? $this->min_date : NULL;
$min_utc = isset($this->min_utc) ? $this->min_utc : NULL;
$max_date = isset($this->max_date) ? $this->max_date : NULL;
$max_utc = isset($this->max_utc) ? $this->max_utc : NULL;
$this->query_fields = array();
foreach ($this->options['date_fields'] as $delta => $name) {
if (array_key_exists($name, $fields) && $field = $fields[$name]) {
$date_handler = new date_sql_handler();
$date_handler->construct($field['sql_type'], date_default_timezone_name());
$date_handler->granularity = $this->options['granularity'];
date_views_set_timezone($date_handler, $this, $field);
$this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
}
}
}
/**
* Make sure the date field is added to the query.
*
* Do this in pre_query() so it will get done even if the argument
* is the wildcard, since query() is skipped when the wildcard is used.
*/
function pre_query() {
// Unset invalid date values before the query runs.
if (!empty($this->view->args) && count($this->view->args) > $this->position) {
$argument = $this->view->args[$this->position];
$parts = $this->date_handler->arg_parts($argument);
if (empty($parts[0]['date']) && empty($parts[0]['period'])) {
unset($this->view->args[$this->position]);
}
}
}
/**
* Set up the query for this argument.
*
* The argument sent may be found at $this->argument.
*/
function query() {
$block_identifier = date_block_identifier($this->view);
if (!empty($this->view->block_identifier) || isset($_GET[$block_identifier])) {
// Retrieve the block arguments in a way that will work for
// urls like user/%/calendar/2009-04.
if (!empty($_GET[$block_identifier])) {
$path_args = explode('/', $this->view->get_path());
$mini_args = explode('/', $_GET[$block_identifier]);
foreach ($path_args as $pos => $key) {
if ($path_args[$pos] != '%') {
unset($mini_args[$pos]);
}
}
// Get rid of gaps in the array caused by embedded args.
$mini_args = array_values($mini_args);
$this->view->args = $mini_args;
}
$i = 0;
foreach ($this->view->argument as $argument) {
if ($argument->field == 'date_argument') {
$this->argument = $this->view->args[$argument->position];
break;
}
$i++;
}
}
$parts = $this->date_handler->arg_parts($this->argument);
foreach ($parts as $type) {
foreach ($type as $part) {
foreach ($part as $key => $value) {
if (!empty($value)) {
// The last part evaluated is the one that will 'stick'
// as the date type.
$this->granularity = $key;
$this->$key = $value;
}
}
}
}
$range = $this->date_handler->arg_range($this->argument);
$min_date = $range[0];
$max_date = $range[1];
$this->min_date = $min_date;
$this->max_date = $max_date;
// See if we're outside the allowed date range for our argument.
if (date_format($min_date, 'Y') < $this->view->date_info->min_allowed_year || date_format($max_date, 'Y') > $this->view->date_info->max_allowed_year) {
$this->forbid = TRUE;
$this->query->add_where('date', "0=1");
return;
}
// The second option seems to work better in the block view if
// set to something other than the original value.
// Need to keep an eye on this to be sure nothing else breaks.
//$format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
$format = $this->date_handler->views_formats($this->granularity, 'sql');
$this->get_query_fields();
if (!empty($this->query_fields)) {
// Use set_where_group() with the selected date_method
// of 'AND' or 'OR' to create the where clause.
$this->query->set_where_group($this->options['date_method'], 'date');
foreach ($this->query_fields as $query_field) {
$field = $query_field['field'];
if ($field['table_name'] != $this->table || !empty($this->relationship)) {
$this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
}
$date_handler = $query_field['date_handler'];
$table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
$from_field = str_replace($field['table_name'] .'_', $table_alias .'.', $field['fromto'][0]);
$to_field = str_replace($field['table_name'] .'_', $table_alias .'.', $field['fromto'][1]);
if ($this->granularity != 'week') {
$from = $date_handler->sql_where_format($format, $from_field, '<=', date_format($max_date, $format));
$to = $date_handler->sql_where_format($format, $to_field, '>=', date_format($min_date, $format));
}
else {
$format = DATE_FORMAT_DATETIME;
$from = $date_handler->sql_where_date('DATE', $from_field, '<=', date_format($max_date, $format));
$to = $date_handler->sql_where_date('DATE', $to_field, '>=', date_format($min_date, $format));
}
$sql = str_replace('***table***', $field['table_name'], "($from AND $to)");
if ($sql) {
$this->query->add_where('date', $sql);
}
}
}
}
}

View file

@ -0,0 +1,179 @@
<?php
/**
* @file
* Identification for fields the Date handlers can use.
*/
/**
* Identify all potential date/timestamp fields.
*
* @return
* array with fieldname, type, and table.
* @see date_api_date_api_fields() which implements
* the hook_date_api_fields() for the core date fields.
*/
function _date_api_fields($base = 'node') {
// Make sure $base is never empty.
if (empty($base)) {
$base = 'node';
}
$cid = 'date_api_fields_'. $base;
cache_clear_all($cid, 'cache_views');
$all_fields = date_api_views_fetch_fields($base, 'field');
$fields = array();
foreach ((array) $all_fields as $name => $val) {
$fromto = array();
$tmp = explode('.', $name);
$field_name = $tmp[1];
$table_name = $tmp[0];
$alias = str_replace('.', '_', $name);
if (!$handler = views_get_handler($table_name, $field_name, 'field')) {
continue;
}
$handler_name = $handler->definition['handler'];
$type = '';
// For cck fields, get the date type.
$custom = array();
if (isset($handler->content_field)) {
if ($handler->content_field['type'] == 'date') {
$type = 'cck_string';
}
elseif ($handler->content_field['type'] == 'datestamp') {
$type = 'cck_timestamp';
}
elseif ($handler->content_field['type'] == 'datetime') {
$type = 'cck_datetime';
}
}
// Allow custom modules to provide date fields.
// The is_a() function makes this work for any handler
// that was derived from 'views_handler_field_date'.
// Unfortunately is_a() is deprecated in PHP 5.2, so we need
// a more convoluted test.
elseif ((version_compare(PHP_VERSION, '5.2', '<') && is_a($handler, 'views_handler_field_date')) || ($handler instanceof views_handler_field_date)) {
foreach (module_implements('date_api_fields') as $module) {
$function = $module .'_date_api_fields';
if ($custom = $function("$table_name.$field_name")) {
$type = 'custom';
break;
}
}
}
// Don't do anything if this is not a date field we can handle.
if (!empty($type)) {
// Handling for simple timestamp fields
$fromto = array($alias, $alias);
$tz_handling = 'site';
$related_fields = array();
$timezone_field = '';
$offset_field = '';
$rrule_field = '';
$delta_field = '';
$granularity = array('year', 'month', 'day', 'hour', 'minute');
// Handling for content field dates
if (isset($handler->content_field['tz_handling'])) {
$tz_handling = $handler->content_field['tz_handling'];
$db_info = content_database_info($handler->content_field);
if ($tz_handling == 'date') {
$offset_field = $table_name .'.'. $db_info['columns']['offset']['column'];
}
$related_fields = array(
$table_name .'.'. $field_name
);
if (isset($db_info['columns']['value2']['column'])) {
$related_fields = array_merge($related_fields, array($table_name .'.'. $db_info['columns']['value2']['column']));
}
if (isset($db_info['columns']['timezone']['column'])) {
$related_fields = array_merge($related_fields, array($table_name .'.'. $db_info['columns']['timezone']['column']));
$timezone_field = $table_name .'.'. $db_info['columns']['timezone']['column'];
}
if (isset($db_info['columns']['rrule']['column'])) {
$related_fields = array_merge($related_fields, array($table_name .'.'. $db_info['columns']['rrule']['column']));
$rrule_field = $table_name .'.'. $db_info['columns']['rrule']['column'];
}
}
// Get the delta value into the query.
if (!empty($handler->content_field['multiple'])) {
array_push($related_fields, "$table_name.delta");
$delta_field = $table_name .'_delta';
}
// Handling for cck fromto dates
if (isset($handler->content_field)) {
switch ($handler->content_field['type']) {
case 'date':
case 'datetime':
case 'datestamp':
$db_info = content_database_info($handler->content_field);
$fromto = array(
$table_name .'_'. $db_info['columns']['value']['column'],
$table_name .'_'. (!empty($handler->content_field['todate']) ? $db_info['columns']['value2']['column'] : $db_info['columns']['value']['column']),
);
break;
}
$granularity = !empty($handler->content_field['granularity']) ? $handler->content_field['granularity'] : array('year', 'month', 'day', 'hour', 'minute');
}
// 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
switch ($type) {
case 'cck_string':
$sql_type = DATE_ISO;
break;
case 'cck_datetime':
$sql_type = DATE_DATETIME;
break;
default:
$sql_type = DATE_UNIX;
break;
}
$fields['name'][$name] = array(
'type' => $type,
'sql_type' => $sql_type,
'label' => $val['group'] .': '. $val['title'],
'granularity' => $granularity,
'fullname' => $name,
'table_name' => $table_name,
'field_name' => $field_name,
'query_name' => $alias,
'fromto' => $fromto,
'tz_handling' => $tz_handling,
'offset_field' => $offset_field,
'timezone_field' => $timezone_field,
'rrule_field' => $rrule_field,
'related_fields' => $related_fields,
'delta_field' => $delta_field,
);
// Allow the custom fields to over-write values.
if (!empty($custom)) {
foreach ($custom as $key => $val) {
$fields['name'][$name][$key] = $val;
}
}
if (isset($handler->content_field)) {
if (drupal_substr($field_name, -1) == '2') {
$len = (drupal_strlen($field_name) - 7);
}
else {
$len = (drupal_strlen($field_name) - 6);
}
$fields['name'][$name]['real_field_name'] = drupal_substr($field_name, 0, $len);
}
else {
$fields['name'][$name]['real_field_name'] = $field_name;
}
$fields['alias'][$alias] = $fields['name'][$name];
}
}
cache_set($cid, $fields, 'cache_views');
return $fields;
}

View file

@ -0,0 +1,617 @@
<?php
/**
* @file
* Date Views filter handler.
*/
/**
* A flexible, configurable date filter.
*
* This filter allows you to select a granularity of date parts to filter on,
* such as year, month, day, etc.
*
* Each part can be set to blank to show all values; 'now' to filter for
* the current value of that part, or a specific value.
*
* An adjustment field is provided that will adjust the selected filter
* value by something like '+90 days' or '-1 month';
*/
class date_api_filter_handler extends views_handler_filter_numeric {
var $date_handler = NULL;
var $offset = NULL;
// Add a date handler to the filter.
function construct() {
parent::construct();
require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_sql.inc');
$this->date_handler = new date_sql_handler();
$this->date_handler->construct();
$this->date_handler->granularity = $this->options['granularity'];
}
function init(&$view, $options) {
parent::init($view, $options);
$handler = $this->date_handler;
$handler->granularity = isset($options['granularity']) ? $options['granularity'] : 'day';
$handler->adjustment_field = isset($options['adjustment_field']) ? $options['adjustment_field'] : 0;
if (empty($this->view->date_info)) $this->view->date_info = new stdClass();
// Set the view range, do this only if not already set in case there are multiple date arguments.
if (empty($this->view->date_info->min_allowed_year)) {
$range = date_range_years($this->options['year_range']);
$this->view->date_info->min_allowed_year = !empty($range) && is_array($range) ? $range[0] : variable_get('min_allowed_year', 100);
$this->view->date_info->max_allowed_year = !empty($range) && is_array($range) ? $range[1] : variable_get('max_allowed_year', 4000);
}
if (empty($this->view->date_info->date_fields)) {
$this->view->date_info->date_fields = array();
}
$this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
$this->definition['allow empty'] = TRUE;
// If no value has been submitted on an exposed filter it is treated as
// a submitted value. Send a flag to the date widget processors so they
// know to set '#value' instead of '#default_value' in that case.
$this->force_value = FALSE;
if (empty($this->options['exposed']) || (isset($this->options['expose']['identifier']) && !isset($_GET[$this->options['expose']['identifier']]))) {
$this->force_value = TRUE;
}
}
// Set default values for the date filter.
function option_definition() {
$options = parent::option_definition();
$options['date_fields'] = array('default' => array());
$options['date_method'] = array('default' => 'OR', 'export' => 'export_plugin');
$options['granularity'] = array('default' => 'day', 'export' => 'export_plugin');
$options['form_type'] = array('default' => 'date_select', 'export' => 'export_plugin');
$options['default_date'] = array('default' => '', 'export' => 'export_plugin');
$options['default_to_date'] = array('default' => '', 'export' => 'export_plugin');
$options['year_range'] = array('default' => '-3:+3', 'export' => 'export_plugin');
return $options;
}
function operators() {
$operators = parent::operators();
$operators['contains'] = array(
'title' => t('Contains'),
'short' => t('contains'),
'values' => 1,
);
return $operators;
}
/**
* Make sure our custom options get exported.
* Handle the options we know about, pass the rest to the parent plugin.
*/
function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
$output = '';
if (in_array($option, array('date_method', 'granularity', 'form_type', 'default_date', 'default_to_date', 'year_range'))) {
$name = $this->options[$option];
$output .= $indent . $prefix . "['$option'] = '$name';\n";
return $output;
}
return parent::export_plugin($indent, $prefix, $storage, $option, $definition, $parents);
}
/**
* Set the granularity of the date parts to use in the filter.
*/
function has_extra_options() { return TRUE; }
/**
* Date selection options.
*
* TODO Only select widget is working right now.
*/
function widget_options() {
$options = array(
'date_select' => t('Select'),
'date_text' => t('Text'),
'date_popup' => t('Popup'),
);
if (!module_exists('date_popup')) {
unset($options['date_popup']);
}
return $options;
}
function year_range() {
$year_range = explode(':', $this->options['year_range']);
if (drupal_substr($this->options['year_range'], 0, 1) == '-' || $year_range[0] < 0) {
$this_year = date_format(date_now(), 'Y');
$year_range[0] = $this_year + $year_range[0];
$year_range[1] = $this_year + $year_range[1];
}
return $year_range;
}
function extra_options_form(&$form, &$form_state) {
$form['form_type'] = array(
'#type' => 'radios',
'#title' => t('Date form type'),
'#default_value' => $this->options['form_type'],
'#options' => $this->widget_options(),
'#description' => t('Choose the form element to use for date selection.'),
);
$form['granularity'] = $this->date_handler->granularity_form($this->options['granularity']);
$form['granularity']['#description'] = '<p>'. t("Select a granularity for the date filter. For instance, selecting 'day' will create a filter where users can select the year, month, and day.") .'</p>';
$form['year_range'] = array(
'#title' => t('Date year range'),
'#type' => 'textfield',
'#default_value' => $this->options['year_range'],
'#description' => t("Set the allowable minimum and maximum year range for this argument, either a -X:+X offset from the current year, like '-3:+3' or an absolute minimum and maximum year, like '2005:2010'. When the argument is set to a date outside the range, the page will be returned as 'Page not found (404)'."),
);
$fields = date_api_fields($this->definition['base']);
$options = array();
foreach ($fields['name'] as $name => $field) {
$options[$name] = $field['label'];
}
// If this filter was added as a CCK field filter and no other date field
// has been chosen, update the default with the right date.
if (empty($this->options['date_fields']) && $this->field != 'date_filter') {
$this->options['date_fields'] = array($this->table .'.'. $this->field);
}
$form['date_fields'] = array(
'#title' => t('Date field(s)'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $this->options['date_fields'],
'#multiple' => FALSE,
'#description' => t('Select date field(s) to filter with this argument.'),
);
$form['date_method'] = array(
'#title' => t('Method'),
'#type' => 'radios',
'#options' => array('OR' => t('OR'), 'AND' => t('AND')),
'#default_value' => $this->options['date_method'],
'#description' => t('Method of handling multiple date fields in the same query. Return items that have any matching date field (date = field_1 OR field_2), or only those with matches in all selected date fields (date = field_1 AND field_2).'),
);
}
function extra_options_validate($form, &$form_state) {
$check_fields = array_filter($form_state['values']['options']['date_fields']);
if (empty($check_fields)) {
form_error($form['date_fields'], t('You must select at least one date field for this filter.'));
}
if (!preg_match('/^(?:\-[0-9]{1,4}|[0-9]{4}):(?:[\+|\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) {
form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9'));
}
}
function extra_options_submit($form, &$form_state) {
$form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
}
/**
* Add the selectors to the value form using the date handler.
*/
function value_form(&$form, &$form_state) {
// We use different values than the parent form, so we must
// construct our own form element.
$form['value'] = array();
$form['value']['#tree'] = TRUE;
// We have to make some choices when creating this as an exposed
// filter form. For example, if the operator is locked and thus
// not rendered, we can't render dependencies; instead we only
// render the form items we need.
$which = 'all';
$source = '';
if (!empty($form['operator'])) {
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
}
if (!empty($form_state['exposed'])) {
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {
// exposed and locked.
$which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
}
else {
$source = 'edit-' . form_clean_id($this->options['expose']['operator']);
}
}
$handler = $this->date_handler;
if ($which == 'all' || $which == 'value') {
$form['value'] += $this->date_parts_form($form_state, 'value', $source, $which, $this->operator_values(1));
if ($this->force_value) {
$form['value']['value']['#force_value'] = TRUE;
}
}
if ($which == 'all' || $which == 'minmax') {
$form['value'] += $this->date_parts_form($form_state, 'min', $source, $which, $this->operator_values(2));
$form['value'] += $this->date_parts_form($form_state, 'max', $source, $which, $this->operator_values(2));
if ($this->force_value) {
$form['value']['min']['#force_value'] = TRUE;
$form['value']['max']['#force_value'] = TRUE;
}
}
// Add some text to make it clear when additional options are available.
$extra = '';
if (version_compare(PHP_VERSION, '5.2', '>=')) {
$extra = t(" You can use any values PHP's date_create() can understand, like between '12AM today' and '12AM tomorrow.");
}
$form['value']['value']['#prefix'] = '<div class="form-item"><label>' . t('Absolute value') . '</label></div>';
$form['value']['default_date'] = array(
'#type' => 'textfield',
'#title' => t('Date default'),
'#default_value' => $this->options['default_date'],
'#prefix' => '<div class="form-item"><label>' . t('Relative value') . '</label><p>' . t("Relative values will be used if no date is set above. Use 'now' to default to the current date at runtime or add modifiers like 'now +1 day'. The To date default value is used when the operator is set to 'between' or 'not between'.") . $extra .'</p><p>' . t('If the filter is exposed, these values will be used to set the inital value of the exposed filter. Leave both date and default values blank to start with no value in the exposed filter.') .'</p></div>',
);
$form['value']['default_to_date'] = array(
'#type' => 'textfield',
'#title' => t('To date default'),
'#default_value' => $this->options['default_to_date'],
);
// Test if this value is in the UI or exposed, only show these elements in the UI.
// We'll save it as an option rather than a value to store it for use
// in the exposed filter.
if (!empty($form_state['exposed'])) {
$form['value']['default_date']['#type'] = 'value';
$form['value']['default_date']['#value'] = $form['value']['default_date']['#default_value'];
$form['value']['default_to_date']['#type'] = 'value';
$form['value']['default_to_date']['#value'] = $form['value']['default_to_date']['#default_value'];
unset($form['value']['default_date']['#prefix']);
unset($form['value']['value']['#prefix']);
}
$form['value']['#theme'] = 'date_views_filter_form';
}
/**
* A form element to select date part values.
*
* @param string $prefix
* A prefix for the date values, 'value', 'min', or 'max'.
* @param string $source
* The operator for this element.
* @param string $which
* Which element to provide, 'all', 'value', or 'minmax'.
* @param array $operator_values
* An array of the allowed operators for this element.
* @param array $limit
* An array of date parts to limit this element to.
*
* @return
* The form date part element for this instance.
*/
function date_parts_form($form_state, $prefix, $source, $which, $operator_values) {
require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_elements.inc');
switch ($prefix) {
case 'min':
$name = t('From date');
break;
case 'max':
$name = t('To date');
break;
default:
$name = '';
break;
}
$type = $this->options['form_type'];
if ($type == 'date_popup' && !module_exists('date_popup')) {
$type = 'date_text';
}
$format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
$granularity = array_keys($this->date_handler->date_parts($this->options['granularity']));
// Don't set a default date in the UI, only in the exposed form.
$default_date = '';
if (!empty($form_state['exposed'])) {
$default_date = $this->default_value($prefix);
}
$form[$prefix] = array(
'#type' => $type,
'#title' => check_plain($name),
'#size' => 20,
'#default_value' => empty($this->force_value) && !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
'#date_format' => date_limit_format($format, $granularity),
'#date_label_position' => 'within',
'#date_year_range' => $this->options['year_range'],
);
if ($which == 'all') {
$dependency = array(
'#process' => array($type .'_process', 'views_process_dependency'),
'#dependency' => array($source => $operator_values),
);
$form[$prefix] += $dependency;
}
return $form;
}
function default_value($prefix, $options = NULL) {
$default_date = '';
if (empty($options)) {
$options = $this->options;
}
// If this is a remembered value, use the value from the SESSION.
if (!empty($this->options['expose']['remember'])) {
$display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
$value = $_SESSION['views'][$this->view->name][$display_id]['date_filter'][$prefix];
if (!empty($value)) {
return $value;
}
}
// This is a date that needs to be constructed from options like 'now'.
$default_option = $prefix == 'max' ? $options['default_to_date'] : $options['default_date'];
if (!empty($default_option)) {
// For PHP 5.2+, take advantage of any strings that native PHP
// date_create() can handle. For older versions use strtotime().
if (version_compare(PHP_VERSION, '5.2', '<')) {
$now = strtotime($default_option .' UTC');
$date = date_create("@$now", timezone_open('UTC'));
}
else {
$date = date_create($default_option, date_default_timezone());
}
$default_date = date_format($date, DATE_FORMAT_DATETIME);
}
// This a fixed date.
else {
$default_date = $options['value'][$prefix];
}
return $default_date;
}
/**
* Value validation.
*
* TODO add in more validation.
*
* We are setting an extra option using a value form
* because it makes more sense to set it there.
* That's not the normal method, so we have to manually
* transfer the selected value back to the option.
*/
function value_validate($form, &$form_state) {
if (($form_state['values']['options']['operator'] == 'between' || $form_state['values']['options']['operator'] == 'not between') &&
!empty($form_state['values']['options']['value']['default_date']) && empty($form_state['values']['options']['value']['default_to_date'])) {
form_error($form['value']['default_to_date'], t('Please set a default value for the To date as well as the From date when using default values with the Between or Not between operators.'));
}
if (isset($form_state['values']['options']['value']['default_date'])) {
$this->options['default_date'] = $form_state['values']['options']['value']['default_date'];
$this->options['default_to_date'] = $form_state['values']['options']['value']['default_to_date'];
}
parent::value_validate($form, $form_state);
}
// Update the summary values to provide
// meaningful information for each option.
function admin_summary() {
if (empty($this->options['date_fields'])) {
return t('Missing date fields!');
}
$handler = $this->date_handler;
$fields = date_api_fields($this->definition['base']);
if (!empty($this->options['date_fields'])) {
$output = array();
foreach ($this->options['date_fields'] as $field) {
if (array_key_exists($field, $fields['name'])) {
$output[] = $fields['name'][$field]['label'];
}
}
}
$field = implode(' '. $this->options['date_method'] .' ', $output);
$output = "$field ". check_plain($this->operator) . ' ';
$parts = $handler->date_parts();
$widget_options = $this->widget_options();
// If the filter is exposed, display the granularity.
if ($this->options['exposed']) {
return t('(@field) <strong>Exposed</strong> @widget @format', array('@field' => $field, '@format' => $parts[$handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
}
// If not exposed, display the value.
if (in_array($this->operator, $this->operator_values(2))) {
$min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
$max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
$output .= t('@min and @max', array('@min' => $min, '@max' => $max));
}
else {
$output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
}
return $output;
}
// Views treats the form as though it has already been submitted
// even when it hasn't, so we when it is really not submitted we
// have to adjust the values to match what should have been the default.
// Overriding exposed_submit() will ensure that the $input value
// used by Views has been adjusted to the right value.
function exposed_submit(&$form, &$form_state) {
if ($this->force_value) {
$default = array();
foreach (array('value', 'min', 'max') as $prefix) {
$default[$prefix] = $this->default_value($prefix);
}
$form_state['values'][$this->options['expose']['identifier']] = $default;
}
}
/**
* Custom implementation of query() so we can get the
* AND and OR methods in the right places.
*/
function query() {
$this->get_query_fields();
if (!empty($this->query_fields)) {
foreach ((array) $this->query_fields as $query_field) {
$field = $query_field['field'];
if (!isset($this->query->table_queue[$field['table_name']])) {
if ($field['table_name'] != $this->table || !empty($this->relationship)) {
$this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
}
}
$table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
$query_field['field']['fullname'] = $table_alias .'.'. $query_field['field']['field_name'];
$sql = '';
$sql_parts = array();
switch ($this->operator) {
case 'between':
$sql_parts[] = $this->date_filter('min', $query_field, '>=');
$sql_parts[] = $this->date_filter('max', $query_field, '<=');
$sql = implode(' AND ', array_filter($sql_parts));
break;
case 'not between':
$sql_parts[] = $this->date_filter('min', $query_field, '<');
$sql_parts[] = $this->date_filter('max', $query_field, '>');
$sql = implode(' OR ', array_filter($sql_parts));
break;
case 'contains':
$query_field['field']['fullname'] = str_replace($field['table_name'] .'_', $table_alias .'.', $field['fromto'][0]);
$sql_parts[] = $this->date_filter('value', $query_field, '<=');
$query_field['field']['fullname'] = str_replace($field['table_name'] .'_', $table_alias .'.', $field['fromto'][1]);
$sql_parts[] = $this->date_filter('value', $query_field, '>=');
$sql = implode(' AND ', array_filter($sql_parts));
break;
default:
$sql = $this->date_filter('value', $query_field, $this->operator);
break;
}
if (!empty($sql)) {
// Use set_where_group() with the selected date_method
// of 'AND' or 'OR' to combine the field WHERE clauses.
$this->query->set_where_group($this->options['date_method'], 'date');
$this->query->add_where('date', $sql);
}
}
}
}
function date_filter($prefix, $query_field, $operator) {
$field = $query_field['field'];
// Handle the simple operators first.
if ($operator == 'empty') {
$this->add_date_field($field);
return $field['fullname'] .' IS NULL';
}
elseif ($operator == 'not empty') {
$this->add_date_field($field);
return $field['fullname'] .' IS NOT NULL';
}
// Views treats the default values as though they are submitted
// so we when it is really not submitted we have to adjust the
// query to match what should have been the default.
$value_parts = !is_array($this->value[$prefix]) ? array($this->value[$prefix]) : $this->value[$prefix];
foreach ($value_parts as $part) {
$default = $this->default_value($prefix);
if (!empty($this->force_value) && !empty($default)) {
$this->value[$prefix] = $default;
}
else {
if (empty($part)) {
return '';
}
}
}
$this->add_date_field($field);
$granularity = $this->options['granularity'];
$date_handler = $query_field['date_handler'];
$this->format = $date_handler->views_formats($granularity, 'sql');
$complete_date = date_fuzzy_datetime($this->value[$prefix]);
$date = date_make_date($complete_date);
$value = date_format($date, $this->format);
$range = $this->date_handler->arg_range($value);
$year_range = date_range_years($this->options['year_range']);
if ($this->operator != 'not between') {
switch ($operator) {
case '>':
case '>=':
$range[1] = date_make_date(date_pad($year_range[1], 4) .'-12-31 23:59:59');
if ($operator == '>') {
date_modify($range[0], '+1 second');
}
break;
case '<':
case '<=':
$range[0] = date_make_date(date_pad($year_range[0], 4) .'-01-01 00:00:00');
if ($operator == '<') {
date_modify($range[1], '-1 second');
}
break;
}
}
$min_date = $range[0];
$max_date = $range[1];
$this->min_date = $min_date;
$this->max_date = $max_date;
$this->year = date_format($date, 'Y');
$this->month = date_format($date, 'n');
$this->day = date_format($date, 'j');
$this->week = date_week(date_format($date, DATE_FORMAT_DATE));
$this->date_handler = $date_handler;
if ($this->date_handler->granularity == 'week') {
$this->format = DATE_FORMAT_DATETIME;
}
switch ($prefix) {
case 'min':
$value = date_format($min_date, $this->format);
break;
case 'max':
$value = date_format($max_date, $this->format);
break;
default:
$value = date_format($date, $this->format);
break;
}
if ($this->date_handler->granularity != 'week') {
$sql = $date_handler->sql_where_format($this->format, $field['fullname'], $operator, $value);
}
else {
$sql = $date_handler->sql_where_date('DATE', $field['fullname'], $operator, $value);
}
return $sql;
}
function add_date_field($field) {
// Explicitly add this table using add_table so Views does not
// remove it if it is a duplicate, since that will break the query.
$this->query->add_table($field['table_name'], NULL, NULL, $field['table_name']);
return;
}
function get_query_fields() {
$fields = date_api_fields($this->definition['base']);
$fields = $fields['name'];
$this->query_fields = array();
foreach ((array) $this->options['date_fields'] as $delta => $name) {
if (array_key_exists($name, $fields) && $field = $fields[$name]) {
$date_handler = new date_sql_handler();
$date_handler->construct($field['sql_type'], date_default_timezone_name());
$date_handler->granularity = $this->options['granularity'];
date_views_set_timezone($date_handler, $this->view, $field);
$this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
}
}
}
}

View file

@ -0,0 +1,68 @@
<?php
/**
* @file
* Date navigation style handler.
*/
/**
* Style plugin to create date back/next navigation.
*
* The style plugin passes some argument values to the theme, and
* ensures that the date argument is present and that the default
* value is set to the current date.
*/
class date_navigation_plugin_style extends views_plugin_style {
/**
* Style validation.
*/
function validate() {
$errors = parent::validate();
$arguments = $this->display->handler->get_option('arguments');
$count = 0;
$found = FALSE;
foreach ($arguments as $id => $argument) {
if ($argument['field'] == 'date_argument') {
if ($count > 0) {
$errors[] = t('The %style cannot use more than one Date: Date argument.', array('%style' => $this->definition['title']));
}
elseif ($argument['default_argument_type'] != 'date') {
$errors[] = t('The %style requires the Date: Date argument be set to default to the current date.', array('%style' => $this->definition['title']));
}
$count++;
$found = TRUE;
}
}
if (!$found) {
$errors[] = t('The %style requires the Date: Date argument.', array('%style' => $this->definition['title']));
}
return $errors;
}
function query() {
require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_sql.inc');
// Bring the argument information into the view so our theme can access it.
$i = 0;
foreach ($this->view->argument as $id => $argument) {
if ($id == 'date_argument') {
$this->view->date_info->granularity = $argument->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->url = $this->view->get_url();
}
$i++;
}
// bring the node type into the query so we can use it in the theme
$this->view->query->add_field('node', 'type');
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* @file
* Views attachment display handler.
*/
/**
* The plugin that handles date navigation attachments.
*
* Creates a special attachment for this purpose only.
*/
class date_plugin_display_attachment extends views_plugin_display_attachment {
// Require the date_nav style. That style has a date_nav type
// so it won't show up as a style option on any other display.
function get_style_type() { return 'date_nav'; }
// No options to set style, force it to the right value.
function defaultable_sections($section = NULL) {
if (in_array($section, array('row_options', 'row_plugin', 'items_per_page'))) {
return FALSE;
}
return parent::defaultable_sections($section);
}
function option_definition() {
$options = parent::option_definition();
$options['style_plugin']['default'] = 'date_nav';
$options['items_per_page']['default'] = 0;
$options['row_plugin']['default'] = '';
//$options['defaults']['default']['style_plugin'] = FALSE;
$options['defaults']['default']['style_options'] = FALSE;
$options['defaults']['default']['items_per_page'] = FALSE;
$options['defaults']['default']['row_plugin'] = FALSE;
$options['defaults']['default']['row_options'] = FALSE;
return $options;
}
}