3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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];
218 $name = $field['table_name'];
219 $fieldName = 'custom_' . $field['id'];
220 $this->_select
["{$name}_id"] = "{$name}.id as {$name}_id";
221 $this->_element
["{$name}_id"] = 1;
222 $this->_select
[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
223 $this->_element
[$fieldName] = 1;
224 $joinTable = $field['search_table'];
226 if ($joinTable == 'civicrm_group' ||
empty($joinTable)) {
230 $this->joinCustomTableForField($field);
234 $joinTableAlias = $joinTable;
235 // Set location-specific query
236 if (isset($this->_locationSpecificCustomFields
[$id])) {
237 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields
[$id];
238 $joinTableAlias = "$locationType-address";
239 $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
241 $this->_tables
[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
242 if (!empty($this->_ids
[$id])) {
243 $this->_whereTables
[$name] = $this->_tables
[$name];
245 if ($joinTable != 'contact_a') {
246 $this->_whereTables
[$joinTableAlias] = $this->_tables
[$joinTableAlias] = $joinClause;
248 elseif ($this->_contactSearch
) {
249 CRM_Contact_BAO_Query
::$_openedPanes[ts('Custom Fields')] = TRUE;
256 * Generate the where clause and also the english language equivalent.
258 public function where() {
259 foreach ($this->_ids
as $id => $values) {
261 // Fixed for Issue CRM 607
262 if (CRM_Utils_Array
::value($id, $this->_fields
) === NULL ||
268 foreach ($values as $tuple) {
269 list($name, $op, $value, $grouping, $wildcard) = $tuple;
271 $field = $this->_fields
[$id];
273 $fieldName = "{$field['table_name']}.{$field['column_name']}";
275 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($field);
277 // fix $value here to escape sql injection attacks
279 if (!is_array($value)) {
280 $value = CRM_Core_DAO
::escapeString(trim($value));
281 $qillValue = CRM_Core_BAO_CustomField
::displayValue($value, $id);
283 elseif (count($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
285 $qillValue = strstr($op, 'NULL') ?
NULL : CRM_Core_BAO_CustomField
::displayValue($value[$op], $id);
288 $op = strstr($op, 'IN') ?
$op : 'IN';
289 $qillValue = CRM_Core_BAO_CustomField
::displayValue($value, $id);
292 $qillOp = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
294 // Ensure the table is joined in (eg if in where but not select).
295 $this->joinCustomTableForField($field);
296 switch ($field['data_type']) {
298 case 'StateProvince':
301 if ($field['is_search_range'] && is_array($value)) {
302 //didn't found any field under any of these three data-types as searchable by range
305 // fix $value here to escape sql injection attacks
306 if (!is_array($value)) {
307 if ($field['data_type'] == 'String') {
308 $value = CRM_Utils_Type
::escape($value, 'String');
311 $value = CRM_Utils_Type
::escape($value, 'Integer');
313 $value = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $value);
314 $value = str_replace('|', '[:separator:]', $value);
316 elseif ($isSerialized) {
317 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
319 $value = $value[$op];
321 // CRM-19006: escape characters like comma, | before building regex pattern
322 $value = (array) $value;
323 foreach ($value as $key => $val) {
324 $value[$key] = str_replace(['[', ']', ','], ['\[', '\]', '[:comma:]'], $val);
325 $value[$key] = str_replace('|', '[:separator:]', $value[$key]);
326 if ($field['data_type'] == 'String') {
327 $value[$key] = CRM_Utils_Type
::escape($value[$key], 'String');
330 $value[$key] = CRM_Utils_Type
::escape($value[$key], 'Integer');
333 $value = implode(',', $value);
336 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
337 if ($isSerialized && !CRM_Utils_System
::isNull($value) && !strstr($op, 'NULL') && !strstr($op, 'LIKE')) {
338 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
339 $value = str_replace(",", "$sp|$sp", $value);
340 $value = str_replace(['[:comma:]', '(', ')'], [',', '[(]', '[)]'], $value);
342 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
343 $value = $sp . $value . $sp;
345 foreach (explode("|", $value) as $val) {
346 $val = str_replace('[:separator:]', '\|', $val);
347 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $val, 'String');
351 $value = str_replace('[:separator:]', '\|', $value);
352 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
356 //FIX for custom data query fired against no value(NULL/NOT NULL)
357 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
359 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $qillValue";
363 case 'ContactReference':
364 $label = $value ? CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
365 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
366 $this->_qill
[$grouping][] = $field['label'] . " $qillOp $label";
370 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
371 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);;
375 if (!is_array($value)) {
376 if (strtolower($value) == 'yes' ||
strtolower($value) == strtolower(ts('Yes'))) {
380 $value = (int) $value;
382 $value = ($value == 1) ?
1 : 0;
383 $qillValue = $value ?
'Yes' : 'No';
385 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Integer');
386 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
391 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'String');
392 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
396 $value = CRM_Utils_Array
::value($op, (array) $value, $value);
397 if (is_array($value)) {
398 foreach ($value as $key => $val) {
399 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
400 // Note the only place I can find that this code is reached by is searching a custom money field in advanced search.
401 // with euro style comma separators this doesn't work - with or without this cleanMoney.
402 // So this should be removed but is not increasing the brokeness IMHO
403 $value[$op][$key] = CRM_Utils_Rule
::cleanMoney($value[$key]);
407 // @todo - this clean money should be in the form layer - it's highly likely to be doing more harm than good here
408 // comments per above apply. cleanMoney
409 $value = CRM_Utils_Rule
::cleanMoney($value);
413 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Float');
414 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['label'], 2 => $qillOp, 3 => $qillValue]);
418 if (substr($name, -9, 9) !== '_relative') {
419 // Relative dates are handled in the buildRelativeDateQuery function.
420 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op, $value, 'Date');
421 list($qillOp, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $field['label'], $value, $op, [], CRM_Utils_Type
::T_DATE
);
422 $this->_qill
[$grouping][] = "{$field['label']} $qillOp '$qillVal'";
427 if ($op == 'IS NULL' ||
$op == 'IS NOT NULL' ||
$op == 'IS EMPTY' ||
$op == 'IS NOT EMPTY') {
437 $this->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($fieldName, $op);
438 $this->_qill
[$grouping][] = $field['label'] . " {$qillOp} ";
447 * Function that does the actual query generation.
448 * basically ties all the above functions together
453 public function query() {
459 if (!empty($this->_where
)) {
461 foreach ($this->_where
as $grouping => $values) {
462 if (!empty($values)) {
463 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
466 if (!empty($clauses)) {
467 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
472 implode(' , ', $this->_select
),
473 implode(' ', $this->_tables
),
479 * Join the custom table for the field in (if not already in the query).
481 * @param array $field
483 protected function joinCustomTableForField($field) {
484 $name = $field['table_name'];
485 $join = "\nLEFT JOIN $name ON $name.entity_id = {$field['search_table']}.id";
486 $this->_tables
[$name] = $this->_tables
[$name] ??
$join;
487 $this->_whereTables
[$name] = $this->_whereTables
[$name] ??
$join;