3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 class Api3SelectQuery
extends SelectQuery
{
17 protected $apiVersion = 3;
22 protected function buildWhereClause() {
24 foreach ($this->where
as $key => $value) {
28 if (substr($key, 0, 7) == 'filter.') {
29 // Legacy support for old filter syntax per the test contract.
30 // (Convert the style to the later one & then deal with them).
31 $filterArray = explode('.', $key);
32 $value = [$filterArray[1] => $value];
36 // Legacy support for 'filter's construct.
37 if ($key == 'filters') {
38 foreach ($value as $filterKey => $filterValue) {
39 if (substr($filterKey, -4, 4) == 'high') {
40 $key = substr($filterKey, 0, -5);
41 $value = ['<=' => $filterValue];
44 if (substr($filterKey, -3, 3) == 'low') {
45 $key = substr($filterKey, 0, -4);
46 $value = ['>=' => $filterValue];
49 if ($filterKey == 'is_current' ||
$filterKey == 'isCurrent') {
50 // Is current is almost worth creating as a 'sql filter' in the DAO function since several entities have the concept.
51 $todayStart = date('Ymd000000', strtotime('now'));
52 $todayEnd = date('Ymd235959', strtotime('now'));
53 $a = self
::MAIN_TABLE_ALIAS
;
54 $this->query
->where("($a.start_date <= '$todayStart' OR $a.start_date IS NULL)
55 AND ($a.end_date >= '$todayEnd' OR $a.end_date IS NULL)
56 AND a.is_active = 1");
60 // Ignore the "options" param if it is referring to api options and not a field in this entity
62 $key === 'options' && is_array($value)
63 && !in_array(\CRM_Utils_Array
::first(array_keys($value)), \CRM_Core_DAO
::acceptedSQLOperators())
67 $field = $this->getField($key);
69 $key = $field['name'];
71 if (in_array($key, $this->entityFieldNames
)) {
72 $table_name = self
::MAIN_TABLE_ALIAS
;
75 elseif (($cf_id = \CRM_Core_BAO_CustomField
::getKeyID($key)) != FALSE) {
76 // If we check a custom field on 'IS NULL', it should also work when there is no
77 // record in the custom value table, see CRM-20740.
78 $side = empty($value['IS NULL']) ?
'INNER' : 'LEFT OUTER';
79 list($table_name, $column_name) = $this->addCustomField($this->apiFieldSpec
['custom_' . $cf_id], $side);
81 elseif (strpos($key, '.')) {
82 $fkInfo = $this->addFkField($key, 'INNER');
84 list($table_name, $column_name) = $fkInfo;
85 $this->validateNestedInput($key, $value);
88 // I don't know why I had to specifically exclude 0 as a key - wouldn't the others have caught it?
89 // We normally silently ignore null values passed in - if people want IS_NULL they can use acceptedSqlOperator syntax.
90 if ((!$table_name) ||
empty($key) ||
is_null($value)) {
91 // No valid filter field. This might be a chained call or something.
92 // Just ignore this for the $where_clause.
95 $operator = is_array($value) ? \CRM_Utils_Array
::first(array_keys($value)) : NULL;
96 if (!in_array($operator, \CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
97 $value = ['=' => $value];
99 $filters[$key] = \CRM_Core_DAO
::createSQLFilter("{$table_name}.{$column_name}", $value);
102 if (!empty($this->where
['options']['or'])) {
103 $orGroups = $this->where
['options']['or'];
104 if (is_string($orGroups)) {
105 $orGroups = array_map('trim', explode(',', $orGroups));
107 if (!is_array(\CRM_Utils_Array
::first($orGroups))) {
108 $orGroups = [$orGroups];
110 foreach ($orGroups as $orGroup) {
112 foreach ($orGroup as $key) {
113 if (!isset($filters[$key])) {
114 throw new \
CiviCRM_API3_Exception("'$key' specified in OR group but not added to params");
116 $orClause[] = $filters[$key];
117 unset($filters[$key]);
119 $this->query
->where(implode(' OR ', $orClause));
122 // Add the remaining params using AND
123 foreach ($filters as $filter) {
124 $this->query
->where($filter);
131 protected function getFields() {
132 require_once 'api/v3/Generic.php';
133 // Call this function directly instead of using the api wrapper to force unique field names off
134 $apiSpec = \
civicrm_api3_generic_getfields([
135 'entity' => $this->entity
,
137 'params' => ['action' => 'get'],
139 return $apiSpec['values'];
143 * Fetch a field from the getFields list
145 * Searches by name, uniqueName, and api.aliases
147 * @param string $fieldName
151 protected function getField($fieldName) {
155 if (isset($this->apiFieldSpec
[$fieldName])) {
156 return $this->apiFieldSpec
[$fieldName];
158 foreach ($this->apiFieldSpec
as $field) {
160 $fieldName == \CRM_Utils_Array
::value('uniqueName', $field) ||
161 array_search($fieldName, \CRM_Utils_Array
::value('api.aliases', $field, [])) !== FALSE