add Upgrader to maintain profile behaviour

This commit is contained in:
Marc Michalsky 2024-04-30 16:51:39 +02:00
parent df51d59cea
commit b3f82fbfba
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -102,4 +102,40 @@ class CRM_Twingle_Upgrader extends CRM_Extension_Upgrader_Base {
return TRUE;
}
/**
* Upgrade to 1.5.0
*
* - Activate mapping of `purpose` and `user_extra_field` to notes in each existing profile to
* maintain default behavior after making the fields optional.
*
* @return bool
* @throws \Civi\Core\Exception\DBQueryException
* @throws \Civi\Twingle\Exceptions\ProfileException
*/
public function upgrade_5150(): bool {
$this->ctx->log->info('Activate mapping of `purpose` and `user_extra_field` to notes in each existing profile.');
$profiles = CRM_Twingle_Profile::getProfiles();
if ($profiles) {
foreach ($profiles as $profile) {
$profile_changed = FALSE;
$contribution_notes = $profile->getAttribute('map_as_contribution_notes', []);
$contact_notes = $profile->getAttribute('map_as_contact_notes', []);
if (!in_array('purpose', $contribution_notes)) {
$profile->setAttribute('map_as_contribution_notes', array_merge($contribution_notes, ['purpose']));
$profile_changed = TRUE;
}
if (!in_array('user_extrafield', $contact_notes)) {
$profile->setAttribute('map_as_contact_notes', array_merge($contact_notes, ['user_extrafield']));
$profile_changed = TRUE;
}
if ($profile_changed) {
$profile->saveProfile();
}
}
}
return TRUE;
}
}