From ccec9d6bd68433bb076bc0674f1ed6eca1dd419a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 17 Feb 2014 15:55:46 -0800 Subject: [PATCH] CRM-13929 Refactor contribution forms --- CRM/Batch/Form/Entry.php | 30 +++++---------- CRM/Contribute/Form/Contribution.php | 15 +++----- CRM/Contribute/Form/SoftCredit.php | 31 ++++++--------- CRM/Core/BAO/UFGroup.php | 2 +- templates/CRM/Batch/Form/Entry.tpl | 4 +- .../CRM/Contribute/Form/Contribution.tpl | 4 +- templates/CRM/Contribute/Form/SoftCredit.tpl | 38 +++---------------- 7 files changed, 37 insertions(+), 87 deletions(-) diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index 309b4a2c25..f9764a48ab 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -194,7 +194,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { $contactTypes = array('Contact', 'Individual', 'Household', 'Organization'); $contactReturnProperties = array(); for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) { - CRM_Contact_Form_NewContact::buildQuickForm($this, $rowNumber, NULL, TRUE, 'primary_', ts('Contact')); + $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array('create' => TRUE)); // special field specific to membership batch udpate if ($this->_batchInfo['type_id'] == 2) { @@ -256,7 +256,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { $batchTotal += $value['total_amount']; //validate for soft credit fields - if (!empty($params['soft_credit_contact_select_id'][$key]) && empty($params['soft_credit_amount'][$key])) { + if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) { $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); } if (!empty($params['soft_credit_amount']) && !empty($params['soft_credit_amount'][$key]) && CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule::cleanMoney($value['total_amount'])) { @@ -271,16 +271,6 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { } } - // if contact name is set for a row using autocomplete widget then make sure contact id exists, CRM-13078 - // I was not able to replicate this on my local but adding this check and hopefully it will fix the issue. - if (!empty($params['primary_contact'])) { - foreach($params['primary_contact'] as $rowIndex => $contactName) { - if (empty($params['primary_contact_select_id'][$rowIndex])) { - $errors['primary_contact['.$rowIndex.']'] = ts('Please select a valid contact.'); - } - } - } - if ($batchTotal != $self->_batchInfo['total']) { $self->assign('batchAmountMismatch', TRUE); $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.'); @@ -406,18 +396,18 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { if (isset($params['field'])) { foreach ($params['field'] as $key => $value) { // if contact is not selected we should skip the row - if (empty($params['primary_contact_select_id'][$key])) { + if (empty($params['primary_contact_id'][$key])) { continue; } - $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_select_id']); + $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']); // update contact information $this->updateContactInfo($value); //build soft credit params - if (!empty($params['soft_credit_contact_select_id'][$key]) && !empty($params['soft_credit_amount'][$key])) { - $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_select_id'][$key]; + if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) { + $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key]; $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); $value['soft_credit'][$key]['soft_credit_type_id'] = $params['field'][$key]['soft_credit_type']; } @@ -554,11 +544,11 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { $customFields = array(); foreach ($params['field'] as $key => $value) { // if contact is not selected we should skip the row - if (empty($params['primary_contact_select_id'][$key])) { + if (empty($params['primary_contact_id'][$key])) { continue; } - $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_select_id']); + $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']); // update contact information $this->updateContactInfo($value); @@ -630,8 +620,8 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { } // handle soft credit - if (is_array(CRM_Utils_Array::value('soft_credit_contact_select_id', $params)) && !empty($params['soft_credit_contact_select_id'][$key]) && CRM_Utils_Array::value($key, $params['soft_credit_amount'])) { - $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_select_id'][$key]; + if (is_array(CRM_Utils_Array::value('soft_credit_contact_id', $params)) && !empty($params['soft_credit_contact_id'][$key]) && CRM_Utils_Array::value($key, $params['soft_credit_amount'])) { + $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key]; $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 8c6db5605f..c1fd03e705 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -592,7 +592,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $this->assign('entityID', $this->_id); if ($this->_context == 'standalone') { - CRM_Contact_Form_NewContact::buildQuickForm($this); + $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE), TRUE); } $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); @@ -860,11 +860,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP static function formRule($fields, $files, $self) { $errors = array(); - //check if contact is selected in standalone mode - if (isset($fields['contact_select_id'][1]) && !$fields['contact_select_id'][1]) { - $errors['contact[1]'] = ts('Please select a contact or create new contact'); - } - //check for Credit Card Contribution. if ($self->_mode) { if (empty($fields['payment_processor_id'])) { @@ -1034,7 +1029,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } } else { - $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_select_id'])); + $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'])); if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) { //Delete existing soft credit records if soft credit list is empty on update CRM_Contribute_BAO_ContributionSoft::del(array('contribution_id' => $this->_id)); @@ -1042,7 +1037,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP else { //build soft credit params $softParams = $softIDs =array(); - foreach ($submittedValues['soft_credit_contact_select_id'] as $key => $val) { + foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) { if ($val && $submittedValues['soft_credit_amount'][$key]) { $softParams[$key]['contact_id'] = $val; $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($submittedValues['soft_credit_amount'][$key]); @@ -1056,8 +1051,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } // set the contact, when contact is selected - if (!empty($submittedValues['contact_select_id'])) { - $this->_contactID = $submittedValues['contact_select_id'][1]; + if (!empty($submittedValues['contact_id'])) { + $this->_contactID = $submittedValues['contact_id']; } $config = CRM_Core_Config::singleton(); diff --git a/CRM/Contribute/Form/SoftCredit.php b/CRM/Contribute/Form/SoftCredit.php index 1767047a30..681a595400 100644 --- a/CRM/Contribute/Form/SoftCredit.php +++ b/CRM/Contribute/Form/SoftCredit.php @@ -68,7 +68,7 @@ class CRM_Contribute_Form_SoftCredit { /** * Function used to build form element for soft credit block * - * @param object $form form object + * @param CRM_Core_Form $form * @access public * * @return void @@ -97,12 +97,10 @@ class CRM_Contribute_Form_SoftCredit { return $form; } - $prefix = 'soft_credit_'; // by default generate 5 blocks $item_count = 6; $showSoftCreditRow = 2; - $showCreateNew = TRUE; if ($form->_action & CRM_Core_Action::UPDATE) { $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE); } @@ -124,23 +122,17 @@ class CRM_Contribute_Form_SoftCredit { if (!empty($form->_softCreditInfo['soft_credit'])) { $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']); $showSoftCreditRow++; - $showCreateNew = FALSE; } } for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { - CRM_Contact_Form_NewContact::buildQuickForm($form, $rowNumber, NULL, FALSE, $prefix); + $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), array('create' => TRUE)); - $form->addMoney("{$prefix}amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); + $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); - $form->add('select', "{$prefix}type[{$rowNumber}]", - ts( 'Soft Credit Type' ), - array( - '' => ts('- select -')) + - CRM_Core_OptionGroup::values("{$prefix}type", FALSE) - ); + $form->addSelect("soft_credit_type[{$rowNumber}]", array('entity' => 'contribution_soft', 'field' => 'soft_credit_type_id', 'label' => ts('Type'))); if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { - $form->add('hidden', "{$prefix}id[{$rowNumber}]", + $form->add('hidden', "soft_credit_id[{$rowNumber}]", $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); } } @@ -162,9 +154,8 @@ class CRM_Contribute_Form_SoftCredit { } $form->assign('showSoftCreditRow', $showSoftCreditRow); $form->assign('rowCount', $item_count); - $form->assign('showCreateNew', $showCreateNew); $form->addElement('hidden', 'sct_default_id', - CRM_Core_OptionGroup::getDefaultValue("{$prefix}type"), + CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), array('id' => 'sct_default_id') ); @@ -181,7 +172,7 @@ class CRM_Contribute_Form_SoftCredit { if (!empty($form->_softCreditInfo['soft_credit'])) { foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a'); - $defaults["soft_credit_contact_select_id[$key]"] = $value['contact_id']; + $defaults["soft_credit_contact_id[$key]"] = $value['contact_id']; $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; } } @@ -220,10 +211,10 @@ class CRM_Contribute_Form_SoftCredit { } if (!empty($fields['soft_credit_amount'])) { - $repeat = array_count_values($fields['soft_credit_contact_select_id']); + $repeat = array_count_values($fields['soft_credit_contact_id']); foreach ($fields['soft_credit_amount'] as $key => $val) { - if (!empty($fields['soft_credit_contact_select_id'][$key])) { - if ($repeat[$fields['soft_credit_contact_select_id'][$key]] > 1) { + if (!empty($fields['soft_credit_contact_id'][$key])) { + if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) { $errors["soft_credit_contact[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); } if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] @@ -233,7 +224,7 @@ class CRM_Contribute_Form_SoftCredit { if (empty($fields['soft_credit_amount'][$key])) { $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); } - $contactType = CRM_Contact_BAO_Contact::getContactType($fields['soft_credit_contact_select_id'][$key]); + $contactType = CRM_Contact_BAO_Contact::getContactType($fields['soft_credit_contact_id'][$key]); if ($self->_honoreeProfileType && $self->_honoreeProfileType != $contactType) { $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType)); } diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index b2077302a3..b4cde4ed54 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -2036,7 +2036,7 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) $form->addElement('checkbox', $name, $title); } elseif ($fieldName == 'soft_credit') { - CRM_Contact_Form_NewContact::buildQuickForm($form, $rowNumber, NULL, FALSE, 'soft_credit_'); + $form->addEntityRef("soft_credit_contact_id[$rowNumber]", ts('Soft Credit To'), array('create' => TRUE)); $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); } elseif ($fieldName == 'product_name') { diff --git a/templates/CRM/Batch/Form/Entry.tpl b/templates/CRM/Batch/Form/Entry.tpl index 96095363f3..1d13d48922 100644 --- a/templates/CRM/Batch/Form/Entry.tpl +++ b/templates/CRM/Batch/Form/Entry.tpl @@ -73,7 +73,7 @@
{* contact select/create option*}
- {include file="CRM/Contact/Form/NewContact.tpl" blockNo = $rowNumber noLabel=true prefix="primary_" newContactCallback="updateContactInfo($rowNumber, 'primary_')"} + {$form.primary_contact_id.$rowNumber.html|crmAddClass:big}
{if $batchType eq 2 } @@ -90,7 +90,7 @@ {elseif $n eq 'soft_credit'}
- {include file="CRM/Contact/Form/NewContact.tpl" blockNo = $rowNumber noLabel=true prefix="soft_credit_"} + {$form.soft_credit_contact_id.$rowNumber.html|crmAddClass:big} {$form.soft_credit_amount.$rowNumber.label} {$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight}
{elseif in_array( $fields.$n.html_type, array('Radio', 'CheckBox'))} diff --git a/templates/CRM/Contribute/Form/Contribution.tpl b/templates/CRM/Contribute/Form/Contribution.tpl index cb84fcf282..2db480deac 100644 --- a/templates/CRM/Contribute/Form/Contribution.tpl +++ b/templates/CRM/Contribute/Form/Contribution.tpl @@ -88,8 +88,8 @@ {if !$contributionMode and !$email and $outBound_option != 2} {assign var='profileCreateCallback' value=1 } {/if} - {* note that if we are using multiple instances of NewContact always pass values for blockNo and prefix *} - {include file="CRM/Contact/Form/NewContact.tpl" blockNo=1 prefix=''} + {$form.contact_id.label} + {$form.contact_id.html} {/if} {if $contributionMode} {$form.payment_processor_id.label} * {$form.payment_processor_id.html} diff --git a/templates/CRM/Contribute/Form/SoftCredit.tpl b/templates/CRM/Contribute/Form/SoftCredit.tpl index 39051f2681..2e0e9bbb10 100644 --- a/templates/CRM/Contribute/Form/SoftCredit.tpl +++ b/templates/CRM/Contribute/Form/SoftCredit.tpl @@ -45,28 +45,21 @@ {assign var='rowNumber' value=$smarty.section.i.index} - {ts}Select Contact{/ts} - {assign var='createNewStatus' value=true} - {if !$showCreateNew and $rowNumber lt $showSoftCreditRow} - {assign var='createNewStatus' value=false} - {/if} - {include file="CRM/Contact/Form/NewContact.tpl" noLabel=true skipBreak=true blockNo=$rowNumber - prefix="soft_credit_" showNewSelect=$createNewStatus focus=false} + {$form.soft_credit_contact_id.$rowNumber.label} {$form.soft_credit_contact_id.$rowNumber.html|crmAddClass:twenty} {$form.soft_credit_amount.$rowNumber.label} {$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight} - {$form.soft_credit_type.$rowNumber.label} {$form.soft_credit_type.$rowNumber.html|crmAddClass:eight} -  {ts}delete{/ts} + {$form.soft_credit_type.$rowNumber.label} {$form.soft_credit_type.$rowNumber.html} +   {/section} - - {ts}add another soft credit{/ts} + {ts}another soft credit{/ts} @@ -101,33 +94,14 @@ $("#pcp_made_through_id" ).val( data[1]); }); - var rowCnt = 1; - $('input[name^="soft_credit_contact_select_id["]').each(function(){ - if ($(this).val()){ - var dataUrl = CRM.url('civicrm/ajax/rest', - 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&id=' + $(this).val()); - $.ajax({ - url : dataUrl, - async : false, - success : function(html){ - htmlText = html.split( '|' , 2); - $('#soft_credit_contact_' + rowCnt).val(htmlText[0]); - rowCnt++; - } - }); - } - }); - $('.crm-soft-credit-block tr span').each(function () { if ($(this).hasClass('crm-error')) { $(this).parents('tr').show(); } }); - $('.delete-link').click(function(){ - var row = $(this).attr('row-no'); - $('#soft-credit-row-' + row).hide().find('input').val(''); - $('input[name="soft_credit_contact_select_id['+row+']"]').val(''); + $('.soft-credit-delete-link').click(function(){ + $(this).closest('tr').hide().find('input').val('').change(); return false; }); -- 2.25.1