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