From d64082526bd5d51a85ebab66cd64fcc1ea06c83f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 28 Jan 2014 15:01:55 -0800 Subject: [PATCH] CRM-10760 - Autocomplte - Create utility to abstract rendering of autocomplete output --- CRM/Case/Page/AJAX.php | 7 ++--- CRM/Contact/Page/AJAX.php | 48 +++++++++++++------------------ CRM/Core/Page/AJAX.php | 21 ++++++++++++++ CRM/Event/Page/AJAX.php | 16 ++++++----- CRM/Group/Form/Edit.php | 12 ++++++-- templates/CRM/Group/Form/Edit.tpl | 23 ++------------- 6 files changed, 66 insertions(+), 61 deletions(-) diff --git a/CRM/Case/Page/AJAX.php b/CRM/Case/Page/AJAX.php index 3bfb16fdab..30ae8a77a5 100644 --- a/CRM/Case/Page/AJAX.php +++ b/CRM/Case/Page/AJAX.php @@ -60,12 +60,11 @@ class CRM_Case_Page_AJAX { $excludeCaseIds = explode(',', $excludeIdStr); } $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds); - + $results = array(); foreach ($unclosedCases as $caseId => $details) { - echo $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ') ' . "|$caseId|" . $details['contact_id'] . '|' . $details['case_type'] . '|' . $details['sort_name'] . "\n"; + $results["$caseId|" . $details['contact_id'] . '|' . $details['case_type'] . '|' . $details['sort_name']] = $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ')'; } - - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } function processCaseTags() { diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index aa0f0c492f..04730ae2ba 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -86,12 +86,7 @@ class CRM_Contact_Page_AJAX { } $result = civicrm_api('Contact', 'getquick', $params); - if (empty($result['is_error']) && !empty($result['values'])) { - foreach ($result['values'] as $key => $val) { - echo "{$val['data']}|{$val['id']}\n"; - } - } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults(CRM_Utils_Array::value('values', $result), 'data'); } static function contactReference() { @@ -105,8 +100,7 @@ class CRM_Contact_Page_AJAX { $fldValues = array(); CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties); if (!$cf['id'] || !$cf['is_active'] || $cf['data_type'] != 'ContactReference') { - echo "$name|error\n"; - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name)); } if ($cf['filter']) { @@ -118,8 +112,7 @@ class CRM_Contact_Page_AJAX { if (!empty($action) && !in_array($action, array('get', 'lookup')) ) { - echo "$name|error\n"; - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name)); } } @@ -172,11 +165,10 @@ class CRM_Contact_Page_AJAX { $contact = civicrm_api('Contact', 'Get', $params); if (!empty($contact['is_error'])) { - echo "$name|error\n"; - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name)); } - $contactList = ''; + $contactList = array(); foreach ($contact['values'] as $value) { $view = array(); foreach ($return as $fld) { @@ -184,14 +176,14 @@ class CRM_Contact_Page_AJAX { $view[] = $value[$fld]; } } - echo $contactList = implode(' :: ', $view) . "|" . $value['id'] . "\n"; + $contactList[$value['id']] = implode(' :: ', $view); } if (!$contactList) { - echo "$name|$name\n"; + $contactList = array($name => $name); } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($contactList); } /** @@ -241,12 +233,11 @@ class CRM_Contact_Page_AJAX { "; $dao = CRM_Core_DAO::executeQuery($query); - + $results = array(); while ($dao->fetch()) { - echo $pcpList = "$dao->data|$dao->id\n"; + $results[$dao->id] = $dao->data; } - - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } /** @@ -258,14 +249,13 @@ class CRM_Contact_Page_AJAX { $label = CRM_Utils_Type::escape($_GET['s'], 'String'); $selectOption = CRM_Core_BAO_CustomOption::valuesByID($fieldID, $optionGroupID); - - $completeList = NULL; + $results = array(); foreach ($selectOption as $id => $value) { if (strtolower($label) == strtolower(substr($value, 0, strlen($label)))) { - echo $completeList = "$value|$id\n"; + $results[$id] = $value; } } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } static function relationship() { @@ -349,13 +339,13 @@ class CRM_Contact_Page_AJAX { $name = str_replace('*', '%', $name); $elements = CRM_Contact_BAO_Relationship::getPermissionedEmployer($cid, $name); - + $results = array(); if (!empty($elements)) { foreach ($elements as $cid => $name) { - echo $element = $name['name'] . "|$cid\n"; + $results[$cid] = $name['name']; } } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } @@ -366,7 +356,9 @@ class CRM_Contact_Page_AJAX { } /** - * Function for building contact combo box + * @deprecated + * Old quicksearch function. No longer used in core. + * @todo: Remove this function and associated menu entry in CiviCRM 5 */ static function search() { $json = TRUE; diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index 8acb653ab8..f9e1093e50 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -196,5 +196,26 @@ class CRM_Core_Page_AJAX { } CRM_Utils_System::civiExit(); } + + /** + * Send autocomplete results to the client. Input can be a simple or nested array. + * @param array $results - If nested array, also provide: + * @param string $val - array key to use as the value + * @param string $key - array key to use as the key + */ + static function autocompleteResults($results, $val='label', $key='id') { + $output = array(); + if (is_array($results)) { + foreach ($results as $k => $v) { + if (is_array($v)) { + echo $v[$val] . '|' . $v[$key] . "\n"; + } + else { + echo "$v|$k\n"; + } + } + } + CRM_Utils_System::civiExit(); + } } diff --git a/CRM/Event/Page/AJAX.php b/CRM/Event/Page/AJAX.php index 6e9e1fee80..44addaac0d 100644 --- a/CRM/Event/Page/AJAX.php +++ b/CRM/Event/Page/AJAX.php @@ -67,6 +67,7 @@ class CRM_Event_Page_AJAX { civicrm_event.title "; $dao = CRM_Core_DAO::executeQuery($query); + $results = array(); while ($dao->fetch()) { $fields = array(); foreach (array('title', 'city') as $field) { @@ -77,10 +78,9 @@ class CRM_Event_Page_AJAX { if (isset($dao->start_date)) { array_push($fields, CRM_Utils_Date::customFormat($dao->start_date)); } - $eventinfo = implode(' - ', $fields); - echo $elements = "$eventinfo|$dao->id\n"; + $results[$dao->id] = implode(' - ', $fields); } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } /** @@ -104,10 +104,11 @@ AND {$whereClause} ORDER by v.weight"; $dao = CRM_Core_DAO::executeQuery($query); + $results = array(); while ($dao->fetch()) { - echo $elements = "$dao->label|$dao->value\n"; + $results[$dao->value] = $dao->label; } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } /** @@ -131,10 +132,11 @@ LEFT JOIN civicrm_price_set_entity ce ON ce.price_set_id = cf.price_set_id WHERE ce.entity_table = 'civicrm_event' AND {$whereClause} GROUP BY cv.label"; $dao = CRM_Core_DAO::executeQuery($query); + $results = array(); while ($dao->fetch()) { - echo $elements = "$dao->label|$dao->id\n"; + $results[$dao->id] = $dao->label; } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::autocompleteResults($results); } function eventList() { diff --git a/CRM/Group/Form/Edit.php b/CRM/Group/Form/Edit.php index 5085648c98..17d97c96c4 100644 --- a/CRM/Group/Form/Edit.php +++ b/CRM/Group/Form/Edit.php @@ -183,8 +183,16 @@ class CRM_Group_Form_Edit extends CRM_Core_Form { //used in edit mode $this->_groupOrganizationID = $defaults['group_organization']; } - - $this->assign('organizationID', CRM_Utils_Array::value('organization_id',$defaults)); + if (!empty($defaults['organization_id'])) { + $result = civicrm_api3('contact', 'getquick', array( + 'org' => 1, + 'id' => $defaults['organization_id'] + )); + $this->assign('organizationName', $result['values'][0]['data']); + } + else { + $this->assign('organizationName', ''); + } } } diff --git a/templates/CRM/Group/Form/Edit.tpl b/templates/CRM/Group/Form/Edit.tpl index 9183f6256e..12bf2e17a4 100644 --- a/templates/CRM/Group/Form/Edit.tpl +++ b/templates/CRM/Group/Form/Edit.tpl @@ -117,7 +117,7 @@       {$form.organization.label} {$form.organization.html|crmAddClass:huge} -
+
{$organizationName}
@@ -151,26 +151,9 @@ cj('input[type=checkbox][name="group_type[{/literal}{$freezeMailignList}{literal cj('input[type=checkbox][name="group_type[{/literal}{$hideMailignList}{literal}]"]').hide(); cj('label[for="group_type[{/literal}{$hideMailignList}{literal}]"]').hide(); {/literal}{/if}{literal} -{/literal}{if $organizationID}{literal} - cj(document).ready( function() { - //group organzation default setting - var dataUrl = "{/literal}{crmURL p='civicrm/ajax/search' h=0 q="org=1&id=$organizationID"}{literal}"; - cj.ajax({ - url : dataUrl, - async : false, - success : function(html){ - //fixme for showing address in div - htmlText = html.split( '|' , 2); - htmlDiv = htmlText[0].replace( /::/gi, ' '); - cj('#organization').val(htmlText[0]); - cj('div#organization_address').html(htmlDiv); - } - }); - }); -{/literal}{/if}{literal} -var dataUrl = "{/literal}{$groupOrgDataURL}{literal}"; -cj('#organization').autocomplete( dataUrl, { +var dataUrl = "{/literal}{crmURL p='civicrm/ajax/rest' q='className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&org=1&context=groupcontact' h=0 }{literal}"; +cj('#organization').val(cj('#organization_address').text()).autocomplete( dataUrl, { width : 250, selectFirst : false, matchContains: true }).result( function(event, data, formatted) { cj( "#organization_id" ).val( data[1] ); -- 2.25.1