3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
34 class CRM_Core_BAO_CustomQuery
{
35 const PREFIX
= 'custom_value_';
38 * The set of custom field ids.
52 * The name of the elements that are in the select clause.
53 * used to extract the values
60 * The tables involved in the query.
75 * The english language version of the query.
82 * No longer needed due to CRM-17646 refactoring, but still used in some places
90 * The custom fields information.
99 public function getFields() {
100 return $this->_fields
;
104 * Searching for contacts?
108 protected $_contactSearch;
110 protected $_locationSpecificCustomFields;
113 * This stores custom data group types and tables that it extends.
117 public static $extendsMap = [
118 'Contact' => 'civicrm_contact',
119 'Individual' => 'civicrm_contact',
120 'Household' => 'civicrm_contact',
121 'Organization' => 'civicrm_contact',
122 'Contribution' => 'civicrm_contribution',
123 'ContributionRecur' => 'civicrm_contribution_recur',
124 'Membership' => 'civicrm_membership',
125 'Participant' => 'civicrm_participant',
126 'Group' => 'civicrm_group',
127 'Relationship' => 'civicrm_relationship',
128 'Event' => 'civicrm_event',
129 'Case' => 'civicrm_case',
130 'Activity' => 'civicrm_activity',
131 'Pledge' => 'civicrm_pledge',
132 'Grant' => 'civicrm_grant',
133 'Address' => 'civicrm_address',
134 'Campaign' => 'civicrm_campaign',
135 'Survey' => 'civicrm_survey',
141 * Takes in a set of custom field ids andsets up the data structures to
145 * The set of custom field ids.
147 * @param bool $contactSearch
148 * @param array $locationSpecificFields
150 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = []) {
152 $this->_locationSpecificCustomFields
= $locationSpecificFields;
155 $this->_element
= [];
157 $this->_whereTables
= [];
160 $this->_options
= [];
162 $this->_contactSearch
= $contactSearch;
163 $this->_fields
= CRM_Core_BAO_CustomField
::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
165 if (empty($this->_ids
)) {
169 // initialize the field array
170 $tmpArray = array_keys($this->_ids
);
171 $idString = implode(',', $tmpArray);
173 SELECT f.id, f.label, f.data_type,
174 f.html_type, f.is_search_range,
175 f.option_group_id, f.custom_group_id,
176 f.column_name, g.table_name,
177 f.date_format,f.time_format
178 FROM civicrm_custom_field f,
179 civicrm_custom_group g
180 WHERE f.custom_group_id = g.id
183 AND f.id IN ( $idString )";
185 $dao = CRM_Core_DAO
::executeQuery($query);
186 while ($dao->fetch()) {
187 // Deprecated (and poorly named) cache of field attributes
188 $this->_options
[$dao->id
] = [
190 'label' => $dao->label
,
191 'data_type' => $dao->data_type
,
192 'html_type' => $dao->html_type
,
196 $options = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $dao->id
, [], 'search');
198 $this->_options
[$dao->id
] +
= $options;
201 if ($dao->html_type
== 'Select Date') {
202 $this->_options
[$dao->id
]['attributes']['date_format'] = $dao->date_format
;
203 $this->_options
[$dao->id
]['attributes']['time_format'] = $dao->time_format
;
209 * Generate the select clause and the associated tables.
211 public function select() {
212 if (empty($this->_fields
)) {
216 foreach (array_keys($this->_ids
) as $id) {
217 $field = $this->_fields
[$id];
219 if ($this->_contactSearch
&& $field['search_table'] === 'contact_a') {
220 CRM_Contact_BAO_Query
::$_openedPanes[ts('Custom Fields')] = TRUE;
223 $name = $field['table_name'];
224 $fieldName = 'custom_' . $field['id'];
225 $this->_select
["{$name}_id"] = "{$name}.id as {$name}_id";
226 $this->_element
["{$name}_id"] = 1;
227 $this->_select
[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
228 $this->_element
[$fieldName] = 1;
230 $this->joinCustomTableForField($field);
235 * Generate the where clause and also the english language equivalent.
237 * @throws \CRM_Core_Exception
239 public function where() {
240 foreach ($this->_ids
as $id => $values) {
242 // Fixed for Issue CRM 607
243 if (CRM_Utils_Array
::value($id, $this->_fields
) === NULL ||
249 foreach ($values as $tuple) {
250 list($name, $op, $value, $grouping, $wildcard) = $tuple;
252 $field = $this->_fields
[$id];
254 $fieldName = "{$field['table_name']}.{$field['column_name']}";
256 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($field);
258 // fix $value here to escape sql injection attacks
260 if (!is_array($value)) {
261 $value = CRM_Core_DAO
::escapeString(trim($value));
262 $qillValue = CRM_Core_BAO_CustomField
::displayValue($value, $id);
264 elseif (count($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
266 $qillValue = strstr($op, 'NULL') ?
NULL : CRM_Core_BAO_CustomField
::displayValue($value[$op], $id);
269 $op = strstr($op, 'IN') ?
$op : 'IN';
270 $qillValue = CRM_Core_BAO_CustomField
::displayValue($value, $id);
273 $qillOp = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
275 // Ensure the table is joined in (eg if in where but not select).
276 $this->joinCustomTableForField($field);
277 switch ($field['data_type']) {
279 case 'StateProvince':
282 if ($field['is_search_range'] && is_array($value)) {
283 //didn't found any field under any of these three data-types as searchable by range
286 // fix $value here to escape sql injection attacks
287 if (!is_array($value)) {
288 if ($field['data_type'] == 'String') {
289 $value = CRM_Utils_Type
::escape($value, 'String');
292 $value = CRM_Utils_Type
::escape($value, 'Integer');
294 $value = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $value);
295 $value = str_replace('|', '[:separator:]', $value);
297 elseif ($isSerialized) {
298 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
300 $value = $value[$op];
302 // CRM-19006: escape characters like comma, | before building regex pattern
303 $value = (array) $value;
304 foreach ($value as $key => $val) {
305 $value[$key] = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $val);
306 $value[$key] = str_replace('|', '[:separator:]', $value[$key]);
307 if ($field['data_type'] == 'String') {
308 $value[$key] = CRM_Utils_Type
::escape($value[$key], 'String');
311 $value[$key] = CRM_Utils_Type
::escape($value[$key], 'Integer');
314 $value = implode(',', $value);
317 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
318 if ($isSerialized && !CRM_Utils_System
::isNull($value) && !strstr($op, 'NULL') && !strstr($op, 'LIKE')) {
319 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
320 $value = str_replace(",", "$sp|$sp", $value);
321 $value = str_replace(['[:comma:]', '(', ')'], [',', '[(]', '[)]'], $value);
323 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
324 $value = $sp . $value . $sp;
326 foreach (explode("|", $value) as $val) {
327 $val = str_replace('[:separator:]', '\|', $val);
328 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $val, 'String');
332 $value = str_replace('[:separator:]', '\|', $value);
333 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
337 //FIX for custom data query fired against no value(NULL/NOT NULL)
338 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
340 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $qillValue";
344 case 'ContactReference':
345 $label = $value ? CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
346 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
347 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $label";
351 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
352 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);;
356 if (!is_array($value)) {
357 if (strtolower($value) == 'yes' ||
strtolower($value) == strtolower(ts('Yes'))) {
361 $value = (int) $value;
363 $value = ($value == 1) ?
1 : 0;
364 $qillValue = $value ?
'Yes' : 'No';
366 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
367 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
372 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
373 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
377 $value = CRM_Utils_Array
::value($op, (array) $value, $value);
378 if (is_array($value)) {
379 foreach ($value as $key => $val) {
380 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
381 // Note the only place I can find that this code is reached by is searching a custom money field in advanced search.
382 // with euro style comma separators this doesn't work - with or without this cleanMoney.
383 // So this should be removed but is not increasing the brokeness IMHO
384 $value[$op][$key] = CRM_Utils_Rule
::cleanMoney($value[$key]);
388 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
389 // comments per above apply. cleanMoney
390 $value = CRM_Utils_Rule
::cleanMoney($value);
394 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
395 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
399 if (substr($name, -9, 9) !== '_relative'
400 && substr($name, -4, 4) !== '_low'
401 && substr($name, -5, 5) !== '_high') {
402 // Relative dates are handled in the buildRelativeDateQuery function.
403 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Date');
404 list($qillOp, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $field['label'], $value, $op, [], CRM_Utils_Type
::T_DATE
);
405 $this->_qill
[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
410 if ($op == 'IS NULL' ||
$op == 'IS NOT NULL' ||
$op == 'IS EMPTY' ||
$op == 'IS NOT EMPTY') {
420 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op);
421 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} ";
430 * Function that does the actual query generation.
431 * basically ties all the above functions together
436 public function query() {
442 if (!empty($this->_where
)) {
444 foreach ($this->_where
as $grouping => $values) {
445 if (!empty($values)) {
446 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
449 if (!empty($clauses)) {
450 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
455 implode(' , ', $this->_select
),
456 implode(' ', $this->_tables
),
462 * Join the custom table for the field in (if not already in the query).
464 * @param array $field
466 protected function joinCustomTableForField($field) {
467 $name = $field['table_name'];
468 $join = "\nLEFT JOIN $name ON $name.entity_id = {$field['search_table']}.id";
469 $this->_tables
[$name] = $this->_tables
[$name] ??
$join;
470 $this->_whereTables
[$name] = $this->_whereTables
[$name] ??
$join;
472 $joinTable = $field['search_table'];
475 $joinTableAlias = $joinTable;
476 // Set location-specific query
477 if (isset($this->_locationSpecificCustomFields
[$field['id']])) {
478 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields
[$field['id']];
479 $joinTableAlias = "$locationType-address";
480 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
482 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
483 if (!empty($this->_ids
[$field['id']])) {
484 $this->_whereTables
[$name] = $this->_tables
[$name];
486 if ($joinTable !== 'contact_a') {
487 $this->_whereTables
[$joinTableAlias] = $this->_tables
[$joinTableAlias] = $joinClause;