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 ext: misc cleanup & fixes #18723

Merged
merged 2 commits into from
Oct 9, 2020
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
5 changes: 4 additions & 1 deletion Civi/Api4/Action/Entity/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Civi\Api4\Action\Entity;

use Civi\Api4\CustomGroup;
use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;

/**
* Get the names & docblocks of all APIv4 entities.
Expand Down Expand Up @@ -91,10 +92,12 @@ private function addCustomEntities(&$entities) {
->execute();
foreach ($customEntities as $customEntity) {
$fieldName = 'Custom_' . $customEntity['name'];
$baseEntity = '\Civi\Api4\\' . CustomGroupJoinable::getEntityFromExtends($customEntity['extends']);
$entities[$fieldName] = [
'name' => $fieldName,
'title' => $customEntity['title'],
'description' => 'Custom group - extends ' . $customEntity['extends'],
'titlePlural' => $customEntity['title'],
'description' => ts('Custom group for %1', [1 => $baseEntity::getInfo()['titlePlural']]),
'see' => [
'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets',
'\\Civi\\Api4\\CustomGroup',
Expand Down
2 changes: 0 additions & 2 deletions ext/search/CRM/Search/Page/Ang.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ private function getSchema() {
if ($loadOptions) {
$entity['optionsLoaded'] = TRUE;
}
// Because multivalue custom pseudo-entities don't have titlePlural
$entity['titlePlural'] = $entity['titlePlural'] ?? $entity['title'];
$entity['fields'] = civicrm_api4($entity['name'], 'getFields', [
'select' => $getFields,
'where' => [['permission', 'IS NULL']],
Expand Down
28 changes: 14 additions & 14 deletions ext/search/ang/search.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,29 @@
.factory('searchMeta', function() {
function getEntity(entityName) {
if (entityName) {
entityName = entityName === true ? searchEntity : entityName;
return _.find(CRM.vars.search.schema, {name: entityName});
}
}
function getField(name) {
var dotSplit = name.split('.'),
function getField(fieldName, entityName) {
var dotSplit = fieldName.split('.'),
joinEntity = dotSplit.length > 1 ? dotSplit[0] : null,
fieldName = _.last(dotSplit).split(':')[0],
entityName = searchEntity;
name = _.last(dotSplit).split(':')[0];
// Custom fields contain a dot in their fieldname
// If 3 segments, the first is the joinEntity and the last 2 are the custom field
if (dotSplit.length === 3) {
fieldName = dotSplit[1] + '.' + fieldName;
name = dotSplit[1] + '.' + name;
}
// If 2 segments, it's ambiguous whether this is a custom field or joined field. Search the main entity first.
if (dotSplit.length === 2) {
var field = _.find(getEntity(true).fields, {name: dotSplit[0] + '.' + fieldName});
var field = _.find(getEntity(entityName).fields, {name: dotSplit[0] + '.' + name});
if (field) {
return field;
}
}
if (joinEntity) {
entityName = _.find(CRM.vars.search.links[entityName], {alias: joinEntity}).entity;
}
return _.find(getEntity(entityName).fields, {name: fieldName});
return _.find(getEntity(entityName).fields, {name: name});
}
return {
getEntity: getEntity,
Expand All @@ -120,12 +118,14 @@
result.fn = _.find(CRM.vars.search.functions, {name: expr.substring(0, bracketPos)});
result.modifier = _.trim(parsed[1]);
}
result.field = getField(fieldName);
var split = fieldName.split(':'),
prefixPos = split[0].lastIndexOf(result.field.name);
result.path = split[0];
result.prefix = prefixPos > 0 ? result.path.substring(0, prefixPos) : '';
result.suffix = !split[1] ? '' : ':' + split[1];
result.field = expr ? getField(fieldName, searchEntity) : undefined;
if (result.field) {
var split = fieldName.split(':'),
prefixPos = split[0].lastIndexOf(result.field.name);
result.path = split[0];
result.prefix = prefixPos > 0 ? result.path.substring(0, prefixPos) : '';
result.suffix = !split[1] ? '' : ':' + split[1];
}
return result;
}
};
Expand Down
13 changes: 4 additions & 9 deletions ext/search/ang/search/crmSearch.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
this.page = 1;
this.params = {};
// After a search this.results is an object of result arrays keyed by page,
// Prior to searching it's an empty string because 1: falsey and 2: doesn't throw an error if you try to access undefined properties
// Initially this.results is an empty string because 1: it's falsey (unlike an empty object) and 2: it doesn't throw an error if you try to access undefined properties (unlike null)
this.results = '';
this.rowCount = false;
// Have the filters (WHERE, HAVING, GROUP BY, JOIN) changed?
Expand Down Expand Up @@ -381,13 +381,6 @@
return value;
}

function getOption(field, value) {
return _.find(field.options, function(option) {
// Type coersion is intentional
return option.id == value;
});
}

$scope.fieldsForGroupBy = function() {
return {results: getAllFields('', function(key) {
return _.contains(ctrl.params.groupBy, key);
Expand All @@ -413,7 +406,9 @@
};

function getDefaultSelect() {
return _.filter(['id', 'display_name', 'label', 'title', 'location_type_id:label'], searchMeta.getField);
return _.filter(['id', 'display_name', 'label', 'title', 'location_type_id:label'], function(field) {
return !!searchMeta.getField(field, ctrl.entity);
});
}

function getAllFields(suffix, disabledIf) {
Expand Down
4 changes: 2 additions & 2 deletions ext/search/ang/search/crmSearch/controls.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
{{:: ts('Auto') }}
</button>
</div>
<crm-search-actions entity="$ctrl.entity" ids="$ctrl.selectedRows"></crm-search-actions>
<crm-search-actions entity="$ctrl.entity" ids="$ctrl.selectedRows" refresh="$ctrl.refreshPage()"></crm-search-actions>
<div class="btn-group pull-right">
<button type="button" class="btn form-control dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<button type="button" class="btn btn-default form-control dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="crm-i fa-save"></i> {{:: ts('Create')}}
<span class="caret"></span>
</button>
Expand Down
8 changes: 3 additions & 5 deletions ext/search/ang/search/crmSearchActions.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
angular.module('search').component('crmSearchActions', {
bindings: {
entity: '<',
refresh: '&',
ids: '<'
},
require: {
search: '^crmSearch'
},
templateUrl: '~/search/crmSearchActions.html',
controller: function($scope, crmApi4, dialogService, searchMeta) {
var ts = $scope.ts = CRM.ts(),
Expand Down Expand Up @@ -44,7 +42,7 @@
var path = $scope.$eval(action.crmPopup.path, data),
query = action.crmPopup.query && $scope.$eval(action.crmPopup.query, data);
CRM.loadForm(CRM.url(path, query))
.on('crmFormSuccess', ctrl.search.refreshPage);
.on('crmFormSuccess', ctrl.refresh);
}
// If action uses dialogService
else if (action.uiDialog) {
Expand All @@ -53,7 +51,7 @@
title: action.title
});
dialogService.open('crmSearchAction', action.uiDialog.templateUrl, data, options)
.then(ctrl.search.refreshPage);
.then(ctrl.refresh);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion ext/search/ang/search/crmSearchActions.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="btn-group" title="{{:: ts('Perform action on selected items.') }}">
<button type="button" ng-disabled="!$ctrl.ids.length" ng-click="$ctrl.init()" class="btn form-control dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<button type="button" ng-disabled="!$ctrl.ids.length" ng-click="$ctrl.init()" class="btn form-control dropdown-toggle btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{:: ts('Action') }} <span class="caret"></span>
</button>
<ul class="dropdown-menu" ng-if=":: $ctrl.actions">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

this.availableFields = function() {
var results = _.transform(ctrl.entity.fields, function(result, item) {
var formatted = {id: item.name, text: item.title, description: item.description};
var formatted = {id: item.name, text: item.label, description: item.description};
if (fieldInUse(item.name)) {
formatted.disabled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<hr />
<div class="buttons pull-right">
<button type="button" ng-click="$ctrl.cancel()" class="btn btn-danger">{{:: ts('Cancel') }}</button>
<button ng-click="$ctrl.save()" class="btn btn-primary" ng-disabled="!$ctrl.values.length">{{:: ts('Update %1 %2', {1: model.ids.length, 2: $ctrl.entity.title}) }}</button>
<button ng-click="$ctrl.save()" class="btn btn-primary" ng-disabled="!$ctrl.values.length">{{:: ts('Update %1 %2', {1: model.ids.length, 2: (model.ids.length === 1 ? $ctrl.entity.title : $ctrl.entity.titlePlural)}) }}</button>
</div>
</div>
</div>