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