From 6175e0623fff2c6f5ccb8bd7c4ad7556f431d2ae Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Sat, 22 Dec 2018 23:05:14 +0000 Subject: [PATCH] added code to insert custom data for soft credits --- softcreditcustomfields.php | 51 +++++++++++++++++-- .../CRM/Contribute/Form/SoftCredit-Custom.tpl | 6 +-- 2 files changed, 51 insertions(+), 6 deletions(-) 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} - {foreach from=$soft_credit_groupTree.$blockId item=cd_edit key=group_id name=custom_sets} + {foreach from=$soft_credit_custom_groupTree.$blockId item=cd_edit key=group_id name=custom_sets} {if $cd_edit.is_multiple and $multiRecordDisplay eq 'single'} {assign var="isSingleRecordEdit" value=TRUE} {else} @@ -12,7 +12,7 @@ {/if} {if $isSingleRecordEdit}
- {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit'} + {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit_custom'}
{else}
@@ -22,7 +22,7 @@
{/if}
- {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit'} + {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit_custom'}
{/if}