diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index da2b6fd94671..b33fa859de54 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -124,20 +124,30 @@ public static function getFormValues($id) { continue; } } + // Check for a date range field, which might be a standard date + // range or a relative date. if (strpos($id, '_date_low') !== FALSE || strpos($id, '_date_high') !== FALSE) { $entityName = strstr($id, '_date', TRUE); - if (!empty($result['relative_dates']) && array_key_exists($entityName, $result['relative_dates'])) { - $result[$id] = NULL; - $result["{$entityName}_date_relative"] = $result['relative_dates'][$entityName]; - } - elseif (!empty($specialDateFields[$id])) { - $entityName = strstr($specialDateFields[$id], '_date', TRUE); - $result[$id] = NULL; - $result["{$entityName}_relative"] = $result['relative_dates'][$entityName]; - } - else { - $result[$id] = $value; - $result["{$entityName}_date_relative"] = 0; + + // This is the default, for non relative dates. We will overwrite + // it if we determine this is a relative date. + $result[$id] = $value; + $result["{$entityName}_date_relative"] = 0; + + if (!empty($result['relative_dates'])) { + if (array_key_exists($entityName, $result['relative_dates'])) { + // We have a match from a regular field. + $result[$id] = NULL; + $result["{$entityName}_date_relative"] = $result['relative_dates'][$entityName]; + } + elseif (!empty($specialDateFields[$id])) { + // We may have a match on a special date field. + $entityName = strstr($specialDateFields[$id], '_date', TRUE); + if (array_key_exists($entityName, $result['relative_dates'])) { + $result[$id] = NULL; + $result["{$entityName}_relative"] = $result['relative_dates'][$entityName]; + } + } } } else { diff --git a/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php b/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php index 71180d5a6d95..df4808b5387b 100644 --- a/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php +++ b/tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php @@ -97,6 +97,52 @@ public function testGetFormValues($formValues, $expectedResult, $searchDescripti } } + /** + * Test if dates ranges are stored correctly + * in civicrm_saved_search table and are + * extracted properly. + */ + public function testDateRange() { + $savedSearch = new CRM_Contact_BAO_SavedSearch(); + $formValues = array( + 'hidden_basic' => 1, + 'group_search_selected' => 'group', + 'component_mode' => 1, + 'operator' => 'AND', + 'privacy_operator' => 'OR', + 'privacy_toggle' => 1, + 'participant_register_date_low' => '01/01/2009', + 'participant_register_date_high' => '01/01/2018', + 'radio_ts' => 'ts_all', + 'title' => 'bah bah bah', + ); + + $queryParams = array( + 0 => array( + 0 => 'participant_register_date_low', + 1 => '=', + 2 => '01/01/2009', + 3 => 0, + 4 => 0, + ), + 1 => array( + 0 => 'participant_register_date_high', + 1 => '=', + 2 => '01/01/2018', + 3 => 0, + 4 => 0, + ), + ); + + CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues); + CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues); + $savedSearch->form_values = serialize($queryParams); + $savedSearch->save(); + + $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()')); + $this->assertEquals('01/01/2009', $result['participant_register_date_low']); + $this->assertEquals('01/01/2018', $result['participant_register_date_high']); + } /** * Test if relative dates are stored correctly * in civicrm_saved_search table.