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