X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FAPI%2FSelectQuery.php;h=76ae48e06489fc2d230ca3d487d82a11d2229952;hb=420a0aa9283395805d1449d2dcc2d3f9a86b8028;hp=d7c6d3f12733f18d2c0ec4b8823137896a8e2bfc;hpb=03f5e517b171ea0c7f04bf05d018a149ca34dac8;p=civicrm-core.git diff --git a/Civi/API/SelectQuery.php b/Civi/API/SelectQuery.php index d7c6d3f127..76ae48e064 100644 --- a/Civi/API/SelectQuery.php +++ b/Civi/API/SelectQuery.php @@ -11,6 +11,7 @@ namespace Civi\API; use Civi\API\Exception\UnauthorizedException; +use CRM_Core_Exception; /** * Query builder for civicrm_api_basic_get. @@ -63,7 +64,7 @@ abstract class SelectQuery { /** * @var array */ - protected $entityFieldNames; + protected $entityFieldNames = []; /** * @var array */ @@ -78,14 +79,19 @@ abstract class SelectQuery { /** * @param string $entity * @param bool $checkPermissions + * + * @throws \CRM_Core_Exception */ public function __construct($entity, $checkPermissions) { $this->entity = $entity; require_once 'api/v3/utils.php'; $baoName = _civicrm_api3_get_BAO($entity); + if (!$baoName) { + throw new CRM_Core_Exception('Invalid entity: ' . \CRM_Utils_Type::validate($entity, 'Alphanumeric')); + } $bao = new $baoName(); - $this->entityFieldNames = _civicrm_api3_field_names(_civicrm_api3_build_fields_array($bao)); + $this->entityFieldNames = array_column($baoName::fields(), 'name'); $this->apiFieldSpec = $this->getFields(); $this->query = \CRM_Utils_SQL_Select::from($bao->tableName() . ' ' . self::MAIN_TABLE_ALIAS); @@ -210,7 +216,7 @@ abstract class SelectQuery { if (!isset($fkField['FKApiSpec'])) { $fkField['FKApiSpec'] = \_civicrm_api_get_fields($fkField['FKApiName']); } - $fieldInfo = \CRM_Utils_Array::value($fieldName, $fkField['FKApiSpec']); + $fieldInfo = $fkField['FKApiSpec'][$fieldName] ?? NULL; $keyColumn = \CRM_Utils_Array::value('FKKeyColumn', $fkField, 'id'); if (!$fieldInfo || !isset($fkField['FKApiSpec'][$keyColumn])) { @@ -239,7 +245,7 @@ abstract class SelectQuery { $this->join($side, $fkTable, $tableAlias, $joinCondition); if (strpos($fieldName, 'custom_') === 0) { - list($tableAlias, $fieldName) = $this->addCustomField($fieldInfo, $side, $tableAlias); + [$tableAlias, $fieldName] = $this->addCustomField($fieldInfo, $side, $tableAlias); } // Get ready to recurse to the next level @@ -259,7 +265,7 @@ abstract class SelectQuery { protected function getJoinInfo(&$fkField, $stack) { if ($fkField['name'] == 'entity_id') { $entityTableParam = substr(implode('.', $stack), 0, -2) . 'table'; - $entityTable = \CRM_Utils_Array::value($entityTableParam, $this->where); + $entityTable = $this->where[$entityTableParam] ?? NULL; if ($entityTable && is_string($entityTable) && \CRM_Core_DAO_AllCoreTables::getClassForTable($entityTable)) { $fkField['FKClassName'] = \CRM_Core_DAO_AllCoreTables::getClassForTable($entityTable); $fkField['FKApiName'] = \CRM_Core_DAO_AllCoreTables::getBriefName($fkField['FKClassName']); @@ -361,11 +367,11 @@ abstract class SelectQuery { * Get acl clause for an entity * * @param string $tableAlias - * @param string $baoName + * @param \CRM_Core_DAO|string $baoName * @param array $stack * @return array */ - protected function getAclClause($tableAlias, $baoName, $stack = []) { + public function getAclClause($tableAlias, $baoName, $stack = []) { if (!$this->checkPermissions) { return []; } @@ -473,7 +479,7 @@ abstract class SelectQuery { } } elseif ($field && strpos($fieldName, 'custom_') === 0) { - list($table_name, $column_name) = $this->addCustomField($field, 'LEFT'); + [$table_name, $column_name] = $this->addCustomField($field, 'LEFT'); if ($field['data_type'] != 'ContactReference') { // 'ordinary' custom field. We will select the value as custom_XX.