Merge pull request #23210 from eileenmcnaughton/cancel
[civicrm-core.git] / CRM / Core / BAO / CustomQuery.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18 class CRM_Core_BAO_CustomQuery {
19 const PREFIX = 'custom_value_';
20
21 /**
22 * The set of custom field ids.
23 *
24 * @var array
25 */
26 protected $_ids;
27
28 /**
29 * The select clause.
30 *
31 * @var array
32 */
33 public $_select;
34
35 /**
36 * The name of the elements that are in the select clause.
37 * used to extract the values
38 *
39 * @var array
40 */
41 public $_element;
42
43 /**
44 * The tables involved in the query.
45 *
46 * @var array
47 */
48 public $_tables;
49 public $_whereTables;
50
51 /**
52 * The where clause.
53 *
54 * @var array
55 */
56 public $_where;
57
58 /**
59 * The english language version of the query.
60 *
61 * @var array
62 */
63 public $_qill;
64
65 /**
66 * The custom fields information.
67 *
68 * @var array
69 */
70 public $_fields;
71
72 /**
73 * @return array
74 */
75 public function getFields() {
76 return $this->_fields;
77 }
78
79 /**
80 * Searching for contacts?
81 *
82 * @var bool
83 */
84 protected $_contactSearch;
85
86 protected $_locationSpecificCustomFields;
87
88 /**
89 * This stores custom data group types and tables that it extends.
90 *
91 * @var array
92 */
93 public static $extendsMap = [
94 'Contact' => 'civicrm_contact',
95 'Individual' => 'civicrm_contact',
96 'Household' => 'civicrm_contact',
97 'Organization' => 'civicrm_contact',
98 'Contribution' => 'civicrm_contribution',
99 'ContributionRecur' => 'civicrm_contribution_recur',
100 'Membership' => 'civicrm_membership',
101 'Participant' => 'civicrm_participant',
102 'Group' => 'civicrm_group',
103 'Relationship' => 'civicrm_relationship',
104 'Event' => 'civicrm_event',
105 'Case' => 'civicrm_case',
106 'Activity' => 'civicrm_activity',
107 'Pledge' => 'civicrm_pledge',
108 'Grant' => 'civicrm_grant',
109 'Address' => 'civicrm_address',
110 'Campaign' => 'civicrm_campaign',
111 'Survey' => 'civicrm_survey',
112 ];
113
114 /**
115 * Class constructor.
116 *
117 * Takes in a set of custom field ids andsets up the data structures to
118 * generate a query
119 *
120 * @param array $ids
121 * The set of custom field ids.
122 *
123 * @param bool $contactSearch
124 * @param array $locationSpecificFields
125 */
126 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = []) {
127 $this->_ids = $ids;
128 $this->_locationSpecificCustomFields = $locationSpecificFields;
129
130 $this->_select = [];
131 $this->_element = [];
132 $this->_tables = [];
133 $this->_whereTables = [];
134 $this->_where = [];
135 $this->_qill = [];
136
137 $this->_contactSearch = $contactSearch;
138 $this->_fields = CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
139 }
140
141 /**
142 * Generate the select clause and the associated tables.
143 */
144 public function select() {
145 if (empty($this->_fields)) {
146 return;
147 }
148
149 foreach (array_keys($this->_ids) as $id) {
150 // Ignore any custom field ids within the ids array that are not present in the fields array.
151 if (empty($this->_fields[$id])) {
152 continue;
153 }
154 $field = $this->_fields[$id];
155
156 if ($this->_contactSearch && $field['search_table'] === 'contact_a') {
157 CRM_Contact_BAO_Query::$_openedPanes[ts('Custom Fields')] = TRUE;
158 }
159
160 $name = $field['table_name'];
161 $fieldName = 'custom_' . $field['id'];
162 $this->_select["{$name}_id"] = "{$name}.id as {$name}_id";
163 $this->_element["{$name}_id"] = 1;
164 $this->_select[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
165 $this->_element[$fieldName] = 1;
166
167 $this->joinCustomTableForField($field);
168 }
169 }
170
171 /**
172 * Generate the where clause and also the english language equivalent.
173 *
174 * @throws \CRM_Core_Exception
175 */
176 public function where() {
177 foreach ($this->_ids as $id => $values) {
178
179 // Fixed for Issue CRM 607
180 if (CRM_Utils_Array::value($id, $this->_fields) === NULL ||
181 !$values
182 ) {
183 continue;
184 }
185
186 foreach ($values as $tuple) {
187 list($name, $op, $value, $grouping, $wildcard) = $tuple;
188
189 $field = $this->_fields[$id];
190
191 $fieldName = "{$field['table_name']}.{$field['column_name']}";
192
193 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($field);
194
195 // fix $value here to escape sql injection attacks
196 $qillValue = NULL;
197 if (!is_array($value)) {
198 $value = CRM_Core_DAO::escapeString(trim($value));
199 $qillValue = CRM_Core_BAO_CustomField::displayValue($value, $id);
200 }
201 elseif (count($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
202 $op = key($value);
203 $qillValue = strstr($op, 'NULL') ? NULL : CRM_Core_BAO_CustomField::displayValue($value[$op], $id);
204 }
205 else {
206 $op = strstr($op, 'IN') ? $op : 'IN';
207 $qillValue = CRM_Core_BAO_CustomField::displayValue($value, $id);
208 }
209
210 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
211
212 // Ensure the table is joined in (eg if in where but not select).
213 $this->joinCustomTableForField($field);
214 switch ($field['data_type']) {
215 case 'String':
216 case 'StateProvince':
217 case 'Country':
218 case 'ContactReference':
219
220 if ($field['is_search_range'] && is_array($value)) {
221 //didn't found any field under any of these three data-types as searchable by range
222 }
223 else {
224 // fix $value here to escape sql injection attacks
225 if (!is_array($value)) {
226 if ($field['data_type'] == 'String') {
227 $value = CRM_Utils_Type::escape($value, 'String');
228 }
229 elseif ($value) {
230 $value = CRM_Utils_Type::escape($value, 'Integer');
231 }
232 $value = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $value);
233 $value = str_replace('|', '[:separator:]', $value);
234 }
235 elseif ($isSerialized) {
236 if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
237 $op = key($value);
238 $value = $value[$op];
239 }
240 // CRM-19006: escape characters like comma, | before building regex pattern
241 $value = (array) $value;
242 foreach ($value as $key => $val) {
243 $value[$key] = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $val);
244 $value[$key] = str_replace('|', '[:separator:]', $value[$key]);
245 if ($field['data_type'] == 'String') {
246 $value[$key] = CRM_Utils_Type::escape($value[$key], 'String');
247 }
248 elseif ($value) {
249 $value[$key] = CRM_Utils_Type::escape($value[$key], 'Integer');
250 }
251 }
252 $value = implode(',', $value);
253 }
254
255 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
256 if ($isSerialized && !CRM_Utils_System::isNull($value) && !strstr($op, 'NULL') && !strstr($op, 'LIKE')) {
257 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
258 $value = str_replace(",", "$sp|$sp", $value);
259 $value = str_replace(['[:comma:]', '(', ')'], [',', '[(]', '[)]'], $value);
260
261 $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
262 $value = $sp . $value . $sp;
263 if (!$wildcard) {
264 foreach (explode("|", $value) as $val) {
265 $val = str_replace('[:separator:]', '\|', $val);
266 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $val, 'String');
267 }
268 }
269 else {
270 $value = str_replace('[:separator:]', '\|', $value);
271 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
272 }
273 }
274 else {
275 //FIX for custom data query fired against no value(NULL/NOT NULL)
276 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
277 }
278 $this->_qill[$grouping][] = $field['label'] . " $qillOp $qillValue";
279 }
280 break;
281
282 case 'Int':
283 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
284 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
285 break;
286
287 case 'Boolean':
288 if (!is_array($value)) {
289 if (strtolower($value) == 'yes' || strtolower($value) == strtolower(ts('Yes'))) {
290 $value = 1;
291 }
292 else {
293 $value = (int) $value;
294 }
295 $value = ($value == 1) ? 1 : 0;
296 $qillValue = $value ? 'Yes' : 'No';
297 }
298 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
299 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
300 break;
301
302 case 'Link':
303 case 'Memo':
304 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
305 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
306 break;
307
308 case 'Money':
309 $value = CRM_Utils_Array::value($op, (array) $value, $value);
310 if (is_array($value)) {
311 foreach ($value as $key => $val) {
312 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
313 // Note the only place I can find that this code is reached by is searching a custom money field in advanced search.
314 // with euro style comma separators this doesn't work - with or without this cleanMoney.
315 // So this should be removed but is not increasing the brokeness IMHO
316 $value[$op][$key] = CRM_Utils_Rule::cleanMoney($value[$key]);
317 }
318 }
319 else {
320 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
321 // comments per above apply. cleanMoney
322 $value = CRM_Utils_Rule::cleanMoney($value);
323 }
324
325 case 'Float':
326 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
327 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
328 break;
329
330 case 'Date':
331 if (substr($name, -9, 9) !== '_relative'
332 && substr($name, -4, 4) !== '_low'
333 && substr($name, -5, 5) !== '_high') {
334 // Relative dates are handled in the buildRelativeDateQuery function.
335 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Date');
336 list($qillOp, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $field['label'], $value, $op, [], CRM_Utils_Type::T_DATE);
337 $this->_qill[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
338 }
339 break;
340
341 case 'File':
342 if ($op == 'IS NULL' || $op == 'IS NOT NULL' || $op == 'IS EMPTY' || $op == 'IS NOT EMPTY') {
343 switch ($op) {
344 case 'IS EMPTY':
345 $op = 'IS NULL';
346 break;
347
348 case 'IS NOT EMPTY':
349 $op = 'IS NOT NULL';
350 break;
351 }
352 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op);
353 $this->_qill[$grouping][] = $field['label'] . " {$qillOp} ";
354 }
355 break;
356 }
357 }
358 }
359 }
360
361 /**
362 * Function that does the actual query generation.
363 * basically ties all the above functions together
364 *
365 * @return array
366 * array of strings
367 */
368 public function query() {
369 $this->select();
370
371 $this->where();
372
373 $whereStr = NULL;
374 if (!empty($this->_where)) {
375 $clauses = [];
376 foreach ($this->_where as $grouping => $values) {
377 if (!empty($values)) {
378 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
379 }
380 }
381 if (!empty($clauses)) {
382 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
383 }
384 }
385
386 return [
387 implode(' , ', $this->_select),
388 implode(' ', $this->_tables),
389 $whereStr,
390 ];
391 }
392
393 /**
394 * Join the custom table for the field in (if not already in the query).
395 *
396 * @param array $field
397 */
398 protected function joinCustomTableForField($field) {
399 $name = $field['table_name'];
400 $join = "\nLEFT JOIN $name ON $name.entity_id = {$field['search_table']}.id";
401 $this->_tables[$name] = $this->_tables[$name] ?? $join;
402 $this->_whereTables[$name] = $this->_whereTables[$name] ?? $join;
403
404 $joinTable = $field['search_table'];
405 if ($joinTable) {
406 $joinClause = 1;
407 $joinTableAlias = $joinTable;
408 // Set location-specific query
409 if (isset($this->_locationSpecificCustomFields[$field['id']])) {
410 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$field['id']];
411 $joinTableAlias = "$locationType-address";
412 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
413 }
414 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
415 if (!empty($this->_ids[$field['id']])) {
416 $this->_whereTables[$name] = $this->_tables[$name];
417 }
418 if ($joinTable !== 'contact_a') {
419 $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause;
420 }
421 }
422 }
423
424 }