Code style

This commit is contained in:
Jens Schuppe 2024-06-12 14:35:42 +02:00
parent cf9483ca3e
commit ac1b08b775
4 changed files with 47 additions and 74 deletions

View file

@ -687,7 +687,10 @@ class CRM_Twingle_Profile {
return $default_profile; return $default_profile;
} }
else { 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
);
} }
} }

View file

@ -115,23 +115,22 @@ class CRM_Twingle_Upgrader extends CRM_Extension_Upgrader_Base {
public function upgrade_5150(): bool { public function upgrade_5150(): bool {
$this->ctx->log->info('Activate mapping of `purpose` and `user_extra_field` to notes in each existing profile.'); $this->ctx->log->info('Activate mapping of `purpose` and `user_extra_field` to notes in each existing profile.');
$profiles = CRM_Twingle_Profile::getProfiles(); foreach (CRM_Twingle_Profile::getProfiles() as $profile) {
if ($profiles) { $profile_changed = FALSE;
foreach ($profiles as $profile) { /** @phpstan-var array<string> $contribution_notes */
$profile_changed = FALSE; $contribution_notes = $profile->getAttribute('map_as_contribution_notes', []);
$contribution_notes = $profile->getAttribute('map_as_contribution_notes', []); /** @phpstan-var array<string> $contact_notes */
$contact_notes = $profile->getAttribute('map_as_contact_notes', []); $contact_notes = $profile->getAttribute('map_as_contact_notes', []);
if (!in_array('purpose', $contribution_notes)) { if (!in_array('purpose', $contribution_notes, TRUE)) {
$profile->setAttribute('map_as_contribution_notes', array_merge($contribution_notes, ['purpose'])); $profile->setAttribute('map_as_contribution_notes', array_merge($contribution_notes, ['purpose']));
$profile_changed = TRUE; $profile_changed = TRUE;
} }
if (!in_array('user_extrafield', $contact_notes)) { if (!in_array('user_extrafield', $contact_notes, TRUE)) {
$profile->setAttribute('map_as_contact_notes', array_merge($contact_notes, ['user_extrafield'])); $profile->setAttribute('map_as_contact_notes', array_merge($contact_notes, ['user_extrafield']));
$profile_changed = TRUE; $profile_changed = TRUE;
} }
if ($profile_changed) { if ($profile_changed) {
$profile->saveProfile(); $profile->saveProfile();
}
} }
} }

View file

@ -17,6 +17,7 @@ declare(strict_types = 1);
use CRM_Twingle_ExtensionUtil as E; use CRM_Twingle_ExtensionUtil as E;
use Civi\Twingle\Exceptions\BaseException; use Civi\Twingle\Exceptions\BaseException;
use Civi\Api4\Note;
/** /**
* TwingleDonation.Submit API specification * TwingleDonation.Submit API specification
@ -485,20 +486,19 @@ function civicrm_api3_twingle_donation_Submit($params) {
} }
// Create contact notes. // Create contact notes.
/** @phpstan-var array<string> $contact_note_mappings */
$contact_note_mappings = $profile->getAttribute('map_as_contact_notes', []); $contact_note_mappings = $profile->getAttribute('map_as_contact_notes', []);
foreach (['user_extrafield'] as $target) { foreach (['user_extrafield'] as $target) {
if ( if (
isset($params[$target]) isset($params[$target])
&& '' !== $params[$target] && '' !== $params[$target]
&& in_array($target, $contact_note_mappings) && in_array($target, $contact_note_mappings, TRUE)
) { ) {
civicrm_api4('Note', 'create', [ Note::create(FALSE)
'values' => [ ->addValue('entity_table', 'civicrm_contact')
'entity_table' => 'civicrm_contact', ->addValue('entity_id', $contact_id)
'entity_id' => $contact_id, ->addValue('note', $params[$target])
'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.
} /** @phpstan-var array<string> $contribution_note_mappings */
$contribution_note_mappings = $profile->getAttribute('map_as_contribution_notes', []);
// Add notes to the contribution. foreach (['purpose', 'remarks'] as $target) {
$contribution_note_mappings = $profile->getAttribute("map_as_contribution_notes", []); if (
foreach (['purpose', 'remarks'] as $target) { in_array($target, $contribution_note_mappings, TRUE)
if ( && isset($params[$target])
in_array($target, $contribution_note_mappings) && '' !== $params[$target]
&& isset($params[$target]) ) {
&& '' !== $params[$target] Note::create(FALSE)
) { ->addValue('entity_table', 'civicrm_contribution')
civicrm_api4('Note', 'create', [ ->addValue('entity_id', reset($contribution['values'])['id'])
'values' => [ ->addValue('note', reset($params[$target]))
'entity_table' => 'civicrm_contribution', ->execute();
'entity_id' => CRM_Utils_Array::first($result_values['contribution'])['id'], }
'note' => $params[$target],
],
]);
} }
$result_values['contribution'] = $contribution['values'];
} }
// MEMBERSHIP CREATION // MEMBERSHIP CREATION

View file

@ -316,21 +316,7 @@
<tr class="crm-section"> <tr class="crm-section">
<td class="label"> <td class="label">
{$form.map_as_contribution_notes.label} {$form.map_as_contribution_notes.label}
<a {help id="id-map_as_contribution_notes" title=$form.map_as_contribution_notes.label}
onclick='
CRM.help(
"{ts domain="de.systopia.twingle"}Create contribution note for{/ts}",
{literal}{
"id": "id-map_as_contribution_notes",
"file": "CRM\/Twingle\/Form\/Profile"
}{/literal}
);
return false;
'
href="#"
title="{ts domain="de.systopia.twingle"}Help{/ts}"
class="helpicon"
></a>
</td> </td>
<td class="content">{$form.map_as_contribution_notes.html}</td> <td class="content">{$form.map_as_contribution_notes.html}</td>
</tr> </tr>
@ -338,21 +324,7 @@
<tr class="crm-section"> <tr class="crm-section">
<td class="label"> <td class="label">
{$form.map_as_contact_notes.label} {$form.map_as_contact_notes.label}
<a {help id="id-map_as_contact_notes" title=$form.map_as_contact_notes.label}
onclick='
CRM.help(
"{ts domain="de.systopia.twingle"}Create contact note for{/ts}",
{literal}{
"id": "id-map_as_contact_notes",
"file": "CRM\/Twingle\/Form\/Profile"
}{/literal}
);
return false;
'
href="#"
title="{ts domain="de.systopia.twingle"}Help{/ts}"
class="helpicon"
></a>
</td> </td>
<td class="content">{$form.map_as_contact_notes.html}</td> <td class="content">{$form.map_as_contact_notes.html}</td>
</tr> </tr>