From ac1b08b77542f10d63f9ae455fd5208883d05525 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Wed, 12 Jun 2024 14:35:42 +0200 Subject: [PATCH] Code style --- CRM/Twingle/Profile.php | 5 ++- CRM/Twingle/Upgrader.php | 33 ++++++++--------- api/v3/TwingleDonation/Submit.php | 51 +++++++++++++------------- templates/CRM/Twingle/Form/Profile.tpl | 32 +--------------- 4 files changed, 47 insertions(+), 74 deletions(-) diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index 94a8a2e..b6f6225 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -687,7 +687,10 @@ class CRM_Twingle_Profile { return $default_profile; } else { - throw new ProfileException('Could not find default profile', ProfileException::ERROR_CODE_DEFAULT_PROFILE_NOT_FOUND); + throw new ProfileException( + 'Could not find default profile', + ProfileException::ERROR_CODE_DEFAULT_PROFILE_NOT_FOUND + ); } } diff --git a/CRM/Twingle/Upgrader.php b/CRM/Twingle/Upgrader.php index 4d00514..70f0180 100644 --- a/CRM/Twingle/Upgrader.php +++ b/CRM/Twingle/Upgrader.php @@ -115,23 +115,22 @@ class CRM_Twingle_Upgrader extends CRM_Extension_Upgrader_Base { 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(); - } + foreach (CRM_Twingle_Profile::getProfiles() as $profile) { + $profile_changed = FALSE; + /** @phpstan-var array $contribution_notes */ + $contribution_notes = $profile->getAttribute('map_as_contribution_notes', []); + /** @phpstan-var array $contact_notes */ + $contact_notes = $profile->getAttribute('map_as_contact_notes', []); + if (!in_array('purpose', $contribution_notes, TRUE)) { + $profile->setAttribute('map_as_contribution_notes', array_merge($contribution_notes, ['purpose'])); + $profile_changed = TRUE; + } + if (!in_array('user_extrafield', $contact_notes, TRUE)) { + $profile->setAttribute('map_as_contact_notes', array_merge($contact_notes, ['user_extrafield'])); + $profile_changed = TRUE; + } + if ($profile_changed) { + $profile->saveProfile(); } } diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 66a3c8d..bac1fd6 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -17,6 +17,7 @@ declare(strict_types = 1); use CRM_Twingle_ExtensionUtil as E; use Civi\Twingle\Exceptions\BaseException; +use Civi\Api4\Note; /** * TwingleDonation.Submit API specification @@ -485,20 +486,19 @@ function civicrm_api3_twingle_donation_Submit($params) { } // Create contact notes. + /** @phpstan-var array $contact_note_mappings */ $contact_note_mappings = $profile->getAttribute('map_as_contact_notes', []); foreach (['user_extrafield'] as $target) { if ( isset($params[$target]) && '' !== $params[$target] - && in_array($target, $contact_note_mappings) + && in_array($target, $contact_note_mappings, TRUE) ) { - civicrm_api4('Note', 'create', [ - 'values' => [ - 'entity_table' => 'civicrm_contact', - 'entity_id' => $contact_id, - 'note' => $params[$target], - ], - ]); + Note::create(FALSE) + ->addValue('entity_table', 'civicrm_contact') + ->addValue('entity_id', $contact_id) + ->addValue('note', $params[$target]) + ->execute(); } } @@ -803,25 +803,24 @@ function civicrm_api3_twingle_donation_Submit($params) { ); } - $result_values['contribution'] = $contribution['values']; - } - - // Add notes to the contribution. - $contribution_note_mappings = $profile->getAttribute("map_as_contribution_notes", []); - foreach (['purpose', 'remarks'] as $target) { - if ( - in_array($target, $contribution_note_mappings) - && isset($params[$target]) - && '' !== $params[$target] - ) { - civicrm_api4('Note', 'create', [ - 'values' => [ - 'entity_table' => 'civicrm_contribution', - 'entity_id' => CRM_Utils_Array::first($result_values['contribution'])['id'], - 'note' => $params[$target], - ], - ]); + // Add notes to the contribution. + /** @phpstan-var array $contribution_note_mappings */ + $contribution_note_mappings = $profile->getAttribute('map_as_contribution_notes', []); + foreach (['purpose', 'remarks'] as $target) { + if ( + in_array($target, $contribution_note_mappings, TRUE) + && isset($params[$target]) + && '' !== $params[$target] + ) { + Note::create(FALSE) + ->addValue('entity_table', 'civicrm_contribution') + ->addValue('entity_id', reset($contribution['values'])['id']) + ->addValue('note', reset($params[$target])) + ->execute(); + } } + + $result_values['contribution'] = $contribution['values']; } // MEMBERSHIP CREATION diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 4557e0d..1577b5f 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -316,21 +316,7 @@ {$form.map_as_contribution_notes.label} - + {help id="id-map_as_contribution_notes" title=$form.map_as_contribution_notes.label} {$form.map_as_contribution_notes.html} @@ -338,21 +324,7 @@ {$form.map_as_contact_notes.label} - + {help id="id-map_as_contact_notes" title=$form.map_as_contact_notes.label} {$form.map_as_contact_notes.html}