From dc677c00d8958d29f48e9b6224dcc42409225f66 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 28 Apr 2014 23:51:53 -0700 Subject: [PATCH] CRM-14527 migrate cid=0 to use entity ref --- CRM/Core/Form.php | 28 ++++++----- js/AutoComplete.js | 82 +++++++------------------------- templates/CRM/common/cidzero.tpl | 6 +-- 3 files changed, 32 insertions(+), 84 deletions(-) diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 992d818774..28d262bdca 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1453,25 +1453,17 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ function addAutoSelector($profiles = array(), $autoCompleteField = array()) { $autoCompleteField = array_merge(array( - 'name_field' => 'select_contact', 'id_field' => 'select_contact_id', - 'field_text' => ts('Select Contact'), + 'placeholder' => ts('Select someone else ...'), 'show_hide' => TRUE, - 'show_text' => ts('to select someone already in our database.'), - 'hide_text' => ts('to clear this person\'s information, and fill the form in for someone else'), - 'url' => array('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'), - 'max' => civicrm_api3('setting', 'getvalue', array( - 'name' => 'search_autocomplete_count', - 'group' => 'Search Preferences', - )) + 'api' => array('params' => array('contact_type' => 'Individual')) ), $autoCompleteField); - if(0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1)))) { - $this->addElement('text', $autoCompleteField['name_field'] , $autoCompleteField['field_text']); - $this->addElement('hidden', $autoCompleteField['id_field'], '', array('id' => $autoCompleteField['id_field'])); + if($this->canUseAjaxContactLookups()) { $this->assign('selectable', $autoCompleteField['id_field']); + $this->addEntityRef($autoCompleteField['id_field'], NULL, array('placeholder' => $autoCompleteField['placeholder'], 'api' => $autoCompleteField['api'])); - CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/AutoComplete.js') + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js') ->addSetting(array( 'form' => array('autocompletes' => $autoCompleteField), 'ids' => array('profile' => $profiles), @@ -1479,6 +1471,16 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } } + /** + * + */ + function canUseAjaxContactLookups() { + if (0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1))) && + CRM_Core_Permission::check(array(array('access AJAX API', 'access CiviCRM')))) { + return TRUE; + } + } + /** * Add the options appropriate to cid = zero - ie. autocomplete * diff --git a/js/AutoComplete.js b/js/AutoComplete.js index ee38992cf7..1dc348ee82 100644 --- a/js/AutoComplete.js +++ b/js/AutoComplete.js @@ -14,83 +14,36 @@ CRM.$(function($) { } }; - //The following methods are queued under fx for more - //flexibility when combining with $.fn.delay() and - //jQuery effects. - - //Set caret to a particular index - $.fn.caretTo = function (index, offset) { - return this.queue(function (next) { - if (isNaN(index)) { - var i = $(this).val().indexOf(index); - if (offset === true) { - i += index.length; - } - else if (offset) { - i += offset; - } - $.caretTo(this, i); - } - else { - $.caretTo(this, index); - } - next(); - }); -}; - /** * Display a personalized message containing the contact's name * and a variable from the server */ - function assignAutoComplete(select_field, id_field, url, varmax, profileids, autocomplete) { - if(varmax === undefined) {varmax = 10;} + function assignAutoComplete( id_field, profileids, autocomplete) { if(profileids === undefined) {profileids = [];} - if(url === undefined) { - url = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'); - } - - var customObj = $('#' + select_field); var customIdObj = $('#' + id_field); - if (!customObj.hasClass('ac_input')) { - customObj.autocomplete(url, - { width : 250, selectFirst : false, matchContains: true, max: varmax }).result( - function (event, data) { - var contactID = data[1]; - customIdObj.val(contactID); - customObj.caretTo(0); - var namefields = ['first_name', 'last_name', 'middle_name']; - CRM.api('profile', 'get', {'profile_id' : profileids, 'contact_id' : contactID}, { - success: function(result) { - $.each(result.values, function (id, value){ + customIdObj.on('change', function (event, data) { + var contactID = $(this).val(); + console.log(contactID); + var namefields = ['first_name', 'last_name', 'middle_name']; + CRM.api('profile', 'get', {'profile_id': profileids, 'contact_id': contactID}, { + success: function (result) { + $.each(result.values, function (id, value) { $.each(value, function (fieldname, fieldvalue) { $('#' + fieldname).val(fieldvalue); }); - }); - } - }); - } - ); - customObj.click(function () { - customIdObj.val(''); - }); - } - - if(autocomplete.show_hide) { - customObj.hide(); - showHideAutoComplete(select_field, id_field, - autocomplete.show_text, - autocomplete.hide_text, - profileids - ); + }); + } + }); + } + ) } - } - + /** * Show or hide the autocomplete and change the text */ - function showHideAutoComplete(name_field, id_field, hidden_text, shown_text, profileids) { + function showHideAutoComplete(id_field, hidden_text, shown_text, profileids) { $('#crm-contact-toggle-' + id_field).on('click', function(event) { event.preventDefault(); $('#' + name_field).toggle(); @@ -110,15 +63,12 @@ CRM.$(function($) { } }); } - }); } var autocompletes = CRM.form.autocompletes; - var url = CRM.url(autocompletes.url[0], autocompletes.url[1]); - $(autocompletes).each(function (index, autocomplete) { - assignAutoComplete(autocomplete.name_field, autocomplete.id_field, url, autocomplete.max, CRM.ids.profile, autocomplete); + assignAutoComplete(autocomplete.id_field, CRM.ids.profile, autocomplete); } ); diff --git a/templates/CRM/common/cidzero.tpl b/templates/CRM/common/cidzero.tpl index 53b33f037c..7e641a7fe9 100644 --- a/templates/CRM/common/cidzero.tpl +++ b/templates/CRM/common/cidzero.tpl @@ -2,13 +2,9 @@
{ts}You are completing this form on behalf of someone else. Please enter their details.{/ts} {if !empty($selectable)} - {ts}

Click here - to select someone already in our database.

{/ts} - -
- {$form.select_contact.html} + {$form.select_contact_id.html}
{/if} -- 2.25.1