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