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

dev/report#31 - Api4 Explorer: Support SQL functions and HAVING clause #17047

Merged
merged 7 commits into from
Apr 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
22 changes: 22 additions & 0 deletions CRM/Api4/Page/Api4Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function run() {
'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(),
'links' => (array) \Civi\Api4\Entity::getLinks()->execute(),
'docs' => \Civi\Api4\Utils\ReflectionUtils::parseDocBlock($apiDoc->getDocComment()),
'functions' => self::getSqlFunctions(),
'groupOptions' => array_column((array) $groupOptions, 'options', 'name'),
];
Civi::resources()
Expand All @@ -49,4 +50,25 @@ public function run() {
parent::run();
}

/**
* Gets info about all available sql functions
* @return array
*/
public static function getSqlFunctions() {
$fns = [];
foreach (glob(Civi::paths()->getPath('[civicrm.root]/Civi/Api4/Query/SqlFunction*.php')) as $file) {
$matches = [];
if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) {
$className = '\Civi\Api4\Query\\' . $matches[1];
if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) {
$fns[] = [
'name' => $className::getName(),
'params' => $className::getParams(),
];
}
}
}
return $fns;
}

}
6 changes: 3 additions & 3 deletions Civi/Api4/Generic/AbstractQueryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ abstract class AbstractQueryAction extends AbstractAction {
/**
* Maximum number of $ENTITIES to return.
*
* Defaults to unlimited.
* Defaults to `0` - unlimited.
*
* Note: the Api Explorer sets this to 25 by default to avoid timeouts.
* Note: the Api Explorer sets this to `25` by default to avoid timeouts.
* Change or remove this default for your application code.
*
* @var int
Expand All @@ -74,7 +74,7 @@ abstract class AbstractQueryAction extends AbstractAction {
/**
* Zero-based index of first $ENTITY to return.
*
* Defaults to "0" - first $ENTITY found.
* Defaults to `0` - first $ENTITY found.
*
* @var int
*/
Expand Down
7 changes: 3 additions & 4 deletions Civi/Api4/Query/SqlFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ public function getAlias(): string {
* Get the name of this sql function.
* @return string
*/
public function getName(): string {
$className = get_class($this);
$pos = strrpos($className, 'SqlFunction');
return substr($className, $pos + 11);
public static function getName(): string {
$className = static::class;
return substr($className, strrpos($className, 'SqlFunction') + 11);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<i class="crm-i fa-trash"></i>
</button>
</div>
<div class="api4-where-group-sortable" ng-model="data.where" ui-sortable="{axis: 'y', connectWith: '.api4-where-group-sortable', containment: '.api4-where-fieldset', over: onSortOver, start: onSort, stop: onSort}">
<div class="api4-input form-inline clearfix" ng-repeat="(index, clause) in data.where">
<div class="api4-clause-group-sortable" ng-model="data.clauses" ui-sortable="{axis: 'y', connectWith: '.api4-clause-group-sortable', containment: '.api4-clause-fieldset', over: onSortOver}" ui-sortable-start="onSort" ui-sortable-stop="onSort">
<div class="api4-input form-inline clearfix" ng-repeat="(index, clause) in data.clauses">
<div class="api4-clause-badge" title="{{ ts('Drag to reposition') }}">
<span class="badge badge-info">
<span ng-if="!index && !data.groupParent">Where</span>
<span ng-if="!index && !data.groupParent">{{ data.type }}</span>
<span ng-if="index || data.groupParent">{{ data.op }}</span>
<i class="crm-i fa-arrows"></i>
</span>
Expand All @@ -18,7 +18,7 @@
<select class="form-control api4-operator" ng-model="clause[1]" ng-options="o for o in operators" ></select>
<input class="form-control" ng-model="clause[2]" api4-exp-value="{field: clause[0], op: clause[1]}" />
</div>
<fieldset class="clearfix" ng-if="clause[0] === 'AND' || clause[0] === 'OR' || clause[0] === 'NOT'" crm-api4-where-clause="{where: clause[1], op: clause[0], fields: data.fields, operators: data.operators, groupParent: data.where, groupIndex: index}">
<fieldset class="clearfix" ng-if="clause[0] === 'AND' || clause[0] === 'OR' || clause[0] === 'NOT'" crm-api4-clause="{type: data.type, clauses: clause[1], op: clause[0], fields: data.fields, groupParent: data.clauses, groupIndex: index}">
</fieldset>
</div>
</div>
Expand Down
64 changes: 45 additions & 19 deletions ang/api4Explorer/Explorer.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,19 @@ <h1 crm-page-title>
</label>
<a href class="crm-hover-button" title="Clear" ng-click="clearParam(name)" ng-show="params[name] !== null"><i class="crm-i fa-times"></i></a>
</div>
<div class="api4-input form-inline" ng-mouseenter="help('select', availableParams.select)" ng-mouseleave="help()" ng-if="availableParams.select && !isSelectRowCount()">
<label for="api4-param-select">select<span class="crm-marker" ng-if="availableParams.select.required"> *</span></label>
<input class="collapsible-optgroups form-control" ng-list crm-ui-select="{data: selectFieldsAndJoins, multiple: true}" placeholder="*" id="api4-param-select" ng-model="params.select" style="width: 85%;"/>
</div>
<fieldset class="api4-input form-inline" ng-mouseenter="help('select', availableParams.select)" ng-mouseleave="help()" ng-if="availableParams.select && !isSelectRowCount()">
<legend>select<span class="crm-marker" ng-if="availableParams.select.required"> *</span></legend>
<div ng-model="params.select" ui-sortable="{axis: 'y'}">
<div class="api4-input form-inline" ng-repeat="item in params.select track by $index">
<i class="crm-i fa-arrows"></i>
<input class="form-control huge" type="text" ng-model="params.select[$index]" />
<a href class="crm-hover-button" title="Clear" ng-click="clearParam('select', $index)"><i class="crm-i fa-times"></i></a>
</div>
</div>
<div class="api4-input form-inline">
<input class="collapsible-optgroups form-control huge" ng-model="controls.select" crm-ui-select="{data: fieldsAndJoinsAndFunctionsAndWildcards}" placeholder="Add select" />
</div>
</fieldset>
<div class="api4-input form-inline" ng-mouseenter="help('fields', availableParams.fields)" ng-mouseleave="help()"ng-if="availableParams.fields">
<label for="api4-param-fields">fields<span class="crm-marker" ng-if="availableParams.fields.required"> *</span></label>
<input class="form-control" ng-list crm-ui-select="{data: fields, multiple: true}" id="api4-param-fields" ng-model="params.fields" style="width: 85%;"/>
Expand All @@ -73,7 +82,7 @@ <h1 crm-page-title>
<textarea class="form-control" type="{{ param.type[0] === 'int' && param.type.length === 1 ? 'number' : 'text' }}" id="api4-param-{{ name }}" ng-model="params[name]">
</textarea>
</div>
<fieldset ng-if="availableParams.where" class="api4-where-fieldset" ng-mouseenter="help('where', availableParams.where)" ng-mouseleave="help()" crm-api4-where-clause="{where: params.where, required: availableParams.where.required, op: 'AND', label: 'where', fields: fieldsAndJoins}">
<fieldset ng-if="availableParams.where" class="api4-clause-fieldset" ng-mouseenter="help('where', availableParams.where)" ng-mouseleave="help()" crm-api4-clause="{type: 'where', clauses: params.where, required: availableParams.where.required, op: 'AND', label: 'where', fields: fieldsAndJoins}">
</fieldset>
<fieldset ng-repeat="name in ['values', 'defaults']" ng-if="availableParams[name]" ng-mouseenter="help(name, availableParams[name])" ng-mouseleave="help()">
<legend>{{ name }}<span class="crm-marker" ng-if="availableParams[name].required"> *</span></legend>
Expand All @@ -84,35 +93,52 @@ <h1 crm-page-title>
<div class="api4-input form-inline">
<input class="collapsible-optgroups form-control" ng-model="controls[name]" crm-ui-select="{formatResult: formatSelect2Item, formatSelection: formatSelect2Item, data: fieldList(name), placeholder: ts('Add %1', {1: name.slice(0, -1)})}"/>
</div>
</fieldset><fieldset ng-if="availableParams.groupBy" ng-mouseenter="help('groupBy', availableParams.groupBy)" ng-mouseleave="help()">
<legend>groupBy<span class="crm-marker" ng-if="availableParams.groupBy.required"> *</span></legend>
<div ng-model="params.groupBy" ui-sortable="{axis: 'y'}">
<div class="api4-input form-inline" ng-repeat="(pos, field) in params.groupBy">
<i class="crm-i fa-arrows"></i>
<input class="collapsible-optgroups form-control" ng-model="params.groupBy[pos]" crm-ui-select="{data: fieldsAndJoins, allowClear: true, placeholder: 'Field'}" />
</fieldset>
<fieldset ng-if="availableParams.groupBy" ng-mouseenter="help('groupBy', availableParams.groupBy)" ng-mouseleave="help()">
<legend>groupBy<span class="crm-marker" ng-if="availableParams.groupBy.required"> *</span></legend>
<div ng-model="params.groupBy" ui-sortable="{axis: 'y'}">
<div class="api4-input form-inline" ng-repeat="item in params.groupBy track by $index">
<i class="crm-i fa-arrows"></i>
<input class="form-control huge" type="text" ng-model="params.groupBy[$index]" />
<a href class="crm-hover-button" title="Clear" ng-click="clearParam('groupBy', $index)"><i class="crm-i fa-times"></i></a>
</div>
</div>
</div>
<div class="api4-input form-inline">
<input class="collapsible-optgroups form-control" ng-model="controls.groupBy" crm-ui-select="{data: fieldsAndJoins}" placeholder="Add groupBy" />
</div>
</fieldset>
<div class="api4-input form-inline">
<input class="collapsible-optgroups form-control huge" ng-model="controls.groupBy" crm-ui-select="{data: fieldsAndJoinsAndFunctions}" placeholder="Add groupBy" />
</div>
</fieldset>
<fieldset ng-if="availableParams.having" class="api4-clause-fieldset" ng-mouseenter="help('having', availableParams.having)" ng-mouseleave="help()" crm-api4-clause="{type: 'having', clauses: params.having, required: availableParams.having.required, op: 'AND', label: 'having', fields: havingOptions}">
</fieldset>
<fieldset ng-if="availableParams.orderBy" ng-mouseenter="help('orderBy', availableParams.orderBy)" ng-mouseleave="help()">
<legend>orderBy<span class="crm-marker" ng-if="availableParams.orderBy.required"> *</span></legend>
<div ng-model="params.orderBy" ui-sortable="{axis: 'y'}">
<div class="api4-input form-inline" ng-repeat="clause in params.orderBy">
<i class="crm-i fa-arrows"></i>
<input class="collapsible-optgroups form-control" ng-model="clause[0]" crm-ui-select="{data: fieldsAndJoins, allowClear: true, placeholder: 'Field'}" />
<input class="form-control huge" type="text" ng-model="clause[0]" />
<select class="form-control" ng-model="clause[1]">
<option value="ASC">ASC</option>
<option value="DESC">DESC</option>
</select>
<a href class="crm-hover-button" title="Clear" ng-click="clearParam('orderBy', $index)"><i class="crm-i fa-times"></i></a>
</div>
</div>
<div class="api4-input form-inline">
<input class="collapsible-optgroups form-control" ng-model="controls.orderBy" crm-ui-select="{data: fieldsAndJoins}" placeholder="Add orderBy" />
<input class="collapsible-optgroups form-control huge" ng-model="controls.orderBy" crm-ui-select="{data: fieldsAndJoinsAndFunctions}" placeholder="Add orderBy" />
</div>
</fieldset>
<fieldset ng-if="availableParams.limit && availableParams.offset">
<div class="api4-input form-inline">
<span ng-mouseenter="help('limit', availableParams.limit)" ng-mouseleave="help()">
<label for="api4-param-limit">limit<span class="crm-marker" ng-if="availableParams.limit.required"> *</span></label>
<input class="form-control" type="number" min="0" id="api4-param-limit" ng-model="params.limit"/>
</span>
<span ng-mouseenter="help('offset', availableParams.offset)" ng-mouseleave="help()">
<label for="api4-param-offset">offset<span class="crm-marker" ng-if="availableParams.offset.required"> *</span></label>
<input class="form-control" type="number" min="0" id="api4-param-offset" ng-model="params.offset"/>
</span>
<a href class="crm-hover-button" title="Clear" ng-click="clearParam('limit');clearParam('offset');" ng-show="!!params.limit || !!params.offset"><i class="crm-i fa-times"></i></a>
</div>
</fieldset>

<fieldset ng-if="availableParams.chain" ng-mouseenter="help('chain', availableParams.chain)" ng-mouseleave="help()">
<legend>chain</legend>
<div class="api4-input form-inline" ng-repeat="clause in params.chain" api4-exp-chain="clause" entities="entities" main-entity="entity" >
Expand Down
Loading