Skip to content

Commit

Permalink
Fix group conversion to add relative key
Browse files Browse the repository at this point in the history
Old format doesn't seem to have it
  • Loading branch information
eileenmcnaughton committed Mar 4, 2019
1 parent f52dcb9 commit efd2dc5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
35 changes: 32 additions & 3 deletions CRM/Upgrade/Incremental/SmartGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit efd2dc5

Please sign in to comment.