From 78b203e53828f5b07bf5ebbe4cc46eb2ebd5de50 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 11 Mar 2014 21:28:06 -0400 Subject: [PATCH] Autocomplete & option list fixes --- api/v3/Generic/Getlist.php | 16 ++++++++++------ js/Common.js | 27 ++++++++++++++++----------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/api/v3/Generic/Getlist.php b/api/v3/Generic/Getlist.php index 7e6a4f6dc9..779c4c0738 100644 --- a/api/v3/Generic/Getlist.php +++ b/api/v3/Generic/Getlist.php @@ -50,13 +50,15 @@ function civicrm_api3_generic_getList($apiRequest) { $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output'; $values = $fnName($result, $request); - $last = $request['params']['limit'] - 1; - $output = array( + $output = array('page_num' => $request['page_num']); + + // Limit is set for searching but not fetching by id + if (!empty($request['params']['limit'])) { // If we have an extra result then this is not the last page - 'more_results' => isset($values[$last]), - 'page_num' => $request['page_num'], - ); - unset($values[$last]); + $last = $request['params']['limit'] - 1; + $output['more_results'] = isset($values[$last]); + unset($values[$last]); + } return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output); } @@ -114,6 +116,8 @@ function _civicrm_api3_generic_getList_defaults($entity, &$request) { if (is_string($request['id']) && strpos(',', $request['id'])) { $request['id'] = explode(',', $request['id']); } + // Don't run into search limits when prefilling selection + unset($params['limit'], $params['offset'], $request['params']['limit'], $request['params']['offset']); $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id']; } $request['params'] += $params; diff --git a/js/Common.js b/js/Common.js index 00c68cd2b6..70b02c2c83 100644 --- a/js/Common.js +++ b/js/Common.js @@ -321,8 +321,7 @@ CRM.validate = CRM.validate || { if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) { callback(multiple ? stored : stored[0]); } else { - var params = $el.data('api-params') || {}; - params.id = val; + var params = $.extend({}, $el.data('api-params') || {}, {id: val}); CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) { callback(multiple ? result.values : result.values[0]) }); @@ -1054,17 +1053,23 @@ CRM.validate = CRM.validate || { }) .on('click', 'a.crm-option-edit-link', function() { - var link = $(this); + var + link = $(this), + optionsChanged = false; CRM.loadForm(this.href, {openInline: 'a:not("[href=#], .no-popup")'}) - // Lots of things can happen once the form opens, this is the only event we can really rely on + .on('crmFormSuccess', function() { + optionsChanged = true; + }) .on('dialogclose', function() { - link.trigger('crmOptionsEdited'); - var $elects = $('select[data-option-edit-path="' + link.data('option-edit-path') + '"]'); - if ($elects.data('api-entity') && $elects.data('api-field')) { - CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')}) - .done(function(data) { - CRM.utils.setOptions($elects, data.values); - }); + if (optionsChanged) { + link.trigger('crmOptionsEdited'); + var $elects = $('select[data-option-edit-path="' + link.data('option-edit-path') + '"]'); + if ($elects.data('api-entity') && $elects.data('api-field')) { + CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')}) + .done(function (data) { + CRM.utils.setOptions($elects, data.values); + }); + } } }); return false; -- 2.25.1