From 94c83e1ccba17350637ec54639f64df9be2f4ed8 Mon Sep 17 00:00:00 2001
From: Joris Vercammen <joris.vercammen@calibrate.be>
Date: Wed, 8 May 2019 14:43:57 +0200
Subject: [PATCH 2/4] Add code for creating links

---
 .../facets/processor/DateItemProcessor.php    |   7 ++
 .../facets/query_type/SearchApiDate.php       | 108 ++++++++++++++++++
 2 files changed, 115 insertions(+)

diff --git a/src/Plugin/facets/processor/DateItemProcessor.php b/src/Plugin/facets/processor/DateItemProcessor.php
index 46fb530..31696f2 100644
--- a/src/Plugin/facets/processor/DateItemProcessor.php
+++ b/src/Plugin/facets/processor/DateItemProcessor.php
@@ -59,6 +59,7 @@ class DateItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
       '#options' => [
         'actual_date' => $this->t('Actual date with granularity'),
         'relative_date' => $this->t('Relative date'),
+        'config_relative' => $this->t('Configured relative date'),
       ],
     ];
 
@@ -67,6 +68,12 @@ class DateItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
       '#title' => $this->t('Granularity'),
       '#default_value' => $this->getConfiguration()['granularity'],
       '#options' => $this->granularityOptions(),
+      '#states' => [
+        'visible' => [
+          [':input[name="facet_settings[date_item][settings][date_display]"]' => ['value' => 'actual_date']],
+          [':input[name="facet_settings[date_item][settings][date_display]"]' => ['value' => 'relative_date']],
+        ],
+      ],
     ];
     $build['date_format'] = [
       '#type' => 'textfield',
diff --git a/src/Plugin/facets/query_type/SearchApiDate.php b/src/Plugin/facets/query_type/SearchApiDate.php
index 8dd0f34..b8f56bc 100644
--- a/src/Plugin/facets/query_type/SearchApiDate.php
+++ b/src/Plugin/facets/query_type/SearchApiDate.php
@@ -79,6 +79,13 @@ class SearchApiDate extends QueryTypeRangeBase {
     if ($this->getDateDisplay() === 'actual_date') {
       return $this->calculateRangeAbsolute($value);
     }
+    if ($this->getDateDisplay() === 'config_relative') {
+      $parts = explode('__', $value);
+      return [
+        'start' => date('U', strtotime($parts[0])),
+        'stop' => date('U', strtotime($parts[1])),
+      ];
+    }
   }
 
   /**
@@ -219,6 +226,9 @@ class SearchApiDate extends QueryTypeRangeBase {
     if ($this->getDateDisplay() === 'actual_date') {
       return $this->calculateResultFilterAbsolute($value);
     }
+    if ($this->getDateDisplay() === 'config_relative') {
+      return $this->calculateResultFilterConfigRelative($value);
+    }
   }
 
   /**
@@ -406,6 +416,104 @@ class SearchApiDate extends QueryTypeRangeBase {
     ];
   }
 
+  protected function calculateResultFilterConfigRelative($value) {
+    $now = new DrupalDateTime();
+    $now->setTimestamp(REQUEST_TIME);
+
+    $date = new DrupalDateTime();
+    $date->setTimestamp($value);
+
+    $future = $date > $now;
+    $interval = $now->diff($date);
+    $interval_days = $interval->days;
+
+    // @todo fix yesterday/today.
+    if ($interval_days == 0) {
+      return [
+        'display' => 'Today',
+        'raw' => $date->format('Y-m-dT00:00:00') . '__' . $date->format('Y-m-dT23:59:59'),
+      ];
+    }
+    if ($interval_days == 1 && !$future) {
+      $modify = clone $now;
+      $modify->modify('- 1 day');
+      return [
+        'display' => 'Yesterday',
+        'raw' => $modify
+            ->format('Y-m-dT00:00:00') . '__' . $now->format('Y-m-dT23:59:59'),
+      ];
+    }
+    if ($interval_days == 1 && $future) {
+      $modify = clone $now;
+      $modify->modify('+ 1 day');
+      return [
+        'display' => 'Tomorrow',
+        'raw' => $now
+            ->format('Y-m-dT00:00:00') . '__' . $modify->format('Y-m-dT23:59:59'),
+      ];
+    }
+
+    if ($interval_days <= 7 && !$future) {
+      $modify = clone $now;
+      $modify->modify('- 7 day');
+      return [
+        'display' => 'Last 7 days',
+        'raw' => $modify
+            ->format('Y-m-dT00:00:00') . '__' . $now->format('Y-m-dT23:59:59'),
+      ];
+    }
+    if ($interval_days <= 7 && $future) {
+      $modify = clone $now;
+      $modify->modify('+ 7 day');
+      return [
+        'display' => 'Coming 7 days',
+        'raw' => $now
+            ->format('Y-m-dT00:00:00') . '__' . $modify->format('Y-m-dT23:59:59'),
+      ];
+    }
+
+    if ($interval_days <= 30 && !$future) {
+      $modify = clone $now;
+      $modify->modify('- 30 day');
+      return [
+        'display' => 'Last 30 days',
+        'raw' => $modify
+            ->format('Y-m-dT00:00:00') . '__' . $now->format('Y-m-dT23:59:59'),
+      ];
+    }
+    if ($interval_days <= 30 && $future) {
+      $modify = clone $now;
+      $modify->modify('+ 30 day');
+      return [
+        'display' => 'Coming 30 days',
+        'raw' => $now
+            ->format('Y-m-dT00:00:00') . '__' . $modify->format('Y-m-dT23:59:59'),
+      ];
+    }
+    if ($interval_days > 30 && !$future) {
+      $modify = clone $now;
+      $modify->modify('- 30 day');
+      $far = clone $now;
+      $far->modify('- 250 year');
+      return [
+        'display' => 'Older',
+        'raw' => $far
+            ->format('Y-m-dT00:00:00') . '__' . $modify->format('Y-m-dT23:59:59'),
+      ];
+    }
+    if ($interval_days > 30 && $future) {
+      $modify = clone $now;
+      $modify->modify('+ 30 day');
+      $far = clone $now;
+      $far->modify('+ 250 year');
+      return [
+        'display' => 'Newer',
+        'raw' => $modify
+            ->format('Y-m-dT00:00:00') . '__' . $far->format('Y-m-dT23:59:59'),
+      ];
+    }
+  }
+
   /**
    * Retrieve configuration: Granularity to use.
    *
-- 
2.19.1

