diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index 2f7a750..2159d3b 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -498,6 +498,31 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { [] ); + $this->add( + 'select', + 'map_as_contribution_notes', + E::ts('Create contribution notes for'), + [ + 'purpose' => E::ts('Purpose'), + 'remarks' => E::ts('Remarks'), + ], + // is not required + FALSE, + ['class' => 'crm-select2 huge', 'multiple' => 'multiple'] + ); + + $this->add( + 'select', + 'map_as_contact_notes', + E::ts('Create contact notes for'), + [ + 'user_extrafield' => E::ts('User Extra Field'), + ], + // is not required + FALSE, + ['class' => 'crm-select2 huge', 'multiple' => 'multiple'] + ); + $this->addButtons([ [ 'type' => 'submit', diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index 79c11ca..b6f6225 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -530,6 +530,8 @@ class CRM_Twingle_Profile { 'membership_postprocess_call' => ['required' => FALSE], 'newsletter_double_opt_in' => ['required' => FALSE], 'required_address_components' => ['required' => FALSE], + 'map_as_contribution_notes' => ['required' => FALSE], + 'map_as_contact_notes' => ['required' => FALSE], ], // Add payment methods. array_combine( @@ -646,6 +648,8 @@ class CRM_Twingle_Profile { 'city', 'country', ], + 'map_as_contribution_notes' => [], + 'map_as_contact_notes' => [], ] // Add contribution status for all payment methods. // phpcs:ignore Drupal.Formatting.SpaceUnaryOperator.PlusMinus @@ -683,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 2af4667..70f0180 100644 --- a/CRM/Twingle/Upgrader.php +++ b/CRM/Twingle/Upgrader.php @@ -102,4 +102,39 @@ 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.'); + + 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(); + } + } + + return TRUE; + } + } diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index e24cb2f..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 @@ -255,6 +256,13 @@ function _civicrm_api3_twingle_donation_Submit_spec(&$params) { 'api.required' => 0, 'description' => E::ts('Additional information for either the contact or the (recurring) contribution.'), ]; + $params['remarks'] = [ + 'name' => 'remarks', + 'title' => E::ts('Remarks'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 0, + 'description' => E::ts('Additional remarks for the donation.'), + ]; } /** @@ -477,13 +485,21 @@ function civicrm_api3_twingle_donation_Submit($params) { ); } - // Save user_extrafield as contact note. - if (isset($params['user_extrafield']) && '' != $params['user_extrafield']) { - civicrm_api3('Note', 'create', [ - 'entity_table' => 'civicrm_contact', - 'entity_id' => $contact_id, - 'note' => $params['user_extrafield'], - ]); + // 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, TRUE) + ) { + Note::create(FALSE) + ->addValue('entity_table', 'civicrm_contact') + ->addValue('entity_id', $contact_id) + ->addValue('note', $params[$target]) + ->execute(); + } } // Share organisation address with individual contact, using configured @@ -620,10 +636,6 @@ function civicrm_api3_twingle_donation_Submit($params) { $contribution_data += $custom_fields['Contribution']; } - if (isset($params['purpose'])) { - $contribution_data['note'] = $params['purpose']; - } - // set campaign, subject to configuration CRM_Twingle_Submission::setCampaign($contribution_data, 'contribution', $params, $profile); @@ -791,6 +803,23 @@ function civicrm_api3_twingle_donation_Submit($params) { ); } + // 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']; } diff --git a/l10n/de_DE/LC_MESSAGES/twingle.mo b/l10n/de_DE/LC_MESSAGES/twingle.mo index eff6c7e..9fd8054 100644 Binary files a/l10n/de_DE/LC_MESSAGES/twingle.mo and b/l10n/de_DE/LC_MESSAGES/twingle.mo differ diff --git a/l10n/de_DE/LC_MESSAGES/twingle.po b/l10n/de_DE/LC_MESSAGES/twingle.po index 1bc928f..0e8d890 100644 --- a/l10n/de_DE/LC_MESSAGES/twingle.po +++ b/l10n/de_DE/LC_MESSAGES/twingle.po @@ -96,6 +96,26 @@ msgstr "" "Nur alphanumerische Zeichen und der Unterstrich (_) sind für Profilnamen " "erlaubt." +#: CRM/Twingle/Form/Profile.php +msgid "Create contribution notes for" +msgstr "Zuwendungs-Notizen erstellen für" + +#: CRM/Twingle/Form/Profile.php ./api/v3/TwingleDonation/Submit.php +msgid "Purpose" +msgstr "Zweck" + +#: CRM/Twingle/Form/Profile.php ./api/v3/TwingleDonation/Submit.php +msgid "Remarks" +msgstr "Anmerkungen" + +#: CRM/Twingle/Form/Profile.php +msgid "Create contact notes for" +msgstr "Kontakt-Notizen erstellen für" + +#: CRM/Twingle/Form/Profile.php +msgid "User Extra Field" +msgstr "Benutzer-Extra-Feld" + #: CRM/Twingle/Form/Profile.php msgid "CiviSEPA" msgstr "CiviSEPA" @@ -231,6 +251,22 @@ msgstr "Zahlungsmethoden" msgid "Groups" msgstr "Gruppen" +#: templates/CRM/Twingle/Form/Profile.tpl +msgid "Create contribution note for" +msgstr "Zuwendungs-Notiz erstellen für" + +#: templates/CRM/Twingle/Form/Profile.tpl +msgid "Create contact note for" +msgstr "Kontakt-Notiz erstellen für" + +#: templates/CRM/Twingle/Form/Profile.hlp +msgid "Create a contribution note for each field specified in this selection. Tip: You can enable or disable this fields in the TwingleMANAGER." +msgstr "Erstelle eine Zuwendungs-Notiz für jedes Feld, das in dieser Auswahl angegeben ist. Tipp: Sie können diese Felder im TwingleMANAGER aktivieren oder deaktivieren." + +#: templates/CRM/Twingle/Form/Profile.hlp +msgid "Create a contact note for each field specified in this selection. Tip: You can enable or disable this fields in the TwingleMANAGER." +msgstr "Erstelle eine Kontakt-Notiz für jedes Feld, das in dieser Auswahl angegeben ist. Tipp: Sie können diese Felder im TwingleMANAGER aktivieren oder deaktivieren." + #: templates/CRM/Twingle/Form/Profile.tpl msgid "Are you sure you want to reset the default profile?" msgstr "Möchten Sie wirklich das Standard-Profil zurücksetzen?" diff --git a/l10n/pot/twingle.pot b/l10n/pot/twingle.pot index 868ab0a..7de3f72 100644 --- a/l10n/pot/twingle.pot +++ b/l10n/pot/twingle.pot @@ -74,6 +74,26 @@ msgstr "" msgid "Only alphanumeric characters and the underscore (_) are allowed for profile names." msgstr "" +#: ./CRM/Twingle/Form/Profile.php +msgid "Create contribution notes for" +msgstr "" + +#: ./CRM/Twingle/Form/Profile.php ./api/v3/TwingleDonation/Submit.php +msgid "Purpose" +msgstr "" + +#: ./CRM/Twingle/Form/Profile.php ./api/v3/TwingleDonation/Submit.php +msgid "Remarks" +msgstr "" + +#: ./CRM/Twingle/Form/Profile.php +msgid "Create contact notes for" +msgstr "" + +#: ./CRM/Twingle/Form/Profile.php +msgid "User Extra Field" +msgstr "" + #: ./CRM/Twingle/Form/Profile.php msgid "CiviSEPA" msgstr "" @@ -206,6 +226,22 @@ msgstr "" msgid "Groups" msgstr "" +#: ./templates/CRM/Twingle/Form/Profile.tpl +msgid "Create contribution note for" +msgstr "" + +#: ./templates/CRM/Twingle/Form/Profile.tpl +msgid "Create contact note for" +msgstr "" + +#: ./templates/CRM/Twingle/Form/Profile.hlp +msgid "

Create a contribution note for each field specified in this selection.

\n

Tip: You can enable or disable this fields in the TwingleMANAGER.

" +msgstr "" + +#: ./templates/CRM/Twingle/Form/Profile.hlp +msgid "

Create a contact note for each field specified in this selection.

\n

Tip: You can enable or disable this fields in the TwingleMANAGER.

" +msgstr "" + #: ./templates/CRM/Twingle/Form/Profile.tpl msgid "Are you sure you want to reset the default profile?" msgstr "" diff --git a/templates/CRM/Twingle/Form/Profile.hlp b/templates/CRM/Twingle/Form/Profile.hlp index e3841d7..b8d67a4 100644 --- a/templates/CRM/Twingle/Form/Profile.hlp +++ b/templates/CRM/Twingle/Form/Profile.hlp @@ -75,4 +75,14 @@
  • ContributionRecur – Will be set on the recurring contribution and deriving single contributions
  • {/ts} {/htxt} -{/crmScope} \ No newline at end of file + +{htxt id='id-map_as_contribution_notes'} + {ts domain="de.systopia.twingle"}

    Create a contribution note for each field specified in this selection.

    +

    Tip: You can enable or disable this fields in the TwingleMANAGER.

    {/ts} +{/htxt} + +{htxt id='id-map_as_contact_notes'} + {ts domain="de.systopia.twingle"}

    Create a contact note for each field specified in this selection.

    +

    Tip: You can enable or disable this fields in the TwingleMANAGER.

    {/ts} +{/htxt} +{/crmScope} diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index a090abd..1577b5f 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -313,6 +313,22 @@ {$form.contribution_source.html} + + + {$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} + + + + + {$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} + + {$form.custom_field_mapping.label}