diff --git a/softcreditcustomfields.php b/softcreditcustomfields.php index 1099c50..342ac5e 100644 --- a/softcreditcustomfields.php +++ b/softcreditcustomfields.php @@ -208,7 +208,7 @@ function _softcreditcustomfields_addCustomDataToForm(&$form, $entityId, $blockId foreach ($groupTree as $id => $group) { foreach ($group['fields'] as $fldId => $field) { $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name']; - $groupTree[$id]['fields'][$fldId]['element_name'] = "soft_credit[$blockId][{$field['element_name']}]"; + $groupTree[$id]['fields'][$fldId]['element_name'] = "soft_credit_custom[$blockId][{$field['element_name']}]"; } } @@ -239,12 +239,57 @@ function _softcreditcustomfields_addCustomDataToForm(&$form, $entityId, $blockId // more handling done in formRule func //self::storeRequiredCustomDataInfo($form, $groupTree); $tplGroupTree = CRM_Core_Smarty::singleton() - ->get_template_vars('soft_credit_groupTree'); + ->get_template_vars('soft_credit_custom_groupTree'); $tplGroupTree = empty($tplGroupTree) ? [] : $tplGroupTree; - $form->assign('soft_credit_groupTree', $tplGroupTree + [$blockId => $groupTree]); + $form->assign('soft_credit_custom_groupTree', $tplGroupTree + [$blockId => $groupTree]); // unset the temp smarty var that got created $form->assign('dnc_groupTree', NULL); } // softCredit custom data processing ends .. } + +/** + * Implements hook_civicrm_postProcess(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postProcess + */ +function softcreditcustomfields_civicrm_postProcess($formName, &$form) { + if ('CRM_Contribute_Form_Contribution' == $formName + && !($form->getVar('_action') & CRM_Core_Action::DELETE) + ) { + $contributionId = $form->getVar('_id'); + $params = $form->getVar('_params'); + if (empty($params['soft_credit_custom'])) { + return; + } + if (!empty($params['soft_credit_contact_id'])) { + foreach ($params['soft_credit_contact_id'] as $key => $softContactId) { + if ($softContactId + && isset($params['soft_credit_amount'][$key]) + && !empty($params['soft_credit_custom'][$key]) + ) { + $softParams = [ + 'contribution_id' => $contributionId, + 'contact_id' => $softContactId, + 'amount' => $params['soft_credit_amount'][$key], + 'return' => 'id', + ]; + if (!empty($params['soft_credit_type'][$key])) { + $softParams['soft_credit_type_id'] = $params['soft_credit_type'][$key]; + } + try { + $softContributionId = civicrm_api3('ContributionSoft', 'getvalue', $softParams); + $softParams = [ + 'id' => $softContributionId, + ] + $params['soft_credit_custom'][$key]; + civicrm_api3('ContributionSoft', 'create', $softParams); + } + catch (Exception $e) { + continue; + } + } + } + } + } +} diff --git a/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl b/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl index fb8d033..33654d9 100644 --- a/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl +++ b/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl @@ -3,7 +3,7 @@ {assign var='blockId' value=$smarty.section.i.index}