You are here

function availability_calendar_handler_sort_sql_date::query in Availability Calendars 7.5

Override to handle the week granularity and handle the day granularity in a different way.

Overrides views_handler_sort_date::query

File

views/availability_calendar_handler_sort_sql_date.inc, line 33

Class

availability_calendar_handler_sort_sql_date
Defines a sort handler for SQL date types.

Code

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':

      // Add the field.
      $db_type = Database::getConnection()
        ->databaseType();
      $field = "{$this->table_alias}.{$this->real_field}";
      switch ($db_type) {
        default:
        case 'mysql':

          // @todo: if first day of week is sunday we can use %X%V.
          $formula = "DATE_FORMAT({$field}, '%x%v')";
          break;
        case 'pgsql':
          $formula = "TO_CHAR({$field}, 'IYYYIW')";
          break;
        case 'sqlite':

          // %V should be used instead, but I think that will not change the
          // %Y modifier to return the year that belongs to the calculated
          // week number (2013-01-01 will result in 201352 instead of 201252).
          $formula = "strftime({$field}, %Y%W')";
          break;
      }
      $this->query
        ->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
      break;
  }
}