fix for custom fields
[civicrm-core.git] / CRM / Core / BAO / CustomQuery.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 *
31 * @package CRM
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Core_BAO_CustomQuery {
7da04cde 37 const PREFIX = 'custom_value_';
6a488035
TO
38
39 /**
d2e5d2ce 40 * The set of custom field ids.
6a488035
TO
41 *
42 * @var array
43 */
44 protected $_ids;
45
46 /**
d2e5d2ce 47 * The select clause.
6a488035
TO
48 *
49 * @var array
50 */
51 public $_select;
52
53 /**
d2e5d2ce 54 * The name of the elements that are in the select clause.
6a488035
TO
55 * used to extract the values
56 *
57 * @var array
58 */
59 public $_element;
60
61 /**
d2e5d2ce 62 * The tables involved in the query.
6a488035
TO
63 *
64 * @var array
65 */
66 public $_tables;
67 public $_whereTables;
68
69 /**
d2e5d2ce 70 * The where clause.
6a488035
TO
71 *
72 * @var array
73 */
74 public $_where;
75
76 /**
d2e5d2ce 77 * The english language version of the query.
6a488035
TO
78 *
79 * @var array
80 */
81 public $_qill;
82
83 /**
d2e5d2ce 84 * The cache to translate the option values into labels.
6a488035
TO
85 *
86 * @var array
87 */
88 public $_options;
89
90 /**
d2e5d2ce 91 * The custom fields information.
6a488035
TO
92 *
93 * @var array
94 */
95 public $_fields;
96
97 /**
98 * Searching for contacts?
99 *
100 * @var boolean
101 */
102 protected $_contactSearch;
103
247ad911 104 protected $_locationSpecificCustomFields;
442df34b 105
6a488035 106 /**
d2e5d2ce 107 * This stores custom data group types and tables that it extends.
6a488035
TO
108 *
109 * @var array
6a488035
TO
110 */
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',
5b37ca7b
EM
127 'Campaign' => 'civicrm_campaign',
128 'Survey' => 'civicrm_survey',
6a488035
TO
129 );
130
131 /**
d2e5d2ce 132 * Class constructor.
6a488035
TO
133 *
134 * Takes in a set of custom field ids andsets up the data structures to
135 * generate a query
136 *
6a0b768e
TO
137 * @param array $ids
138 * The set of custom field ids.
fd31fa4c
EM
139 *
140 * @param bool $contactSearch
141 * @param array $locationSpecificFields
442df34b 142 */
00be9182 143 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) {
6a488035 144 $this->_ids = &$ids;
247ad911 145 $this->_locationSpecificCustomFields = $locationSpecificFields;
6a488035 146
353ffa53
TO
147 $this->_select = array();
148 $this->_element = array();
149 $this->_tables = array();
6a488035 150 $this->_whereTables = array();
353ffa53
TO
151 $this->_where = array();
152 $this->_qill = array();
153 $this->_options = array();
6a488035
TO
154
155 $this->_fields = array();
156 $this->_contactSearch = $contactSearch;
157
158 if (empty($this->_ids)) {
159 return;
160 }
161
162 // initialize the field array
163 $tmpArray = array_keys($this->_ids);
164 $idString = implode(',', $tmpArray);
353ffa53 165 $query = "
6a488035
TO
166SELECT 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
174 AND g.is_active = 1
175 AND f.is_active = 1
176 AND f.id IN ( $idString )";
177
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];
184 }
185 elseif (in_array($extends, CRM_Contact_BAO_ContactType::subTypes())) {
186 // if $extends is a subtype, refer contact table
187 $extendsTable = self::$extendsMap['Contact'];
188 }
189 $this->_fields[$dao->id] = array(
190 'id' => $dao->id,
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,
199 );
200
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,
208 );
209
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;
215 }
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)
219 );
220 CRM_Core_Error::fatal($errorMessage);
221 }
222 }
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;
226 }
227
228 // build the cache for custom values with options (label => value)
229 if ($optionGroupID != NULL) {
230 $query = "
231SELECT label, value
232 FROM civicrm_option_value
233 WHERE option_group_id = $optionGroupID
234";
235
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;
242 }
243 else {
244 $this->_options[$dao->id][$option->value] = $option->label;
245 }
246 }
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);
251 }
252 }
253 }
254
255 /**
d2e5d2ce 256 * Generate the select clause and the associated tables.
6a488035
TO
257 * for the from clause
258 *
6a488035 259 * @return void
6a488035 260 */
00be9182 261 public function select() {
6a488035
TO
262 if (empty($this->_fields)) {
263 return;
264 }
265
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;
273 $joinTable = NULL;
dcf0d348
PJ
274 // CRM-14265
275 if ($field['extends'] == 'civicrm_group') {
276 return;
277 }
278 elseif ($field['extends'] == 'civicrm_contact') {
6a488035
TO
279 $joinTable = 'contact_a';
280 }
281 elseif ($field['extends'] == 'civicrm_contribution') {
dcf0d348 282 $joinTable = $field['extends'];
6a488035 283 }
dcf0d348
PJ
284 elseif (in_array($field['extends'], self::$extendsMap)) {
285 $joinTable = $field['extends'];
6a488035 286 }
dcf0d348
PJ
287 else {
288 return;
6a488035 289 }
dcf0d348
PJ
290
291 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id";
292
293 if ($this->_ids[$id]) {
294 $this->_whereTables[$name] = $this->_tables[$name];
6a488035
TO
295 }
296
297 if ($joinTable) {
442df34b
CW
298 $joinClause = 1;
299 $joinTableAlias = $joinTable;
300 // Set location-specific query
247ad911 301 if (isset($this->_locationSpecificCustomFields[$id])) {
302 list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$id];
442df34b
CW
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)";
305 }
306 $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
6a488035
TO
307 if ($this->_ids[$id]) {
308 $this->_whereTables[$name] = $this->_tables[$name];
309 }
310 if ($joinTable != 'contact_a') {
442df34b 311 $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause;
6a488035
TO
312 }
313 elseif ($this->_contactSearch) {
314 CRM_Contact_BAO_Query::$_openedPanes[ts('Custom Fields')] = TRUE;
315 }
316 }
317 }
318 }
319
320 /**
d2e5d2ce 321 * Generate the where clause and also the english language.
6a488035
TO
322 * equivalent
323 *
6a488035 324 * @return void
6a488035 325 */
00be9182 326 public function where() {
6a488035
TO
327 foreach ($this->_ids as $id => $values) {
328
329 // Fixed for Isuue CRM 607
330 if (CRM_Utils_Array::value($id, $this->_fields) === NULL ||
331 !$values
332 ) {
333 continue;
334 }
335
336 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
337
338 foreach ($values as $tuple) {
339 list($name, $op, $value, $grouping, $wildcard) = $tuple;
340
6a488035 341 $field = $this->_fields[$id];
6a488035 342
3130209f
CW
343 $fieldName = "{$field['table_name']}.{$field['column_name']}";
344
9f388466
CW
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);
348 }
349
05c5cbc8 350 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($field);
351
3130209f 352 // Handle multi-select search for any data type
05c5cbc8 353 if (is_array($value) && !$field['is_search_range'] && $field['data_type'] != 'String') {
3130209f 354 $wildcard = $isSerialized ? $wildcard : TRUE;
353ffa53
TO
355 $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array(
356 'field' => $name,
408b79bf 357 'context' => 'search',
353ffa53 358 ), array()));
3130209f
CW
359 $qillValue = '';
360 $sqlOP = $wildcard ? ' OR ' : ' AND ';
361 $sqlValue = array();
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);
366 if ($isSerialized) {
367 $sqlValue[] = "( $fieldName like '%" . CRM_Core_DAO::VALUE_SEPARATOR . $v . CRM_Core_DAO::VALUE_SEPARATOR . "%' ) ";
368 }
369 else {
370 $v = "'$v'";
371 }
372 }
373 if (!$isSerialized) {
374 $sqlValue = array("$fieldName IN (" . implode(',', $value) . ")");
375 }
376 $this->_where[$grouping][] = ' ( ' . implode($sqlOP, $sqlValue) . ' ) ';
05c5cbc8 377 $this->_qill[$grouping][] = "$field[label] $qillOp $qillValue";
3130209f 378 continue;
6a488035
TO
379 }
380
3130209f 381 // fix $value here to escape sql injection attacks
7ecb613a 382 $qillValue = NULL;
9f388466
CW
383 if (!is_array($value)) {
384 $value = CRM_Core_DAO::escapeString(trim($value));
7ecb613a 385 $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value, $id, $this->_options);
386 }
387 elseif (count($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
388 $op = key($value);
389 $qillValue = CRM_Core_BAO_CustomField::getDisplayValue($value[$op], $id, $this->_options);
9f388466 390 }
3130209f 391
2c261619 392 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
3130209f 393
6a488035
TO
394 switch ($field['data_type']) {
395 case 'String':
396 $sql = "$fieldName";
3130209f
CW
397
398 if ($field['is_search_range'] && is_array($value)) {
399 $this->searchRange($field['id'],
400 $field['label'],
401 $field['data_type'],
402 $fieldName,
403 $value,
404 $grouping
405 );
6a488035
TO
406 }
407 else {
05c5cbc8 408 // fix $value here to escape sql injection attacks
409 if (!is_array($value)) {
410 $value = CRM_Utils_Type::escape($strtolower($value), 'String');
411 }
412 elseif ($isSerialized && strstr($op, 'IN')) {
413 $value = implode(',', $value);
414 }
3130209f 415
2c261619 416 // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
05c5cbc8 417 if (!empty($value)) {
418 if ($isSerialized) {
2c261619 419 if (strstr($op, 'IN')) {
05c5cbc8 420 $value = str_replace(array('(', ')'), '', str_replace(",", "[[:cntrl:]]|[[:cntrl:]]", $value));
2c261619 421 }
422 $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
05c5cbc8 423 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
2c261619 424 }
425 elseif ($wildcard) {
05c5cbc8 426 $value = "[[:cntrl:]]%$value%[[:cntrl:]]";
2c261619 427 $op = 'LIKE';
428 }
6a488035 429 }
3130209f
CW
430
431 //FIX for custom data query fired against no value(NULL/NOT NULL)
05c5cbc8 432 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($sql, $op, $value, $field['data_type']);
2c261619 433 $this->_qill[$grouping][] = "$field[label] $qillOp $qillValue";
6a488035 434 }
3130209f 435 break;
6a488035
TO
436
437 case 'ContactReference':
438 $label = $value ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
439 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
2c261619 440 $this->_qill[$grouping][] = $field['label'] . " $qillOp $label";
3130209f 441 break;
6a488035
TO
442
443 case 'Int':
444 if ($field['is_search_range'] && is_array($value)) {
445 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
446 }
447 else {
448 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
2c261619 449 $this->_qill[$grouping][] = $field['label'] . " $qillOp $value";
6a488035 450 }
3130209f 451 break;
6a488035
TO
452
453 case 'Boolean':
454 if (strtolower($value) == 'yes' || strtolower($value) == strtolower(ts('Yes'))) {
455 $value = 1;
456 }
457 else {
458 $value = (int) $value;
459 }
460 $value = ($value == 1) ? 1 : 0;
461 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
462 $value = $value ? ts('Yes') : ts('No');
2c261619 463 $this->_qill[$grouping][] = $field['label'] . " $qillOp {$value}";
3130209f 464 break;
6a488035
TO
465
466 case 'Link':
467 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
2c261619 468 $this->_qill[$grouping][] = $field['label'] . " $qillOp $value";
3130209f 469 break;
6a488035
TO
470
471 case 'Float':
472 if ($field['is_search_range'] && is_array($value)) {
473 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
474 }
475 else {
476 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
2c261619 477 $this->_qill[$grouping][] = $field['label'] . " $qillOp {$value}";
6a488035 478 }
3130209f 479 break;
6a488035
TO
480
481 case 'Money':
482 if ($field['is_search_range'] && is_array($value)) {
483 foreach ($value as $key => $val) {
484 $moneyFormat = CRM_Utils_Rule::cleanMoney($value[$key]);
485 $value[$key] = $moneyFormat;
486 }
487 $this->searchRange($field['id'], $field['label'], $field['data_type'], $fieldName, $value, $grouping);
488 }
489 else {
490 $moneyFormat = CRM_Utils_Rule::cleanMoney($value);
491 $value = $moneyFormat;
492 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
2c261619 493 $this->_qill[$grouping][] = $field['label'] . " {$qillOp} {$value}";
6a488035 494 }
3130209f 495 break;
6a488035
TO
496
497 case 'Memo':
498 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
2c261619 499 $this->_qill[$grouping][] = "$field[label] $qillOp $value";
3130209f 500 break;
6a488035
TO
501
502 case 'Date':
503 $fromValue = CRM_Utils_Array::value('from', $value);
504 $toValue = CRM_Utils_Array::value('to', $value);
505
506 if (!$fromValue && !$toValue) {
1f87870b 507 if (!CRM_Utils_Date::processDate($value) && !in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
6a488035
TO
508 continue;
509 }
510
511 // hack to handle yy format during search
512 if (is_numeric($value) && strlen($value) == 4) {
513 $value = "01-01-{$value}";
514 }
515
516 $date = CRM_Utils_Date::processDate($value);
517 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $date, 'String');
2c261619 518 $this->_qill[$grouping][] = $field['label'] . " {$qillOp} " . CRM_Utils_Date::customFormat($date);
6a488035
TO
519 }
520 else {
521 if (is_numeric($fromValue) && strlen($fromValue) == 4) {
522 $fromValue = "01-01-{$fromValue}";
523 }
524
525 if (is_numeric($toValue) && strlen($toValue) == 4) {
526 $toValue = "01-01-{$toValue}";
527 }
528
529 // TO DO: add / remove time based on date parts
530 $fromDate = CRM_Utils_Date::processDate($fromValue);
531 $toDate = CRM_Utils_Date::processDate($toValue);
532 if (!$fromDate && !$toDate) {
533 continue;
534 }
535 if ($fromDate) {
536 $this->_where[$grouping][] = "$fieldName >= $fromDate";
537 $this->_qill[$grouping][] = $field['label'] . ' >= ' . CRM_Utils_Date::customFormat($fromDate);
538 }
539 if ($toDate) {
540 $this->_where[$grouping][] = "$fieldName <= $toDate";
541 $this->_qill[$grouping][] = $field['label'] . ' <= ' . CRM_Utils_Date::customFormat($toDate);
542 }
543 }
3130209f 544 break;
6a488035
TO
545
546 case 'StateProvince':
547 case 'Country':
3130209f 548 $this->_where[$grouping][] = "$fieldName {$op} " . CRM_Utils_Type::escape($value, 'Int');
2c261619 549 $this->_qill[$grouping][] = $field['label'] . " {$qillOp} {$qillValue}";
3130209f 550 break;
6a488035
TO
551
552 case 'File':
481a74f4 553 if ($op == 'IS NULL' || $op == 'IS NOT NULL' || $op == 'IS EMPTY' || $op == 'IS NOT EMPTY') {
6a488035
TO
554 switch ($op) {
555 case 'IS EMPTY':
556 $op = 'IS NULL';
557 break;
2aa397bc 558
6a488035
TO
559 case 'IS NOT EMPTY':
560 $op = 'IS NOT NULL';
561 break;
562 }
563 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op);
2c261619 564 $this->_qill[$grouping][] = $field['label'] . " {$qillOp} ";
6a488035 565 }
3130209f 566 break;
6a488035
TO
567 }
568 }
569 }
570 }
571
572 /**
d2e5d2ce 573 * Function that does the actual query generation.
6a488035
TO
574 * basically ties all the above functions together
575 *
a6c01b45
CW
576 * @return array
577 * array of strings
6a488035 578 */
00be9182 579 public function query() {
6a488035
TO
580 $this->select();
581
582 $this->where();
583
584 $whereStr = NULL;
585 if (!empty($this->_where)) {
586 $clauses = array();
587 foreach ($this->_where as $grouping => $values) {
588 if (!empty($values)) {
589 $clauses[] = ' ( ' . implode(' AND ', $values) . ' ) ';
590 }
591 }
592 if (!empty($clauses)) {
593 $whereStr = ' ( ' . implode(' OR ', $clauses) . ' ) ';
594 }
595 }
596
353ffa53
TO
597 return array(
598 implode(' , ', $this->_select),
6a488035
TO
599 implode(' ', $this->_tables),
600 $whereStr,
601 );
602 }
603
b5c2afd0 604 /**
100fef9d 605 * @param int $id
b5c2afd0
EM
606 * @param $label
607 * @param $type
100fef9d 608 * @param string $fieldName
b5c2afd0
EM
609 * @param $value
610 * @param $grouping
611 */
00be9182 612 public function searchRange(&$id, &$label, $type, $fieldName, &$value, &$grouping) {
6a488035
TO
613 $qill = array();
614
615 if (isset($value['from'])) {
616 $val = CRM_Utils_Type::escape($value['from'], $type);
617
618 if ($type == 'String') {
619 $this->_where[$grouping][] = "$fieldName >= '$val'";
620 }
621 else {
622 $this->_where[$grouping][] = "$fieldName >= $val";
623 }
624 $qill[] = ts('greater than or equal to \'%1\'', array(1 => $value['from']));
625 }
626
627 if (isset($value['to'])) {
628 $val = CRM_Utils_Type::escape($value['to'], $type);
629 if ($type == 'String') {
630 $this->_where[$grouping][] = "$fieldName <= '$val'";
631 }
632 else {
633 $this->_where[$grouping][] = "$fieldName <= $val";
634 }
635 $qill[] = ts('less than or equal to \'%1\'', array(1 => $value['to']));
636 }
637
638 if (!empty($qill)) {
639 $this->_qill[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);
640 }
641 }
96025800 642
6a488035 643}