From 0a8855f74e0ea3c072b375d7ba83f8fdf2ff6f86 Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Sat, 22 Dec 2018 00:52:16 +0000 Subject: [PATCH] Added custom field for soft credit on Add/Edit contribution form --- softcreditcustomfields.php | 67 +++++++++++++++++++ .../CRM/Contribute/Form/SoftCredit-Custom.tpl | 64 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 templates/CRM/Contribute/Form/SoftCredit-Custom.tpl diff --git a/softcreditcustomfields.php b/softcreditcustomfields.php index 1a14fe4..7c44445 100644 --- a/softcreditcustomfields.php +++ b/softcreditcustomfields.php @@ -173,4 +173,71 @@ function softcreditcustomfields_civicrm_buildForm($formName, &$form) { )); } } + if ('CRM_Contribute_Form_Contribution' == $formName && !($form->getVar('_action') & CRM_Core_Action::DELETE)) { + CRM_Core_Region::instance('page-footer')->add(array( + 'template' => "CRM/Contribute/Form/SoftCredit-Custom.tpl", + )); + for ($blockId = 1; $blockId <= 10; $blockId++) { + $entityId = NULL;//CRM_Utils_Array::value('id', CRM_Utils_Array::value($blockId, $softCredits)); + _softcreditcustomfields_addCustomDataToForm($form, $entityId, $blockId); + } + + } +} +/** + * Add custom data to the form. + * + * @param CRM_Core_Form $form + * @param int $entityId + * @param int $blockId + */ +function _softcreditcustomfields_addCustomDataToForm(&$form, $entityId, $blockId) { + $groupTree = CRM_Core_BAO_CustomGroup::getTree('ContributionSoft', NULL, $entityId); + + if (isset($groupTree) && is_array($groupTree)) { + // use simplified formatted groupTree + $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form); + // make sure custom fields are added /w element-name in the format - 'soft_credit_amount[$blockId][custom-X]' + 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']}]"; + } + } + + $defaults = []; + CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); + + // since we change element name for softCredit custom data, we need to format the setdefault values + $softCreditDefaults = []; + foreach ($defaults as $key => $val) { + if (empty($val)) { + continue; + } + + // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array + // this works for all types custom data + $keyValues = explode('[', str_replace(']', '', $key)); + $softCreditDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val; + } + + $form->setDefaults($softCreditDefaults); + + // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var. + // And we can't set it to 'soft_credit_' because we want to set it in a slightly different format. + CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); + + // during contact editing : if no softCredit is filled + // required custom data must not produce 'required' form rule error + // more handling done in formRule func + //self::storeRequiredCustomDataInfo($form, $groupTree); + $tplGroupTree = CRM_Core_Smarty::singleton() + ->get_template_vars('soft_credit_groupTree'); + $tplGroupTree = empty($tplGroupTree) ? [] : $tplGroupTree; + + $form->assign('soft_credit_groupTree', $tplGroupTree + [$blockId => $groupTree]); + // unset the temp smarty var that got created + $form->assign('dnc_groupTree', NULL); + } + // softCredit custom data processing ends .. } diff --git a/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl b/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl new file mode 100644 index 0000000..fb8d033 --- /dev/null +++ b/templates/CRM/Contribute/Form/SoftCredit-Custom.tpl @@ -0,0 +1,64 @@ + + {section name='i' start=1 loop=$rowCount} + {assign var='blockId' value=$smarty.section.i.index} + + + + {/section} +
+ {foreach from=$soft_credit_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} + {* always assign to prevent leakage*} + {assign var="isSingleRecordEdit" value=''} + {/if} + {if $isSingleRecordEdit} +
+ {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit'} +
+ {else} +
+ {if !$skipTitle} +
+ {$cd_edit.title} +
+ {/if} +
+ {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit'} +
+
+ {/if} +
+ {/foreach} +
+ +