From 8250601effdcf47e02208188da04d31da70bf2f8 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 23 Feb 2014 13:05:02 -0500 Subject: [PATCH] CRM-13966 - Allow extra data to be returned with autocomplete results --- api/v3/Contact.php | 10 +++++++--- api/v3/Event.php | 6 +++++- api/v3/Generic/Getlist.php | 22 ++++++++++++++++------ tests/phpunit/api/v3/TagTest.php | 6 ++++-- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 4031b9b0f4..3e0b6a863c 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -923,8 +923,9 @@ function _civicrm_api3_contact_getlist_params(&$request) { if(!in_array($searchField, $list)) { $list[] = $searchField; } + $request['description_field'] = $list; $list[] = 'contact_type'; - $request['params']['return'] = $list; + $request['params']['return'] = array_unique(array_merge($list, $request['extra'])); $request['params']['options']['sort'] = 'sort_name'; // Contact api doesn't support array(LIKE => 'foo') syntax $request['params'][$request['search_field']] = $request['input']; @@ -948,8 +949,8 @@ function _civicrm_api3_contact_getlist_output($result, $request) { 'label' => $row[$request['label_field']], 'description' => array(), ); - foreach ($request['params']['return'] as $item) { - if (!strpos($item, '_name') && $item != 'contact_type' && !in_array($item, $addressFields) && !empty($row[$item])) { + foreach ($request['description_field'] as $item) { + if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) { $data['description'][] = $row[$item]; } } @@ -968,6 +969,9 @@ function _civicrm_api3_contact_getlist_output($result, $request) { else { $data['icon_class'] = $row['contact_type']; } + foreach ($request['extra'] as $field) { + $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; + } $output[] = $data; } } diff --git a/api/v3/Event.php b/api/v3/Event.php index 78fd052734..47194379da 100644 --- a/api/v3/Event.php +++ b/api/v3/Event.php @@ -238,7 +238,8 @@ function _civicrm_api3_event_getisfull(&$event, $event_id) { * @param $request array */ function _civicrm_api3_event_getlist_params(&$request) { - $request['params']['return'] = array('start_date', 'event_type_id', 'title', 'summary'); + $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary'); + $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); $request['params']['options']['sort'] = 'start_date DESC'; $request['params'] += array( 'is_template' => 0, @@ -269,6 +270,9 @@ function _civicrm_api3_event_getlist_output($result, $request) { if (!empty($row['summary'])) { $data['description'][] = $row['summary']; } + foreach ($request['extra'] as $field) { + $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; + } $output[] = $data; } } diff --git a/api/v3/Generic/Getlist.php b/api/v3/Generic/Getlist.php index 2754159c9e..448282f67d 100644 --- a/api/v3/Generic/Getlist.php +++ b/api/v3/Generic/Getlist.php @@ -74,7 +74,9 @@ function _civicrm_api3_generic_getList_defaults($entity, &$request) { 'input' => '', 'image_field' => NULL, 'id_field' => $entity == 'option_value' ? 'value' : 'id', + 'description_field' => array(), 'params' => array(), + 'extra' => array(), ); // Find main field from meta foreach (array('sort_name', 'title', 'label', 'name') as $field) { @@ -83,10 +85,10 @@ function _civicrm_api3_generic_getList_defaults($entity, &$request) { break; } } + // Find fields to be used for the description foreach (array('description') as $field) { if (isset($fields[$field])) { - $defaults['description_field'] = $field; - break; + $defaults['description_field'][] = $field; } } $resultsPerPage = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); @@ -127,9 +129,9 @@ function _civicrm_api3_generic_getlist_params(&$request) { $fieldsToReturn[] = $request['image_field']; } if (!empty($request['description_field'])) { - $fieldsToReturn[] = $request['description_field']; + $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']); } - $request['params']['return'] = $fieldsToReturn; + $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); } /** @@ -149,11 +151,19 @@ function _civicrm_api3_generic_getlist_output($result, $request) { 'label' => $row[$request['label_field']], ); if (!empty($request['description_field'])) { - $data['description'] = !empty($row[$request['description_field']]) ? array($row[$request['description_field']]) : NULL; + $data['description'] = array(); + foreach ((array) $request['description_field'] as $field) { + if (!empty($row[$field])) { + $data['description'][] = $row[$field]; + } + } }; if (!empty($request['image_field'])) { $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; - }; + } + foreach ($request['extra'] as $field) { + $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; + } $output[] = $data; } } diff --git a/tests/phpunit/api/v3/TagTest.php b/tests/phpunit/api/v3/TagTest.php index 33902c823e..ca5eff2ff3 100644 --- a/tests/phpunit/api/v3/TagTest.php +++ b/tests/phpunit/api/v3/TagTest.php @@ -196,10 +196,12 @@ class api_v3_TagTest extends CiviUnitTestCase { $description = "Demonstrates use of api.getlist for autocomplete and quicksearch applications"; $params = array( 'input' => $this->tag['name'], + 'extra' => array('used_for') ); - $result = $this->callAPIAndDocument('tag', 'getlist', $params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument('tag', 'getlist', $params, __FUNCTION__, __FILE__, $description); $this->assertEquals($this->tag['id'], $result['values'][0]['id'], 'In line ' . __LINE__); - $this->assertEquals($this->tag['description'], $result['values'][0]['description'], 'In line ' . __LINE__); + $this->assertEquals($this->tag['description'], $result['values'][0]['description'][0], 'In line ' . __LINE__); + $this->assertEquals($this->tag['used_for'], $result['values'][0]['extra']['used_for'], 'In line ' . __LINE__); } } -- 2.25.1