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