You are here

public function ReportDate::query in Commerce Reporting 8

Called to add the sort to a query.

Overrides Date::query

File

src/Plugin/views/sort/ReportDate.php, line 25

Class

ReportDate
Extends View basic sort handler for dates to add Week granularity.

Namespace

Drupal\commerce_reports\Plugin\views\sort

Code

public function query() {
  $this
    ->ensureMyTable();
  switch ($this->options['granularity']) {
    case 'second':
    default:
      $this->query
        ->addOrderBy($this->tableAlias, $this->realField, $this->options['order']);
      return;
    case 'minute':
      $formula = $this
        ->getDateFormat('YmdHi');
      break;
    case 'hour':
      $formula = $this
        ->getDateFormat('YmdH');
      break;
    case 'day':
      $formula = $this
        ->getDateFormat('Ymd');
      break;
    case 'week':
      $formula = $this
        ->getDateFormat('YW');
      break;
    case 'month':
      $formula = $this
        ->getDateFormat('Ym');
      break;
    case 'year':
      $formula = $this
        ->getDateFormat('Y');
      break;
  }

  // Add the field.
  $this->query
    ->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_' . $this->options['granularity']);
}