Merge pull request #3211 from eileenmcnaughton/comments
[civicrm-core.git] / CRM / Core / BAO / CustomQuery.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
33 * @copyright CiviCRM LLC (c) 2004-2014
34 * $Id$
35 *
36 */
37 class CRM_Core_BAO_CustomQuery {
38 CONST PREFIX = 'custom_value_';
39
40 /**
41 * the set of custom field ids
42 *
43 * @var array
44 */
45 protected $_ids;
46
47 /**
48 * the select clause
49 *
50 * @var array
51 */
52 public $_select;
53
54 /**
55 * the name of the elements that are in the select clause
56 * used to extract the values
57 *
58 * @var array
59 */
60 public $_element;
61
62 /**
63 * the tables involved in the query
64 *
65 * @var array
66 */
67 public $_tables;
68 public $_whereTables;
69
70 /**
71 * the where clause
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
105 protected $_locationSpecificCustomFields;
106
107 /**
108 * This stores custom data group types and tables that it extends
109 *
110 * @todo add comments explaining why survey & campaign are missing from this
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 /**
133 * class constructor
134 *
135 * Takes in a set of custom field ids andsets up the data structures to
136 * generate a query
137 *
138 * @param array $ids the set of custom field ids
139 *
140 * @access public
141 */
142 function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) {
143 $this->_ids = &$ids;
144 $this->_locationSpecificCustomFields = $locationSpecificFields;
145
146 $this->_select = array();
147 $this->_element = array();
148 $this->_tables = array();
149 $this->_whereTables = array();
150 $this->_where = array();
151 $this->_qill = array();
152 $this->_options = array();
153
154 $this->_fields = array();
155 $this->_contactSearch = $contactSearch;
156
157 if (empty($this->_ids)) {
158 return;
159 }
160
161 // initialize the field array
162 $tmpArray = array_keys($this->_ids);
163 $idString = implode(',', $tmpArray);
164 $query = "
165 SELECT f.id, f.label, f.data_type,
166 f.html_type, f.is_search_range,
167 f.option_group_id, f.custom_group_id,
168 f.column_name, g.table_name,
169 f.date_format,f.time_format
170 FROM civicrm_custom_field f,
171 civicrm_custom_group g
172 WHERE f.custom_group_id = g.id
173 AND g.is_active = 1
174 AND f.is_active = 1
175 AND f.id IN ( $idString )";
176
177 $dao = CRM_Core_DAO::executeQuery($query);
178 while ($dao->fetch()) {
179 // get the group dao to figure which class this custom field extends
180 $extends = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id, 'extends');
181 if (array_key_exists($extends, self::$extendsMap)) {
182 $extendsTable = self::$extendsMap[$extends];
183 }
184 elseif (in_array($extends, CRM_Contact_BAO_ContactType::subTypes())) {
185 // if $extends is a subtype, refer contact table
186 $extendsTable = self::$extendsMap['Contact'];
187 }
188 $this->_fields[$dao->id] = array(
189 'id' => $dao->id,
190 'label' => $dao->label,
191 'extends' => $extendsTable,
192 'data_type' => $dao->data_type,
193 'html_type' => $dao->html_type,
194 'is_search_range' => $dao->is_search_range,
195 'column_name' => $dao->column_name,
196 'table_name' => $dao->table_name,
197 'option_group_id' => $dao->option_group_id,
198 );
199
200 // store it in the options cache to make things easier
201 // during option lookup
202 $this->_options[$dao->id] = array();
203 $this->_options[$dao->id]['attributes'] = array(
204 'label' => $dao->label,
205 'data_type' => $dao->data_type,
206 'html_type' => $dao->html_type,
207 );
208
209 $optionGroupID = NULL;
210 $htmlTypes = array('CheckBox', 'Radio', 'Select', 'Multi-Select', 'AdvMulti-Select', 'Autocomplete-Select');
211 if (in_array($dao->html_type, $htmlTypes) && $dao->data_type != 'ContactReference') {
212 if ($dao->option_group_id) {
213 $optionGroupID = $dao->option_group_id;
214 }
215 elseif ($dao->data_type != 'Boolean') {
216 $errorMessage = ts("The custom field %1 is corrupt. Please delete and re-build the field",
217 array(1 => $dao->label)
218 );
219 CRM_Core_Error::fatal($errorMessage);
220 }
221 }
222 elseif ($dao->html_type == 'Select Date') {
223 $this->_options[$dao->id]['attributes']['date_format'] = $dao->date_format;
224 $this->_options[$dao->id]['attributes']['time_format'] = $dao->time_format;
225 }
226
227 // build the cache for custom values with options (label => value)
228 if ($optionGroupID != NULL) {
229 $query = "
230 SELECT label, value
231 FROM civicrm_option_value
232 WHERE option_group_id = $optionGroupID
233 ";
234
235 $option = CRM_Core_DAO::executeQuery($query);
236 while ($option->fetch()) {
237 $dataType = $this->_fields[$dao->id]['data_type'];
238 if ($dataType == 'Int' || $dataType == 'Float') {
239 $num = round($option->value, 2);
240 $this->_options[$dao->id]["$num"] = $option->label;
241 }
242 else {
243 $this->_options[$dao->id][$option->value] = $option->label;
244 }
245 }
246 $options = $this->_options[$dao->id];
247 //unset attributes to avoid confussion
248 unset($options['attributes']);
249 CRM_Utils_Hook::customFieldOptions($dao->id, $options, FALSE);
250 }
251 }
252 }
253
254 /**
255 * generate the select clause and the associated tables
256 * for the from clause
257 *
258 * @param NULL
259 *
260 * @return void
261 * @access public
262 */
263 function select() {
264 if (empty($this->_fields)) {
265 return;
266 }
267
268 foreach ($this->_fields as $id => $field) {
269 $name = $field['table_name'];
270 $fieldName = 'custom_' . $field['id'];
271 $this->_select["{$name}_id"] = "{$name}.id as {$name}_id";
272 $this->_element["{$name}_id"] = 1;
273 $this->_select[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
274 $this->_element[$fieldName] = 1;
275 $joinTable = NULL;
276 // CRM-14265
277 if ($field['extends'] == 'civicrm_group') {
278 return;
279 }
280 elseif ($field['extends'] == 'civicrm_contact') {
281 $joinTable = 'contact_a';
282 }
283 elseif ($field['extends'] == 'civicrm_contribution') {
284 $joinTable = $field['extends'];
285 }
286 elseif (in_array($field['extends'], self::$extendsMap)) {
287 $joinTable = $field['extends'];
288 }
289 else {
290 return;
291 }
292
293 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id";
294
295 if ($this->_ids[$id]) {
296 $this->_whereTables[$name] = $this->_tables[$name];
297 }
298
299 if ($joinTable) {
300 $joinClause = 1;
301 $joinTableAlias = $joinTable;
302 // Set location-specific query
303 if (isset($this->_locationSpecificCustomFields[$id])) {
304 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$id];
305 $joinTableAlias = "$locationType-address";
306 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
307 }
308 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
309 if ($this->_ids[$id]) {
310 $this->_whereTables[$name] = $this->_tables[$name];
311 }
312 if ($joinTable != 'contact_a') {
313 $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause;
314 }
315 elseif ($this->_contactSearch) {
316 CRM_Contact_BAO_Query::$_openedPanes[ts('Custom Fields')] = TRUE;
317 }
318 }
319 }
320 }
321
322 /**
323 * generate the where clause and also the english language
324 * equivalent
325 *
326 * @param NULL
327 *
328 * @return void
329 *
330 * @access public
331 */
332 function where() {
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
347 // fix $value here to escape sql injection attacks
348 $field = $this->_fields[$id];
349 $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value, $id, $this->_options);
350
351 if (!is_array($value)) {
352 $value = CRM_Core_DAO::escapeString(trim($value));
353 }
354
355 $fieldName = "{$field['table_name']}.{$field['column_name']}";
356 switch ($field['data_type']) {
357 case 'String':
358 $sql = "$fieldName";
359 // if we are coming in from listings,
360 // for checkboxes the value is already in the right format and is NOT an array
361 if (is_array($value)) {
362
363 //ignoring $op value for checkbox and multi select
364 $sqlValue = array();
365 $sqlOP = ' AND ';
366 $sqlOPlabel = ts('match ALL');
367 if ($field['html_type'] == 'CheckBox') {
368 foreach ($value as $k => $v) {
369 if ($v) {
370 if ($k == 'CiviCRM_OP_OR') {
371 $sqlOP = ' OR ';
372 $sqlOPlabel = ts('match ANY');
373 continue;
374 }
375
376 $sqlValue[] = "( $sql like '%" . CRM_Core_DAO::VALUE_SEPARATOR . $k . CRM_Core_DAO::VALUE_SEPARATOR . "%' ) ";
377 }
378 }
379 //if user check only 'CiviCRM_OP_OR' check box
380 //of custom checkbox field, then ignore this field.
381 if (!empty($sqlValue)) {
382 $this->_where[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
383 $this->_qill[$grouping][] = "{$field['label']} $op $qillValue ( $sqlOPlabel )";
384 }
385 // for multi select
386 }
387 else {
388 foreach ($value as $k => $v) {
389 if ($v == 'CiviCRM_OP_OR') {
390 $sqlOP = ' OR ';
391 $sqlOPlabel = ts('match ANY');
392 continue;
393 }
394 $v = CRM_Core_DAO::escapeString($v);
395 $sqlValue[] = "( $sql like '%" . CRM_Core_DAO::VALUE_SEPARATOR . $v . CRM_Core_DAO::VALUE_SEPARATOR . "%' ) ";
396 }
397 //if user select only 'CiviCRM_OP_OR' value
398 //of custom multi select field, then ignore this field.
399 if (!empty($sqlValue)) {
400 $this->_where[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
401 $this->_qill[$grouping][] = "$field[label] $op $qillValue ( $sqlOPlabel )";
402 }
403 }
404 }
405 else {
406 if ($field['is_search_range'] && is_array($value)) {
407 $this->searchRange($field['id'],
408 $field['label'],
409 $field['data_type'],
410 $fieldName,
411 $value,
412 $grouping
413 );
414 }
415 else {
416 if (in_array($field['html_type'], array('Select', 'Radio', 'Autocomplete-Select'))) {
417 $wildcard = FALSE;
418 $val = CRM_Utils_Type::escape($value, 'String');
419 }
420 else {
421 $val = CRM_Utils_Type::escape($strtolower(trim($value)), 'String');
422 }
423
424 if ($wildcard) {
425 $val = $strtolower(CRM_Core_DAO::escapeString($val));
426 $val = "%$val%";
427 $op = 'LIKE';
428 }
429
430 //FIX for custom data query fired against no value(NULL/NOT NULL)
431 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($sql, $op, $val, $field['data_type']);
432 $this->_qill[$grouping][] = "$field[label] $op $qillValue";
433 }
434 }
435 continue;
436
437 case 'ContactReference':
438 $label = $value ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
439 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
440 $this->_qill[$grouping][] = $field['label'] . " $op $label";
441 continue;
442
443 case 'Int':
444 if ($field['is_search_range'] && is_array($value)) {
445 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
446 }
447 else {
448 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
449 $this->_qill[$grouping][] = $field['label'] . " $op $value";
450 }
451 continue;
452
453 case 'Boolean':
454 if (strtolower($value) == 'yes' || strtolower($value) == strtolower(ts('Yes'))) {
455 $value = 1;
456 }
457 else {
458 $value = (int) $value;
459 }
460 $value = ($value == 1) ? 1 : 0;
461 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
462 $value = $value ? ts('Yes') : ts('No');
463 $this->_qill[$grouping][] = $field['label'] . " {$op} {$value}";
464 continue;
465
466 case 'Link':
467 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
468 $this->_qill[$grouping][] = $field['label'] . " $op $value";
469 continue;
470
471 case 'Float':
472 if ($field['is_search_range'] && is_array($value)) {
473 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
474 }
475 else {
476 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
477 $this->_qill[$grouping][] = $field['label'] . " {$op} {$value}";
478 }
479 continue;
480
481 case 'Money':
482 if ($field['is_search_range'] && is_array($value)) {
483 foreach ($value as $key => $val) {
484 $moneyFormat = CRM_Utils_Rule::cleanMoney($value[$key]);
485 $value[$key] = $moneyFormat;
486 }
487 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
488 }
489 else {
490 $moneyFormat = CRM_Utils_Rule::cleanMoney($value);
491 $value = $moneyFormat;
492 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
493 $this->_qill[$grouping][] = $field['label'] . " {$op} {$value}";
494 }
495 continue;
496
497 case 'Memo':
498 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
499 $this->_qill[$grouping][] = "$field[label] $op $value";
500 continue;
501
502 case 'Date':
503 $fromValue = CRM_Utils_Array::value('from', $value);
504 $toValue = CRM_Utils_Array::value('to', $value);
505
506 if (!$fromValue && !$toValue) {
507 if (!CRM_Utils_Date::processDate($value) && $op != 'IS NULL' && $op != 'IS NOT NULL') {
508 continue;
509 }
510
511 // hack to handle yy format during search
512 if (is_numeric($value) && strlen($value) == 4) {
513 $value = "01-01-{$value}";
514 }
515
516 $date = CRM_Utils_Date::processDate($value);
517 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $date, 'String');
518 $this->_qill[$grouping][] = $field['label'] . " {$op} " . CRM_Utils_Date::customFormat($date);
519 }
520 else {
521 if (is_numeric($fromValue) && strlen($fromValue) == 4) {
522 $fromValue = "01-01-{$fromValue}";
523 }
524
525 if (is_numeric($toValue) && strlen($toValue) == 4) {
526 $toValue = "01-01-{$toValue}";
527 }
528
529 // TO DO: add / remove time based on date parts
530 $fromDate = CRM_Utils_Date::processDate($fromValue);
531 $toDate = CRM_Utils_Date::processDate($toValue);
532 if (!$fromDate && !$toDate) {
533 continue;
534 }
535 if ($fromDate) {
536 $this->_where[$grouping][] = "$fieldName >= $fromDate";
537 $this->_qill[$grouping][] = $field['label'] . ' >= ' . CRM_Utils_Date::customFormat($fromDate);
538 }
539 if ($toDate) {
540 $this->_where[$grouping][] = "$fieldName <= $toDate";
541 $this->_qill[$grouping][] = $field['label'] . ' <= ' . CRM_Utils_Date::customFormat($toDate);
542 }
543 }
544 continue;
545
546 case 'StateProvince':
547 case 'Country':
548 if (!is_array($value)) {
549 $this->_where[$grouping][] = "$fieldName {$op} " . CRM_Utils_Type::escape($value, 'Int');
550 $this->_qill[$grouping][] = $field['label'] . " {$op} {$qillValue}";
551 }
552 else {
553 $sqlOP = ' AND ';
554 $sqlOPlabel = ts('match ALL');
555 foreach ($value as $k => $v) {
556 if ($v == 'CiviCRM_OP_OR') {
557 $sqlOP = ' OR ';
558 $sqlOPlabel = ts('match ANY');
559 continue;
560 }
561 $sqlValue[] = "( $fieldName like '%" . CRM_Core_DAO::VALUE_SEPARATOR . $v . CRM_Core_DAO::VALUE_SEPARATOR . "%' ) ";
562 }
563
564 //if user select only 'CiviCRM_OP_OR' value
565 //of custom multi select field, then ignore this field.
566 if (!empty($sqlValue)) {
567 $this->_where[$grouping][] = " ( " . implode($sqlOP, $sqlValue) . " ) ";
568 $this->_qill[$grouping][] = "$field[label] $op $qillValue ( $sqlOPlabel )";
569 }
570 }
571 continue;
572
573 case 'File':
574 if ( $op == 'IS NULL' || $op == 'IS NOT NULL' || $op == 'IS EMPTY' || $op == 'IS NOT EMPTY' ) {
575 switch ($op) {
576 case 'IS EMPTY':
577 $op = 'IS NULL';
578 break;
579 case 'IS NOT EMPTY':
580 $op = 'IS NOT NULL';
581 break;
582 }
583 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op);
584 $this->_qill[$grouping][] = $field['label'] . " {$op} ";
585 }
586 continue;
587 }
588 }
589 }
590 }
591
592 /**
593 * function that does the actual query generation
594 * basically ties all the above functions together
595 *
596 * @param NULL
597 *
598 * @return array array of strings
599 * @access public
600 */
601 function query() {
602 $this->select();
603
604 $this->where();
605
606 $whereStr = NULL;
607 if (!empty($this->_where)) {
608 $clauses = array();
609 foreach ($this->_where as $grouping => $values) {
610 if (!empty($values)) {
611 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
612 }
613 }
614 if (!empty($clauses)) {
615 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
616 }
617 }
618
619 return array(implode(' , ', $this->_select),
620 implode(' ', $this->_tables),
621 $whereStr,
622 );
623 }
624
625 function searchRange(&$id, &$label, $type, $fieldName, &$value, &$grouping) {
626 $qill = array();
627
628 if (isset($value['from'])) {
629 $val = CRM_Utils_Type::escape($value['from'], $type);
630
631 if ($type == 'String') {
632 $this->_where[$grouping][] = "$fieldName >= '$val'";
633 }
634 else {
635 $this->_where[$grouping][] = "$fieldName >= $val";
636 }
637 $qill[] = ts('greater than or equal to \'%1\'', array(1 => $value['from']));
638 }
639
640 if (isset($value['to'])) {
641 $val = CRM_Utils_Type::escape($value['to'], $type);
642 if ($type == 'String') {
643 $this->_where[$grouping][] = "$fieldName <= '$val'";
644 }
645 else {
646 $this->_where[$grouping][] = "$fieldName <= $val";
647 }
648 $qill[] = ts('less than or equal to \'%1\'', array(1 => $value['to']));
649 }
650
651 if (!empty($qill)) {
652 $this->_qill[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);
653 }
654 }
655 }