-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add routine for updating smartgroups, currently handling datepicker conversion #13395
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 5 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2019 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License and the CiviCRM Licensing Exception along | | ||
| with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC (c) 2004-2019 | ||
* | ||
* Class to handled upgrading any saved searches with changed patterns. | ||
*/ | ||
class CRM_Upgrade_Incremental_SmartGroups { | ||
|
||
/** | ||
* Version we are upgrading to. | ||
* | ||
* @var string | ||
*/ | ||
protected $upgradeVersion; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getUpgradeVersion() { | ||
return $this->upgradeVersion; | ||
} | ||
|
||
/** | ||
* @param string $upgradeVersion | ||
*/ | ||
public function setUpgradeVersion($upgradeVersion) { | ||
$this->upgradeVersion = $upgradeVersion; | ||
} | ||
|
||
/** | ||
* CRM_Upgrade_Incremental_MessageTemplates constructor. | ||
* | ||
* @param string $upgradeVersion | ||
*/ | ||
public function __construct($upgradeVersion) { | ||
$this->setUpgradeVersion($upgradeVersion); | ||
} | ||
|
||
/** | ||
* Get any conversions required for saved smart groups. | ||
* | ||
* @return array | ||
*/ | ||
public function getSmartGroupConversions() { | ||
return [ | ||
[ | ||
'version' => '5.11.alpha1', | ||
'upgrade_descriptors' => [ts('Upgrade grant smart groups to datepicker format')], | ||
'actions' => [ | ||
'function' => 'datepickerConversion', | ||
'fields' => [ | ||
'grant_application_received_date', | ||
'grant_decision_date', | ||
'grant_money_transfer_date', | ||
'grant_due_date' | ||
] | ||
] | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Convert any | ||
* @param array $fields | ||
*/ | ||
public function datePickerConversion($fields) { | ||
$fieldPossibilities = []; | ||
foreach ($fields as $field) { | ||
$fieldPossibilities[] = $field; | ||
$fieldPossibilities[] = $field . '_high'; | ||
$fieldPossibilities[] = $field . '_low'; | ||
} | ||
|
||
foreach ($fields as $field) { | ||
$savedSearches = civicrm_api3('SavedSearch', 'get', [ | ||
'options' => ['limit' => 0], | ||
'form_values' => ['LIKE' => "%{$field}%"], | ||
])['values']; | ||
foreach ($savedSearches as $savedSearch) { | ||
$formValues = $savedSearch['form_values']; | ||
foreach ($formValues as $index => $formValue) { | ||
if (in_array($formValue[0], $fieldPossibilities)) { | ||
$formValues[$index][2] = $this->getConvertedDateValue($formValue[2]); | ||
} | ||
} | ||
if ($formValues !== $savedSearch['form_values']) { | ||
civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Update message templates. | ||
*/ | ||
public function updateGroups() { | ||
$conversions = $this->getSmartGroupConversionsToApply(); | ||
foreach ($conversions as $conversion) { | ||
$function = $conversion['function']; | ||
$this->{$function}($conversion['fields']); | ||
} | ||
} | ||
|
||
/** | ||
* Get any required template updates. | ||
* | ||
* @return array | ||
*/ | ||
public function getSmartGroupConversionsToApply() { | ||
$conversions = $this->getSmartGroupConversions(); | ||
$return = []; | ||
foreach ($conversions as $conversion) { | ||
if ($conversion['version'] === $this->getUpgradeVersion()) { | ||
$return[] = $conversion['actions']; | ||
} | ||
} | ||
return $return; | ||
} | ||
|
||
/** | ||
* Get converted date value. | ||
* | ||
* @param string $dateValue | ||
* | ||
* @return string | ||
* $dateValue | ||
*/ | ||
protected function getConvertedDateValue($dateValue) { | ||
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))); | ||
} | ||
return $dateValue; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is excellent work @eileenmcnaughton. I have one nitpick about the design. The way you've done it here, the
addTask
function passes a version number toupdateSmartGroups
which then relies on the data ingetSmartGroupConversions
to figure out what to do. That's a bit hard to grok and seems like it might cause trouble for future devs trying to figure out and extend what you've done. Since theaddTask
function can accept a variable number of arguments, I'd suggest passing in whatever's needed directly, e.g.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colemanw yeah I guess so - I just thought it would create one array where we could kinda track what had been done when