Added custom field for soft credit on Add/Edit contribution form
This commit is contained in:
		@@ -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 ..
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										64
									
								
								templates/CRM/Contribute/Form/SoftCredit-Custom.tpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								templates/CRM/Contribute/Form/SoftCredit-Custom.tpl
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
<table class="form-layout-compressed crm-soft-credit-block">
 | 
			
		||||
  {section name='i' start=1 loop=$rowCount}
 | 
			
		||||
    {assign var='blockId' value=$smarty.section.i.index}
 | 
			
		||||
    <tr id="soft-credit-custom_row-{$blockId}" class="customFieldIgnore {if $blockId gte $showSoftCreditRow}hiddenElement{/if}" >
 | 
			
		||||
      <td colspan='3'>
 | 
			
		||||
        {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}
 | 
			
		||||
            <div class="custom-group custom-group-{$cd_edit.name}">
 | 
			
		||||
              {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit'}
 | 
			
		||||
            </div>
 | 
			
		||||
          {else}
 | 
			
		||||
           <div class="custom-group custom-group-{$cd_edit.name} crm-accordion-wrapper crm-custom-accordion {if $cd_edit.collapse_display and !$skipTitle}collapsed{/if}">
 | 
			
		||||
            {if !$skipTitle}
 | 
			
		||||
            <div class="crm-accordion-header">
 | 
			
		||||
              {$cd_edit.title}
 | 
			
		||||
             </div><!-- /.crm-accordion-header -->
 | 
			
		||||
            {/if}
 | 
			
		||||
            <div class="crm-accordion-body">
 | 
			
		||||
              {include file="CRM/Custom/Form/Edit/CustomData.tpl" customDataEntity='soft_credit'}
 | 
			
		||||
            </div>
 | 
			
		||||
           </div>
 | 
			
		||||
          {/if}
 | 
			
		||||
          <div id="custom_group_{$group_id}_{$blockId}"></div>
 | 
			
		||||
        {/foreach}
 | 
			
		||||
      </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
  {/section}
 | 
			
		||||
</table>
 | 
			
		||||
 | 
			
		||||
<script type="text/javascript">
 | 
			
		||||
{literal}
 | 
			
		||||
  CRM.$(function($) {
 | 
			
		||||
    $('div#softCredit table tr[id*="soft-credit-row-"]').each(function() {
 | 
			
		||||
      if (!$(this).hasClass('customFieldIgnore')) {
 | 
			
		||||
        var trId = this.id;
 | 
			
		||||
        var $id = trId.replace('soft-credit-row-', '');
 | 
			
		||||
        $($('tr#soft-credit-custom_row-' + $id)).insertAfter('div#softCredit table tr#' + trId);
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    $('#addMoreSoftCredit').on('click', function () {
 | 
			
		||||
      if ($('tr.customFieldIgnore').hasClass("hiddenElement")) {
 | 
			
		||||
        $('.crm-contribution-form-block-soft_credit_to tr.hiddenElement').filter(':first').show().removeClass('hiddenElement');
 | 
			
		||||
      }
 | 
			
		||||
      if ($('.crm-soft-credit-block tr.hiddenElement').length < 1) {
 | 
			
		||||
        $('#addMoreSoftCredit').hide();
 | 
			
		||||
      }
 | 
			
		||||
      return false;
 | 
			
		||||
    });
 | 
			
		||||
    $('.soft-credit-delete-link').click(function() {
 | 
			
		||||
      var closestTr = $(this).closest('tr').get(0).id;
 | 
			
		||||
      var $id = closestTr.replace('soft-credit-row-', '');
 | 
			
		||||
      $('tr#soft-credit-custom_row-' + $id).addClass('hiddenElement').removeAttr('style');
 | 
			
		||||
      return false;
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
{/literal}
 | 
			
		||||
</script>
 | 
			
		||||
		Reference in New Issue
	
	Block a user