Merge pull request #13555 from colemanw/loadScript
[civicrm-core.git] / CRM / Contact / BAO / Query.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
dc195289 35 * This is the heart of the search query building mechanism.
6a488035
TO
36 */
37class CRM_Contact_BAO_Query {
38
39 /**
fe482240 40 * The various search modes.
6a488035 41 *
746aa919
CB
42 * As of February 2017, entries not present for 4, 32, 64, 1024.
43 *
44 * MODE_ALL seems to be out of sync with the available constants;
45 * if this is intentionally excluding MODE_MAILING then that may
46 * bear documenting?
47 *
48 * Likewise if there's reason for the missing modes (4, 32, 64 etc).
49 *
6a488035
TO
50 * @var int
51 */
7da04cde 52 const
33092c89 53 NO_RETURN_PROPERTIES = 'CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES',
6a488035
TO
54 MODE_CONTACTS = 1,
55 MODE_CONTRIBUTE = 2,
746aa919 56 // There is no 4,
6a488035
TO
57 MODE_MEMBER = 8,
58 MODE_EVENT = 16,
eda34f9b
MW
59 MODE_CONTACTSRELATED = 32,
60 // no 64.
6a488035
TO
61 MODE_GRANT = 128,
62 MODE_PLEDGEBANK = 256,
63 MODE_PLEDGE = 512,
746aa919 64 // There is no 1024,
6a488035 65 MODE_CASE = 2048,
6a488035
TO
66 MODE_ACTIVITY = 4096,
67 MODE_CAMPAIGN = 8192,
746aa919
CB
68 MODE_MAILING = 16384,
69 MODE_ALL = 17407;
6a488035 70
eda34f9b
MW
71 /**
72 * Constants for search operators
73 */
74 const
75 SEARCH_OPERATOR_AND = 'AND',
76 SEARCH_OPERATOR_OR = 'OR';
77
6a488035 78 /**
fe482240 79 * The default set of return properties.
6a488035
TO
80 *
81 * @var array
6a488035
TO
82 */
83 static $_defaultReturnProperties = NULL;
84
85 /**
fe482240 86 * The default set of hier return properties.
6a488035
TO
87 *
88 * @var array
6a488035
TO
89 */
90 static $_defaultHierReturnProperties;
91
92 /**
fe482240 93 * The set of input params.
6a488035
TO
94 *
95 * @var array
96 */
97 public $_params;
98
99 public $_cfIDs;
100
101 public $_paramLookup;
102
ac4985d5 103 public $_sort;
746aa919 104
6a488035 105 /**
100fef9d 106 * The set of output params
6a488035
TO
107 *
108 * @var array
109 */
110 public $_returnProperties;
111
112 /**
100fef9d 113 * The select clause
6a488035
TO
114 *
115 * @var array
116 */
117 public $_select;
118
119 /**
100fef9d 120 * The name of the elements that are in the select clause
746aa919 121 * used to extract the values.
6a488035
TO
122 *
123 * @var array
124 */
125 public $_element;
126
127 /**
746aa919 128 * The tables involved in the query.
6a488035
TO
129 *
130 * @var array
131 */
132 public $_tables;
133
134 /**
746aa919 135 * The table involved in the where clause.
6a488035
TO
136 *
137 * @var array
138 */
139 public $_whereTables;
140
141 /**
746aa919 142 * Array of WHERE clause components.
6a488035
TO
143 *
144 * @var array
145 */
146 public $_where;
147
148 /**
746aa919 149 * The WHERE clause as a string.
6a488035
TO
150 *
151 * @var string
6a488035
TO
152 */
153 public $_whereClause;
154
155 /**
746aa919 156 * Additional WHERE clause for permissions.
6a488035
TO
157 *
158 * @var string
6a488035
TO
159 */
160 public $_permissionWhereClause;
a30e57e0 161
6a488035 162 /**
100fef9d 163 * The from string
6a488035
TO
164 *
165 * @var string
6a488035
TO
166 */
167 public $_fromClause;
168
169 /**
100fef9d 170 * Additional permission from clause
6a488035
TO
171 *
172 * @var string
6a488035
TO
173 */
174 public $_permissionFromClause;
175
176 /**
100fef9d 177 * The from clause for the simple select and alphabetical
6a488035
TO
178 * select
179 *
180 * @var string
181 */
182 public $_simpleFromClause;
183
184 /**
100fef9d 185 * The having values
6a488035
TO
186 *
187 * @var string
6a488035
TO
188 */
189 public $_having;
190
191 /**
192 * The english language version of the query
193 *
194 * @var array
195 */
196 public $_qill;
197
198 /**
199 * All the fields that could potentially be involved in
200 * this query
201 *
202 * @var array
203 */
204 public $_fields;
205
206 /**
fe482240 207 * The cache to translate the option values into labels.
6a488035
TO
208 *
209 * @var array
210 */
211 public $_options;
212
213 /**
fe482240 214 * Are we in search mode.
6a488035
TO
215 *
216 * @var boolean
217 */
218 public $_search = TRUE;
219
220 /**
fe482240 221 * Should we skip permission checking.
6a488035
TO
222 *
223 * @var boolean
224 */
225 public $_skipPermission = FALSE;
226
227 /**
fe482240 228 * Should we skip adding of delete clause.
6a488035
TO
229 *
230 * @var boolean
231 */
232 public $_skipDeleteClause = FALSE;
233
234 /**
100fef9d 235 * Are we in strict mode (use equality over LIKE)
6a488035
TO
236 *
237 * @var boolean
238 */
239 public $_strict = FALSE;
240
241 /**
fe482240 242 * What operator to use to group the clauses.
6a488035
TO
243 *
244 * @var string
245 */
246 public $_operator = 'AND';
247
248 public $_mode = 1;
249
250 /**
fe482240 251 * Should we only search on primary location.
6a488035
TO
252 *
253 * @var boolean
254 */
255 public $_primaryLocation = TRUE;
256
257 /**
fe482240 258 * Are contact ids part of the query.
6a488035
TO
259 *
260 * @var boolean
261 */
262 public $_includeContactIds = FALSE;
263
264 /**
fe482240 265 * Should we use the smart group cache.
6a488035
TO
266 *
267 * @var boolean
268 */
269 public $_smartGroupCache = TRUE;
270
271 /**
fe482240 272 * Should we display contacts with a specific relationship type.
6a488035
TO
273 *
274 * @var string
275 */
276 public $_displayRelationshipType = NULL;
277
278 /**
fe482240 279 * Reference to the query object for custom values.
6a488035
TO
280 *
281 * @var Object
282 */
283 public $_customQuery;
284
285 /**
100fef9d 286 * Should we enable the distinct clause, used if we are including
6a488035
TO
287 * more than one group
288 *
289 * @var boolean
290 */
291 public $_useDistinct = FALSE;
292
293 /**
294 * Should we just display one contact record
295 */
296 public $_useGroupBy = FALSE;
297
298 /**
100fef9d 299 * The relationship type direction
6a488035
TO
300 *
301 * @var array
6a488035
TO
302 */
303 static $_relType;
304
305 /**
100fef9d 306 * The activity role
6a488035
TO
307 *
308 * @var array
6a488035
TO
309 */
310 static $_activityRole;
311
312 /**
313 * Consider the component activity type
314 * during activity search.
315 *
316 * @var array
6a488035
TO
317 */
318 static $_considerCompActivities;
319
320 /**
321 * Consider with contact activities only,
322 * during activity search.
323 *
324 * @var array
6a488035
TO
325 */
326 static $_withContactActivitiesOnly;
327
328 /**
100fef9d 329 * Use distinct component clause for component searches
6a488035
TO
330 *
331 * @var string
332 */
333 public $_distinctComponentClause;
334
3dbf477c
DS
335 public $_rowCountClause;
336
6a488035 337 /**
100fef9d 338 * Use groupBy component clause for component searches
6a488035
TO
339 *
340 * @var string
341 */
342 public $_groupByComponentClause;
343
344 /**
345 * Track open panes, useful in advance search
346 *
347 * @var array
6a488035
TO
348 */
349 public static $_openedPanes = array();
350
442df34b
CW
351 /**
352 * For search builder - which custom fields are location-dependent
353 * @var array
354 */
355 public $_locationSpecificCustomFields = array();
356
6a488035
TO
357 /**
358 * The tables which have a dependency on location and/or address
359 *
360 * @var array
6a488035
TO
361 */
362 static $_dependencies = array(
363 'civicrm_state_province' => 1,
364 'civicrm_country' => 1,
365 'civicrm_county' => 1,
366 'civicrm_address' => 1,
367 'civicrm_location_type' => 1,
368 );
369
370 /**
fe482240 371 * List of location specific fields.
6a488035
TO
372 */
373 static $_locationSpecificFields = array(
374 'street_address',
375 'street_number',
376 'street_name',
377 'street_unit',
378 'supplemental_address_1',
379 'supplemental_address_2',
207f62c6 380 'supplemental_address_3',
6a488035
TO
381 'city',
382 'postal_code',
383 'postal_code_suffix',
384 'geo_code_1',
385 'geo_code_2',
386 'state_province',
387 'country',
388 'county',
389 'phone',
390 'email',
391 'im',
392 'address_name',
994a070c 393 'master_id',
6a488035
TO
394 );
395
396 /**
4e6072d5 397 * Remember if we handle either end of a number or date range
6a488035
TO
398 * so we can skip the other
399 */
400 protected $_rangeCache = array();
d9ab802d
PJ
401 /**
402 * Set to true when $this->relationship is run to avoid adding twice
403 * @var Boolean
404 */
ad682134 405 protected $_relationshipValuesAdded = FALSE;
d9ab802d 406
7c96365b 407 /**
408 * Set to the name of the temp table if one has been created
409 * @var String
410 */
411 static $_relationshipTempTable = NULL;
d9ab802d 412
7c96365b 413 public $_pseudoConstantsSelect = array();
9131c564 414
6a488035 415 /**
fe482240 416 * Class constructor which also does all the work.
6a488035 417 *
2a6da8d7
EM
418 * @param array $params
419 * @param array $returnProperties
420 * @param array $fields
77c5b619
TO
421 * @param bool $includeContactIds
422 * @param bool $strict
2a6da8d7 423 * @param bool|int $mode - mode the search is operating on
6a488035 424 *
2a6da8d7
EM
425 * @param bool $skipPermission
426 * @param bool $searchDescendentGroups
427 * @param bool $smartGroupCache
428 * @param null $displayRelationshipType
429 * @param string $operator
66670e4d 430 * @param string $apiEntity
0606198b 431 * @param bool|NULL $primaryLocationOnly
6a488035 432 */
59f4c9ee 433 public function __construct(
6a488035
TO
434 $params = NULL, $returnProperties = NULL, $fields = NULL,
435 $includeContactIds = FALSE, $strict = FALSE, $mode = 1,
436 $skipPermission = FALSE, $searchDescendentGroups = TRUE,
437 $smartGroupCache = TRUE, $displayRelationshipType = NULL,
66670e4d 438 $operator = 'AND',
0606198b 439 $apiEntity = NULL,
440 $primaryLocationOnly = NULL
6a488035 441 ) {
0606198b 442 if ($primaryLocationOnly === NULL) {
443 $primaryLocationOnly = Civi::settings()->get('searchPrimaryDetailsOnly');
444 }
445 $this->_primaryLocation = $primaryLocationOnly;
6a488035
TO
446 $this->_params = &$params;
447 if ($this->_params == NULL) {
448 $this->_params = array();
449 }
450
33092c89
SB
451 if ($returnProperties === self::NO_RETURN_PROPERTIES) {
452 $this->_returnProperties = array();
453 }
454 elseif (empty($returnProperties)) {
6a488035
TO
455 $this->_returnProperties = self::defaultReturnProperties($mode);
456 }
457 else {
458 $this->_returnProperties = &$returnProperties;
459 }
460
461 $this->_includeContactIds = $includeContactIds;
462 $this->_strict = $strict;
463 $this->_mode = $mode;
464 $this->_skipPermission = $skipPermission;
465 $this->_smartGroupCache = $smartGroupCache;
466 $this->_displayRelationshipType = $displayRelationshipType;
467 $this->setOperator($operator);
468
469 if ($fields) {
470 $this->_fields = &$fields;
471 $this->_search = FALSE;
472 $this->_skipPermission = TRUE;
473 }
474 else {
5837835b 475 $this->_fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission);
6a488035 476
5837835b 477 $fields = CRM_Core_Component::getQueryFields(!$this->_skipPermission);
6a488035
TO
478 unset($fields['note']);
479 $this->_fields = array_merge($this->_fields, $fields);
480
481 // add activity fields
482 $fields = CRM_Activity_BAO_Activity::exportableFields();
483 $this->_fields = array_merge($this->_fields, $fields);
f2a9b25c 484
99e9587a
DS
485 // add any fields provided by hook implementers
486 $extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields();
f2a9b25c 487 $this->_fields = array_merge($this->_fields, $extFields);
6a488035
TO
488 }
489
490 // basically do all the work once, and then reuse it
66670e4d 491 $this->initialize($apiEntity);
6a488035
TO
492 }
493
494 /**
fe482240 495 * Function which actually does all the work for the constructor.
8f165fa5 496 *
497 * @param string $apiEntity
498 * The api entity being called.
499 * This sort-of duplicates $mode in a confusing way. Probably not by design.
6a488035 500 */
66670e4d 501 public function initialize($apiEntity = NULL) {
6a488035
TO
502 $this->_select = array();
503 $this->_element = array();
504 $this->_tables = array();
505 $this->_whereTables = array();
506 $this->_where = array();
507 $this->_qill = array();
508 $this->_options = array();
509 $this->_cfIDs = array();
510 $this->_paramLookup = array();
511 $this->_having = array();
512
513 $this->_customQuery = NULL;
514
515 // reset cached static variables - CRM-5803
516 self::$_activityRole = NULL;
517 self::$_considerCompActivities = NULL;
518 self::$_withContactActivitiesOnly = NULL;
519
520 $this->_select['contact_id'] = 'contact_a.id as contact_id';
521 $this->_element['contact_id'] = 1;
522 $this->_tables['civicrm_contact'] = 1;
523
524 if (!empty($this->_params)) {
525 $this->buildParamsLookup();
526 }
527
528 $this->_whereTables = $this->_tables;
529
66670e4d 530 $this->selectClause($apiEntity);
57f8e7f0 531 $this->_whereClause = $this->whereClause($apiEntity);
be274d8a
E
532 if (array_key_exists('civicrm_contribution', $this->_whereTables)) {
533 $component = 'contribution';
534 }
535 if (array_key_exists('civicrm_membership', $this->_whereTables)) {
536 $component = 'membership';
537 }
ecb39dfd
E
538 if (isset($component)) {
539 CRM_Financial_BAO_FinancialType::buildPermissionedClause($this->_whereClause, $component);
540 }
6a488035 541
b3e1c09d 542 $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode, $apiEntity);
6a488035
TO
543 $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode);
544
545 $this->openedSearchPanes(TRUE);
546 }
547
67d19299 548 /**
bb05da0c 549 * Function for same purpose as convertFormValues.
550 *
551 * Like convert form values this function exists to pre-Process parameters from the form.
552 *
553 * It is unclear why they are different functions & likely relates to advances search
554 * versus search builder.
555 *
556 * The direction we are going is having the form convert values to a standardised format &
8f165fa5 557 * moving away from weird & wonderful where clause switches.
bb05da0c 558 *
67d19299 559 * Fix and handle contact deletion nicely.
560 *
561 * this code is primarily for search builder use case where different clauses can specify if they want deleted.
562 *
563 * CRM-11971
564 */
00be9182 565 public function buildParamsLookup() {
6a488035 566 $trashParamExists = FALSE;
5c7dad85
TO
567 $paramByGroup = array();
568 foreach ($this->_params as $k => $param) {
569 if (!empty($param[0]) && $param[0] == 'contact_is_deleted') {
6a488035
TO
570 $trashParamExists = TRUE;
571 }
e6f3f602 572 if (!empty($param[3])) {
573 $paramByGroup[$param[3]][$k] = $param;
574 }
6a488035
TO
575 }
576
5c7dad85 577 if ($trashParamExists) {
6a488035
TO
578 $this->_skipDeleteClause = TRUE;
579
580 //cycle through group sets and explicitly add trash param if not set
5c7dad85 581 foreach ($paramByGroup as $setID => $set) {
6a488035
TO
582 if (
583 !in_array(array('contact_is_deleted', '=', '1', $setID, '0'), $this->_params) &&
5c7dad85
TO
584 !in_array(array('contact_is_deleted', '=', '0', $setID, '0'), $this->_params)
585 ) {
6a488035
TO
586 $this->_params[] = array(
587 'contact_is_deleted',
588 '=',
589 '0',
590 $setID,
591 '0',
592 );
593 }
594 }
595 }
596
597 foreach ($this->_params as $value) {
a7488080 598 if (empty($value[0])) {
6a488035
TO
599 continue;
600 }
601 $cfID = CRM_Core_BAO_CustomField::getKeyID($value[0]);
602 if ($cfID) {
603 if (!array_key_exists($cfID, $this->_cfIDs)) {
604 $this->_cfIDs[$cfID] = array();
605 }
3130209f
CW
606 // Set wildcard value based on "and/or" selection
607 foreach ($this->_params as $key => $param) {
608 if ($param[0] == $value[0] . '_operator') {
609 $value[4] = $param[2] == 'or';
610 break;
611 }
612 }
6a488035
TO
613 $this->_cfIDs[$cfID][] = $value;
614 }
615
616 if (!array_key_exists($value[0], $this->_paramLookup)) {
617 $this->_paramLookup[$value[0]] = array();
618 }
485a3a1f 619 if ($value[0] !== 'group') {
8f165fa5 620 // Just trying to unravel how group interacts here! This whole function is weird.
485a3a1f 621 $this->_paramLookup[$value[0]][] = $value;
622 }
6a488035
TO
623 }
624 }
625
626 /**
67d19299 627 * Some composite fields do not appear in the fields array hack to make them part of the query.
8f165fa5 628 *
629 * @param $apiEntity
630 * The api entity being called.
631 * This sort-of duplicates $mode in a confusing way. Probably not by design.
6a488035 632 */
66670e4d 633 public function addSpecialFields($apiEntity) {
6a488035 634 static $special = array('contact_type', 'contact_sub_type', 'sort_name', 'display_name');
66670e4d 635 // if get called via Contact.get API having address_id as return parameter
636 if ($apiEntity == 'Contact') {
637 $special[] = 'address_id';
638 }
6a488035 639 foreach ($special as $name) {
a7488080 640 if (!empty($this->_returnProperties[$name])) {
66670e4d 641 if ($name == 'address_id') {
642 $this->_tables['civicrm_address'] = 1;
643 $this->_select['address_id'] = 'civicrm_address.id as address_id';
644 $this->_element['address_id'] = 1;
645 }
646 else {
647 $this->_select[$name] = "contact_a.{$name} as $name";
648 $this->_element[$name] = 1;
649 }
6a488035
TO
650 }
651 }
652 }
653
654 /**
655 * Given a list of conditions in params and a list of desired
656 * return Properties generate the required select and from
657 * clauses. Note that since the where clause introduces new
658 * tables, the initial attempt also retrieves all variables used
659 * in the params list
8f165fa5 660 *
661 * @param string $apiEntity
662 * The api entity being called.
663 * This sort-of duplicates $mode in a confusing way. Probably not by design.
6a488035 664 */
66670e4d 665 public function selectClause($apiEntity = NULL) {
6a488035 666
3af96592 667 // @todo Tidy up this. This arises because 1) we are ignoring the $mode & adding a new
668 // param ($apiEntity) instead - presumably an oversight & 2 because
669 // contact is not implemented as a component.
66670e4d 670 $this->addSpecialFields($apiEntity);
6a488035
TO
671
672 foreach ($this->_fields as $name => $field) {
6a488035
TO
673 // skip component fields
674 // there are done by the alter query below
675 // and need not be done on every field
3af96592 676 // @todo remove these & handle using metadata - only obscure fields
677 // that are hack-added should need to be excluded from the main loop.
1071730c
DL
678 if (
679 (substr($name, 0, 12) == 'participant_') ||
6a488035 680 (substr($name, 0, 7) == 'pledge_') ||
9d5c7f14 681 (substr($name, 0, 5) == 'case_')
6a488035
TO
682 ) {
683 continue;
684 }
685
686 // redirect to activity select clause
4c24c842 687 if (
86538308 688 (substr($name, 0, 9) == 'activity_') ||
4c24c842
PN
689 ($name == 'parent_id')
690 ) {
6a488035 691 CRM_Activity_BAO_Query::select($this);
6a488035
TO
692 }
693
694 // if this is a hierarchical name, we ignore it
695 $names = explode('-', $name);
0742f843 696 if (count($names) > 1 && isset($names[1]) && is_numeric($names[1])) {
6a488035
TO
697 continue;
698 }
699
c6ff5b0d 700 // make an exception for special cases, to add the field in select clause
fd18baa6 701 $makeException = FALSE;
c6ff5b0d
KJ
702
703 //special handling for groups/tags
704 if (in_array($name, array('groups', 'tags', 'notes'))
fd18baa6
KJ
705 && isset($this->_returnProperties[substr($name, 0, -1)])
706 ) {
8f165fa5 707 // @todo instead of setting make exception to get us into
708 // an if clause that has handling for these fields buried with in it
709 // move the handling to here.
fd18baa6
KJ
710 $makeException = TRUE;
711 }
712
c6ff5b0d
KJ
713 // since note has 3 different options we need special handling
714 // note / note_subject / note_body
715 if ($name == 'notes') {
716 foreach (array('note', 'note_subject', 'note_body') as $noteField) {
717 if (isset($this->_returnProperties[$noteField])) {
718 $makeException = TRUE;
719 break;
720 }
721 }
722 }
a87ba7c0 723
6a488035 724 $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
86ab13b7 725 if (
726 !empty($this->_paramLookup[$name])
727 || !empty($this->_returnProperties[$name])
728 || $this->pseudoConstantNameIsInReturnProperties($field)
729 || $makeException
6a488035 730 ) {
6a488035
TO
731 if ($cfID) {
732 // add to cfIDs array if not present
733 if (!array_key_exists($cfID, $this->_cfIDs)) {
734 $this->_cfIDs[$cfID] = array();
735 }
736 }
737 elseif (isset($field['where'])) {
738 list($tableName, $fieldName) = explode('.', $field['where'], 2);
739 if (isset($tableName)) {
6a488035
TO
740 if (CRM_Utils_Array::value($tableName, self::$_dependencies)) {
741 $this->_tables['civicrm_address'] = 1;
742 $this->_select['address_id'] = 'civicrm_address.id as address_id';
743 $this->_element['address_id'] = 1;
744 }
745
d9ab802d
PJ
746 if ($tableName == 'im_provider' || $tableName == 'email_greeting' ||
747 $tableName == 'postal_greeting' || $tableName == 'addressee'
6a488035 748 ) {
d9ab802d
PJ
749 if ($tableName == 'im_provider') {
750 CRM_Core_OptionValue::select($this);
751 }
752
753 if (in_array($tableName,
5c7dad85 754 array('email_greeting', 'postal_greeting', 'addressee'))) {
d9ab802d
PJ
755 $this->_element["{$name}_id"] = 1;
756 $this->_select["{$name}_id"] = "contact_a.{$name}_id as {$name}_id";
757 $this->_pseudoConstantsSelect[$name] = array('pseudoField' => $tableName, 'idCol' => "{$name}_id");
758 $this->_pseudoConstantsSelect[$name]['select'] = "{$name}.{$fieldName} as $name";
759 $this->_pseudoConstantsSelect[$name]['element'] = $name;
760
761 if ($tableName == 'email_greeting') {
3af96592 762 // @todo bad join.
59f4c9ee
TO
763 $this->_pseudoConstantsSelect[$name]['join']
764 = " LEFT JOIN civicrm_option_group option_group_email_greeting ON (option_group_email_greeting.name = 'email_greeting')";
d9ab802d
PJ
765 $this->_pseudoConstantsSelect[$name]['join'] .=
766 " LEFT JOIN civicrm_option_value email_greeting ON (contact_a.email_greeting_id = email_greeting.value AND option_group_email_greeting.id = email_greeting.option_group_id ) ";
767 }
768 elseif ($tableName == 'postal_greeting') {
3af96592 769 // @todo bad join.
59f4c9ee
TO
770 $this->_pseudoConstantsSelect[$name]['join']
771 = " LEFT JOIN civicrm_option_group option_group_postal_greeting ON (option_group_postal_greeting.name = 'postal_greeting')";
d9ab802d
PJ
772 $this->_pseudoConstantsSelect[$name]['join'] .=
773 " LEFT JOIN civicrm_option_value postal_greeting ON (contact_a.postal_greeting_id = postal_greeting.value AND option_group_postal_greeting.id = postal_greeting.option_group_id ) ";
774 }
775 elseif ($tableName == 'addressee') {
3af96592 776 // @todo bad join.
59f4c9ee
TO
777 $this->_pseudoConstantsSelect[$name]['join']
778 = " LEFT JOIN civicrm_option_group option_group_addressee ON (option_group_addressee.name = 'addressee')";
d9ab802d
PJ
779 $this->_pseudoConstantsSelect[$name]['join'] .=
780 " LEFT JOIN civicrm_option_value addressee ON (contact_a.addressee_id = addressee.value AND option_group_addressee.id = addressee.option_group_id ) ";
781 }
782 $this->_pseudoConstantsSelect[$name]['table'] = $tableName;
783
6a488035
TO
784 //get display
785 $greetField = "{$name}_display";
786 $this->_select[$greetField] = "contact_a.{$greetField} as {$greetField}";
787 $this->_element[$greetField] = 1;
788 //get custom
789 $greetField = "{$name}_custom";
790 $this->_select[$greetField] = "contact_a.{$greetField} as {$greetField}";
791 $this->_element[$greetField] = 1;
792 }
793 }
794 else {
d9ab802d
PJ
795 if (!in_array($tableName, array('civicrm_state_province', 'civicrm_country', 'civicrm_county'))) {
796 $this->_tables[$tableName] = 1;
797 }
6a488035
TO
798
799 // also get the id of the tableName
800 $tName = substr($tableName, 8);
d9ab802d 801 if (in_array($tName, array('country', 'state_province', 'county'))) {
d9ab802d 802 if ($tName == 'state_province') {
59f4c9ee
TO
803 $this->_pseudoConstantsSelect['state_province_name'] = array(
804 'pseudoField' => "{$tName}",
805 'idCol' => "{$tName}_id",
806 'bao' => 'CRM_Core_BAO_Address',
807 'table' => "civicrm_{$tName}",
808 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
809 );
810
811 $this->_pseudoConstantsSelect[$tName] = array(
812 'pseudoField' => 'state_province_abbreviation',
813 'idCol' => "{$tName}_id",
814 'table' => "civicrm_{$tName}",
815 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
816 );
d9ab802d 817 }
7f84f734 818 else {
59f4c9ee
TO
819 $this->_pseudoConstantsSelect[$name] = array(
820 'pseudoField' => "{$tName}_id",
821 'idCol' => "{$tName}_id",
822 'bao' => 'CRM_Core_BAO_Address',
823 'table' => "civicrm_{$tName}",
824 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
825 );
7f84f734 826 }
6a488035 827
d9ab802d
PJ
828 $this->_select["{$tName}_id"] = "civicrm_address.{$tName}_id as {$tName}_id";
829 $this->_element["{$tName}_id"] = 1;
830 }
831 elseif ($tName != 'contact') {
6a488035
TO
832 $this->_select["{$tName}_id"] = "{$tableName}.id as {$tName}_id";
833 $this->_element["{$tName}_id"] = 1;
834 }
835
836 //special case for phone
837 if ($name == 'phone') {
838 $this->_select['phone_type_id'] = "civicrm_phone.phone_type_id as phone_type_id";
839 $this->_element['phone_type_id'] = 1;
840 }
841
842 // if IM then select provider_id also
843 // to get "IM Service Provider" in a file to be exported, CRM-3140
844 if ($name == 'im') {
845 $this->_select['provider_id'] = "civicrm_im.provider_id as provider_id";
846 $this->_element['provider_id'] = 1;
847 }
848
86ab13b7 849 if ($tName == 'contact' && $fieldName == 'organization_name') {
6a488035 850 // special case, when current employer is set for Individual contact
86ab13b7 851 $this->_select[$name] = "IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name";
852 }
853 elseif ($tName == 'contact' && $fieldName === 'id') {
854 // Handled elsewhere, explicitly ignore. Possibly for all tables...
6a488035 855 }
4e6490d4
E
856 elseif (in_array($tName, array('country', 'county'))) {
857 $this->_pseudoConstantsSelect[$name]['select'] = "{$field['where']} as `$name`";
858 $this->_pseudoConstantsSelect[$name]['element'] = $name;
859 }
860 elseif ($tName == 'state_province') {
861 $this->_pseudoConstantsSelect[$tName]['select'] = "{$field['where']} as `$name`";
862 $this->_pseudoConstantsSelect[$tName]['element'] = $name;
d9ab802d 863 }
e4ce3252 864 elseif (strpos($name, 'contribution_soft_credit') !== FALSE) {
2d94ea54 865 if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_params)) {
866 $this->_select[$name] = "{$field['where']} as `$name`";
867 }
868 }
6a488035 869 else {
86ab13b7 870 // If we have an option group defined then rather than joining the option value table in
871 // (which is an unindexed join) we render the option value on output.
872 // @todo - extend this to other pseudoconstants.
7bd16b05 873 if ($this->pseudoConstantNameIsInReturnProperties($field, $name)) {
86ab13b7 874 $pseudoFieldName = $field['pseudoconstant']['optionGroupName'];
875 $this->_pseudoConstantsSelect[$pseudoFieldName] = array(
876 'pseudoField' => $field['name'],
7bd16b05 877 'idCol' => $name,
86ab13b7 878 'field_name' => $field['name'],
879 'bao' => $field['bao'],
880 'pseudoconstant' => $field['pseudoconstant'],
881 );
882 $this->_tables[$tableName] = 1;
883 $this->_element[$pseudoFieldName] = 1;
884 }
885 $this->_select[$name] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$name`");
6a488035 886 }
d9ab802d
PJ
887 if (!in_array($tName, array('state_province', 'country', 'county'))) {
888 $this->_element[$name] = 1;
889 }
6a488035
TO
890 }
891 }
892 }
893 elseif ($name === 'tags') {
8f165fa5 894 //@todo move this handling outside the big IF & ditch $makeException
6a488035
TO
895 $this->_useGroupBy = TRUE;
896 $this->_select[$name] = "GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags";
897 $this->_element[$name] = 1;
898 $this->_tables['civicrm_tag'] = 1;
899 $this->_tables['civicrm_entity_tag'] = 1;
900 }
901 elseif ($name === 'groups') {
8f165fa5 902 //@todo move this handling outside the big IF & ditch $makeException
6a488035 903 $this->_useGroupBy = TRUE;
3875e6b6 904 // Duplicates will be created here but better to sort them out in php land.
905 $this->_select[$name] = "
906 CONCAT_WS(',',
907 GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')),
908 GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id)
909 )
910 as groups";
6a488035 911 $this->_element[$name] = 1;
3875e6b6 912 $this->_tables['civicrm_group_contact'] = 1;
913 $this->_tables['civicrm_group_contact_cache'] = 1;
914 $this->_pseudoConstantsSelect["{$name}"] = array(
915 'pseudoField' => "groups",
916 'idCol' => "groups",
917 );
6a488035
TO
918 }
919 elseif ($name === 'notes') {
8f165fa5 920 //@todo move this handling outside the big IF & ditch $makeException
e3199114 921 // if note field is subject then return subject else body of the note
922 $noteColumn = 'note';
923 if (isset($noteField) && $noteField == 'note_subject') {
924 $noteColumn = 'subject';
925 }
926
6a488035 927 $this->_useGroupBy = TRUE;
e3199114 928 $this->_select[$name] = "GROUP_CONCAT(DISTINCT(civicrm_note.$noteColumn)) as notes";
6a488035
TO
929 $this->_element[$name] = 1;
930 $this->_tables['civicrm_note'] = 1;
931 }
932 elseif ($name === 'current_employer') {
933 $this->_select[$name] = "IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer";
934 $this->_element[$name] = 1;
935 }
936 }
937
8cc574cf 938 if ($cfID && !empty($field['is_search_range'])) {
6a488035 939 // this is a custom field with range search enabled, so we better check for two/from values
a7488080 940 if (!empty($this->_paramLookup[$name . '_from'])) {
6a488035
TO
941 if (!array_key_exists($cfID, $this->_cfIDs)) {
942 $this->_cfIDs[$cfID] = array();
943 }
944 foreach ($this->_paramLookup[$name . '_from'] as $pID => $p) {
945 // search in the cdID array for the same grouping
946 $fnd = FALSE;
947 foreach ($this->_cfIDs[$cfID] as $cID => $c) {
948 if ($c[3] == $p[3]) {
949 $this->_cfIDs[$cfID][$cID][2]['from'] = $p[2];
950 $fnd = TRUE;
951 }
952 }
953 if (!$fnd) {
954 $p[2] = array('from' => $p[2]);
955 $this->_cfIDs[$cfID][] = $p;
956 }
957 }
958 }
a7488080 959 if (!empty($this->_paramLookup[$name . '_to'])) {
6a488035
TO
960 if (!array_key_exists($cfID, $this->_cfIDs)) {
961 $this->_cfIDs[$cfID] = array();
962 }
963 foreach ($this->_paramLookup[$name . '_to'] as $pID => $p) {
964 // search in the cdID array for the same grouping
965 $fnd = FALSE;
966 foreach ($this->_cfIDs[$cfID] as $cID => $c) {
967 if ($c[4] == $p[4]) {
968 $this->_cfIDs[$cfID][$cID][2]['to'] = $p[2];
969 $fnd = TRUE;
970 }
971 }
972 if (!$fnd) {
973 $p[2] = array('to' => $p[2]);
974 $this->_cfIDs[$cfID][] = $p;
975 }
976 }
977 }
978 }
979 }
980
981 // add location as hierarchical elements
982 $this->addHierarchicalElements();
983
984 // add multiple field like website
985 $this->addMultipleElements();
986
987 //fix for CRM-951
988 CRM_Core_Component::alterQuery($this, 'select');
989
99e9587a 990 CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'select');
f2a9b25c 991
6a488035 992 if (!empty($this->_cfIDs)) {
bb05da0c 993 // @todo This function is the select function but instead of running 'select' it
994 // is running the whole query.
442df34b 995 $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE, $this->_locationSpecificCustomFields);
6a488035
TO
996 $this->_customQuery->query();
997 $this->_select = array_merge($this->_select, $this->_customQuery->_select);
998 $this->_element = array_merge($this->_element, $this->_customQuery->_element);
999 $this->_tables = array_merge($this->_tables, $this->_customQuery->_tables);
1000 $this->_whereTables = array_merge($this->_whereTables, $this->_customQuery->_whereTables);
1001 $this->_options = $this->_customQuery->_options;
1002 }
1003 }
1004
1005 /**
c037736a 1006 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
6a488035 1007 */
00be9182 1008 public function addHierarchicalElements() {
a7488080 1009 if (empty($this->_returnProperties['location'])) {
6a488035
TO
1010 return;
1011 }
1012 if (!is_array($this->_returnProperties['location'])) {
1013 return;
1014 }
1015
f9ba4d01 1016 $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
6a488035
TO
1017 $processed = array();
1018 $index = 0;
1019
1020 $addressCustomFields = CRM_Core_BAO_CustomField::getFieldsForImport('Address');
1021 $addressCustomFieldIds = array();
1022
1023 foreach ($this->_returnProperties['location'] as $name => $elements) {
1024 $lCond = self::getPrimaryCondition($name);
1025
1026 if (!$lCond) {
1027 $locationTypeId = array_search($name, $locationTypes);
1028 if ($locationTypeId === FALSE) {
1029 continue;
1030 }
1031 $lCond = "location_type_id = $locationTypeId";
1032 $this->_useDistinct = TRUE;
1033
1034 //commented for CRM-3256
1035 $this->_useGroupBy = TRUE;
1036 }
1037
1038 $name = str_replace(' ', '_', $name);
1039
1040 $tName = "$name-location_type";
1041 $ltName = "`$name-location_type`";
1042 $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1043 $this->_select["{$tName}"] = "`$tName`.name as `{$tName}`";
1044 $this->_element["{$tName}_id"] = 1;
1045 $this->_element["{$tName}"] = 1;
1046
1047 $locationTypeName = $tName;
1048 $locationTypeJoin = array();
1049
1050 $addAddress = FALSE;
1051 $addWhereCount = 0;
1052 foreach ($elements as $elementFullName => $dontCare) {
1053 $index++;
1054 $elementName = $elementCmpName = $elementFullName;
1055
1056 if (substr($elementCmpName, 0, 5) == 'phone') {
1057 $elementCmpName = 'phone';
1058 }
1059
1060 if (in_array($elementCmpName, array_keys($addressCustomFields))) {
1061 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($elementCmpName)) {
1062 $addressCustomFieldIds[$cfID][$name] = 1;
1063 }
1064 }
1065 //add address table only once
1066 if ((in_array($elementCmpName, self::$_locationSpecificFields) || !empty($addressCustomFieldIds))
1067 && !$addAddress
1068 && !in_array($elementCmpName, array('email', 'phone', 'im', 'openid'))
1069 ) {
1070 $tName = "$name-address";
1071 $aName = "`$name-address`";
1072 $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1073 $this->_element["{$tName}_id"] = 1;
1074 $addressJoin = "\nLEFT JOIN civicrm_address $aName ON ($aName.contact_id = contact_a.id AND $aName.$lCond)";
1075 $this->_tables[$tName] = $addressJoin;
1076 $locationTypeJoin[$tName] = " ( $aName.location_type_id = $ltName.id ) ";
1077 $processed[$aName] = 1;
1078 $addAddress = TRUE;
1079 }
1080
1081 $cond = $elementType = '';
1082 if (strpos($elementName, '-') !== FALSE) {
1083 // this is either phone, email or IM
1084 list($elementName, $elementType) = explode('-', $elementName);
1085
6a488035
TO
1086 if (($elementName != 'phone') && ($elementName != 'im')) {
1087 $cond = self::getPrimaryCondition($elementType);
1088 }
6e7e6dc0
PJ
1089 // CRM-13011 : If location type is primary, do not restrict search to the phone
1090 // type id - we want the primary phone, regardless of what type it is.
1091 // Otherwise, restrict to the specified phone type for the given field.
9457ac35 1092 if ((!$cond) && ($elementName == 'phone')) {
6a488035
TO
1093 $cond = "phone_type_id = '$elementType'";
1094 }
1095 elseif ((!$cond) && ($elementName == 'im')) {
1096 // IM service provider id, CRM-3140
1097 $cond = "provider_id = '$elementType'";
1098 }
1099 $elementType = '-' . $elementType;
1100 }
1101
1102 $field = CRM_Utils_Array::value($elementName, $this->_fields);
1103
1104 // hack for profile, add location id
1105 if (!$field) {
1106 if ($elementType &&
1107 // fix for CRM-882( to handle phone types )
1108 !is_numeric($elementType)
1109 ) {
1110 if (is_numeric($name)) {
1111 $field = CRM_Utils_Array::value($elementName . "-Primary$elementType", $this->_fields);
1112 }
1113 else {
1114 $field = CRM_Utils_Array::value($elementName . "-$locationTypeId$elementType", $this->_fields);
1115 }
1116 }
1117 elseif (is_numeric($name)) {
1118 //this for phone type to work
1119 if (in_array($elementName, array('phone', 'phone_ext'))) {
1120 $field = CRM_Utils_Array::value($elementName . "-Primary" . $elementType, $this->_fields);
1121 }
1122 else {
1123 $field = CRM_Utils_Array::value($elementName . "-Primary", $this->_fields);
1124 }
1125 }
1126 else {
1127 //this is for phone type to work for profile edit
1128 if (in_array($elementName, array('phone', 'phone_ext'))) {
1129 $field = CRM_Utils_Array::value($elementName . "-$locationTypeId$elementType", $this->_fields);
1130 }
1131 else {
1132 $field = CRM_Utils_Array::value($elementName . "-$locationTypeId", $this->_fields);
1133 }
1134 }
1135 }
1136
314dbef8 1137 // Check if there is a value, if so also add to where Clause
6a488035
TO
1138 $addWhere = FALSE;
1139 if ($this->_params) {
1140 $nm = $elementName;
1141 if (isset($locationTypeId)) {
1142 $nm .= "-$locationTypeId";
1143 }
1144 if (!is_numeric($elementType)) {
1145 $nm .= "$elementType";
1146 }
1147
1148 foreach ($this->_params as $id => $values) {
8af73472 1149 if ((is_array($values) && $values[0] == $nm) ||
6a488035
TO
1150 (in_array($elementName, array('phone', 'im'))
1151 && (strpos($values[0], $nm) !== FALSE)
1152 )
1153 ) {
1154 $addWhere = TRUE;
1155 $addWhereCount++;
1156 break;
1157 }
1158 }
1159 }
1160
1161 if ($field && isset($field['where'])) {
1162 list($tableName, $fieldName) = explode('.', $field['where'], 2);
d9ab802d
PJ
1163 $pf = substr($tableName, 8);
1164 $tName = $name . '-' . $pf . $elementType;
6a488035 1165 if (isset($tableName)) {
d9ab802d
PJ
1166 if ($tableName == 'civicrm_state_province' || $tableName == 'civicrm_country' || $tableName == 'civicrm_county') {
1167 $this->_select["{$tName}_id"] = "{$aName}.{$pf}_id as `{$tName}_id`";
1168 }
1169 else {
1170 $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1171 }
1172
6a488035
TO
1173 $this->_element["{$tName}_id"] = 1;
1174 if (substr($tName, -15) == '-state_province') {
1175 // FIXME: hack to fix CRM-1900
aaffa79f 1176 $a = Civi::settings()->get('address_format');
6a488035
TO
1177
1178 if (substr_count($a, 'state_province_name') > 0) {
59f4c9ee
TO
1179 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"] = array(
1180 'pseudoField' => "{$pf}_id",
1181 'idCol' => "{$tName}_id",
1182 'bao' => 'CRM_Core_BAO_Address',
1183 );
d9ab802d 1184 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['select'] = "`$tName`.name as `{$name}-{$elementFullName}`";
6a488035
TO
1185 }
1186 else {
59f4c9ee
TO
1187 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"] = array(
1188 'pseudoField' => 'state_province_abbreviation',
1189 'idCol' => "{$tName}_id",
1190 );
d9ab802d 1191 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['select'] = "`$tName`.abbreviation as `{$name}-{$elementFullName}`";
6a488035
TO
1192 }
1193 }
1194 else {
1195 if (substr($elementFullName, 0, 2) == 'im') {
1196 $provider = "{$name}-{$elementFullName}-provider_id";
1197 $this->_select[$provider] = "`$tName`.provider_id as `{$name}-{$elementFullName}-provider_id`";
1198 $this->_element[$provider] = 1;
1199 }
d9ab802d 1200 if ($pf == 'country' || $pf == 'county') {
5c7dad85
TO
1201 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"] = array(
1202 'pseudoField' => "{$pf}_id",
1203 'idCol' => "{$tName}_id",
21dfd5f5 1204 'bao' => 'CRM_Core_BAO_Address',
5c7dad85 1205 );
d9ab802d
PJ
1206 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['select'] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1207 }
1208 else {
1209 $this->_select["{$name}-{$elementFullName}"] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1210 }
1211 }
6a488035 1212
d9ab802d
PJ
1213 if (in_array($pf, array('state_province', 'country', 'county'))) {
1214 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['element'] = "{$name}-{$elementFullName}";
1215 }
1216 else {
1217 $this->_element["{$name}-{$elementFullName}"] = 1;
6a488035
TO
1218 }
1219
a7488080 1220 if (empty($processed["`$tName`"])) {
6a488035
TO
1221 $processed["`$tName`"] = 1;
1222 $newName = $tableName . '_' . $index;
1223 switch ($tableName) {
1224 case 'civicrm_phone':
1225 case 'civicrm_email':
1226 case 'civicrm_im':
1227 case 'civicrm_openid':
1228
9457ac35 1229 $this->_tables[$tName] = "\nLEFT JOIN $tableName `$tName` ON contact_a.id = `$tName`.contact_id";
1230 if ($tableName != 'civicrm_phone') {
1231 $this->_tables[$tName] .= " AND `$tName`.$lCond";
1232 }
1233 elseif (is_numeric($name)) {
1234 $this->_select[$tName] = "IF (`$tName`.is_primary = $name, `$tName`.phone, NULL) as `$tName`";
1235 }
1236
6a488035
TO
1237 // this special case to add phone type
1238 if ($cond) {
1239 $phoneTypeCondition = " AND `$tName`.$cond ";
1240 //gross hack to pickup corrupted data also, CRM-7603
1241 if (strpos($cond, 'phone_type_id') !== FALSE) {
1242 $phoneTypeCondition = " AND ( `$tName`.$cond OR `$tName`.phone_type_id IS NULL ) ";
8b3d5966
J
1243 if (!empty($lCond)) {
1244 $phoneTypeCondition .= " AND ( `$tName`.$lCond ) ";
1245 }
6a488035
TO
1246 }
1247 $this->_tables[$tName] .= $phoneTypeCondition;
1248 }
1249
1250 //build locationType join
1251 $locationTypeJoin[$tName] = " ( `$tName`.location_type_id = $ltName.id )";
1252
1253 if ($addWhere) {
1254 $this->_whereTables[$tName] = $this->_tables[$tName];
1255 }
1256 break;
1257
1258 case 'civicrm_state_province':
d9ab802d 1259 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['table'] = $tName;
59f4c9ee
TO
1260 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['join']
1261 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.state_province_id";
6a488035
TO
1262 if ($addWhere) {
1263 $this->_whereTables["{$name}-address"] = $addressJoin;
6a488035
TO
1264 }
1265 break;
1266
1267 case 'civicrm_country':
d9ab802d 1268 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['table'] = $newName;
59f4c9ee
TO
1269 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['join']
1270 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.country_id";
6a488035
TO
1271 if ($addWhere) {
1272 $this->_whereTables["{$name}-address"] = $addressJoin;
6a488035
TO
1273 }
1274 break;
1275
1276 case 'civicrm_county':
d9ab802d 1277 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['table'] = $newName;
59f4c9ee
TO
1278 $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['join']
1279 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.county_id";
6a488035
TO
1280 if ($addWhere) {
1281 $this->_whereTables["{$name}-address"] = $addressJoin;
6a488035
TO
1282 }
1283 break;
1284
1285 default:
1286 if ($addWhere) {
1287 $this->_whereTables["{$name}-address"] = $addressJoin;
1288 }
1289 break;
1290 }
1291 }
1292 }
1293 }
1294 }
1295
1296 // add location type join
1297 $ltypeJoin = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locationTypeJoin) . " )";
1298 $this->_tables[$locationTypeName] = $ltypeJoin;
1299
1300 // table should be present in $this->_whereTables,
1301 // to add its condition in location type join, CRM-3939.
1302 if ($addWhereCount) {
1303 $locClause = array();
1304 foreach ($this->_whereTables as $tableName => $clause) {
a7488080 1305 if (!empty($locationTypeJoin[$tableName])) {
6a488035
TO
1306 $locClause[] = $locationTypeJoin[$tableName];
1307 }
1308 }
1309
1310 if (!empty($locClause)) {
1311 $this->_whereTables[$locationTypeName] = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locClause) . " )";
1312 }
1313 }
1314 }
1315
1316 if (!empty($addressCustomFieldIds)) {
442df34b 1317 $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds);
6a488035
TO
1318 foreach ($addressCustomFieldIds as $cfID => $locTypeName) {
1319 foreach ($locTypeName as $name => $dnc) {
442df34b 1320 $this->_locationSpecificCustomFields[$cfID] = array($name, array_search($name, $locationTypes));
6a488035
TO
1321 $fieldName = "$name-custom_{$cfID}";
1322 $tName = "$name-address-custom-{$cfID}";
1323 $aName = "`$name-address-custom-{$cfID}`";
1324 $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1325 $this->_element["{$tName}_id"] = 1;
1326 $this->_select[$fieldName] = "`$tName`.{$customQuery->_fields[$cfID]['column_name']} as `{$fieldName}`";
1327 $this->_element[$fieldName] = 1;
1328 $this->_tables[$tName] = "\nLEFT JOIN {$customQuery->_fields[$cfID]['table_name']} $aName ON ($aName.entity_id = `$name-address`.id)";
1329 }
1330 }
1331 }
1332 }
1333
1334 /**
c037736a 1335 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
6a488035 1336 */
00be9182 1337 public function addMultipleElements() {
a7488080 1338 if (empty($this->_returnProperties['website'])) {
6a488035
TO
1339 return;
1340 }
1341 if (!is_array($this->_returnProperties['website'])) {
1342 return;
1343 }
1344
1345 foreach ($this->_returnProperties['website'] as $key => $elements) {
1346 foreach ($elements as $elementFullName => $dontCare) {
1347 $tName = "website-{$key}-{$elementFullName}";
1348 $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1349 $this->_select["{$tName}"] = "`$tName`.url as `{$tName}`";
1350 $this->_element["{$tName}_id"] = 1;
1351 $this->_element["{$tName}"] = 1;
1352
1353 $type = "website-{$key}-website_type_id";
1354 $this->_select[$type] = "`$tName`.website_type_id as `{$type}`";
1355 $this->_element[$type] = 1;
887e764d 1356 $this->_tables[$tName] = "\nLEFT JOIN civicrm_website `$tName` ON (`$tName`.contact_id = contact_a.id AND `$tName`.website_type_id = $key )";
6a488035
TO
1357 }
1358 }
1359 }
1360
1361 /**
fe482240 1362 * Generate the query based on what type of query we need.
6a488035 1363 *
77c5b619
TO
1364 * @param bool $count
1365 * @param bool $sortByChar
1366 * @param bool $groupContacts
1367 * @param bool $onlyDeleted
6a488035 1368 *
a6c01b45
CW
1369 * @return array
1370 * sql query parts as an array
6a488035 1371 */
00be9182 1372 public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE, $onlyDeleted = FALSE) {
2c3ad4b7 1373 // build permission clause
1374 $this->generatePermissionClause($onlyDeleted, $count);
1375
6a488035 1376 if ($count) {
3dbf477c
DS
1377 if (isset($this->_rowCountClause)) {
1378 $select = "SELECT {$this->_rowCountClause}";
5c7dad85 1379 }
4c9b6178 1380 elseif (isset($this->_distinctComponentClause)) {
6a488035
TO
1381 // we add distinct to get the right count for components
1382 // for the more complex result set, we use GROUP BY the same id
1383 // CRM-9630
9ae25b56 1384 $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount";
6a488035
TO
1385 }
1386 else {
1387 $select = 'SELECT count(DISTINCT contact_a.id) as rowCount';
1388 }
1389 $from = $this->_simpleFromClause;
1390 if ($this->_useDistinct) {
1391 $this->_useGroupBy = TRUE;
1392 }
1393 }
1394 elseif ($sortByChar) {
1395 $select = 'SELECT DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) as sort_name';
1396 $from = $this->_simpleFromClause;
1397 }
1398 elseif ($groupContacts) {
1399 $select = 'SELECT contact_a.id as id';
1400 if ($this->_useDistinct) {
1401 $this->_useGroupBy = TRUE;
1402 }
1403 $from = $this->_simpleFromClause;
1404 }
1405 else {
a7488080 1406 if (!empty($this->_paramLookup['group'])) {
ca91bd7f 1407
1408 list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup['group'][0];
1409
1410 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
1411 $this->_paramLookup['group'][0][1] = key($value);
1412 }
1413
3875e6b6 1414 // Presumably the lines below come into manage groups screen.
6a488035
TO
1415 // make sure there is only one element
1416 // this is used when we are running under smog and need to know
1417 // how the contact was added (CRM-1203)
db135a44 1418 $groups = (array) CRM_Utils_Array::value($this->_paramLookup['group'][0][1], $this->_paramLookup['group'][0][2], $this->_paramLookup['group'][0][2]);
6a488035 1419 if ((count($this->_paramLookup['group']) == 1) &&
7cc09daf 1420 (count($groups) == 1)
6a488035 1421 ) {
6a488035
TO
1422 $groupId = $groups[0];
1423
1424 //check if group is saved search
1425 $group = new CRM_Contact_BAO_Group();
1426 $group->id = $groupId;
1427 $group->find(TRUE);
1428
1429 if (!isset($group->saved_search_id)) {
f30aa754 1430 $tbName = "civicrm_group_contact";
82ae55f4 1431 // CRM-17254 don't retrieve extra fields if contact_id is specifically requested
1432 // as this will add load to an intentionally light query.
1433 // ideally this code would be removed as it appears to be to support CRM-1203
1434 // and passing in the required returnProperties from the url would
1435 // make more sense that globally applying the requirements of one form.
1436 if (($this->_returnProperties != array('contact_id'))) {
1437 $this->_select['group_contact_id'] = "$tbName.id as group_contact_id";
1438 $this->_element['group_contact_id'] = 1;
1439 $this->_select['status'] = "$tbName.status as status";
1440 $this->_element['status'] = 1;
1441 }
6a488035
TO
1442 }
1443 }
1444 $this->_useGroupBy = TRUE;
1445 }
1446 if ($this->_useDistinct && !isset($this->_distinctComponentClause)) {
1447 if (!($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY)) {
1448 // CRM-5954
1449 $this->_select['contact_id'] = 'contact_a.id as contact_id';
1450 $this->_useDistinct = FALSE;
1451 $this->_useGroupBy = TRUE;
1452 }
1453 }
1454
f946d152 1455 $select = $this->getSelect();
6a488035
TO
1456 $from = $this->_fromClause;
1457 }
1458
1459 $where = '';
1460 if (!empty($this->_whereClause)) {
1461 $where = "WHERE {$this->_whereClause}";
1462 }
1463
0619ff2f 1464 if (!empty($this->_permissionWhereClause) && empty($this->_displayRelationshipType)) {
2c3ad4b7 1465 if (empty($where)) {
1466 $where = "WHERE $this->_permissionWhereClause";
1467 }
1468 else {
1469 $where = "$where AND $this->_permissionWhereClause";
1470 }
1471 }
1472
6a488035
TO
1473 $having = '';
1474 if (!empty($this->_having)) {
2ef90ca7
EM
1475 foreach ($this->_having as $havingSets) {
1476 foreach ($havingSets as $havingSet) {
1477 $havingValue[] = $havingSet;
6a488035
TO
1478 }
1479 }
2ef90ca7 1480 $having = ' HAVING ' . implode(' AND ', $havingValue);
6a488035
TO
1481 }
1482
1483 // if we are doing a transform, do it here
1484 // use the $from, $where and $having to get the contact ID
1485 if ($this->_displayRelationshipType) {
1486 $this->filterRelatedContacts($from, $where, $having);
1487 }
1488
1489 return array($select, $from, $where, $having);
1490 }
1491
86538308 1492 /**
c037736a 1493 * Get where values from the parameters.
1494 *
100fef9d 1495 * @param string $name
c037736a 1496 * @param mixed $grouping
86538308 1497 *
c037736a 1498 * @return mixed
86538308 1499 */
c037736a 1500 public function getWhereValues($name, $grouping) {
6a488035 1501 $result = NULL;
f4bff68a 1502 foreach ($this->_params as $values) {
6a488035
TO
1503 if ($values[0] == $name && $values[3] == $grouping) {
1504 return $values;
1505 }
1506 }
1507
1508 return $result;
1509 }
1510
86538308 1511 /**
c037736a 1512 * Fix date values.
1513 *
1514 * @param bool $relative
1515 * @param string $from
1516 * @param string $to
86538308 1517 */
00be9182 1518 public static function fixDateValues($relative, &$from, &$to) {
6a488035
TO
1519 if ($relative) {
1520 list($from, $to) = CRM_Utils_Date::getFromTo($relative, $from, $to);
1521 }
1522 }
1523
86538308 1524 /**
bb05da0c 1525 * Convert values from form-appropriate to query-object appropriate.
1526 *
1527 * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax.
1528 * So, ideally we would convert all fields to look like
1529 * array(
1530 * 0 => $fieldName
1531 * // Set the operator for legacy reasons, but it is ignored
1532 * 1 => '='
1533 * // array in sql filter syntax
1534 * 2 => array('BETWEEN' => array(1,60),
1535 * 3 => null
1536 * 4 => null
1537 * );
1538 *
1539 * There are some examples of the syntax in
1540 * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship
1541 *
1542 * More notes at CRM_Core_DAO::createSQLFilter
1543 *
1544 * and a list of supported operators in CRM_Core_DAO
c037736a 1545 *
1546 * @param array $formValues
86538308
EM
1547 * @param int $wildcard
1548 * @param bool $useEquals
1549 *
0e2e76cf
EM
1550 * @param string $apiEntity
1551 *
df60621b 1552 * @param array $entityReferenceFields
1553 * Field names of any entity reference fields (which will need reformatting to IN syntax).
1554 *
86538308
EM
1555 * @return array
1556 */
df60621b 1557 public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL,
1558 $entityReferenceFields = array()) {
6a488035
TO
1559 $params = array();
1560 if (empty($formValues)) {
1561 return $params;
1562 }
1563
20306bb8 1564 self::filterCountryFromValuesIfStateExists($formValues);
1565
be588106
AS
1566 // Handle relative dates first
1567 foreach (array_keys($formValues) as $id) {
1568 if (preg_match('/_date_relative$/', $id) ||
1569 $id == 'event_relative' ||
1570 $id == 'case_from_relative' ||
1571 $id == 'case_to_relative' ||
1572 $id == 'participant_relative'
1573 ) {
1574 if ($id == 'event_relative') {
1575 $fromRange = 'event_start_date_low';
1576 $toRange = 'event_end_date_high';
1577 }
1578 elseif ($id == 'participant_relative') {
1579 $fromRange = 'participant_register_date_low';
1580 $toRange = 'participant_register_date_high';
1581 }
1582 elseif ($id == 'case_from_relative') {
1583 $fromRange = 'case_from_start_date_low';
1584 $toRange = 'case_from_start_date_high';
1585 }
1586 elseif ($id == 'case_to_relative') {
1587 $fromRange = 'case_to_end_date_low';
1588 $toRange = 'case_to_end_date_high';
1589 }
1590 else {
1591 $dateComponent = explode('_date_relative', $id);
1592 $fromRange = "{$dateComponent[0]}_date_low";
1593 $toRange = "{$dateComponent[0]}_date_high";
1594 }
1595
1596 if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
1597 CRM_Contact_BAO_Query::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
1598 }
1599 }
1600 }
e34642c9 1601
5840d253 1602 foreach ($formValues as $id => $values) {
df60621b 1603 if (self::isAlreadyProcessedForQueryFormat($values)) {
1604 $params[] = $values;
1605 continue;
1606 }
06d67d53 1607
1608 self::legacyConvertFormValues($id, $values);
1609
46b3417a
CW
1610 // The form uses 1 field to represent two db fields
1611 if ($id == 'contact_type' && $values && (!is_array($values) || !array_intersect(array_keys($values), CRM_Core_DAO::acceptedSQLOperators()))) {
1612 $contactType = array();
1613 $subType = array();
1614 foreach ((array) $values as $key => $type) {
f12a86ad 1615 $types = explode('__', is_numeric($type) ? $key : $type, 2);
46b3417a
CW
1616 $contactType[$types[0]] = $types[0];
1617 // Add sub-type if specified
1618 if (!empty($types[1])) {
1619 $subType[$types[1]] = $types[1];
1620 }
1621 }
1622 $params[] = array('contact_type', 'IN', $contactType, 0, 0);
1623 if ($subType) {
1624 $params[] = array('contact_sub_type', 'IN', $subType, 0, 0);
1625 }
1626 }
1627 elseif ($id == 'privacy') {
6a488035 1628 if (is_array($formValues['privacy'])) {
0d8afee2 1629 $op = !empty($formValues['privacy']['do_not_toggle']) ? '=' : '!=';
6a488035
TO
1630 foreach ($formValues['privacy'] as $key => $value) {
1631 if ($value) {
1632 $params[] = array($key, $op, $value, 0, 0);
1633 }
1634 }
1635 }
1636 }
1637 elseif ($id == 'email_on_hold') {
38056b36
AS
1638 if ($onHoldValue = CRM_Utils_Array::value('email_on_hold', $formValues)) {
1639 $onHoldValue = (array) $onHoldValue;
1640 $params[] = array('on_hold', 'IN', $onHoldValue, 0, 0);
6a488035
TO
1641 }
1642 }
bb05da0c 1643 elseif (substr($id, 0, 7) == 'custom_'
4c2fe77b 1644 && (
1645 substr($id, -9, 9) == '_relative'
1646 || substr($id, -5, 5) == '_from'
1647 || substr($id, -3, 3) == '_to'
1648 )
bb05da0c 1649 ) {
4c2fe77b 1650 self::convertCustomRelativeFields($formValues, $params, $values, $id);
bb05da0c 1651 }
6a488035 1652 elseif (preg_match('/_date_relative$/', $id) ||
5c7dad85
TO
1653 $id == 'event_relative' ||
1654 $id == 'case_from_relative' ||
e520211a
SL
1655 $id == 'case_to_relative' ||
1656 $id == 'participant_relative'
5c7dad85 1657 ) {
be588106
AS
1658 // Already handled in previous loop
1659 continue;
6a488035 1660 }
df60621b 1661 elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
1662 FALSE)) {
1663 $params[] = array($id, 'IN', explode(',', $values), 0, 0);
1664 }
6a488035 1665 else {
3c151c70 1666 $values = CRM_Contact_BAO_Query::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity);
6a488035
TO
1667
1668 if (!$values) {
1669 continue;
1670 }
1671 $params[] = $values;
1672 }
1673 }
1674 return $params;
1675 }
1676
e34642c9 1677 /**
1678 * Function to support legacy format for groups and tags.
1679 *
1680 * @param string $id
1681 * @param array|int $values
1682 *
1683 */
5450bf88 1684 public static function legacyConvertFormValues($id, &$values) {
06d67d53 1685 $legacyElements = array(
1686 'group',
1687 'tag',
1688 'contact_tags',
1689 'contact_type',
1690 'membership_type_id',
1691 'membership_status_id',
1692 );
1693 if (in_array($id, $legacyElements) && is_array($values)) {
5450bf88 1694 // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
1695 // as per the recent Search fixes $values need to be in standard array(id1, id2) format
e5ad0335 1696 $values = CRM_Utils_Array::convertCheckboxFormatToArray($values);
5450bf88 1697 }
e34642c9 1698 }
1699
86538308 1700 /**
c037736a 1701 * Fix values from query from/to something no-one cared enough to document.
1702 *
100fef9d 1703 * @param int $id
c037736a 1704 * @param array $values
86538308
EM
1705 * @param int $wildcard
1706 * @param bool $useEquals
1707 *
0e2e76cf
EM
1708 * @param string $apiEntity
1709 *
86538308
EM
1710 * @return array|null
1711 */
c037736a 1712 public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) {
6a488035
TO
1713 // skip a few search variables
1714 static $skipWhere = NULL;
6a488035
TO
1715 static $likeNames = NULL;
1716 $result = NULL;
57f8e7f0 1717
244bbdd8
CW
1718 // Change camelCase EntityName to lowercase with underscores
1719 $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity);
6a488035 1720
57f8e7f0 1721 // check if $value is in OK (Operator as Key) format as used by Get API
6a488035
TO
1722 if (CRM_Utils_System::isNull($values)) {
1723 return $result;
1724 }
1725
1726 if (!$skipWhere) {
1727 $skipWhere = array(
5c7dad85
TO
1728 'task',
1729 'radio_ts',
1730 'uf_group_id',
1731 'component_mode',
1732 'qfKey',
1733 'operator',
6a488035
TO
1734 'display_relationship_type',
1735 );
1736 }
1737
1738 if (in_array($id, $skipWhere) ||
1739 substr($id, 0, 4) == '_qf_' ||
1740 substr($id, 0, 7) == 'hidden_'
1741 ) {
1742 return $result;
1743 }
1744
afa0b07c 1745 if ($apiEntity &&
1746 (substr($id, 0, strlen($apiEntity)) != $apiEntity) &&
1a3e22cc
MM
1747 (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_') &&
1748 (substr($id, 0, 7) != 'custom_')
afa0b07c 1749 ) {
3c151c70 1750 $id = $apiEntity . '_' . $id;
1751 }
1752
6a488035
TO
1753 if (!$likeNames) {
1754 $likeNames = array('sort_name', 'email', 'note', 'display_name');
1755 }
1756
1757 // email comes in via advanced search
1758 // so use wildcard always
1759 if ($id == 'email') {
1760 $wildcard = 1;
1761 }
1762
5c7dad85 1763 if (!$useEquals && in_array($id, $likeNames)) {
6a488035
TO
1764 $result = array($id, 'LIKE', $values, 0, 1);
1765 }
1766 elseif (is_string($values) && strpos($values, '%') !== FALSE) {
1767 $result = array($id, 'LIKE', $values, 0, 0);
1768 }
a0cbe4fa 1769 elseif ($id == 'contact_type' ||
1770 (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO::acceptedSQLOperators(), TRUE))
1771 ) {
c33e01a5 1772 $result = array($id, 'IN', $values, 0, $wildcard);
1773 }
6a488035
TO
1774 else {
1775 $result = array($id, '=', $values, 0, $wildcard);
1776 }
1777
1778 return $result;
1779 }
1780
86538308 1781 /**
c037736a 1782 * Get the where clause for a single field.
1783 *
1784 * @param array $values
57f8e7f0 1785 * @param string $apiEntity
86538308 1786 */
11f8e194 1787 public function whereClauseSingle(&$values, $apiEntity = NULL) {
6a488035
TO
1788 // do not process custom fields or prefixed contact ids or component params
1789 if (CRM_Core_BAO_CustomField::getKeyID($values[0]) ||
1790 (substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) ||
1791 (substr($values[0], 0, 13) == 'contribution_') ||
1792 (substr($values[0], 0, 6) == 'event_') ||
1793 (substr($values[0], 0, 12) == 'participant_') ||
1794 (substr($values[0], 0, 7) == 'member_') ||
1795 (substr($values[0], 0, 6) == 'grant_') ||
1796 (substr($values[0], 0, 7) == 'pledge_') ||
289c8051 1797 (substr($values[0], 0, 5) == 'case_') ||
3c53f77e 1798 (substr($values[0], 0, 10) == 'financial_') ||
afa0b07c 1799 (substr($values[0], 0, 8) == 'payment_') ||
3c53f77e 1800 (substr($values[0], 0, 11) == 'membership_')
6a488035
TO
1801 ) {
1802 return;
1803 }
1804
efa3a566
DS
1805 // skip for hook injected fields / params
1806 $extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields();
1807 if (array_key_exists($values[0], $extFields)) {
1808 return;
1809 }
1810
6a488035
TO
1811 switch ($values[0]) {
1812 case 'deleted_contacts':
1813 $this->deletedContacts($values);
1814 return;
1815
6a488035
TO
1816 case 'contact_sub_type':
1817 $this->contactSubType($values);
1818 return;
1819
1820 case 'group':
6a488035 1821 case 'group_type':
6a488035
TO
1822 $this->group($values);
1823 return;
6a488035 1824
c905b59f 1825 // case tag comes from find contacts
6a488035
TO
1826 case 'tag_search':
1827 $this->tagSearch($values);
1828 return;
1829
1830 case 'tag':
1831 case 'contact_tags':
1832 $this->tag($values);
1833 return;
1834
1835 case 'note':
1836 case 'note_body':
1837 case 'note_subject':
1838 $this->notes($values);
1839 return;
1840
1841 case 'uf_user':
1842 $this->ufUser($values);
1843 return;
1844
1845 case 'sort_name':
1846 case 'display_name':
1847 $this->sortName($values);
6a488035
TO
1848 return;
1849
02adf2f9
BS
1850 case 'addressee':
1851 case 'postal_greeting':
1852 case 'email_greeting':
1853 $this->greetings($values);
1854 return;
1855
6a488035 1856 case 'email':
57f8e7f0 1857 case 'email_id':
1858 $this->email($values, $apiEntity);
6a488035
TO
1859 return;
1860
1861 case 'phone_numeric':
1862 $this->phone_numeric($values);
1863 return;
1864
1865 case 'phone_phone_type_id':
1866 case 'phone_location_type_id':
1867 $this->phone_option_group($values);
1868 return;
1869
1870 case 'street_address':
1871 $this->street_address($values);
1872 return;
1873
1874 case 'street_number':
1875 $this->street_number($values);
1876 return;
1877
1878 case 'sortByCharacter':
1879 $this->sortByCharacter($values);
1880 return;
1881
1882 case 'location_type':
1883 $this->locationType($values);
1884 return;
1885
1886 case 'county':
1887 $this->county($values);
1888 return;
1889
1890 case 'state_province':
3493947a 1891 case 'state_province_id':
1892 case 'state_province_name':
6a488035
TO
1893 $this->stateProvince($values);
1894 return;
1895
1896 case 'country':
3493947a 1897 case 'country_id':
6a488035
TO
1898 $this->country($values, FALSE);
1899 return;
1900
1901 case 'postal_code':
1902 case 'postal_code_low':
1903 case 'postal_code_high':
1904 $this->postalCode($values);
1905 return;
1906
1907 case 'activity_date':
1908 case 'activity_date_low':
1909 case 'activity_date_high':
1910 case 'activity_role':
9ab34172 1911 case 'activity_status_id':
6a488035 1912 case 'activity_status':
da236f9a 1913 case 'activity_priority':
1914 case 'activity_priority_id':
4c24c842
PN
1915 case 'followup_parent_id':
1916 case 'parent_id':
9ab34172 1917 case 'source_contact_id':
92e9c781 1918 case 'activity_text':
c9c5c58d 1919 case 'activity_option':
6a488035
TO
1920 case 'test_activities':
1921 case 'activity_type_id':
3e4a4597 1922 case 'activity_type':
6a488035
TO
1923 case 'activity_survey_id':
1924 case 'activity_tags':
1925 case 'activity_taglist':
1926 case 'activity_test':
6a488035
TO
1927 case 'activity_campaign_id':
1928 case 'activity_engagement_level':
1929 case 'activity_id':
4041c9b6 1930 case 'activity_result':
6a488035
TO
1931 case 'source_contact':
1932 CRM_Activity_BAO_Query::whereClauseSingle($values, $this);
1933 return;
1934
c4a7c967 1935 case 'age_low':
1936 case 'age_high':
6a488035
TO
1937 case 'birth_date_low':
1938 case 'birth_date_high':
1939 case 'deceased_date_low':
1940 case 'deceased_date_high':
1941 $this->demographics($values);
1942 return;
1943
c4a7c967 1944 case 'age_asof_date':
1945 // handled by demographics
1946 return;
1947
6a488035
TO
1948 case 'log_date_low':
1949 case 'log_date_high':
1950 $this->modifiedDates($values);
1951 return;
1952
1953 case 'changed_by':
1954 $this->changeLog($values);
1955 return;
1956
1957 case 'do_not_phone':
1958 case 'do_not_email':
1959 case 'do_not_mail':
1960 case 'do_not_sms':
1961 case 'do_not_trade':
1962 case 'is_opt_out':
1963 $this->privacy($values);
1964 return;
1965
1966 case 'privacy_options':
1967 $this->privacyOptions($values);
1968 return;
1969
1970 case 'privacy_operator':
1971 case 'privacy_toggle':
1972 // these are handled by privacy options
1973 return;
1974
1975 case 'preferred_communication_method':
1976 $this->preferredCommunication($values);
1977 return;
1978
1979 case 'relation_type_id':
ad682134 1980 case 'relation_start_date_high':
1981 case 'relation_start_date_low':
1982 case 'relation_end_date_high':
1983 case 'relation_end_date_low':
eea5db81 1984 case 'relation_active_period_date_high':
1985 case 'relation_active_period_date_low':
6a488035
TO
1986 case 'relation_target_name':
1987 case 'relation_status':
1988 case 'relation_date_low':
1989 case 'relation_date_high':
7c96365b 1990 $this->relationship($values);
1991 $this->_relationshipValuesAdded = TRUE;
6a488035
TO
1992 return;
1993
1994 case 'task_status_id':
1995 $this->task($values);
1996 return;
1997
1998 case 'task_id':
1999 // since this case is handled with the above
2000 return;
2001
2002 case 'prox_distance':
2003 CRM_Contact_BAO_ProximityQuery::process($this, $values);
2004 return;
2005
2006 case 'prox_street_address':
2007 case 'prox_city':
2008 case 'prox_postal_code':
2009 case 'prox_state_province_id':
2010 case 'prox_country_id':
4b2d36d7 2011 case 'prox_geo_code_1':
2012 case 'prox_geo_code_2':
6a488035
TO
2013 // handled by the proximity_distance clause
2014 return;
2015
2016 default:
2017 $this->restWhere($values);
2018 return;
2019 }
2020 }
2021
2022 /**
c037736a 2023 * Given a list of conditions in params generate the required where clause.
6a488035 2024 *
57f8e7f0 2025 * @param string $apiEntity
2026 *
fffe9ee1 2027 * @return string
6a488035 2028 */
11f8e194 2029 public function whereClause($apiEntity = NULL) {
6a488035
TO
2030 $this->_where[0] = array();
2031 $this->_qill[0] = array();
2032
6a488035
TO
2033 $this->includeContactIds();
2034 if (!empty($this->_params)) {
6a488035 2035 foreach (array_keys($this->_params) as $id) {
a7488080 2036 if (empty($this->_params[$id][0])) {
6a488035
TO
2037 continue;
2038 }
2039 // check for both id and contact_id
2040 if ($this->_params[$id][0] == 'id' || $this->_params[$id][0] == 'contact_id') {
f819588f 2041 $this->_where[0][] = self::buildClause("contact_a.id", $this->_params[$id][1], $this->_params[$id][2]);
9ea799a3
PN
2042 $field = CRM_Utils_Array::value('id', $this->_fields);
2043 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(
2044 'CRM_Contact_BAO_Contact',
2045 "contact_a.id",
2046 $this->_params[$id][2],
2047 $this->_params[$id][1]
2048 );
2049 $this->_qill[0][] = ts("%1 %2 %3", [
2050 1 => $field['title'],
2051 2 => $qillop,
2052 3 => $qillVal
2053 ]);
6a488035
TO
2054 }
2055 else {
57f8e7f0 2056 $this->whereClauseSingle($this->_params[$id], $apiEntity);
6a488035
TO
2057 }
2058 }
2059
2060 CRM_Core_Component::alterQuery($this, 'where');
f2a9b25c 2061
99e9587a 2062 CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'where');
6a488035
TO
2063 }
2064
2065 if ($this->_customQuery) {
c037736a 2066 // Added following if condition to avoid the wrong value display for 'my account' / any UF info.
6a488035
TO
2067 // Hope it wont affect the other part of civicrm.. if it does please remove it.
2068 if (!empty($this->_customQuery->_where)) {
2069 $this->_where = CRM_Utils_Array::crmArrayMerge($this->_where, $this->_customQuery->_where);
2070 }
2071
2072 $this->_qill = CRM_Utils_Array::crmArrayMerge($this->_qill, $this->_customQuery->_qill);
2073 }
2074
2075 $clauses = array();
2076 $andClauses = array();
2077
2078 $validClauses = 0;
2079 if (!empty($this->_where)) {
2080 foreach ($this->_where as $grouping => $values) {
2081 if ($grouping > 0 && !empty($values)) {
2082 $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) ';
2083 $validClauses++;
2084 }
2085 }
2086
2087 if (!empty($this->_where[0])) {
2088 $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where[0]) . ' ) ';
2089 }
2090 if (!empty($clauses)) {
2091 $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
2092 }
2093
2094 if ($validClauses > 1) {
2095 $this->_useDistinct = TRUE;
2096 }
2097 }
2098
2099 return implode(' AND ', $andClauses);
2100 }
2101
86538308 2102 /**
c037736a 2103 * Generate where clause for any parameters not already handled.
2104 *
2105 * @param array $values
86538308
EM
2106 *
2107 * @throws Exception
2108 */
00be9182 2109 public function restWhere(&$values) {
6a488035
TO
2110 $name = CRM_Utils_Array::value(0, $values);
2111 $op = CRM_Utils_Array::value(1, $values);
2112 $value = CRM_Utils_Array::value(2, $values);
2113 $grouping = CRM_Utils_Array::value(3, $values);
2114 $wildcard = CRM_Utils_Array::value(4, $values);
2115
8cc574cf 2116 if (isset($grouping) && empty($this->_where[$grouping])) {
6a488035
TO
2117 $this->_where[$grouping] = array();
2118 }
2119
2120 $multipleFields = array('url');
2121
3493947a 2122 //check if the location type exists for fields
6a488035
TO
2123 $lType = '';
2124 $locType = explode('-', $name);
2125
2126 if (!in_array($locType[0], $multipleFields)) {
2127 //add phone type if exists
2128 if (isset($locType[2]) && $locType[2]) {
2129 $locType[2] = CRM_Core_DAO::escapeString($locType[2]);
2130 }
2131 }
2132
2133 $field = CRM_Utils_Array::value($name, $this->_fields);
2134
2135 if (!$field) {
2136 $field = CRM_Utils_Array::value($locType[0], $this->_fields);
2137
2138 if (!$field) {
2139 return;
2140 }
2141 }
2142
2143 $setTables = TRUE;
2144
db1a73f5 2145 $locationType = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
fdfca3e9
VG
2146 if (isset($locType[1]) && is_numeric($locType[1])) {
2147 $lType = $locationType[$locType[1]];
2148 }
2149 if ($lType) {
2150 $field['title'] .= " ($lType)";
2151 }
6a488035
TO
2152
2153 if (substr($name, 0, 14) === 'state_province') {
2154 if (isset($locType[1]) && is_numeric($locType[1])) {
2e155dbd 2155 $setTables = FALSE;
fdfca3e9 2156 $aName = "{$lType}-address";
d9ab802d 2157 $where = "`$aName`.state_province_id";
6a488035
TO
2158 }
2159 else {
d9ab802d 2160 $where = "civicrm_address.state_province_id";
6a488035
TO
2161 }
2162
8ab09481 2163 $this->_where[$grouping][] = self::buildClause($where, $op, $value);
8f0fd14c 2164 $this->_tables[$aName] = $this->_whereTables[$aName] = 1;
7cc09daf 2165 list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
fdfca3e9 2166 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
6a488035 2167 }
1c1a84a2 2168 elseif (!empty($field['pseudoconstant'])) {
1071730c
DL
2169 $this->optionValueQuery(
2170 $name, $op, $value, $grouping,
9b183452 2171 'CRM_Contact_DAO_Contact',
1071730c 2172 $field,
a30e57e0
DL
2173 $field['title'],
2174 'String',
2175 TRUE
1071730c 2176 );
9df8a11d 2177 if ($name == 'gender_id') {
1071730c
DL
2178 self::$_openedPanes[ts('Demographics')] = TRUE;
2179 }
2180 }
9b183452 2181 elseif (substr($name, 0, 7) === 'country' || substr($name, 0, 6) === 'county') {
2182 $name = (substr($name, 0, 7) === 'country') ? "country_id" : "county_id";
6a488035 2183 if (isset($locType[1]) && is_numeric($locType[1])) {
2e155dbd 2184 $setTables = FALSE;
fdfca3e9 2185 $aName = "{$lType}-address";
9b183452 2186 $where = "`$aName`.$name";
6a488035
TO
2187 }
2188 else {
9b183452 2189 $where = "civicrm_address.$name";
6a488035 2190 }
d9ab802d 2191
9b183452 2192 $this->_where[$grouping][] = self::buildClause($where, $op, $value, 'Positive');
8f0fd14c 2193 $this->_tables[$aName] = $this->_whereTables[$aName] = 1;
6a488035 2194
9d9cfb9f 2195 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op);
9b183452 2196 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
6a488035
TO
2197 }
2198 elseif ($name === 'world_region') {
2199 $this->optionValueQuery(
2200 $name, $op, $value, $grouping,
728eefa0 2201 NULL,
6a488035 2202 $field,
8b695e91
CW
2203 ts('World Region'),
2204 'Positive',
2205 TRUE
6a488035
TO
2206 );
2207 }
6a488035 2208 elseif ($name === 'is_deceased') {
962f4484 2209 $this->setQillAndWhere($name, $op, $value, $grouping, $field);
6a488035
TO
2210 self::$_openedPanes[ts('Demographics')] = TRUE;
2211 }
d820f4e8 2212 elseif ($name === 'created_date' || $name === 'modified_date' || $name === 'deceased_date' || $name === 'birth_date') {
ee657a1c 2213 $appendDateTime = TRUE;
d820f4e8 2214 if ($name === 'deceased_date' || $name === 'birth_date') {
ee657a1c 2215 $appendDateTime = FALSE;
d820f4e8 2216 self::$_openedPanes[ts('Demographics')] = TRUE;
2217 }
0b83b6c2 2218 $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime);
d820f4e8 2219 }
6a488035
TO
2220 elseif ($name === 'contact_id') {
2221 if (is_int($value)) {
2222 $this->_where[$grouping][] = self::buildClause($field['where'], $op, $value);
2223 $this->_qill[$grouping][] = "$field[title] $op $value";
2224 }
2225 }
2226 elseif ($name === 'name') {
2bde57c6 2227 $value = CRM_Core_DAO::escapeString($value);
6a488035 2228 if ($wildcard) {
6a488035 2229 $op = 'LIKE';
0b38e8f1 2230 $value = self::getWildCardedValue($wildcard, $op, $value);
6a488035 2231 }
2bde57c6 2232 CRM_Core_Error::deprecatedFunctionWarning('Untested code path');
2233 // @todo it's likely this code path is obsolete / never called. It is definitely not
2234 // passed through in our test suite.
2235 $this->_where[$grouping][] = self::buildClause($field['where'], $op, "'$value'");
6a488035
TO
2236 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2237 }
2238 elseif ($name === 'current_employer') {
6a488035 2239 if ($wildcard) {
6a488035 2240 $op = 'LIKE';
0b38e8f1 2241 $value = self::getWildCardedValue($wildcard, $op, $value);
6a488035 2242 }
30415e03 2243 $ceWhereClause = self::buildClause("contact_a.organization_name", $op,
0d77b56a 2244 $value
6a488035 2245 );
0d77b56a
PJ
2246 $ceWhereClause .= " AND contact_a.contact_type = 'Individual'";
2247 $this->_where[$grouping][] = $ceWhereClause;
6a488035
TO
2248 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2249 }
2250 elseif ($name === 'email_greeting') {
b7aa36b6 2251 CRM_Core_Error::deprecatedFunctionWarning('pass in email_greeting_id or email_greeting_display');
6a488035
TO
2252 $filterCondition = array('greeting_type' => 'email_greeting');
2253 $this->optionValueQuery(
2254 $name, $op, $value, $grouping,
2255 CRM_Core_PseudoConstant::greeting($filterCondition),
2256 $field,
2257 ts('Email Greeting')
2258 );
2259 }
2260 elseif ($name === 'postal_greeting') {
b7aa36b6 2261 CRM_Core_Error::deprecatedFunctionWarning('pass in postal_greeting_id or postal_greeting_display');
6a488035
TO
2262 $filterCondition = array('greeting_type' => 'postal_greeting');
2263 $this->optionValueQuery(
2264 $name, $op, $value, $grouping,
2265 CRM_Core_PseudoConstant::greeting($filterCondition),
2266 $field,
2267 ts('Postal Greeting')
2268 );
2269 }
2270 elseif ($name === 'addressee') {
b7aa36b6 2271 CRM_Core_Error::deprecatedFunctionWarning('pass in addressee_id or addressee_display');
6a488035
TO
2272 $filterCondition = array('greeting_type' => 'addressee');
2273 $this->optionValueQuery(
2274 $name, $op, $value, $grouping,
2275 CRM_Core_PseudoConstant::greeting($filterCondition),
2276 $field,
2277 ts('Addressee')
2278 );
2279 }
2280 elseif (substr($name, 0, 4) === 'url-') {
2281 $tName = 'civicrm_website';
2282 $this->_whereTables[$tName] = $this->_tables[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )";
2fc64082 2283 $value = CRM_Core_DAO::escapeString($value);
6a488035 2284 if ($wildcard) {
6a488035 2285 $op = 'LIKE';
0b38e8f1 2286 $value = self::getWildCardedValue($wildcard, $op, $value);
6a488035
TO
2287 }
2288
962f4484 2289 $this->_where[$grouping][] = $d = self::buildClause('civicrm_website.url', $op, $value);
6a488035
TO
2290 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2291 }
2292 elseif ($name === 'contact_is_deleted') {
962f4484 2293 $this->setQillAndWhere('is_deleted', $op, $value, $grouping, $field);
6a488035 2294 }
efb88612 2295 elseif (!empty($field['where'])) {
2296 $type = NULL;
2297 if (!empty($field['type'])) {
2298 $type = CRM_Utils_Type::typeToString($field['type']);
2299 }
2300
2301 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2302
2303 if (isset($locType[1]) &&
2304 is_numeric($locType[1])
2305 ) {
2306 $setTables = FALSE;
2307
2308 //get the location name
2309 list($tName, $fldName) = self::getLocationTableName($field['where'], $locType);
54e02ce8 2310 $fieldName = "`$tName`.$fldName";
efb88612 2311
2312 // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
2313 $this->_tables[$tName] = $this->_whereTables[$tName] = 1;
2314
2315 }
2316 else {
2317 if ($tableName == 'civicrm_contact') {
1809f3cf 2318 $fieldName = "contact_a.{$fieldName}";
efb88612 2319 }
2320 else {
7fde0061 2321 $fieldName = $field['where'];
efb88612 2322 }
2323 }
2324
2325 list($qillop, $qillVal) = self::buildQillForFieldValue(NULL, $field['title'], $value, $op);
9b183452 2326 $this->_qill[$grouping][] = ts("%1 %2 %3", array(
2327 1 => $field['title'],
2328 2 => $qillop,
2329 3 => (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) ? $qillVal : "'$qillVal'"));
efb88612 2330
6a488035 2331 if (is_array($value)) {
a75c13cc
EM
2332 // traditionally an array being passed has been a fatal error. We can take advantage of this to add support
2333 // for api style operators for functions that hit this point without worrying about regression
2334 // (the previous comments indicated the condition for hitting this point were unknown
2335 // per CRM-14743 we are adding modified_date & created_date operator support
2336 $operations = array_keys($value);
2337 foreach ($operations as $operator) {
11a5aa5d 2338 if (!in_array($operator, CRM_Core_DAO::acceptedSQLOperators())) {
efb88612 2339 //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators
2340 //so this condition will satisfy the search for now
0a74debb 2341 if (strpos($op, 'IN') !== FALSE) {
efb88612 2342 $value = array($op => $value);
2343 }
a75c13cc 2344 // we don't know when this might happen
efb88612 2345 else {
2346 CRM_Core_Error::fatal(ts("%1 is not a valid operator", array(1 => $operator)));
2347 }
a75c13cc
EM
2348 }
2349 }
efb88612 2350 $this->_where[$grouping][] = CRM_Core_DAO::createSQLFilter($fieldName, $value, $type);
6a488035 2351 }
efb88612 2352 else {
6a488035 2353 if ($wildcard) {
6a488035 2354 $op = 'LIKE';
0b38e8f1 2355 $value = self::getWildCardedValue($wildcard, $op, $value);
6a488035
TO
2356 }
2357
efb88612 2358 $this->_where[$grouping][] = self::buildClause($fieldName, $op, $value, $type);
6a488035
TO
2359 }
2360 }
2361
2362 if ($setTables && isset($field['where'])) {
2363 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2364 if (isset($tableName)) {
2365 $this->_tables[$tableName] = 1;
2366 $this->_whereTables[$tableName] = 1;
2367 }
2368 }
2369 }
2370
86538308
EM
2371 /**
2372 * @param $where
2373 * @param $locType
2374 *
2375 * @return array
2376 * @throws Exception
2377 */
00be9182 2378 public static function getLocationTableName(&$where, &$locType) {
6a488035
TO
2379 if (isset($locType[1]) && is_numeric($locType[1])) {
2380 list($tbName, $fldName) = explode(".", $where);
2381
2382 //get the location name
f9ba4d01 2383 $locationType = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
6a488035
TO
2384 $specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
2385 if (in_array($locType[0], $specialFields)) {
2386 //hack to fix / special handing for phone_ext
2387 if ($locType[0] == 'phone_ext') {
2388 $locType[0] = 'phone';
2389 }
2390 if (isset($locType[2]) && $locType[2]) {
2391 $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}";
2392 }
2393 else {
2394 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2395 }
2396 }
2397 elseif (in_array($locType[0],
5c7dad85
TO
2398 array(
2399 'address_name',
2400 'street_address',
7cc09daf 2401 'street_name',
2402 'street_number_suffix',
2403 'street_unit',
5c7dad85
TO
2404 'supplemental_address_1',
2405 'supplemental_address_2',
207f62c6 2406 'supplemental_address_3',
5c7dad85
TO
2407 'city',
2408 'postal_code',
2409 'postal_code_suffix',
2410 'geo_code_1',
2411 'geo_code_2',
7cc09daf 2412 'master_id',
5c7dad85
TO
2413 )
2414 )) {
6a488035 2415 //fix for search by profile with address fields.
a822d3d8 2416 $tName = "{$locationType[$locType[1]]}-address";
6a488035 2417 }
7cc09daf 2418 elseif (in_array($locType[0],
2419 array(
2420 'on_hold',
2421 'signature_html',
2422 'signature_text',
2423 'is_bulkmail',
2424 )
2425 )) {
6a488035
TO
2426 $tName = "{$locationType[$locType[1]]}-email";
2427 }
7cc09daf 2428 elseif ($locType[0] == 'provider_id') {
2429 $tName = "{$locationType[$locType[1]]}-im";
2430 }
2431 elseif ($locType[0] == 'openid') {
2432 $tName = "{$locationType[$locType[1]]}-openid";
2433 }
6a488035
TO
2434 else {
2435 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2436 }
2437 $tName = str_replace(' ', '_', $tName);
2438 return array($tName, $fldName);
2439 }
2440 CRM_Core_Error::fatal();
2441 }
2442
2443 /**
2444 * Given a result dao, extract the values and return that array
2445 *
c490a46a 2446 * @param CRM_Core_DAO $dao
6a488035 2447 *
a6c01b45
CW
2448 * @return array
2449 * values for this query
6a488035 2450 */
00be9182 2451 public function store($dao) {
6a488035
TO
2452 $value = array();
2453
2454 foreach ($this->_element as $key => $dontCare) {
2455 if (property_exists($dao, $key)) {
2456 if (strpos($key, '-') !== FALSE) {
2457 $values = explode('-', $key);
2458 $lastElement = array_pop($values);
2459 $current = &$value;
2460 $cnt = count($values);
2461 $count = 1;
2462 foreach ($values as $v) {
2463 if (!array_key_exists($v, $current)) {
2464 $current[$v] = array();
2465 }
2466 //bad hack for im_provider
2467 if ($lastElement == 'provider_id') {
2468 if ($count < $cnt) {
2469 $current = &$current[$v];
2470 }
2471 else {
2472 $lastElement = "{$v}_{$lastElement}";
2473 }
2474 }
2475 else {
2476 $current = &$current[$v];
2477 }
2478 $count++;
2479 }
2480
2481 $current[$lastElement] = $dao->$key;
2482 }
2483 else {
2484 $value[$key] = $dao->$key;
2485 }
2486 }
2487 }
2488 return $value;
2489 }
2490
2491 /**
fe482240 2492 * Getter for tables array.
6a488035
TO
2493 *
2494 * @return array
6a488035 2495 */
00be9182 2496 public function tables() {
6a488035
TO
2497 return $this->_tables;
2498 }
2499
86538308 2500 /**
746aa919
CB
2501 * Sometimes used to create the from clause, but, not reliably, set
2502 * this AND set tables.
2503 *
2504 * It's unclear the intent - there is a 'simpleFrom' clause which
2505 * takes whereTables into account & a fromClause which doesn't.
2506 *
2507 * logic may have eroded?
2508 *
86538308
EM
2509 * @return array
2510 */
00be9182 2511 public function whereTables() {
6a488035
TO
2512 return $this->_whereTables;
2513 }
2514
2515 /**
100fef9d 2516 * Generate the where clause (used in match contacts and permissions)
6a488035
TO
2517 *
2518 * @param array $params
2519 * @param array $fields
2520 * @param array $tables
77b97be7 2521 * @param $whereTables
77c5b619 2522 * @param bool $strict
6a488035
TO
2523 *
2524 * @return string
6a488035 2525 */
00be9182 2526 public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) {
6a488035
TO
2527 $query = new CRM_Contact_BAO_Query($params, NULL, $fields,
2528 FALSE, $strict
2529 );
2530
2531 $tables = array_merge($query->tables(), $tables);
2532 $whereTables = array_merge($query->whereTables(), $whereTables);
2533
2534 return $query->_whereClause;
2535 }
2536
2537 /**
fe482240 2538 * Create the from clause.
6a488035 2539 *
77c5b619 2540 * @param array $tables
84193f76
CB
2541 * Tables that need to be included in this from clause. If null,
2542 * return mimimal from clause (i.e. civicrm_contact).
77c5b619
TO
2543 * @param array $inner
2544 * Tables that should be inner-joined.
2545 * @param array $right
2546 * Tables that should be right-joined.
77b97be7 2547 * @param bool $primaryLocation
84193f76 2548 * Search on primary location. See note below.
77b97be7 2549 * @param int $mode
84193f76 2550 * Determines search mode based on bitwise MODE_* constants.
b3e1c09d 2551 * @param string|NULL $apiEntity
84193f76
CB
2552 * Determines search mode based on entity by string.
2553 *
2554 * The $primaryLocation flag only seems to be used when
2555 * locationType() has been called. This may be a search option
2556 * exposed, or perhaps it's a "search all details" approach which
2557 * predates decoupling of location types and primary fields?
6a488035 2558 *
6c58f897
CB
2559 * @see https://issues.civicrm.org/jira/browse/CRM-19967
2560 *
a6c01b45
CW
2561 * @return string
2562 * the from clause
6a488035 2563 */
b3e1c09d 2564 public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1, $apiEntity = NULL) {
6a488035
TO
2565
2566 $from = ' FROM civicrm_contact contact_a';
2567 if (empty($tables)) {
2568 return $from;
2569 }
2570
a7488080 2571 if (!empty($tables['civicrm_worldregion'])) {
6a488035
TO
2572 $tables = array_merge(array('civicrm_country' => 1), $tables);
2573 }
2574
8cc574cf 2575 if ((!empty($tables['civicrm_state_province']) || !empty($tables['civicrm_country']) ||
6a488035 2576 CRM_Utils_Array::value('civicrm_county', $tables)
5c7dad85
TO
2577 ) && empty($tables['civicrm_address'])
2578 ) {
6a488035
TO
2579 $tables = array_merge(array('civicrm_address' => 1),
2580 $tables
2581 );
2582 }
2583
6a488035 2584 // add group_contact and group table is subscription history is present
8cc574cf 2585 if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) {
6a488035 2586 $tables = array_merge(array(
5c7dad85 2587 'civicrm_group' => 1,
6a488035
TO
2588 'civicrm_group_contact' => 1,
2589 ),
2590 $tables
2591 );
2592 }
2593
2594 // to handle table dependencies of components
2595 CRM_Core_Component::tableNames($tables);
efa3a566
DS
2596 // to handle table dependencies of hook injected tables
2597 CRM_Contact_BAO_Query_Hook::singleton()->setTableDependency($tables);
6a488035
TO
2598
2599 //format the table list according to the weight
2600 $info = CRM_Core_TableHierarchy::info();
2601
2602 foreach ($tables as $key => $value) {
2603 $k = 99;
2604 if (strpos($key, '-') !== FALSE) {
2605 $keyArray = explode('-', $key);
2606 $k = CRM_Utils_Array::value('civicrm_' . $keyArray[1], $info, 99);
2607 }
2608 elseif (strpos($key, '_') !== FALSE) {
2609 $keyArray = explode('_', $key);
2610 if (is_numeric(array_pop($keyArray))) {
2611 $k = CRM_Utils_Array::value(implode('_', $keyArray), $info, 99);
2612 }
2613 else {
2614 $k = CRM_Utils_Array::value($key, $info, 99);
2615 }
2616 }
2617 else {
2618 $k = CRM_Utils_Array::value($key, $info, 99);
2619 }
2620 $tempTable[$k . ".$key"] = $key;
2621 }
2622 ksort($tempTable);
2623 $newTables = array();
2624 foreach ($tempTable as $key) {
2625 $newTables[$key] = $tables[$key];
2626 }
2627
2628 $tables = $newTables;
2629
2630 foreach ($tables as $name => $value) {
2631 if (!$value) {
2632 continue;
2633 }
2634
a7488080 2635 if (!empty($inner[$name])) {
6a488035
TO
2636 $side = 'INNER';
2637 }
a7488080 2638 elseif (!empty($right[$name])) {
6a488035
TO
2639 $side = 'RIGHT';
2640 }
2641 else {
2642 $side = 'LEFT';
2643 }
2644
2645 if ($value != 1) {
2646 // if there is already a join statement in value, use value itself
2647 if (strpos($value, 'JOIN')) {
2648 $from .= " $value ";
2649 }
2650 else {
2651 $from .= " $side JOIN $name ON ( $value ) ";
2652 }
2653 continue;
2654 }
0606198b 2655
af023bf8
EE
2656 $from .= self::getEntitySpecificJoins($name, $mode, $side, $primaryLocation);
2657 }
2658 return $from;
2659 }
6a488035 2660
af023bf8
EE
2661 /**
2662 * Get join statements for the from clause depending on entity type
2663 *
2664 * @param string $name
2665 * @param int $mode
2666 * @param string $side
2667 * @param string $primaryLocation
2668 * @return string
2669 */
2670 protected static function getEntitySpecificJoins($name, $mode, $side, $primaryLocation) {
2671 $limitToPrimaryClause = $primaryLocation ? "AND {$name}.is_primary = 1" : '';
2672 switch ($name) {
2673 case 'civicrm_address':
2674 //CRM-14263 further handling of address joins further down...
2675 return " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id {$limitToPrimaryClause} )";
6a488035 2676
af023bf8
EE
2677 case 'civicrm_phone':
2678 return " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id {$limitToPrimaryClause}) ";
6a488035 2679
af023bf8
EE
2680 case 'civicrm_email':
2681 return " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id {$limitToPrimaryClause})";
6a488035 2682
af023bf8
EE
2683 case 'civicrm_im':
2684 return " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id {$limitToPrimaryClause}) ";
6a488035 2685
af023bf8
EE
2686 case 'im_provider':
2687 $from = " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) ";
2688 $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'";
2689 $from .= " $side JOIN civicrm_option_value im_provider ON (civicrm_im.provider_id = im_provider.value AND option_group_imProvider.id = im_provider.option_group_id)";
2690 return $from;
6a488035 2691
af023bf8
EE
2692 case 'civicrm_openid':
2693 return " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id {$limitToPrimaryClause} )";
6a488035 2694
af023bf8
EE
2695 case 'civicrm_worldregion':
2696 $from = " $side JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id ";
2697 return "$from $side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ";
0cd50623 2698
af023bf8
EE
2699 case 'civicrm_location_type':
2700 return " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id ";
6a488035 2701
af023bf8
EE
2702 case 'civicrm_group':
2703 return " $side JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id ";
6a488035 2704
af023bf8
EE
2705 case 'civicrm_group_contact':
2706 return " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id ";
6a488035 2707
af023bf8
EE
2708 case 'civicrm_group_contact_cache':
2709 return " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id ";
6a488035 2710
af023bf8
EE
2711 case 'civicrm_activity':
2712 case 'civicrm_activity_tag':
2713 case 'activity_type':
2714 case 'activity_status':
2715 case 'parent_id':
2716 case 'civicrm_activity_contact':
2717 case 'source_contact':
2718 case 'activity_priority':
2719 return CRM_Activity_BAO_Query::from($name, $mode, $side);
2720
2721 case 'civicrm_entity_tag':
2722 $from = " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact'";
2723 return "$from AND civicrm_entity_tag.entity_id = contact_a.id ) ";
2724
2725 case 'civicrm_note':
2726 $from = " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact'";
2727 return "$from AND contact_a.id = civicrm_note.entity_id ) ";
2728
2729 case 'civicrm_subscription_history':
2730 $from = " $side JOIN civicrm_subscription_history";
2731 $from .= " ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id";
2732 return "$from AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id";
2733
2734 case 'civicrm_relationship':
2735 if (self::$_relType == 'reciprocal') {
2736 if (self::$_relationshipTempTable) {
2737 // we have a temptable to join on
2738 $tbl = self::$_relationshipTempTable;
2739 return " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id";
6a488035
TO
2740 }
2741 else {
af023bf8
EE
2742 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)";
2743 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id OR civicrm_relationship.contact_id_b = contact_b.id)";
2744 return $from;
6a488035 2745 }
af023bf8
EE
2746 }
2747 elseif (self::$_relType == 'b') {
2748 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )";
2749 return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
2750 }
2751 else {
2752 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )";
2753 return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
2754 }
6a488035 2755
af023bf8
EE
2756 case 'civicrm_log':
2757 $from = " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')";
2758 return "$from INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)";
6a488035 2759
af023bf8
EE
2760 case 'civicrm_tag':
2761 return " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id ";
6a488035 2762
af023bf8
EE
2763 case 'civicrm_grant':
2764 return CRM_Grant_BAO_Query::from($name, $mode, $side);
6a488035 2765
af023bf8
EE
2766 case 'civicrm_website':
2767 return " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id ";
7851dc81 2768
af023bf8
EE
2769 case 'civicrm_campaign':
2770 //Move to default case if not in either mode.
2771 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
2772 return CRM_Contribute_BAO_Query::from($name, $mode, $side);
2773 }
2774 elseif ($mode & CRM_Contact_BAO_Query::MODE_MAILING) {
2775 return CRM_Mailing_BAO_Query::from($name, $mode, $side);
2776 }
2777 elseif ($mode & CRM_Contact_BAO_Query::MODE_CAMPAIGN) {
2778 return CRM_Campaign_BAO_Query::from($name, $mode, $side);
2779 }
6a488035 2780
af023bf8
EE
2781 default:
2782 $locationTypeName = '';
2783 if (strpos($name, '-address') != 0) {
2784 $locationTypeName = 'address';
2785 }
2786 elseif (strpos($name, '-phone') != 0) {
2787 $locationTypeName = 'phone';
2788 }
2789 elseif (strpos($name, '-email') != 0) {
2790 $locationTypeName = 'email';
2791 }
2792 elseif (strpos($name, '-im') != 0) {
2793 $locationTypeName = 'im';
2794 }
2795 elseif (strpos($name, '-openid') != 0) {
2796 $locationTypeName = 'openid';
2797 }
7cc09daf 2798
af023bf8
EE
2799 if ($locationTypeName) {
2800 //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
2801 $parts = explode('-', $name);
2802 $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
2803 foreach ($locationTypes as $locationTypeID => $locationType) {
2804 if ($parts[0] == str_replace(' ', '_', $locationType)) {
2805 $locationID = $locationTypeID;
140e25ee 2806 }
e5fccefb 2807 }
af023bf8
EE
2808 $from = " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
2809 }
2810 else {
2811 $from = CRM_Core_Component::from($name, $mode, $side);
2812 }
2813 $from .= CRM_Contact_BAO_Query_Hook::singleton()->buildSearchfrom($name, $mode, $side);
99e9587a 2814
af023bf8 2815 return $from;
6a488035 2816 }
6a488035
TO
2817 }
2818
2819 /**
2820 * WHERE / QILL clause for deleted_contacts
2821 *
c037736a 2822 * @param array $values
6a488035 2823 */
00be9182 2824 public function deletedContacts($values) {
6a488035
TO
2825 list($_, $_, $value, $grouping, $_) = $values;
2826 if ($value) {
2827 // *prepend* to the relevant grouping as this is quite an important factor
2828 array_unshift($this->_qill[$grouping], ts('Search in Trash'));
2829 }
2830 }
2831
2832 /**
100fef9d 2833 * Where / qill clause for contact_type
6a488035 2834 *
77b97be7 2835 * @param $values
6a488035 2836 */
00be9182 2837 public function contactType(&$values) {
6a488035
TO
2838 list($name, $op, $value, $grouping, $wildcard) = $values;
2839
2840 $subTypes = array();
2841 $clause = array();
2842
2843 // account for search builder mapping multiple values
2844 if (!is_array($value)) {
2845 $values = self::parseSearchBuilderString($value, 'String');
2846 if (is_array($values)) {
2847 $value = array_flip($values);
2848 }
2849 }
2850
2851 if (is_array($value)) {
2852 foreach ($value as $k => $v) {
2853 // fix for CRM-771
2854 if ($k) {
2855 $subType = NULL;
2856 $contactType = $k;
2857 if (strpos($k, CRM_Core_DAO::VALUE_SEPARATOR)) {
2858 list($contactType, $subType) = explode(CRM_Core_DAO::VALUE_SEPARATOR, $k, 2);
2859 }
2860
2861 if (!empty($subType)) {
2862 $subTypes[$subType] = 1;
2863 }
2864 $clause[$contactType] = "'" . CRM_Utils_Type::escape($contactType, 'String') . "'";
2865 }
2866 }
2867 }
2868 else {
2869 $contactTypeANDSubType = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value, 2);
2870 $contactType = $contactTypeANDSubType[0];
2871 $subType = CRM_Utils_Array::value(1, $contactTypeANDSubType);
2872 if (!empty($subType)) {
2873 $subTypes[$subType] = 1;
2874 }
2875 $clause[$contactType] = "'" . CRM_Utils_Type::escape($contactType, 'String') . "'";
2876 }
2877
2878 // fix for CRM-771
2879 if (!empty($clause)) {
c33e01a5 2880 $quill = $clause;
3b3bc827
CW
2881 if ($op == 'IN' || $op == 'NOT IN') {
2882 $this->_where[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')';
2883 }
2884 else {
2885 $type = array_pop($clause);
dbc6f6d6 2886 $this->_where[$grouping][] = self::buildClause("contact_a.contact_type", $op, $contactType);
3b3bc827 2887 }
d4b81b9a 2888
aa3c3360 2889 $this->_qill[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill);
6a488035
TO
2890
2891 if (!empty($subTypes)) {
2892 $this->includeContactSubTypes($subTypes, $grouping);
2893 }
2894 }
2895 }
2896
2897 /**
8f165fa5 2898 * Where / qill clause for contact_sub_type.
6a488035 2899 *
8f165fa5 2900 * @param array $values
6a488035 2901 */
00be9182 2902 public function contactSubType(&$values) {
6a488035 2903 list($name, $op, $value, $grouping, $wildcard) = $values;
1d2bcc81 2904 $this->includeContactSubTypes($value, $grouping, $op);
6a488035
TO
2905 }
2906
86538308
EM
2907 /**
2908 * @param $value
2909 * @param $grouping
0e2e76cf 2910 * @param string $op
86538308 2911 */
00be9182 2912 public function includeContactSubTypes($value, $grouping, $op = 'LIKE') {
6a488035 2913
e9fd1f48 2914 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
2915 $op = key($value);
2916 $value = $value[$op];
2917 }
2918
6a488035
TO
2919 $clause = array();
2920 $alias = "contact_a.contact_sub_type";
46b3417a 2921 $qillOperators = CRM_Core_SelectValues::getSearchBuilderOperators();
6a488035 2922
1d2bcc81 2923 $op = str_replace('IN', 'LIKE', $op);
ccc79dfc 2924 $op = str_replace('=', 'LIKE', $op);
2925 $op = str_replace('!', 'NOT ', $op);
1d2bcc81 2926
2927 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
2928 $this->_where[$grouping][] = self::buildClause($alias, $op, $value, 'String');
2929 }
4c9b6178 2930 elseif (is_array($value)) {
6a488035 2931 foreach ($value as $k => $v) {
35fbf8a2 2932 $clause[$k] = "($alias $op '%" . CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($v, 'String') . CRM_Core_DAO::VALUE_SEPARATOR . "%')";
6a488035
TO
2933 }
2934 }
2935 else {
1d2bcc81 2936 $clause[$value] = "($alias $op '%" . CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($value, 'String') . CRM_Core_DAO::VALUE_SEPARATOR . "%')";
6a488035
TO
2937 }
2938
2939 if (!empty($clause)) {
2940 $this->_where[$grouping][] = "( " . implode(' OR ', $clause) . " )";
6a488035 2941 }
1d2bcc81 2942 $this->_qill[$grouping][] = ts('Contact Subtype %1 ', array(1 => $qillOperators[$op])) . implode(' ' . ts('or') . ' ', array_keys($clause));
6a488035
TO
2943 }
2944
2945 /**
c3137c08 2946 * Where / qill clause for groups.
6a488035 2947 *
77b97be7 2948 * @param $values
6a488035 2949 */
c3137c08 2950 public function group($values) {
6a488035
TO
2951 list($name, $op, $value, $grouping, $wildcard) = $values;
2952
ca91bd7f 2953 // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first
2954 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
2955 $op = key($value);
2956 $value = $value[$op];
2957 }
2958
e8401ae2 2959 // Replace pseudo operators from search builder
2960 $op = str_replace('EMPTY', 'NULL', $op);
2961
ca91bd7f 2962 if (strpos($op, 'NULL')) {
2963 $value = NULL;
2964 }
2965
0d71d885 2966 if (is_array($value) && count($value) > 1) {
e8401ae2 2967 if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) {
2968 CRM_Core_Error::fatal(ts("%1 is not a valid operator", array(1 => $op)));
2969 }
6a488035
TO
2970 $this->_useDistinct = TRUE;
2971 }
2972
7cc09daf 2973 if (isset($value)) {
2974 $value = CRM_Utils_Array::value($op, $value, $value);
2975 }
2976
d412bcca 2977 if ($name == 'group_type') {
2978 $value = array_keys($this->getGroupsFromTypeCriteria($value));
2979 }
2980
181962cf 2981 $regularGroupIDs = $smartGroupIDs = array();
2982 foreach ((array) $value as $id) {
2983 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
2984 $smartGroupIDs[] = $id;
2985 }
2986 else {
2987 $regularGroupIDs[] = $id;
2988 }
2989 }
c3137c08 2990
e8401ae2 2991 $isNotOp = ($op == 'NOT IN' || $op == '!=');
d79d2a59 2992
6a488035 2993 $statii = array();
05b13102 2994 $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
6a488035
TO
2995 if ($gcsValues &&
2996 is_array($gcsValues[2])
2997 ) {
2998 foreach ($gcsValues[2] as $k => $v) {
2999 if ($v) {
6a488035
TO
3000 $statii[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
3001 }
3002 }
3003 }
3004 else {
181962cf 3005 $statii[] = "'Added'";
6a488035 3006 }
181962cf 3007 $groupClause = array();
3008 if (count($regularGroupIDs) || empty($value)) {
783144b0 3009 // include child groups IDs if any
3010 $childGroupIds = (array) CRM_Contact_BAO_Group::getChildGroupIds($regularGroupIDs);
3011 foreach ($childGroupIds as $key => $id) {
3012 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
3013 $smartGroupIDs[] = $id;
3014 unset($childGroupIds[$key]);
3015 }
3016 }
3017 if (count($childGroupIds)) {
3018 $regularGroupIDs = array_merge($regularGroupIDs, $childGroupIds);
3019 }
3020
3021 // if $regularGroupIDs is populated with regular child group IDs
3022 // then change the mysql operator to desired
3023 if (count($regularGroupIDs) > 1) {
3024 $op = strpos($op, 'IN') ? $op : ($op == '!=') ? 'NOT IN' : 'IN';
3025 }
181962cf 3026 $groupIds = implode(',', (array) $regularGroupIDs);
f30aa754 3027 $gcTable = "`civicrm_group_contact-" . uniqid() . "`";
d79d2a59 3028 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
783144b0 3029
e8401ae2 3030 if (strpos($op, 'IN') !== FALSE) {
783144b0 3031 $clause = "{$gcTable}.group_id $op ( $groupIds ) ";
e8401ae2 3032 }
c112a6f9 3033 elseif ($op == '!=') {
783144b0 3034 $clause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds )";
c112a6f9 3035 }
3036 else {
783144b0 3037 $clause = "{$gcTable}.group_id $op $groupIds ";
c112a6f9 3038 }
783144b0 3039 $groupClause[] = "( {$clause} )";
6a488035 3040
783144b0 3041 if ($statii) {
3042 $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")";
6a488035 3043 }
783144b0 3044 $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
6a488035
TO
3045 }
3046
181962cf 3047 //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table
9f6a1556
JP
3048 $groupContactCacheClause = '';
3049 if (count($smartGroupIDs) || empty($value)) {
f30aa754 3050 $gccTableAlias = "civicrm_group_contact_cache";
9f6a1556 3051 $groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, $gccTableAlias, "contact_a", $op);
6451118c 3052 if (!empty($groupContactCacheClause)) {
9f6a1556
JP
3053 if ($isNotOp) {
3054 $groupIds = implode(',', (array) $smartGroupIDs);
f30aa754 3055 $gcTable = "civicrm_group_contact";
9f6a1556
JP
3056 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
3057 $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
3058 if (strpos($op, 'IN') !== FALSE) {
3059 $groupClause[] = "{$gcTable}.group_id $op ( $groupIds ) AND {$gccTableAlias}.group_id IS NULL";
3060 }
3061 else {
3062 $groupClause[] = "{$gcTable}.group_id $op $groupIds AND {$gccTableAlias}.group_id IS NULL";
3063 }
3064 }
6451118c
JP
3065 $groupClause[] = " ( {$groupContactCacheClause} ) ";
3066 }
6a488035
TO
3067 }
3068
883e1e76 3069 $and = ($op == 'IS NULL') ? ' AND ' : ' OR ';
6451118c
JP
3070 if (!empty($groupClause)) {
3071 $this->_where[$grouping][] = ' ( ' . implode($and, $groupClause) . ' ) ';
3072 }
181962cf 3073
e8401ae2 3074 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
3075 $this->_qill[$grouping][] = ts("Group(s) %1 %2", array(1 => $qillop, 2 => $qillVal));
0cd50623 3076 if (strpos($op, 'NULL') === FALSE) {
3077 $this->_qill[$grouping][] = ts("Group Status %1", array(1 => implode(' ' . ts('or') . ' ', $statii)));
3078 }
6a488035 3079 }
d424ffde 3080
86538308 3081 /**
fe482240 3082 * Function translates selection of group type into a list of groups.
86538308
EM
3083 * @param $value
3084 *
3085 * @return array
3086 */
5c7dad85 3087 public function getGroupsFromTypeCriteria($value) {
6a488035 3088 $groupIds = array();
d79d2a59 3089 foreach ((array) $value as $groupTypeValue) {
6a488035
TO
3090 $groupList = CRM_Core_PseudoConstant::group($groupTypeValue);
3091 $groupIds = ($groupIds + $groupList);
3092 }
3093 return $groupIds;
3094 }
3095
e6b676a0 3096 /**
72a2eeab
AS
3097 * Prime smart group cache for smart groups in the search, and join
3098 * civicrm_group_contact_cache table into the query.
3099 *
3100 * @param array $groups IDs of groups specified in search criteria.
3101 * @param string $tableAlias Alias to use for civicrm_group_contact_cache table.
3102 * @param string $joinTable Table on which to join civicrm_group_contact_cache
3103 * @param string $op SQL comparison operator (NULL, IN, !=, IS NULL, etc.)
3104 * @param string $joinColumn Column in $joinTable on which to join civicrm_group_contact_cache.contact_id
e6b676a0 3105 *
72a2eeab 3106 * @return string WHERE clause component for smart group criteria.
e6b676a0 3107 */
f30aa754 3108 public function addGroupContactCache($groups, $tableAlias, $joinTable = "contact_a", $op, $joinColumn = 'id') {
e8401ae2 3109 $isNullOp = (strpos($op, 'NULL') !== FALSE);
3110 $groupsIds = $groups;
6451118c
JP
3111
3112 $operator = ['=' => 'IN', '!=' => 'NOT IN'];
3113 if (!empty($operator[$op]) && is_array($groups)) {
3114 $op = $operator[$op];
3115 }
e8401ae2 3116 if (!$isNullOp && !$groups) {
6a488035
TO
3117 return NULL;
3118 }
e8401ae2 3119 elseif (strpos($op, 'IN') !== FALSE) {
3120 $groups = array($op => $groups);
3121 }
d4ed6fbd 3122 elseif (is_array($groups) && count($groups)) {
3123 $groups = array('IN' => $groups);
3124 }
6a488035 3125
e8401ae2 3126 // Find all the groups that are part of a saved search.
3127 $smartGroupClause = self::buildClause("id", $op, $groups, 'Int');
6a488035
TO
3128 $sql = "
3129SELECT id, cache_date, saved_search_id, children
3130FROM civicrm_group
e8401ae2 3131WHERE $smartGroupClause
6a488035
TO
3132 AND ( saved_search_id != 0
3133 OR saved_search_id IS NOT NULL
3134 OR children IS NOT NULL )
3135";
e6b676a0 3136
5c7dad85 3137 $group = CRM_Core_DAO::executeQuery($sql);
e6b676a0 3138
6a488035 3139 while ($group->fetch()) {
6a488035 3140 $this->_useDistinct = TRUE;
6a488035
TO
3141 if (!$this->_smartGroupCache || $group->cache_date == NULL) {
3142 CRM_Contact_BAO_GroupContactCache::load($group);
3143 }
6a488035 3144 }
9f6a1556 3145 if ($group->N == 0 && $op != 'NOT IN') {
485a3a1f 3146 return NULL;
3147 }
6a488035 3148
72a2eeab 3149 $this->_tables[$tableAlias] = $this->_whereTables[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.{$joinColumn} = {$tableAlias}.contact_id ";
9f6a1556
JP
3150
3151 if ($op == 'NOT IN') {
3152 return "{$tableAlias}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( " . implode(',', (array) $groupsIds) . " ) )";
3153 }
e8401ae2 3154 return self::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
6a488035
TO
3155 }
3156
3157 /**
100fef9d 3158 * Where / qill clause for cms users
6a488035 3159 *
77b97be7 3160 * @param $values
6a488035 3161 */
00be9182 3162 public function ufUser(&$values) {
6a488035
TO
3163 list($name, $op, $value, $grouping, $wildcard) = $values;
3164
3165 if ($value == 1) {
3166 $this->_tables['civicrm_uf_match'] = $this->_whereTables['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3167
3168 $this->_qill[$grouping][] = ts('CMS User');
3169 }
3170 elseif ($value == 0) {
3171 $this->_tables['civicrm_uf_match'] = $this->_whereTables['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3172
3173 $this->_where[$grouping][] = " civicrm_uf_match.contact_id IS NULL";
3174 $this->_qill[$grouping][] = ts('Not a CMS User');
3175 }
3176 }
3177
3178 /**
fe482240 3179 * All tag search specific.
6a488035 3180 *
c037736a 3181 * @param array $values
6a488035 3182 */
00be9182 3183 public function tagSearch(&$values) {
6a488035
TO
3184 list($name, $op, $value, $grouping, $wildcard) = $values;
3185
3186 $op = "LIKE";
3187 $value = "%{$value}%";
3188
6a488035
TO
3189 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3190 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3191
3192 $etTable = "`civicrm_entity_tag-" . $value . "`";
3193 $tTable = "`civicrm_tag-" . $value . "`";
3194
3195 if ($useAllTagTypes[2]) {
59f4c9ee
TO
3196 $this->_tables[$etTable] = $this->_whereTables[$etTable]
3197 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id)
3198 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )";
6a488035
TO
3199
3200 // search tag in cases
3201 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3202 $tCaseTable = "`civicrm_case_tag-" . $value . "`";
59f4c9ee
TO
3203 $this->_tables[$etCaseTable] = $this->_whereTables[$etCaseTable]
3204 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3205 LEFT JOIN civicrm_case
6a488035
TO
3206 ON (civicrm_case_contact.case_id = civicrm_case.id
3207 AND civicrm_case.is_deleted = 0 )
59f4c9ee
TO
3208 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id )
3209 LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )";
6a488035
TO
3210 // search tag in activities
3211 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3212 $tActTable = "`civicrm_act_tag-" . $value . "`";
44f817d4 3213 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
9e74e3ce 3214 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
1071730c 3215
59f4c9ee
TO
3216 $this->_tables[$etActTable] = $this->_whereTables[$etActTable]
3217 = " LEFT JOIN civicrm_activity_contact
9e74e3ce 3218 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
59f4c9ee 3219 LEFT JOIN civicrm_activity
91da6cd5 3220 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
6a488035 3221 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
59f4c9ee
TO
3222 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id )
3223 LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )";
6a488035 3224
5c7dad85 3225 $this->_where[$grouping][] = "({$tTable}.name $op '" . $value . "' OR {$tCaseTable}.name $op '" . $value . "' OR {$tActTable}.name $op '" . $value . "')";
8a88287d 3226 $this->_qill[$grouping][] = ts('Tag %1 %2', array(1 => $tagTypesText[2], 2 => $op)) . ' ' . $value;
5c7dad85
TO
3227 }
3228 else {
6a488035
TO
3229 $etTable = "`civicrm_entity_tag-" . $value . "`";
3230 $tTable = "`civicrm_tag-" . $value . "`";
3231 $this->_tables[$etTable] = $this->_whereTables[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND
3232 {$etTable}.entity_table = 'civicrm_contact' )
3233 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) ";
3234
3235 $this->_where[$grouping][] = self::buildClause("{$tTable}.name", $op, $value, 'String');
3236 $this->_qill[$grouping][] = ts('Tagged %1', array(1 => $op)) . ' ' . $value;
3237 }
3238 }
3239
3240 /**
8f165fa5 3241 * Where / qill clause for tag.
6a488035 3242 *
c037736a 3243 * @param array $values
6a488035 3244 */
00be9182 3245 public function tag(&$values) {
6a488035
TO
3246 list($name, $op, $value, $grouping, $wildcard) = $values;
3247
7cc09daf 3248 list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, array('onlyActive' => FALSE));
3249 // API/Search Builder format array(operator => array(values))
6a488035 3250 if (is_array($value)) {
7cc09daf 3251 if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
3252 $op = key($value);
3253 $value = $value[$op];
3254 }
6a488035
TO
3255 if (count($value) > 1) {
3256 $this->_useDistinct = TRUE;
3257 }
7cc09daf 3258 $value = implode(',', (array) $value);
6a488035
TO
3259 }
3260
6a488035
TO
3261 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3262 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3263
3264 $etTable = "`civicrm_entity_tag-" . $value . "`";
3265
3266 if ($useAllTagTypes[2]) {
59f4c9ee
TO
3267 $this->_tables[$etTable] = $this->_whereTables[$etTable]
3268 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
6a488035
TO
3269
3270 // search tag in cases
3271 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
44f817d4 3272 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
9e74e3ce 3273 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
3274
59f4c9ee
TO
3275 $this->_tables[$etCaseTable] = $this->_whereTables[$etCaseTable]
3276 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3277 LEFT JOIN civicrm_case
6a488035
TO
3278 ON (civicrm_case_contact.case_id = civicrm_case.id
3279 AND civicrm_case.is_deleted = 0 )
59f4c9ee 3280 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) ";
6a488035
TO
3281 // search tag in activities
3282 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
59f4c9ee
TO
3283 $this->_tables[$etActTable] = $this->_whereTables[$etActTable]
3284 = " LEFT JOIN civicrm_activity_contact
9e74e3ce 3285 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
59f4c9ee 3286 LEFT JOIN civicrm_activity
91da6cd5 3287 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
6a488035 3288 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
59f4c9ee 3289 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) ";
6a488035
TO
3290
3291 // CRM-10338
5c7dad85 3292 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
6a488035
TO
3293 $this->_where[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)";
3294 }
3295 else {
5c7dad85 3296 $this->_where[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
6a488035 3297 }
5c7dad85
TO
3298 }
3299 else {
59f4c9ee
TO
3300 $this->_tables[$etTable] = $this->_whereTables[$etTable]
3301 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
6a488035
TO
3302
3303 // CRM-10338
5c7dad85 3304 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
6a488035
TO
3305 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3306 $op = str_replace('EMPTY', 'NULL', $op);
3307 $this->_where[$grouping][] = "{$etTable}.tag_id $op";
3308 }
c112a6f9 3309 // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag
3310 elseif ($op == '!=') {
3311 $this->_where[$grouping][] = "{$etTable}.entity_id NOT IN (SELECT entity_id FROM civicrm_entity_tag cet WHERE cet.entity_table = 'civicrm_contact' AND " . self::buildClause("cet.tag_id", '=', $value, 'Int') . ")";
3312 }
3313 elseif ($op == '=' || strstr($op, 'IN')) {
3314 $op = ($op == '=') ? 'IN' : $op;
3315 $this->_where[$grouping][] = "{$etTable}.tag_id $op ( $value )";
6a488035 3316 }
6a488035 3317 }
7cc09daf 3318 $this->_qill[$grouping][] = ts('Tagged %1 %2', array(1 => $qillop, 2 => $qillVal));
6a488035
TO
3319 }
3320
3321 /**
100fef9d 3322 * Where/qill clause for notes
6a488035 3323 *
c037736a 3324 * @param array $values
6a488035 3325 */
00be9182 3326 public function notes(&$values) {
6a488035
TO
3327 list($name, $op, $value, $grouping, $wildcard) = $values;
3328
3329 $noteOptionValues = $this->getWhereValues('note_option', $grouping);
3330 $noteOption = CRM_Utils_Array::value('2', $noteOptionValues, '6');
3331 $noteOption = ($name == 'note_body') ? 2 : (($name == 'note_subject') ? 3 : $noteOption);
3332
3333 $this->_useDistinct = TRUE;
3334
59f4c9ee
TO
3335 $this->_tables['civicrm_note'] = $this->_whereTables['civicrm_note']
3336 = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) ";
6a488035 3337
6a488035 3338 $n = trim($value);
2fc64082 3339 $value = CRM_Core_DAO::escapeString($n);
05d214de 3340 if ($wildcard) {
6a488035
TO
3341 if (strpos($value, '%') === FALSE) {
3342 $value = "%$value%";
3343 }
3344 $op = 'LIKE';
3345 }
3346 elseif ($op == 'IS NULL' || $op == 'IS NOT NULL') {
3347 $value = NULL;
3348 }
3349
3350 $label = NULL;
3351 $clauses = array();
5c7dad85 3352 if ($noteOption % 2 == 0) {
6a488035
TO
3353 $clauses[] = self::buildClause('civicrm_note.note', $op, $value, 'String');
3354 $label = ts('Note: Body Only');
3355 }
5c7dad85 3356 if ($noteOption % 3 == 0) {
6a488035
TO
3357 $clauses[] = self::buildClause('civicrm_note.subject', $op, $value, 'String');
3358 $label = $label ? ts('Note: Body and Subject') : ts('Note: Subject Only');
3359 }
3360 $this->_where[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
05d214de 3361 list($qillOp, $qillVal) = self::buildQillForFieldValue(NULL, $name, $n, $op);
3362 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillOp, 3 => $qillVal));
6a488035
TO
3363 }
3364
86538308 3365 /**
100fef9d 3366 * @param string $name
86538308
EM
3367 * @param $op
3368 * @param $grouping
3369 *
3370 * @return bool
3371 */
00be9182 3372 public function nameNullOrEmptyOp($name, $op, $grouping) {
5c7dad85 3373 switch ($op) {
6a488035
TO
3374 case 'IS NULL':
3375 case 'IS NOT NULL':
3376 $this->_where[$grouping][] = "contact_a.$name $op";
3377 $this->_qill[$grouping][] = ts('Name') . ' ' . $op;
5c7dad85 3378 return TRUE;
6a488035
TO
3379
3380 case 'IS EMPTY':
3381 $this->_where[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')";
3382 $this->_qill[$grouping][] = ts('Name') . ' ' . $op;
5c7dad85 3383 return TRUE;
6a488035
TO
3384
3385 case 'IS NOT EMPTY':
3386 $this->_where[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')";
3387 $this->_qill[$grouping][] = ts('Name') . ' ' . $op;
5c7dad85 3388 return TRUE;
6a488035
TO
3389
3390 default:
5c7dad85 3391 return FALSE;
6a488035
TO
3392 }
3393 }
3394
3395 /**
100fef9d 3396 * Where / qill clause for sort_name
6a488035 3397 *
c037736a 3398 * @param array $values
6a488035 3399 */
00be9182 3400 public function sortName(&$values) {
fd816db5 3401 list($fieldName, $op, $value, $grouping, $wildcard) = $values;
6a488035
TO
3402
3403 // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY
fd816db5 3404 if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) {
6a488035
TO
3405 return;
3406 }
3407
fd816db5 3408 $input = $value = trim($value);
6a488035 3409
fd816db5 3410 if (!strlen($value)) {
6a488035
TO
3411 return;
3412 }
3413
3414 $config = CRM_Core_Config::singleton();
3415
3416 $sub = array();
3417
3418 //By default, $sub elements should be joined together with OR statements (don't change this variable).
3419 $subGlue = ' OR ';
3420
fd816db5
CW
3421 $firstChar = substr($value, 0, 1);
3422 $lastChar = substr($value, -1, 1);
3423 $quotes = array("'", '"');
3424 // If string is quoted, strip quotes and otherwise don't alter it
3425 if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) {
3426 $value = trim($value, implode('', $quotes));
3427 }
3428 // Replace spaces with wildcards for a LIKE operation
3429 // UNLESS string contains a comma (this exception is a tiny bit questionable)
3430 elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) {
3431 $value = str_replace(' ', '%', $value);
3432 }
9436d5d5 3433 $value = CRM_Core_DAO::escapeString(trim($value));
fd816db5
CW
3434 if (strlen($value)) {
3435 $fieldsub = array();
0b38e8f1 3436 $value = "'" . self::getWildCardedValue($wildcard, $op, $value) . "'";
fd816db5 3437 if ($fieldName == 'sort_name') {
9436d5d5 3438 $wc = "contact_a.sort_name";
6a488035
TO
3439 }
3440 else {
9436d5d5 3441 $wc = "contact_a.display_name";
6a488035 3442 }
fd816db5 3443 $fieldsub[] = " ( $wc $op $value )";
6a488035 3444 if ($config->includeNickNameInName) {
9436d5d5 3445 $wc = "contact_a.nick_name";
fd816db5 3446 $fieldsub[] = " ( $wc $op $value )";
6a488035
TO
3447 }
3448 if ($config->includeEmailInName) {
fd816db5 3449 $fieldsub[] = " ( civicrm_email.email $op $value ) ";
6a488035 3450 }
fd816db5 3451 $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
6a488035
TO
3452 }
3453
3454 $sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
3455
3456 $this->_where[$grouping][] = $sub;
3457 if ($config->includeEmailInName) {
3458 $this->_tables['civicrm_email'] = $this->_whereTables['civicrm_email'] = 1;
fd816db5 3459 $this->_qill[$grouping][] = ts('Name or Email') . " $op - '$input'";
6a488035
TO
3460 }
3461 else {
fd816db5 3462 $this->_qill[$grouping][] = ts('Name') . " $op - '$input'";
6a488035
TO
3463 }
3464 }
3465
0e2e76cf 3466 /**
cde2037d 3467 * Where/qill clause for greeting fields.
02adf2f9 3468 *
cd2dfbd7 3469 * @param array $values
02adf2f9 3470 */
0e2e76cf 3471 public function greetings(&$values) {
02adf2f9
BS
3472 list($name, $op, $value, $grouping, $wildcard) = $values;
3473 $name .= '_display';
3474
9b183452 3475 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op);
3476 $this->_qill[$grouping][] = ts('Greeting %1 %2', array(1 => $qillop, 2 => $qillVal));
3477 $this->_where[$grouping][] = self::buildClause("contact_a.{$name}", $op, $value, 'String');
02adf2f9
BS
3478 }
3479
6a488035 3480 /**
100fef9d 3481 * Where / qill clause for email
6a488035 3482 *
c037736a 3483 * @param array $values
57f8e7f0 3484 * @param string $apiEntity
6a488035 3485 */
57f8e7f0 3486 protected function email(&$values, $apiEntity) {
6a488035 3487 list($name, $op, $value, $grouping, $wildcard) = $values;
11f8e194 3488 $this->_tables['civicrm_email'] = $this->_whereTables['civicrm_email'] = 1;
6a488035 3489
57f8e7f0 3490 // CRM-18147: for Contact's GET API, email fieldname got appended with its entity as in {$apiEntiy}_{$name}
3491 // so following code is use build whereClause for contact's primart email id
3492 if (!empty($apiEntity)) {
3493 $dataType = 'String';
3494 if ($name == 'email_id') {
3495 $dataType = 'Integer';
3496 $name = 'id';
3497 }
3498
3499 $this->_where[$grouping][] = self::buildClause('civicrm_email.is_primary', '=', 1, 'Integer');
3500 $this->_where[$grouping][] = self::buildClause("civicrm_email.$name", $op, $value, $dataType);
3501 return;
3502 }
3503
c245fd99 3504 $n = trim($value);
6a488035 3505 if ($n) {
6a488035
TO
3506 if (substr($n, 0, 1) == '"' &&
3507 substr($n, -1, 1) == '"'
3508 ) {
3509 $n = substr($n, 1, -1);
836eb043 3510 $value = CRM_Core_DAO::escapeString($n);
6a488035
TO
3511 $op = '=';
3512 }
3513 else {
0b38e8f1 3514 $value = self::getWildCardedValue($wildcard, $op, $n);
6a488035
TO
3515 }
3516 $this->_qill[$grouping][] = ts('Email') . " $op '$n'";
3517 $this->_where[$grouping][] = self::buildClause('civicrm_email.email', $op, $value, 'String');
3518 }
3519 else {
3520 $this->_qill[$grouping][] = ts('Email') . " $op ";
3521 $this->_where[$grouping][] = self::buildClause('civicrm_email.email', $op, NULL, 'String');
3522 }
6a488035
TO
3523 }
3524
3525 /**
100fef9d 3526 * Where / qill clause for phone number
6a488035 3527 *
c037736a 3528 * @param array $values
6a488035 3529 */
00be9182 3530 public function phone_numeric(&$values) {
6a488035 3531 list($name, $op, $value, $grouping, $wildcard) = $values;
a195fab2
BS
3532 // Strip non-numeric characters; allow wildcards
3533 $number = preg_replace('/[^\d%]/', '', $value);
6a488035 3534 if ($number) {
5c7dad85 3535 if (strpos($number, '%') === FALSE) {
a195fab2
BS
3536 $number = "%$number%";
3537 }
3538
6a488035 3539 $this->_qill[$grouping][] = ts('Phone number contains') . " $number";
a195fab2 3540 $this->_where[$grouping][] = self::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String');
6a488035
TO
3541 $this->_tables['civicrm_phone'] = $this->_whereTables['civicrm_phone'] = 1;
3542 }
3543 }
3544
3545 /**
100fef9d 3546 * Where / qill clause for phone type/location
6a488035 3547 *
c037736a 3548 * @param array $values
6a488035 3549 */
00be9182 3550 public function phone_option_group($values) {
6a488035 3551 list($name, $op, $value, $grouping, $wildcard) = $values;
cbf48754
AS
3552 $option = ($name == 'phone_phone_type_id' ? 'phone_type_id' : 'location_type_id');
3553 $options = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', $option);
6a488035
TO
3554 $optionName = $options[$value];
3555 $this->_qill[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ? ts('type') : ('location')) . " $op $optionName";
3556 $this->_where[$grouping][] = self::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer');
3557 $this->_tables['civicrm_phone'] = $this->_whereTables['civicrm_phone'] = 1;
3558 }
3559
3560 /**
c037736a 3561 * Where / qill clause for street_address.
6a488035 3562 *
c037736a 3563 * @param array $values
6a488035 3564 */
00be9182 3565 public function street_address(&$values) {
6d52bfe5 3566 list($name, $op, $value, $grouping) = $values;
6a488035
TO
3567
3568 if (!$op) {
3569 $op = 'LIKE';
3570 }
3571
3572 $n = trim($value);
3573
3574 if ($n) {
6a488035
TO
3575 if (strpos($value, '%') === FALSE) {
3576 // only add wild card if not there
3577 $value = "%{$value}%";
3578 }
3579 $op = 'LIKE';
fea8ae41 3580 $this->_where[$grouping][] = self::buildClause('civicrm_address.street_address', $op, $value, 'String');
6a488035
TO
3581 $this->_qill[$grouping][] = ts('Street') . " $op '$n'";
3582 }
3583 else {
3584 $this->_where[$grouping][] = self::buildClause('civicrm_address.street_address', $op, NULL, 'String');
3585 $this->_qill[$grouping][] = ts('Street') . " $op ";
3586 }
3587
3588 $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1;
3589 }
3590
3591 /**
c037736a 3592 * Where / qill clause for street_unit.
77b97be7 3593 *
c037736a 3594 * @param array $values
6a488035 3595 */
00be9182 3596 public function street_number(&$values) {
6a488035
TO
3597 list($name, $op, $value, $grouping, $wildcard) = $values;
3598
3599 if (!$op) {
3600 $op = '=';
3601 }
3602
3603 $n = trim($value);
3604
3605 if (strtolower($n) == 'odd') {
3606 $this->_where[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )";
3607 $this->_qill[$grouping][] = ts('Street Number is odd');
3608 }
3609 elseif (strtolower($n) == 'even') {
3610 $this->_where[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )";
3611 $this->_qill[$grouping][] = ts('Street Number is even');
3612 }
3613 else {
2fc64082 3614 $value = $n;
3615 $this->_where[$grouping][] = self::buildClause('civicrm_address.street_number', $op, $value, 'String');
6a488035
TO
3616 $this->_qill[$grouping][] = ts('Street Number') . " $op '$n'";
3617 }
3618
3619 $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1;
3620 }
3621
3622 /**
c037736a 3623 * Where / qill clause for sorting by character.
77b97be7 3624 *
c037736a 3625 * @param array $values
6a488035 3626 */
00be9182 3627 public function sortByCharacter(&$values) {
6a488035
TO
3628 list($name, $op, $value, $grouping, $wildcard) = $values;
3629
3630 $name = trim($value);
2fc64082 3631 $cond = " contact_a.sort_name LIKE '" . CRM_Core_DAO::escapeWildCardString($name) . "%'";
6a488035
TO
3632 $this->_where[$grouping][] = $cond;
3633 $this->_qill[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', array(1 => $name));
3634 }
3635
3636 /**
c037736a 3637 * Where / qill clause for including contact ids.
6a488035 3638 */
00be9182 3639 public function includeContactIDs() {
6a488035
TO
3640 if (!$this->_includeContactIds || empty($this->_params)) {
3641 return;
3642 }
3643
3644 $contactIds = array();
3645 foreach ($this->_params as $id => $values) {
3646 if (substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
3647 $contactIds[] = substr($values[0], CRM_Core_Form::CB_PREFIX_LEN);
3648 }
3649 }
b1128d0b 3650 CRM_Utils_Type::validateAll($contactIds, 'Positive');
6a488035
TO
3651 if (!empty($contactIds)) {
3652 $this->_where[0][] = " ( contact_a.id IN (" . implode(',', $contactIds) . " ) ) ";
3653 }
3654 }
3655
3656 /**
c037736a 3657 * Where / qill clause for postal code.
77b97be7 3658 *
c037736a 3659 * @param array $values
6a488035 3660 */
00be9182 3661 public function postalCode(&$values) {
6a488035 3662 // skip if the fields dont have anything to do with postal_code
a7488080 3663 if (empty($this->_fields['postal_code'])) {
6a488035
TO
3664 return;
3665 }
3666
3667 list($name, $op, $value, $grouping, $wildcard) = $values;
3668
3669 // Handle numeric postal code range searches properly by casting the column as numeric
3670 if (is_numeric($value)) {
abc78dd4 3671 $field = "IF (civicrm_address.postal_code REGEXP '^[0-9]{1,10}$', CAST(civicrm_address.postal_code AS UNSIGNED), 0)";
6a488035
TO
3672 $val = CRM_Utils_Type::escape($value, 'Integer');
3673 }
3674 else {
3675 $field = 'civicrm_address.postal_code';
715277e7 3676 // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string.
3677 if (!is_array($value)) {
3678 $val = CRM_Utils_Type::escape($value, 'String');
3679 }
3680 else {
3681 // Do we need to escape values here? I would expect buildClause does.
3682 $val = $value;
3683 }
6a488035
TO
3684 }
3685
3686 $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1;
3687
3688 if ($name == 'postal_code') {
3689 $this->_where[$grouping][] = self::buildClause($field, $op, $val, 'String');
3690 $this->_qill[$grouping][] = ts('Postal code') . " {$op} {$value}";
3691 }
3692 elseif ($name == 'postal_code_low') {
3693 $this->_where[$grouping][] = " ( $field >= '$val' ) ";
3694 $this->_qill[$grouping][] = ts('Postal code greater than or equal to \'%1\'', array(1 => $value));
3695 }
3696 elseif ($name == 'postal_code_high') {
3697 $this->_where[$grouping][] = " ( $field <= '$val' ) ";
3698 $this->_qill[$grouping][] = ts('Postal code less than or equal to \'%1\'', array(1 => $value));
3699 }
3700 }
3701
3702 /**
c037736a 3703 * Where / qill clause for location type.
6a488035 3704 *
c037736a 3705 * @param array $values
77b97be7
EM
3706 * @param null $status
3707 *
c037736a 3708 * @return string
6a488035 3709 */
00be9182 3710 public function locationType(&$values, $status = NULL) {
6a488035
TO
3711 list($name, $op, $value, $grouping, $wildcard) = $values;
3712
3713 if (is_array($value)) {
d8a8bb0b 3714 $this->_where[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')';
6a488035
TO
3715 $this->_tables['civicrm_address'] = 1;
3716 $this->_whereTables['civicrm_address'] = 1;
3717
b2b0530a 3718 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035 3719 $names = array();
d8a8bb0b 3720 foreach ($value as $id) {
6a488035
TO
3721 $names[] = $locationType[$id];
3722 }
3723
3724 $this->_primaryLocation = FALSE;
3725
3726 if (!$status) {
3727 $this->_qill[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3728 }
3729 else {
3730 return implode(' ' . ts('or') . ' ', $names);
3731 }
3732 }
3733 }
3734
86538308
EM
3735 /**
3736 * @param $values
3737 * @param bool $fromStateProvince
3738 *
59f4c9ee 3739 * @return array|NULL
86538308 3740 */
00be9182 3741 public function country(&$values, $fromStateProvince = TRUE) {
6a488035
TO
3742 list($name, $op, $value, $grouping, $wildcard) = $values;
3743
3744 if (!$fromStateProvince) {
3745 $stateValues = $this->getWhereValues('state_province', $grouping);
3746 if (!empty($stateValues)) {
3747 // return back to caller if there are state province values
3748 // since that handles this case
59f4c9ee 3749 return NULL;
6a488035
TO
3750 }
3751 }
3752
3753 $countryClause = $countryQill = NULL;
a220b357 3754 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) || ($values && !empty($value))) {
d9ab802d
PJ
3755 $this->_tables['civicrm_address'] = 1;
3756 $this->_whereTables['civicrm_address'] = 1;
6a488035 3757
836f1f3f 3758 $countryClause = self::buildClause('civicrm_address.country_id', $op, $value, 'Positive');
3759 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, 'country_id', $value, $op);
3760 $countryQill = ts("%1 %2 %3", array(1 => 'Country', 2 => $qillop, 3 => $qillVal));
6a488035
TO
3761
3762 if (!$fromStateProvince) {
3763 $this->_where[$grouping][] = $countryClause;
3764 $this->_qill[$grouping][] = $countryQill;
3765 }
3766 }
3767
3768 if ($fromStateProvince) {
3769 if (!empty($countryClause)) {
3770 return array(
3771 $countryClause,
3772 " ...AND... " . $countryQill,
3773 );
3774 }
3775 else {
3776 return array(NULL, NULL);
3777 }
3778 }
3779 }
3780
3781 /**
c037736a 3782 * Where / qill clause for county (if present).
6a488035 3783 *
c037736a 3784 * @param array $values
77b97be7
EM
3785 * @param null $status
3786 *
c037736a 3787 * @return string
6a488035 3788 */
5c7dad85 3789 public function county(&$values, $status = NULL) {
6a488035
TO
3790 list($name, $op, $value, $grouping, $wildcard) = $values;
3791
5c7dad85 3792 if (!is_array($value)) {
6a488035
TO
3793 // force the county to be an array
3794 $value = array($value);
3795 }
3796
3797 // check if the values are ids OR names of the counties
3798 $inputFormat = 'id';
3799 foreach ($value as $v) {
3800 if (!is_numeric($v)) {
3801 $inputFormat = 'name';
3802 break;
3803 }
3804 }
3805 $names = array();
d9ab802d
PJ
3806 if ($op == '=') {
3807 $op = 'IN';
3808 }
4c9b6178 3809 elseif ($op == '!=') {
d9ab802d
PJ
3810 $op = 'NOT IN';
3811 }
3812 else {
3813 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3814 $op = str_replace('EMPTY', 'NULL', $op);
3815 }
3816
5c7dad85 3817 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
d9ab802d
PJ
3818 $clause = "civicrm_address.county_id $op";
3819 }
3820 elseif ($inputFormat == 'id') {
3821 $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')';
6a488035
TO
3822
3823 $county = CRM_Core_PseudoConstant::county();
3824 foreach ($value as $id) {
3825 $names[] = CRM_Utils_Array::value($id, $county);
3826 }
3827 }
3828 else {
3829 $inputClause = array();
d9ab802d 3830 $county = CRM_Core_PseudoConstant::county();
6a488035
TO
3831 foreach ($value as $name) {
3832 $name = trim($name);
d9ab802d 3833 $inputClause[] = CRM_Utils_Array::key($name, $county);
6a488035 3834 }
d9ab802d 3835 $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')';
6a488035
TO
3836 $names = $value;
3837 }
d9ab802d
PJ
3838 $this->_tables['civicrm_address'] = 1;
3839 $this->_whereTables['civicrm_address'] = 1;
6a488035
TO
3840
3841 $this->_where[$grouping][] = $clause;
d9ab802d 3842 if (!$status) {
6a488035 3843 $this->_qill[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names);
5c7dad85
TO
3844 }
3845 else {
6a488035
TO
3846 return implode(' ' . ts('or') . ' ', $names);
3847 }
3848 }
3849
3850 /**
c037736a 3851 * Where / qill clause for state/province AND country (if present).
6a488035 3852 *
c037736a 3853 * @param array $values
77b97be7
EM
3854 * @param null $status
3855 *
c037736a 3856 * @return string
6a488035 3857 */
00be9182 3858 public function stateProvince(&$values, $status = NULL) {
6a488035
TO
3859 list($name, $op, $value, $grouping, $wildcard) = $values;
3860
6bd09f41 3861 $stateClause = self::buildClause('civicrm_address.state_province_id', $op, $value, 'Positive');
d9ab802d
PJ
3862 $this->_tables['civicrm_address'] = 1;
3863 $this->_whereTables['civicrm_address'] = 1;
6a488035
TO
3864
3865 $countryValues = $this->getWhereValues('country', $grouping);
3866 list($countryClause, $countryQill) = $this->country($countryValues, TRUE);
6a488035
TO
3867 if ($countryClause) {
3868 $clause = "( $stateClause AND $countryClause )";
3869 }
3870 else {
3871 $clause = $stateClause;
3872 }
3873
3874 $this->_where[$grouping][] = $clause;
6bd09f41 3875 list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
6a488035 3876 if (!$status) {
6bd09f41 3877 $this->_qill[$grouping][] = ts("State/Province %1 %2 %3", array(1 => $qillop, 2 => $qillVal, 3 => $countryQill));
6a488035
TO
3878 }
3879 else {
6bd09f41 3880 return implode(' ' . ts('or') . ' ', $qillVal) . $countryQill;
6a488035
TO
3881 }
3882 }
3883
3884 /**
c037736a 3885 * Where / qill clause for change log.
77b97be7 3886 *
c037736a 3887 * @param array $values
6a488035 3888 */
00be9182 3889 public function changeLog(&$values) {
6a488035
TO
3890 list($name, $op, $value, $grouping, $wildcard) = $values;
3891
3892 $targetName = $this->getWhereValues('changed_by', $grouping);
3893 if (!$targetName) {
3894 return;
3895 }
3896
3897 $name = trim($targetName[2]);
2fc64082 3898 $name = CRM_Core_DAO::escapeString($name);
6a488035
TO
3899 $name = $targetName[4] ? "%$name%" : $name;
3900 $this->_where[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
3901 $this->_tables['civicrm_log'] = $this->_whereTables['civicrm_log'] = 1;
ab090b59 3902 $this->_qill[$grouping][] = ts('Modified By') . " $name";
6a488035
TO
3903 }
3904
86538308
EM
3905 /**
3906 * @param $values
3907 */
00be9182 3908 public function modifiedDates($values) {
6a488035 3909 $this->_useDistinct = TRUE;
9e9b8fdb
DL
3910
3911 // CRM-11281, default to added date if not set
228917cd 3912 $fieldTitle = ts('Added Date');
06103b23 3913 $fieldName = 'created_date';
6a488035
TO
3914 foreach (array_keys($this->_params) as $id) {
3915 if ($this->_params[$id][0] == 'log_date') {
228917cd
DL
3916 if ($this->_params[$id][2] == 2) {
3917 $fieldTitle = ts('Modified Date');
06103b23 3918 $fieldName = 'modified_date';
6a488035
TO
3919 }
3920 }
3921 }
3922
06103b23 3923 $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle);
3924
3925 self::$_openedPanes[ts('Change Log')] = TRUE;
6a488035
TO
3926 }
3927
86538308
EM
3928 /**
3929 * @param $values
3930 */
00be9182 3931 public function demographics(&$values) {
6a488035
TO
3932 list($name, $op, $value, $grouping, $wildcard) = $values;
3933
c4a7c967 3934 if (($name == 'age_low') || ($name == 'age_high')) {
3935 $this->ageRangeQueryBuilder($values,
3936 'contact_a', 'age', 'birth_date', ts('Age')
3937 );
3938 }
3939 elseif (($name == 'birth_date_low') || ($name == 'birth_date_high')) {
6a488035
TO
3940
3941 $this->dateQueryBuilder($values,
3942 'contact_a', 'birth_date', 'birth_date', ts('Birth Date')
3943 );
3944 }
3945 elseif (($name == 'deceased_date_low') || ($name == 'deceased_date_high')) {
3946
3947 $this->dateQueryBuilder($values,
3948 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date')
3949 );
3950 }
3951
3952 self::$_openedPanes[ts('Demographics')] = TRUE;
3953 }
3954
86538308
EM
3955 /**
3956 * @param $values
3957 */
00be9182 3958 public function privacy(&$values) {
6d52bfe5 3959 list($name, $op, $value, $grouping) = $values;
6a488035
TO
3960 //fixed for profile search listing CRM-4633
3961 if (strpbrk($value, "[")) {
3962 $value = "'{$value}'";
3963 $op = "!{$op}";
3964 $this->_where[$grouping][] = "contact_a.{$name} $op $value";
3965 }
3966 else {
3967 $this->_where[$grouping][] = "contact_a.{$name} $op $value";
3968 }
3969 $field = CRM_Utils_Array::value($name, $this->_fields);
9b183452 3970 $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
6a488035
TO
3971 $title = $field ? $field['title'] : $name;
3972 $this->_qill[$grouping][] = "$title $op $value";
3973 }
3974
86538308
EM
3975 /**
3976 * @param $values
3977 */
00be9182 3978 public function privacyOptions($values) {
6a488035
TO
3979 list($name, $op, $value, $grouping, $wildcard) = $values;
3980
9fd71915
CW
3981 if (empty($value) || !is_array($value)) {
3982 return;
6a488035
TO
3983 }
3984
3985 // get the operator and toggle values
3986 $opValues = $this->getWhereValues('privacy_operator', $grouping);
3987 $operator = 'OR';
3988 if ($opValues &&
3989 strtolower($opValues[2] == 'AND')
3990 ) {
2fc64082 3991 // @todo this line is logially unreachable
6a488035
TO
3992 $operator = 'AND';
3993 }
3994
3995 $toggleValues = $this->getWhereValues('privacy_toggle', $grouping);
e10c7983 3996 $compareOP = '!';
6a488035
TO
3997 if ($toggleValues &&
3998 $toggleValues[2] == 2
3999 ) {
e10c7983 4000 $compareOP = '';
6a488035
TO
4001 }
4002
4003 $clauses = array();
4004 $qill = array();
4005 foreach ($value as $dontCare => $pOption) {
e10c7983 4006 $clauses[] = " ( contact_a.{$pOption} = 1 ) ";
6a488035
TO
4007 $field = CRM_Utils_Array::value($pOption, $this->_fields);
4008 $title = $field ? $field['title'] : $pOption;
e10c7983 4009 $qill[] = " $title = 1 ";
6a488035
TO
4010 }
4011
e10c7983
JL
4012 $this->_where[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )';
4013 $this->_qill[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )';
6a488035
TO
4014 }
4015
86538308
EM
4016 /**
4017 * @param $values
4018 */
00be9182 4019 public function preferredCommunication(&$values) {
6a488035
TO
4020 list($name, $op, $value, $grouping, $wildcard) = $values;
4021
c112a6f9 4022 if (!is_array($value)) {
7cc09daf 4023 $value = str_replace(array('(', ')'), '', explode(",", $value));
6a488035 4024 }
7cc09daf 4025 elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
4026 $op = key($value);
4027 $value = $value[$op];
6a488035 4028 }
05d214de 4029 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
6a488035 4030
05d214de 4031 if (self::caseImportant($op)) {
4032 $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value);
4033 $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
4034 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
4b20f7df 4035 }
6a488035 4036
05d214de 4037 $this->_where[$grouping][] = self::buildClause("contact_a.preferred_communication_method", $op, $value);
4038 $this->_qill[$grouping][] = ts('Preferred Communication Method %1 %2', array(1 => $qillop, 2 => $qillVal));
6a488035
TO
4039 }
4040
6a488035 4041 /**
c037736a 4042 * Where / qill clause for relationship.
77b97be7 4043 *
c037736a 4044 * @param array $values
6a488035 4045 */
00be9182 4046 public function relationship(&$values) {
6a488035 4047 list($name, $op, $value, $grouping, $wildcard) = $values;
5c7dad85
TO
4048 if ($this->_relationshipValuesAdded) {
4049 return;
ad682134 4050 }
6a488035 4051 // also get values array for relation_target_name
2fef460a 4052 // for relationship search we always do wildcard
b3342109 4053 $relationType = $this->getWhereValues('relation_type_id', $grouping);
6a488035
TO
4054 $targetName = $this->getWhereValues('relation_target_name', $grouping);
4055 $relStatus = $this->getWhereValues('relation_status', $grouping);
4056 $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
b9cb427a 4057
6a488035
TO
4058 $nameClause = $name = NULL;
4059 if ($targetName) {
4060 $name = trim($targetName[2]);
4061 if (substr($name, 0, 1) == '"' &&
4062 substr($name, -1, 1) == '"'
4063 ) {
4064 $name = substr($name, 1, -1);
dd2db5e5 4065 $name = CRM_Core_DAO::escapeString($name);
6a488035
TO
4066 $nameClause = "= '$name'";
4067 }
4068 else {
dd2db5e5 4069 $name = CRM_Core_DAO::escapeString($name);
6a488035
TO
4070 $nameClause = "LIKE '%{$name}%'";
4071 }
4072 }
4073
dd2db5e5 4074 $relTypes = $relTypesIds = array();
5c7dad85 4075 if (!empty($relationType)) {
026242fc
SL
4076 $relationType[2] = (array) $relationType[2];
4077 foreach ($relationType[2] as $relType) {
4078 $rel = explode('_', $relType);
c0184a48 4079 self::$_relType = $rel[1];
026242fc 4080 $params = array('id' => $rel[0]);
026242fc 4081 $typeValues = array();
5ad36be5
SL
4082 $rTypeValue = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues);
4083 if (!empty($rTypeValue)) {
dd2db5e5 4084 if ($rTypeValue->name_a_b == $rTypeValue->name_b_a) {
4085 // if we don't know which end of the relationship we are dealing with we'll create a temp table
4086 self::$_relType = 'reciprocal';
4087 }
5ad36be5
SL
4088 $relTypesIds[] = $rel[0];
4089 $relTypes[] = $relType;
4090 }
026242fc
SL
4091 }
4092 }
dd2db5e5 4093
7c96365b 4094 // if we are creating a temp table we build our own where for the relationship table
085e6226 4095 $relationshipTempTable = NULL;
72a2eeab 4096 if (self::$_relType == 'reciprocal') {
7c96365b 4097 $where = array();
59f4c9ee 4098 self::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO::createTempTableName('civicrm_rel');
11a5aa5d 4099 if ($nameClause) {
7c96365b 4100 $where[$grouping][] = " sort_name $nameClause ";
4101 }
72a2eeab
AS
4102 $groupJoinTable = "civicrm_relationship";
4103 $groupJoinColumn = "contact_id_alt";
6a488035 4104 }
7c96365b 4105 else {
4106 $where = &$this->_where;
4107 if ($nameClause) {
4108 $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )";
4109 }
72a2eeab
AS
4110 $groupJoinTable = "contact_b";
4111 $groupJoinColumn = "id";
7c96365b 4112 }
1de4945d 4113 $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
6a488035 4114 if ($nameClause || !$targetGroup) {
5c7dad85 4115 if (!empty($relationType)) {
026242fc 4116 $relQill = '';
5ad36be5 4117 foreach ($relTypes as $rel) {
026242fc
SL
4118 if (!empty($relQill)) {
4119 $relQill .= ' OR ';
4120 }
4121 $relQill .= $allRelationshipType[$rel];
4122 }
4123 $this->_qill[$grouping][] = 'Relationship Type(s) ' . $relQill . " $name";
5c7dad85
TO
4124 }
4125 else {
2b0c31bc
DG
4126 $this->_qill[$grouping][] = $name;
4127 }
6a488035
TO
4128 }
4129
6a488035
TO
4130 //check to see if the target contact is in specified group
4131 if ($targetGroup) {
4132 //add contacts from static groups
59f4c9ee 4133 $this->_tables['civicrm_relationship_group_contact'] = $this->_whereTables['civicrm_relationship_group_contact']
72a2eeab 4134 = " LEFT JOIN civicrm_group_contact civicrm_relationship_group_contact ON civicrm_relationship_group_contact.contact_id = {$groupJoinTable}.{$groupJoinColumn} AND civicrm_relationship_group_contact.status = 'Added'";
59f4c9ee 4135 $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" .
5c7dad85 4136 implode(",", $targetGroup[2]) . ") ) ";
6a488035
TO
4137
4138 //add contacts from saved searches
72a2eeab 4139 $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", $groupJoinTable, $op, $groupJoinColumn);
6a488035
TO
4140
4141 //set the group where clause
4142 if ($ssWhere) {
4143 $groupWhere[] = "( " . $ssWhere . " )";
4144 }
4145 $this->_where[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
4146
4147 //Get the names of the target groups for the qill
085e6226 4148 $groupNames = CRM_Core_PseudoConstant::group();
6a488035
TO
4149 $qillNames = array();
4150 foreach ($targetGroup[2] as $groupId) {
4151 if (array_key_exists($groupId, $groupNames)) {
4152 $qillNames[] = $groupNames[$groupId];
4153 }
4154 }
5c7dad85 4155 if (!empty($relationType)) {
026242fc 4156 $relQill = '';
5ad36be5 4157 foreach ($relTypes as $rel) {
026242fc
SL
4158 if (!empty($relQill)) {
4159 $relQill .= ' OR ';
4160 }
72a2eeab 4161 $relQill .= CRM_Utils_Array::value($rel, $allRelationshipType);
026242fc
SL
4162 }
4163 $this->_qill[$grouping][] = 'Relationship Type(s) ' . $relQill . " ( " . implode(", ", $qillNames) . " )";
5c7dad85
TO
4164 }
4165 else {
2b0c31bc
DG
4166 $this->_qill[$grouping][] = implode(", ", $qillNames);
4167 }
6a488035
TO
4168 }
4169
12ba37a0
CW
4170 // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
4171 $today = date('Ymd');
6a488035
TO
4172 //check for active, inactive and all relation status
4173 if ($relStatus[2] == 0) {
7c96365b 4174 $where[$grouping][] = "(
6a488035 4175civicrm_relationship.is_active = 1 AND
12ba37a0
CW
4176( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
4177( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
6a488035
TO
4178)";
4179 $this->_qill[$grouping][] = ts('Relationship - Active and Current');
4180 }
4181 elseif ($relStatus[2] == 1) {
7c96365b 4182 $where[$grouping][] = "(
6a488035 4183civicrm_relationship.is_active = 0 OR
12ba37a0
CW
4184civicrm_relationship.end_date < {$today} OR
4185civicrm_relationship.start_date > {$today}
6a488035
TO
4186)";
4187 $this->_qill[$grouping][] = ts('Relationship - Inactive or not Current');
4188 }
4189
d64e31ab 4190 $onlyDeleted = 0;
4191 if (in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params)) {
4192 $onlyDeleted = 1;
4193 }
4194 $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
4195
f871c3a9 4196 $this->addRelationshipPermissionClauses($grouping, $where);
7c96365b 4197 $this->addRelationshipDateClauses($grouping, $where);
eea5db81 4198 $this->addRelationshipActivePeriodClauses($grouping, $where);
026242fc 4199 if (!empty($relTypes)) {
5ad36be5 4200 $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')';
6a488035 4201 }
6a488035
TO
4202 $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
4203 $this->_useDistinct = TRUE;
ad682134 4204 $this->_relationshipValuesAdded = TRUE;
7c96365b 4205 // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
4206 // join on that,
2b0c31bc
DG
4207 if ($relationshipTempTable) {
4208 $whereClause = '';
5c7dad85 4209 if (!empty($where[$grouping])) {
2b0c31bc 4210 $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
792750a5 4211 $whereClause = str_replace('contact_b', 'c', $whereClause);
2b0c31bc 4212 }
7c96365b 4213 $sql = "
4214 CREATE TEMPORARY TABLE {$relationshipTempTable}
08fa90dc
AS
4215 (
4216 `contact_id` int(10) unsigned NOT NULL DEFAULT '0',
4217 `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0',
4218 KEY `contact_id` (`contact_id`),
4219 KEY `contact_id_alt` (`contact_id_alt`)
4220 )
72a2eeab 4221 (SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id
7c96365b 4222 FROM civicrm_relationship
4223 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
4224 $whereClause )
4225 UNION
72a2eeab 4226 (SELECT contact_id_a as contact_id, contact_id_b as contact_id_alt, civicrm_relationship.id
7c96365b 4227 FROM civicrm_relationship
4228 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id
4229 $whereClause )
4230 ";
4231 CRM_Core_DAO::executeQuery($sql);
4232 }
6a488035 4233 }
5c7dad85 4234
9c9908e9 4235 /**
4236 * Add relationship permission criteria to where clause.
4237 *
4238 * @param string $grouping
4239 * @param array $where Array to add "where" criteria to, in case you are generating a temp table.
4240 * Not the main query.
4241 */
f871c3a9
AS
4242 public function addRelationshipPermissionClauses($grouping, &$where) {
4243 $relPermission = $this->getWhereValues('relation_permission', $grouping);
4244 if ($relPermission) {
f85f95ba
NM
4245 if (!is_array($relPermission[2])) {
4246 // this form value was scalar in previous versions of Civi
4247 $relPermission[2] = array($relPermission[2]);
4248 }
f871c3a9
AS
4249 $where[$grouping][] = "(civicrm_relationship.is_permission_a_b IN (" . implode(",", $relPermission[2]) . "))";
4250
4251 $allRelationshipPermissions = CRM_Contact_BAO_Relationship::buildOptions('is_permission_a_b');
9c9908e9 4252
4253 $relPermNames = array_intersect_key($allRelationshipPermissions, array_flip($relPermission[2]));
4254 $this->_qill[$grouping][] = ts('Permissioned Relationships') . ' - ' . implode(' OR ', $relPermNames);
f871c3a9
AS
4255 }
4256 }
4257
5c7dad85
TO
4258 /**
4259 * Add start & end date criteria in
4260 * @param string $grouping
4261 * @param array $where
4262 * = array to add where clauses to, in case you are generating a temp table.
4263 * not the main query.
4264 */
4265 public function addRelationshipDateClauses($grouping, &$where) {
ad682134 4266 $dateValues = array();
4267 $dateTypes = array(
5c7dad85
TO
4268 'start_date',
4269 'end_date',
ad682134 4270 );
6a488035 4271
5c7dad85
TO
4272 foreach ($dateTypes as $dateField) {
4273 $dateValueLow = $this->getWhereValues('relation_' . $dateField . '_low', $grouping);
4274 $dateValueHigh = $this->getWhereValues('relation_' . $dateField . '_high', $grouping);
4275 if (!empty($dateValueLow)) {
ad682134 4276 $date = date('Ymd', strtotime($dateValueLow[2]));
7c96365b 4277 $where[$grouping][] = "civicrm_relationship.$dateField >= $date";
c8e625fc 4278 $this->_qill[$grouping][] = ($dateField == 'end_date' ? ts('Relationship Ended on or After') : ts('Relationship Recorded Start Date On or After')) . " " . CRM_Utils_Date::customFormat($date);
ad682134 4279 }
5c7dad85 4280 if (!empty($dateValueHigh)) {
ad682134 4281 $date = date('Ymd', strtotime($dateValueHigh[2]));
7c96365b 4282 $where[$grouping][] = "civicrm_relationship.$dateField <= $date";
c8e625fc 4283 $this->_qill[$grouping][] = ($dateField == 'end_date' ? ts('Relationship Ended on or Before') : ts('Relationship Recorded Start Date On or Before')) . " " . CRM_Utils_Date::customFormat($date);
ad682134 4284 }
4285 }
4286 }
77b97be7 4287
eea5db81 4288 /**
4289 * Add start & end active period criteria in
4290 * @param string $grouping
4291 * @param array $where
4292 * = array to add where clauses to, in case you are generating a temp table.
4293 * not the main query.
4294 */
4295 public function addRelationshipActivePeriodClauses($grouping, &$where) {
4296 $dateValues = array();
4297 $dateField = 'active_period_date';
4298
4299 $dateValueLow = $this->getWhereValues('relation_active_period_date_low', $grouping);
4300 $dateValueHigh = $this->getWhereValues('relation_active_period_date_high', $grouping);
02633eb0 4301 $dateValueLowFormated = $dateValueHighFormated = NULL;
eea5db81 4302 if (!empty($dateValueLow) && !empty($dateValueHigh)) {
4303 $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2]));
4304 $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2]));
eea5db81 4305 $this->_qill[$grouping][] = (ts('Relationship was active between')) . " " . CRM_Utils_Date::customFormat($dateValueLowFormated) . " and " . CRM_Utils_Date::customFormat($dateValueHighFormated);
4306 }
4307 elseif (!empty($dateValueLow)) {
4308 $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2]));
eea5db81 4309 $this->_qill[$grouping][] = (ts('Relationship was active after')) . " " . CRM_Utils_Date::customFormat($dateValueLowFormated);
4310 }
4311 elseif (!empty($dateValueHigh)) {
4312 $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2]));
eea5db81 4313 $this->_qill[$grouping][] = (ts('Relationship was active before')) . " " . CRM_Utils_Date::customFormat($dateValueHighFormated);
4314 }
02633eb0
FBB
4315
4316 if ($activePeriodClauses = self::getRelationshipActivePeriodClauses($dateValueLowFormated, $dateValueHighFormated, TRUE)) {
4317 $where[$grouping][] = $activePeriodClauses;
4318 }
eea5db81 4319 }
4320
4321 /**
4322 * Get start & end active period criteria
4323 */
4324 public static function getRelationshipActivePeriodClauses($from, $to, $forceTableName) {
4325 $tableName = $forceTableName ? 'civicrm_relationship.' : '';
4326 if (!is_null($from) && !is_null($to)) {
4327 return '(((' . $tableName . 'start_date >= ' . $from . ' AND ' . $tableName . 'start_date <= ' . $to . ') OR
4328 (' . $tableName . 'end_date >= ' . $from . ' AND ' . $tableName . 'end_date <= ' . $to . ') OR
4329 (' . $tableName . 'start_date <= ' . $from . ' AND ' . $tableName . 'end_date >= ' . $to . ' )) OR
4330 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4331 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . ') OR
4332 (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))';
4333 }
4334 elseif (!is_null($from)) {
4335 return '((' . $tableName . 'start_date >= ' . $from . ') OR
4336 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4337 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . '))';
4338 }
4339 elseif (!is_null($to)) {
4340 return '((' . $tableName . 'start_date <= ' . $to . ') OR
4341 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4342 (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))';
4343 }
4344 }
4345
6a488035 4346 /**
fe482240 4347 * Default set of return properties.
6a488035 4348 *
77b97be7
EM
4349 * @param int $mode
4350 *
a6c01b45
CW
4351 * @return array
4352 * derault return properties
6a488035 4353 */
00be9182 4354 public static function &defaultReturnProperties($mode = 1) {
6a488035
TO
4355 if (!isset(self::$_defaultReturnProperties)) {
4356 self::$_defaultReturnProperties = array();
4357 }
4358
4359 if (!isset(self::$_defaultReturnProperties[$mode])) {
4360 // add activity return properties
4361 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
4362 self::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query::defaultReturnProperties($mode, FALSE);
4363 }
4364 else {
4365 self::$_defaultReturnProperties[$mode] = CRM_Core_Component::defaultReturnProperties($mode, FALSE);
4366 }
4367
4368 if (empty(self::$_defaultReturnProperties[$mode])) {
4369 self::$_defaultReturnProperties[$mode] = array(
4370 'home_URL' => 1,
4371 'image_URL' => 1,
4372 'legal_identifier' => 1,
4373 'external_identifier' => 1,
4374 'contact_type' => 1,
4375 'contact_sub_type' => 1,
4376 'sort_name' => 1,
4377 'display_name' => 1,
4378 'preferred_mail_format' => 1,
4379 'nick_name' => 1,
4380 'first_name' => 1,
4381 'middle_name' => 1,
4382 'last_name' => 1,
04ffef8d
CW
4383 'prefix_id' => 1,
4384 'suffix_id' => 1,
e171748b 4385 'formal_title' => 1,
aa62b355 4386 'communication_style_id' => 1,
6a488035 4387 'birth_date' => 1,
04ffef8d 4388 'gender_id' => 1,
6a488035
TO
4389 'street_address' => 1,
4390 'supplemental_address_1' => 1,
4391 'supplemental_address_2' => 1,
207f62c6 4392 'supplemental_address_3' => 1,
6a488035
TO
4393 'city' => 1,
4394 'postal_code' => 1,
4395 'postal_code_suffix' => 1,
4396 'state_province' => 1,
4397 'country' => 1,
4398 'world_region' => 1,
4399 'geo_code_1' => 1,
4400 'geo_code_2' => 1,
4401 'email' => 1,
4402 'on_hold' => 1,
4403 'phone' => 1,
4404 'im' => 1,
4405 'household_name' => 1,
4406 'organization_name' => 1,
4407 'deceased_date' => 1,
4408 'is_deceased' => 1,
4409 'job_title' => 1,
4410 'legal_name' => 1,
4411 'sic_code' => 1,
4412 'current_employer' => 1,
4413 // FIXME: should we use defaultHierReturnProperties() for the below?
4414 'do_not_email' => 1,
4415 'do_not_mail' => 1,
4416 'do_not_sms' => 1,
4417 'do_not_phone' => 1,
4418 'do_not_trade' => 1,
4419 'is_opt_out' => 1,
4420 'contact_is_deleted' => 1,
b725488f
CW
4421 'preferred_communication_method' => 1,
4422 'preferred_language' => 1,
6a488035
TO
4423 );
4424 }
4425 }
4426 return self::$_defaultReturnProperties[$mode];
4427 }
4428
4429 /**
fe482240 4430 * Get primary condition for a sql clause.
6a488035
TO
4431 *
4432 * @param int $value
4433 *
fffe9ee1 4434 * @return string|NULL
6a488035 4435 */
00be9182 4436 public static function getPrimaryCondition($value) {
6a488035
TO
4437 if (is_numeric($value)) {
4438 $value = (int ) $value;
4439 return ($value == 1) ? 'is_primary = 1' : 'is_primary = 0';
4440 }
4441 return NULL;
4442 }
4443
4444 /**
fe482240 4445 * Wrapper for a simple search query.
6a488035
TO
4446 *
4447 * @param array $params
4448 * @param array $returnProperties
4e6072d5 4449 * @param bool $count
6a488035 4450 *
4e6072d5 4451 * @return string
6a488035 4452 */
00be9182 4453 public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) {
6a488035
TO
4454 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
4455 list($select, $from, $where, $having) = $query->query();
3875e6b6 4456 $groupBy = ($query->_useGroupBy) ? 'GROUP BY contact_a.id' : '';
6a488035 4457
c9c5c58d
AS
4458 $query = "$select $from $where $groupBy $having";
4459 return $query;
6a488035
TO
4460 }
4461
4462 /**
4463 * These are stub comments as this function needs more explanation - particularly in terms of how it
4464 * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.
4465 *
4466 * This function was originally written as a wrapper for the api query but is called from multiple places
4467 * in the core code directly so the name is misleading. This function does not use the searchQuery function
4468 * but it is unclear as to whehter that is historical or there is a reason
4469 * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function
4470 *
837c4660
E
4471 * @param array $params
4472 * @param array $returnProperties
4473 * @param null $fields
6a488035 4474 * @param string $sort
837c4660
E
4475 * @param int $offset
4476 * @param int $row_count
77c5b619
TO
4477 * @param bool $smartGroupCache
4478 * ?? update smart group cache?.
4479 * @param bool $count
4480 * Return count obnly.
4481 * @param bool $skipPermissions
4482 * Should permissions be ignored or should the logged in user's permissions be applied.
9ae25b56 4483 * @param int $mode
4484 * This basically correlates to the component.
8f165fa5 4485 * @param string $apiEntity
4486 * The api entity being called.
4487 * This sort-of duplicates $mode in a confusing way. Probably not by design.
837c4660 4488 *
0606198b 4489 * @param bool|null $primaryLocationOnly
b725488f 4490 * @return array
6a488035 4491 */
59f4c9ee 4492 public static function apiQuery(
6a488035
TO
4493 $params = NULL,
4494 $returnProperties = NULL,
4495 $fields = NULL,
4496 $sort = NULL,
4497 $offset = 0,
4498 $row_count = 25,
4499 $smartGroupCache = TRUE,
4500 $count = FALSE,
9ae25b56 4501 $skipPermissions = TRUE,
3123273f 4502 $mode = CRM_Contact_BAO_Query::MODE_CONTACTS,
0606198b 4503 $apiEntity = NULL,
4504 $primaryLocationOnly = NULL
6a488035
TO
4505 ) {
4506
4507 $query = new CRM_Contact_BAO_Query(
4508 $params, $returnProperties,
9ae25b56 4509 NULL, TRUE, FALSE, $mode,
6a488035 4510 $skipPermissions,
66670e4d 4511 TRUE, $smartGroupCache,
4512 NULL, 'AND',
0606198b 4513 $apiEntity, $primaryLocationOnly
6a488035
TO
4514 );
4515
5c7dad85
TO
4516 //this should add a check for view deleted if permissions are enabled
4517 if ($skipPermissions) {
4518 $query->_skipDeleteClause = TRUE;
4519 }
6a488035 4520 $query->generatePermissionClause(FALSE, $count);
d9ab802d
PJ
4521
4522 // note : this modifies _fromClause and _simpleFromClause
4523 $query->includePseudoFieldsJoin($sort);
4524
6a488035
TO
4525 list($select, $from, $where, $having) = $query->query($count);
4526
4527 $options = $query->_options;
5c7dad85 4528 if (!empty($query->_permissionWhereClause)) {
6a488035
TO
4529 if (empty($where)) {
4530 $where = "WHERE $query->_permissionWhereClause";
4531 }
4532 else {
4533 $where = "$where AND $query->_permissionWhereClause";
4534 }
4535 }
4536
4537 $sql = "$select $from $where $having";
4538
783144b0 4539 // add group by only when API action is not getcount
4540 // otherwise query fetches incorrect count
4541 if ($query->_useGroupBy && !$count) {
e5cceea5 4542 $sql .= self::getGroupByFromSelectColumns($query->_select, 'contact_a.id');
6a488035
TO
4543 }
4544 if (!empty($sort)) {
bd9a3dd5 4545 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
4546 $sql .= " ORDER BY $sort ";
4547 }
4548 if ($row_count > 0 && $offset >= 0) {
bf00d1b6 4549 $offset = CRM_Utils_Type::escape($offset, 'Int');
9ae25b56 4550 $row_count = CRM_Utils_Type::escape($row_count, 'Int');
6a488035
TO
4551 $sql .= " LIMIT $offset, $row_count ";
4552 }
4553
4554 $dao = CRM_Core_DAO::executeQuery($sql);
4555
9ae25b56 4556 // @todo derive this from the component class rather than hard-code two options.
4557 $entityIDField = ($mode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) ? 'contribution_id' : 'contact_id';
4558
6a488035
TO
4559 $values = array();
4560 while ($dao->fetch()) {
4561 if ($count) {
4562 $noRows = $dao->rowCount;
5c7dad85 4563 return array($noRows, NULL);
6a488035 4564 }
d9ab802d 4565 $val = $query->store($dao);
86ab13b7 4566 $convertedVals = $query->convertToPseudoNames($dao, TRUE, TRUE);
d9ab802d
PJ
4567
4568 if (!empty($convertedVals)) {
e3571aca 4569 $val = array_replace_recursive($val, $convertedVals);
d9ab802d 4570 }
9ae25b56 4571 $values[$dao->$entityIDField] = $val;
6a488035 4572 }
6a488035
TO
4573 return array($values, $options);
4574 }
4575
bb05da0c 4576 /**
4577 * Get the actual custom field name by stripping off the appended string.
4578 *
4579 * The string could be _relative, _from, or _to
4580 *
4581 * @todo use metadata rather than convention to do this.
4582 *
4583 * @param string $parameterName
4584 * The name of the parameter submitted to the form.
4585 * e.g
4586 * custom_3_relative
4587 * custom_3_from
4588 *
4589 * @return string
4590 */
4591 public static function getCustomFieldName($parameterName) {
4592 if (substr($parameterName, -5, 5) == '_from') {
4593 return substr($parameterName, 0, strpos($parameterName, '_from'));
4594 }
4595 if (substr($parameterName, -9, 9) == '_relative') {
4596 return substr($parameterName, 0, strpos($parameterName, '_relative'));
4597 }
4598 if (substr($parameterName, -3, 3) == '_to') {
4599 return substr($parameterName, 0, strpos($parameterName, '_to'));
4600 }
4601 }
4602
4603 /**
4c2fe77b 4604 * Convert submitted values for relative custom fields to query object format.
bb05da0c 4605 *
4606 * The query will support the sqlOperator format so convert to that format.
4607 *
4608 * @param array $formValues
4609 * Submitted values.
4610 * @param array $params
4611 * Converted parameters for the query object.
4612 * @param string $values
4613 * Submitted value.
4614 * @param string $fieldName
4615 * Submitted field name. (Matches form field not DB field.)
4616 */
4c2fe77b 4617 protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) {
bb05da0c 4618 if (empty($values)) {
4619 // e.g we might have relative set & from & to empty. The form flow is a bit funky &
4620 // this function gets called again after they fields have been converted which can get ugly.
4621 return;
4622 }
4623 $customFieldName = self::getCustomFieldName($fieldName);
4624
4625 if (substr($fieldName, -9, 9) == '_relative') {
4626 list($from, $to) = CRM_Utils_Date::getFromTo($values, NULL, NULL);
4627 }
4628 else {
4629 if ($fieldName == $customFieldName . '_to' && CRM_Utils_Array::value($customFieldName . '_from', $formValues)) {
4630 // Both to & from are set. We only need to acton one, choosing from.
4631 return;
4632 }
4633
4c2fe77b 4634 $from = CRM_Utils_Array::value($customFieldName . '_from', $formValues, NULL);
4635 $to = CRM_Utils_Array::value($customFieldName . '_to', $formValues, NULL);
4636
2fe91f9d 4637 if (self::isCustomDateField($customFieldName)) {
4c2fe77b 4638 list($from, $to) = CRM_Utils_Date::getFromTo(NULL, $from, $to);
4639 }
bb05da0c 4640 }
4641
4642 if ($from) {
4643 if ($to) {
4644 $relativeFunction = array('BETWEEN' => array($from, $to));
4645 }
4646 else {
4647 $relativeFunction = array('>=' => $from);
4648 }
4649 }
4650 else {
4651 $relativeFunction = array('<=' => $to);
4652 }
4653 $params[] = array(
4654 $customFieldName,
4655 '=',
4656 $relativeFunction,
4657 0,
4658 0,
4659 );
4660 }
4661
2fe91f9d 4662 /**
4663 * Are we dealing with custom field of type date.
4664 *
4665 * @param $fieldName
4666 *
4667 * @return bool
4668 */
4669 public static function isCustomDateField($fieldName) {
4670 if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($fieldName)) == FALSE) {
4671 return FALSE;
4672 }
4673 if ('Date' == civicrm_api3('CustomField', 'getvalue', array('id' => $customFieldID, 'return' => 'data_type'))) {
4674 return TRUE;
4675 }
4676 return FALSE;
4677 }
4678
df60621b 4679 /**
4680 * Has this field already been reformatting to Query object syntax.
4681 *
4682 * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems
4683 * to come with associated double queries & is possibly damaging performance.
4684 *
4685 * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns
4686 * them as they are.
4687 *
4688 * @param mixed $values
4689 * Value in formValues for the field.
4690 *
4691 * @return bool;
4692 */
06d67d53 4693 public static function isAlreadyProcessedForQueryFormat($values) {
df60621b 4694 if (!is_array($values)) {
4695 return FALSE;
4696 }
4697 if (($operator = CRM_Utils_Array::value(1, $values)) == FALSE) {
4698 return FALSE;
4699 }
4700 return in_array($operator, CRM_Core_DAO::acceptedSQLOperators());
4701 }
4702
20306bb8 4703 /**
4704 * If the state and country are passed remove state.
4705 *
4706 * Country is implicit from the state, but including both results in
4707 * a poor query as there is no combined index on state AND country.
4708 *
4709 * CRM-18125
4710 *
4711 * @param array $formValues
4712 */
4713 public static function filterCountryFromValuesIfStateExists(&$formValues) {
e4001cf0 4714 if (!empty($formValues['country']) && !empty($formValues['state_province'])) {
4715 // The use of array map sanitises the data by ensuring we are dealing with integers.
4716 $states = implode(', ', array_map('intval', $formValues['state_province']));
4717 $countryList = CRM_Core_DAO::singleValueQuery(
4718 "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE id IN ($states)"
4719 );
4720 if ($countryList == $formValues['country']) {
4721 unset($formValues['country']);
20306bb8 4722 }
4723 }
4724 }
4725
36d2f4d5 4726 /**
4727 * For some special cases, grouping by subset of select fields becomes mandatory.
4728 * Hence, full_group_by mode is handled by appending any_value
4729 * keyword to select fields not present in groupBy
4730 *
4731 * @param array $selectClauses
4732 * @param array $groupBy - Columns already included in GROUP By clause.
0ad26017 4733 * @param string $aggregateFunction
36d2f4d5 4734 *
4735 * @return string
4736 */
0ad26017 4737 public static function appendAnyValueToSelect($selectClauses, $groupBy, $aggregateFunction = 'ANY_VALUE') {
b07c960a 4738 if (!CRM_Utils_SQL::disableFullGroupByMode()) {
36d2f4d5 4739 $groupBy = array_map('trim', (array) $groupBy);
f8a02ee1 4740 $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i';
36d2f4d5 4741 foreach ($selectClauses as $key => &$val) {
4742 list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
4743 // append ANY_VALUE() keyword
4744 if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
0ad26017 4745 $val = ($aggregateFunction == 'GROUP_CONCAT') ?
4746 str_replace($selectColumn, "$aggregateFunction(DISTINCT {$selectColumn})", $val) :
4747 str_replace($selectColumn, "$aggregateFunction({$selectColumn})", $val);
36d2f4d5 4748 }
4749 }
4750 }
4751
4752 return "SELECT " . implode(', ', $selectClauses) . " ";
4753 }
4754
b07c960a 4755 /**
4756 * For some special cases, where if non-aggregate ORDER BY columns are not present in GROUP BY
4757 * on full_group_by mode, then append the those missing columns to GROUP BY clause
4758 * keyword to select fields not present in groupBy
4759 *
e5167553 4760 * @param string $groupBy - GROUP BY clause where missing ORDER BY columns will be appended if not present
b07c960a 4761 * @param array $orderBys - ORDER BY sub-clauses
4762 *
4763 */
4764 public static function getGroupByFromOrderBy(&$groupBy, $orderBys) {
0ad26017 4765 if (!CRM_Utils_SQL::disableFullGroupByMode()) {
b07c960a 4766 foreach ($orderBys as $orderBy) {
4fa404ba 4767 $orderBy = str_ireplace(array(' DESC', ' ASC', '`'), '', $orderBy); // remove sort syntax from ORDER BY clauses if present
b07c960a 4768 // if ORDER BY column is not present in GROUP BY then append it to end
e5167553 4769 if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1 && !strstr($groupBy, $orderBy)) {
4770 $groupBy .= ", {$orderBy}";
b07c960a 4771 }
4772 }
4773 }
4774 }
4775
e5cceea5 4776 /**
4777 * Include Select columns in groupBy clause.
4778 *
dc852c7b 4779 * @param array $selectClauses
e5cceea5 4780 * @param array $groupBy - Columns already included in GROUP By clause.
4781 *
4782 * @return string
4783 */
4784 public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
b708c08d 4785 $groupBy = (array) $groupBy;
3636b520 4786 $mysqlVersion = CRM_Core_DAO::singleValueQuery('SELECT VERSION()');
bad98dd5 4787 $sqlMode = CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode');
b708c08d 4788
4789 //return if ONLY_FULL_GROUP_BY is not enabled.
712e729f 4790 if (CRM_Utils_SQL::supportsFullGroupBy() && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) {
f8a02ee1 4791 $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i';
dc852c7b 4792 foreach ($selectClauses as $key => $val) {
4793 $aliasArray = preg_split('/ as /i', $val);
4794 // if more than 1 alias we need to split by ','.
4795 if (count($aliasArray) > 2) {
4796 $aliasArray = preg_split('/,/', $val);
4797 foreach ($aliasArray as $key => $value) {
4798 $alias = current(preg_split('/ as /i', $value));
4799 if (!in_array($alias, $groupBy) && preg_match($regexToExclude, trim($alias)) !== 1) {
4800 $groupBy[] = $alias;
4801 }
e5cceea5 4802 }
4803 }
dc852c7b 4804 else {
4805 list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
4806 $dateRegex = '/^(DATE_FORMAT|DATE_ADD|CASE)/i';
4807 $tableName = current(explode('.', $selectColumn));
4808 $primaryKey = "{$tableName}.id";
4809 // exclude columns which are already included in groupBy and aggregate functions from select
4810 // CRM-18439 - Also exclude the columns which are functionally dependent on columns in $groupBy (MySQL 5.7+)
4811 if (!in_array($selectColumn, $groupBy) && !in_array($primaryKey, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
4812 if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
4813 $groupBy[] = $alias;
4814 }
4815 else {
4816 $groupBy[] = $selectColumn;
4817 }
e5cceea5 4818 }
4819 }
4820 }
4821 }
4822
b708c08d 4823 if (!empty($groupBy)) {
4824 return " GROUP BY " . implode(', ', $groupBy);
e5cceea5 4825 }
4826 return '';
4827 }
4828
6a488035 4829 /**
fe482240 4830 * Create and query the db for an contact search.
6a488035 4831 *
77c5b619
TO
4832 * @param int $offset
4833 * The offset for the query.
4834 * @param int $rowCount
4835 * The number of rows to return.
db54ba06 4836 * @param string|CRM_Utils_Sort $sort
77c5b619
TO
4837 * The order by string.
4838 * @param bool $count
4839 * Is this a count only query ?.
4840 * @param bool $includeContactIds
4841 * Should we include contact ids?.
4842 * @param bool $sortByChar
4843 * If true returns the distinct array of first characters for search results.
4844 * @param bool $groupContacts
4845 * If true, return only the contact ids.
4846 * @param bool $returnQuery
4847 * Should we return the query as a string.
4848 * @param string $additionalWhereClause
4849 * If the caller wants to further restrict the search (used for components).
77b97be7 4850 * @param null $sortOrder
77c5b619
TO
4851 * @param string $additionalFromClause
4852 * Should be clause with proper joins, effective to reduce where clause load.
77b97be7
EM
4853 *
4854 * @param bool $skipOrderAndLimit
6a488035 4855 *
55eb4e22 4856 * @return CRM_Core_DAO
6a488035 4857 */
59f4c9ee 4858 public function searchQuery(
6a488035
TO
4859 $offset = 0, $rowCount = 0, $sort = NULL,
4860 $count = FALSE, $includeContactIds = FALSE,
4861 $sortByChar = FALSE, $groupContacts = FALSE,
4862 $returnQuery = FALSE,
4863 $additionalWhereClause = NULL, $sortOrder = NULL,
4864 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE
4865 ) {
4866
4867 if ($includeContactIds) {
4868 $this->_includeContactIds = TRUE;
4869 $this->_whereClause = $this->whereClause();
4870 }
4871
6a488035
TO
4872 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params);
4873
f4bff68a 4874 // if we’re explicitly looking for a certain contact’s contribs, events, etc.
6a488035
TO
4875 // and that contact happens to be deleted, set $onlyDeleted to true
4876 foreach ($this->_params as $values) {
4877 $name = CRM_Utils_Array::value(0, $values);
4878 $op = CRM_Utils_Array::value(1, $values);
4879 $value = CRM_Utils_Array::value(2, $values);
4880 if ($name == 'contact_id' and $op == '=') {
4881 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) {
4882 $onlyDeleted = TRUE;
4883 }
4884 break;
4885 }
4886 }
6a488035 4887
6a488035 4888 // building the query string
0ad26017 4889 $groupBy = $groupByCols = NULL;
6a488035
TO
4890 if (!$count) {
4891 if (isset($this->_groupByComponentClause)) {
4892 $groupBy = $this->_groupByComponentClause;
0ad26017 4893 $groupByCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause));
4894 $groupByCols = explode(', ', $groupByCols);
6a488035
TO
4895 }
4896 elseif ($this->_useGroupBy) {
0ad26017 4897 $groupByCols = array('contact_a.id');
6a488035
TO
4898 }
4899 }
4900 if ($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY && (!$count)) {
0ad26017 4901 $groupByCols = array('civicrm_activity.id');
6a488035
TO
4902 }
4903
4904 $order = $orderBy = $limit = '';
4905 if (!$count) {
db54ba06 4906 list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
6a488035 4907
6a488035 4908 if ($rowCount > 0 && $offset >= 0) {
bf00d1b6
DL
4909 $offset = CRM_Utils_Type::escape($offset, 'Int');
4910 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
6a488035 4911 $limit = " LIMIT $offset, $rowCount ";
4243847f
CW
4912 }
4913 }
e5167553 4914 // Two cases where we are disabling FGB (FULL_GROUP_BY_MODE):
4915 // 1. Expecting the search query to return all the first single letter characters of contacts ONLY, but when FGB is enabled
4916 // MySQL expect the columns present in GROUP BY, must be present in SELECT clause and that results into error, needless to have other columns.
4917 // 2. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT
4918 // clause. This will impact the search query output.
2663d5df 4919 $disableFullGroupByMode = ($sortByChar || !empty($groupByCols) || $groupContacts);
e5167553 4920
4921 if ($disableFullGroupByMode) {
4922 CRM_Core_DAO::disableFullGroupByMode();
4923 }
6a488035 4924
47cd307b 4925 // CRM-15231
94cb2350 4926 $this->_sort = $sort;
d9ab802d 4927
99c00f76
DG
4928 //CRM-15967
4929 $this->includePseudoFieldsJoin($sort);
4930
2c3ad4b7 4931 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);
1939d296 4932
0ad26017 4933 if (!empty($groupByCols)) {
0ad26017 4934 $groupBy = " GROUP BY " . implode(', ', $groupByCols);
e5cceea5 4935 }
4936
1939d296 4937 if ($additionalWhereClause) {
4938 $where = $where . ' AND ' . $additionalWhereClause;
4939 }
4940
5c7dad85 4941 //additional from clause should be w/ proper joins.
4243847f
CW
4942 if ($additionalFromClause) {
4943 $from .= "\n" . $additionalFromClause;
6a488035
TO
4944 }
4945
4946 // if we are doing a transform, do it here
4947 // use the $from, $where and $having to get the contact ID
4948 if ($this->_displayRelationshipType) {
4949 $this->filterRelatedContacts($from, $where, $having);
4950 }
4951
4952 if ($skipOrderAndLimit) {
4953 $query = "$select $from $where $having $groupBy";
4954 }
4955 else {
4956 $query = "$select $from $where $having $groupBy $order $limit";
4957 }
4958
4959 if ($returnQuery) {
4960 return $query;
4961 }
6a488035
TO
4962 if ($count) {
4963 return CRM_Core_DAO::singleValueQuery($query);
4964 }
4965
6a488035 4966 $dao = CRM_Core_DAO::executeQuery($query);
d567ed10 4967
e5167553 4968 if ($disableFullGroupByMode) {
d567ed10 4969 CRM_Core_DAO::reenableFullGroupByMode();
4970 }
4971
6a488035
TO
4972 if ($groupContacts) {
4973 $ids = array();
4974 while ($dao->fetch()) {
4975 $ids[] = $dao->id;
4976 }
4977 return implode(',', $ids);
4978 }
4979
4980 return $dao;
4981 }
4982
4243847f 4983 /**
2ca46d4d 4984 * Fetch a list of contacts for displaying a search results page
4243847f 4985 *
2ca46d4d
TO
4986 * @param array $cids
4987 * List of contact IDs
77c5b619 4988 * @param bool $includeContactIds
4243847f
CW
4989 * @return CRM_Core_DAO
4990 */
2ca46d4d
TO
4991 public function getCachedContacts($cids, $includeContactIds) {
4992 CRM_Utils_Type::validateAll($cids, 'Positive');
4243847f 4993 $this->_includeContactIds = $includeContactIds;
f6364403 4994 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params);
4995 list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
2ca46d4d
TO
4996 $select .= sprintf(", (%s) AS _wgt", $this->createSqlCase('contact_a.id', $cids));
4997 $where .= sprintf(' AND contact_a.id IN (%s)', implode(',', $cids));
4998 $order = 'ORDER BY _wgt';
1d8fcdc5 4999 $groupBy = $this->_useGroupBy ? ' GROUP BY contact_a.id' : '';
2ca46d4d 5000 $limit = '';
9b5914f2 5001 $query = "$select $from $where $groupBy $order $limit";
23379b49 5002
4243847f
CW
5003 return CRM_Core_DAO::executeQuery($query);
5004 }
5005
2ca46d4d
TO
5006 /**
5007 * Construct a SQL CASE expression.
5008 *
5009 * @param string $idCol
5010 * The name of a column with ID's (eg 'contact_a.id').
5011 * @param array $cids
5012 * Array(int $weight => int $id).
5013 * @return string
5014 * CASE WHEN id=123 THEN 1 WHEN id=456 THEN 2 END
5015 */
5016 private function createSqlCase($idCol, $cids) {
5017 $buf = "CASE\n";
5018 foreach ($cids as $weight => $cid) {
5019 $buf .= " WHEN $idCol = $cid THEN $weight \n";
5020 }
5021 $buf .= "END\n";
5022 return $buf;
5023 }
5024
6a488035
TO
5025 /**
5026 * Populate $this->_permissionWhereClause with permission related clause and update other
5027 * query related properties.
5028 *
5029 * Function calls ACL permission class and hooks to filter the query appropriately
5030 *
5031 * Note that these 2 params were in the code when extracted from another function
5032 * and a second round extraction would be to make them properties of the class
5033 *
77c5b619
TO
5034 * @param bool $onlyDeleted
5035 * Only get deleted contacts.
5036 * @param bool $count
5037 * Return Count only.
6a488035 5038 */
00be9182 5039 public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) {
6a488035
TO
5040 if (!$this->_skipPermission) {
5041 $this->_permissionWhereClause = CRM_ACL_API::whereClause(
5042 CRM_Core_Permission::VIEW,
5043 $this->_tables,
5044 $this->_whereTables,
5045 NULL,
5046 $onlyDeleted,
5047 $this->_skipDeleteClause
5048 );
5049
5050 // regenerate fromClause since permission might have added tables
5051 if ($this->_permissionWhereClause) {
5052 //fix for row count in qill (in contribute/membership find)
5053 if (!$count) {
5054 $this->_useDistinct = TRUE;
5055 }
94cb2350 5056 //CRM-15231
5057 $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode);
5058 $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode);
47cd307b 5059 // note : this modifies _fromClause and _simpleFromClause
ac4985d5 5060 $this->includePseudoFieldsJoin($this->_sort);
6a488035
TO
5061 }
5062 }
5063 else {
5064 // add delete clause if needed even if we are skipping permission
5065 // CRM-7639
5066 if (!$this->_skipDeleteClause) {
5067 if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
5068 $this->_permissionWhereClause = '(contact_a.is_deleted)';
5069 }
5070 else {
5071 // CRM-6181
5072 $this->_permissionWhereClause = '(contact_a.is_deleted = 0)';
5073 }
5074 }
5075 }
5076 }
5077
86538308
EM
5078 /**
5079 * @param $val
5080 */
00be9182 5081 public function setSkipPermission($val) {
6a488035
TO
5082 $this->_skipPermission = $val;
5083 }
5084
86538308
EM
5085 /**
5086 * @param null $context
5087 *
5088 * @return array
5089 */
00be9182 5090 public function &summaryContribution($context = NULL) {
d7803ec1 5091 list($innerselect, $from, $where, $having) = $this->query(TRUE);
6a488035
TO
5092
5093 // hack $select
5094 $select = "
d7803ec1
EM
5095SELECT COUNT( conts.total_amount ) as total_count,
5096 SUM( conts.total_amount ) as total_amount,
5097 AVG( conts.total_amount ) as total_avg,
5098 conts.currency as currency";
11a5aa5d 5099 if ($this->_permissionWhereClause) {
d7803ec1
EM
5100 $where .= " AND " . $this->_permissionWhereClause;
5101 }
6a488035
TO
5102 if ($context == 'search') {
5103 $where .= " AND contact_a.is_deleted = 0 ";
5104 }
5105
dbaa9d7d 5106 $query = $this->appendFinancialTypeWhereAndFromToQueryStrings($where, $from);
5107
d7803ec1 5108 // make sure contribution is completed - CRM-4989
5c7dad85 5109 $completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 ";
d7803ec1 5110
6a488035
TO
5111 $summary = array();
5112 $summary['total'] = array();
5113 $summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a";
d03881d3 5114 $innerQuery = "SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
5115 civicrm_contribution.currency $from $completedWhere";
d7803ec1 5116 $query = "$select FROM (
3636b520 5117 $innerQuery GROUP BY civicrm_contribution.id
d7803ec1
EM
5118 ) as conts
5119 GROUP BY currency";
6a488035 5120
d7803ec1 5121 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
5122
5123 $summary['total']['count'] = 0;
5124 $summary['total']['amount'] = $summary['total']['avg'] = array();
5125 while ($dao->fetch()) {
5126 $summary['total']['count'] += $dao->total_count;
5127 $summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency);
5128 $summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency);
5129 }
d03881d3 5130
5131 $orderBy = 'ORDER BY civicrm_contribution_total_amount_count DESC';
6550b153 5132 $groupBy = 'GROUP BY currency, civicrm_contribution.total_amount';
e5cceea5 5133 $modeSQL = "$select, SUBSTRING_INDEX(GROUP_CONCAT(conts.total_amount
5134 ORDER BY conts.civicrm_contribution_total_amount_count DESC SEPARATOR ';'), ';', 1) as amount,
5135 MAX(conts.civicrm_contribution_total_amount_count) as civicrm_contribution_total_amount_count
5136 FROM ($innerQuery
5137 $groupBy $orderBy) as conts
5138 GROUP BY currency";
d03881d3 5139
5140 $summary['total']['mode'] = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL);
5141
5142 $medianSQL = "{$from} {$completedWhere}";
5143 $summary['total']['median'] = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, 'civicrm_contribution');
5144 $summary['total']['currencyCount'] = count($summary['total']['median']);
5145
6a488035
TO
5146 if (!empty($summary['total']['amount'])) {
5147 $summary['total']['amount'] = implode(',&nbsp;', $summary['total']['amount']);
5148 $summary['total']['avg'] = implode(',&nbsp;', $summary['total']['avg']);
d03881d3 5149 $summary['total']['mode'] = implode(',&nbsp;', $summary['total']['mode']);
5150 $summary['total']['median'] = implode(',&nbsp;', $summary['total']['median']);
6a488035
TO
5151 }
5152 else {
d03881d3 5153 $summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0;
6a488035
TO
5154 }
5155
d4e2b978
DS
5156 // soft credit summary
5157 if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) {
5158 $softCreditWhere = "{$completedWhere} AND civicrm_contribution_soft.id IS NOT NULL";
5159 $query = "
5160 $select FROM (
5161 SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency $from $softCreditWhere
3636b520 5162 GROUP BY civicrm_contribution_soft.id
d4e2b978
DS
5163 ) as conts
5164 GROUP BY currency";
5165 $dao = CRM_Core_DAO::executeQuery($query);
5166 $summary['soft_credit']['count'] = 0;
5167 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = array();
5168 while ($dao->fetch()) {
5169 $summary['soft_credit']['count'] += $dao->total_count;
5170 $summary['soft_credit']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency);
5171 $summary['soft_credit']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency);
5172 }
5173 if (!empty($summary['soft_credit']['amount'])) {
5174 $summary['soft_credit']['amount'] = implode(',&nbsp;', $summary['soft_credit']['amount']);
5175 $summary['soft_credit']['avg'] = implode(',&nbsp;', $summary['soft_credit']['avg']);
5176 }
5177 else {
5178 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0;
5179 }
5180 }
5181
6a488035 5182 // hack $select
d7803ec1
EM
5183 //@todo - this could be one query using the IF in mysql - eg
5184 // SELECT sum(total_completed), sum(count_completed), sum(count_cancelled), sum(total_cancelled) FROM (
5185 // SELECT civicrm_contribution.total_amount, civicrm_contribution.currency ,
5186 // IF(civicrm_contribution.contribution_status_id = 1, 1, 0 ) as count_completed,
5187 // IF(civicrm_contribution.contribution_status_id = 1, total_amount, 0 ) as total_completed,
5188 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, 1, 0 ) as count_cancelled,
5189 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, total_amount, 0 ) as total_cancelled
5190 // FROM civicrm_contact contact_a
5191 // LEFT JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id
5192 // WHERE ( ... where clause....
5193 // AND (civicrm_contribution.cancel_date IS NOT NULL OR civicrm_contribution.contribution_status_id = 1)
5194 // ) as conts
5195
6a488035 5196 $select = "
d7803ec1
EM
5197SELECT COUNT( conts.total_amount ) as cancel_count,
5198 SUM( conts.total_amount ) as cancel_amount,
5199 AVG( conts.total_amount ) as cancel_avg,
5200 conts.currency as currency";
6a488035
TO
5201
5202 $where .= " AND civicrm_contribution.cancel_date IS NOT NULL ";
5203 if ($context == 'search') {
5204 $where .= " AND contact_a.is_deleted = 0 ";
5205 }
5206
d7803ec1
EM
5207 $query = "$select FROM (
5208 SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $where
3636b520 5209 GROUP BY civicrm_contribution.id
d7803ec1
EM
5210 ) as conts
5211 GROUP BY currency";
5212
5213 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
5214
5215 if ($dao->N <= 1) {
5216 if ($dao->fetch()) {
5217 $summary['cancel']['count'] = $dao->cancel_count;
982feebf
J
5218 $summary['cancel']['amount'] = CRM_Utils_Money::format($dao->cancel_amount, $dao->currency);
5219 $summary['cancel']['avg'] = CRM_Utils_Money::format($dao->cancel_avg, $dao->currency);
6a488035
TO
5220 }
5221 }
5222 else {
5223 $summary['cancel']['count'] = 0;
5224 $summary['cancel']['amount'] = $summary['cancel']['avg'] = array();
5225 while ($dao->fetch()) {
5226 $summary['cancel']['count'] += $dao->cancel_count;
5227 $summary['cancel']['amount'][] = CRM_Utils_Money::format($dao->cancel_amount, $dao->currency);
5228 $summary['cancel']['avg'][] = CRM_Utils_Money::format($dao->cancel_avg, $dao->currency);
5229 }
5230 $summary['cancel']['amount'] = implode(',&nbsp;', $summary['cancel']['amount']);
5231 $summary['cancel']['avg'] = implode(',&nbsp;', $summary['cancel']['avg']);
5232 }
5233
5234 return $summary;
5235 }
5236
5384a978 5237 /**
dbaa9d7d 5238 * Append financial ACL limits to the query from & where clauses, if applicable.
5384a978 5239 *
dbaa9d7d 5240 * @param string $where
5241 * @param string $from
5384a978 5242 */
dbaa9d7d 5243 public function appendFinancialTypeWhereAndFromToQueryStrings(&$where, &$from) {
5244 if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
5245 return;
5384a978
VR
5246 }
5247 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
5248 if (!empty($financialTypes)) {
5249 $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL";
5250 $from .= " LEFT JOIN civicrm_line_item li
5251 ON civicrm_contribution.id = li.contribution_id AND
5252 li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") ";
5253 }
5254 else {
5255 $where .= " AND civicrm_contribution.financial_type_id IN (0)";
5256 }
5384a978
VR
5257 }
5258
6a488035 5259 /**
fe482240 5260 * Getter for the qill object.
6a488035
TO
5261 *
5262 * @return string
6a488035 5263 */
00be9182 5264 public function qill() {
6a488035
TO
5265 return $this->_qill;
5266 }
5267
5268 /**
fe482240 5269 * Default set of return default hier return properties.
6a488035 5270 *
fffe9ee1 5271 * @return array
6a488035 5272 */
00be9182 5273 public static function &defaultHierReturnProperties() {
6a488035
TO
5274 if (!isset(self::$_defaultHierReturnProperties)) {
5275 self::$_defaultHierReturnProperties = array(
5276 'home_URL' => 1,
5277 'image_URL' => 1,
5278 'legal_identifier' => 1,
5279 'external_identifier' => 1,
5280 'contact_type' => 1,
5281 'contact_sub_type' => 1,
5282 'sort_name' => 1,
5283 'display_name' => 1,
5284 'nick_name' => 1,
5285 'first_name' => 1,
5286 'middle_name' => 1,
5287 'last_name' => 1,
67744c4e
CW
5288 'prefix_id' => 1,
5289 'suffix_id' => 1,
e171748b 5290 'formal_title' => 1,
aa62b355 5291 'communication_style_id' => 1,
6a488035
TO
5292 'email_greeting' => 1,
5293 'postal_greeting' => 1,
5294 'addressee' => 1,
5295 'birth_date' => 1,
67744c4e 5296 'gender_id' => 1,
6a488035
TO
5297 'preferred_communication_method' => 1,
5298 'do_not_phone' => 1,
5299 'do_not_email' => 1,
5300 'do_not_mail' => 1,
5301 'do_not_sms' => 1,
5302 'do_not_trade' => 1,
59f4c9ee
TO
5303 'location' => array(
5304 '1' => array(
5305 'location_type' => 1,
5306 'street_address' => 1,
5307 'city' => 1,
5308 'state_province' => 1,
5309 'postal_code' => 1,
5310 'postal_code_suffix' => 1,
5311 'country' => 1,
5312 'phone-Phone' => 1,
5313 'phone-Mobile' => 1,
5314 'phone-Fax' => 1,
5315 'phone-1' => 1,
5316 'phone-2' => 1,
5317 'phone-3' => 1,
5318 'im-1' => 1,
5319 'im-2' => 1,
5320 'im-3' => 1,
5321 'email-1' => 1,
5322 'email-2' => 1,
5323 'email-3' => 1,
6a488035 5324 ),
59f4c9ee
TO
5325 '2' => array(
5326 'location_type' => 1,
5327 'street_address' => 1,
5328 'city' => 1,
5329 'state_province' => 1,
5330 'postal_code' => 1,
5331 'postal_code_suffix' => 1,
5332 'country' => 1,
5333 'phone-Phone' => 1,
5334 'phone-Mobile' => 1,
5335 'phone-1' => 1,
5336 'phone-2' => 1,
5337 'phone-3' => 1,
5338 'im-1' => 1,
5339 'im-2' => 1,
5340 'im-3' => 1,
5341 'email-1' => 1,
5342 'email-2' => 1,
5343 'email-3' => 1,
5344 ),
5345 ),
6a488035
TO
5346 );
5347 }
5348 return self::$_defaultHierReturnProperties;
5349 }
5350
86538308 5351 /**
c037736a 5352 * Build query for a date field.
5353 *
5354 * @param array $values
100fef9d
CW
5355 * @param string $tableName
5356 * @param string $fieldName
5357 * @param string $dbFieldName
c037736a 5358 * @param string $fieldTitle
86538308 5359 * @param bool $appendTimeStamp
d51e2817 5360 * @param string $dateFormat
86538308 5361 */
59f4c9ee 5362 public function dateQueryBuilder(
6a488035
TO
5363 &$values, $tableName, $fieldName,
5364 $dbFieldName, $fieldTitle,
d51e2817 5365 $appendTimeStamp = TRUE,
5366 $dateFormat = 'YmdHis'
6a488035
TO
5367 ) {
5368 list($name, $op, $value, $grouping, $wildcard) = $values;
5369
6a488035
TO
5370 if ($name == "{$fieldName}_low" ||
5371 $name == "{$fieldName}_high"
5372 ) {
efb88612 5373 if (isset($this->_rangeCache[$fieldName]) || !$value) {
6a488035
TO
5374 return;
5375 }
5376 $this->_rangeCache[$fieldName] = 1;
5377
5378 $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL;
5379
5380 if ($name == $fieldName . '_low') {
5381 $firstOP = '>=';
228917cd 5382 $firstPhrase = ts('greater than or equal to');
d51e2817 5383 $firstDate = CRM_Utils_Date::processDate($value, NULL, FALSE, $dateFormat);
6a488035
TO
5384
5385 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
228917cd 5386 if (!empty($secondValues) && $secondValues[2]) {
6a488035 5387 $secondOP = '<=';
228917cd 5388 $secondPhrase = ts('less than or equal to');
6a488035
TO
5389 $secondValue = $secondValues[2];
5390
228917cd 5391 if ($appendTimeStamp && strlen($secondValue) == 10) {
6a488035
TO
5392 $secondValue .= ' 23:59:59';
5393 }
d51e2817 5394 $secondDate = CRM_Utils_Date::processDate($secondValue, NULL, FALSE, $dateFormat);
6a488035
TO
5395 }
5396 }
5397 elseif ($name == $fieldName . '_high') {
5398 $firstOP = '<=';
228917cd 5399 $firstPhrase = ts('less than or equal to');
6a488035 5400
228917cd 5401 if ($appendTimeStamp && strlen($value) == 10) {
6a488035
TO
5402 $value .= ' 23:59:59';
5403 }
d51e2817 5404 $firstDate = CRM_Utils_Date::processDate($value, NULL, FALSE, $dateFormat);
6a488035
TO
5405
5406 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
228917cd 5407 if (!empty($secondValues) && $secondValues[2]) {
6a488035 5408 $secondOP = '>=';
228917cd 5409 $secondPhrase = ts('greater than or equal to');
6a488035 5410 $secondValue = $secondValues[2];
d51e2817 5411 $secondDate = CRM_Utils_Date::processDate($secondValue, NULL, FALSE, $dateFormat);
6a488035
TO
5412 }
5413 }
5414
5415 if (!$appendTimeStamp) {
5416 $firstDate = substr($firstDate, 0, 8);
5417 }
5418 $firstDateFormat = CRM_Utils_Date::customFormat($firstDate);
5419
5420 if ($secondDate) {
5421 if (!$appendTimeStamp) {
5422 $secondDate = substr($secondDate, 0, 8);
5423 }
5424 $secondDateFormat = CRM_Utils_Date::customFormat($secondDate);
5425 }
5426
5427 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5428 if ($secondDate) {
5429 $this->_where[$grouping][] = "
5430( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5431( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5432";
5433 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\"";
5434 }
5435 else {
5436 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5437 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
5438 }
5439 }
5440
5441 if ($name == $fieldName) {
d820f4e8 5442 //In Get API, for operators other then '=' the $value is in array(op => value) format
5443 if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
5444 $op = key($value);
5445 $value = $value[$op];
5446 }
6a488035 5447
efb88612 5448 $date = $format = NULL;
fbc6a4d4 5449 if (strstr($op, 'IN')) {
5450 $format = array();
ee657a1c 5451 foreach ($value as &$date) {
d51e2817 5452 $date = CRM_Utils_Date::processDate($date, NULL, FALSE, $dateFormat);
ee657a1c 5453 if (!$appendTimeStamp) {
5454 $date = substr($date, 0, 8);
5455 }
fbc6a4d4 5456 $format[] = CRM_Utils_Date::customFormat($date);
ee657a1c 5457 }
5458 $date = "('" . implode("','", $value) . "')";
fbc6a4d4 5459 $format = implode(', ', $format);
ee657a1c 5460 }
0a74debb 5461 elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) {
d51e2817 5462 $date = CRM_Utils_Date::processDate($value, NULL, FALSE, $dateFormat);
ee657a1c 5463 if (!$appendTimeStamp) {
5464 $date = substr($date, 0, 8);
5465 }
fbc6a4d4 5466 $format = CRM_Utils_Date::customFormat($date);
efb88612 5467 $date = "'$date'";
6a488035 5468 }
6a488035
TO
5469
5470 if ($date) {
f1339cf2 5471 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op $date";
6a488035
TO
5472 }
5473 else {
515293b2 5474 $this->_where[$grouping][] = self::buildClause("{$tableName}.{$dbFieldName}", $op);
6a488035 5475 }
efb88612 5476
6a488035 5477 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
d820f4e8 5478
5479 $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
efb88612 5480 $this->_qill[$grouping][] = "$fieldTitle $op $format";
6a488035 5481 }
6a488035
TO
5482 }
5483
86538308
EM
5484 /**
5485 * @param $values
100fef9d
CW
5486 * @param string $tableName
5487 * @param string $fieldName
5488 * @param string $dbFieldName
86538308
EM
5489 * @param $fieldTitle
5490 * @param null $options
5491 */
59f4c9ee 5492 public function numberRangeBuilder(
51ccfbbe
TO
5493 &$values,
5494 $tableName, $fieldName,
5495 $dbFieldName, $fieldTitle,
5496 $options = NULL
6a488035
TO
5497 ) {
5498 list($name, $op, $value, $grouping, $wildcard) = $values;
5499
5500 if ($name == "{$fieldName}_low" ||
5501 $name == "{$fieldName}_high"
5502 ) {
5503 if (isset($this->_rangeCache[$fieldName])) {
5504 return;
5505 }
5506 $this->_rangeCache[$fieldName] = 1;
5507
5508 $secondOP = $secondPhrase = $secondValue = NULL;
5509
5510 if ($name == "{$fieldName}_low") {
5511 $firstOP = '>=';
228917cd 5512 $firstPhrase = ts('greater than');
6a488035
TO
5513
5514 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5515 if (!empty($secondValues)) {
5516 $secondOP = '<=';
228917cd 5517 $secondPhrase = ts('less than');
6a488035
TO
5518 $secondValue = $secondValues[2];
5519 }
5520 }
5521 else {
5522 $firstOP = '<=';
228917cd 5523 $firstPhrase = ts('less than');
6a488035
TO
5524
5525 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5526 if (!empty($secondValues)) {
5527 $secondOP = '>=';
228917cd 5528 $secondPhrase = ts('greater than');
6a488035
TO
5529 $secondValue = $secondValues[2];
5530 }
5531 }
5532
5533 if ($secondOP) {
5534 $this->_where[$grouping][] = "
5535( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND
5536( {$tableName}.{$dbFieldName} $secondOP {$secondValue} )
5537";
5538 $displayValue = $options ? $options[$value] : $value;
5539 $secondDisplayValue = $options ? $options[$secondValue] : $secondValue;
5540
59f4c9ee
TO
5541 $this->_qill[$grouping][]
5542 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
6a488035
TO
5543 }
5544 else {
5545 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}";
5546 $displayValue = $options ? $options[$value] : $value;
5547 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5548 }
5549 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5550
5551 return;
5552 }
5553
5554 if ($name == $fieldName) {
5555 $op = '=';
5556 $phrase = '=';
5557
5558 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}";
5559
5560 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5561 $displayValue = $options ? $options[$value] : $value;
5562 $this->_qill[$grouping][] = "$fieldTitle - $phrase \"$displayValue\"";
5563 }
6a488035
TO
5564 }
5565
c4a7c967 5566
5567 /**
5568 * @param $values
5569 * @param string $tableName
5570 * @param string $fieldName
5571 * @param string $dbFieldName
5572 * @param $fieldTitle
5573 * @param null $options
5574 */
5575 public function ageRangeQueryBuilder(
5576 &$values,
5577 $tableName, $fieldName,
5578 $dbFieldName, $fieldTitle,
5579 $options = NULL
5580 ) {
5581 list($name, $op, $value, $grouping, $wildcard) = $values;
5582
5583 $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping);
5584 $asofDate = NULL; // will be treated as current day
5585 if ($asofDateValues) {
5586 $asofDate = CRM_Utils_Date::processDate($asofDateValues[2]);
5587 $asofDateFormat = CRM_Utils_Date::customFormat(substr($asofDate, 0, 8));
5588 $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat;
5589 }
5590
5591 if ($name == "{$fieldName}_low" ||
5592 $name == "{$fieldName}_high"
5593 ) {
5594 if (isset($this->_rangeCache[$fieldName])) {
5595 return;
5596 }
5597 $this->_rangeCache[$fieldName] = 1;
5598
5599 $secondOP = $secondPhrase = $secondValue = NULL;
5600
5601 if ($name == "{$fieldName}_low") {
5602 $firstPhrase = ts('greater than or equal to');
5603 // NB: age > X means date of birth < Y
5604 $firstOP = '<=';
5605 $firstDate = self::calcDateFromAge($asofDate, $value, 'min');
5606
5607 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5608 if (!empty($secondValues)) {
5609 $secondOP = '>=';
5610 $secondPhrase = ts('less than or equal to');
5611 $secondValue = $secondValues[2];
5612 $secondDate = self::calcDateFromAge($asofDate, $secondValue, 'max');
5613 }
5614 }
5615 else {
5616 $firstOP = '>=';
5617 $firstPhrase = ts('less than or equal to');
5618 $firstDate = self::calcDateFromAge($asofDate, $value, 'max');
5619
5620 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5621 if (!empty($secondValues)) {
5622 $secondOP = '<=';
5623 $secondPhrase = ts('greater than or equal to');
5624 $secondValue = $secondValues[2];
5625 $secondDate = self::calcDateFromAge($asofDate, $secondValue, 'min');
5626 }
5627 }
5628
c4a7c967 5629 if ($secondOP) {
5630 $this->_where[$grouping][] = "
5631( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5632( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5633";
5634 $displayValue = $options ? $options[$value] : $value;
5635 $secondDisplayValue = $options ? $options[$secondValue] : $secondValue;
5636
5637 $this->_qill[$grouping][]
5638 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5639 }
5640 else {
5641 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5642 $displayValue = $options ? $options[$value] : $value;
5643 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5644 }
5645 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5646 return;
5647 }
5648 }
5649
70599df6 5650 /**
5651 * Calculate date from age.
5652 *
5653 * @param string $asofDate
5654 * @param int $age
5655 * @param string $type
5656 *
5657 * @return string
5658 */
c4a7c967 5659 public static function calcDateFromAge($asofDate, $age, $type) {
5660 $date = new DateTime($asofDate);
5661 if ($type == "min") {
5662 // minimum age is $age: dob <= date - age "235959"
5663 $date->sub(new DateInterval("P" . $age . "Y"));
5664 return $date->format('Ymd') . "235959";
69c53a43 5665 }
5666 else {
c4a7c967 5667 // max age is $age: dob >= date - (age + 1y) + 1d "000000"
5668 $date->sub(new DateInterval("P" . ($age + 1) . "Y"))->add(new DateInterval("P1D"));
5669 return $date->format('Ymd') . "000000";
5670 }
5671 }
5672
6a488035
TO
5673 /**
5674 * Given the field name, operator, value & its data type
5675 * builds the where Clause for the query
5676 * used for handling 'IS NULL'/'IS NOT NULL' operators
5677 *
77c5b619
TO
5678 * @param string $field
5679 * Fieldname.
5680 * @param string $op
5681 * Operator.
5682 * @param string $value
5683 * Value.
5684 * @param string $dataType
5685 * Data type of the field.
6a488035 5686 *
a6c01b45 5687 * @return string
59f4c9ee 5688 * Where clause for the query.
6a488035 5689 */
00be9182 5690 public static function buildClause($field, $op, $value = NULL, $dataType = NULL) {
6a488035
TO
5691 $op = trim($op);
5692 $clause = "$field $op";
5693
5694 switch ($op) {
5695 case 'IS NULL':
5696 case 'IS NOT NULL':
5697 return $clause;
5698
5699 case 'IS EMPTY':
89d4a22f 5700 $clause = ($dataType == 'Date') ? " $field IS NULL " : " (NULLIF($field, '') IS NULL) ";
6a488035
TO
5701 return $clause;
5702
5703 case 'IS NOT EMPTY':
89d4a22f 5704 $clause = ($dataType == 'Date') ? " $field IS NOT NULL " : " (NULLIF($field, '') IS NOT NULL) ";
6a488035
TO
5705 return $clause;
5706
a0475688 5707 case 'RLIKE':
5708 return " {$clause} BINARY '{$value}' ";
5709
e2b0fbec 5710 case 'IN':
5711 case 'NOT IN':
715277e7 5712 // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter.
46b3417a
CW
5713 if (!empty($value) && (!is_array($value) || !array_key_exists($op, $value))) {
5714 $value = array($op => (array) $value);
e2b0fbec 5715 }
5716
6a488035 5717 default:
89d4a22f 5718 if (empty($dataType) || $dataType == 'Date') {
6a488035
TO
5719 $dataType = 'String';
5720 }
461c9a60
EM
5721 if (is_array($value)) {
5722 //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more
5723 // widely used and consistent across the codebase
5724 // adding this here won't accept the search functions which don't submit an array
5725 if (($queryString = CRM_Core_DAO::createSqlFilter($field, $value, $dataType)) != FALSE) {
6e23130a 5726
461c9a60
EM
5727 return $queryString;
5728 }
ccabbc44 5729 if (!empty($value[0]) && $op === 'BETWEEN') {
5730 CRM_Core_Error::deprecatedFunctionWarning('Fix search input params');
6e23130a
EM
5731 if (($queryString = CRM_Core_DAO::createSqlFilter($field, array($op => $value), $dataType)) != FALSE) {
5732 return $queryString;
5733 }
5734 }
ccabbc44 5735 throw new CRM_Core_Exception(ts('Failed to interpret input for search'));
461c9a60 5736 }
c0b154eb 5737
6a488035 5738 $value = CRM_Utils_Type::escape($value, $dataType);
a75c13cc 5739 // if we don't have a dataType we should assume
85bdc94e 5740 if ($dataType == 'String' || $dataType == 'Text') {
2fc64082 5741 $value = "'" . $value . "'";
6a488035
TO
5742 }
5743 return "$clause $value";
5744 }
5745 }
5746
86538308
EM
5747 /**
5748 * @param bool $reset
5749 *
5750 * @return array
5751 */
00be9182 5752 public function openedSearchPanes($reset = FALSE) {
6a488035
TO
5753 if (!$reset || empty($this->_whereTables)) {
5754 return self::$_openedPanes;
5755 }
5756
5757 // pane name to table mapper
5758 $panesMapper = array(
5759 ts('Contributions') => 'civicrm_contribution',
5760 ts('Memberships') => 'civicrm_membership',
5761 ts('Events') => 'civicrm_participant',
5762 ts('Relationships') => 'civicrm_relationship',
5763 ts('Activities') => 'civicrm_activity',
5764 ts('Pledges') => 'civicrm_pledge',
5765 ts('Cases') => 'civicrm_case',
5766 ts('Grants') => 'civicrm_grant',
5767 ts('Address Fields') => 'civicrm_address',
5768 ts('Notes') => 'civicrm_note',
5769 ts('Change Log') => 'civicrm_log',
974ae237 5770 ts('Mailings') => 'civicrm_mailing',
6a488035 5771 );
066b4d4a 5772 CRM_Contact_BAO_Query_Hook::singleton()->getPanesMapper($panesMapper);
6a488035
TO
5773
5774 foreach (array_keys($this->_whereTables) as $table) {
5775 if ($panName = array_search($table, $panesMapper)) {
5776 self::$_openedPanes[$panName] = TRUE;
5777 }
5778 }
5779
5780 return self::$_openedPanes;
5781 }
5782
86538308
EM
5783 /**
5784 * @param $operator
5785 */
00be9182 5786 public function setOperator($operator) {
6a488035
TO
5787 $validOperators = array('AND', 'OR');
5788 if (!in_array($operator, $validOperators)) {
5789 $operator = 'AND';
5790 }
5791 $this->_operator = $operator;
5792 }
5793
86538308
EM
5794 /**
5795 * @return string
5796 */
00be9182 5797 public function getOperator() {
6a488035
TO
5798 return $this->_operator;
5799 }
5800
86538308
EM
5801 /**
5802 * @param $from
5803 * @param $where
5804 * @param $having
5805 */
00be9182 5806 public function filterRelatedContacts(&$from, &$where, &$having) {
2bedfb3f
HA
5807 if (!isset(Civi::$statics[__CLASS__]['related_contacts_filter'])) {
5808 Civi::$statics[__CLASS__]['related_contacts_filter'] = array();
5809 }
5810 $_rTempCache =& Civi::$statics[__CLASS__]['related_contacts_filter'];
68f8975b 5811 // since there only can be one instance of this filter in every query
2bedfb3f
HA
5812 // skip if filter has already applied
5813 foreach ($_rTempCache as $acache) {
68f8975b
HA
5814 foreach ($acache['queries'] as $aqcache) {
5815 if (strpos($from, $aqcache['from']) !== FALSE) {
5816 $having = NULL;
5817 return;
5818 }
2bedfb3f
HA
5819 }
5820 }
5821 $arg_sig = sha1("$from $where $having");
5822 if (isset($_rTempCache[$arg_sig])) {
5823 $cache = $_rTempCache[$arg_sig];
5824 }
5825 else {
6a488035
TO
5826 // create temp table with contact ids
5827 $tableName = CRM_Core_DAO::createTempTableName('civicrm_transform', TRUE);
68f8975b 5828
6a488035
TO
5829 $sql = "CREATE TEMPORARY TABLE $tableName ( contact_id int primary key) ENGINE=HEAP";
5830 CRM_Core_DAO::executeQuery($sql);
5831
5832 $sql = "
5833REPLACE INTO $tableName ( contact_id )
5834SELECT contact_a.id
5835 $from
5836 $where
5837 $having
5838";
5839 CRM_Core_DAO::executeQuery($sql);
5840
68f8975b
HA
5841 $cache = array('tableName' => $tableName, 'queries' => array());
5842 $_rTempCache[$arg_sig] = $cache;
5843 }
5844 // upsert the query depending on relationship type
5845 if (isset($cache['queries'][$this->_displayRelationshipType])) {
5846 $qcache = $cache['queries'][$this->_displayRelationshipType];
5847 }
5848 else {
5849 $tableName = $cache['tableName'];
5850 $qcache = array(
5851 "from" => "",
5852 "where" => "",
5853 );
6a488035 5854 $rTypes = CRM_Core_PseudoConstant::relationshipType();
6a488035
TO
5855 if (is_numeric($this->_displayRelationshipType)) {
5856 $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType]['label_a_b'];
68f8975b 5857 $qcache['from'] = "
6a488035
TO
5858INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )
5859INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )
5860";
68f8975b 5861 $qcache['where'] = "
6a488035
TO
5862WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}
5863AND displayRelType.is_active = 1
5864";
5865 }
5866 else {
5867 list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType);
5868 if ($dirOne == 'a') {
5869 $relationshipTypeLabel = $rTypes[$relType]['label_a_b'];
68f8975b 5870 $qcache['from'] .= "
6a488035
TO
5871INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )
5872INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )
5873";
5874 }
5875 else {
5876 $relationshipTypeLabel = $rTypes[$relType]['label_b_a'];
68f8975b 5877 $qcache['from'] .= "
6a488035
TO
5878INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )
5879INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )
5880";
5881 }
68f8975b 5882 $qcache['where'] = "
6a488035
TO
5883WHERE displayRelType.relationship_type_id = $relType
5884AND displayRelType.is_active = 1
5885";
5886 }
68f8975b
HA
5887 $qcache['relTypeLabel'] = $relationshipTypeLabel;
5888 $_rTempCache[$arg_sig]['queries'][$this->_displayRelationshipType] = $qcache;
6a488035 5889 }
68f8975b
HA
5890 $qillMessage = ts('Contacts with a Relationship Type of: ');
5891 $iqill = $qillMessage . "'" . $qcache['relTypeLabel'] . "'";
5892 if (!is_array($this->_qill[0]) || !in_array($iqill, $this->_qill[0])) {
5893 $this->_qill[0][] = $iqill;
5894 }
5895 if (strpos($from, $qcache['from']) === FALSE) {
6a488035
TO
5896 // lets replace all the INNER JOIN's in the $from so we dont exclude other data
5897 // this happens when we have an event_type in the quert (CRM-7969)
5898 $from = str_replace("INNER JOIN", "LEFT JOIN", $from);
68f8975b
HA
5899 $from .= $qcache['from'];
5900 $where = $qcache['where'];
2bedfb3f
HA
5901 if (!empty($this->_permissionWhereClause)) {
5902 $where .= "AND $this->_permissionWhereClause";
5903 }
6a488035
TO
5904 }
5905
5906 $having = NULL;
5907 }
5908
86538308 5909 /**
30415e03 5910 * See CRM-19811 for why this is database hurty without apparent benefit.
5911 *
86538308
EM
5912 * @param $op
5913 *
5914 * @return bool
5915 */
5c7dad85 5916 public static function caseImportant($op) {
6a488035
TO
5917 return
5918 in_array($op, array('LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ? FALSE : TRUE;
5919 }
5920
86538308
EM
5921 /**
5922 * @param $returnProperties
5923 * @param $prefix
5924 *
5925 * @return bool
5926 */
5c7dad85
TO
5927 public static function componentPresent(&$returnProperties, $prefix) {
5928 foreach ($returnProperties as $name => $dontCare) {
6a488035
TO
5929 if (substr($name, 0, strlen($prefix)) == $prefix) {
5930 return TRUE;
5931 }
5932 }
5933 return FALSE;
5934 }
5935
5936 /**
c037736a 5937 * Builds the necessary structures for all fields that are similar to option value look-ups.
6a488035 5938 *
5a4f6742
CW
5939 * @param string $name
5940 * the name of the field.
5941 * @param string $op
5942 * the sql operator, this function should handle ALL SQL operators.
5943 * @param string $value
5944 * depends on the operator and who's calling the query builder.
5945 * @param int $grouping
5946 * the index where to place the where clause.
3bdf1f3a 5947 * @param string $daoName
5948 * DAO Name.
5a4f6742
CW
5949 * @param array $field
5950 * an array that contains various properties of the field identified by $name.
5951 * @param string $label
5952 * The label for this field element.
5953 * @param string $dataType
5954 * The data type for this element.
837c4660 5955 * @param bool $useIDsOnly
6a488035 5956 */
59f4c9ee 5957 public function optionValueQuery(
6a488035
TO
5958 $name,
5959 $op,
5960 $value,
5961 $grouping,
9b183452 5962 $daoName = NULL,
6a488035
TO
5963 $field,
5964 $label,
a30e57e0
DL
5965 $dataType = 'String',
5966 $useIDsOnly = FALSE
6a488035 5967 ) {
bd3728fb 5968
5c7dad85
TO
5969 $pseudoFields = array(
5970 'email_greeting',
5971 'postal_greeting',
5972 'addressee',
5973 'gender_id',
5974 'prefix_id',
5975 'suffix_id',
21dfd5f5 5976 'communication_style_id',
5c7dad85 5977 );
d9ab802d 5978
9df8a11d
DL
5979 if ($useIDsOnly) {
5980 list($tableName, $fieldName) = explode('.', $field['where'], 2);
5981 if ($tableName == 'civicrm_contact') {
5982 $wc = "contact_a.$fieldName";
5983 }
8b695e91 5984 else {
f61d1b83
AS
5985 // Special handling for on_hold, so that we actually use the 'where'
5986 // property in order to limit the query by the on_hold status of the email,
5987 // instead of using email.id which would be nonsensical.
5988 if ($field['name'] == 'on_hold') {
5989 $wc = "{$field['where']}";
5990 }
5991 else {
5992 $wc = "$tableName.id";
5993 }
8b695e91 5994 }
9df8a11d
DL
5995 }
5996 else {
b7aa36b6 5997 CRM_Core_Error::deprecatedFunctionWarning('pass $ids to this method');
5998 $wc = "{$field['where']}";
9df8a11d 5999 }
d9ab802d 6000 if (in_array($name, $pseudoFields)) {
aa62b355 6001 if (!in_array($name, array('gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'))) {
d9ab802d
PJ
6002 $wc = "contact_a.{$name}_id";
6003 }
6004 $dataType = 'Positive';
6005 $value = (!$value) ? 0 : $value;
6006 }
728eefa0 6007 if ($name == "world_region") {
6008 $field['name'] = $name;
6009 }
d9ab802d 6010
9b183452 6011 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($daoName, $field['name'], $value, $op);
6012 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal));
d9ab802d 6013 $this->_where[$grouping][] = self::buildClause($wc, $op, $value, $dataType);
6a488035
TO
6014 }
6015
a30e57e0 6016 /**
100fef9d 6017 * Check and explode a user defined numeric string into an array
6a488035
TO
6018 * this was the protocol used by search builder in the old old days before we had
6019 * super nice js widgets to do the hard work
6020 *
4e6072d5 6021 * @param string $string
77c5b619
TO
6022 * @param string $dataType
6023 * The dataType we should check for the values, default integer.
6a488035 6024 *
72b3a70c
CW
6025 * @return bool|array
6026 * false if string does not match the pattern
6027 * array of numeric values if string does match the pattern
6a488035 6028 */
00be9182 6029 public static function parseSearchBuilderString($string, $dataType = 'Integer') {
6a488035
TO
6030 $string = trim($string);
6031 if (substr($string, 0, 1) != '(' || substr($string, -1, 1) != ')') {
6032 Return FALSE;
6033 }
6034
6035 $string = substr($string, 1, -1);
6036 $values = explode(',', $string);
6037 if (empty($values)) {
6038 return FALSE;
6039 }
6040
6041 $returnValues = array();
6042 foreach ($values as $v) {
5c7dad85 6043 if ($dataType == 'Integer' && !is_numeric($v)) {
6a488035
TO
6044 return FALSE;
6045 }
4c9b6178 6046 elseif ($dataType == 'String' && !is_string($v)) {
6a488035
TO
6047 return FALSE;
6048 }
6049 $returnValues[] = trim($v);
6050 }
6051
6052 if (empty($returnValues)) {
6053 return FALSE;
6054 }
6055
6056 return $returnValues;
6057 }
6a488035 6058
d9ab802d 6059 /**
100fef9d 6060 * Convert the pseudo constants id's to their names
837c4660 6061 *
59f4c9ee 6062 * @param CRM_Core_DAO $dao
837c4660 6063 * @param bool $return
a5e77b37 6064 * @param bool $usedForAPI
837c4660 6065 *
59f4c9ee 6066 * @return array|NULL
d9ab802d 6067 */
a5e77b37 6068 public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) {
d9ab802d 6069 if (empty($this->_pseudoConstantsSelect)) {
59f4c9ee 6070 return NULL;
d9ab802d 6071 }
d9ab802d
PJ
6072 $values = array();
6073 foreach ($this->_pseudoConstantsSelect as $key => $value) {
a7488080 6074 if (!empty($this->_pseudoConstantsSelect[$key]['sorting'])) {
d9ab802d
PJ
6075 continue;
6076 }
6077
9db71ff2 6078 if (is_object($dao) && property_exists($dao, $value['idCol'])) {
6ef04c72 6079 $val = $dao->{$value['idCol']};
3875e6b6 6080 if ($key == 'groups') {
6081 $dao->groups = $this->convertGroupIDStringToLabelString($dao, $val);
86ab13b7 6082 continue;
3875e6b6 6083 }
d9ab802d
PJ
6084
6085 if (CRM_Utils_System::isNull($val)) {
6086 $dao->$key = NULL;
d9ab802d 6087 }
86ab13b7 6088 elseif (!empty($value['pseudoconstant'])) {
6089 // If pseudoconstant is set that is kind of defacto for 'we have a bit more info about this'
6090 // and we can use the metadata to figure it out.
6091 // ideally this bit of IF will absorb & replace all the rest in time as we move to
6092 // more metadata based choices.
6093 if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
6094 $dbValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($val, CRM_Core_DAO::VALUE_SEPARATOR));
6095 foreach ($dbValues as $pseudoValue) {
6096 $convertedValues[] = CRM_Core_PseudoConstant::getLabel($value['bao'], $value['idCol'], $pseudoValue);
6097 }
6098
6099 $dao->$key = ($usedForAPI) ? $convertedValues : implode(', ', $convertedValues);
6100 $realFieldName = CRM_Utils_Array::value('field_name', $this->_pseudoConstantsSelect[$key]);
6101 if ($usedForAPI && $realFieldName) {
6102 // normally we would see 2 fields returned for pseudoConstants. An exception is
6103 // preferred_communication_method where there is no id-variant.
6104 // For the api we prioritise getting the real data returned.
6105 // over the resolved version
6106 $dao->$realFieldName = $dbValues;
6107 }
6108
6109 }
6110 else {
6111 // This is basically the same as the default but since we have the bao we can use
6112 // a cached function.
6113 $dao->$key = CRM_Core_PseudoConstant::getLabel($value['bao'], $value['idCol'], $val);
6114 }
6115 }
b719f000 6116 elseif ($baoName = CRM_Utils_Array::value('bao', $value, NULL)) {
827659b2 6117 //preserve id value
6118 $idColumn = "{$key}_id";
6119 $dao->$idColumn = $val;
b105067c 6120
6121 if ($key == 'state_province_name') {
144d0efe 6122 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant::stateProvince($val);
b105067c 6123 }
6124 else {
6ef04c72 6125 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant::getLabel($baoName, $value['pseudoField'], $val);
b105067c 6126 }
d9ab802d
PJ
6127 }
6128 elseif ($value['pseudoField'] == 'state_province_abbreviation') {
6129 $dao->$key = CRM_Core_PseudoConstant::stateProvinceAbbreviation($val);
6130 }
86ab13b7 6131 // @todo handle this in the section above for pseudoconstants.
348b1fda 6132 elseif (in_array($value['pseudoField'], array('participant_role_id', 'participant_role'))) {
3af96592 6133 // @todo define bao on this & merge into the above condition.
348b1fda 6134 $viewValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $val);
6135
6136 if ($value['pseudoField'] == 'participant_role') {
6137 $pseudoOptions = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'role_id');
6138 foreach ($viewValues as $k => $v) {
6139 $viewValues[$k] = $pseudoOptions[$v];
6140 }
62fd7fe8 6141 }
a5e77b37 6142 $dao->$key = ($usedForAPI && count($viewValues) > 1) ? $viewValues : implode(', ', $viewValues);
62fd7fe8 6143 }
d9ab802d
PJ
6144 else {
6145 $labels = CRM_Core_OptionGroup::values($value['pseudoField']);
6146 $dao->$key = $labels[$val];
6147 }
b719f000
PJ
6148
6149 // return converted values in array format
6150 if ($return) {
6151 if (strpos($key, '-') !== FALSE) {
6152 $keyVal = explode('-', $key);
6153 $current = &$values;
6154 $lastElement = array_pop($keyVal);
6155 foreach ($keyVal as $v) {
6156 if (!array_key_exists($v, $current)) {
6157 $current[$v] = array();
6158 }
6159 $current = &$current[$v];
6160 }
6161 $current[$lastElement] = $dao->$key;
6162 }
6163 else {
6164 $values[$key] = $dao->$key;
6165 }
6166 }
d9ab802d
PJ
6167 }
6168 }
86ab13b7 6169 if (!$usedForAPI) {
6170 foreach (array(
6171 'gender_id' => 'gender',
6172 'prefix_id' => 'individual_prefix',
6173 'suffix_id' => 'individual_suffix',
6174 'communication_style_id' => 'communication_style',
6175 ) as $realField => $labelField) {
6176 // This is a temporary routine for handling these fields while
6177 // we figure out how to handled them based on metadata in
6178 /// export and search builder. CRM-19815, CRM-19830.
f02f3e59 6179 if (isset($dao->$realField) && is_numeric($dao->$realField) && isset($dao->$labelField)) {
86ab13b7 6180 $dao->$realField = $dao->$labelField;
6181 }
6182 }
6183 }
d9ab802d
PJ
6184 return $values;
6185 }
6186
837c4660 6187 /**
fe482240 6188 * Include pseudo fields LEFT JOIN.
77c5b619 6189 * @param string|array $sort can be a object or string
d9ab802d 6190 *
59f4c9ee 6191 * @return array|NULL
d9ab802d 6192 */
00be9182 6193 public function includePseudoFieldsJoin($sort) {
d9ab802d 6194 if (!$sort || empty($this->_pseudoConstantsSelect)) {
59f4c9ee 6195 return NULL;
d9ab802d
PJ
6196 }
6197 $sort = is_string($sort) ? $sort : $sort->orderBy();
6198 $present = array();
6199
6200 foreach ($this->_pseudoConstantsSelect as $name => $value) {
a7488080 6201 if (!empty($value['table'])) {
d9ab802d
PJ
6202 $regex = "/({$value['table']}\.|{$name})/";
6203 if (preg_match($regex, $sort)) {
6204 $this->_elemnt[$value['element']] = 1;
6205 $this->_select[$value['element']] = $value['select'];
6206 $this->_pseudoConstantsSelect[$name]['sorting'] = 1;
6207 $present[$value['table']] = $value['join'];
6208 }
6209 }
6210 }
6211 $presentSimpleFrom = $present;
6212
6213 if (array_key_exists('civicrm_worldregion', $this->_whereTables) &&
5c7dad85
TO
6214 array_key_exists('civicrm_country', $presentSimpleFrom)
6215 ) {
d9ab802d
PJ
6216 unset($presentSimpleFrom['civicrm_country']);
6217 }
6218 if (array_key_exists('civicrm_worldregion', $this->_tables) &&
5c7dad85
TO
6219 array_key_exists('civicrm_country', $present)
6220 ) {
d9ab802d
PJ
6221 unset($present['civicrm_country']);
6222 }
6223
6224 $presentClause = $presentSimpleFromClause = NULL;
6225 if (!empty($present)) {
6226 $presentClause = implode(' ', $present);
6227 }
6228 if (!empty($presentSimpleFrom)) {
6229 $presentSimpleFromClause = implode(' ', $presentSimpleFrom);
6230 }
6231
6232 $this->_fromClause = $this->_fromClause . $presentClause;
6233 $this->_simpleFromClause = $this->_simpleFromClause . $presentSimpleFromClause;
6234
6235 return array($presentClause, $presentSimpleFromClause);
6236 }
9ab34172 6237
0e2e76cf
EM
6238 /**
6239 * Build qill for field.
6240 *
6241 * Qill refers to the query detail visible on the UI.
6242 *
bb05da0c 6243 * @param string $daoName
6244 * @param string $fieldName
6245 * @param mixed $fieldValue
6246 * @param string $op
6247 * @param array $pseudoExtraParam
6248 * @param int $type
6249 * Type of the field per CRM_Utils_Type
0e2e76cf
EM
6250 *
6251 * @return array
6252 */
bb05da0c 6253 public static function buildQillForFieldValue(
6254 $daoName,
6255 $fieldName,
6256 $fieldValue,
6257 $op,
6258 $pseudoExtraParam = array(),
6259 $type = CRM_Utils_Type::T_STRING
6260 ) {
afa0b07c 6261 $qillOperators = CRM_Core_SelectValues::getSearchBuilderOperators();
97c1cf60 6262
3086e282 6263 //API usually have fieldValue format as array(operator => array(values)),
6264 //so we need to separate operator out of fieldValue param
6265 if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
6266 $op = key($fieldValue);
6267 $fieldValue = $fieldValue[$op];
6268 }
6269
9b183452 6270 // if Operator chosen is NULL/EMPTY then
6271 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
6272 return array(CRM_Utils_Array::value($op, $qillOperators, $op), '');
6273 }
6274
9ab34172 6275 if ($fieldName == 'activity_type_id') {
bb05da0c 6276 $pseudoOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
9ab34172 6277 }
9b183452 6278 elseif ($fieldName == 'country_id') {
e4001cf0 6279 $pseudoOptions = CRM_Core_PseudoConstant::country();
9b183452 6280 }
6281 elseif ($fieldName == 'county_id') {
e4001cf0 6282 $pseudoOptions = CRM_Core_PseudoConstant::county();
9b183452 6283 }
728eefa0 6284 elseif ($fieldName == 'world_region') {
e4001cf0 6285 $pseudoOptions = CRM_Core_PseudoConstant::worldRegion();
728eefa0 6286 }
97c1cf60 6287 elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') {
450fcddb
JP
6288 $checkPermission = CRM_Utils_Array::value('check_permission', $pseudoExtraParam, TRUE);
6289 $pseudoOptions = CRM_Event_BAO_Event::getEvents(0, $fieldValue, TRUE, $checkPermission, TRUE);
97c1cf60 6290 }
6ffab5b7 6291 elseif ($fieldName == 'contribution_product_id') {
bb05da0c 6292 $pseudoOptions = CRM_Contribute_PseudoConstant::products();
6ffab5b7 6293 }
e8401ae2 6294 elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
bb05da0c 6295 $pseudoOptions = CRM_Core_PseudoConstant::group();
e8401ae2 6296 }
3086e282 6297 elseif ($daoName == 'CRM_Batch_BAO_EntityBatch' && $fieldName == 'batch_id') {
6298 $pseudoOptions = CRM_Contribute_PseudoConstant::batch();
6299 }
efb88612 6300 elseif ($daoName) {
bb05da0c 6301 $pseudoOptions = CRM_Core_PseudoConstant::get($daoName, $fieldName, $pseudoExtraParam);
97c1cf60 6302 }
9ab34172 6303
efb88612 6304 if (is_array($fieldValue)) {
9ab34172 6305 $qillString = array();
bb05da0c 6306 if (!empty($pseudoOptions)) {
efb88612 6307 foreach ((array) $fieldValue as $val) {
bb05da0c 6308 $qillString[] = CRM_Utils_Array::value($val, $pseudoOptions, $val);
efb88612 6309 }
6310 $fieldValue = implode(', ', $qillString);
9ab34172 6311 }
efb88612 6312 else {
bb05da0c 6313 if ($type == CRM_Utils_Type::T_DATE) {
6314 foreach ($fieldValue as $index => $value) {
6315 $fieldValue[$index] = CRM_Utils_Date::customFormat($value);
6316 }
6317 }
6318 $separator = ', ';
6319 // @todo - this is a bit specific (one operator).
6320 // However it is covered by a unit test so can be altered later with
6321 // some confidence.
6322 if ($op == 'BETWEEN') {
6323 $separator = ' AND ';
6324 }
6325 $fieldValue = implode($separator, $fieldValue);
9ab34172 6326 }
9ab34172 6327 }
bb05da0c 6328 elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) {
6329 $fieldValue = $pseudoOptions[$fieldValue];
6330 }
6331 elseif ($type === CRM_Utils_Type::T_DATE) {
6332 $fieldValue = CRM_Utils_Date::customFormat($fieldValue);
9ab34172 6333 }
afa0b07c 6334
6335 return array(CRM_Utils_Array::value($op, $qillOperators, $op), $fieldValue);
9ab34172 6336 }
96025800 6337
836eb043 6338 /**
6339 * Alter value to reflect wildcard settings.
6340 *
6341 * The form will have tried to guess whether this is a good field to wildcard but there is
6342 * also a site-wide setting that specifies whether it is OK to append the wild card to the beginning
6343 * or only the end of the string
6344 *
6345 * @param bool $wildcard
6346 * This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard.
6347 * @param string $op
6348 * Generally '=' or 'LIKE'.
6349 * @param string $value
6350 * The search string.
6351 *
6352 * @return string
6353 */
0b38e8f1 6354 public static function getWildCardedValue($wildcard, $op, $value) {
836eb043 6355 if ($wildcard && $op == 'LIKE') {
6356 if (CRM_Core_Config::singleton()->includeWildCardInName && (substr($value, 0, 1) != '%')) {
6357 return "%$value%";
6358 }
6359 else {
6360 return "$value%";
6361 }
6362 }
6363 else {
6364 return "$value";
6365 }
6366 }
6367
0b38e8f1 6368 /**
6369 * Process special fields of Search Form in OK (Operator in Key) format
6370 *
6371 * @param array $formValues
6372 * @param array $specialFields
6373 * Special params to be processed
6374 * @param array $changeNames
6375 * Array of fields whose name should be changed
6376 */
6377 public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = array()) {
3086e282 6378 // Array of special fields whose value are considered only for NULL or EMPTY operators
6379 $nullableFields = array('contribution_batch_id');
6380
0b38e8f1 6381 foreach ($specialFields as $element) {
6382 $value = CRM_Utils_Array::value($element, $formValues);
6383 if ($value) {
6384 if (is_array($value)) {
a8d8984f 6385 if (in_array($element, array_keys($changeNames))) {
0b38e8f1 6386 unset($formValues[$element]);
6387 $element = $changeNames[$element];
6388 }
6389 $formValues[$element] = array('IN' => $value);
6390 }
3086e282 6391 elseif (in_array($value, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
6392 $formValues[$element] = array($value => 1);
6393 }
6394 elseif (!in_array($element, $nullableFields)) {
0b38e8f1 6395 // if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
6396 $isWilcard = strstr($value, '%') ? FALSE : CRM_Core_Config::singleton()->includeWildCardInName;
0a63f5e5 6397 $formValues[$element] = array('LIKE' => self::getWildCardedValue($isWilcard, 'LIKE', $value));
0b38e8f1 6398 }
6399 }
6400 }
6401 }
6402
db54ba06
TO
6403 /**
6404 * Parse and assimilate the various sort options.
6405 *
6406 * Side-effect: if sorting on a common column from a related table (`city`, `postal_code`,
6407 * `email`), the related table may be joined automatically.
6408 *
6409 * At time of writing, this code is deeply flawed and should be rewritten. For the moment,
6410 * it's been extracted to a standalone function.
6411 *
6412 * @param string|CRM_Utils_Sort $sort
6413 * The order by string.
6414 * @param bool $sortByChar
6415 * If true returns the distinct array of first characters for search results.
6416 * @param null $sortOrder
6417 * Who knows? Hu knows. He who knows Hu knows who.
6418 * @param string $additionalFromClause
6419 * Should be clause with proper joins, effective to reduce where clause load.
6420 * @return array
6421 * list(string $orderByClause, string $additionalFromClause).
6422 */
6423 protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause) {
6103c166 6424 $order = NULL;
252f0858 6425 $orderByArray = array();
db54ba06
TO
6426 $config = CRM_Core_Config::singleton();
6427 if ($config->includeOrderByClause ||
6428 isset($this->_distinctComponentClause)
6429 ) {
6430 if ($sort) {
6431 if (is_string($sort)) {
6432 $orderBy = $sort;
6433 }
6434 else {
6435 $orderBy = trim($sort->orderBy());
6436 }
6437 // Deliberately remove the backticks again, as they mess up the evil
6438 // string munging below. This balanced by re-escaping before use.
6439 $orderBy = str_replace('`', '', $orderBy);
6440
6441 if (!empty($orderBy)) {
6442 // this is special case while searching for
6443 // change log CRM-1718
6444 if (preg_match('/sort_name/i', $orderBy)) {
6445 $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy);
6446 }
6447
6448 $order = " ORDER BY $orderBy";
6449
6450 if ($sortOrder) {
6451 $order .= " $sortOrder";
6452 }
6453
6454 // always add contact_a.id to the ORDER clause
6455 // so the order is deterministic
6456 if (strpos('contact_a.id', $order) === FALSE) {
6457 $order .= ", contact_a.id";
6458 }
6459 }
6460 }
6461 elseif ($sortByChar) {
252f0858 6462 $orderByArray = array("UPPER(LEFT(contact_a.sort_name, 1)) asc");
db54ba06
TO
6463 }
6464 else {
0ad26017 6465 $order = " ORDER BY contact_a.sort_name ASC, contact_a.id";
db54ba06
TO
6466 }
6467 }
252f0858 6468 if (!$order && empty($orderByArray)) {
6469 return array($order, $additionalFromClause);
6470 }
6471 // Remove this here & add it at the end for simplicity.
6472 $order = trim(str_replace('ORDER BY', '', $order));
db54ba06
TO
6473
6474 // hack for order clause
9d5c7f14 6475 if (!empty($orderByArray)) {
6476 $order = implode(', ', $orderByArray);
6477 }
6478 else {
6479 $orderByArray = explode(',', $order);
6480 }
6481 foreach ($orderByArray as $orderByClause) {
6482 $orderByClauseParts = explode(' ', trim($orderByClause));
6483 $field = $orderByClauseParts[0];
6484 $direction = isset($orderByClauseParts[1]) ? $orderByClauseParts[1] : 'asc';
252f0858 6485
252f0858 6486 switch ($field) {
6487 case 'city':
6488 case 'postal_code':
6489 $this->_tables["civicrm_address"] = $this->_whereTables["civicrm_address"] = 1;
6490 $order = str_replace($field, "civicrm_address.{$field}", $order);
6491 break;
6492
6493 case 'country':
6494 case 'state_province':
6495 $this->_tables["civicrm_{$field}"] = $this->_whereTables["civicrm_{$field}"] = 1;
6496 if (is_array($this->_returnProperties) && empty($this->_returnProperties)) {
6497 $additionalFromClause .= " LEFT JOIN civicrm_{$field} ON civicrm_{$field}.id = civicrm_address.{$field}_id";
6498 }
6499 $order = str_replace($field, "civicrm_{$field}.name", $order);
6500 break;
db54ba06 6501
252f0858 6502 case 'email':
6503 $this->_tables["civicrm_email"] = $this->_whereTables["civicrm_email"] = 1;
6504 $order = str_replace($field, "civicrm_email.{$field}", $order);
6505 break;
db54ba06 6506
252f0858 6507 default:
744050b0
JP
6508 $cfID = CRM_Core_BAO_CustomField::getKeyID($field);
6509 // add to cfIDs array if not present
6510 if (!empty($cfID) && !array_key_exists($cfID, $this->_cfIDs)) {
6511 $this->_cfIDs[$cfID] = array();
6512 $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE, $this->_locationSpecificCustomFields);
6513 $this->_customQuery->query();
6514 $this->_select = array_merge($this->_select, $this->_customQuery->_select);
6515 $this->_tables = array_merge($this->_tables, $this->_customQuery->_tables);
6516 }
9d5c7f14 6517 foreach ($this->_pseudoConstantsSelect as $key => $pseudoConstantMetadata) {
6518 // By replacing the join to the option value table with the mysql construct
6519 // ORDER BY field('contribution_status_id', 2,1,4)
6520 // we can remove a join. In the case of the option value join it is
6521 /// a join known to cause slow queries.
6522 // @todo cover other pseudoconstant types. Limited to option group ones in the
6523 // first instance for scope reasons. They require slightly different handling as the column (label)
6524 // is not declared for them.
6525 // @todo so far only integer fields are being handled. If we add string fields we need to look at
6526 // escaping.
6527 if (isset($pseudoConstantMetadata['pseudoconstant'])
6528 && isset($pseudoConstantMetadata['pseudoconstant']['optionGroupName'])
6529 && $field === CRM_Utils_Array::value('optionGroupName', $pseudoConstantMetadata['pseudoconstant'])
6530 ) {
6531 $sortedOptions = $pseudoConstantMetadata['bao']::buildOptions($pseudoConstantMetadata['pseudoField'], NULL, array(
6532 'orderColumn' => 'label',
6533 ));
6534 $order = str_replace("$field $direction", "field({$pseudoConstantMetadata['pseudoField']}," . implode(',', array_keys($sortedOptions)) . ") $direction", $order);
6535 }
6536 //CRM-12565 add "`" around $field if it is a pseudo constant
6537 // This appears to be for 'special' fields like locations with appended numbers or hyphens .. maybe.
6538 if (!empty($pseudoConstantMetadata['element']) && $pseudoConstantMetadata['element'] == $field) {
252f0858 6539 $order = str_replace($field, "`{$field}`", $order);
db54ba06 6540 }
252f0858 6541 }
db54ba06
TO
6542 }
6543 }
6544
9d5c7f14 6545 $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode);
6546 $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode);
6547
db54ba06
TO
6548 // The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
6549 // clause. But this magic helper which forgivingly reescapes ORDER BY.
6550 // Note: $sortByChar implies that $order was hard-coded/trusted, so it can do funky things.
9d5c7f14 6551 if ($sortByChar) {
6552 return array(' ORDER BY ' . $order, $additionalFromClause);
6553 }
6554 if ($order) {
252f0858 6555 $order = CRM_Utils_Type::escape($order, 'MysqlOrderBy');
6556 return array(' ORDER BY ' . $order, $additionalFromClause);
db54ba06 6557 }
db54ba06
TO
6558 }
6559
3875e6b6 6560 /**
6561 * Convert a string of group IDs to a string of group labels.
6562 *
6563 * The original string may include duplicates and groups the user does not have
6564 * permission to see.
6565 *
6566 * @param CRM_Core_DAO $dao
6567 * @param string $val
6568 *
6569 * @return string
6570 */
6571 public function convertGroupIDStringToLabelString(&$dao, $val) {
6572 $groupIDs = explode(',', $val);
3875e6b6 6573 // Note that groups that the user does not have permission to will be excluded (good).
6d054a8e 6574 $groups = array_intersect_key(CRM_Core_PseudoConstant::group(), array_flip($groupIDs));
3875e6b6 6575 return implode(', ', $groups);
962f4484 6576
6577 }
6578
6579 /**
6580 * Set the qill and where properties for a field.
6581 *
6582 * This function is intended as a short-term function to encourage refactoring
6583 * & re-use - but really we should just have less special-casing.
6584 *
6585 * @param string $name
6586 * @param string $op
6587 * @param string|array $value
6588 * @param string $grouping
6589 * @param string $field
6590 */
6591 public function setQillAndWhere($name, $op, $value, $grouping, $field) {
6592 $this->_where[$grouping][] = self::buildClause("contact_a.{$name}", $op, $value);
6593 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op);
6594 $this->_qill[$grouping][] = ts("%1 %2 %3", array(
6595 1 => $field['title'],
6596 2 => $qillop,
6597 3 => $qillVal,
6598 ));
3875e6b6 6599 }
6600
86ab13b7 6601 /**
6602 * Has the pseudoconstant of the field been requested.
6603 *
6604 * For example if the field is payment_instrument_id then it
6605 * has been requested if either payment_instrument_id or payment_instrument
6606 * have been requested. Payment_instrument is the option groun name field value.
6607 *
6608 * @param array $field
7bd16b05 6609 * @param string $fieldName
6610 * The unique name of the field - ie. the one it will be aliased to in the query.
86ab13b7 6611 *
6612 * @return bool
6613 */
7bd16b05 6614 private function pseudoConstantNameIsInReturnProperties($field, $fieldName = NULL) {
86ab13b7 6615 if (!isset($field['pseudoconstant']['optionGroupName'])) {
6616 return FALSE;
6617 }
86ab13b7 6618
6619 if (CRM_Utils_Array::value($field['pseudoconstant']['optionGroupName'], $this->_returnProperties)) {
6620 return TRUE;
6621 }
7bd16b05 6622 if (CRM_Utils_Array::value($fieldName, $this->_returnProperties)) {
6623 return TRUE;
6624 }
6625 // Is this still required - the above goes off the unique name. Test with things like
6626 // communication_prefferences & prefix_id.
86ab13b7 6627 if (CRM_Utils_Array::value($field['name'], $this->_returnProperties)) {
6628 return TRUE;
6629 }
6630 return FALSE;
6631 }
6632
f946d152 6633 /**
6634 * Get Select Clause.
6635 *
6636 * @return string
6637 */
6638 public function getSelect() {
6639 $select = "SELECT ";
6640 if (isset($this->_distinctComponentClause)) {
6641 $select .= "{$this->_distinctComponentClause}, ";
6642 }
6643 $select .= implode(', ', $this->_select);
6644 return $select;
6645 }
6646
e7830dbe 6647}