From 341a0f0e320b27812e3b9789e89f9b1bc6fdfceb Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Wed, 7 Aug 2019 11:34:07 +0200 Subject: [PATCH 1/4] [#5] Add membership type configuration option in profile --- CRM/Twingle/Form/Profile.php | 30 ++++++++++++++++++++++++++ CRM/Twingle/Profile.php | 2 ++ templates/CRM/Twingle/Form/Profile.tpl | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index 29ed717..25f871c 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -224,6 +224,15 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { array('class' => 'crm-select2 huge') ); + $this->add( + 'select', // field type + 'membership_type_id', // field name + E::ts('Create membership of type'), // field label + array('' => E::ts('- none -')) + $this->getMembershipTypes(), // list of options + FALSE, // is not required + array('class' => 'crm-select2 huge') + ); + $this->add( 'text', // field type 'contribution_source', // field name @@ -349,6 +358,27 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { return $financial_types; } + /** + * Retrieves membership types present within the system as options for select + * form elements. + * + * @return array + * + * @throws \CiviCRM_API3_Exception + */ + public function getMembershipTypes() { + $membership_types = array(); + $query = civicrm_api3('MembershipType', 'get', array( + 'is_active' => 1, + 'option.limit' => 0, + 'return' => 'id,name' + )); + foreach ($query['values'] as $type) { + $membership_types[$type['id']] = $type['name']; + } + return $membership_types; + } + /** * Retrieves campaigns present within the system as options for select form * elements. diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index 85e6fd0..e818062 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -191,6 +191,7 @@ class CRM_Twingle_Profile { 'donation_receipt_groups', 'campaign', 'contribution_source', + 'membership_type_id', ); } @@ -250,6 +251,7 @@ class CRM_Twingle_Profile { 'donation_receipt_groups' => NULL, 'campaign' => NULL, 'contribution_source' => NULL, + 'membreship_type_id' => NULL, )); } diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 773dfb7..c7ad9db 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -185,6 +185,11 @@ {$form.campaign.html} + + {$form.membership_type_id.label} + {$form.membership_type_id.html} + + {$form.contribution_source.label} {$form.contribution_source.html} From 9ffbd5b0707acfa17c1f7866853f75190fe06cf8 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Wed, 7 Aug 2019 11:35:20 +0200 Subject: [PATCH 2/4] [#5] Create membership when a type is selected in the profile --- api/v3/TwingleDonation/Submit.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 2bdc898..34709a6 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -571,6 +571,13 @@ function civicrm_api3_twingle_donation_Submit($params) { } $result_values = $contribution['values']; + + // Create membership if a membership type is configured within the profile. + if (!empty($membership_type_id = $profile->getAttribute('membership_type_id'))) { + $membership = civicrm_api3('Membership', 'create', array( + 'contact_id' => $contact_id, + 'membership_type_id' => $membership_type_id, + )); } $result = civicrm_api3_create_success($result_values); From 2413b09f3beafa5c7a00fd1f815858b3e2e864ee Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Wed, 7 Aug 2019 11:35:52 +0200 Subject: [PATCH 3/4] [#5] Re-format results output to return membership and contact information --- api/v3/TwingleDonation/Submit.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 34709a6..3bfc41f 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -258,6 +258,9 @@ function civicrm_api3_twingle_donation_Submit($params) { // Copy submitted parameters. $original_params = $params; + // Prepare results array. + $result_values = array(); + // Get the profile defined for the given form ID, or the default profile // if none matches. $profile = CRM_Twingle_Profile::getProfileForProject($params['project_id']); @@ -417,6 +420,11 @@ function civicrm_api3_twingle_donation_Submit($params) { } } + $result_values['contact'] = $contact_id; + if (isset($organisation_id)) { + $result_values['organization'] = $organisation_id; + } + // If requested, add contact to newsletter groups defined in the profile. if (!empty($params['newsletter']) && !empty($groups = $profile->getAttribute('newsletter_groups'))) { foreach ($groups as $group_id) { @@ -424,6 +432,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'group_id' => $group_id, 'contact_id' => $contact_id, )); + + $result_values['newsletter'][] = $group_id; } } @@ -434,6 +444,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'group_id' => $group_id, 'contact_id' => $contact_id, )); + + $result_values['postinfo'][] = $group_id; } } @@ -445,6 +457,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'group_id' => $group_id, 'contact_id' => $contact_id, )); + + $result_values['donation_receipt'][] = $group_id; } } @@ -529,7 +543,7 @@ function civicrm_api3_twingle_donation_Submit($params) { // Create the mandate. $mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data); - $result_values = $mandate['values']; + $result_values['sepa_mandate'] = $mandate['values']; } else { // Create (recurring) contribution. @@ -570,7 +584,8 @@ function civicrm_api3_twingle_donation_Submit($params) { ); } - $result_values = $contribution['values']; + $result_values['contribution'] = $contribution['values']; + } // Create membership if a membership type is configured within the profile. if (!empty($membership_type_id = $profile->getAttribute('membership_type_id'))) { @@ -578,6 +593,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'contact_id' => $contact_id, 'membership_type_id' => $membership_type_id, )); + + $result_values['membership'] = $membership; } $result = civicrm_api3_create_success($result_values); From c43ebf452f950416f9cbffaac176b2e4435d0688 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Wed, 7 Aug 2019 11:37:08 +0200 Subject: [PATCH 4/4] [#5] Fix typo. --- CRM/Twingle/Profile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index e818062..8fc9ec4 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -251,7 +251,7 @@ class CRM_Twingle_Profile { 'donation_receipt_groups' => NULL, 'campaign' => NULL, 'contribution_source' => NULL, - 'membreship_type_id' => NULL, + 'membership_type_id' => NULL, )); }