// Add paths (but only RUD actions) with translated titles
foreach ($entity['paths'] as $action => $path) {
unset($entity['paths'][$action]);
- switch ($action) {
- case 'view':
- $title = E::ts('View %1', [1 => $entity['title']]);
- break;
-
- case 'update':
- $title = E::ts('Edit %1', [1 => $entity['title']]);
- break;
-
- case 'delete':
- $title = E::ts('Delete %1', [1 => $entity['title']]);
- break;
-
- default:
- continue 2;
+ if (in_array($action, ['view', 'update', 'delete'], TRUE)) {
+ $entity['paths'][] = [
+ 'path' => $path,
+ 'action' => $action,
+ ];
}
- $entity['paths'][] = [
- 'path' => $path,
- 'title' => $title,
- 'action' => $action,
- ];
}
$getFields = civicrm_api4($entity['name'], 'getFields', [
'select' => ['name', 'title', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize', 'entity', 'fk_entity', 'readonly'],
// Build a list of all possible links to main entity or join entities
function buildLinks() {
+ function addTitle(link, entityName) {
+ switch (link.action) {
+ case 'view':
+ link.title = ts('View %1', {1: entityName});
+ break;
+
+ case 'update':
+ link.title = ts('Edit %1', {1: entityName});
+ break;
+
+ case 'delete':
+ link.title = ts('Delete %1', {1: entityName});
+ break;
+ }
+ }
+
// Links to main entity
- var links = _.cloneDeep(searchMeta.getEntity(ctrl.savedSearch.api_entity).paths || []),
- entityCount = {};
- entityCount[ctrl.savedSearch.api_entity] = 1;
+ var mainEntity = searchMeta.getEntity(ctrl.savedSearch.api_entity),
+ links = _.cloneDeep(mainEntity.paths || []);
+ _.each(links, function(link) {
+ addTitle(link, mainEntity.title);
+ });
// Links to explicitly joined entities
- _.each(ctrl.savedSearch.api_params.join, function(join) {
- var joinName = join[0].split(' AS '),
- joinEntity = searchMeta.getEntity(joinName[0]);
- entityCount[joinEntity.name] = (entityCount[joinEntity.name] || 0) + 1;
+ _.each(ctrl.savedSearch.api_params.join, function(joinClause) {
+ var join = searchMeta.getJoin(joinClause[0]),
+ joinEntity = searchMeta.getEntity(join.entity);
_.each(joinEntity.paths, function(path) {
var link = _.cloneDeep(path);
- link.path = link.path.replace(/\[/g, '[' + joinName[1] + '.');
- if (entityCount[joinEntity.name] > 1) {
- link.title += ' ' + entityCount[joinEntity.name];
- }
- link.join = joinName[1];
+ link.path = link.path.replace(/\[/g, '[' + join.alias + '.');
+ link.join = join.alias;
+ addTitle(link, join.label);
links.push(link);
});
});
if (!_.includes(fieldName, ' AS ')) {
var info = searchMeta.parseExpr(fieldName);
if (info.field && !info.suffix && !info.fn && (info.field.fk_entity || info.field.name !== info.field.fieldName)) {
- var idField = info.field.fk_entity ? fieldName : fieldName.substr(0, fieldName.lastIndexOf('.'));
- if (!ctrl.crmSearchAdmin.canAggregate(idField)) {
- var joinEntity = searchMeta.getEntity(info.field.fk_entity || info.field.entity);
+ var idFieldName = info.field.fk_entity ? fieldName : fieldName.substr(0, fieldName.lastIndexOf('.')),
+ idField = searchMeta.parseExpr(idFieldName).field;
+ if (!ctrl.crmSearchAdmin.canAggregate(idFieldName)) {
+ var joinEntity = searchMeta.getEntity(idField.fk_entity),
+ label = (idField.join ? idField.join.label + ': ' : '') + (idField.input_attrs && idField.input_attrs.label || idField.label);
_.each((joinEntity || {}).paths, function(path) {
var link = _.cloneDeep(path);
- link.path = link.path.replace(/\[id/g, '[' + idField);
- link.join = idField;
+ link.path = link.path.replace(/\[id/g, '[' + idFieldName);
+ link.join = idFieldName;
+ addTitle(link, label);
links.push(link);
});
}
ctrl.group.push({
path: path,
style: link && defaultStyles[link.action] || 'default',
- text: link ? link.title : '',
+ text: link ? link.title : ts('Link'),
icon: link && defaultIcons[link.action] || 'fa-external-link'
});
};
+ this.onChangeLink = function(item, before, after) {
+ var beforeLink = before && ctrl.getLink(before),
+ beforeTitle = beforeLink ? beforeLink.title : ts('Link'),
+ afterLink = after && ctrl.getLink(after);
+ if (afterLink && (!item.text || beforeTitle === item.text)) {
+ item.text = afterLink.title;
+ }
+ };
+
this.$onInit = function() {
var defaultLinks = _.filter(ctrl.links, function(link) {
return !link.join;