diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index be31ffb..c965b3f 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 @@ -417,6 +426,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 7286a23..a2d993e 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -207,6 +207,7 @@ class CRM_Twingle_Profile { 'campaign', 'contribution_source', 'custom_field_mapping', + 'membership_type_id', ); } @@ -267,6 +268,7 @@ class CRM_Twingle_Profile { 'campaign' => NULL, 'contribution_source' => NULL, 'custom_field_mapping' => NULL, + 'membership_type_id' => NULL, )); } diff --git a/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index e6209da..fad4d93 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -265,6 +265,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']); @@ -460,6 +463,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) { @@ -467,6 +475,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'group_id' => $group_id, 'contact_id' => $contact_id, )); + + $result_values['newsletter'][] = $group_id; } } @@ -477,6 +487,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'group_id' => $group_id, 'contact_id' => $contact_id, )); + + $result_values['postinfo'][] = $group_id; } } @@ -488,6 +500,8 @@ function civicrm_api3_twingle_donation_Submit($params) { 'group_id' => $group_id, 'contact_id' => $contact_id, )); + + $result_values['donation_receipt'][] = $group_id; } } @@ -581,7 +595,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. @@ -629,7 +643,17 @@ 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'))) { + $membership = civicrm_api3('Membership', 'create', array( + 'contact_id' => $contact_id, + 'membership_type_id' => $membership_type_id, + )); + + $result_values['membership'] = $membership; } $result = civicrm_api3_create_success($result_values); diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 0835359..ae79e64 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}