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