From efd2dc5fd6f7d7a05d2293d7e41fa848ac9e43d8 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 4 Mar 2019 11:09:16 +1300 Subject: [PATCH] Fix group conversion to add relative key Old format doesn't seem to have it --- CRM/Upgrade/Incremental/SmartGroups.php | 35 +++++++++++++++++-- .../CRM/Upgrade/Incremental/BaseTest.php | 7 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CRM/Upgrade/Incremental/SmartGroups.php b/CRM/Upgrade/Incremental/SmartGroups.php index 764e12ab0818..4e54d874ee01 100644 --- a/CRM/Upgrade/Incremental/SmartGroups.php +++ b/CRM/Upgrade/Incremental/SmartGroups.php @@ -48,19 +48,42 @@ public function updateGroups($actions) { * @param array $fields */ public function datePickerConversion($fields) { - $fieldPossibilities = []; + $fieldPossibilities = $relativeFieldNames = []; foreach ($fields as $field) { $fieldPossibilities[] = $field; $fieldPossibilities[] = $field . '_high'; $fieldPossibilities[] = $field . '_low'; } + $relativeDateMappings = ['activity_date_time' => 'activity']; foreach ($fields as $field) { foreach ($this->getSearchesWithField($field) as $savedSearch) { $formValues = $savedSearch['form_values']; + $isRelative = $hasRelative = FALSE; + $relativeFieldName = $field . '_relative'; + + if (!empty($relativeDateMappings[$field]) && isset($formValues['relative_dates'])) { + if (!empty($formValues['relative_dates'][$relativeDateMappings[$field]])) { + $formValues[] = [$relativeFieldName, '=', $savedSearch['form_values']['relative_dates'][$relativeDateMappings[$field]]]; + unset($formValues['relative_dates'][$relativeDateMappings[$field]]); + $isRelative = TRUE; + } + } foreach ($formValues as $index => $formValue) { if (in_array($formValue[0], $fieldPossibilities)) { - $formValues[$index][2] = $this->getConvertedDateValue($formValue[2]); + if ($isRelative) { + unset($formValues[$index]); + } + else { + $isHigh = substr($formValue[0], -5, 5) === '_high'; + $formValues[$index][2] = $this->getConvertedDateValue($formValue[2], $isHigh); + } + } + } + if (!$isRelative) { + if (!in_array($relativeFieldName, $relativeFieldNames)) { + $relativeFieldNames[] = $relativeFieldName; + $formValues[] = [$relativeFieldName, '=', 0]; } } if ($formValues !== $savedSearch['form_values']) { @@ -98,15 +121,21 @@ public function convertEqualsStringToInArray($field) { * Get converted date value. * * @param string $dateValue + * @param bool $isEndOfDay + * Is this the upper value in a search range? If so alter the time to + * get the end of day if none set. * * @return string * $dateValue */ - protected function getConvertedDateValue($dateValue) { + protected function getConvertedDateValue($dateValue, $isEndOfDay) { if (date('Y-m-d', strtotime($dateValue)) !== $dateValue && date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue ) { $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue))); + if ($isEndOfDay) { + $dateValue = str_replace('00:00:00', '23:59:59', $dateValue); + } } return $dateValue; } diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php index 20e4cefc11d1..90629a90ee13 100644 --- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -108,6 +108,13 @@ public function testSmartGroupDatePickerConversion() { $this->assertEquals('2019-01-20 00:00:00', $savedSearch['form_values'][0][2]); $this->assertEquals('grant_due_date_low', $savedSearch['form_values'][1][0]); $this->assertEquals('2019-01-22 00:00:00', $savedSearch['form_values'][1][2]); + $hasRelative = FALSE; + foreach ($savedSearch['form_values'] as $form_value) { + if ($form_value[0] === 'grant_due_date_relative') { + $hasRelative = TRUE; + } + } + $this->assertEquals(TRUE, $hasRelative); } /**