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.
237 // Skip if we don't have permission to access this field
238 if ($this->checkPermissions
&& !empty($fieldInfo['permission']) && !\CRM_Core_Permission
::check($fieldInfo['permission'])) {
242 $fkTable = \CRM_Core_DAO_AllCoreTables
::getTableForClass($fkField['FKClassName']);
243 $tableAlias = implode('_to_', $subStack) . "_to_$fkTable";
246 $joinCondition = array_merge(
247 ["$prev.$fk = $tableAlias.$keyColumn"],
248 $this->getAclClause($tableAlias, \
_civicrm_api3_get_BAO($fkField['FKApiName']), $subStack)
251 if (!empty($fkField['FKCondition'])) {
252 $joinCondition[] = str_replace($fkTable, $tableAlias, $fkField['FKCondition']);
255 $this->join($side, $fkTable, $tableAlias, $joinCondition);
257 if (strpos($fieldName, 'custom_') === 0) {
258 list($tableAlias, $fieldName) = $this->addCustomField($fieldInfo, $side, $tableAlias);
261 // Get ready to recurse to the next level
263 $fkField = &$fkField['FKApiSpec'][$fieldName];
266 return [$tableAlias, $fieldName];
270 * Get join info for dynamically-joined fields (e.g. "entity_id", "option_group")
275 protected function getJoinInfo(&$fkField, $stack) {
276 if ($fkField['name'] == 'entity_id') {
277 $entityTableParam = substr(implode('.', $stack), 0, -2) . 'table';
278 $entityTable = \CRM_Utils_Array
::value($entityTableParam, $this->where
);
279 if ($entityTable && is_string($entityTable) && \CRM_Core_DAO_AllCoreTables
::getClassForTable($entityTable)) {
280 $fkField['FKClassName'] = \CRM_Core_DAO_AllCoreTables
::getClassForTable($entityTable);
281 $fkField['FKApiName'] = \CRM_Core_DAO_AllCoreTables
::getBriefName($fkField['FKClassName']);
284 if (!empty($fkField['pseudoconstant']['optionGroupName'])) {
285 $fkField['FKClassName'] = 'CRM_Core_DAO_OptionValue';
286 $fkField['FKApiName'] = 'OptionValue';
287 $fkField['FKKeyColumn'] = 'value';
288 $fkField['FKCondition'] = "civicrm_option_value.option_group_id = (SELECT id FROM civicrm_option_group WHERE name = '{$fkField['pseudoconstant']['optionGroupName']}')";
293 * Joins onto a custom field
295 * Adds a join to the query to make this field available for use in a clause.
297 * @param array $customField
298 * @param string $side
299 * @param string $baseTable
301 * Returns the table and field name for adding this field to a SELECT or WHERE clause
303 protected function addCustomField($customField, $side, $baseTable = self
::MAIN_TABLE_ALIAS
) {
304 $tableName = $customField["table_name"];
305 $columnName = $customField["column_name"];
306 $tableAlias = "{$baseTable}_to_$tableName";
307 $this->join($side, $tableName, $tableAlias, ["`$tableAlias`.entity_id = `$baseTable`.id"]);
308 return [$tableAlias, $columnName];
312 * Fetch a field from the getFields list
314 * @param string $fieldName
317 abstract protected function getField($fieldName);
320 * Perform input validation on params that use the join syntax
322 * Arguably this should be done at the api wrapper level, but doing it here provides a bit more consistency
323 * in that api permissions to perform the join are checked first.
329 protected function validateNestedInput($fieldName, &$value) {
330 $stack = explode('.', $fieldName);
331 $spec = $this->apiFieldSpec
;
332 $fieldName = array_pop($stack);
333 foreach ($stack as $depth => $name) {
334 $entity = $spec[$name]['FKApiName'];
335 $spec = $spec[$name]['FKApiSpec'];
337 $params = [$fieldName => $value];
338 \
_civicrm_api3_validate_fields($entity, 'get', $params, $spec);
339 $value = $params[$fieldName];
343 * Check permission to join onto another api entity
345 * @param string $entity
346 * @param array $fieldStack
347 * The stack of fields leading up to this join
350 protected function checkPermissionToJoin($entity, $fieldStack) {
351 if (!$this->checkPermissions
) {
354 // Build an array of params that relate to the joined entity
358 'check_permissions' => $this->checkPermissions
,
360 $prefix = implode('.', $fieldStack) . '.';
361 $len = strlen($prefix);
362 foreach ($this->select
as $key => $ret) {
363 if (strpos($key, $prefix) === 0) {
364 $params['return'][substr($key, $len)] = $ret;
367 foreach ($this->where
as $key => $param) {
368 if (strpos($key, $prefix) === 0) {
369 $params[substr($key, $len)] = $param;
373 return \Civi
::service('civi_api_kernel')->runAuthorize($entity, 'get', $params);
377 * Get acl clause for an entity
379 * @param string $tableAlias
380 * @param string $baoName
381 * @param array $stack
384 protected function getAclClause($tableAlias, $baoName, $stack = []) {
385 if (!$this->checkPermissions
) {
388 // Prevent (most) redundant acl sub clauses if they have already been applied to the main entity.
389 // FIXME: Currently this only works 1 level deep, but tracking through multiple joins would increase complexity
390 // and just doing it for the first join takes care of most acl clause deduping.
391 if (count($stack) === 1 && in_array($stack[0], $this->aclFields
)) {
394 $clauses = $baoName::getSelectWhereClause($tableAlias);
396 // Track field clauses added to the main entity
397 $this->aclFields
= array_keys($clauses);
399 return array_filter($clauses);
403 * Orders the query by one or more fields
405 * @throws \API_Exception
406 * @throws \Civi\API\Exception\UnauthorizedException
408 protected function buildOrderBy() {
409 $sortParams = is_string($this->orderBy
) ?
explode(',', $this->orderBy
) : (array) $this->orderBy
;
410 foreach ($sortParams as $index => $item) {
412 if ($item == '(1)') {
415 $words = preg_split("/[\s]+/", $item);
417 // Direction defaults to ASC unless DESC is specified
418 $direction = strtoupper(\CRM_Utils_Array
::value(1, $words, '')) == 'DESC' ?
' DESC' : '';
419 $field = $this->getField($words[0]);
421 $this->query
->orderBy(self
::MAIN_TABLE_ALIAS
. '.' . $field['name'] . $direction, NULL, $index);
423 elseif (strpos($words[0], '.')) {
424 $join = $this->addFkField($words[0], 'LEFT');
426 $this->query
->orderBy("`{$join[0]}`.`{$join[1]}`$direction", NULL, $index);
430 throw new \
API_Exception("Unknown field specified for sort. Cannot order by '$item'");
437 * @param string $side
438 * @param string $tableName
439 * @param string $tableAlias
440 * @param array $conditions
442 public function join($side, $tableName, $tableAlias, $conditions) {
443 // INNER JOINs take precedence over LEFT JOINs
444 if ($side != 'LEFT' ||
!isset($this->joins
[$tableAlias])) {
445 $this->joins
[$tableAlias] = $side;
446 $this->query
->join($tableAlias, "$side JOIN `$tableName` `$tableAlias` ON " . implode(' AND ', $conditions));
451 * Populate where clauses
453 * @throws \Civi\API\Exception\UnauthorizedException
456 abstract protected function buildWhereClause();
459 * Populate $this->selectFields
461 * @throws \Civi\API\Exception\UnauthorizedException
463 protected function buildSelectFields() {
464 $return_all_fields = (empty($this->select
) ||
!is_array($this->select
));
465 $return = $return_all_fields ?
$this->entityFieldNames
: $this->select
;
466 if ($return_all_fields ||
in_array('custom', $this->select
)) {
467 foreach (array_keys($this->apiFieldSpec
) as $fieldName) {
468 if (strpos($fieldName, 'custom_') === 0) {
469 $return[] = $fieldName;
474 // Always select the ID if the table has one.
475 if (array_key_exists('id', $this->apiFieldSpec
)) {
476 $this->selectFields
[self
::MAIN_TABLE_ALIAS
. ".id"] = "id";
479 // core return fields
480 foreach ($return as $fieldName) {
481 $field = $this->getField($fieldName);
482 if ($field && in_array($field['name'], $this->entityFieldNames
)) {
483 $this->selectFields
[self
::MAIN_TABLE_ALIAS
. ".{$field['name']}"] = $field['name'];
485 elseif (strpos($fieldName, '.')) {
486 $fkField = $this->addFkField($fieldName, 'LEFT');
488 $this->selectFields
[implode('.', $fkField)] = $fieldName;
491 elseif ($field && strpos($fieldName, 'custom_') === 0) {
492 list($table_name, $column_name) = $this->addCustomField($field, 'LEFT');
494 if ($field['data_type'] != 'ContactReference') {
495 // 'ordinary' custom field. We will select the value as custom_XX.
496 $this->selectFields
["$table_name.$column_name"] = $fieldName;
499 // contact reference custom field. The ID will be stored in custom_XX_id.
500 // custom_XX will contain the sort name of the contact.
501 $this->query
->join("c_$fieldName", "LEFT JOIN civicrm_contact c_$fieldName ON c_$fieldName.id = `$table_name`.`$column_name`");
502 $this->selectFields
["$table_name.$column_name"] = $fieldName . "_id";
503 // We will call the contact table for the join c_XX.
504 $this->selectFields
["c_$fieldName.sort_name"] = $fieldName;
514 abstract protected function getFields();