Merge pull request #16017 from mattwire/setentitysubtype
[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 * No longer needed due to CRM-17646 refactoring, but still used in some places
67 *
68 * @var array
69 * @deprecated
70 */
71 public $_options;
72
73 /**
74 * The custom fields information.
75 *
76 * @var array
77 */
78 public $_fields;
79
80 /**
81 * @return array
82 */
83 public function getFields() {
84 return $this->_fields;
85 }
86
87 /**
88 * Searching for contacts?
89 *
90 * @var bool
91 */
92 protected $_contactSearch;
93
94 protected $_locationSpecificCustomFields;
95
96 /**
97 * This stores custom data group types and tables that it extends.
98 *
99 * @var array
100 */
101 public static $extendsMap = [
102 'Contact' => 'civicrm_contact',
103 'Individual' => 'civicrm_contact',
104 'Household' => 'civicrm_contact',
105 'Organization' => 'civicrm_contact',
106 'Contribution' => 'civicrm_contribution',
107 'ContributionRecur' => 'civicrm_contribution_recur',
108 'Membership' => 'civicrm_membership',
109 'Participant' => 'civicrm_participant',
110 'Group' => 'civicrm_group',
111 'Relationship' => 'civicrm_relationship',
112 'Event' => 'civicrm_event',
113 'Case' => 'civicrm_case',
114 'Activity' => 'civicrm_activity',
115 'Pledge' => 'civicrm_pledge',
116 'Grant' => 'civicrm_grant',
117 'Address' => 'civicrm_address',
118 'Campaign' => 'civicrm_campaign',
119 'Survey' => 'civicrm_survey',
120 ];
121
122 /**
123 * Class constructor.
124 *
125 * Takes in a set of custom field ids andsets up the data structures to
126 * generate a query
127 *
128 * @param array $ids
129 * The set of custom field ids.
130 *
131 * @param bool $contactSearch
132 * @param array $locationSpecificFields
133 */
134 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = []) {
135 $this->_ids = $ids;
136 $this->_locationSpecificCustomFields = $locationSpecificFields;
137
138 $this->_select = [];
139 $this->_element = [];
140 $this->_tables = [];
141 $this->_whereTables = [];
142 $this->_where = [];
143 $this->_qill = [];
144 $this->_options = [];
145
146 $this->_contactSearch = $contactSearch;
147 $this->_fields = CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
148
149 if (empty($this->_ids)) {
150 return;
151 }
152
153 // initialize the field array
154 $tmpArray = array_keys($this->_ids);
155 $idString = implode(',', $tmpArray);
156 $query = "
157 SELECT f.id, f.label, f.data_type,
158 f.html_type, f.is_search_range,
159 f.option_group_id, f.custom_group_id,
160 f.column_name, g.table_name,
161 f.date_format,f.time_format
162 FROM civicrm_custom_field f,
163 civicrm_custom_group g
164 WHERE f.custom_group_id = g.id
165 AND g.is_active = 1
166 AND f.is_active = 1
167 AND f.id IN ( $idString )";
168
169 $dao = CRM_Core_DAO::executeQuery($query);
170 while ($dao->fetch()) {
171 // Deprecated (and poorly named) cache of field attributes
172 $this->_options[$dao->id] = [
173 'attributes' => [
174 'label' => $dao->label,
175 'data_type' => $dao->data_type,
176 'html_type' => $dao->html_type,
177 ],
178 ];
179
180 $options = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $dao->id, [], 'search');
181 if ($options) {
182 $this->_options[$dao->id] += $options;
183 }
184
185 if ($dao->html_type == 'Select Date') {
186 $this->_options[$dao->id]['attributes']['date_format'] = $dao->date_format;
187 $this->_options[$dao->id]['attributes']['time_format'] = $dao->time_format;
188 }
189 }
190 }
191
192 /**
193 * Generate the select clause and the associated tables.
194 */
195 public function select() {
196 if (empty($this->_fields)) {
197 return;
198 }
199
200 foreach (array_keys($this->_ids) as $id) {
201 $field = $this->_fields[$id];
202
203 if ($this->_contactSearch && $field['search_table'] === 'contact_a') {
204 CRM_Contact_BAO_Query::$_openedPanes[ts('Custom Fields')] = TRUE;
205 }
206
207 $name = $field['table_name'];
208 $fieldName = 'custom_' . $field['id'];
209 $this->_select["{$name}_id"] = "{$name}.id as {$name}_id";
210 $this->_element["{$name}_id"] = 1;
211 $this->_select[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
212 $this->_element[$fieldName] = 1;
213
214 $this->joinCustomTableForField($field);
215 }
216 }
217
218 /**
219 * Generate the where clause and also the english language equivalent.
220 *
221 * @throws \CRM_Core_Exception
222 */
223 public function where() {
224 foreach ($this->_ids as $id => $values) {
225
226 // Fixed for Issue CRM 607
227 if (CRM_Utils_Array::value($id, $this->_fields) === NULL ||
228 !$values
229 ) {
230 continue;
231 }
232
233 foreach ($values as $tuple) {
234 list($name, $op, $value, $grouping, $wildcard) = $tuple;
235
236 $field = $this->_fields[$id];
237
238 $fieldName = "{$field['table_name']}.{$field['column_name']}";
239
240 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($field);
241
242 // fix $value here to escape sql injection attacks
243 $qillValue = NULL;
244 if (!is_array($value)) {
245 $value = CRM_Core_DAO::escapeString(trim($value));
246 $qillValue = CRM_Core_BAO_CustomField::displayValue($value, $id);
247 }
248 elseif (count($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
249 $op = key($value);
250 $qillValue = strstr($op, 'NULL') ? NULL : CRM_Core_BAO_CustomField::displayValue($value[$op], $id);
251 }
252 else {
253 $op = strstr($op, 'IN') ? $op : 'IN';
254 $qillValue = CRM_Core_BAO_CustomField::displayValue($value, $id);
255 }
256
257 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
258
259 // Ensure the table is joined in (eg if in where but not select).
260 $this->joinCustomTableForField($field);
261 switch ($field['data_type']) {
262 case 'String':
263 case 'StateProvince':
264 case 'Country':
265
266 if ($field['is_search_range'] && is_array($value)) {
267 //didn't found any field under any of these three data-types as searchable by range
268 }
269 else {
270 // fix $value here to escape sql injection attacks
271 if (!is_array($value)) {
272 if ($field['data_type'] == 'String') {
273 $value = CRM_Utils_Type::escape($value, 'String');
274 }
275 elseif ($value) {
276 $value = CRM_Utils_Type::escape($value, 'Integer');
277 }
278 $value = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $value);
279 $value = str_replace('|', '[:separator:]', $value);
280 }
281 elseif ($isSerialized) {
282 if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
283 $op = key($value);
284 $value = $value[$op];
285 }
286 // CRM-19006: escape characters like comma, | before building regex pattern
287 $value = (array) $value;
288 foreach ($value as $key => $val) {
289 $value[$key] = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $val);
290 $value[$key] = str_replace('|', '[:separator:]', $value[$key]);
291 if ($field['data_type'] == 'String') {
292 $value[$key] = CRM_Utils_Type::escape($value[$key], 'String');
293 }
294 elseif ($value) {
295 $value[$key] = CRM_Utils_Type::escape($value[$key], 'Integer');
296 }
297 }
298 $value = implode(',', $value);
299 }
300
301 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
302 if ($isSerialized && !CRM_Utils_System::isNull($value) && !strstr($op, 'NULL') && !strstr($op, 'LIKE')) {
303 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
304 $value = str_replace(",", "$sp|$sp", $value);
305 $value = str_replace(['[:comma:]', '(', ')'], [',', '[(]', '[)]'], $value);
306
307 $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
308 $value = $sp . $value . $sp;
309 if (!$wildcard) {
310 foreach (explode("|", $value) as $val) {
311 $val = str_replace('[:separator:]', '\|', $val);
312 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $val, 'String');
313 }
314 }
315 else {
316 $value = str_replace('[:separator:]', '\|', $value);
317 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
318 }
319 }
320 else {
321 //FIX for custom data query fired against no value(NULL/NOT NULL)
322 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
323 }
324 $this->_qill[$grouping][] = $field['label'] . " $qillOp $qillValue";
325 }
326 break;
327
328 case 'ContactReference':
329 $label = $value ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
330 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
331 $this->_qill[$grouping][] = $field['label'] . " $qillOp $label";
332 break;
333
334 case 'Int':
335 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
336 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
337 break;
338
339 case 'Boolean':
340 if (!is_array($value)) {
341 if (strtolower($value) == 'yes' || strtolower($value) == strtolower(ts('Yes'))) {
342 $value = 1;
343 }
344 else {
345 $value = (int) $value;
346 }
347 $value = ($value == 1) ? 1 : 0;
348 $qillValue = $value ? 'Yes' : 'No';
349 }
350 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
351 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
352 break;
353
354 case 'Link':
355 case 'Memo':
356 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
357 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
358 break;
359
360 case 'Money':
361 $value = CRM_Utils_Array::value($op, (array) $value, $value);
362 if (is_array($value)) {
363 foreach ($value as $key => $val) {
364 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
365 // Note the only place I can find that this code is reached by is searching a custom money field in advanced search.
366 // with euro style comma separators this doesn't work - with or without this cleanMoney.
367 // So this should be removed but is not increasing the brokeness IMHO
368 $value[$op][$key] = CRM_Utils_Rule::cleanMoney($value[$key]);
369 }
370 }
371 else {
372 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
373 // comments per above apply. cleanMoney
374 $value = CRM_Utils_Rule::cleanMoney($value);
375 }
376
377 case 'Float':
378 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
379 $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
380 break;
381
382 case 'Date':
383 if (substr($name, -9, 9) !== '_relative'
384 && substr($name, -4, 4) !== '_low'
385 && substr($name, -5, 5) !== '_high') {
386 // Relative dates are handled in the buildRelativeDateQuery function.
387 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Date');
388 list($qillOp, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $field['label'], $value, $op, [], CRM_Utils_Type::T_DATE);
389 $this->_qill[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
390 }
391 break;
392
393 case 'File':
394 if ($op == 'IS NULL' || $op == 'IS NOT NULL' || $op == 'IS EMPTY' || $op == 'IS NOT EMPTY') {
395 switch ($op) {
396 case 'IS EMPTY':
397 $op = 'IS NULL';
398 break;
399
400 case 'IS NOT EMPTY':
401 $op = 'IS NOT NULL';
402 break;
403 }
404 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op);
405 $this->_qill[$grouping][] = $field['label'] . " {$qillOp} ";
406 }
407 break;
408 }
409 }
410 }
411 }
412
413 /**
414 * Function that does the actual query generation.
415 * basically ties all the above functions together
416 *
417 * @return array
418 * array of strings
419 */
420 public function query() {
421 $this->select();
422
423 $this->where();
424
425 $whereStr = NULL;
426 if (!empty($this->_where)) {
427 $clauses = [];
428 foreach ($this->_where as $grouping => $values) {
429 if (!empty($values)) {
430 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
431 }
432 }
433 if (!empty($clauses)) {
434 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
435 }
436 }
437
438 return [
439 implode(' , ', $this->_select),
440 implode(' ', $this->_tables),
441 $whereStr,
442 ];
443 }
444
445 /**
446 * Join the custom table for the field in (if not already in the query).
447 *
448 * @param array $field
449 */
450 protected function joinCustomTableForField($field) {
451 $name = $field['table_name'];
452 $join = "\nLEFT JOIN $name ON $name.entity_id = {$field['search_table']}.id";
453 $this->_tables[$name] = $this->_tables[$name] ?? $join;
454 $this->_whereTables[$name] = $this->_whereTables[$name] ?? $join;
455
456 $joinTable = $field['search_table'];
457 if ($joinTable) {
458 $joinClause = 1;
459 $joinTableAlias = $joinTable;
460 // Set location-specific query
461 if (isset($this->_locationSpecificCustomFields[$field['id']])) {
462 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$field['id']];
463 $joinTableAlias = "$locationType-address";
464 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
465 }
466 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
467 if (!empty($this->_ids[$field['id']])) {
468 $this->_whereTables[$name] = $this->_tables[$name];
469 }
470 if ($joinTable !== 'contact_a') {
471 $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause;
472 }
473 }
474 }
475
476 }