SearchKit - Improve link titles to match entity titles
authorColeman Watts <coleman@civicrm.org>
Sat, 24 Apr 2021 22:27:11 +0000 (18:27 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 24 Apr 2021 22:27:18 +0000 (18:27 -0400)
ext/search_kit/Civi/Search/Admin.php
ext/search_kit/ang/crmSearchAdmin/crmSearchAdminDisplay.component.js
ext/search_kit/ang/crmSearchAdmin/crmSearchAdminLinkGroup.component.js
ext/search_kit/ang/crmSearchAdmin/crmSearchAdminLinkGroup.html

index 99fd42f74c89f9e4330341d2f35c6feaf1373dfb..ac5d8fa916784a958c55daf26330affaffed8a2f 100644 (file)
@@ -93,27 +93,12 @@ class Admin {
         // 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'],
index 83272f795da32338d98b19ef9fda71e5eba3260e..f754d34cb6fa7520e5801badf554ed4f2e7d52a8 100644 (file)
 
       // 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);
                 });
               }
index 2768a4bcfce3cedf6613e7b0100dd2c2588cb621..f6e47996596e2d909ad8c3a3e1f7a716c71b58ef 100644 (file)
         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;
index 87a236ce7fff912c60fd54d2db04cab11c2c8b61..db0b228a913198a37fc4ec7b9d489c989b902abf 100644 (file)
@@ -31,7 +31,7 @@
         <input type="text" class="form-control" ng-model="item.text">
       </td>
       <td class="form-inline">
-        <crm-search-admin-link-select api-entity="$ctrl.apiEntity" api-params="$ctrl.apiParams" link="item" links="$ctrl.links"></crm-search-admin-link-select>
+        <crm-search-admin-link-select api-entity="$ctrl.apiEntity" api-params="$ctrl.apiParams" link="item" links="$ctrl.links" on-change="$ctrl.onChangeLink(item, before, after)"></crm-search-admin-link-select>
       </td>
       <td>
         <div class="btn-group">