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