You are here

public function CalendarYearMonthDate::query in Calendar 8.2

Build the query based upon the formula.

Overrides Formula::query

File

src/Plugin/views/argument/CalendarYearMonthDate.php, line 106

Class

CalendarYearMonthDate
Argument handler for a day.

Namespace

Drupal\calendar\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  $this
    ->ensureMyTable();

  // Extend query logic only if field is of type daterange.
  if ($this
    ->getFieldDefinition()
    ->getType() === 'daterange') {
    if (!isset($this->argument)) {
      return;
    }
    else {
      $validated_argument = $this->calendarHelper
        ->getCalendarArguments($this->view);
    }
    $this->argument = $validated_argument[0]['argument'];
    $monthstartdate = new DrupalDateTime($this->argument . '01');
    $monthenddate = new DrupalDateTime($this->argument . '01');
    $monthenddate
      ->modify('last day of this month');
    $month_start = $monthstartdate
      ->format('Y-m-d H:i:s');
    $month_end = (string) $monthenddate
      ->format('Y-m-d H:i:s');
    $start_field_name = "{$this->tableAlias}.{$this->realField}";
    $end_field_name = substr($start_field_name, 0, -6) . '_end_value';
    $date_start = $this->query
      ->getDateFormat($this->query
      ->getDateField($start_field_name, TRUE), 'Y-m-d H:i:s', FALSE);
    $date_end = $this->query
      ->getDateFormat($this->query
      ->getDateField($end_field_name, TRUE), 'Y-m-d H:i:s', FALSE);
    $this->query
      ->setWhereGroup('OR', 'StartDate');
    $this->query
      ->setWhereGroup('OR', 'EndDate');
    $expression1 = "{$date_start} < '{$month_end}'";
    $expression2 = "{$date_end} > '{$month_start}'";
    $this->query
      ->addWhereExpression('StartDate', $expression1);
    $this->query
      ->addWhereExpression('EndDate', $expression2);
  }
}