CRM-19394: Relative date ranges no longer stored in Smart Group criteria
authordeb.monish <monish.deb@webaccessglobal.com>
Fri, 23 Sep 2016 13:37:02 +0000 (19:07 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Wed, 28 Sep 2016 05:41:36 +0000 (11:11 +0530)
CRM/Contact/BAO/SavedSearch.php
CRM/Contact/Form/Task/SaveSearch.php

index 90f4ddc53002379823fd093791bced33ef8efa74..bfc6e10f947b9df656bfe458d2e41b3f24d7d2af 100644 (file)
@@ -113,9 +113,14 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch {
           $value = CRM_Utils_Array::value(key($value), $value);
         }
         if (strpos($id, '_date_low') !== FALSE || strpos($id, '_date_high') !== FALSE) {
-          $result[$id] = date($dateFormat, strtotime($value));
           $entityName = strstr($id, '_date', TRUE);
-          $result["{$entityName}_date_relative"] = 0;
+          if (!empty($result['relative_dates']) && array_key_exists($entityName, $result['relative_dates'])) {
+            $result["{$entityName}_date_relative"] = $result['relative_dates'][$entityName];
+          }
+          else {
+            $result[$id] = date($dateFormat, strtotime($value));
+            $result["{$entityName}_date_relative"] = 0;
+          }
         }
         else {
           $result[$id] = $value;
@@ -389,4 +394,24 @@ LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_
     }
   }
 
+  /**
+   * Store relative dates in separate array format
+   *
+   * @param array $queryParams
+   * @param array $formValues
+   */
+  public static function saveRelativeDates(&$queryParams, $formValues) {
+    $relativeDates = array('relative_dates' => array());
+    foreach ($formValues as $id => $value) {
+      if (preg_match('/_date_relative$/', $id) && !empty($value)) {
+        $entityName = strstr($id, '_date', TRUE);
+        $relativeDates['relative_dates'][$entityName] = $value;
+      }
+    }
+    // merge with original queryParams if relative date value(s) found
+    if (count($relativeDates['relative_dates'])) {
+      $queryParams = array_merge($queryParams, $relativeDates);
+    }
+  }
+
 }
index e4ef5cb0202b04e6ac5132b454a0b3b243e27c43..2e1f7a9e77303bf5098f47e5fd65c977381969d2 100644 (file)
@@ -189,6 +189,7 @@ class CRM_Contact_Form_Task_SaveSearch extends CRM_Contact_Form_Task {
     // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the
     // sql operator syntax at the query layer.
     if (!$isSearchBuilder) {
+      CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues);
       $savedSearch->form_values = serialize($queryParams);
     }
     else {