4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2014
37 class CRM_Core_BAO_CustomQuery
{
38 CONST PREFIX
= 'custom_value_';
41 * the set of custom field ids
55 * the name of the elements that are in the select clause
56 * used to extract the values
63 * the tables involved in the query
78 * The english language version of the query
85 * The cache to translate the option values into labels
92 * The custom fields information
99 * Searching for contacts?
103 protected $_contactSearch;
105 protected $_locationSpecificCustomFields;
108 * This stores custom data group types and tables that it extends
110 * @todo add comments explaining why survey & campaign are missing from this
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',
135 * Takes in a set of custom field ids andsets up the data structures to
138 * @param array $ids the set of custom field ids
140 * @param bool $contactSearch
141 * @param array $locationSpecificFields
145 function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) {
147 $this->_locationSpecificCustomFields
= $locationSpecificFields;
149 $this->_select
= array();
150 $this->_element
= array();
151 $this->_tables
= array();
152 $this->_whereTables
= array();
153 $this->_where
= array();
154 $this->_qill
= array();
155 $this->_options
= array();
157 $this->_fields
= array();
158 $this->_contactSearch
= $contactSearch;
160 if (empty($this->_ids
)) {
164 // initialize the field array
165 $tmpArray = array_keys($this->_ids
);
166 $idString = implode(',', $tmpArray);
168 SELECT f.id, f.label, f.data_type,
169 f.html_type, f.is_search_range,
170 f.option_group_id, f.custom_group_id,
171 f.column_name, g.table_name,
172 f.date_format,f.time_format
173 FROM civicrm_custom_field f,
174 civicrm_custom_group g
175 WHERE f.custom_group_id = g.id
178 AND f.id IN ( $idString )";
180 $dao = CRM_Core_DAO
::executeQuery($query);
181 while ($dao->fetch()) {
182 // get the group dao to figure which class this custom field extends
183 $extends = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id
, 'extends');
184 if (array_key_exists($extends, self
::$extendsMap)) {
185 $extendsTable = self
::$extendsMap[$extends];
187 elseif (in_array($extends, CRM_Contact_BAO_ContactType
::subTypes())) {
188 // if $extends is a subtype, refer contact table
189 $extendsTable = self
::$extendsMap['Contact'];
191 $this->_fields
[$dao->id
] = array(
193 'label' => $dao->label
,
194 'extends' => $extendsTable,
195 'data_type' => $dao->data_type
,
196 'html_type' => $dao->html_type
,
197 'is_search_range' => $dao->is_search_range
,
198 'column_name' => $dao->column_name
,
199 'table_name' => $dao->table_name
,
200 'option_group_id' => $dao->option_group_id
,
203 // store it in the options cache to make things easier
204 // during option lookup
205 $this->_options
[$dao->id
] = array();
206 $this->_options
[$dao->id
]['attributes'] = array(
207 'label' => $dao->label
,
208 'data_type' => $dao->data_type
,
209 'html_type' => $dao->html_type
,
212 $optionGroupID = NULL;
213 $htmlTypes = array('CheckBox', 'Radio', 'Select', 'Multi-Select', 'AdvMulti-Select', 'Autocomplete-Select');
214 if (in_array($dao->html_type
, $htmlTypes) && $dao->data_type
!= 'ContactReference') {
215 if ($dao->option_group_id
) {
216 $optionGroupID = $dao->option_group_id
;
218 elseif ($dao->data_type
!= 'Boolean') {
219 $errorMessage = ts("The custom field %1 is corrupt. Please delete and re-build the field",
220 array(1 => $dao->label
)
222 CRM_Core_Error
::fatal($errorMessage);
225 elseif ($dao->html_type
== 'Select Date') {
226 $this->_options
[$dao->id
]['attributes']['date_format'] = $dao->date_format
;
227 $this->_options
[$dao->id
]['attributes']['time_format'] = $dao->time_format
;
230 // build the cache for custom values with options (label => value)
231 if ($optionGroupID != NULL) {
234 FROM civicrm_option_value
235 WHERE option_group_id = $optionGroupID
238 $option = CRM_Core_DAO
::executeQuery($query);
239 while ($option->fetch()) {
240 $dataType = $this->_fields
[$dao->id
]['data_type'];
241 if ($dataType == 'Int' ||
$dataType == 'Float') {
242 $num = round($option->value
, 2);
243 $this->_options
[$dao->id
]["$num"] = $option->label
;
246 $this->_options
[$dao->id
][$option->value
] = $option->label
;
249 $options = $this->_options
[$dao->id
];
250 //unset attributes to avoid confussion
251 unset($options['attributes']);
252 CRM_Utils_Hook
::customFieldOptions($dao->id
, $options, FALSE);
258 * generate the select clause and the associated tables
259 * for the from clause
267 if (empty($this->_fields
)) {
271 foreach ($this->_fields
as $id => $field) {
272 $name = $field['table_name'];
273 $fieldName = 'custom_' . $field['id'];
274 $this->_select
["{$name}_id"] = "{$name}.id as {$name}_id";
275 $this->_element
["{$name}_id"] = 1;
276 $this->_select
[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
277 $this->_element
[$fieldName] = 1;
280 if ($field['extends'] == 'civicrm_group') {
283 elseif ($field['extends'] == 'civicrm_contact') {
284 $joinTable = 'contact_a';
286 elseif ($field['extends'] == 'civicrm_contribution') {
287 $joinTable = $field['extends'];
289 elseif (in_array($field['extends'], self
::$extendsMap)) {
290 $joinTable = $field['extends'];
296 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id";
298 if ($this->_ids
[$id]) {
299 $this->_whereTables
[$name] = $this->_tables
[$name];
304 $joinTableAlias = $joinTable;
305 // Set location-specific query
306 if (isset($this->_locationSpecificCustomFields
[$id])) {
307 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields
[$id];
308 $joinTableAlias = "$locationType-address";
309 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
311 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
312 if ($this->_ids
[$id]) {
313 $this->_whereTables
[$name] = $this->_tables
[$name];
315 if ($joinTable != 'contact_a') {
316 $this->_whereTables
[$joinTableAlias] = $this->_tables
[$joinTableAlias] = $joinClause;
318 elseif ($this->_contactSearch
) {
319 CRM_Contact_BAO_Query
::$_openedPanes[ts('Custom Fields')] = TRUE;
326 * generate the where clause and also the english language
336 foreach ($this->_ids
as $id => $values) {
338 // Fixed for Isuue CRM 607
339 if (CRM_Utils_Array
::value($id, $this->_fields
) === NULL ||
345 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
347 foreach ($values as $tuple) {
348 list($name, $op, $value, $grouping, $wildcard) = $tuple;
350 // fix $value here to escape sql injection attacks
351 $field = $this->_fields
[$id];
352 $qillValue = CRM_Core_BAO_CustomField
::getDisplayValue($value, $id, $this->_options
);
354 if (!is_array($value)) {
355 $value = CRM_Core_DAO
::escapeString(trim($value));
358 $fieldName = "{$field['table_name']}.{$field['column_name']}";
359 switch ($field['data_type']) {
362 // if we are coming in from listings,
363 // for checkboxes the value is already in the right format and is NOT an array
364 if (is_array($value)) {
366 //ignoring $op value for checkbox and multi select
369 $sqlOPlabel = ts('match ALL');
370 if ($field['html_type'] == 'CheckBox') {
371 foreach ($value as $k => $v) {
373 if ($k == 'CiviCRM_OP_OR') {
375 $sqlOPlabel = ts('match ANY');
379 $sqlValue[] = "( $sql like '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $k . CRM_Core_DAO
::VALUE_SEPARATOR
. "%' ) ";
382 //if user check only 'CiviCRM_OP_OR' check box
383 //of custom checkbox field, then ignore this field.
384 if (!empty($sqlValue)) {
385 $this->_where
[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
386 $this->_qill
[$grouping][] = "{$field['label']} $op $qillValue ( $sqlOPlabel )";
391 foreach ($value as $k => $v) {
392 if ($v == 'CiviCRM_OP_OR') {
394 $sqlOPlabel = ts('match ANY');
397 $v = CRM_Core_DAO
::escapeString($v);
398 $sqlValue[] = "( $sql like '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $v . CRM_Core_DAO
::VALUE_SEPARATOR
. "%' ) ";
400 //if user select only 'CiviCRM_OP_OR' value
401 //of custom multi select field, then ignore this field.
402 if (!empty($sqlValue)) {
403 $this->_where
[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
404 $this->_qill
[$grouping][] = "$field[label] $op $qillValue ( $sqlOPlabel )";
409 if ($field['is_search_range'] && is_array($value)) {
410 $this->searchRange($field['id'],
419 if (in_array($field['html_type'], array('Select', 'Radio', 'Autocomplete-Select'))) {
421 $val = CRM_Utils_Type
::escape($value, 'String');
424 $val = CRM_Utils_Type
::escape($strtolower(trim($value)), 'String');
428 $val = $strtolower(CRM_Core_DAO
::escapeString($val));
433 //FIX for custom data query fired against no value(NULL/NOT NULL)
434 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($sql, $op, $val, $field['data_type']);
435 $this->_qill
[$grouping][] = "$field[label] $op $qillValue";
440 case 'ContactReference':
441 $label = $value ? CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
442 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
443 $this->_qill
[$grouping][] = $field['label'] . " $op $label";
447 if ($field['is_search_range'] && is_array($value)) {
448 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
451 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
452 $this->_qill
[$grouping][] = $field['label'] . " $op $value";
457 if (strtolower($value) == 'yes' ||
strtolower($value) == strtolower(ts('Yes'))) {
461 $value = (int) $value;
463 $value = ($value == 1) ?
1 : 0;
464 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
465 $value = $value ?
ts('Yes') : ts('No');
466 $this->_qill
[$grouping][] = $field['label'] . " {$op} {$value}";
470 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
471 $this->_qill
[$grouping][] = $field['label'] . " $op $value";
475 if ($field['is_search_range'] && is_array($value)) {
476 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
479 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
480 $this->_qill
[$grouping][] = $field['label'] . " {$op} {$value}";
485 if ($field['is_search_range'] && is_array($value)) {
486 foreach ($value as $key => $val) {
487 $moneyFormat = CRM_Utils_Rule
::cleanMoney($value[$key]);
488 $value[$key] = $moneyFormat;
490 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
493 $moneyFormat = CRM_Utils_Rule
::cleanMoney($value);
494 $value = $moneyFormat;
495 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
496 $this->_qill
[$grouping][] = $field['label'] . " {$op} {$value}";
501 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
502 $this->_qill
[$grouping][] = "$field[label] $op $value";
506 $fromValue = CRM_Utils_Array
::value('from', $value);
507 $toValue = CRM_Utils_Array
::value('to', $value);
509 if (!$fromValue && !$toValue) {
510 if (!CRM_Utils_Date
::processDate($value) && $op != 'IS NULL' && $op != 'IS NOT NULL') {
514 // hack to handle yy format during search
515 if (is_numeric($value) && strlen($value) == 4) {
516 $value = "01-01-{$value}";
519 $date = CRM_Utils_Date
::processDate($value);
520 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $date, 'String');
521 $this->_qill
[$grouping][] = $field['label'] . " {$op} " . CRM_Utils_Date
::customFormat($date);
524 if (is_numeric($fromValue) && strlen($fromValue) == 4) {
525 $fromValue = "01-01-{$fromValue}";
528 if (is_numeric($toValue) && strlen($toValue) == 4) {
529 $toValue = "01-01-{$toValue}";
532 // TO DO: add / remove time based on date parts
533 $fromDate = CRM_Utils_Date
::processDate($fromValue);
534 $toDate = CRM_Utils_Date
::processDate($toValue);
535 if (!$fromDate && !$toDate) {
539 $this->_where
[$grouping][] = "$fieldName >= $fromDate";
540 $this->_qill
[$grouping][] = $field['label'] . ' >= ' . CRM_Utils_Date
::customFormat($fromDate);
543 $this->_where
[$grouping][] = "$fieldName <= $toDate";
544 $this->_qill
[$grouping][] = $field['label'] . ' <= ' . CRM_Utils_Date
::customFormat($toDate);
549 case 'StateProvince':
551 if (!is_array($value)) {
552 $this->_where
[$grouping][] = "$fieldName {$op} " . CRM_Utils_Type
::escape($value, 'Int');
553 $this->_qill
[$grouping][] = $field['label'] . " {$op} {$qillValue}";
557 $sqlOPlabel = ts('match ALL');
558 foreach ($value as $k => $v) {
559 if ($v == 'CiviCRM_OP_OR') {
561 $sqlOPlabel = ts('match ANY');
564 $sqlValue[] = "( $fieldName like '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $v . CRM_Core_DAO
::VALUE_SEPARATOR
. "%' ) ";
567 //if user select only 'CiviCRM_OP_OR' value
568 //of custom multi select field, then ignore this field.
569 if (!empty($sqlValue)) {
570 $this->_where
[$grouping][] = " ( " . implode($sqlOP, $sqlValue) . " ) ";
571 $this->_qill
[$grouping][] = "$field[label] $op $qillValue ( $sqlOPlabel )";
577 if ( $op == 'IS NULL' ||
$op == 'IS NOT NULL' ||
$op == 'IS EMPTY' ||
$op == 'IS NOT EMPTY' ) {
586 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op);
587 $this->_qill
[$grouping][] = $field['label'] . " {$op} ";
596 * function that does the actual query generation
597 * basically ties all the above functions together
601 * @return array array of strings
610 if (!empty($this->_where
)) {
612 foreach ($this->_where
as $grouping => $values) {
613 if (!empty($values)) {
614 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
617 if (!empty($clauses)) {
618 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
622 return array(implode(' , ', $this->_select
),
623 implode(' ', $this->_tables
),
636 function searchRange(&$id, &$label, $type, $fieldName, &$value, &$grouping) {
639 if (isset($value['from'])) {
640 $val = CRM_Utils_Type
::escape($value['from'], $type);
642 if ($type == 'String') {
643 $this->_where
[$grouping][] = "$fieldName >= '$val'";
646 $this->_where
[$grouping][] = "$fieldName >= $val";
648 $qill[] = ts('greater than or equal to \'%1\'', array(1 => $value['from']));
651 if (isset($value['to'])) {
652 $val = CRM_Utils_Type
::escape($value['to'], $type);
653 if ($type == 'String') {
654 $this->_where
[$grouping][] = "$fieldName <= '$val'";
657 $this->_where
[$grouping][] = "$fieldName <= $val";
659 $qill[] = ts('less than or equal to \'%1\'', array(1 => $value['to']));
663 $this->_qill
[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);