availability_calendar_handler_sort_sql_date.inc in Availability Calendars 7.5
File
views/availability_calendar_handler_sort_sql_date.inc
View source
<?php
class availability_calendar_handler_sort_sql_date extends views_handler_sort_date {
function option_definition() {
$options = parent::option_definition();
$options['granularity'] = array(
'default' => 'day',
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['granularity']['#options'] = array(
'day' => t('Day'),
'week' => t('Week'),
'month' => t('Month'),
'year' => t('Year'),
);
}
function query() {
$this
->ensure_my_table();
switch ($this->options['granularity']) {
case 'day':
default:
$this->query
->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
break;
case 'month':
case 'year':
parent::query();
break;
case 'week':
$db_type = Database::getConnection()
->databaseType();
$field = "{$this->table_alias}.{$this->real_field}";
switch ($db_type) {
default:
case 'mysql':
$formula = "DATE_FORMAT({$field}, '%x%v')";
break;
case 'pgsql':
$formula = "TO_CHAR({$field}, 'IYYYIW')";
break;
case 'sqlite':
$formula = "strftime({$field}, %Y%W')";
break;
}
$this->query
->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
break;
}
}
}