3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2015
36 class CRM_Core_BAO_CustomQuery
{
37 const PREFIX
= 'custom_value_';
40 * The set of custom field ids.
54 * The name of the elements that are in the select clause.
55 * used to extract the values
62 * The tables involved in the query.
77 * The english language version of the query.
84 * The cache to translate the option values into labels.
91 * The custom fields information.
98 * Searching for contacts?
102 protected $_contactSearch;
104 protected $_locationSpecificCustomFields;
107 * This stores custom data group types and tables that it extends.
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 'Campaign' => 'civicrm_campaign',
128 'Survey' => 'civicrm_survey',
134 * Takes in a set of custom field ids andsets up the data structures to
138 * The set of custom field ids.
140 * @param bool $contactSearch
141 * @param array $locationSpecificFields
143 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) {
145 $this->_locationSpecificCustomFields
= $locationSpecificFields;
147 $this->_select
= array();
148 $this->_element
= array();
149 $this->_tables
= array();
150 $this->_whereTables
= array();
151 $this->_where
= array();
152 $this->_qill
= array();
153 $this->_options
= array();
155 $this->_fields
= array();
156 $this->_contactSearch
= $contactSearch;
158 if (empty($this->_ids
)) {
162 // initialize the field array
163 $tmpArray = array_keys($this->_ids
);
164 $idString = implode(',', $tmpArray);
166 SELECT f.id, f.label, f.data_type,
167 f.html_type, f.is_search_range,
168 f.option_group_id, f.custom_group_id,
169 f.column_name, g.table_name,
170 f.date_format,f.time_format
171 FROM civicrm_custom_field f,
172 civicrm_custom_group g
173 WHERE f.custom_group_id = g.id
176 AND f.id IN ( $idString )";
178 $dao = CRM_Core_DAO
::executeQuery($query);
179 while ($dao->fetch()) {
180 // get the group dao to figure which class this custom field extends
181 $extends = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id
, 'extends');
182 if (array_key_exists($extends, self
::$extendsMap)) {
183 $extendsTable = self
::$extendsMap[$extends];
185 elseif (in_array($extends, CRM_Contact_BAO_ContactType
::subTypes())) {
186 // if $extends is a subtype, refer contact table
187 $extendsTable = self
::$extendsMap['Contact'];
189 $this->_fields
[$dao->id
] = array(
191 'label' => $dao->label
,
192 'extends' => $extendsTable,
193 'data_type' => $dao->data_type
,
194 'html_type' => $dao->html_type
,
195 'is_search_range' => $dao->is_search_range
,
196 'column_name' => $dao->column_name
,
197 'table_name' => $dao->table_name
,
198 'option_group_id' => $dao->option_group_id
,
201 // store it in the options cache to make things easier
202 // during option lookup
203 $this->_options
[$dao->id
] = array();
204 $this->_options
[$dao->id
]['attributes'] = array(
205 'label' => $dao->label
,
206 'data_type' => $dao->data_type
,
207 'html_type' => $dao->html_type
,
210 $optionGroupID = NULL;
211 $htmlTypes = array('CheckBox', 'Radio', 'Select', 'Multi-Select', 'AdvMulti-Select', 'Autocomplete-Select');
212 if (in_array($dao->html_type
, $htmlTypes) && $dao->data_type
!= 'ContactReference') {
213 if ($dao->option_group_id
) {
214 $optionGroupID = $dao->option_group_id
;
216 elseif ($dao->data_type
!= 'Boolean') {
217 $errorMessage = ts("The custom field %1 is corrupt. Please delete and re-build the field",
218 array(1 => $dao->label
)
220 CRM_Core_Error
::fatal($errorMessage);
223 elseif ($dao->html_type
== 'Select Date') {
224 $this->_options
[$dao->id
]['attributes']['date_format'] = $dao->date_format
;
225 $this->_options
[$dao->id
]['attributes']['time_format'] = $dao->time_format
;
228 // build the cache for custom values with options (label => value)
229 if ($optionGroupID != NULL) {
232 FROM civicrm_option_value
233 WHERE option_group_id = $optionGroupID
236 $option = CRM_Core_DAO
::executeQuery($query);
237 while ($option->fetch()) {
238 $dataType = $this->_fields
[$dao->id
]['data_type'];
239 if ($dataType == 'Int' ||
$dataType == 'Float') {
240 $num = round($option->value
, 2);
241 $this->_options
[$dao->id
]["$num"] = $option->label
;
244 $this->_options
[$dao->id
][$option->value
] = $option->label
;
247 $options = $this->_options
[$dao->id
];
248 //unset attributes to avoid confussion
249 unset($options['attributes']);
250 CRM_Utils_Hook
::customFieldOptions($dao->id
, $options, FALSE);
256 * Generate the select clause and the associated tables.
257 * for the from clause
261 public function select() {
262 if (empty($this->_fields
)) {
266 foreach ($this->_fields
as $id => $field) {
267 $name = $field['table_name'];
268 $fieldName = 'custom_' . $field['id'];
269 $this->_select
["{$name}_id"] = "{$name}.id as {$name}_id";
270 $this->_element
["{$name}_id"] = 1;
271 $this->_select
[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
272 $this->_element
[$fieldName] = 1;
275 if ($field['extends'] == 'civicrm_group') {
278 elseif ($field['extends'] == 'civicrm_contact') {
279 $joinTable = 'contact_a';
281 elseif ($field['extends'] == 'civicrm_contribution') {
282 $joinTable = $field['extends'];
284 elseif (in_array($field['extends'], self
::$extendsMap)) {
285 $joinTable = $field['extends'];
291 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id";
293 if ($this->_ids
[$id]) {
294 $this->_whereTables
[$name] = $this->_tables
[$name];
299 $joinTableAlias = $joinTable;
300 // Set location-specific query
301 if (isset($this->_locationSpecificCustomFields
[$id])) {
302 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields
[$id];
303 $joinTableAlias = "$locationType-address";
304 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
306 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
307 if ($this->_ids
[$id]) {
308 $this->_whereTables
[$name] = $this->_tables
[$name];
310 if ($joinTable != 'contact_a') {
311 $this->_whereTables
[$joinTableAlias] = $this->_tables
[$joinTableAlias] = $joinClause;
313 elseif ($this->_contactSearch
) {
314 CRM_Contact_BAO_Query
::$_openedPanes[ts('Custom Fields')] = TRUE;
321 * Generate the where clause and also the english language.
326 public function where() {
327 foreach ($this->_ids
as $id => $values) {
329 // Fixed for Isuue CRM 607
330 if (CRM_Utils_Array
::value($id, $this->_fields
) === NULL ||
336 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
338 foreach ($values as $tuple) {
339 list($name, $op, $value, $grouping, $wildcard) = $tuple;
341 $field = $this->_fields
[$id];
343 $fieldName = "{$field['table_name']}.{$field['column_name']}";
345 // Autocomplete comes back as a string not an array
346 if ($field['data_type'] == 'String' && $field['html_type'] == 'Autocomplete-Select' && $op == '=') {
347 $value = explode(',', $value);
350 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($field);
352 // Handle multi-select search for any data type
353 if (is_array($value) && !$field['is_search_range'] && $field['data_type'] != 'String') {
354 $wildcard = $isSerialized ?
$wildcard : TRUE;
355 $options = CRM_Utils_Array
::value('values', civicrm_api3('contact', 'getoptions', array(
357 'context' => 'search',
360 $sqlOP = $wildcard ?
' OR ' : ' AND ';
362 foreach ($value as $num => &$v) {
363 $sep = count($value) > (1 +
$num) ?
', ' : (' ' . ($wildcard ?
ts('OR') : ts('AND')) . ' ');
364 $qillValue .= ($num ?
$sep : '') . $options[$v];
365 $v = CRM_Core_DAO
::escapeString($v);
367 $sqlValue[] = "( $fieldName like '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $v . CRM_Core_DAO
::VALUE_SEPARATOR
. "%' ) ";
373 if (!$isSerialized) {
374 $sqlValue = array("$fieldName IN (" . implode(',', $value) . ")");
376 $this->_where
[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
377 $this->_qill
[$grouping][] = "$field[label] $qillOp $qillValue";
381 // fix $value here to escape sql injection attacks
383 if (!is_array($value)) {
384 $value = CRM_Core_DAO
::escapeString(trim($value));
385 $qillValue = CRM_Core_BAO_CustomField
::getDisplayValue($value, $id, $this->_options
);
387 elseif (count($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
389 $qillValue = CRM_Core_BAO_CustomField
::getDisplayValue($value[$op], $id, $this->_options
);
392 $qillValue = CRM_Core_BAO_CustomField
::getDisplayValue($value, $id, $this->_options
);
393 $value = array('IN' => $value);
396 $qillOp = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
398 switch ($field['data_type']) {
402 if ($field['is_search_range'] && is_array($value)) {
403 $this->searchRange($field['id'],
412 // fix $value here to escape sql injection attacks
413 if (!is_array($value)) {
414 $value = CRM_Utils_Type
::escape($strtolower($value), 'String');
416 elseif ($isSerialized) {
417 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
419 $value = $value[$op];
421 $value = implode(',', $value);
424 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
425 if ($isSerialized && !empty($value)) {
426 if (strstr($op, 'IN')) {
427 $value = str_replace(",", "[[:cntrl:]]*|[[:cntrl:]]*", $value);
428 $value = str_replace('(', '[[.left-parenthesis.]]', $value);
429 $value = str_replace(')', '[[.right-parenthesis.]]', $value);
431 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
432 $value = "[[:cntrl:]]*" . $value . "[[:cntrl:]]*";
434 $value = str_replace("[[:cntrl:]]*|", '', $value);
438 //FIX for custom data query fired against no value(NULL/NOT NULL)
439 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($sql, $op, $value, $field['data_type']);
440 $this->_qill
[$grouping][] = "$field[label] $qillOp $qillValue";
444 case 'ContactReference':
445 $label = $value ? CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
446 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
447 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $label";
451 if ($field['is_search_range'] && is_array($value)) {
452 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
455 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
456 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $value";
461 if (strtolower($value) == 'yes' ||
strtolower($value) == strtolower(ts('Yes'))) {
465 $value = (int) $value;
467 $value = ($value == 1) ?
1 : 0;
468 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
469 $value = $value ?
ts('Yes') : ts('No');
470 $this->_qill
[$grouping][] = $field['label'] . " $qillOp {$value}";
474 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
475 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $value";
479 if ($field['is_search_range'] && is_array($value)) {
480 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
483 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
484 $this->_qill
[$grouping][] = $field['label'] . " $qillOp {$value}";
489 if ($field['is_search_range'] && is_array($value)) {
490 foreach ($value as $key => $val) {
491 $moneyFormat = CRM_Utils_Rule
::cleanMoney($value[$key]);
492 $value[$key] = $moneyFormat;
494 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
497 $moneyFormat = CRM_Utils_Rule
::cleanMoney($value);
498 $value = $moneyFormat;
499 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
500 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} {$value}";
505 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
506 $this->_qill
[$grouping][] = "$field[label] $qillOp $value";
510 $fromValue = CRM_Utils_Array
::value('from', $value);
511 $toValue = CRM_Utils_Array
::value('to', $value);
513 if (!$fromValue && !$toValue) {
514 if (!CRM_Utils_Date
::processDate($value) && !in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
518 // hack to handle yy format during search
519 if (is_numeric($value) && strlen($value) == 4) {
520 $value = "01-01-{$value}";
523 $date = CRM_Utils_Date
::processDate($value);
524 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $date, 'String');
525 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} " . CRM_Utils_Date
::customFormat($date);
528 if (is_numeric($fromValue) && strlen($fromValue) == 4) {
529 $fromValue = "01-01-{$fromValue}";
532 if (is_numeric($toValue) && strlen($toValue) == 4) {
533 $toValue = "01-01-{$toValue}";
536 // TO DO: add / remove time based on date parts
537 $fromDate = CRM_Utils_Date
::processDate($fromValue);
538 $toDate = CRM_Utils_Date
::processDate($toValue);
539 if (!$fromDate && !$toDate) {
543 $this->_where
[$grouping][] = "$fieldName >= $fromDate";
544 $this->_qill
[$grouping][] = $field['label'] . ' >= ' . CRM_Utils_Date
::customFormat($fromDate);
547 $this->_where
[$grouping][] = "$fieldName <= $toDate";
548 $this->_qill
[$grouping][] = $field['label'] . ' <= ' . CRM_Utils_Date
::customFormat($toDate);
553 case 'StateProvince':
555 $this->_where
[$grouping][] = "$fieldName {$op} " . CRM_Utils_Type
::escape($value, 'Int');
556 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} {$qillValue}";
560 if ($op == 'IS NULL' ||
$op == 'IS NOT NULL' ||
$op == 'IS EMPTY' ||
$op == 'IS NOT EMPTY') {
570 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op);
571 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} ";
580 * Function that does the actual query generation.
581 * basically ties all the above functions together
586 public function query() {
592 if (!empty($this->_where
)) {
594 foreach ($this->_where
as $grouping => $values) {
595 if (!empty($values)) {
596 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
599 if (!empty($clauses)) {
600 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
605 implode(' , ', $this->_select
),
606 implode(' ', $this->_tables
),
615 * @param string $fieldName
619 public function searchRange(&$id, &$label, $type, $fieldName, &$value, &$grouping) {
622 if (isset($value['from'])) {
623 $val = CRM_Utils_Type
::escape($value['from'], $type);
625 if ($type == 'String') {
626 $this->_where
[$grouping][] = "$fieldName >= '$val'";
629 $this->_where
[$grouping][] = "$fieldName >= $val";
631 $qill[] = ts('greater than or equal to \'%1\'', array(1 => $value['from']));
634 if (isset($value['to'])) {
635 $val = CRM_Utils_Type
::escape($value['to'], $type);
636 if ($type == 'String') {
637 $this->_where
[$grouping][] = "$fieldName <= '$val'";
640 $this->_where
[$grouping][] = "$fieldName <= $val";
642 $qill[] = ts('less than or equal to \'%1\'', array(1 => $value['to']));
646 $this->_qill
[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);