3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 class CRM_Core_BAO_CustomQuery
{
19 const PREFIX
= 'custom_value_';
22 * The set of custom field ids.
36 * The name of the elements that are in the select clause.
37 * used to extract the values
44 * The tables involved in the query.
59 * The english language version of the query.
66 * No longer needed due to CRM-17646 refactoring, but still used in some places
74 * The custom fields information.
83 public function getFields() {
84 return $this->_fields
;
88 * Searching for contacts?
92 protected $_contactSearch;
94 protected $_locationSpecificCustomFields;
97 * This stores custom data group types and tables that it extends.
101 public static $extendsMap = [
102 'Contact' => 'civicrm_contact',
103 'Individual' => 'civicrm_contact',
104 'Household' => 'civicrm_contact',
105 'Organization' => 'civicrm_contact',
106 'Contribution' => 'civicrm_contribution',
107 'ContributionRecur' => 'civicrm_contribution_recur',
108 'Membership' => 'civicrm_membership',
109 'Participant' => 'civicrm_participant',
110 'Group' => 'civicrm_group',
111 'Relationship' => 'civicrm_relationship',
112 'Event' => 'civicrm_event',
113 'Case' => 'civicrm_case',
114 'Activity' => 'civicrm_activity',
115 'Pledge' => 'civicrm_pledge',
116 'Grant' => 'civicrm_grant',
117 'Address' => 'civicrm_address',
118 'Campaign' => 'civicrm_campaign',
119 'Survey' => 'civicrm_survey',
125 * Takes in a set of custom field ids andsets up the data structures to
129 * The set of custom field ids.
131 * @param bool $contactSearch
132 * @param array $locationSpecificFields
134 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = []) {
136 $this->_locationSpecificCustomFields
= $locationSpecificFields;
139 $this->_element
= [];
141 $this->_whereTables
= [];
144 $this->_options
= [];
146 $this->_contactSearch
= $contactSearch;
147 $this->_fields
= CRM_Core_BAO_CustomField
::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
149 if (empty($this->_ids
)) {
153 // initialize the field array
154 $tmpArray = array_keys($this->_ids
);
155 $idString = implode(',', $tmpArray);
157 SELECT f.id, f.label, f.data_type,
158 f.html_type, f.is_search_range,
159 f.option_group_id, f.custom_group_id,
160 f.column_name, g.table_name,
161 f.date_format,f.time_format
162 FROM civicrm_custom_field f,
163 civicrm_custom_group g
164 WHERE f.custom_group_id = g.id
167 AND f.id IN ( $idString )";
169 $dao = CRM_Core_DAO
::executeQuery($query);
170 while ($dao->fetch()) {
171 // Deprecated (and poorly named) cache of field attributes
172 $this->_options
[$dao->id
] = [
174 'label' => $dao->label
,
175 'data_type' => $dao->data_type
,
176 'html_type' => $dao->html_type
,
180 $options = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $dao->id
, [], 'search');
182 $this->_options
[$dao->id
] +
= $options;
185 if ($dao->html_type
== 'Select Date') {
186 $this->_options
[$dao->id
]['attributes']['date_format'] = $dao->date_format
;
187 $this->_options
[$dao->id
]['attributes']['time_format'] = $dao->time_format
;
193 * Generate the select clause and the associated tables.
195 public function select() {
196 if (empty($this->_fields
)) {
200 foreach (array_keys($this->_ids
) as $id) {
201 $field = $this->_fields
[$id];
203 if ($this->_contactSearch
&& $field['search_table'] === 'contact_a') {
204 CRM_Contact_BAO_Query
::$_openedPanes[ts('Custom Fields')] = TRUE;
207 $name = $field['table_name'];
208 $fieldName = 'custom_' . $field['id'];
209 $this->_select
["{$name}_id"] = "{$name}.id as {$name}_id";
210 $this->_element
["{$name}_id"] = 1;
211 $this->_select
[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
212 $this->_element
[$fieldName] = 1;
214 $this->joinCustomTableForField($field);
219 * Generate the where clause and also the english language equivalent.
221 * @throws \CRM_Core_Exception
223 public function where() {
224 foreach ($this->_ids
as $id => $values) {
226 // Fixed for Issue CRM 607
227 if (CRM_Utils_Array
::value($id, $this->_fields
) === NULL ||
233 foreach ($values as $tuple) {
234 list($name, $op, $value, $grouping, $wildcard) = $tuple;
236 $field = $this->_fields
[$id];
238 $fieldName = "{$field['table_name']}.{$field['column_name']}";
240 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($field);
242 // fix $value here to escape sql injection attacks
244 if (!is_array($value)) {
245 $value = CRM_Core_DAO
::escapeString(trim($value));
246 $qillValue = CRM_Core_BAO_CustomField
::displayValue($value, $id);
248 elseif (count($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
250 $qillValue = strstr($op, 'NULL') ?
NULL : CRM_Core_BAO_CustomField
::displayValue($value[$op], $id);
253 $op = strstr($op, 'IN') ?
$op : 'IN';
254 $qillValue = CRM_Core_BAO_CustomField
::displayValue($value, $id);
257 $qillOp = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
259 // Ensure the table is joined in (eg if in where but not select).
260 $this->joinCustomTableForField($field);
261 switch ($field['data_type']) {
263 case 'StateProvince':
266 if ($field['is_search_range'] && is_array($value)) {
267 //didn't found any field under any of these three data-types as searchable by range
270 // fix $value here to escape sql injection attacks
271 if (!is_array($value)) {
272 if ($field['data_type'] == 'String') {
273 $value = CRM_Utils_Type
::escape($value, 'String');
276 $value = CRM_Utils_Type
::escape($value, 'Integer');
278 $value = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $value);
279 $value = str_replace('|', '[:separator:]', $value);
281 elseif ($isSerialized) {
282 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
284 $value = $value[$op];
286 // CRM-19006: escape characters like comma, | before building regex pattern
287 $value = (array) $value;
288 foreach ($value as $key => $val) {
289 $value[$key] = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $val);
290 $value[$key] = str_replace('|', '[:separator:]', $value[$key]);
291 if ($field['data_type'] == 'String') {
292 $value[$key] = CRM_Utils_Type
::escape($value[$key], 'String');
295 $value[$key] = CRM_Utils_Type
::escape($value[$key], 'Integer');
298 $value = implode(',', $value);
301 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
302 if ($isSerialized && !CRM_Utils_System
::isNull($value) && !strstr($op, 'NULL') && !strstr($op, 'LIKE')) {
303 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
304 $value = str_replace(",", "$sp|$sp", $value);
305 $value = str_replace(['[:comma:]', '(', ')'], [',', '[(]', '[)]'], $value);
307 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
308 $value = $sp . $value . $sp;
310 foreach (explode("|", $value) as $val) {
311 $val = str_replace('[:separator:]', '\|', $val);
312 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $val, 'String');
316 $value = str_replace('[:separator:]', '\|', $value);
317 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
321 //FIX for custom data query fired against no value(NULL/NOT NULL)
322 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
324 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $qillValue";
328 case 'ContactReference':
329 $label = $value ? CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
330 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
331 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $label";
335 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
336 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);;
340 if (!is_array($value)) {
341 if (strtolower($value) == 'yes' ||
strtolower($value) == strtolower(ts('Yes'))) {
345 $value = (int) $value;
347 $value = ($value == 1) ?
1 : 0;
348 $qillValue = $value ?
'Yes' : 'No';
350 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
351 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
356 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
357 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
361 $value = CRM_Utils_Array
::value($op, (array) $value, $value);
362 if (is_array($value)) {
363 foreach ($value as $key => $val) {
364 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
365 // Note the only place I can find that this code is reached by is searching a custom money field in advanced search.
366 // with euro style comma separators this doesn't work - with or without this cleanMoney.
367 // So this should be removed but is not increasing the brokeness IMHO
368 $value[$op][$key] = CRM_Utils_Rule
::cleanMoney($value[$key]);
372 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
373 // comments per above apply. cleanMoney
374 $value = CRM_Utils_Rule
::cleanMoney($value);
378 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
379 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
383 if (substr($name, -9, 9) !== '_relative'
384 && substr($name, -4, 4) !== '_low'
385 && substr($name, -5, 5) !== '_high') {
386 // Relative dates are handled in the buildRelativeDateQuery function.
387 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Date');
388 list($qillOp, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $field['label'], $value, $op, [], CRM_Utils_Type
::T_DATE
);
389 $this->_qill
[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
394 if ($op == 'IS NULL' ||
$op == 'IS NOT NULL' ||
$op == 'IS EMPTY' ||
$op == 'IS NOT EMPTY') {
404 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op);
405 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} ";
414 * Function that does the actual query generation.
415 * basically ties all the above functions together
420 public function query() {
426 if (!empty($this->_where
)) {
428 foreach ($this->_where
as $grouping => $values) {
429 if (!empty($values)) {
430 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
433 if (!empty($clauses)) {
434 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
439 implode(' , ', $this->_select
),
440 implode(' ', $this->_tables
),
446 * Join the custom table for the field in (if not already in the query).
448 * @param array $field
450 protected function joinCustomTableForField($field) {
451 $name = $field['table_name'];
452 $join = "\nLEFT JOIN $name ON $name.entity_id = {$field['search_table']}.id";
453 $this->_tables
[$name] = $this->_tables
[$name] ??
$join;
454 $this->_whereTables
[$name] = $this->_whereTables
[$name] ??
$join;
456 $joinTable = $field['search_table'];
459 $joinTableAlias = $joinTable;
460 // Set location-specific query
461 if (isset($this->_locationSpecificCustomFields
[$field['id']])) {
462 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields
[$field['id']];
463 $joinTableAlias = "$locationType-address";
464 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
466 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
467 if (!empty($this->_ids
[$field['id']])) {
468 $this->_whereTables
[$name] = $this->_tables
[$name];
470 if ($joinTable !== 'contact_a') {
471 $this->_whereTables
[$joinTableAlias] = $this->_tables
[$joinTableAlias] = $joinClause;