CRM-13966 - Refactor event fee autocomplete
authorColeman Watts <coleman@civicrm.org>
Sun, 2 Mar 2014 01:37:14 +0000 (20:37 -0500)
committerColeman Watts <coleman@civicrm.org>
Sun, 2 Mar 2014 02:37:06 +0000 (21:37 -0500)
CRM/Event/BAO/Query.php
CRM/Event/Page/AJAX.php
templates/CRM/Event/Form/Search/Common.tpl

index 520773f1523d05ed92490484f086c73ef12e5734..2d504c76a729accaee4b97125495925015914d9e 100644 (file)
@@ -565,10 +565,7 @@ class CRM_Event_BAO_Query {
         ),
       )
     );
-    $participantFee = &$form->add('text', 'participant_fee_level', ts('Fee Level'));
-
-    //elements for assigning value operation
-    $participantFeeId = &$form->add('hidden', 'participant_fee_id', '', array('id' => 'participant_fee_id'));
+    $form->add('text', 'participant_fee_id', ts('Fee Level'), array('class' => 'big crm-ajax-select'));
 
     CRM_Core_Form_Date::buildDateRange($form, 'event', 1, '_start_date_low', '_end_date_high', ts('From'), FALSE);
 
index 410c5e172fb404fb4bdad666e824bab17d743bc2..89e62197971741c4a3149b5182a43e6362fedac8 100644 (file)
@@ -40,9 +40,10 @@ class CRM_Event_Page_AJAX {
 
   /**
    * Function for building EventFee combo box
+   * FIXME: This ajax callback could be eliminated in favor of an entityRef field but the priceFieldValue api doesn't currently support filtering on entity_table
    */
   function eventFee() {
-    $name = trim(CRM_Utils_Type::escape($_GET['s'], 'String'));
+    $name = trim(CRM_Utils_Type::escape($_GET['term'], 'String'));
 
     if (!$name) {
       $name = '%';
@@ -61,9 +62,10 @@ GROUP BY cv.label";
     $dao = CRM_Core_DAO::executeQuery($query);
     $results = array();
     while ($dao->fetch()) {
-      $results[$dao->id] = $dao->label;
+      $results[] = array('id' => $dao->id, 'text' => $dao->label);
     }
-    CRM_Core_Page_AJAX::autocompleteResults($results);
+    echo json_encode($results);
+    CRM_Utils_System::civiExit();
   }
 
 }
index f209a72042aafd7fc73b62796ad8f141ca80a44c..8f28ab2e9906a7eba68c2491597754c52f721f1f 100644 (file)
@@ -65,8 +65,8 @@
   </td>
 </tr>
 <tr>
-  <td class="crm-event-form-block-participant_fee_level">
-    {$form.participant_fee_level.label}<br />{$form.participant_fee_level.html}
+  <td class="crm-event-form-block-participant_fee_id">
+    {$form.participant_fee_id.label}<br />{$form.participant_fee_id.html}
   </td>
   <td class="crm-event-form-block-participant_fee_amount">
     <label>{ts}Fee Amount{/ts}</label><br />
@@ -89,10 +89,27 @@ campaignTrClass='' campaignTdClass='crm-event-form-block-participant_campaign_id
 
 {literal}
 <script type="text/javascript">
-var feeUrl   = "{/literal}{$dataURLEventFee}{literal}";
-
-cj('#participant_fee_level').autocomplete( feeUrl, { width : 180, selectFirst : false, matchContains: true
-}).result(function(event, data, formatted) { cj( "input#participant_fee_id" ).val( data[1] );
-  }).bind( 'click', function( ) { cj( "input#participant_fee_id" ).val(''); });
+cj(function($) {
+  // FIXME: This could be much simpler as an entityRef field but the priceFieldValue api doesn't currently support the filters we need
+  $('#participant_fee_id').select2({
+    placeholder: {/literal}'{ts escape="js"}- any -{/ts}'{literal},
+    minimumInputLength: 1,
+    allowClear: true,
+    ajax: {
+      url: "{/literal}{$dataURLEventFee}{literal}",
+      data: function(term) {
+        return {term: term};
+      },
+      results: function(response) {
+        return {results: response};
+      }
+    },
+    initSelection: function(el, callback) {
+      CRM.api3('price_field_value', 'getsingle', {id: $(el).val()}).done(function(data) {
+        callback({id: data.id, text: data.label});
+      });
+    }
+  });
+});
 </script>
 {/literal}