Skip to content

Commit

Permalink
Merge pull request #59 from jackaponte/1.x-5.6.0
Browse files Browse the repository at this point in the history
Update to match Drupal 7.x-5.6
  • Loading branch information
herbdool authored Oct 8, 2021
2 parents f2797a2 + a6b375a commit dd06d43
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 23 deletions.
77 changes: 55 additions & 22 deletions includes/wf_crm_webform_postprocess.inc
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,19 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$dt = $element['#civicrm_data_type'];
// Validate state/prov abbreviation
if ($dt == 'state_province_abbr') {
$ckey = str_replace('state_province', 'country', $key);
if (!empty($this->crmValues[$ckey]) && is_numeric($this->crmValues[$ckey])) {
$country_id = $this->crmValues[$ckey];
}
else {
$country_id = wf_crm_get_civi_setting('defaultContactCountry', 1228);
}
$states = wf_crm_get_states($country_id);
if ($states && !array_key_exists(strtoupper($element['#value']), $states)) {
$countries = wf_crm_apivalues('address', 'getoptions', ['field' => 'country_id']);
form_error($element, t('Mismatch: "@state" is not a state/province of %country. Please enter a valid state/province abbreviation for %field.', ['@state' => $element['#value'], '%country' => $countries[$country_id], '%field' => $element['#title']]));
if (!empty($this->crmValues[$key])) {
$ckey = str_replace('state_province', 'country', $key);
if (!empty($this->crmValues[$ckey]) && is_numeric($this->crmValues[$ckey])) {
$country_id = $this->crmValues[$ckey];
}
else {
$country_id = wf_crm_get_civi_setting('defaultContactCountry', 1228);
}
$states = wf_crm_get_states($country_id);
if ($states && !array_key_exists(strtoupper($element['#value']), $states)) {
$countries = wf_crm_apivalues('address', 'getoptions', ['field' => 'country_id']);
form_error($element, t('Mismatch: "@state" is not a state/province of %country. Please enter a valid state/province abbreviation for %field.', ['@state' => $element['#value'], '%country' => $countries[$country_id], '%field' => $element['#title']]));
}
}
}
// Strings and files don't need any validation
Expand Down Expand Up @@ -590,7 +592,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$m = $contact['address'][1]['master_id'];
// If master address is exposed to the form, use it
if (!empty($this->data['contact'][$m]['address'][1])) {
$contact['address'][1] = $this->data['contact']['address'][1];
$contact['address'][1] = $this->data['contact'][$m]['address'][1];
}
// Else look up the master contact's address
elseif (!empty($this->existing_contacts[$m])) {
Expand Down Expand Up @@ -1255,6 +1257,16 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
foreach (wf_crm_aval($this->data, 'case', []) as $n => $data) {
if (is_array($data) && !empty($data['case'][1]['client_id'])) {
$params = $data['case'][1];
//Check if webform is set to update the existing case on contact.
if (!empty($this->data['case'][$n]['existing_case_status']) && empty($this->ent['case'][$n]['id']) && !empty($this->ent['contact'][$n]['id'])) {
$existingCase = $this->findCaseForContact($this->ent['contact'][$n]['id'], [
'case_type_id' => wf_crm_aval($this->data['case'][$n], 'case:1:case_type_id'),
'status_id' => $this->data['case'][$n]['existing_case_status']
]);
if (!empty($existingCase['id'])) {
$this->ent['case'][$n]['id'] = $existingCase['id'];
}
}
// Set some defaults in create mode
// Create duplicate case based on existing case if 'duplicate_case' checkbox is checked
if (empty($this->ent['case'][$n]['id']) || !empty($this->data['case'][$n]['duplicate_case'])) {
Expand Down Expand Up @@ -1978,6 +1990,10 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$paymentProcessor->setCancelUrl($this->getIpnRedirectUrl('cancel'));
}

// Required by (at least) 'Paypal - Website Payments Standard' and 'Redsys'
$params['webform_redirect_cancel'] = $this->getIpnRedirectUrl('cancel');
$params['webform_redirect_success'] = $this->getIpnRedirectUrl('success');

if (method_exists($paymentProcessor, 'doTransferCheckout')) {
//Paypal Express checkout
if(wf_crm_aval($_POST, 'credit_card_number') == 'express'){
Expand Down Expand Up @@ -2010,7 +2026,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$paymentProcessor->doPayment($params);
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
drupal_set_message(ts('Payment approval failed with message: ') . $e->getMessage(),'error');
backdrop_set_message(ts('Payment approval failed with message: ') . $e->getMessage(),'error');
CRM_Utils_System::redirect($this->getIpnRedirectUrl('cancel'));
}
}
Expand Down Expand Up @@ -2287,19 +2303,20 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$createModeKey = 'civicrm_' . $c . '_contact_' . $n . '_' . $table . '_createmode';
$multivaluesCreateMode = $this->data['config']['create_mode'][$createModeKey] ?? NULL;
$cgMaxInstance = $this->all_sets[$table]['max_instances'] ?? 1;
$key = $n;
if (substr($name, 0, 6) == 'custom' && $cgMaxInstance > 1) {
// Retrieve name for multi value custom fields.
$customName = $this->getNameForMultiValueFields($multivaluesCreateMode, $name, $table, $c, $n);
$n = $c;
$key = 1;
}
if (!empty($this->ent['contact'][$val]['id'])) {
unset($this->data[$ent][$c][$table][$n][$name]);
$tableName = substr($name, 0, 6) == 'custom' ? $ent : $table;
$fld = $customName ?? $name;
$this->data[$ent][$c][$tableName][$n][$fld] = $this->ent['contact'][$val]['id'];
$this->data[$ent][$c][$tableName][$key][$fld] = $this->ent['contact'][$val]['id'];
}
elseif (substr($name, 0, 6) == 'custom') {
$this->data[$ent][$c]['update_contact_ref'][] = $name;
$this->data[$ent][$c]['update_contact_ref'][$n][$name] = $val;
}
}
}
Expand Down Expand Up @@ -2431,7 +2448,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
// Is this a multi-value custom field?
if ($cgMaxInstance > 1) {
$name = $this->getNameForMultiValueFields($multivaluesCreateMode, $name, $table, $c, $n);
$n = $c;
$n = 1;
}
}
$this->data[$ent][$c][$ent][$n][$name] = $customValue ?? $val;
Expand Down Expand Up @@ -2701,11 +2718,27 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base {
$updateParams = [
'id' => $cid,
];
foreach ($params['update_contact_ref'] as $refKey) {
foreach ($params['contact'] as $contactParams) {
foreach ($contactParams as $key => $value) {
if (strpos($key, $refKey) === 0 && !isset($updateParams[$key])) {
$updateParams[$key] = $value;
$skipKeys = [];
foreach ($params['update_contact_ref'] as $n => $refKeys) {
foreach ($refKeys as $refKey => $val) {
// Skip contact ref that doesn't have a valid contact ids.
if (empty($this->ent['contact'][$val]['id'])) {
continue;
}
foreach ($params['contact'] as $contactParams) {
foreach ($contactParams as $key => $value) {
if (strpos($key, "{$refKey}_") === 0 && !isset($updateParams[$key]) && !in_array($key, $skipKeys)) {
//If this is an edit to an existing record, remove any matching keys that was previously set to 'create' new records.
if (strpos($key, "{$refKey}_-") !== 0) {
foreach ($updateParams as $k => $v) {
if (strpos($k, "{$refKey}_-") === 0) {
$skipKeys[] = $k;
unset($updateParams[$k]);
}
}
}
$updateParams[$key] = $value;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/webform_civicrm_payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ cj(function($) {
updateLineItem(lineKey, amount, label);
}

$('.civicrm-enabled.contribution-line-item:not(".form-radios")')
$('.civicrm-enabled.contribution-line-item:not(".form-radios, .form-checkboxes")')
.each(calculateLineItemAmount)
.on('change keyup', calculateLineItemAmount)
.each(function() {
Expand Down
18 changes: 18 additions & 0 deletions webform_civicrm.module
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,21 @@ function webform_civicrm_civicrm_pre($op, $objectName, $id, &$params) {
}
}
}

/**
* Implements hook_civicrm_alterPaymentProcessorParams().
*
* Legacy handling for paypal.
* We use it to override the return url so that the user gets redirected to the right place from paypal.
*
* Required by (at least) 'Paypal - Website Payments Standard' and 'Redsys'
*
*/
function webform_civicrm_civicrm_alterPaymentProcessorParams($paymentObj, $rawParams, &$cookedParams) {
if (!empty($rawParams['webform_redirect_cancel']) && !empty($rawParams['webform_redirect_success'])
&& !empty($cookedParams['return']) && !empty($cookedParams['cancel_return'])
) {
$cookedParams['return'] = $rawParams['webform_redirect_success'];
$cookedParams['cancel_return'] = $rawParams['webform_redirect_cancel'];
}
}

0 comments on commit dd06d43

Please sign in to comment.