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(':'),
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;
};
}
});
<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>