[#4] (wip) Process custom fields parameter

This commit is contained in:
Jens Schuppe 2019-08-05 16:34:35 +02:00
parent 4537ac540a
commit 73481fb7ed
3 changed files with 78 additions and 0 deletions

View file

@ -69,6 +69,20 @@ class CRM_Twingle_Profile {
return in_array($project_id, $project_ids); return in_array($project_id, $project_ids);
} }
/**
* @return array
* The profile's configured custom field mapping
*/
public function getCustomFieldMapping() {
$custom_field_mapping = array();
if (!empty($custom_field_definition = $this->getAttribute('custom_field_mapping'))) {
foreach (preg_split('/\r\n|\r|\n/', $custom_field_definition, -1, PREG_SPLIT_NO_EMPTY) as $twingle_field_name => $custom_field_name) {
$custom_field_mapping[$twingle_field_name] = $custom_field_name;
}
}
return $custom_field_mapping;
}
/** /**
* Retrieves all data attributes of the profile. * Retrieves all data attributes of the profile.
* *
@ -191,6 +205,7 @@ class CRM_Twingle_Profile {
'donation_receipt_groups', 'donation_receipt_groups',
'campaign', 'campaign',
'contribution_source', 'contribution_source',
'custom_field_mapping',
); );
} }
@ -250,6 +265,7 @@ class CRM_Twingle_Profile {
'donation_receipt_groups' => NULL, 'donation_receipt_groups' => NULL,
'campaign' => NULL, 'campaign' => NULL,
'contribution_source' => NULL, 'contribution_source' => NULL,
'custom_field_mapping' => NULL,
)); ));
} }

View file

@ -99,6 +99,16 @@ class CRM_Twingle_Submission {
} }
$params['gender_id'] = $gender_id; $params['gender_id'] = $gender_id;
} }
// Validate custom fields parameter, if given.
if (!empty($params['custom_fields'])) {
if (!is_array($custom_fields = json_decode($params['custom_fields'], TRUE))) {
throw new CiviCRM_API3_Exception(
E::ts('Invalid format for custom fields.'),
'invalid_format'
);
}
}
} }
/** /**

View file

@ -238,6 +238,13 @@ function _civicrm_api3_twingle_donation_Submit_spec(&$params) {
'api.required' => 0, 'api.required' => 0,
'description' => E::ts('The CiviCRM ID of a campaign to assign the contribution.'), 'description' => E::ts('The CiviCRM ID of a campaign to assign the contribution.'),
); );
$params['custom_fields'] = array(
'name' => 'custom_fields',
'title' => E::ts('Custom fields'),
'type' => CRM_Utils_Type::T_STRING,
'api.required' => 0,
'description' => E::ts('Additional information for either the contact or the (recurring) contribution.'),
);
} }
/** /**
@ -280,6 +287,27 @@ function civicrm_api3_twingle_donation_Submit($params) {
); );
} }
// Extract custom field values using the profile's mapping of Twingle fields
// to CiviCRM custom fields.
$custom_fields = array();
if (!empty($params['custom_fields'])) {
$custom_field_mapping = $profile->getCustomFieldMapping();
foreach (json_decode($params['custom_fields']) as $twingle_field => $value) {
if (isset($custom_field_mapping[$twingle_field])) {
// Get custom field definition to store values by entity the field
// extends.
$custom_field_id = substr($custom_field_mapping[$twingle_field], strlen('custom_'));
$custom_field = civicrm_api3('CustomField', 'getsingle', array(
'id' => $custom_field_id,
// Chain a CustomGroup.getsingle API call.
'api.CustomGroup.getsingle' => array(),
));
$custom_fields[$custom_field['api.CustomGroup.getsingle']['extends']][$custom_field_mapping[$twingle_field]] = $value;
}
}
}
// Create contact(s). // Create contact(s).
if ($params['is_anonymous']) { if ($params['is_anonymous']) {
// Retrieve the ID of the contact to use for anonymous donations defined // Retrieve the ID of the contact to use for anonymous donations defined
@ -351,6 +379,15 @@ function civicrm_api3_twingle_donation_Submit($params) {
$contact_data[$contact_component] = $params[$contact_param]; $contact_data[$contact_component] = $params[$contact_param];
} }
} }
// Add custom field values.
if (!empty($custom_fields['Contact'])) {
$contact_data += $custom_fields['Contact'];
}
if (!empty($custom_fields['Individual'])) {
$contact_data += $custom_fields['Individual'];
}
if (!$contact_id = CRM_Twingle_Submission::getContact( if (!$contact_id = CRM_Twingle_Submission::getContact(
'Individual', 'Individual',
$contact_data $contact_data
@ -375,6 +412,12 @@ function civicrm_api3_twingle_donation_Submit($params) {
$organisation_data = array( $organisation_data = array(
'organization_name' => $params['organization_name'], 'organization_name' => $params['organization_name'],
); );
// Add custom field values.
if (!empty($custom_fields['Organization'])) {
$organisation_data += $custom_fields['Organization'];
}
if (!empty($submitted_address)) { if (!empty($submitted_address)) {
$organisation_data += $submitted_address; $organisation_data += $submitted_address;
// Use configured location type for organisation address. // Use configured location type for organisation address.
@ -459,6 +502,15 @@ function civicrm_api3_twingle_donation_Submit($params) {
'total_amount' => $params['amount'] / 100, 'total_amount' => $params['amount'] / 100,
); );
// Add custom field values.
if (!empty($custom_fields['Contribution'])) {
$contribution_data += $custom_fields['Contribution'];
}
// Add custom field values.
if (!empty($custom_fields['ContributionRecur'])) {
$contribution_data += $custom_fields['ContributionRecur'];
}
if (!empty($params['purpose'])) { if (!empty($params['purpose'])) {
$contribution_data['note'] = $params['purpose']; $contribution_data['note'] = $params['purpose'];
} }