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