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