Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search kit select current user #19620

Merged
merged 2 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ang/api4Explorer/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@
$el.removeClass('loading').crmSelect2({data: options, multiple: multi});
});
} else if (field.fk_entity) {
$el.crmEntityRef({entity: field.fk_entity, select:{multiple: multi}});
$el.crmEntityRef({entity: field.fk_entity, select:{multiple: multi}, static: field.fk_entity === 'Contact' ? ['user_contact_id'] : []});
} else if (dataType === 'Boolean') {
$el.attr('placeholder', ts('- select -')).crmSelect2({allowClear: false, multiple: multi, placeholder: ts('- select -'), data: [
{id: 'true', text: ts('Yes')},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
this.$onInit = function() {
var rendered = false;
ctrl.dateRanges = CRM.crmSearchActions.dateRanges;
ctrl.entity = ctrl.field.fk_entity || ctrl.field.entity;

this.ngModel.$render = function() {
ctrl.value = ctrl.ngModel.$viewValue;
Expand Down Expand Up @@ -97,7 +98,7 @@
return '~/crmSearchActions/crmSearchInput/select.html';
}

if (ctrl.field.fk_entity) {
if (ctrl.field.fk_entity || ctrl.field.name === 'id') {
return '~/crmSearchActions/crmSearchInput/entityRef.html';
}

Expand Down
4 changes: 2 additions & 2 deletions ext/search/ang/crmSearchActions/crmSearchInput/entityRef.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="form-group" ng-if="!$ctrl.multi">
<input class="form-control" ng-model="$ctrl.value" crm-entityref="{entity: $ctrl.field.fk_entity}">
<input class="form-control" ng-model="$ctrl.value" crm-entityref="{entity: $ctrl.entity, static: $ctrl.entity === 'Contact' ? ['user_contact_id'] : []}">
</div>
<div class="form-group" ng-if="$ctrl.multi">
<input class="form-control" ng-model="$ctrl.value" crm-entityref="{entity: $ctrl.field.fk_entity, select: {multiple: true}}" ng-list >
<input class="form-control" ng-model="$ctrl.value" crm-entityref="{entity: $ctrl.entity, select: {multiple: true}, static: $ctrl.entity === 'Contact' ? ['user_contact_id'] : []}" ng-list >
</div>
65 changes: 56 additions & 9 deletions js/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,14 @@ if (!CRM.vars) CRM.vars = {};
var
$el = $(this).off('.crmEntity'),
entity = options.entity || $el.data('api-entity') || 'Contact',
selectParams = {};
selectParams = {},
staticPresets = {
user_contact_id: {
id: 'user_contact_id',
label: ts('Select Current User'),
icon: 'fa-user-circle-o'
}
};
// Legacy: fix entity name if passed in as snake case
if (entity.charAt(0).toUpperCase() !== entity.charAt(0)) {
entity = _.capitalize(_.camelCase(entity));
Expand All @@ -510,6 +517,27 @@ if (!CRM.vars) CRM.vars = {};
$el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select));
$el.data('api-params', $.extend(true, {}, $el.data('api-params') || {}, options.api));
$el.data('create-links', options.create || $el.data('create-links'));
var staticItems = options.static || $el.data('static') || [];
_.each(staticItems, function(option, i) {
if (_.isString(option)) {
staticItems[i] = staticPresets[option];
}
});

function staticItemMarkup() {
if (!staticItems.length) {
return '';
}
var markup = '<div class="crm-entityref-links crm-entityref-links-static">';
_.each(staticItems, function(link) {
markup += ' <a class="crm-hover-button" href="#' + link.id + '">' +
'<i class="crm-i ' + link.icon + '" aria-hidden="true"></i> ' +
_.escape(link.label) + '</a>';
});
markup += '</div>';
return markup;
}

$el.addClass('crm-form-entityref crm-' + _.kebabCase(entity) + '-ref');
var settings = {
// Use select2 ajax helper instead of CRM.api3 because it provides more value
Expand Down Expand Up @@ -538,17 +566,21 @@ if (!CRM.vars) CRM.vars = {};
var
multiple = !!$el.data('select-params').multiple,
val = $el.val(),
stored = $el.data('entity-value') || [];
stored = ($el.data('entity-value') || []).concat(staticItems);
if (val === '') {
return;
}
var idsNeeded = _.difference(val.split(','), _.pluck(stored, 'id'));
var existing = _.remove(stored, function(item) {
return _.includes(val.split(','), item.id);
});
// If we already have this data, just return it
if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) {
callback(multiple ? stored : stored[0]);
if (!idsNeeded.length) {
callback(multiple ? existing : existing[0]);
} else {
var params = $.extend({}, $el.data('api-params') || {}, {id: val});
var params = $.extend({}, $el.data('api-params') || {}, {id: idsNeeded.join(',')});
CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) {
callback(multiple ? result.values : result.values[0]);
callback(multiple ? result.values.concat(existing) : result.values[0]);
// Trigger change (store data to avoid an infinite loop of lookups)
$el.data('entity-value', result.values).trigger('change');
});
Expand Down Expand Up @@ -589,7 +621,7 @@ if (!CRM.vars) CRM.vars = {};
else {
selectParams.formatInputTooShort = function() {
var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort.call(this);
txt += entityRefFiltersMarkup($el) + renderEntityRefCreateLinks($el);
txt += entityRefFiltersMarkup($el) + staticItemMarkup() + renderEntityRefCreateLinks($el);
return txt;
};
selectParams.formatNoMatches = function() {
Expand Down Expand Up @@ -624,6 +656,21 @@ if (!CRM.vars) CRM.vars = {};
});
return false;
})
.on('click.crmEntity', '.crm-entityref-links-static a', function(e) {
var id = $(this).attr('href').substr(1),
item = _.findWhere(staticItems, {id: id});
$el.select2('close');
if ($el.select2('container').hasClass('select2-container-multi')) {
var selection = $el.select2('data');
if (!_.findWhere(selection, {id: id})) {
selection.push(item);
$el.select2('data', selection, true);
}
} else {
$el.select2('data', item, true);
}
return false;
})
.on('change.crmEntity', '.crm-entityref-filter-value', function() {
var filter = $el.data('user-filter') || {};
filter.value = $(this).val();
Expand All @@ -634,7 +681,7 @@ if (!CRM.vars) CRM.vars = {};
$el.select2('close');
$el.select2('open');
} else {
$('.crm-entityref-links', '#select2-drop').replaceWith(renderEntityRefCreateLinks($el));
$('.crm-entityref-links-create', '#select2-drop').replaceWith(renderEntityRefCreateLinks($el));
}
})
.on('change.crmEntity', 'select.crm-entityref-filter-key', function() {
Expand Down Expand Up @@ -714,7 +761,7 @@ if (!CRM.vars) CRM.vars = {};
createLinks = $el.data('create-links'),
params = getEntityRefApiParams($el).params,
entity = $el.data('api-entity'),
markup = '<div class="crm-entityref-links">';
markup = '<div class="crm-entityref-links crm-entityref-links-create">';
if (!createLinks || (createLinks === true && !CRM.config.entityRef.links[entity])) {
return '';
}
Expand Down