CRM-13929 Refactor contribution forms
authorColeman Watts <coleman@civicrm.org>
Mon, 17 Feb 2014 23:55:46 +0000 (15:55 -0800)
committerColeman Watts <coleman@civicrm.org>
Tue, 18 Feb 2014 06:09:08 +0000 (22:09 -0800)
CRM/Batch/Form/Entry.php
CRM/Contribute/Form/Contribution.php
CRM/Contribute/Form/SoftCredit.php
CRM/Core/BAO/UFGroup.php
templates/CRM/Batch/Form/Entry.tpl
templates/CRM/Contribute/Form/Contribution.tpl
templates/CRM/Contribute/Form/SoftCredit.tpl

index 309b4a2c25a11799574acdba00ff80a113a84ec7..f9764a48ab32902ab148dda96f1ed2a3a6069479 100644 (file)
@@ -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]);
         }
 
index 8c6db5605fb1776e37aef27cb7ecab7c81a33f6d..c1fd03e705787b28e09e54f89d0326afce2b8d85 100644 (file)
@@ -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();
index 1767047a3069c5a853f0399f0e2cb08549339d88..681a595400a304de473162539c8f756cc1b1c936 100644 (file)
@@ -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));
           }
index b2077302a3328767f16cc133a86859690a62ebdc..b4cde4ed545faf813cccf13e5b60c65dce76ccf7 100644 (file)
@@ -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') {
index 96095363f366a6070350899682db96b672832d3c..1d13d48922fd0b4c82f3076c6400cc58a3583fc0 100644 (file)
@@ -73,7 +73,7 @@
         <div class="compressed crm-grid-cell"><span class="batch-edit"></span></div>
         {* contact select/create option*}
         <div class="compressed crm-grid-cell">
-          {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}
         </div>
 
         {if $batchType eq 2 }
@@ -90,7 +90,7 @@
             </div>
           {elseif $n eq 'soft_credit'}
             <div class="compressed crm-grid-cell">
-              {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}&nbsp;{$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight}
             </div>
           {elseif in_array( $fields.$n.html_type, array('Radio', 'CheckBox'))}
index cb84fcf2821291ce23fc217edcf4bf506be462fe..2db480deac4c25e8107ce3c2b102bae9b498eef5 100644 (file)
@@ -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=''}
+      <td class="label">{$form.contact_id.label}</td>
+      <td>{$form.contact_id.html}</td>
     {/if}
     {if $contributionMode}
     <tr class="crm-contribution-form-block-payment_processor_id"><td class="label nowrap">{$form.payment_processor_id.label}<span class="marker"> * </span></td><td>{$form.payment_processor_id.html}</td></tr>
index 39051f26815d69ba6e5a8e74821eb4a4fe1fded1..2e0e9bbb10e6a6ea15de150c1d2f6b75c3d6e17d 100644 (file)
     {assign var='rowNumber' value=$smarty.section.i.index}
     <tr id="soft-credit-row-{$rowNumber}"
         class="crm-contribution-form-block-soft_credit_to {if $rowNumber gte $showSoftCreditRow}hiddenElement{/if}">
-      <td class="label">{ts}Select Contact{/ts}</td>
       <td>
-        {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}&nbsp;{$form.soft_credit_contact_id.$rowNumber.html|crmAddClass:twenty}
       </td>
       <td>
         {$form.soft_credit_amount.$rowNumber.label}&nbsp;{$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight}
       </td>
       <td>
-        {$form.soft_credit_type.$rowNumber.label}&nbsp;{$form.soft_credit_type.$rowNumber.html|crmAddClass:eight}
-        &nbsp;<a class="delete-link" row-no={$rowNumber} href="#">{ts}delete{/ts}</a>
+        {$form.soft_credit_type.$rowNumber.label}&nbsp;{$form.soft_credit_type.$rowNumber.html}
+        &nbsp;<a class="crm-hover-button soft-credit-delete-link" href="#"><span class="icon delete-icon"></span></a>
       </td>
     </tr>
   {/section}
   <tr>
-    <td></td>
     <td>
-      <a href="#" id="addMoreSoftCredit">{ts}add another soft credit{/ts}</a>
+      <a href="#" class="crm-hover-button" id="addMoreSoftCredit"><span class="icon add-icon"></span> {ts}another soft credit{/ts}</a>
     </td>
   </tr>
 </table>
         $("#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;
     });