You are here

public function SearchApiDate::calculateResultFilterRelative in Facets 8

1 call to SearchApiDate::calculateResultFilterRelative()
SearchApiDate::calculateResultFilter in src/Plugin/facets/query_type/SearchApiDate.php
Calculates the result of the filter.

File

src/Plugin/facets/query_type/SearchApiDate.php, line 274

Class

SearchApiDate
Support for date facets within the Search API scope.

Namespace

Drupal\facets\Plugin\facets\query_type

Code

public function calculateResultFilterRelative($value) {
  $date = new DrupalDateTime();
  $date
    ->setTimestamp($value);
  $now = new DrupalDateTime();
  $now
    ->setTimestamp(\Drupal::time()
    ->getRequestTime());
  $interval = $date
    ->diff($now);
  $future = $date > $now;
  switch ($this
    ->getGranularity()) {
    case static::FACETAPI_DATE_YEAR:
      $rounded = new \DateInterval('P' . $interval->y . 'Y');
      if ($future) {
        $display = $interval->y ? $this
          ->formatPlural($interval->y, '1 year hence', '@count years hence') : $this
          ->t('In the next year');
        $now
          ->add($rounded);
      }
      else {
        $display = $interval->y ? $this
          ->formatPlural($interval->y, '1 year ago', '@count years ago') : $this
          ->t('In the last year');
        $now
          ->sub($rounded);
        $now
          ->sub(new \DateInterval('P1Y'));
      }
      $raw = $now
        ->format('Y-m');
      break;
    case static::FACETAPI_DATE_MONTH:
      $rounded = new \DateInterval('P' . $interval->y . 'Y' . $interval->m . 'M');
      $display = $interval->y ? $this
        ->formatPlural($interval->y, '1 year', '@count years') . ' ' : '';
      if ($future) {
        $display .= $interval->m ? $this
          ->formatPlural($interval->m, '1 month hence', '@count months hence') : (empty($display) ? $this
          ->t('In the next month') : $this
          ->t('0 months hence'));
        $now
          ->add($rounded);
      }
      else {
        $display .= $interval->m ? $this
          ->formatPlural($interval->m, '1 month ago', '@count months ago') : (empty($display) ? $this
          ->t('In the last month') : $this
          ->t('0 months ago'));
        $now
          ->sub($rounded);
        $now
          ->sub(new \DateInterval('P1M'));
      }
      $raw = $now
        ->format('Y-m-d');
      break;
    case static::FACETAPI_DATE_DAY:
      $rounded = new \DateInterval('P' . $interval->y . 'Y' . $interval->m . 'M' . $interval->d . 'D');
      $display = $interval->y ? $this
        ->formatPlural($interval->y, '1 year', '@count years') . ' ' : '';
      $display .= $interval->m ? $this
        ->formatPlural($interval->m, '1 month', '@count months') . ' ' : '';
      if ($future) {
        $display .= $interval->d ? $this
          ->formatPlural($interval->d, '1 day hence', '@count days hence') : (empty($display) ? $this
          ->t('In the next day') : $this
          ->t('0 days hence'));
        $now
          ->add($rounded);
      }
      else {
        $display .= $interval->d ? $this
          ->formatPlural($interval->d, '1 day ago', '@count days ago') : (empty($display) ? $this
          ->t('In the last day') : $this
          ->t('0 days ago'));
        $now
          ->sub($rounded);
        $now
          ->sub(new \DateInterval('P1D'));
      }
      $raw = $now
        ->format('Y-m-d\\TH');
      break;
    case static::FACETAPI_DATE_HOUR:
      $rounded = new \DateInterval('P' . $interval->y . 'Y' . $interval->m . 'M' . $interval->d . 'DT' . $interval->h . 'H');
      $display = $interval->y ? $this
        ->formatPlural($interval->y, '1 year', '@count years') . ' ' : '';
      $display .= $interval->m ? $this
        ->formatPlural($interval->m, '1 month', '@count months') . ' ' : '';
      $display .= $interval->d ? $this
        ->formatPlural($interval->d, '1 day', '@count days') . ' ' : '';
      if ($future) {
        $display .= $interval->h ? $this
          ->formatPlural($interval->h, '1 hour hence', '@count hours hence') : (empty($display) ? $this
          ->t('In the next hour') : $this
          ->t('0 hours hence'));
        $now
          ->add($rounded);
      }
      else {
        $display .= $interval->h ? $this
          ->formatPlural($interval->h, '1 hour ago', '@count hours ago') : (empty($display) ? $this
          ->t('In the last hour') : $this
          ->t('0 hours ago'));
        $now
          ->sub($rounded);
        $now
          ->sub(new \DateInterval('PT1H'));
      }
      $raw = $now
        ->format('Y-m-d\\TH:i');
      break;
    case static::FACETAPI_DATE_MINUTE:
      $rounded = new \DateInterval('P' . $interval->y . 'Y' . $interval->m . 'M' . $interval->d . 'DT' . $interval->h . 'H' . $interval->i);
      $display = $interval->y ? $this
        ->formatPlural($interval->y, '1 year', '@count years') . ' ' : '';
      $display .= $interval->m ? $this
        ->formatPlural($interval->m, '1 month', '@count months') . ' ' : '';
      $display .= $interval->d ? $this
        ->formatPlural($interval->d, '1 day', '@count days') . ' ' : '';
      $display .= $interval->h ? $this
        ->formatPlural($interval->h, '1 hour', '@count hours') . ' ' : '';
      if ($future) {
        $display .= $interval->i ? $this
          ->formatPlural($interval->i, '1 minute hence', '@count minutes hence') : (empty($display) ? $this
          ->t('In the next minute') : $this
          ->t('0 minutes hence'));
        $now
          ->add($rounded);
      }
      else {
        $display .= $interval->i ? $this
          ->formatPlural($interval->i, '1 minute ago', '@count minutes ago') : (empty($display) ? $this
          ->t('In the last minute') : $this
          ->t('0 minutes ago'));
        $now
          ->sub($rounded);
        $now
          ->sub(new \DateInterval('PT1M'));
      }
      $raw = $date
        ->format('Y-m-d\\TH:i:s');
      break;
    default:
      $rounded = new \DateInterval('P' . $interval->y . 'Y' . $interval->m . 'M' . $interval->d . 'DT' . $interval->h . 'H' . $interval->i . $interval->s . 'S');
      $display = $interval->y ? $this
        ->formatPlural($interval->y, '1 year', '@count years') . ' ' : '';
      $display .= $interval->m ? $this
        ->formatPlural($interval->m, '1 month', '@count months') . ' ' : '';
      $display .= $interval->d ? $this
        ->formatPlural($interval->d, '1 day', '@count days') . ' ' : '';
      $display .= $interval->h ? $this
        ->formatPlural($interval->h, '1 hour', '@count hours') . ' ' : '';
      $display .= $interval->i ? $this
        ->formatPlural($interval->i, '1 minute', '@count minutes') . ' ' : '';
      if ($future) {
        $display .= $interval->s ? $this
          ->formatPlural($interval->s, '1 second hence', '@count seconds hence') : (empty($display) ? $this
          ->t('In the next second') : $this
          ->t('0 secondss hence'));
        $now
          ->add($rounded);
      }
      else {
        $display .= $interval->s ? $this
          ->formatPlural($interval->s, '1 second ago', '@count seconds ago') : (empty($display) ? $this
          ->t('In the last second') : $this
          ->t('0 seconds ago'));
        $now
          ->sub($rounded);
        $now
          ->sub(new \DateInterval('PT1S'));
      }
      $raw = $date
        ->format('Y-m-d\\TH:i:s');
      break;
  }
  return [
    'display' => $display,
    'raw' => $raw,
  ];
}