CRM-15495 - Improve entityRef contact_type filter
[civicrm-core.git] / CRM / Core / BAO / CustomQuery.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 *
32 * @package CRM
06b69b18 33 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
34 * $Id$
35 *
36 */
37class CRM_Core_BAO_CustomQuery {
7da04cde 38 const PREFIX = 'custom_value_';
6a488035
TO
39
40 /**
100fef9d 41 * The set of custom field ids
6a488035
TO
42 *
43 * @var array
44 */
45 protected $_ids;
46
47 /**
100fef9d 48 * The select clause
6a488035
TO
49 *
50 * @var array
51 */
52 public $_select;
53
54 /**
100fef9d 55 * The name of the elements that are in the select clause
6a488035
TO
56 * used to extract the values
57 *
58 * @var array
59 */
60 public $_element;
61
62 /**
100fef9d 63 * The tables involved in the query
6a488035
TO
64 *
65 * @var array
66 */
67 public $_tables;
68 public $_whereTables;
69
70 /**
100fef9d 71 * The where clause
6a488035
TO
72 *
73 * @var array
74 */
75 public $_where;
76
77 /**
78 * The english language version of the query
79 *
80 * @var array
81 */
82 public $_qill;
83
84 /**
85 * The cache to translate the option values into labels
86 *
87 * @var array
88 */
89 public $_options;
90
91 /**
92 * The custom fields information
93 *
94 * @var array
95 */
96 public $_fields;
97
98 /**
99 * Searching for contacts?
100 *
101 * @var boolean
102 */
103 protected $_contactSearch;
104
247ad911 105 protected $_locationSpecificCustomFields;
442df34b 106
6a488035
TO
107 /**
108 * This stores custom data group types and tables that it extends
109 *
dcf0d348 110 * @todo add comments explaining why survey & campaign are missing from this
6a488035
TO
111 * @var array
112 * @static
113 */
114 static $extendsMap = array(
115 'Contact' => 'civicrm_contact',
116 'Individual' => 'civicrm_contact',
117 'Household' => 'civicrm_contact',
118 'Organization' => 'civicrm_contact',
119 'Contribution' => 'civicrm_contribution',
120 'Membership' => 'civicrm_membership',
121 'Participant' => 'civicrm_participant',
122 'Group' => 'civicrm_group',
123 'Relationship' => 'civicrm_relationship',
124 'Event' => 'civicrm_event',
125 'Case' => 'civicrm_case',
126 'Activity' => 'civicrm_activity',
127 'Pledge' => 'civicrm_pledge',
128 'Grant' => 'civicrm_grant',
129 'Address' => 'civicrm_address',
130 );
131
132 /**
100fef9d 133 * Class constructor
6a488035
TO
134 *
135 * Takes in a set of custom field ids andsets up the data structures to
136 * generate a query
137 *
fd31fa4c
EM
138 * @param array $ids the set of custom field ids
139 *
140 * @param bool $contactSearch
141 * @param array $locationSpecificFields
6a488035 142 *
442df34b 143 */
00be9182 144 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) {
6a488035 145 $this->_ids = &$ids;
247ad911 146 $this->_locationSpecificCustomFields = $locationSpecificFields;
6a488035
TO
147
148 $this->_select = array();
149 $this->_element = array();
150 $this->_tables = array();
151 $this->_whereTables = array();
152 $this->_where = array();
153 $this->_qill = array();
154 $this->_options = array();
155
156 $this->_fields = array();
157 $this->_contactSearch = $contactSearch;
158
159 if (empty($this->_ids)) {
160 return;
161 }
162
163 // initialize the field array
164 $tmpArray = array_keys($this->_ids);
165 $idString = implode(',', $tmpArray);
166 $query = "
167SELECT f.id, f.label, f.data_type,
168 f.html_type, f.is_search_range,
169 f.option_group_id, f.custom_group_id,
170 f.column_name, g.table_name,
171 f.date_format,f.time_format
172 FROM civicrm_custom_field f,
173 civicrm_custom_group g
174 WHERE f.custom_group_id = g.id
175 AND g.is_active = 1
176 AND f.is_active = 1
177 AND f.id IN ( $idString )";
178
179 $dao = CRM_Core_DAO::executeQuery($query);
180 while ($dao->fetch()) {
181 // get the group dao to figure which class this custom field extends
182 $extends = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id, 'extends');
183 if (array_key_exists($extends, self::$extendsMap)) {
184 $extendsTable = self::$extendsMap[$extends];
185 }
186 elseif (in_array($extends, CRM_Contact_BAO_ContactType::subTypes())) {
187 // if $extends is a subtype, refer contact table
188 $extendsTable = self::$extendsMap['Contact'];
189 }
190 $this->_fields[$dao->id] = array(
191 'id' => $dao->id,
192 'label' => $dao->label,
193 'extends' => $extendsTable,
194 'data_type' => $dao->data_type,
195 'html_type' => $dao->html_type,
196 'is_search_range' => $dao->is_search_range,
197 'column_name' => $dao->column_name,
198 'table_name' => $dao->table_name,
199 'option_group_id' => $dao->option_group_id,
200 );
201
202 // store it in the options cache to make things easier
203 // during option lookup
204 $this->_options[$dao->id] = array();
205 $this->_options[$dao->id]['attributes'] = array(
206 'label' => $dao->label,
207 'data_type' => $dao->data_type,
208 'html_type' => $dao->html_type,
209 );
210
211 $optionGroupID = NULL;
212 $htmlTypes = array('CheckBox', 'Radio', 'Select', 'Multi-Select', 'AdvMulti-Select', 'Autocomplete-Select');
213 if (in_array($dao->html_type, $htmlTypes) && $dao->data_type != 'ContactReference') {
214 if ($dao->option_group_id) {
215 $optionGroupID = $dao->option_group_id;
216 }
217 elseif ($dao->data_type != 'Boolean') {
218 $errorMessage = ts("The custom field %1 is corrupt. Please delete and re-build the field",
219 array(1 => $dao->label)
220 );
221 CRM_Core_Error::fatal($errorMessage);
222 }
223 }
224 elseif ($dao->html_type == 'Select Date') {
225 $this->_options[$dao->id]['attributes']['date_format'] = $dao->date_format;
226 $this->_options[$dao->id]['attributes']['time_format'] = $dao->time_format;
227 }
228
229 // build the cache for custom values with options (label => value)
230 if ($optionGroupID != NULL) {
231 $query = "
232SELECT label, value
233 FROM civicrm_option_value
234 WHERE option_group_id = $optionGroupID
235";
236
237 $option = CRM_Core_DAO::executeQuery($query);
238 while ($option->fetch()) {
239 $dataType = $this->_fields[$dao->id]['data_type'];
240 if ($dataType == 'Int' || $dataType == 'Float') {
241 $num = round($option->value, 2);
242 $this->_options[$dao->id]["$num"] = $option->label;
243 }
244 else {
245 $this->_options[$dao->id][$option->value] = $option->label;
246 }
247 }
248 $options = $this->_options[$dao->id];
249 //unset attributes to avoid confussion
250 unset($options['attributes']);
251 CRM_Utils_Hook::customFieldOptions($dao->id, $options, FALSE);
252 }
253 }
254 }
255
256 /**
100fef9d 257 * Generate the select clause and the associated tables
6a488035
TO
258 * for the from clause
259 *
260 * @param NULL
261 *
262 * @return void
6a488035 263 */
00be9182 264 public function select() {
6a488035
TO
265 if (empty($this->_fields)) {
266 return;
267 }
268
269 foreach ($this->_fields as $id => $field) {
270 $name = $field['table_name'];
271 $fieldName = 'custom_' . $field['id'];
272 $this->_select["{$name}_id"] = "{$name}.id as {$name}_id";
273 $this->_element["{$name}_id"] = 1;
274 $this->_select[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
275 $this->_element[$fieldName] = 1;
276 $joinTable = NULL;
dcf0d348
PJ
277 // CRM-14265
278 if ($field['extends'] == 'civicrm_group') {
279 return;
280 }
281 elseif ($field['extends'] == 'civicrm_contact') {
6a488035
TO
282 $joinTable = 'contact_a';
283 }
284 elseif ($field['extends'] == 'civicrm_contribution') {
dcf0d348 285 $joinTable = $field['extends'];
6a488035 286 }
dcf0d348
PJ
287 elseif (in_array($field['extends'], self::$extendsMap)) {
288 $joinTable = $field['extends'];
6a488035 289 }
dcf0d348
PJ
290 else {
291 return;
6a488035 292 }
dcf0d348
PJ
293
294 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id";
295
296 if ($this->_ids[$id]) {
297 $this->_whereTables[$name] = $this->_tables[$name];
6a488035
TO
298 }
299
300 if ($joinTable) {
442df34b
CW
301 $joinClause = 1;
302 $joinTableAlias = $joinTable;
303 // Set location-specific query
247ad911 304 if (isset($this->_locationSpecificCustomFields[$id])) {
305 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$id];
442df34b
CW
306 $joinTableAlias = "$locationType-address";
307 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
308 }
309 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
6a488035
TO
310 if ($this->_ids[$id]) {
311 $this->_whereTables[$name] = $this->_tables[$name];
312 }
313 if ($joinTable != 'contact_a') {
442df34b 314 $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause;
6a488035
TO
315 }
316 elseif ($this->_contactSearch) {
317 CRM_Contact_BAO_Query::$_openedPanes[ts('Custom Fields')] = TRUE;
318 }
319 }
320 }
321 }
322
323 /**
100fef9d 324 * Generate the where clause and also the english language
6a488035
TO
325 * equivalent
326 *
327 * @param NULL
328 *
329 * @return void
330 *
6a488035 331 */
00be9182 332 public function where() {
6a488035
TO
333 foreach ($this->_ids as $id => $values) {
334
335 // Fixed for Isuue CRM 607
336 if (CRM_Utils_Array::value($id, $this->_fields) === NULL ||
337 !$values
338 ) {
339 continue;
340 }
341
342 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
343
344 foreach ($values as $tuple) {
345 list($name, $op, $value, $grouping, $wildcard) = $tuple;
346
6a488035 347 $field = $this->_fields[$id];
6a488035 348
3130209f
CW
349 $fieldName = "{$field['table_name']}.{$field['column_name']}";
350
9f388466
CW
351 // Autocomplete comes back as a string not an array
352 if ($field['data_type'] == 'String' && $field['html_type'] == 'Autocomplete-Select' && $op == '=') {
353 $value = explode(',', $value);
354 }
355
3130209f
CW
356 // Handle multi-select search for any data type
357 if (is_array($value) && !$field['is_search_range']) {
358 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($field);
359 $wildcard = $isSerialized ? $wildcard : TRUE;
360 $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array('field' => $name, 'context' => 'search'), array()));
361 $qillValue = '';
362 $sqlOP = $wildcard ? ' OR ' : ' AND ';
363 $sqlValue = array();
364 foreach ($value as $num => &$v) {
365 $sep = count($value) > (1 + $num) ? ', ' : (' ' . ($wildcard ? ts('OR') : ts('AND')) . ' ');
366 $qillValue .= ($num ? $sep : '') . $options[$v];
367 $v = CRM_Core_DAO::escapeString($v);
368 if ($isSerialized) {
369 $sqlValue[] = "( $fieldName like '%" . CRM_Core_DAO::VALUE_SEPARATOR . $v . CRM_Core_DAO::VALUE_SEPARATOR . "%' ) ";
370 }
371 else {
372 $v = "'$v'";
373 }
374 }
375 if (!$isSerialized) {
376 $sqlValue = array("$fieldName IN (" . implode(',', $value) . ")");
377 }
378 $this->_where[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
379 $this->_qill[$grouping][] = "$field[label] $op $qillValue";
380 continue;
6a488035
TO
381 }
382
3130209f 383 // fix $value here to escape sql injection attacks
9f388466
CW
384 if (!is_array($value)) {
385 $value = CRM_Core_DAO::escapeString(trim($value));
386 }
3130209f
CW
387
388 $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value, $id, $this->_options);
389
6a488035
TO
390 switch ($field['data_type']) {
391 case 'String':
392 $sql = "$fieldName";
3130209f
CW
393
394 if ($field['is_search_range'] && is_array($value)) {
395 $this->searchRange($field['id'],
396 $field['label'],
397 $field['data_type'],
398 $fieldName,
399 $value,
400 $grouping
401 );
6a488035
TO
402 }
403 else {
9f388466 404 $val = CRM_Utils_Type::escape($strtolower(trim($value)), 'String');
3130209f
CW
405
406 if ($wildcard) {
407 $val = $strtolower(CRM_Core_DAO::escapeString($val));
408 $val = "%$val%";
409 $op = 'LIKE';
6a488035 410 }
3130209f
CW
411
412 //FIX for custom data query fired against no value(NULL/NOT NULL)
413 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($sql, $op, $val, $field['data_type']);
414 $this->_qill[$grouping][] = "$field[label] $op $qillValue";
6a488035 415 }
3130209f 416 break;
6a488035
TO
417
418 case 'ContactReference':
419 $label = $value ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
420 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
421 $this->_qill[$grouping][] = $field['label'] . " $op $label";
3130209f 422 break;
6a488035
TO
423
424 case 'Int':
425 if ($field['is_search_range'] && is_array($value)) {
426 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
427 }
428 else {
429 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
430 $this->_qill[$grouping][] = $field['label'] . " $op $value";
431 }
3130209f 432 break;
6a488035
TO
433
434 case 'Boolean':
435 if (strtolower($value) == 'yes' || strtolower($value) == strtolower(ts('Yes'))) {
436 $value = 1;
437 }
438 else {
439 $value = (int) $value;
440 }
441 $value = ($value == 1) ? 1 : 0;
442 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
443 $value = $value ? ts('Yes') : ts('No');
444 $this->_qill[$grouping][] = $field['label'] . " {$op} {$value}";
3130209f 445 break;
6a488035
TO
446
447 case 'Link':
448 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
449 $this->_qill[$grouping][] = $field['label'] . " $op $value";
3130209f 450 break;
6a488035
TO
451
452 case 'Float':
453 if ($field['is_search_range'] && is_array($value)) {
454 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
455 }
456 else {
457 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
458 $this->_qill[$grouping][] = $field['label'] . " {$op} {$value}";
459 }
3130209f 460 break;
6a488035
TO
461
462 case 'Money':
463 if ($field['is_search_range'] && is_array($value)) {
464 foreach ($value as $key => $val) {
465 $moneyFormat = CRM_Utils_Rule::cleanMoney($value[$key]);
466 $value[$key] = $moneyFormat;
467 }
468 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
469 }
470 else {
471 $moneyFormat = CRM_Utils_Rule::cleanMoney($value);
472 $value = $moneyFormat;
473 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
474 $this->_qill[$grouping][] = $field['label'] . " {$op} {$value}";
475 }
3130209f 476 break;
6a488035
TO
477
478 case 'Memo':
479 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
480 $this->_qill[$grouping][] = "$field[label] $op $value";
3130209f 481 break;
6a488035
TO
482
483 case 'Date':
484 $fromValue = CRM_Utils_Array::value('from', $value);
485 $toValue = CRM_Utils_Array::value('to', $value);
486
487 if (!$fromValue && !$toValue) {
488 if (!CRM_Utils_Date::processDate($value) && $op != 'IS NULL' && $op != 'IS NOT NULL') {
489 continue;
490 }
491
492 // hack to handle yy format during search
493 if (is_numeric($value) && strlen($value) == 4) {
494 $value = "01-01-{$value}";
495 }
496
497 $date = CRM_Utils_Date::processDate($value);
498 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $date, 'String');
499 $this->_qill[$grouping][] = $field['label'] . " {$op} " . CRM_Utils_Date::customFormat($date);
500 }
501 else {
502 if (is_numeric($fromValue) && strlen($fromValue) == 4) {
503 $fromValue = "01-01-{$fromValue}";
504 }
505
506 if (is_numeric($toValue) && strlen($toValue) == 4) {
507 $toValue = "01-01-{$toValue}";
508 }
509
510 // TO DO: add / remove time based on date parts
511 $fromDate = CRM_Utils_Date::processDate($fromValue);
512 $toDate = CRM_Utils_Date::processDate($toValue);
513 if (!$fromDate && !$toDate) {
514 continue;
515 }
516 if ($fromDate) {
517 $this->_where[$grouping][] = "$fieldName >= $fromDate";
518 $this->_qill[$grouping][] = $field['label'] . ' >= ' . CRM_Utils_Date::customFormat($fromDate);
519 }
520 if ($toDate) {
521 $this->_where[$grouping][] = "$fieldName <= $toDate";
522 $this->_qill[$grouping][] = $field['label'] . ' <= ' . CRM_Utils_Date::customFormat($toDate);
523 }
524 }
3130209f 525 break;
6a488035
TO
526
527 case 'StateProvince':
528 case 'Country':
3130209f
CW
529 $this->_where[$grouping][] = "$fieldName {$op} " . CRM_Utils_Type::escape($value, 'Int');
530 $this->_qill[$grouping][] = $field['label'] . " {$op} {$qillValue}";
531 break;
6a488035
TO
532
533 case 'File':
534 if ( $op == 'IS NULL' || $op == 'IS NOT NULL' || $op == 'IS EMPTY' || $op == 'IS NOT EMPTY' ) {
535 switch ($op) {
536 case 'IS EMPTY':
537 $op = 'IS NULL';
538 break;
539 case 'IS NOT EMPTY':
540 $op = 'IS NOT NULL';
541 break;
542 }
543 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op);
544 $this->_qill[$grouping][] = $field['label'] . " {$op} ";
545 }
3130209f 546 break;
6a488035
TO
547 }
548 }
549 }
550 }
551
552 /**
100fef9d 553 * Function that does the actual query generation
6a488035
TO
554 * basically ties all the above functions together
555 *
556 * @param NULL
557 *
558 * @return array array of strings
6a488035 559 */
00be9182 560 public function query() {
6a488035
TO
561 $this->select();
562
563 $this->where();
564
565 $whereStr = NULL;
566 if (!empty($this->_where)) {
567 $clauses = array();
568 foreach ($this->_where as $grouping => $values) {
569 if (!empty($values)) {
570 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
571 }
572 }
573 if (!empty($clauses)) {
574 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
575 }
576 }
577
578 return array(implode(' , ', $this->_select),
579 implode(' ', $this->_tables),
580 $whereStr,
581 );
582 }
583
b5c2afd0 584 /**
100fef9d 585 * @param int $id
b5c2afd0
EM
586 * @param $label
587 * @param $type
100fef9d 588 * @param string $fieldName
b5c2afd0
EM
589 * @param $value
590 * @param $grouping
591 */
00be9182 592 public function searchRange(&$id, &$label, $type, $fieldName, &$value, &$grouping) {
6a488035
TO
593 $qill = array();
594
595 if (isset($value['from'])) {
596 $val = CRM_Utils_Type::escape($value['from'], $type);
597
598 if ($type == 'String') {
599 $this->_where[$grouping][] = "$fieldName >= '$val'";
600 }
601 else {
602 $this->_where[$grouping][] = "$fieldName >= $val";
603 }
604 $qill[] = ts('greater than or equal to \'%1\'', array(1 => $value['from']));
605 }
606
607 if (isset($value['to'])) {
608 $val = CRM_Utils_Type::escape($value['to'], $type);
609 if ($type == 'String') {
610 $this->_where[$grouping][] = "$fieldName <= '$val'";
611 }
612 else {
613 $this->_where[$grouping][] = "$fieldName <= $val";
614 }
615 $qill[] = ts('less than or equal to \'%1\'', array(1 => $value['to']));
616 }
617
618 if (!empty($qill)) {
619 $this->_qill[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);
620 }
621 }
622}