Search ext: Expose DISTINCT modifier
authorColeman Watts <coleman@civicrm.org>
Thu, 24 Sep 2020 14:31:09 +0000 (10:31 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 24 Sep 2020 14:31:09 +0000 (10:31 -0400)
ext/search/ang/search.module.js
ext/search/ang/search/crmSearchFunction.component.js
ext/search/ang/search/crmSearchFunction.html

index ed60680e0020ac76b20a3c1de9ace9fe694fe492..cec26c5d6ea05093205516ad0c108f4741e63f1f 100644 (file)
         getEntity: getEntity,
         getField: getField,
         parseExpr: function(expr) {
-          var result = {},
+          var result = {fn: null, modifier: ''},
             fieldName = expr,
             bracketPos = expr.indexOf('(');
           if (bracketPos >= 0) {
-            fieldName = expr.match(/[A-Z( _]*([\w.:]+)/)[1];
+            var parsed = expr.substr(bracketPos).match(/[ ]?([A-Z]+[ ]+)?([\w.:]+)/);
+            fieldName = parsed[2];
             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(':'),
index 7a46aff4311f18a5a566a229cfd5b932931d58f2..9b7efce4b8379b33defb1fc61a8a5ce81001dfee 100644 (file)
         ctrl.path = fieldInfo.path + fieldInfo.suffix;
         ctrl.field = fieldInfo.field;
         ctrl.fn = !fieldInfo.fn ? '' : fieldInfo.fn.name;
+        ctrl.modifier = fieldInfo.modifier || null;
+        initFunction();
       };
 
+      function initFunction() {
+        ctrl.fnInfo = _.find(CRM.vars.search.functions, {name: ctrl.fn});
+        if (ctrl.fnInfo && _.includes(ctrl.fnInfo.params[0].prefix, 'DISTINCT')) {
+          ctrl.modifierAllowed = true;
+        }
+        else {
+          ctrl.modifierAllowed = false;
+          ctrl.modifier = null;
+        }
+      }
+
       this.selectFunction = function() {
-        ctrl.expr = ctrl.fn ? (ctrl.fn + '(' + ctrl.path + ')') : ctrl.path;
+        initFunction();
+        ctrl.writeExpr();
+      };
+
+      this.writeExpr = function() {
+        ctrl.expr = ctrl.fn ? (ctrl.fn + '(' + (ctrl.modifier ? ctrl.modifier + ' ' : '') + ctrl.path + ')') : ctrl.path;
       };
     }
   });
index e0692e33d3e4f420634adfaeec7b1fc1b9ed9d8c..03238b825c052b627cda6a58d671f382895e3106 100644 (file)
@@ -1,4 +1,8 @@
 <div class="form-inline">
   <label>{{ $ctrl.field.label }}:</label>
   <input class="form-control" style="width: 15em;" ng-model="$ctrl.fn" crm-ui-select="{data: $ctrl.functions, placeholder: ts('Select')}" ng-change="$ctrl.selectFunction()">
+  <label ng-if="$ctrl.modifierAllowed">
+    <input type="checkbox" ng-model="$ctrl.modifier" ng-change="$ctrl.writeExpr()" ng-true-value="'DISTINCT'" ng-false-value="null">
+    {{ ts('Distinct') }}
+  </label>
 </div>