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