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