3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 use Civi\API\Exception\UnauthorizedException
;
32 * Query builder for civicrm_api_basic_get.
34 * Fetches an entity based on specified params for the "where" clause,
35 * return properties for the "select" clause,
36 * as well as limit and order.
38 * Automatically joins on custom fields to return or filter by them.
40 * Supports an additional sql fragment which the calling api can provide.
44 abstract class SelectQuery
{
48 MAIN_TABLE_ALIAS
= 'a';
62 protected $selectFields = [];
66 public $isFillUniqueFields = FALSE;
68 * @var \CRM_Utils_SQL_Select
74 protected $joins = [];
78 protected $apiFieldSpec;
82 protected $entityFieldNames;
86 protected $aclFields = [];
90 protected $checkPermissions;
92 protected $apiVersion;
95 * @param string $entity
96 * @param bool $checkPermissions
98 public function __construct($entity, $checkPermissions) {
99 $this->entity
= $entity;
100 require_once 'api/v3/utils.php';
101 $baoName = _civicrm_api3_get_BAO($entity);
102 $bao = new $baoName();
104 $this->entityFieldNames
= _civicrm_api3_field_names(_civicrm_api3_build_fields_array($bao));
105 $this->apiFieldSpec
= $this->getFields();
107 $this->query
= \CRM_Utils_SQL_Select
::from($bao->tableName() . ' ' . self
::MAIN_TABLE_ALIAS
);
109 // Add ACLs first to avoid redundant subclauses
110 $this->checkPermissions
= $checkPermissions;
111 $this->query
->where($this->getAclClause(self
::MAIN_TABLE_ALIAS
, $baoName));
115 * Build & execute the query and return results array
118 * @throws \API_Exception
119 * @throws \CRM_Core_Exception
122 public function run() {
123 $this->buildSelectFields();
125 $this->buildWhereClause();
126 if (in_array('count_rows', $this->select
)) {
127 $this->query
->select("count(*) as c");
130 foreach ($this->selectFields
as $column => $alias) {
131 $this->query
->select("$column as `$alias`");
134 $this->buildOrderBy();
138 if (!empty($this->limit
) ||
!empty($this->offset
)) {
139 $this->query
->limit($this->limit
, $this->offset
);
142 $result_entities = [];
143 $result_dao = \CRM_Core_DAO
::executeQuery($this->query
->toSQL());
145 while ($result_dao->fetch()) {
146 if (in_array('count_rows', $this->select
)) {
147 return (int) $result_dao->c
;
149 $result_entities[$result_dao->id
] = [];
150 foreach ($this->selectFields
as $column => $alias) {
151 $returnName = $alias;
152 $alias = str_replace('.', '_', $alias);
153 if (property_exists($result_dao, $alias) && $result_dao->$alias != NULL) {
154 $result_entities[$result_dao->id
][$returnName] = $result_dao->$alias;
156 // Backward compatibility on fields names.
157 if ($this->isFillUniqueFields
&& !empty($this->apiFieldSpec
[$alias]['uniqueName'])) {
158 $result_entities[$result_dao->id
][$this->apiFieldSpec
[$alias]['uniqueName']] = $result_dao->$alias;
160 foreach ($this->apiFieldSpec
as $returnName => $spec) {
161 if (empty($result_entities[$result_dao->id
][$returnName]) && !empty($result_entities[$result_dao->id
][$spec['name']])) {
162 $result_entities[$result_dao->id
][$returnName] = $result_entities[$result_dao->id
][$spec['name']];
167 return $result_entities;
171 * @param \CRM_Utils_SQL_Select $sqlFragment
172 * @return SelectQuery
174 public function merge($sqlFragment) {
175 $this->query
->merge($sqlFragment);
180 * Joins onto an fk field
182 * Adds one or more joins to the query to make this field available for use in a clause.
184 * Enforces permissions at the api level and by appending the acl clause for that entity to the join.
186 * @param $fkFieldName
190 * Returns the table and field name for adding this field to a SELECT or WHERE clause
191 * @throws \API_Exception
192 * @throws \Civi\API\Exception\UnauthorizedException
194 protected function addFkField($fkFieldName, $side) {
195 $stack = explode('.', $fkFieldName);
196 if (count($stack) < 2) {
199 $prev = self
::MAIN_TABLE_ALIAS
;
200 foreach ($stack as $depth => $fieldName) {
201 // Setup variables then skip the first level
204 // We only join on core fields
205 // @TODO: Custom contact ref fields could be supported too
206 if (!in_array($fk, $this->entityFieldNames
)) {
209 $fkField = &$this->apiFieldSpec
[$fk];
212 // More than 4 joins deep seems excessive - DOS attack?
213 if ($depth > self
::MAX_JOINS
) {
214 throw new UnauthorizedException("Maximum number of joins exceeded in parameter $fkFieldName");
216 $subStack = array_slice($stack, 0, $depth);
217 $this->getJoinInfo($fkField, $subStack);
218 if (!isset($fkField['FKApiName']) ||
!isset($fkField['FKClassName'])) {
219 // Join doesn't exist - might be another param with a dot in it for some reason, we'll just ignore it.
222 // Ensure we have permission to access the other api
223 if (!$this->checkPermissionToJoin($fkField['FKApiName'], $subStack)) {
224 throw new UnauthorizedException("Authorization failed to join onto {$fkField['FKApiName']} api in parameter $fkFieldName");
226 if (!isset($fkField['FKApiSpec'])) {
227 $fkField['FKApiSpec'] = \
_civicrm_api_get_fields($fkField['FKApiName']);
229 $fieldInfo = \CRM_Utils_Array
::value($fieldName, $fkField['FKApiSpec']);
231 $keyColumn = \CRM_Utils_Array
::value('FKKeyColumn', $fkField, 'id');
232 if (!$fieldInfo ||
!isset($fkField['FKApiSpec'][$keyColumn])) {
233 // Join doesn't exist - might be another param with a dot in it for some reason, we'll just ignore it.
236 $fkTable = \CRM_Core_DAO_AllCoreTables
::getTableForClass($fkField['FKClassName']);
237 $tableAlias = implode('_to_', $subStack) . "_to_$fkTable";
240 $joinCondition = array_merge(
241 ["$prev.$fk = $tableAlias.$keyColumn"],
242 $this->getAclClause($tableAlias, \
_civicrm_api3_get_BAO($fkField['FKApiName']), $subStack)
245 if (!empty($fkField['FKCondition'])) {
246 $joinCondition[] = str_replace($fkTable, $tableAlias, $fkField['FKCondition']);
249 $this->join($side, $fkTable, $tableAlias, $joinCondition);
251 if (strpos($fieldName, 'custom_') === 0) {
252 list($tableAlias, $fieldName) = $this->addCustomField($fieldInfo, $side, $tableAlias);
255 // Get ready to recurse to the next level
257 $fkField = &$fkField['FKApiSpec'][$fieldName];
260 return [$tableAlias, $fieldName];
264 * Get join info for dynamically-joined fields (e.g. "entity_id", "option_group")
269 protected function getJoinInfo(&$fkField, $stack) {
270 if ($fkField['name'] == 'entity_id') {
271 $entityTableParam = substr(implode('.', $stack), 0, -2) . 'table';
272 $entityTable = \CRM_Utils_Array
::value($entityTableParam, $this->where
);
273 if ($entityTable && is_string($entityTable) && \CRM_Core_DAO_AllCoreTables
::getClassForTable($entityTable)) {
274 $fkField['FKClassName'] = \CRM_Core_DAO_AllCoreTables
::getClassForTable($entityTable);
275 $fkField['FKApiName'] = \CRM_Core_DAO_AllCoreTables
::getBriefName($fkField['FKClassName']);
278 if (!empty($fkField['pseudoconstant']['optionGroupName'])) {
279 $fkField['FKClassName'] = 'CRM_Core_DAO_OptionValue';
280 $fkField['FKApiName'] = 'OptionValue';
281 $fkField['FKKeyColumn'] = 'value';
282 $fkField['FKCondition'] = "civicrm_option_value.option_group_id = (SELECT id FROM civicrm_option_group WHERE name = '{$fkField['pseudoconstant']['optionGroupName']}')";
287 * Joins onto a custom field
289 * Adds a join to the query to make this field available for use in a clause.
291 * @param array $customField
292 * @param string $side
293 * @param string $baseTable
295 * Returns the table and field name for adding this field to a SELECT or WHERE clause
297 protected function addCustomField($customField, $side, $baseTable = self
::MAIN_TABLE_ALIAS
) {
298 $tableName = $customField["table_name"];
299 $columnName = $customField["column_name"];
300 $tableAlias = "{$baseTable}_to_$tableName";
301 $this->join($side, $tableName, $tableAlias, ["`$tableAlias`.entity_id = `$baseTable`.id"]);
302 return [$tableAlias, $columnName];
306 * Fetch a field from the getFields list
308 * @param string $fieldName
311 abstract protected function getField($fieldName);
314 * Perform input validation on params that use the join syntax
316 * Arguably this should be done at the api wrapper level, but doing it here provides a bit more consistency
317 * in that api permissions to perform the join are checked first.
323 protected function validateNestedInput($fieldName, &$value) {
324 $stack = explode('.', $fieldName);
325 $spec = $this->apiFieldSpec
;
326 $fieldName = array_pop($stack);
327 foreach ($stack as $depth => $name) {
328 $entity = $spec[$name]['FKApiName'];
329 $spec = $spec[$name]['FKApiSpec'];
331 $params = [$fieldName => $value];
332 \
_civicrm_api3_validate_fields($entity, 'get', $params, $spec);
333 $value = $params[$fieldName];
337 * Check permission to join onto another api entity
339 * @param string $entity
340 * @param array $fieldStack
341 * The stack of fields leading up to this join
344 protected function checkPermissionToJoin($entity, $fieldStack) {
345 if (!$this->checkPermissions
) {
348 // Build an array of params that relate to the joined entity
352 'check_permissions' => $this->checkPermissions
,
354 $prefix = implode('.', $fieldStack) . '.';
355 $len = strlen($prefix);
356 foreach ($this->select
as $key => $ret) {
357 if (strpos($key, $prefix) === 0) {
358 $params['return'][substr($key, $len)] = $ret;
361 foreach ($this->where
as $key => $param) {
362 if (strpos($key, $prefix) === 0) {
363 $params[substr($key, $len)] = $param;
367 return \Civi
::service('civi_api_kernel')->runAuthorize($entity, 'get', $params);
371 * Get acl clause for an entity
373 * @param string $tableAlias
374 * @param string $baoName
375 * @param array $stack
378 protected function getAclClause($tableAlias, $baoName, $stack = []) {
379 if (!$this->checkPermissions
) {
382 // Prevent (most) redundant acl sub clauses if they have already been applied to the main entity.
383 // FIXME: Currently this only works 1 level deep, but tracking through multiple joins would increase complexity
384 // and just doing it for the first join takes care of most acl clause deduping.
385 if (count($stack) === 1 && in_array($stack[0], $this->aclFields
)) {
388 $clauses = $baoName::getSelectWhereClause($tableAlias);
390 // Track field clauses added to the main entity
391 $this->aclFields
= array_keys($clauses);
393 return array_filter($clauses);
397 * Orders the query by one or more fields
399 * @throws \API_Exception
400 * @throws \Civi\API\Exception\UnauthorizedException
402 protected function buildOrderBy() {
403 $sortParams = is_string($this->orderBy
) ?
explode(',', $this->orderBy
) : (array) $this->orderBy
;
404 foreach ($sortParams as $index => $item) {
406 if ($item == '(1)') {
409 $words = preg_split("/[\s]+/", $item);
411 // Direction defaults to ASC unless DESC is specified
412 $direction = strtoupper(\CRM_Utils_Array
::value(1, $words, '')) == 'DESC' ?
' DESC' : '';
413 $field = $this->getField($words[0]);
415 $this->query
->orderBy(self
::MAIN_TABLE_ALIAS
. '.' . $field['name'] . $direction, NULL, $index);
417 elseif (strpos($words[0], '.')) {
418 $join = $this->addFkField($words[0], 'LEFT');
420 $this->query
->orderBy("`{$join[0]}`.`{$join[1]}`$direction", NULL, $index);
424 throw new \
API_Exception("Unknown field specified for sort. Cannot order by '$item'");
431 * @param string $side
432 * @param string $tableName
433 * @param string $tableAlias
434 * @param array $conditions
436 public function join($side, $tableName, $tableAlias, $conditions) {
437 // INNER JOINs take precedence over LEFT JOINs
438 if ($side != 'LEFT' ||
!isset($this->joins
[$tableAlias])) {
439 $this->joins
[$tableAlias] = $side;
440 $this->query
->join($tableAlias, "$side JOIN `$tableName` `$tableAlias` ON " . implode(' AND ', $conditions));
445 * Populate where clauses
447 * @throws \Civi\API\Exception\UnauthorizedException
450 abstract protected function buildWhereClause();
453 * Populate $this->selectFields
455 * @throws \Civi\API\Exception\UnauthorizedException
457 protected function buildSelectFields() {
458 $return_all_fields = (empty($this->select
) ||
!is_array($this->select
));
459 $return = $return_all_fields ?
$this->entityFieldNames
: $this->select
;
460 if ($return_all_fields ||
in_array('custom', $this->select
)) {
461 foreach (array_keys($this->apiFieldSpec
) as $fieldName) {
462 if (strpos($fieldName, 'custom_') === 0) {
463 $return[] = $fieldName;
468 // Always select the ID if the table has one.
469 if (array_key_exists('id', $this->apiFieldSpec
)) {
470 $this->selectFields
[self
::MAIN_TABLE_ALIAS
. ".id"] = "id";
473 // core return fields
474 foreach ($return as $fieldName) {
475 $field = $this->getField($fieldName);
476 if ($field && in_array($field['name'], $this->entityFieldNames
)) {
477 $this->selectFields
[self
::MAIN_TABLE_ALIAS
. ".{$field['name']}"] = $field['name'];
479 elseif (strpos($fieldName, '.')) {
480 $fkField = $this->addFkField($fieldName, 'LEFT');
482 $this->selectFields
[implode('.', $fkField)] = $fieldName;
485 elseif ($field && strpos($fieldName, 'custom_') === 0) {
486 list($table_name, $column_name) = $this->addCustomField($field, 'LEFT');
488 if ($field['data_type'] != 'ContactReference') {
489 // 'ordinary' custom field. We will select the value as custom_XX.
490 $this->selectFields
["$table_name.$column_name"] = $fieldName;
493 // contact reference custom field. The ID will be stored in custom_XX_id.
494 // custom_XX will contain the sort name of the contact.
495 $this->query
->join("c_$fieldName", "LEFT JOIN civicrm_contact c_$fieldName ON c_$fieldName.id = `$table_name`.`$column_name`");
496 $this->selectFields
["$table_name.$column_name"] = $fieldName . "_id";
497 // We will call the contact table for the join c_XX.
498 $this->selectFields
["c_$fieldName.sort_name"] = $fieldName;
508 abstract protected function getFields();