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