Compare commits

...

10 Commits

4 changed files with 359 additions and 0 deletions

View File

@ -0,0 +1,83 @@
<?php
class CRM_SoftCredit_SoftCreditQueryObject extends CRM_Contact_BAO_Query_Interface {
static $_networkFields = array();
public function &getFields() {
return self::$_networkFields;
}
/**
* @param $query
*
*/
public function select(&$query) {
}
/**
* @param $query
*
*/
public function where(&$query) {
if (!empty($query->_paramLookup['soft_credit_name'])) {
list($name, $op, $value, $grouping, $wildcard) = reset($query->_paramLookup['soft_credit_name']);
if ($value) {
$query->_where[$grouping][] = "civicrm_contact_creditee.sort_name LIKE '%{$value}%'";
$query->_whereTables['civicrm_contribution_soft_creditee_contact'] = 1;
$query->_tables['civicrm_contribution_soft_creditee_contact'] = 1;
$query->_whereTables['civicrm_contribution_soft'] = 1;
$query->_tables['civicrm_contribution_soft'] = 1;
$query->_qill[$grouping][] = ts("%1 %2 - '%3'", [1 => 'Soft Creditee\'s name', 2 => 'LIKE', 3 => $value]);
}
}
}
/**
* @param string $name
* @param $mode
* @param $side
*
*/
public function from($name, $mode, $side) {
if ($name == 'civicrm_contribution_soft_creditee_contact') {
return " $side JOIN civicrm_contact civicrm_contact_creditee
ON (civicrm_contribution_soft.contact_id = civicrm_contact_creditee.id)
";
}
}
/**
* @param $tables
*
*/
public function setTableDependency(&$tables) {
}
public function getPanesMapper(&$panes) {
}
/**
* @param $panes
*
*/
public function registerAdvancedSearchPane(&$panes) {
}
/**
* @param CRM_Core_Form $form
* @param $type
*
*/
public function buildAdvancedSearchPaneForm(&$form, $type) {
}
/**
* @param $paneTemplatePathArray
* @param $type
*
*/
public function setAdvancedSearchPaneTemplatePath(&$paneTemplatePathArray, $type) {
}
}

View File

@ -149,12 +149,36 @@ function softcreditcustomfields_civicrm_entityTypes(&$entityTypes) {
_softcreditcustomfields_civix_civicrm_entityTypes($entityTypes);
}
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*/
function softcreditcustomfields_civicrm_preProcess($formName, &$form) {
if (in_array($formName, ['CRM_Contribute_Form_Search', 'CRM_Contact_Form_Search_Advanced'])) {
$extendsMap = &CRM_Core_BAO_CustomQuery::$extendsMap;
$extendsMap['ContributionSoft'] = 'civicrm_contribution_soft';
}
}
/**
* Implements hook_civicrm_buildForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*/
function softcreditcustomfields_civicrm_buildForm($formName, &$form) {
if (('CRM_Contribute_Form_Search' == $formName && $form->getVar('_context') == 'search')
|| ('CRM_Contact_Form_Search_Advanced' == $formName && ('CiviContribute' == $form->_searchPane || $form->_flagSubmitted))
) {
CRM_Contribute_BAO_Query::addCustomFormFields($form, ['ContributionSoft']);
$form->add('text', 'soft_credit_name', ts('Soft Creditee\'s name'));
if ('CRM_Contact_Form_Search_Advanced' == $formName && 'CiviContribute' != $form->_searchPane) {
return;
}
CRM_Core_Region::instance('page-footer')->add(array(
'template' => "CRM/Contribute/Form/Search/SoftCredit-Search.tpl",
));
}
if ('CRM_Contribute_Form_ContributionView' == $formName) {
$softCredits = $form->get_template_vars('softContributions');
if (!empty($softCredits)) {
@ -173,4 +197,159 @@ 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 = CRM_Utils_Array::value(
'soft_credit_id',
CRM_Utils_Array::value(
$blockId,
CRM_Utils_Array::value('soft_credit', $form->_softCreditInfo)
)
);
_softcreditcustomfields_addCustomDataToForm($form, $entityId, $blockId);
}
if ($form->_flagSubmitted) {
$submitValues = $form->_submitValues;
foreach ($form->_required as $key => $fieldName) {
if (strpos($fieldName, 'soft_credit_custom[') !== FALSE) {
$fieldName = str_replace('soft_credit_custom[', '', $fieldName);
$blockId = strstr($fieldName, ']', TRUE);
if (empty($submitValues['soft_credit_amount'][$blockId])) {
unset($form->_required[$key]);
}
}
}
}
}
}
/**
* 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_custom[$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_');
$tplGroupTree = CRM_Core_Smarty::singleton()
->get_template_vars('soft_credit_custom_groupTree');
$tplGroupTree = empty($tplGroupTree) ? [] : $tplGroupTree;
$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;
}
}
}
}
}
}
/**
* Implements hook_civicrm_queryObjects().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_queryObjects
*/
function softcreditcustomfields_civicrm_queryObjects(&$queryObjects, $type) {
if ($type == 'Contact') {
$queryObjects[] = new CRM_SoftCredit_SoftCreditQueryObject();
}
}
/**
* Implements hook_civicrm_queryObjects().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_queryObjects
*/
function softcreditcustomfields_civicrm_alterContent(&$content, $context, $tplName, &$object) {
if (in_array($tplName, [
'CRM/Contribute/Form/Search/AdvancedSearchPane.tpl',
'CRM/Contribute/Form/Search.tpl'
])) {
$content = str_replace(
"cj('#contribution_soft_credit_type_id').val('');",
'',
$content
);
}
}

View File

@ -0,0 +1,33 @@
{if $form.soft_credit_name}
<div class="crm-softcredit-form-block-soft_credit_sort_name">
{$form.soft_credit_name.label}<br>
{$form.soft_credit_name.html}
</div>
{/if}
{if $contributionSoftGroupTree}
<div class="softcredit_custom_groups">
<br>
{include file="CRM/Custom/Form/Search.tpl" groupTree=$contributionSoftGroupTree showHideLinks=false}
</div>
{/if}
{if $contributionSoftGroupTree OR $form.soft_credit_name}
<script type="text/javascript">
{literal}
CRM.$(function($) {
$('div#contribution_soft_credit_type_wrapper').append($('div.crm-softcredit-form-block-soft_credit_sort_name'));
$('div#contribution_soft_credit_type_wrapper').append($('div.softcredit_custom_groups'));
hideShowInMemoryOf();
$('select#contribution_or_softcredits').change(hideShowInMemoryOf);
function hideShowInMemoryOf() {
let softOption = $('select#contribution_or_softcredits').val();
if (softOption == 'only_contribs') {
$('div#contribution_soft_credit_type_wrapper').show();
}
}
});
{/literal}
</script>
{/if}

View 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_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}
{* 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_custom'}
</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_custom'}
</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>