From 6bb107cefedbecc371bdc100b87992753e8e686c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 23 Mar 2014 10:34:36 -0400 Subject: [PATCH] CRM-13966 - Replace pcp autocomplete with select2 --- CRM/Contact/Page/AJAX.php | 5 ++-- CRM/Contribute/Form/SoftCredit.php | 11 +++----- .../CRM/Contribute/Form/Contribution.tpl | 4 +-- templates/CRM/Contribute/Form/SoftCredit.tpl | 25 +++++++++++++------ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index 1be7e61152..abd85cd5b2 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -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() { diff --git a/CRM/Contribute/Form/SoftCredit.php b/CRM/Contribute/Form/SoftCredit.php index 5109621a64..55628b02f0 100644 --- a/CRM/Contribute/Form/SoftCredit.php +++ b/CRM/Contribute/Form/SoftCredit.php @@ -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.'); } } diff --git a/templates/CRM/Contribute/Form/Contribution.tpl b/templates/CRM/Contribute/Form/Contribution.tpl index 86943d2494..de6f9322d3 100644 --- a/templates/CRM/Contribute/Form/Contribution.tpl +++ b/templates/CRM/Contribute/Form/Contribution.tpl @@ -238,9 +238,9 @@ - {$form.pcp_made_through.label} + {$form.pcp_made_through_id.label} - {$form.pcp_made_through.html}   + {$form.pcp_made_through_id.html}   {ts}unlink from personal campaign page{/ts}
diff --git a/templates/CRM/Contribute/Form/SoftCredit.tpl b/templates/CRM/Contribute/Form/SoftCredit.tpl index 25bb5a5aa2..f45897e4a4 100644 --- a/templates/CRM/Contribute/Form/SoftCredit.tpl +++ b/templates/CRM/Contribute/Form/SoftCredit.tpl @@ -88,13 +88,24 @@ 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')) { -- 2.25.1