CRM-13966 - Replace pcp autocomplete with select2
authorColeman Watts <coleman@civicrm.org>
Sun, 23 Mar 2014 14:34:36 +0000 (10:34 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 23 Mar 2014 14:34:36 +0000 (10:34 -0400)
CRM/Contact/Page/AJAX.php
CRM/Contribute/Form/SoftCredit.php
templates/CRM/Contribute/Form/Contribution.tpl
templates/CRM/Contribute/Form/SoftCredit.tpl

index 1be7e611528a02fabfcf27ab48e7ceee25e100ff..abd85cd5b246ee52c6a3ce9af056c0d87c695164 100644 (file)
@@ -236,9 +236,10 @@ class CRM_Contact_Page_AJAX {
     $dao = CRM_Core_DAO::executeQuery($query);
     $results = array();
     while ($dao->fetch()) {
-      $results[$dao->id] = $dao->data;
+      $results[] = array('id' => $dao->id, 'text' => $dao->data);
     }
-    CRM_Core_Page_AJAX::autocompleteResults($results);
+    print json_encode($results);
+    CRM_Utils_System::civiExit();
   }
 
   static function relationship() {
index 5109621a6481399a3dc44d3170c220a7df4c90ad..55628b02f09ca4d38dfb1b649cb029bb576b7005 100644 (file)
@@ -141,13 +141,8 @@ class CRM_Contribute_Form_SoftCredit {
     $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage();
     if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) {
       $form->assign('siteHasPCPs', 1);
-      $pcpDataUrl = CRM_Utils_System::url('civicrm/ajax/rest',
-        'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1',
-        FALSE, NULL, FALSE
-      );
-      $form->assign('pcpDataUrl', $pcpDataUrl);
-      $form->addElement('text', 'pcp_made_through', ts('Credit to a Personal Campaign Page'));
-      $form->addElement('hidden', 'pcp_made_through_id', '', array('id' => 'pcp_made_through_id'));
+      $form->add('hidden', 'pcp_made_through'); // stores the label
+      $form->add('text', 'pcp_made_through_id', ts('Credit to a Personal Campaign Page'), array('class' => 'twenty'));
       $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL);
       $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)'));
       $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)'));
@@ -206,7 +201,7 @@ class CRM_Contribute_Form_SoftCredit {
       if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) ||
         CRM_Utils_Array::value('pcp_personal_note', $fields)
       ) {
-        $errors['pcp_made_through'] = ts('Please select a Personal Campaign Page, OR uncheck Display in Honor Roll and clear both the Honor Roll Name and the Personal Note field.');
+        $errors['pcp_made_through_id'] = ts('Please select a Personal Campaign Page, OR uncheck Display in Honor Roll and clear both the Honor Roll Name and the Personal Note field.');
       }
     }
 
index 86943d24944ea483cd71e087a0d3c8132754bf5d..de6f9322d37c3b49b62fdab99b4442635ede9a77 100644 (file)
               </td>
             </tr>
             <tr class="crm-contribution-pcp-block crm-contribution-form-block-pcp_made_through_id hiddenElement">
-              <td class="label">{$form.pcp_made_through.label}</td>
+              <td class="label">{$form.pcp_made_through_id.label}</td>
               <td>
-                {$form.pcp_made_through.html} &nbsp;
+                {$form.pcp_made_through_id.html} &nbsp;
                 <span class="showSoftCreditLink">
                   <a href="#" id="showSoftCredit">{ts}unlink from personal campaign page{/ts}</a>
                 </span><br/>
index 25bb5a5aa2880e82ed2b10588aa50b6cac196a7f..f45897e4a49c4f9993b860bb55c4cc2e82cc65d2 100644 (file)
       return false;
     });
 
-    var pcpURL = CRM.url('civicrm/ajax/rest',
-      'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1');
-    $('#pcp_made_through').autocomplete(pcpURL,
-      { width : 360, selectFirst : false, matchContains: true
-      }).result( function(event, data, formatted) {
-        $("#pcp_made_through_id" ).val( data[1]);
-      });
+    // FIXME: This could be much simpler as an entityRef field but pcp doesn't have a searchable api :(
+    var pcpURL = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1');
+    $('#pcp_made_through_id').crmSelect2({
+      placeholder: {/literal}'{ts escape="js"}- select -{/ts}'{literal},
+      minimumInputLength: 1,
+      ajax: {
+        url: pcpURL,
+        data: function(term) {
+          return {term: term};
+        },
+        results: function(response) {
+          return {results: response};
+        }
+      },
+      initSelection: function(el, callback) {
+        callback({id: $(el).val(), text: $('#pcp_made_through').val()});
+      }
+    });
 
     $('.crm-soft-credit-block tr span').each(function () {
       if ($(this).hasClass('crm-error')) {