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