legacy support for group and tag
[civicrm-core.git] / CRM / Contact / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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
1335 list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup['group'][0];
1336
1337 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
1338 $this->_paramLookup['group'][0][1] = key($value);
1339 }
1340
1341 // make sure there is only one element
1342 // this is used when we are running under smog and need to know
1343 // how the contact was added (CRM-1203)
1344 $groups = (array) CRM_Utils_Array::value($this->_paramLookup['group'][0][1], $this->_paramLookup['group'][0][2], $this->_paramLookup['group'][0][2]);
1345 if ((count($this->_paramLookup['group']) == 1) &&
1346 (count($groups) == 1)
1347 ) {
1348 $groupId = $groups[0];
1349
1350 //check if group is saved search
1351 $group = new CRM_Contact_BAO_Group();
1352 $group->id = $groupId;
1353 $group->find(TRUE);
1354
1355 if (!isset($group->saved_search_id)) {
1356 $tbName = "`civicrm_group_contact-{$groupId}`";
1357 $this->_select['group_contact_id'] = "$tbName.id as group_contact_id";
1358 $this->_element['group_contact_id'] = 1;
1359 $this->_select['status'] = "$tbName.status as status";
1360 $this->_element['status'] = 1;
1361 }
1362 }
1363 $this->_useGroupBy = TRUE;
1364 }
1365 if ($this->_useDistinct && !isset($this->_distinctComponentClause)) {
1366 if (!($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY)) {
1367 // CRM-5954
1368 $this->_select['contact_id'] = 'contact_a.id as contact_id';
1369 $this->_useDistinct = FALSE;
1370 $this->_useGroupBy = TRUE;
1371 }
1372 }
1373
1374 $select = "SELECT ";
1375 if (isset($this->_distinctComponentClause)) {
1376 $select .= "{$this->_distinctComponentClause}, ";
1377 }
1378 $select .= implode(', ', $this->_select);
1379 $from = $this->_fromClause;
1380 }
1381
1382 $where = '';
1383 if (!empty($this->_whereClause)) {
1384 $where = "WHERE {$this->_whereClause}";
1385 }
1386
1387 if (!empty($this->_permissionWhereClause) && empty($this->_displayRelationshipType)) {
1388 if (empty($where)) {
1389 $where = "WHERE $this->_permissionWhereClause";
1390 }
1391 else {
1392 $where = "$where AND $this->_permissionWhereClause";
1393 }
1394 }
1395
1396 $having = '';
1397 if (!empty($this->_having)) {
1398 foreach ($this->_having as $havingSets) {
1399 foreach ($havingSets as $havingSet) {
1400 $havingValue[] = $havingSet;
1401 }
1402 }
1403 $having = ' HAVING ' . implode(' AND ', $havingValue);
1404 }
1405
1406 // if we are doing a transform, do it here
1407 // use the $from, $where and $having to get the contact ID
1408 if ($this->_displayRelationshipType) {
1409 $this->filterRelatedContacts($from, $where, $having);
1410 }
1411
1412 return array($select, $from, $where, $having);
1413 }
1414
1415 /**
1416 * Get where values from the parameters.
1417 *
1418 * @param string $name
1419 * @param mixed $grouping
1420 *
1421 * @return mixed
1422 */
1423 public function getWhereValues($name, $grouping) {
1424 $result = NULL;
1425 foreach ($this->_params as $values) {
1426 if ($values[0] == $name && $values[3] == $grouping) {
1427 return $values;
1428 }
1429 }
1430
1431 return $result;
1432 }
1433
1434 /**
1435 * Fix date values.
1436 *
1437 * @param bool $relative
1438 * @param string $from
1439 * @param string $to
1440 */
1441 public static function fixDateValues($relative, &$from, &$to) {
1442 if ($relative) {
1443 list($from, $to) = CRM_Utils_Date::getFromTo($relative, $from, $to);
1444 }
1445 }
1446
1447 /**
1448 * Convert values from form-appropriate to query-object appropriate.
1449 *
1450 * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax.
1451 * So, ideally we would convert all fields to look like
1452 * array(
1453 * 0 => $fieldName
1454 * // Set the operator for legacy reasons, but it is ignored
1455 * 1 => '='
1456 * // array in sql filter syntax
1457 * 2 => array('BETWEEN' => array(1,60),
1458 * 3 => null
1459 * 4 => null
1460 * );
1461 *
1462 * There are some examples of the syntax in
1463 * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship
1464 *
1465 * More notes at CRM_Core_DAO::createSQLFilter
1466 *
1467 * and a list of supported operators in CRM_Core_DAO
1468 *
1469 * @param array $formValues
1470 * @param int $wildcard
1471 * @param bool $useEquals
1472 *
1473 * @param string $apiEntity
1474 *
1475 * @param array $entityReferenceFields
1476 * Field names of any entity reference fields (which will need reformatting to IN syntax).
1477 *
1478 * @return array
1479 */
1480 public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL,
1481 $entityReferenceFields = array()) {
1482 $params = array();
1483 if (empty($formValues)) {
1484 return $params;
1485 }
1486
1487 foreach ($formValues as $id => $values) {
1488
1489 self::legacyConvertFormValues($id, $values);
1490
1491 if (self::isAlreadyProcessedForQueryFormat($values)) {
1492 $params[] = $values;
1493 continue;
1494 }
1495 if ($id == 'privacy') {
1496 if (is_array($formValues['privacy'])) {
1497 $op = !empty($formValues['privacy']['do_not_toggle']) ? '=' : '!=';
1498 foreach ($formValues['privacy'] as $key => $value) {
1499 if ($value) {
1500 $params[] = array($key, $op, $value, 0, 0);
1501 }
1502 }
1503 }
1504 }
1505 elseif ($id == 'email_on_hold') {
1506 if ($formValues['email_on_hold']['on_hold']) {
1507 $params[] = array('on_hold', '=', $formValues['email_on_hold']['on_hold'], 0, 0);
1508 }
1509 }
1510 elseif (substr($id, 0, 7) == 'custom_'
1511 && (
1512 substr($id, -9, 9) == '_relative'
1513 || substr($id, -5, 5) == '_from'
1514 || substr($id, -3, 3) == '_to'
1515 )
1516 ) {
1517 self::convertCustomDateRelativeFields($formValues, $params, $values, $id);
1518 }
1519 elseif (preg_match('/_date_relative$/', $id) ||
1520 $id == 'event_relative' ||
1521 $id == 'case_from_relative' ||
1522 $id == 'case_to_relative' ||
1523 $id == 'participant_relative'
1524 ) {
1525 if ($id == 'event_relative') {
1526 $fromRange = 'event_start_date_low';
1527 $toRange = 'event_end_date_high';
1528 }
1529 elseif ($id == 'participant_relative') {
1530 $fromRange = 'participant_register_date_low';
1531 $toRange = 'participant_register_date_high';
1532 }
1533 elseif ($id == 'case_from_relative') {
1534 $fromRange = 'case_from_start_date_low';
1535 $toRange = 'case_from_start_date_high';
1536 }
1537 elseif ($id == 'case_to_relative') {
1538 $fromRange = 'case_to_end_date_low';
1539 $toRange = 'case_to_end_date_high';
1540 }
1541 else {
1542 $dateComponent = explode('_date_relative', $id);
1543 $fromRange = "{$dateComponent[0]}_date_low";
1544 $toRange = "{$dateComponent[0]}_date_high";
1545 }
1546
1547 if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
1548 CRM_Contact_BAO_Query::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
1549 continue;
1550 }
1551 }
1552 elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
1553 FALSE)) {
1554 $params[] = array($id, 'IN', explode(',', $values), 0, 0);
1555 }
1556 else {
1557 $values = CRM_Contact_BAO_Query::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity);
1558
1559 if (!$values) {
1560 continue;
1561 }
1562 $params[] = $values;
1563 }
1564 }
1565 return $params;
1566 }
1567
1568 /**
1569 * Function to support legacy format for groups and tags.
1570 *
1571 * @param string $id
1572 * @param array|int $values
1573 *
1574 */
1575 public static function legacyConvertFormValues($id, &$values) {
1576 if (in_array($id, array('group', 'tag')) && is_array($values)) {
1577 // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
1578 // as per the recent Search fixes $values need to be in standard array(id1, id2) format
1579 $ids = array_keys($values, 1);
1580 if (count($ids) > 1 || (count($ids) == 1 && key($values) > 1)) {
1581 $values = $ids;
1582 }
1583 }
1584 }
1585
1586
1587 /**
1588 * Fix values from query from/to something no-one cared enough to document.
1589 *
1590 * @param int $id
1591 * @param array $values
1592 * @param int $wildcard
1593 * @param bool $useEquals
1594 *
1595 * @param string $apiEntity
1596 *
1597 * @return array|null
1598 */
1599 public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) {
1600 // skip a few search variables
1601 static $skipWhere = NULL;
1602 static $likeNames = NULL;
1603 $result = NULL;
1604 // Change camelCase EntityName to lowercase with underscores
1605 $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity);
1606
1607 if (CRM_Utils_System::isNull($values)) {
1608 return $result;
1609 }
1610
1611 if (!$skipWhere) {
1612 $skipWhere = array(
1613 'task',
1614 'radio_ts',
1615 'uf_group_id',
1616 'component_mode',
1617 'qfKey',
1618 'operator',
1619 'display_relationship_type',
1620 );
1621 }
1622
1623 if (in_array($id, $skipWhere) ||
1624 substr($id, 0, 4) == '_qf_' ||
1625 substr($id, 0, 7) == 'hidden_'
1626 ) {
1627 return $result;
1628 }
1629
1630 if ($apiEntity &&
1631 (substr($id, 0, strlen($apiEntity)) != $apiEntity) &&
1632 (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_')
1633 ) {
1634 $id = $apiEntity . '_' . $id;
1635 }
1636
1637 if (!$likeNames) {
1638 $likeNames = array('sort_name', 'email', 'note', 'display_name');
1639 }
1640
1641 // email comes in via advanced search
1642 // so use wildcard always
1643 if ($id == 'email') {
1644 $wildcard = 1;
1645 }
1646
1647 if (!$useEquals && in_array($id, $likeNames)) {
1648 $result = array($id, 'LIKE', $values, 0, 1);
1649 }
1650 elseif (is_string($values) && strpos($values, '%') !== FALSE) {
1651 $result = array($id, 'LIKE', $values, 0, 0);
1652 }
1653 elseif ($id == 'contact_type' ||
1654 (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO::acceptedSQLOperators(), TRUE))
1655 ) {
1656 $result = array($id, 'IN', $values, 0, $wildcard);
1657 }
1658 else {
1659 $result = array($id, '=', $values, 0, $wildcard);
1660 }
1661
1662 return $result;
1663 }
1664
1665 /**
1666 * Get the where clause for a single field.
1667 *
1668 * @param array $values
1669 */
1670 public function whereClauseSingle(&$values) {
1671 // do not process custom fields or prefixed contact ids or component params
1672 if (CRM_Core_BAO_CustomField::getKeyID($values[0]) ||
1673 (substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) ||
1674 (substr($values[0], 0, 13) == 'contribution_') ||
1675 (substr($values[0], 0, 6) == 'event_') ||
1676 (substr($values[0], 0, 12) == 'participant_') ||
1677 (substr($values[0], 0, 7) == 'member_') ||
1678 (substr($values[0], 0, 6) == 'grant_') ||
1679 (substr($values[0], 0, 7) == 'pledge_') ||
1680 (substr($values[0], 0, 5) == 'case_') ||
1681 (substr($values[0], 0, 10) == 'financial_') ||
1682 (substr($values[0], 0, 8) == 'payment_') ||
1683 (substr($values[0], 0, 11) == 'membership_')
1684 ) {
1685 return;
1686 }
1687
1688 // skip for hook injected fields / params
1689 $extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields();
1690 if (array_key_exists($values[0], $extFields)) {
1691 return;
1692 }
1693
1694 switch ($values[0]) {
1695 case 'deleted_contacts':
1696 $this->deletedContacts($values);
1697 return;
1698
1699 case 'contact_type':
1700 $this->contactType($values);
1701 return;
1702
1703 case 'contact_sub_type':
1704 $this->contactSubType($values);
1705 return;
1706
1707 case 'group':
1708 $this->group($values);
1709 return;
1710
1711 case 'group_type':
1712 // so we resolve this into a list of groups & proceed as if they had been
1713 // handed in
1714 list($name, $op, $value, $grouping, $wildcard) = $values;
1715 $values[0] = 'group';
1716 $values[1] = 'IN';
1717 $this->_paramLookup['group'][0][0] = 'group';
1718 $this->_paramLookup['group'][0][1] = 'IN';
1719 $this->_paramLookup['group'][0][2] = $values[2] = $this->getGroupsFromTypeCriteria($value);
1720 $this->group($values);
1721 return;
1722
1723 // case tag comes from find contacts
1724 case 'tag_search':
1725 $this->tagSearch($values);
1726 return;
1727
1728 case 'tag':
1729 case 'contact_tags':
1730 $this->tag($values);
1731 return;
1732
1733 case 'note':
1734 case 'note_body':
1735 case 'note_subject':
1736 $this->notes($values);
1737 return;
1738
1739 case 'uf_user':
1740 $this->ufUser($values);
1741 return;
1742
1743 case 'sort_name':
1744 case 'display_name':
1745 $this->sortName($values);
1746 return;
1747
1748 case 'addressee':
1749 case 'postal_greeting':
1750 case 'email_greeting':
1751 $this->greetings($values);
1752 return;
1753
1754 case 'email':
1755 $this->email($values);
1756 return;
1757
1758 case 'phone_numeric':
1759 $this->phone_numeric($values);
1760 return;
1761
1762 case 'phone_phone_type_id':
1763 case 'phone_location_type_id':
1764 $this->phone_option_group($values);
1765 return;
1766
1767 case 'street_address':
1768 $this->street_address($values);
1769 return;
1770
1771 case 'street_number':
1772 $this->street_number($values);
1773 return;
1774
1775 case 'sortByCharacter':
1776 $this->sortByCharacter($values);
1777 return;
1778
1779 case 'location_type':
1780 $this->locationType($values);
1781 return;
1782
1783 case 'county':
1784 $this->county($values);
1785 return;
1786
1787 case 'state_province':
1788 $this->stateProvince($values);
1789 return;
1790
1791 case 'country':
1792 $this->country($values, FALSE);
1793 return;
1794
1795 case 'postal_code':
1796 case 'postal_code_low':
1797 case 'postal_code_high':
1798 $this->postalCode($values);
1799 return;
1800
1801 case 'activity_date':
1802 case 'activity_date_low':
1803 case 'activity_date_high':
1804 case 'activity_role':
1805 case 'activity_status_id':
1806 case 'activity_status':
1807 case 'followup_parent_id':
1808 case 'parent_id':
1809 case 'source_contact_id':
1810 case 'activity_subject':
1811 case 'test_activities':
1812 case 'activity_type_id':
1813 case 'activity_type':
1814 case 'activity_survey_id':
1815 case 'activity_tags':
1816 case 'activity_taglist':
1817 case 'activity_test':
1818 case 'activity_campaign_id':
1819 case 'activity_engagement_level':
1820 case 'activity_id':
1821 case 'activity_result':
1822 case 'source_contact':
1823 CRM_Activity_BAO_Query::whereClauseSingle($values, $this);
1824 return;
1825
1826 case 'age_low':
1827 case 'age_high':
1828 case 'birth_date_low':
1829 case 'birth_date_high':
1830 case 'deceased_date_low':
1831 case 'deceased_date_high':
1832 $this->demographics($values);
1833 return;
1834
1835 case 'age_asof_date':
1836 // handled by demographics
1837 return;
1838
1839 case 'log_date_low':
1840 case 'log_date_high':
1841 $this->modifiedDates($values);
1842 return;
1843
1844 case 'changed_by':
1845 $this->changeLog($values);
1846 return;
1847
1848 case 'do_not_phone':
1849 case 'do_not_email':
1850 case 'do_not_mail':
1851 case 'do_not_sms':
1852 case 'do_not_trade':
1853 case 'is_opt_out':
1854 $this->privacy($values);
1855 return;
1856
1857 case 'privacy_options':
1858 $this->privacyOptions($values);
1859 return;
1860
1861 case 'privacy_operator':
1862 case 'privacy_toggle':
1863 // these are handled by privacy options
1864 return;
1865
1866 case 'preferred_communication_method':
1867 $this->preferredCommunication($values);
1868 return;
1869
1870 case 'relation_type_id':
1871 case 'relation_start_date_high':
1872 case 'relation_start_date_low':
1873 case 'relation_end_date_high':
1874 case 'relation_end_date_low':
1875 case 'relation_target_name':
1876 case 'relation_status':
1877 case 'relation_date_low':
1878 case 'relation_date_high':
1879 $this->relationship($values);
1880 $this->_relationshipValuesAdded = TRUE;
1881 return;
1882
1883 case 'task_status_id':
1884 $this->task($values);
1885 return;
1886
1887 case 'task_id':
1888 // since this case is handled with the above
1889 return;
1890
1891 case 'prox_distance':
1892 CRM_Contact_BAO_ProximityQuery::process($this, $values);
1893 return;
1894
1895 case 'prox_street_address':
1896 case 'prox_city':
1897 case 'prox_postal_code':
1898 case 'prox_state_province_id':
1899 case 'prox_country_id':
1900 // handled by the proximity_distance clause
1901 return;
1902
1903 default:
1904 $this->restWhere($values);
1905 return;
1906 }
1907 }
1908
1909 /**
1910 * Given a list of conditions in params generate the required where clause.
1911 *
1912 * @return string
1913 */
1914 public function whereClause() {
1915 $this->_where[0] = array();
1916 $this->_qill[0] = array();
1917
1918 $this->includeContactIds();
1919 if (!empty($this->_params)) {
1920 foreach (array_keys($this->_params) as $id) {
1921 if (empty($this->_params[$id][0])) {
1922 continue;
1923 }
1924 // check for both id and contact_id
1925 if ($this->_params[$id][0] == 'id' || $this->_params[$id][0] == 'contact_id') {
1926 $this->_where[0][] = self::buildClause("contact_a.id", $this->_params[$id][1], $this->_params[$id][2]);
1927 }
1928 else {
1929 $this->whereClauseSingle($this->_params[$id]);
1930 }
1931 }
1932
1933 CRM_Core_Component::alterQuery($this, 'where');
1934
1935 CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'where');
1936 }
1937
1938 if ($this->_customQuery) {
1939 // Added following if condition to avoid the wrong value display for 'my account' / any UF info.
1940 // Hope it wont affect the other part of civicrm.. if it does please remove it.
1941 if (!empty($this->_customQuery->_where)) {
1942 $this->_where = CRM_Utils_Array::crmArrayMerge($this->_where, $this->_customQuery->_where);
1943 }
1944
1945 $this->_qill = CRM_Utils_Array::crmArrayMerge($this->_qill, $this->_customQuery->_qill);
1946 }
1947
1948 $clauses = array();
1949 $andClauses = array();
1950
1951 $validClauses = 0;
1952 if (!empty($this->_where)) {
1953 foreach ($this->_where as $grouping => $values) {
1954 if ($grouping > 0 && !empty($values)) {
1955 $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) ';
1956 $validClauses++;
1957 }
1958 }
1959
1960 if (!empty($this->_where[0])) {
1961 $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where[0]) . ' ) ';
1962 }
1963 if (!empty($clauses)) {
1964 $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
1965 }
1966
1967 if ($validClauses > 1) {
1968 $this->_useDistinct = TRUE;
1969 }
1970 }
1971
1972 return implode(' AND ', $andClauses);
1973 }
1974
1975 /**
1976 * Generate where clause for any parameters not already handled.
1977 *
1978 * @param array $values
1979 *
1980 * @throws Exception
1981 */
1982 public function restWhere(&$values) {
1983 $name = CRM_Utils_Array::value(0, $values);
1984 $op = CRM_Utils_Array::value(1, $values);
1985 $value = CRM_Utils_Array::value(2, $values);
1986 $grouping = CRM_Utils_Array::value(3, $values);
1987 $wildcard = CRM_Utils_Array::value(4, $values);
1988
1989 if (isset($grouping) && empty($this->_where[$grouping])) {
1990 $this->_where[$grouping] = array();
1991 }
1992
1993 $multipleFields = array('url');
1994
1995 //check if the location type exits for fields
1996 $lType = '';
1997 $locType = explode('-', $name);
1998
1999 if (!in_array($locType[0], $multipleFields)) {
2000 //add phone type if exists
2001 if (isset($locType[2]) && $locType[2]) {
2002 $locType[2] = CRM_Core_DAO::escapeString($locType[2]);
2003 }
2004 }
2005
2006 $field = CRM_Utils_Array::value($name, $this->_fields);
2007
2008 if (!$field) {
2009 $field = CRM_Utils_Array::value($locType[0], $this->_fields);
2010
2011 if (!$field) {
2012 return;
2013 }
2014 }
2015
2016 $setTables = TRUE;
2017
2018 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
2019 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
2020
2021 if (substr($name, 0, 14) === 'state_province') {
2022 if (isset($locType[1]) && is_numeric($locType[1])) {
2023 $setTables = FALSE;
2024 $aName = "{$locationType[$locType[1]]}-address";
2025 $where = "`$aName`.state_province_id";
2026 }
2027 else {
2028 $where = "civicrm_address.state_province_id";
2029 }
2030
2031 $this->_where[$grouping][] = self::buildClause($where, $op, $value);
2032 list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
2033 $this->_qill[$grouping][] = ts("State %1 %2", array(1 => $qillop, 2 => $qillVal));
2034 }
2035 elseif (!empty($field['pseudoconstant'])) {
2036 $this->optionValueQuery(
2037 $name, $op, $value, $grouping,
2038 'CRM_Contact_DAO_Contact',
2039 $field,
2040 $field['title'],
2041 'String',
2042 TRUE
2043 );
2044 if ($name == 'gender_id') {
2045 self::$_openedPanes[ts('Demographics')] = TRUE;
2046 }
2047 }
2048 elseif (substr($name, 0, 7) === 'country' || substr($name, 0, 6) === 'county') {
2049 $name = (substr($name, 0, 7) === 'country') ? "country_id" : "county_id";
2050 if (isset($locType[1]) && is_numeric($locType[1])) {
2051 $setTables = FALSE;
2052 $aName = "{$locationType[$locType[1]]}-address";
2053 $where = "`$aName`.$name";
2054 }
2055 else {
2056 $where = "civicrm_address.$name";
2057 }
2058
2059 $this->_where[$grouping][] = self::buildClause($where, $op, $value, 'Positive');
2060
2061 if ($lType) {
2062 $field['title'] .= " ($lType)";
2063 }
2064 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op);
2065 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2066 }
2067 elseif ($name === 'world_region') {
2068 $this->optionValueQuery(
2069 $name, $op, $value, $grouping,
2070 NULL,
2071 $field,
2072 ts('World Region'),
2073 'Positive',
2074 TRUE
2075 );
2076 }
2077 elseif ($name === 'is_deceased') {
2078 $this->_where[$grouping][] = self::buildClause("contact_a.{$name}", $op, $value);
2079 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2080 self::$_openedPanes[ts('Demographics')] = TRUE;
2081 }
2082 elseif ($name === 'created_date' || $name === 'modified_date' || $name === 'deceased_date' || $name === 'birth_date') {
2083 $appendDateTime = TRUE;
2084 if ($name === 'deceased_date' || $name === 'birth_date') {
2085 $appendDateTime = FALSE;
2086 self::$_openedPanes[ts('Demographics')] = TRUE;
2087 }
2088 $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime);
2089 }
2090 elseif ($name === 'contact_id') {
2091 if (is_int($value)) {
2092 $this->_where[$grouping][] = self::buildClause($field['where'], $op, $value);
2093 $this->_qill[$grouping][] = "$field[title] $op $value";
2094 }
2095 }
2096 elseif ($name === 'name') {
2097 $value = $strtolower(CRM_Core_DAO::escapeString($value));
2098 if ($wildcard) {
2099 $value = "%$value%";
2100 $op = 'LIKE';
2101 }
2102 $wc = self::caseImportant($op) ? "LOWER({$field['where']})" : "{$field['where']}";
2103 $this->_where[$grouping][] = self::buildClause($wc, $op, "'$value'");
2104 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2105 }
2106 elseif ($name === 'current_employer') {
2107 $value = $strtolower(CRM_Core_DAO::escapeString($value));
2108 if ($wildcard) {
2109 $value = "%$value%";
2110 $op = 'LIKE';
2111 }
2112 $wc = self::caseImportant($op) ? "LOWER(contact_a.organization_name)" : "contact_a.organization_name";
2113 $ceWhereClause = self::buildClause($wc, $op,
2114 $value
2115 );
2116 $ceWhereClause .= " AND contact_a.contact_type = 'Individual'";
2117 $this->_where[$grouping][] = $ceWhereClause;
2118 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2119 }
2120 elseif ($name === 'email_greeting') {
2121 $filterCondition = array('greeting_type' => 'email_greeting');
2122 $this->optionValueQuery(
2123 $name, $op, $value, $grouping,
2124 CRM_Core_PseudoConstant::greeting($filterCondition),
2125 $field,
2126 ts('Email Greeting')
2127 );
2128 }
2129 elseif ($name === 'postal_greeting') {
2130 $filterCondition = array('greeting_type' => 'postal_greeting');
2131 $this->optionValueQuery(
2132 $name, $op, $value, $grouping,
2133 CRM_Core_PseudoConstant::greeting($filterCondition),
2134 $field,
2135 ts('Postal Greeting')
2136 );
2137 }
2138 elseif ($name === 'addressee') {
2139 $filterCondition = array('greeting_type' => 'addressee');
2140 $this->optionValueQuery(
2141 $name, $op, $value, $grouping,
2142 CRM_Core_PseudoConstant::greeting($filterCondition),
2143 $field,
2144 ts('Addressee')
2145 );
2146 }
2147 elseif (substr($name, 0, 4) === 'url-') {
2148 $tName = 'civicrm_website';
2149 $this->_whereTables[$tName] = $this->_tables[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )";
2150 $value = $strtolower(CRM_Core_DAO::escapeString($value));
2151 if ($wildcard) {
2152 $value = "%$value%";
2153 $op = 'LIKE';
2154 }
2155
2156 $wc = 'civicrm_website.url';
2157 $this->_where[$grouping][] = $d = self::buildClause($wc, $op, $value);
2158 $this->_qill[$grouping][] = "$field[title] $op \"$value\"";
2159 }
2160 elseif ($name === 'contact_is_deleted') {
2161 $this->_where[$grouping][] = self::buildClause("contact_a.is_deleted", $op, $value);
2162 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op);
2163 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2164 }
2165 elseif (!empty($field['where'])) {
2166 $type = NULL;
2167 if (!empty($field['type'])) {
2168 $type = CRM_Utils_Type::typeToString($field['type']);
2169 }
2170
2171 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2172
2173 if (isset($locType[1]) &&
2174 is_numeric($locType[1])
2175 ) {
2176 $setTables = FALSE;
2177
2178 //get the location name
2179 list($tName, $fldName) = self::getLocationTableName($field['where'], $locType);
2180
2181 $fieldName = "LOWER(`$tName`.$fldName)";
2182
2183 // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
2184 $this->_tables[$tName] = $this->_whereTables[$tName] = 1;
2185
2186 }
2187 else {
2188 if ($tableName == 'civicrm_contact') {
2189 $fieldName = "LOWER(contact_a.{$fieldName})";
2190 }
2191 else {
2192 if ($op != 'IN' && !is_numeric($value)) {
2193 $fieldName = "LOWER({$field['where']})";
2194 }
2195 else {
2196 $fieldName = "{$field['where']}";
2197 }
2198 }
2199 }
2200
2201 list($qillop, $qillVal) = self::buildQillForFieldValue(NULL, $field['title'], $value, $op);
2202 $this->_qill[$grouping][] = ts("%1 %2 %3", array(
2203 1 => $field['title'],
2204 2 => $qillop,
2205 3 => (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) ? $qillVal : "'$qillVal'"));
2206
2207 if (is_array($value)) {
2208 // traditionally an array being passed has been a fatal error. We can take advantage of this to add support
2209 // for api style operators for functions that hit this point without worrying about regression
2210 // (the previous comments indicated the condition for hitting this point were unknown
2211 // per CRM-14743 we are adding modified_date & created_date operator support
2212 $operations = array_keys($value);
2213 foreach ($operations as $operator) {
2214 if (!in_array($operator, CRM_Core_DAO::acceptedSQLOperators())) {
2215 //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators
2216 //so this condition will satisfy the search for now
2217 if (strpos($op, 'IN') !== FALSE) {
2218 $value = array($op => $value);
2219 }
2220 // we don't know when this might happen
2221 else {
2222 CRM_Core_Error::fatal(ts("%1 is not a valid operator", array(1 => $operator)));
2223 }
2224 }
2225 }
2226 $this->_where[$grouping][] = CRM_Core_DAO::createSQLFilter($fieldName, $value, $type);
2227 }
2228 else {
2229 if (!strpos($op, 'IN')) {
2230 $value = $strtolower($value);
2231 }
2232 if ($wildcard) {
2233 $value = "%$value%";
2234 $op = 'LIKE';
2235 }
2236
2237 $this->_where[$grouping][] = self::buildClause($fieldName, $op, $value, $type);
2238 }
2239 }
2240
2241 if ($setTables && isset($field['where'])) {
2242 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2243 if (isset($tableName)) {
2244 $this->_tables[$tableName] = 1;
2245 $this->_whereTables[$tableName] = 1;
2246 }
2247 }
2248 }
2249
2250
2251 /**
2252 * @param $where
2253 * @param $locType
2254 *
2255 * @return array
2256 * @throws Exception
2257 */
2258 public static function getLocationTableName(&$where, &$locType) {
2259 if (isset($locType[1]) && is_numeric($locType[1])) {
2260 list($tbName, $fldName) = explode(".", $where);
2261
2262 //get the location name
2263 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
2264 $specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
2265 if (in_array($locType[0], $specialFields)) {
2266 //hack to fix / special handing for phone_ext
2267 if ($locType[0] == 'phone_ext') {
2268 $locType[0] = 'phone';
2269 }
2270 if (isset($locType[2]) && $locType[2]) {
2271 $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}";
2272 }
2273 else {
2274 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2275 }
2276 }
2277 elseif (in_array($locType[0],
2278 array(
2279 'address_name',
2280 'street_address',
2281 'street_name',
2282 'street_number_suffix',
2283 'street_unit',
2284 'supplemental_address_1',
2285 'supplemental_address_2',
2286 'city',
2287 'postal_code',
2288 'postal_code_suffix',
2289 'geo_code_1',
2290 'geo_code_2',
2291 'master_id',
2292 )
2293 )) {
2294 //fix for search by profile with address fields.
2295 $tName = "{$locationType[$locType[1]]}-address";
2296 }
2297 elseif (in_array($locType[0],
2298 array(
2299 'on_hold',
2300 'signature_html',
2301 'signature_text',
2302 'is_bulkmail',
2303 )
2304 )) {
2305 $tName = "{$locationType[$locType[1]]}-email";
2306 }
2307 elseif ($locType[0] == 'provider_id') {
2308 $tName = "{$locationType[$locType[1]]}-im";
2309 }
2310 elseif ($locType[0] == 'openid') {
2311 $tName = "{$locationType[$locType[1]]}-openid";
2312 }
2313 else {
2314 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2315 }
2316 $tName = str_replace(' ', '_', $tName);
2317 return array($tName, $fldName);
2318 }
2319 CRM_Core_Error::fatal();
2320 }
2321
2322 /**
2323 * Given a result dao, extract the values and return that array
2324 *
2325 * @param CRM_Core_DAO $dao
2326 *
2327 * @return array
2328 * values for this query
2329 */
2330 public function store($dao) {
2331 $value = array();
2332
2333 foreach ($this->_element as $key => $dontCare) {
2334 if (property_exists($dao, $key)) {
2335 if (strpos($key, '-') !== FALSE) {
2336 $values = explode('-', $key);
2337 $lastElement = array_pop($values);
2338 $current = &$value;
2339 $cnt = count($values);
2340 $count = 1;
2341 foreach ($values as $v) {
2342 if (!array_key_exists($v, $current)) {
2343 $current[$v] = array();
2344 }
2345 //bad hack for im_provider
2346 if ($lastElement == 'provider_id') {
2347 if ($count < $cnt) {
2348 $current = &$current[$v];
2349 }
2350 else {
2351 $lastElement = "{$v}_{$lastElement}";
2352 }
2353 }
2354 else {
2355 $current = &$current[$v];
2356 }
2357 $count++;
2358 }
2359
2360 $current[$lastElement] = $dao->$key;
2361 }
2362 else {
2363 $value[$key] = $dao->$key;
2364 }
2365 }
2366 }
2367 return $value;
2368 }
2369
2370 /**
2371 * Getter for tables array.
2372 *
2373 * @return array
2374 */
2375 public function tables() {
2376 return $this->_tables;
2377 }
2378
2379 /**
2380 * Where tables is sometimes used to create the from clause, but, not reliably, set this AND set tables
2381 * It's unclear the intent - there is a 'simpleFrom' clause which takes whereTables into account & a fromClause which doesn't
2382 * logic may have eroded
2383 * @return array
2384 */
2385 public function whereTables() {
2386 return $this->_whereTables;
2387 }
2388
2389 /**
2390 * Generate the where clause (used in match contacts and permissions)
2391 *
2392 * @param array $params
2393 * @param array $fields
2394 * @param array $tables
2395 * @param $whereTables
2396 * @param bool $strict
2397 *
2398 * @return string
2399 */
2400 public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) {
2401 $query = new CRM_Contact_BAO_Query($params, NULL, $fields,
2402 FALSE, $strict
2403 );
2404
2405 $tables = array_merge($query->tables(), $tables);
2406 $whereTables = array_merge($query->whereTables(), $whereTables);
2407
2408 return $query->_whereClause;
2409 }
2410
2411 /**
2412 * Create the from clause.
2413 *
2414 * @param array $tables
2415 * Tables that need to be included in this from clause.
2416 * if null, return mimimal from clause (i.e. civicrm_contact)
2417 * @param array $inner
2418 * Tables that should be inner-joined.
2419 * @param array $right
2420 * Tables that should be right-joined.
2421 *
2422 * @param bool $primaryLocation
2423 * @param int $mode
2424 *
2425 * @return string
2426 * the from clause
2427 */
2428 public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1) {
2429
2430 $from = ' FROM civicrm_contact contact_a';
2431 if (empty($tables)) {
2432 return $from;
2433 }
2434
2435 if (!empty($tables['civicrm_worldregion'])) {
2436 $tables = array_merge(array('civicrm_country' => 1), $tables);
2437 }
2438
2439 if ((!empty($tables['civicrm_state_province']) || !empty($tables['civicrm_country']) ||
2440 CRM_Utils_Array::value('civicrm_county', $tables)
2441 ) && empty($tables['civicrm_address'])
2442 ) {
2443 $tables = array_merge(array('civicrm_address' => 1),
2444 $tables
2445 );
2446 }
2447
2448 // add group_contact and group_contact_cache table if group table is present
2449 if (!empty($tables['civicrm_group'])) {
2450 if (empty($tables['civicrm_group_contact'])) {
2451 $tables['civicrm_group_contact'] = " LEFT JOIN civicrm_group_contact ON civicrm_group_contact.contact_id = contact_a.id AND civicrm_group_contact.status = 'Added' ";
2452 }
2453 if (empty($tables['civicrm_group_contact_cache'])) {
2454 $tables['civicrm_group_contact_cache'] = " LEFT JOIN civicrm_group_contact_cache ON civicrm_group_contact_cache.contact_id = contact_a.id ";
2455 }
2456 }
2457
2458 // add group_contact and group table is subscription history is present
2459 if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) {
2460 $tables = array_merge(array(
2461 'civicrm_group' => 1,
2462 'civicrm_group_contact' => 1,
2463 ),
2464 $tables
2465 );
2466 }
2467
2468 // to handle table dependencies of components
2469 CRM_Core_Component::tableNames($tables);
2470 // to handle table dependencies of hook injected tables
2471 CRM_Contact_BAO_Query_Hook::singleton()->setTableDependency($tables);
2472
2473 //format the table list according to the weight
2474 $info = CRM_Core_TableHierarchy::info();
2475
2476 foreach ($tables as $key => $value) {
2477 $k = 99;
2478 if (strpos($key, '-') !== FALSE) {
2479 $keyArray = explode('-', $key);
2480 $k = CRM_Utils_Array::value('civicrm_' . $keyArray[1], $info, 99);
2481 }
2482 elseif (strpos($key, '_') !== FALSE) {
2483 $keyArray = explode('_', $key);
2484 if (is_numeric(array_pop($keyArray))) {
2485 $k = CRM_Utils_Array::value(implode('_', $keyArray), $info, 99);
2486 }
2487 else {
2488 $k = CRM_Utils_Array::value($key, $info, 99);
2489 }
2490 }
2491 else {
2492 $k = CRM_Utils_Array::value($key, $info, 99);
2493 }
2494 $tempTable[$k . ".$key"] = $key;
2495 }
2496 ksort($tempTable);
2497 $newTables = array();
2498 foreach ($tempTable as $key) {
2499 $newTables[$key] = $tables[$key];
2500 }
2501
2502 $tables = $newTables;
2503
2504 foreach ($tables as $name => $value) {
2505 if (!$value) {
2506 continue;
2507 }
2508
2509 if (!empty($inner[$name])) {
2510 $side = 'INNER';
2511 }
2512 elseif (!empty($right[$name])) {
2513 $side = 'RIGHT';
2514 }
2515 else {
2516 $side = 'LEFT';
2517 }
2518
2519 if ($value != 1) {
2520 // if there is already a join statement in value, use value itself
2521 if (strpos($value, 'JOIN')) {
2522 $from .= " $value ";
2523 }
2524 else {
2525 $from .= " $side JOIN $name ON ( $value ) ";
2526 }
2527 continue;
2528 }
2529 switch ($name) {
2530 case 'civicrm_address':
2531 if ($primaryLocation) {
2532 $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )";
2533 }
2534 else {
2535 //CRM-14263 further handling of address joins further down...
2536 $from .= " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id ) ";
2537 }
2538 continue;
2539
2540 case 'civicrm_phone':
2541 $from .= " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) ";
2542 continue;
2543
2544 case 'civicrm_email':
2545 $from .= " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) ";
2546 continue;
2547
2548 case 'civicrm_im':
2549 $from .= " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) ";
2550 continue;
2551
2552 case 'im_provider':
2553 $from .= " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) ";
2554 $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'";
2555 $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)";
2556 continue;
2557
2558 case 'civicrm_openid':
2559 $from .= " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id AND civicrm_openid.is_primary = 1 )";
2560 continue;
2561
2562 case 'civicrm_worldregion':
2563 $from .= " $side JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id ";
2564 $from .= " $side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ";
2565 continue;
2566
2567 case 'civicrm_location_type':
2568 $from .= " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id ";
2569 continue;
2570
2571 case 'civicrm_group':
2572 $from .= " $side JOIN civicrm_group ON (civicrm_group.id = civicrm_group_contact.group_id OR civicrm_group.id = civicrm_group_contact_cache.group_id) ";
2573 continue;
2574
2575 case 'civicrm_group_contact':
2576 $from .= " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id ";
2577 continue;
2578
2579 case 'civicrm_group_contact_cache':
2580 $from .= " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id ";
2581 continue;
2582
2583 case 'civicrm_activity':
2584 case 'civicrm_activity_tag':
2585 case 'activity_type':
2586 case 'activity_status':
2587 case 'parent_id':
2588 case 'civicrm_activity_contact':
2589 case 'source_contact':
2590 $from .= CRM_Activity_BAO_Query::from($name, $mode, $side);
2591 continue;
2592
2593 case 'civicrm_entity_tag':
2594 $from .= " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND
2595 civicrm_entity_tag.entity_id = contact_a.id ) ";
2596 continue;
2597
2598 case 'civicrm_note':
2599 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND
2600 contact_a.id = civicrm_note.entity_id ) ";
2601 continue;
2602
2603 case 'civicrm_subscription_history':
2604 $from .= " $side JOIN civicrm_subscription_history
2605 ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id
2606 AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id";
2607 continue;
2608
2609 case 'civicrm_relationship':
2610 if (self::$_relType == 'reciprocal') {
2611 if (self::$_relationshipTempTable) {
2612 // we have a temptable to join on
2613 $tbl = self::$_relationshipTempTable;
2614 $from .= " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id";
2615 }
2616 else {
2617 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)";
2618 $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)";
2619 }
2620 }
2621 elseif (self::$_relType == 'b') {
2622 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )";
2623 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
2624 }
2625 else {
2626 $from .= " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )";
2627 $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
2628 }
2629 continue;
2630
2631 case 'civicrm_log':
2632 $from .= " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')";
2633 $from .= " INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)";
2634 continue;
2635
2636 case 'civicrm_tag':
2637 $from .= " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id ";
2638 continue;
2639
2640 case 'civicrm_grant':
2641 $from .= CRM_Grant_BAO_Query::from($name, $mode, $side);
2642 continue;
2643
2644 case 'civicrm_website':
2645 $from .= " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id ";
2646 continue;
2647
2648 default:
2649 $locationTypeName = '';
2650 if (strpos($name, '-address') != 0) {
2651 $locationTypeName = 'address';
2652 }
2653 elseif (strpos($name, '-phone') != 0) {
2654 $locationTypeName = 'phone';
2655 }
2656 elseif (strpos($name, '-email') != 0) {
2657 $locationTypeName = 'email';
2658 }
2659 elseif (strpos($name, '-im') != 0) {
2660 $locationTypeName = 'im';
2661 }
2662 elseif (strpos($name, '-openid') != 0) {
2663 $locationTypeName = 'openid';
2664 }
2665
2666 if ($locationTypeName) {
2667 //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
2668 $parts = explode('-', $name);
2669 $locationID = array_search($parts[0], CRM_Core_BAO_Address::buildOptions('location_type_id', 'get', array('name' => $parts[0])));
2670 $from .= " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
2671 }
2672 else {
2673 $from .= CRM_Core_Component::from($name, $mode, $side);
2674 }
2675 $from .= CRM_Contact_BAO_Query_Hook::singleton()->buildSearchfrom($name, $mode, $side);
2676
2677 continue;
2678 }
2679 }
2680 return $from;
2681 }
2682
2683 /**
2684 * WHERE / QILL clause for deleted_contacts
2685 *
2686 * @param array $values
2687 */
2688 public function deletedContacts($values) {
2689 list($_, $_, $value, $grouping, $_) = $values;
2690 if ($value) {
2691 // *prepend* to the relevant grouping as this is quite an important factor
2692 array_unshift($this->_qill[$grouping], ts('Search in Trash'));
2693 }
2694 }
2695
2696 /**
2697 * Where / qill clause for contact_type
2698 *
2699 * @param $values
2700 */
2701 public function contactType(&$values) {
2702 list($name, $op, $value, $grouping, $wildcard) = $values;
2703
2704 $subTypes = array();
2705 $clause = array();
2706
2707 // account for search builder mapping multiple values
2708 if (!is_array($value)) {
2709 $values = self::parseSearchBuilderString($value, 'String');
2710 if (is_array($values)) {
2711 $value = array_flip($values);
2712 }
2713 }
2714
2715 if (is_array($value)) {
2716 foreach ($value as $k => $v) {
2717 // fix for CRM-771
2718 if ($k) {
2719 $subType = NULL;
2720 $contactType = $k;
2721 if (strpos($k, CRM_Core_DAO::VALUE_SEPARATOR)) {
2722 list($contactType, $subType) = explode(CRM_Core_DAO::VALUE_SEPARATOR, $k, 2);
2723 }
2724
2725 if (!empty($subType)) {
2726 $subTypes[$subType] = 1;
2727 }
2728 $clause[$contactType] = "'" . CRM_Utils_Type::escape($contactType, 'String') . "'";
2729 }
2730 }
2731 }
2732 else {
2733 $contactTypeANDSubType = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value, 2);
2734 $contactType = $contactTypeANDSubType[0];
2735 $subType = CRM_Utils_Array::value(1, $contactTypeANDSubType);
2736 if (!empty($subType)) {
2737 $subTypes[$subType] = 1;
2738 }
2739 $clause[$contactType] = "'" . CRM_Utils_Type::escape($contactType, 'String') . "'";
2740 }
2741
2742 // fix for CRM-771
2743 if (!empty($clause)) {
2744 $quill = $clause;
2745 if ($op == 'IN' || $op == 'NOT IN') {
2746 $this->_where[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')';
2747 }
2748 else {
2749 $type = array_pop($clause);
2750 $this->_where[$grouping][] = self::buildClause("contact_a.contact_type", $op, $contactType);
2751 }
2752
2753 $this->_qill[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill);
2754
2755 if (!empty($subTypes)) {
2756 $this->includeContactSubTypes($subTypes, $grouping);
2757 }
2758 }
2759 }
2760
2761 /**
2762 * Where / qill clause for contact_sub_type
2763 *
2764 * @param $values
2765 */
2766 public function contactSubType(&$values) {
2767 list($name, $op, $value, $grouping, $wildcard) = $values;
2768 $this->includeContactSubTypes($value, $grouping, $op);
2769 }
2770
2771 /**
2772 * @param $value
2773 * @param $grouping
2774 * @param string $op
2775 */
2776 public function includeContactSubTypes($value, $grouping, $op = 'LIKE') {
2777
2778 $clause = array();
2779 $alias = "contact_a.contact_sub_type";
2780 $qillOperators = array('NOT LIKE' => ts('Not Like')) + CRM_Core_SelectValues::getSearchBuilderOperators();
2781
2782 $op = str_replace('IN', 'LIKE', $op);
2783 $op = str_replace('=', 'LIKE', $op);
2784 $op = str_replace('!', 'NOT ', $op);
2785
2786 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
2787 $this->_where[$grouping][] = self::buildClause($alias, $op, $value, 'String');
2788 }
2789 elseif (is_array($value)) {
2790 foreach ($value as $k => $v) {
2791 if (!empty($k)) {
2792 $clause[$k] = "($alias $op '%" . CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($k, 'String') . CRM_Core_DAO::VALUE_SEPARATOR . "%')";
2793 }
2794 }
2795 }
2796 else {
2797 $clause[$value] = "($alias $op '%" . CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($value, 'String') . CRM_Core_DAO::VALUE_SEPARATOR . "%')";
2798 }
2799
2800 if (!empty($clause)) {
2801 $this->_where[$grouping][] = "( " . implode(' OR ', $clause) . " )";
2802 }
2803 $this->_qill[$grouping][] = ts('Contact Subtype %1 ', array(1 => $qillOperators[$op])) . implode(' ' . ts('or') . ' ', array_keys($clause));
2804 }
2805
2806 /**
2807 * Where / qill clause for groups
2808 *
2809 * @param $values
2810 */
2811 public function group(&$values) {
2812 list($name, $op, $value, $grouping, $wildcard) = $values;
2813
2814 // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first
2815 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
2816 $op = key($value);
2817 $value = $value[$op];
2818 }
2819
2820 // Replace pseudo operators from search builder
2821 $op = str_replace('EMPTY', 'NULL', $op);
2822
2823 if (strpos($op, 'NULL')) {
2824 $value = NULL;
2825 }
2826
2827 if (count($value) > 1) {
2828 if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) {
2829 CRM_Core_Error::fatal(ts("%1 is not a valid operator", array(1 => $op)));
2830 }
2831 $this->_useDistinct = TRUE;
2832 }
2833
2834 if (isset($value)) {
2835 $value = CRM_Utils_Array::value($op, $value, $value);
2836 }
2837
2838 $groupIds = NULL;
2839 $names = array();
2840 $isSmart = FALSE;
2841 $isNotOp = ($op == 'NOT IN' || $op == '!=');
2842
2843 $statii = array();
2844 $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
2845 if ($gcsValues &&
2846 is_array($gcsValues[2])
2847 ) {
2848 foreach ($gcsValues[2] as $k => $v) {
2849 if ($v) {
2850 $statii[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
2851 }
2852 }
2853 }
2854 else {
2855 $statii[] = '"Added"';
2856 }
2857
2858 $skipGroup = FALSE;
2859 if (!is_array($value) &&
2860 count($statii) == 1 &&
2861 $statii[0] == '"Added"' &&
2862 !$isNotOp
2863 ) {
2864 if (!empty($value) && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $value, 'saved_search_id')) {
2865 $isSmart = TRUE;
2866 }
2867 }
2868
2869 $ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op);
2870 $isSmart = (!$ssClause) ? FALSE : $isSmart;
2871 $groupClause = NULL;
2872
2873 if (!$isSmart) {
2874 $groupIds = implode(',', (array) $value);
2875 $gcTable = "`civicrm_group_contact-{$groupIds}`";
2876 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
2877 if ($statii) {
2878 $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")";
2879 }
2880 $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
2881 if (strpos($op, 'IN') !== FALSE) {
2882 $groupClause = "{$gcTable}.group_id $op ( $groupIds )";
2883 }
2884 elseif ($op == '!=') {
2885 $groupClause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds)";
2886 }
2887 else {
2888 $groupClause = "{$gcTable}.group_id $op $groupIds";
2889 }
2890 }
2891
2892 if ($ssClause) {
2893 $and = ($op == 'IS NULL') ? 'AND' : 'OR';
2894 if ($groupClause) {
2895 $groupClause = "( ( $groupClause ) $and ( $ssClause ) )";
2896 }
2897 else {
2898 $groupClause = $ssClause;
2899 }
2900 }
2901
2902 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
2903 $this->_qill[$grouping][] = ts("Group(s) %1 %2", array(1 => $qillop, 2 => $qillVal));
2904 if (strpos($op, 'NULL') === FALSE) {
2905 $this->_qill[$grouping][] = ts("Group Status %1", array(1 => implode(' ' . ts('or') . ' ', $statii)));
2906 }
2907 if ($groupClause) {
2908 $this->_where[$grouping][] = $groupClause;
2909 }
2910 }
2911
2912 /**
2913 * Function translates selection of group type into a list of groups.
2914 * @param $value
2915 *
2916 * @return array
2917 */
2918 public function getGroupsFromTypeCriteria($value) {
2919 $groupIds = array();
2920 foreach ((array) $value as $groupTypeValue) {
2921 $groupList = CRM_Core_PseudoConstant::group($groupTypeValue);
2922 $groupIds = ($groupIds + $groupList);
2923 }
2924 return $groupIds;
2925 }
2926
2927 /**
2928 * @param array $groups
2929 * @param string $tableAlias
2930 * @param string $joinTable
2931 * @param string $op
2932 *
2933 * @return null|string
2934 */
2935 public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a", $op) {
2936 $isNullOp = (strpos($op, 'NULL') !== FALSE);
2937 $groupsIds = $groups;
2938 if (!$isNullOp && !$groups) {
2939 return NULL;
2940 }
2941 elseif (strpos($op, 'IN') !== FALSE) {
2942 $groups = array($op => $groups);
2943 }
2944 elseif (is_array($groups) && count($groups)) {
2945 $groups = array('IN' => $groups);
2946 }
2947
2948 // Find all the groups that are part of a saved search.
2949 $smartGroupClause = self::buildClause("id", $op, $groups, 'Int');
2950 $sql = "
2951 SELECT id, cache_date, saved_search_id, children
2952 FROM civicrm_group
2953 WHERE $smartGroupClause
2954 AND ( saved_search_id != 0
2955 OR saved_search_id IS NOT NULL
2956 OR children IS NOT NULL )
2957 ";
2958
2959 $group = CRM_Core_DAO::executeQuery($sql);
2960
2961 while ($group->fetch()) {
2962 $this->_useDistinct = TRUE;
2963 if (!$this->_smartGroupCache || $group->cache_date == NULL) {
2964 CRM_Contact_BAO_GroupContactCache::load($group);
2965 }
2966 }
2967
2968 if (!$tableAlias) {
2969 $tableAlias = "`civicrm_group_contact_cache_";
2970 $tableAlias .= ($isNullOp) ? "a`" : implode(',', (array) $groupsIds) . "`";
2971 }
2972
2973 $this->_tables[$tableAlias] = $this->_whereTables[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.id = {$tableAlias}.contact_id ";
2974 return self::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
2975 }
2976
2977 /**
2978 * Where / qill clause for cms users
2979 *
2980 * @param $values
2981 */
2982 public function ufUser(&$values) {
2983 list($name, $op, $value, $grouping, $wildcard) = $values;
2984
2985 if ($value == 1) {
2986 $this->_tables['civicrm_uf_match'] = $this->_whereTables['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
2987
2988 $this->_qill[$grouping][] = ts('CMS User');
2989 }
2990 elseif ($value == 0) {
2991 $this->_tables['civicrm_uf_match'] = $this->_whereTables['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
2992
2993 $this->_where[$grouping][] = " civicrm_uf_match.contact_id IS NULL";
2994 $this->_qill[$grouping][] = ts('Not a CMS User');
2995 }
2996 }
2997
2998 /**
2999 * All tag search specific.
3000 *
3001 * @param array $values
3002 */
3003 public function tagSearch(&$values) {
3004 list($name, $op, $value, $grouping, $wildcard) = $values;
3005
3006 $op = "LIKE";
3007 $value = "%{$value}%";
3008
3009 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3010 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3011
3012 $etTable = "`civicrm_entity_tag-" . $value . "`";
3013 $tTable = "`civicrm_tag-" . $value . "`";
3014
3015 if ($useAllTagTypes[2]) {
3016 $this->_tables[$etTable] = $this->_whereTables[$etTable]
3017 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id)
3018 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )";
3019
3020 // search tag in cases
3021 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3022 $tCaseTable = "`civicrm_case_tag-" . $value . "`";
3023 $this->_tables[$etCaseTable] = $this->_whereTables[$etCaseTable]
3024 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3025 LEFT JOIN civicrm_case
3026 ON (civicrm_case_contact.case_id = civicrm_case.id
3027 AND civicrm_case.is_deleted = 0 )
3028 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id )
3029 LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )";
3030 // search tag in activities
3031 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3032 $tActTable = "`civicrm_act_tag-" . $value . "`";
3033 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3034 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
3035
3036 $this->_tables[$etActTable] = $this->_whereTables[$etActTable]
3037 = " LEFT JOIN civicrm_activity_contact
3038 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3039 LEFT JOIN civicrm_activity
3040 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3041 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3042 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id )
3043 LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )";
3044
3045 $this->_where[$grouping][] = "({$tTable}.name $op '" . $value . "' OR {$tCaseTable}.name $op '" . $value . "' OR {$tActTable}.name $op '" . $value . "')";
3046 $this->_qill[$grouping][] = ts('Tag %1 %2', array(1 => $tagTypesText[2], 2 => $op)) . ' ' . $value;
3047 }
3048 else {
3049 $etTable = "`civicrm_entity_tag-" . $value . "`";
3050 $tTable = "`civicrm_tag-" . $value . "`";
3051 $this->_tables[$etTable] = $this->_whereTables[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND
3052 {$etTable}.entity_table = 'civicrm_contact' )
3053 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) ";
3054
3055 $this->_where[$grouping][] = self::buildClause("{$tTable}.name", $op, $value, 'String');
3056 $this->_qill[$grouping][] = ts('Tagged %1', array(1 => $op)) . ' ' . $value;
3057 }
3058 }
3059
3060 /**
3061 * Where / qill clause for tag
3062 *
3063 * @param array $values
3064 */
3065 public function tag(&$values) {
3066 list($name, $op, $value, $grouping, $wildcard) = $values;
3067
3068 list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, array('onlyActive' => FALSE));
3069 // API/Search Builder format array(operator => array(values))
3070 if (is_array($value)) {
3071 if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
3072 $op = key($value);
3073 $value = $value[$op];
3074 }
3075 if (count($value) > 1) {
3076 $this->_useDistinct = TRUE;
3077 }
3078 $value = implode(',', (array) $value);
3079 }
3080
3081 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3082 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3083
3084 $etTable = "`civicrm_entity_tag-" . $value . "`";
3085
3086 if ($useAllTagTypes[2]) {
3087 $this->_tables[$etTable] = $this->_whereTables[$etTable]
3088 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3089
3090 // search tag in cases
3091 $etCaseTable = "`civicrm_entity_case_tag-" . $value . "`";
3092 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3093 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
3094
3095 $this->_tables[$etCaseTable] = $this->_whereTables[$etCaseTable]
3096 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3097 LEFT JOIN civicrm_case
3098 ON (civicrm_case_contact.case_id = civicrm_case.id
3099 AND civicrm_case.is_deleted = 0 )
3100 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) ";
3101 // search tag in activities
3102 $etActTable = "`civicrm_entity_act_tag-" . $value . "`";
3103 $this->_tables[$etActTable] = $this->_whereTables[$etActTable]
3104 = " LEFT JOIN civicrm_activity_contact
3105 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3106 LEFT JOIN civicrm_activity
3107 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3108 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3109 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) ";
3110
3111 // CRM-10338
3112 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3113 $this->_where[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)";
3114 }
3115 else {
3116 $this->_where[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
3117 }
3118 }
3119 else {
3120 $this->_tables[$etTable] = $this->_whereTables[$etTable]
3121 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3122
3123 // CRM-10338
3124 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3125 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3126 $op = str_replace('EMPTY', 'NULL', $op);
3127 $this->_where[$grouping][] = "{$etTable}.tag_id $op";
3128 }
3129 // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag
3130 elseif ($op == '!=') {
3131 $this->_where[$grouping][] = "{$etTable}.entity_id NOT IN (SELECT entity_id FROM civicrm_entity_tag cet WHERE cet.entity_table = 'civicrm_contact' AND " . self::buildClause("cet.tag_id", '=', $value, 'Int') . ")";
3132 }
3133 elseif ($op == '=' || strstr($op, 'IN')) {
3134 $op = ($op == '=') ? 'IN' : $op;
3135 $this->_where[$grouping][] = "{$etTable}.tag_id $op ( $value )";
3136 }
3137 }
3138 $this->_qill[$grouping][] = ts('Tagged %1 %2', array(1 => $qillop, 2 => $qillVal));
3139 }
3140
3141 /**
3142 * Where/qill clause for notes
3143 *
3144 * @param array $values
3145 */
3146 public function notes(&$values) {
3147 list($name, $op, $value, $grouping, $wildcard) = $values;
3148
3149 $noteOptionValues = $this->getWhereValues('note_option', $grouping);
3150 $noteOption = CRM_Utils_Array::value('2', $noteOptionValues, '6');
3151 $noteOption = ($name == 'note_body') ? 2 : (($name == 'note_subject') ? 3 : $noteOption);
3152
3153 $this->_useDistinct = TRUE;
3154
3155 $this->_tables['civicrm_note'] = $this->_whereTables['civicrm_note']
3156 = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) ";
3157
3158 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
3159 $n = trim($value);
3160 $value = $strtolower(CRM_Core_DAO::escapeString($n));
3161 if ($wildcard) {
3162 if (strpos($value, '%') === FALSE) {
3163 $value = "%$value%";
3164 }
3165 $op = 'LIKE';
3166 }
3167 elseif ($op == 'IS NULL' || $op == 'IS NOT NULL') {
3168 $value = NULL;
3169 }
3170
3171 $label = NULL;
3172 $clauses = array();
3173 if ($noteOption % 2 == 0) {
3174 $clauses[] = self::buildClause('civicrm_note.note', $op, $value, 'String');
3175 $label = ts('Note: Body Only');
3176 }
3177 if ($noteOption % 3 == 0) {
3178 $clauses[] = self::buildClause('civicrm_note.subject', $op, $value, 'String');
3179 $label = $label ? ts('Note: Body and Subject') : ts('Note: Subject Only');
3180 }
3181 $this->_where[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
3182 list($qillOp, $qillVal) = self::buildQillForFieldValue(NULL, $name, $n, $op);
3183 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillOp, 3 => $qillVal));
3184 }
3185
3186 /**
3187 * @param string $name
3188 * @param $op
3189 * @param $grouping
3190 *
3191 * @return bool
3192 */
3193 public function nameNullOrEmptyOp($name, $op, $grouping) {
3194 switch ($op) {
3195 case 'IS NULL':
3196 case 'IS NOT NULL':
3197 $this->_where[$grouping][] = "contact_a.$name $op";
3198 $this->_qill[$grouping][] = ts('Name') . ' ' . $op;
3199 return TRUE;
3200
3201 case 'IS EMPTY':
3202 $this->_where[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')";
3203 $this->_qill[$grouping][] = ts('Name') . ' ' . $op;
3204 return TRUE;
3205
3206 case 'IS NOT EMPTY':
3207 $this->_where[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')";
3208 $this->_qill[$grouping][] = ts('Name') . ' ' . $op;
3209 return TRUE;
3210
3211 default:
3212 return FALSE;
3213 }
3214 }
3215
3216 /**
3217 * Where / qill clause for sort_name
3218 *
3219 * @param array $values
3220 */
3221 public function sortName(&$values) {
3222 list($fieldName, $op, $value, $grouping, $wildcard) = $values;
3223
3224 // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY
3225 if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) {
3226 return;
3227 }
3228
3229 $input = $value = trim($value);
3230
3231 if (!strlen($value)) {
3232 return;
3233 }
3234
3235 $config = CRM_Core_Config::singleton();
3236
3237 $sub = array();
3238
3239 //By default, $sub elements should be joined together with OR statements (don't change this variable).
3240 $subGlue = ' OR ';
3241
3242 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
3243
3244 $firstChar = substr($value, 0, 1);
3245 $lastChar = substr($value, -1, 1);
3246 $quotes = array("'", '"');
3247 // If string is quoted, strip quotes and otherwise don't alter it
3248 if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) {
3249 $value = trim($value, implode('', $quotes));
3250 }
3251 // Replace spaces with wildcards for a LIKE operation
3252 // UNLESS string contains a comma (this exception is a tiny bit questionable)
3253 elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) {
3254 $value = str_replace(' ', '%', $value);
3255 }
3256 $value = $strtolower(CRM_Core_DAO::escapeString(trim($value)));
3257 if (strlen($value)) {
3258 $fieldsub = array();
3259 if ($wildcard && $op == 'LIKE') {
3260 if ($config->includeWildCardInName) {
3261 $value = "'%$value%'";
3262 }
3263 else {
3264 $value = "'$value%'";
3265 }
3266 $op = 'LIKE';
3267 }
3268 else {
3269 $value = "'$value'";
3270 }
3271 if ($fieldName == 'sort_name') {
3272 $wc = self::caseImportant($op) ? "LOWER(contact_a.sort_name)" : "contact_a.sort_name";
3273 }
3274 else {
3275 $wc = self::caseImportant($op) ? "LOWER(contact_a.display_name)" : "contact_a.display_name";
3276 }
3277 $fieldsub[] = " ( $wc $op $value )";
3278 if ($config->includeNickNameInName) {
3279 $wc = self::caseImportant($op) ? "LOWER(contact_a.nick_name)" : "contact_a.nick_name";
3280 $fieldsub[] = " ( $wc $op $value )";
3281 }
3282 if ($config->includeEmailInName) {
3283 $fieldsub[] = " ( civicrm_email.email $op $value ) ";
3284 }
3285 $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
3286 }
3287
3288 $sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
3289
3290 $this->_where[$grouping][] = $sub;
3291 if ($config->includeEmailInName) {
3292 $this->_tables['civicrm_email'] = $this->_whereTables['civicrm_email'] = 1;
3293 $this->_qill[$grouping][] = ts('Name or Email') . " $op - '$input'";
3294 }
3295 else {
3296 $this->_qill[$grouping][] = ts('Name') . " $op - '$input'";
3297 }
3298 }
3299
3300 /**
3301 * Where/qill clause for greeting fields.
3302 *
3303 * @param array $values
3304 */
3305 public function greetings(&$values) {
3306 list($name, $op, $value, $grouping, $wildcard) = $values;
3307 $name .= '_display';
3308
3309 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op);
3310 $this->_qill[$grouping][] = ts('Greeting %1 %2', array(1 => $qillop, 2 => $qillVal));
3311 $this->_where[$grouping][] = self::buildClause("contact_a.{$name}", $op, $value, 'String');
3312 }
3313
3314 /**
3315 * Where / qill clause for email
3316 *
3317 * @param array $values
3318 */
3319 public function email(&$values) {
3320 list($name, $op, $value, $grouping, $wildcard) = $values;
3321
3322 $n = trim($value);
3323 if ($n) {
3324 $config = CRM_Core_Config::singleton();
3325
3326 if (substr($n, 0, 1) == '"' &&
3327 substr($n, -1, 1) == '"'
3328 ) {
3329 $n = substr($n, 1, -1);
3330 $value = strtolower(CRM_Core_DAO::escapeString($n));
3331 $value = "'$value'";
3332 $op = '=';
3333 }
3334 else {
3335 $value = strtolower($n);
3336 if ($wildcard) {
3337 if (strpos($value, '%') === FALSE) {
3338 $value = "%{$value}%";
3339 }
3340 $op = 'LIKE';
3341 }
3342 }
3343 $this->_qill[$grouping][] = ts('Email') . " $op '$n'";
3344 $this->_where[$grouping][] = self::buildClause('civicrm_email.email', $op, $value, 'String');
3345 }
3346 else {
3347 $this->_qill[$grouping][] = ts('Email') . " $op ";
3348 $this->_where[$grouping][] = self::buildClause('civicrm_email.email', $op, NULL, 'String');
3349 }
3350
3351 $this->_tables['civicrm_email'] = $this->_whereTables['civicrm_email'] = 1;
3352 }
3353
3354 /**
3355 * Where / qill clause for phone number
3356 *
3357 * @param array $values
3358 */
3359 public function phone_numeric(&$values) {
3360 list($name, $op, $value, $grouping, $wildcard) = $values;
3361 // Strip non-numeric characters; allow wildcards
3362 $number = preg_replace('/[^\d%]/', '', $value);
3363 if ($number) {
3364 if (strpos($number, '%') === FALSE) {
3365 $number = "%$number%";
3366 }
3367
3368 $this->_qill[$grouping][] = ts('Phone number contains') . " $number";
3369 $this->_where[$grouping][] = self::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String');
3370 $this->_tables['civicrm_phone'] = $this->_whereTables['civicrm_phone'] = 1;
3371 }
3372 }
3373
3374 /**
3375 * Where / qill clause for phone type/location
3376 *
3377 * @param array $values
3378 */
3379 public function phone_option_group($values) {
3380 list($name, $op, $value, $grouping, $wildcard) = $values;
3381 $option = ($name == 'phone_phone_type_id' ? 'phone_type_id' : 'location_type_id');
3382 $options = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', $option);
3383 $optionName = $options[$value];
3384 $this->_qill[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ? ts('type') : ('location')) . " $op $optionName";
3385 $this->_where[$grouping][] = self::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer');
3386 $this->_tables['civicrm_phone'] = $this->_whereTables['civicrm_phone'] = 1;
3387 }
3388
3389 /**
3390 * Where / qill clause for street_address.
3391 *
3392 * @param array $values
3393 */
3394 public function street_address(&$values) {
3395 list($name, $op, $value, $grouping, $wildcard) = $values;
3396
3397 if (!$op) {
3398 $op = 'LIKE';
3399 }
3400
3401 $n = trim($value);
3402
3403 if ($n) {
3404 $value = strtolower($n);
3405 if (strpos($value, '%') === FALSE) {
3406 // only add wild card if not there
3407 $value = "%{$value}%";
3408 }
3409 $op = 'LIKE';
3410 $this->_where[$grouping][] = self::buildClause('LOWER(civicrm_address.street_address)', $op, $value, 'String');
3411 $this->_qill[$grouping][] = ts('Street') . " $op '$n'";
3412 }
3413 else {
3414 $this->_where[$grouping][] = self::buildClause('civicrm_address.street_address', $op, NULL, 'String');
3415 $this->_qill[$grouping][] = ts('Street') . " $op ";
3416 }
3417
3418 $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1;
3419 }
3420
3421 /**
3422 * Where / qill clause for street_unit.
3423 *
3424 * @param array $values
3425 */
3426 public function street_number(&$values) {
3427 list($name, $op, $value, $grouping, $wildcard) = $values;
3428
3429 if (!$op) {
3430 $op = '=';
3431 }
3432
3433 $n = trim($value);
3434
3435 if (strtolower($n) == 'odd') {
3436 $this->_where[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )";
3437 $this->_qill[$grouping][] = ts('Street Number is odd');
3438 }
3439 elseif (strtolower($n) == 'even') {
3440 $this->_where[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )";
3441 $this->_qill[$grouping][] = ts('Street Number is even');
3442 }
3443 else {
3444 $value = strtolower($n);
3445
3446 $this->_where[$grouping][] = self::buildClause('LOWER(civicrm_address.street_number)', $op, $value, 'String');
3447 $this->_qill[$grouping][] = ts('Street Number') . " $op '$n'";
3448 }
3449
3450 $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1;
3451 }
3452
3453 /**
3454 * Where / qill clause for sorting by character.
3455 *
3456 * @param array $values
3457 */
3458 public function sortByCharacter(&$values) {
3459 list($name, $op, $value, $grouping, $wildcard) = $values;
3460
3461 $name = trim($value);
3462 $cond = " contact_a.sort_name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($name)) . "%'";
3463 $this->_where[$grouping][] = $cond;
3464 $this->_qill[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', array(1 => $name));
3465 }
3466
3467 /**
3468 * Where / qill clause for including contact ids.
3469 */
3470 public function includeContactIDs() {
3471 if (!$this->_includeContactIds || empty($this->_params)) {
3472 return;
3473 }
3474
3475 $contactIds = array();
3476 foreach ($this->_params as $id => $values) {
3477 if (substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
3478 $contactIds[] = substr($values[0], CRM_Core_Form::CB_PREFIX_LEN);
3479 }
3480 }
3481 if (!empty($contactIds)) {
3482 $this->_where[0][] = " ( contact_a.id IN (" . implode(',', $contactIds) . " ) ) ";
3483 }
3484 }
3485
3486 /**
3487 * Where / qill clause for postal code.
3488 *
3489 * @param array $values
3490 */
3491 public function postalCode(&$values) {
3492 // skip if the fields dont have anything to do with postal_code
3493 if (empty($this->_fields['postal_code'])) {
3494 return;
3495 }
3496
3497 list($name, $op, $value, $grouping, $wildcard) = $values;
3498
3499 // Handle numeric postal code range searches properly by casting the column as numeric
3500 if (is_numeric($value)) {
3501 $field = 'ROUND(civicrm_address.postal_code)';
3502 $val = CRM_Utils_Type::escape($value, 'Integer');
3503 }
3504 else {
3505 $field = 'civicrm_address.postal_code';
3506 // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string.
3507 if (!is_array($value)) {
3508 $val = CRM_Utils_Type::escape($value, 'String');
3509 }
3510 else {
3511 // Do we need to escape values here? I would expect buildClause does.
3512 $val = $value;
3513 }
3514 }
3515
3516 $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1;
3517
3518 if ($name == 'postal_code') {
3519 $this->_where[$grouping][] = self::buildClause($field, $op, $val, 'String');
3520 $this->_qill[$grouping][] = ts('Postal code') . " {$op} {$value}";
3521 }
3522 elseif ($name == 'postal_code_low') {
3523 $this->_where[$grouping][] = " ( $field >= '$val' ) ";
3524 $this->_qill[$grouping][] = ts('Postal code greater than or equal to \'%1\'', array(1 => $value));
3525 }
3526 elseif ($name == 'postal_code_high') {
3527 $this->_where[$grouping][] = " ( $field <= '$val' ) ";
3528 $this->_qill[$grouping][] = ts('Postal code less than or equal to \'%1\'', array(1 => $value));
3529 }
3530 }
3531
3532 /**
3533 * Where / qill clause for location type.
3534 *
3535 * @param array $values
3536 * @param null $status
3537 *
3538 * @return string
3539 */
3540 public function locationType(&$values, $status = NULL) {
3541 list($name, $op, $value, $grouping, $wildcard) = $values;
3542
3543 if (is_array($value)) {
3544 $this->_where[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')';
3545 $this->_tables['civicrm_address'] = 1;
3546 $this->_whereTables['civicrm_address'] = 1;
3547
3548 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
3549 $names = array();
3550 foreach ($value as $id) {
3551 $names[] = $locationType[$id];
3552 }
3553
3554 $this->_primaryLocation = FALSE;
3555
3556 if (!$status) {
3557 $this->_qill[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3558 }
3559 else {
3560 return implode(' ' . ts('or') . ' ', $names);
3561 }
3562 }
3563 }
3564
3565 /**
3566 * @param $values
3567 * @param bool $fromStateProvince
3568 *
3569 * @return array|NULL
3570 */
3571 public function country(&$values, $fromStateProvince = TRUE) {
3572 list($name, $op, $value, $grouping, $wildcard) = $values;
3573
3574 if (!$fromStateProvince) {
3575 $stateValues = $this->getWhereValues('state_province', $grouping);
3576 if (!empty($stateValues)) {
3577 // return back to caller if there are state province values
3578 // since that handles this case
3579 return NULL;
3580 }
3581 }
3582
3583 $countryClause = $countryQill = NULL;
3584 if (
3585 $values &&
3586 !empty($value)
3587 ) {
3588
3589 $this->_tables['civicrm_address'] = 1;
3590 $this->_whereTables['civicrm_address'] = 1;
3591
3592 $countryClause = self::buildClause('civicrm_address.country_id', $op, $value, 'Positive');
3593 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, 'country_id', $value, $op);
3594 $countryQill = ts("%1 %2 %3", array(1 => 'Country', 2 => $qillop, 3 => $qillVal));
3595
3596 if (!$fromStateProvince) {
3597 $this->_where[$grouping][] = $countryClause;
3598 $this->_qill[$grouping][] = $countryQill;
3599 }
3600 }
3601
3602 if ($fromStateProvince) {
3603 if (!empty($countryClause)) {
3604 return array(
3605 $countryClause,
3606 " ...AND... " . $countryQill,
3607 );
3608 }
3609 else {
3610 return array(NULL, NULL);
3611 }
3612 }
3613 }
3614
3615 /**
3616 * Where / qill clause for county (if present).
3617 *
3618 * @param array $values
3619 * @param null $status
3620 *
3621 * @return string
3622 */
3623 public function county(&$values, $status = NULL) {
3624 list($name, $op, $value, $grouping, $wildcard) = $values;
3625
3626 if (!is_array($value)) {
3627 // force the county to be an array
3628 $value = array($value);
3629 }
3630
3631 // check if the values are ids OR names of the counties
3632 $inputFormat = 'id';
3633 foreach ($value as $v) {
3634 if (!is_numeric($v)) {
3635 $inputFormat = 'name';
3636 break;
3637 }
3638 }
3639 $names = array();
3640 if ($op == '=') {
3641 $op = 'IN';
3642 }
3643 elseif ($op == '!=') {
3644 $op = 'NOT IN';
3645 }
3646 else {
3647 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3648 $op = str_replace('EMPTY', 'NULL', $op);
3649 }
3650
3651 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3652 $clause = "civicrm_address.county_id $op";
3653 }
3654 elseif ($inputFormat == 'id') {
3655 $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')';
3656
3657 $county = CRM_Core_PseudoConstant::county();
3658 foreach ($value as $id) {
3659 $names[] = CRM_Utils_Array::value($id, $county);
3660 }
3661 }
3662 else {
3663 $inputClause = array();
3664 $county = CRM_Core_PseudoConstant::county();
3665 foreach ($value as $name) {
3666 $name = trim($name);
3667 $inputClause[] = CRM_Utils_Array::key($name, $county);
3668 }
3669 $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')';
3670 $names = $value;
3671 }
3672 $this->_tables['civicrm_address'] = 1;
3673 $this->_whereTables['civicrm_address'] = 1;
3674
3675 $this->_where[$grouping][] = $clause;
3676 if (!$status) {
3677 $this->_qill[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3678 }
3679 else {
3680 return implode(' ' . ts('or') . ' ', $names);
3681 }
3682 }
3683
3684 /**
3685 * Where / qill clause for state/province AND country (if present).
3686 *
3687 * @param array $values
3688 * @param null $status
3689 *
3690 * @return string
3691 */
3692 public function stateProvince(&$values, $status = NULL) {
3693 list($name, $op, $value, $grouping, $wildcard) = $values;
3694
3695 // quick escape for IS NULL
3696 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3697 $value = NULL;
3698 }
3699 elseif (!is_array($value)) {
3700 // force the state to be an array
3701 // check if its in the mapper format!
3702 $values = self::parseSearchBuilderString($value);
3703 if (is_array($values)) {
3704 $value = $values;
3705 }
3706 else {
3707 $value = array($value);
3708 }
3709 }
3710
3711 // check if the values are ids OR names of the states
3712 $inputFormat = 'id';
3713 if ($value) {
3714 foreach ($value as $v) {
3715 if (!is_numeric($v)) {
3716 $inputFormat = 'name';
3717 break;
3718 }
3719 }
3720 }
3721
3722 $names = array();
3723 if ($op == '=') {
3724 $op = 'IN';
3725 }
3726 elseif ($op == '!=') {
3727 $op = 'NOT IN';
3728 }
3729 else {
3730 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3731 $op = str_replace('EMPTY', 'NULL', $op);
3732 }
3733 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3734 $stateClause = "civicrm_address.state_province_id $op";
3735 }
3736 elseif ($inputFormat == 'id') {
3737 if ($op != 'NOT IN') {
3738 $op = 'IN';
3739 }
3740 $stateClause = "civicrm_address.state_province_id $op (" . implode(',', $value) . ')';
3741
3742 foreach ($value as $id) {
3743 $names[] = CRM_Core_PseudoConstant::stateProvince($id, FALSE);
3744 }
3745 }
3746 else {
3747 $inputClause = array();
3748 $stateProvince = CRM_Core_PseudoConstant::stateProvince();
3749 foreach ($value as $name) {
3750 $name = trim($name);
3751 $inputClause[] = CRM_Utils_Array::key($name, $stateProvince);
3752 }
3753 $stateClause = "civicrm_address.state_province_id $op (" . implode(',', $inputClause) . ')';
3754 $names = $value;
3755 }
3756 $this->_tables['civicrm_address'] = 1;
3757 $this->_whereTables['civicrm_address'] = 1;
3758
3759 $countryValues = $this->getWhereValues('country', $grouping);
3760 list($countryClause, $countryQill) = $this->country($countryValues, TRUE);
3761
3762 if ($countryClause) {
3763 $clause = "( $stateClause AND $countryClause )";
3764 }
3765 else {
3766 $clause = $stateClause;
3767 }
3768
3769 $this->_where[$grouping][] = $clause;
3770 if (!$status) {
3771 $this->_qill[$grouping][] = ts('State/Province') . " $op " . implode(' ' . ts('or') . ' ', $names) . $countryQill;
3772 }
3773 else {
3774 return implode(' ' . ts('or') . ' ', $names) . $countryQill;
3775 }
3776 }
3777
3778 /**
3779 * Where / qill clause for change log.
3780 *
3781 * @param array $values
3782 */
3783 public function changeLog(&$values) {
3784 list($name, $op, $value, $grouping, $wildcard) = $values;
3785
3786 $targetName = $this->getWhereValues('changed_by', $grouping);
3787 if (!$targetName) {
3788 return;
3789 }
3790
3791 $name = trim($targetName[2]);
3792 $name = strtolower(CRM_Core_DAO::escapeString($name));
3793 $name = $targetName[4] ? "%$name%" : $name;
3794 $this->_where[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
3795 $this->_tables['civicrm_log'] = $this->_whereTables['civicrm_log'] = 1;
3796 $this->_qill[$grouping][] = ts('Modified By') . " $name";
3797 }
3798
3799 /**
3800 * @param $values
3801 */
3802 public function modifiedDates($values) {
3803 $this->_useDistinct = TRUE;
3804
3805 // CRM-11281, default to added date if not set
3806 $fieldTitle = ts('Added Date');
3807 $fieldName = 'created_date';
3808 foreach (array_keys($this->_params) as $id) {
3809 if ($this->_params[$id][0] == 'log_date') {
3810 if ($this->_params[$id][2] == 2) {
3811 $fieldTitle = ts('Modified Date');
3812 $fieldName = 'modified_date';
3813 }
3814 }
3815 }
3816
3817 $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle);
3818
3819 self::$_openedPanes[ts('Change Log')] = TRUE;
3820 }
3821
3822 /**
3823 * @param $values
3824 */
3825 public function demographics(&$values) {
3826 list($name, $op, $value, $grouping, $wildcard) = $values;
3827
3828 if (($name == 'age_low') || ($name == 'age_high')) {
3829 $this->ageRangeQueryBuilder($values,
3830 'contact_a', 'age', 'birth_date', ts('Age')
3831 );
3832 }
3833 elseif (($name == 'birth_date_low') || ($name == 'birth_date_high')) {
3834
3835 $this->dateQueryBuilder($values,
3836 'contact_a', 'birth_date', 'birth_date', ts('Birth Date')
3837 );
3838 }
3839 elseif (($name == 'deceased_date_low') || ($name == 'deceased_date_high')) {
3840
3841 $this->dateQueryBuilder($values,
3842 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date')
3843 );
3844 }
3845
3846 self::$_openedPanes[ts('Demographics')] = TRUE;
3847 }
3848
3849 /**
3850 * @param $values
3851 */
3852 public function privacy(&$values) {
3853 list($name, $op, $value, $grouping, $wildcard) = $values;
3854 //fixed for profile search listing CRM-4633
3855 if (strpbrk($value, "[")) {
3856 $value = "'{$value}'";
3857 $op = "!{$op}";
3858 $this->_where[$grouping][] = "contact_a.{$name} $op $value";
3859 }
3860 else {
3861 $this->_where[$grouping][] = "contact_a.{$name} $op $value";
3862 }
3863 $field = CRM_Utils_Array::value($name, $this->_fields);
3864 $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
3865 $title = $field ? $field['title'] : $name;
3866 $this->_qill[$grouping][] = "$title $op $value";
3867 }
3868
3869 /**
3870 * @param $values
3871 */
3872 public function privacyOptions($values) {
3873 list($name, $op, $value, $grouping, $wildcard) = $values;
3874
3875 if (empty($value) || !is_array($value)) {
3876 return;
3877 }
3878
3879 // get the operator and toggle values
3880 $opValues = $this->getWhereValues('privacy_operator', $grouping);
3881 $operator = 'OR';
3882 if ($opValues &&
3883 strtolower($opValues[2] == 'AND')
3884 ) {
3885 $operator = 'AND';
3886 }
3887
3888 $toggleValues = $this->getWhereValues('privacy_toggle', $grouping);
3889 $compareOP = '!';
3890 if ($toggleValues &&
3891 $toggleValues[2] == 2
3892 ) {
3893 $compareOP = '';
3894 }
3895
3896 $clauses = array();
3897 $qill = array();
3898 foreach ($value as $dontCare => $pOption) {
3899 $clauses[] = " ( contact_a.{$pOption} = 1 ) ";
3900 $field = CRM_Utils_Array::value($pOption, $this->_fields);
3901 $title = $field ? $field['title'] : $pOption;
3902 $qill[] = " $title = 1 ";
3903 }
3904
3905 $this->_where[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )';
3906 $this->_qill[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )';
3907 }
3908
3909 /**
3910 * @param $values
3911 */
3912 public function preferredCommunication(&$values) {
3913 list($name, $op, $value, $grouping, $wildcard) = $values;
3914
3915 if (!is_array($value)) {
3916 $value = str_replace(array('(', ')'), '', explode(",", $value));
3917 }
3918 elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
3919 $op = key($value);
3920 $value = $value[$op];
3921 }
3922 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
3923
3924 if (self::caseImportant($op)) {
3925 $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value);
3926 $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE';
3927 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
3928 }
3929
3930 $this->_where[$grouping][] = self::buildClause("contact_a.preferred_communication_method", $op, $value);
3931 $this->_qill[$grouping][] = ts('Preferred Communication Method %1 %2', array(1 => $qillop, 2 => $qillVal));
3932 }
3933
3934 /**
3935 * Where / qill clause for relationship.
3936 *
3937 * @param array $values
3938 */
3939 public function relationship(&$values) {
3940 list($name, $op, $value, $grouping, $wildcard) = $values;
3941 if ($this->_relationshipValuesAdded) {
3942 return;
3943 }
3944 // also get values array for relation_target_name
3945 // for relationship search we always do wildcard
3946 $relationType = $this->getWhereValues('relation_type_id', $grouping);
3947 $targetName = $this->getWhereValues('relation_target_name', $grouping);
3948 $relStatus = $this->getWhereValues('relation_status', $grouping);
3949 $relPermission = $this->getWhereValues('relation_permission', $grouping);
3950 $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
3951
3952 $nameClause = $name = NULL;
3953 if ($targetName) {
3954 $name = trim($targetName[2]);
3955 if (substr($name, 0, 1) == '"' &&
3956 substr($name, -1, 1) == '"'
3957 ) {
3958 $name = substr($name, 1, -1);
3959 $name = strtolower(CRM_Core_DAO::escapeString($name));
3960 $nameClause = "= '$name'";
3961 }
3962 else {
3963 $name = strtolower(CRM_Core_DAO::escapeString($name));
3964 $nameClause = "LIKE '%{$name}%'";
3965 }
3966 }
3967
3968 $rTypeValues = array();
3969 if (!empty($relationType)) {
3970 $rel = explode('_', $relationType[2]);
3971 self::$_relType = $rel[1];
3972 $params = array('id' => $rel[0]);
3973 $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues);
3974 }
3975 if (!empty($rTypeValues) && $rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
3976 // if we don't know which end of the relationship we are dealing with we'll create a temp table
3977 //@todo unless we are dealing with a target group
3978 self::$_relType = 'reciprocal';
3979 }
3980 // if we are creating a temp table we build our own where for the relationship table
3981 $relationshipTempTable = NULL;
3982 if (self::$_relType == 'reciprocal' && empty($targetGroup)) {
3983 $where = array();
3984 self::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO::createTempTableName('civicrm_rel');
3985 if ($nameClause) {
3986 $where[$grouping][] = " sort_name $nameClause ";
3987 }
3988 }
3989 else {
3990 $where = &$this->_where;
3991 if ($nameClause) {
3992 $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )";
3993 }
3994 }
3995
3996 $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
3997
3998 if ($nameClause || !$targetGroup) {
3999 if (!empty($relationType)) {
4000 $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] . " $name";
4001 }
4002 else {
4003 $this->_qill[$grouping][] = $name;
4004 }
4005 }
4006
4007 //check to see if the target contact is in specified group
4008 if ($targetGroup) {
4009 //add contacts from static groups
4010 $this->_tables['civicrm_relationship_group_contact'] = $this->_whereTables['civicrm_relationship_group_contact']
4011 = " 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'";
4012 $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" .
4013 implode(",", $targetGroup[2]) . ") ) ";
4014
4015 //add contacts from saved searches
4016 $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", "contact_b", $op);
4017
4018 //set the group where clause
4019 if ($ssWhere) {
4020 $groupWhere[] = "( " . $ssWhere . " )";
4021 }
4022 $this->_where[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
4023
4024 //Get the names of the target groups for the qill
4025 $groupNames = CRM_Core_PseudoConstant::group();
4026 $qillNames = array();
4027 foreach ($targetGroup[2] as $groupId) {
4028 if (array_key_exists($groupId, $groupNames)) {
4029 $qillNames[] = $groupNames[$groupId];
4030 }
4031 }
4032 if (!empty($relationType)) {
4033 $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] . " ( " . implode(", ", $qillNames) . " )";
4034 }
4035 else {
4036 $this->_qill[$grouping][] = implode(", ", $qillNames);
4037 }
4038 }
4039
4040 // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
4041 $today = date('Ymd');
4042 //check for active, inactive and all relation status
4043 if ($relStatus[2] == 0) {
4044 $where[$grouping][] = "(
4045 civicrm_relationship.is_active = 1 AND
4046 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
4047 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
4048 )";
4049 $this->_qill[$grouping][] = ts('Relationship - Active and Current');
4050 }
4051 elseif ($relStatus[2] == 1) {
4052 $where[$grouping][] = "(
4053 civicrm_relationship.is_active = 0 OR
4054 civicrm_relationship.end_date < {$today} OR
4055 civicrm_relationship.start_date > {$today}
4056 )";
4057 $this->_qill[$grouping][] = ts('Relationship - Inactive or not Current');
4058 }
4059
4060 $onlyDeleted = 0;
4061 if (in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params)) {
4062 $onlyDeleted = 1;
4063 }
4064 $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
4065
4066 //check for permissioned, non-permissioned and all permissioned relations
4067 if ($relPermission[2] == 1) {
4068 $where[$grouping][] = "(
4069 civicrm_relationship.is_permission_a_b = 1
4070 )";
4071 $this->_qill[$grouping][] = ts('Relationship - Permissioned');
4072 }
4073 elseif ($relPermission[2] == 2) {
4074 //non-allowed permission relationship.
4075 $where[$grouping][] = "(
4076 civicrm_relationship.is_permission_a_b = 0
4077 )";
4078 $this->_qill[$grouping][] = ts('Relationship - Non-permissioned');
4079 }
4080
4081 $this->addRelationshipDateClauses($grouping, $where);
4082 if (!empty($relationType) && !empty($rType) && isset($rType->id)) {
4083 $where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id;
4084 }
4085 $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
4086 $this->_useDistinct = TRUE;
4087 $this->_relationshipValuesAdded = TRUE;
4088 // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
4089 // join on that,
4090 // @todo creating a temp table could be expanded to group filter
4091 // as even creating a temp table of all relationships is much much more efficient than
4092 // an OR in the join
4093 if ($relationshipTempTable) {
4094 $whereClause = '';
4095 if (!empty($where[$grouping])) {
4096 $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
4097 $whereClause = str_replace('contact_b', 'c', $whereClause);
4098 }
4099 $sql = "
4100 CREATE TEMPORARY TABLE {$relationshipTempTable}
4101 (SELECT contact_id_b as contact_id, civicrm_relationship.id
4102 FROM civicrm_relationship
4103 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
4104 $whereClause )
4105 UNION
4106 (SELECT contact_id_a as contact_id, civicrm_relationship.id
4107 FROM civicrm_relationship
4108 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id
4109 $whereClause )
4110 ";
4111 CRM_Core_DAO::executeQuery($sql);
4112 }
4113
4114 }
4115
4116 /**
4117 * Add start & end date criteria in
4118 * @param string $grouping
4119 * @param array $where
4120 * = array to add where clauses to, in case you are generating a temp table.
4121 * not the main query.
4122 */
4123 public function addRelationshipDateClauses($grouping, &$where) {
4124 $dateValues = array();
4125 $dateTypes = array(
4126 'start_date',
4127 'end_date',
4128 );
4129
4130 foreach ($dateTypes as $dateField) {
4131 $dateValueLow = $this->getWhereValues('relation_' . $dateField . '_low', $grouping);
4132 $dateValueHigh = $this->getWhereValues('relation_' . $dateField . '_high', $grouping);
4133 if (!empty($dateValueLow)) {
4134 $date = date('Ymd', strtotime($dateValueLow[2]));
4135 $where[$grouping][] = "civicrm_relationship.$dateField >= $date";
4136 $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);
4137 }
4138 if (!empty($dateValueHigh)) {
4139 $date = date('Ymd', strtotime($dateValueHigh[2]));
4140 $where[$grouping][] = "civicrm_relationship.$dateField <= $date";
4141 $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);
4142 }
4143 }
4144 }
4145
4146 /**
4147 * Default set of return properties.
4148 *
4149 * @param int $mode
4150 *
4151 * @return array
4152 * derault return properties
4153 */
4154 public static function &defaultReturnProperties($mode = 1) {
4155 if (!isset(self::$_defaultReturnProperties)) {
4156 self::$_defaultReturnProperties = array();
4157 }
4158
4159 if (!isset(self::$_defaultReturnProperties[$mode])) {
4160 // add activity return properties
4161 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
4162 self::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query::defaultReturnProperties($mode, FALSE);
4163 }
4164 else {
4165 self::$_defaultReturnProperties[$mode] = CRM_Core_Component::defaultReturnProperties($mode, FALSE);
4166 }
4167
4168 if (empty(self::$_defaultReturnProperties[$mode])) {
4169 self::$_defaultReturnProperties[$mode] = array(
4170 'home_URL' => 1,
4171 'image_URL' => 1,
4172 'legal_identifier' => 1,
4173 'external_identifier' => 1,
4174 'contact_type' => 1,
4175 'contact_sub_type' => 1,
4176 'sort_name' => 1,
4177 'display_name' => 1,
4178 'preferred_mail_format' => 1,
4179 'nick_name' => 1,
4180 'first_name' => 1,
4181 'middle_name' => 1,
4182 'last_name' => 1,
4183 'prefix_id' => 1,
4184 'suffix_id' => 1,
4185 'formal_title' => 1,
4186 'communication_style_id' => 1,
4187 'birth_date' => 1,
4188 'gender_id' => 1,
4189 'street_address' => 1,
4190 'supplemental_address_1' => 1,
4191 'supplemental_address_2' => 1,
4192 'city' => 1,
4193 'postal_code' => 1,
4194 'postal_code_suffix' => 1,
4195 'state_province' => 1,
4196 'country' => 1,
4197 'world_region' => 1,
4198 'geo_code_1' => 1,
4199 'geo_code_2' => 1,
4200 'email' => 1,
4201 'on_hold' => 1,
4202 'phone' => 1,
4203 'im' => 1,
4204 'household_name' => 1,
4205 'organization_name' => 1,
4206 'deceased_date' => 1,
4207 'is_deceased' => 1,
4208 'job_title' => 1,
4209 'legal_name' => 1,
4210 'sic_code' => 1,
4211 'current_employer' => 1,
4212 // FIXME: should we use defaultHierReturnProperties() for the below?
4213 'do_not_email' => 1,
4214 'do_not_mail' => 1,
4215 'do_not_sms' => 1,
4216 'do_not_phone' => 1,
4217 'do_not_trade' => 1,
4218 'is_opt_out' => 1,
4219 'contact_is_deleted' => 1,
4220 'preferred_communication_method' => 1,
4221 'preferred_language' => 1,
4222 );
4223 }
4224 }
4225 return self::$_defaultReturnProperties[$mode];
4226 }
4227
4228 /**
4229 * Get primary condition for a sql clause.
4230 *
4231 * @param int $value
4232 *
4233 * @return string|NULL
4234 */
4235 public static function getPrimaryCondition($value) {
4236 if (is_numeric($value)) {
4237 $value = (int ) $value;
4238 return ($value == 1) ? 'is_primary = 1' : 'is_primary = 0';
4239 }
4240 return NULL;
4241 }
4242
4243 /**
4244 * Wrapper for a simple search query.
4245 *
4246 * @param array $params
4247 * @param array $returnProperties
4248 * @param bool $count
4249 *
4250 * @return string
4251 */
4252 public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) {
4253 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
4254 list($select, $from, $where, $having) = $query->query();
4255
4256 return "$select $from $where $having";
4257 }
4258
4259 /**
4260 * These are stub comments as this function needs more explanation - particularly in terms of how it
4261 * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.
4262 *
4263 * This function was originally written as a wrapper for the api query but is called from multiple places
4264 * in the core code directly so the name is misleading. This function does not use the searchQuery function
4265 * but it is unclear as to whehter that is historical or there is a reason
4266 * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function
4267 *
4268 * @param array $params
4269 * @param array $returnProperties
4270 * @param null $fields
4271 * @param string $sort
4272 * @param int $offset
4273 * @param int $row_count
4274 * @param bool $smartGroupCache
4275 * ?? update smart group cache?.
4276 * @param bool $count
4277 * Return count obnly.
4278 * @param bool $skipPermissions
4279 * Should permissions be ignored or should the logged in user's permissions be applied.
4280 *
4281 *
4282 * @return array
4283 */
4284 public static function apiQuery(
4285 $params = NULL,
4286 $returnProperties = NULL,
4287 $fields = NULL,
4288 $sort = NULL,
4289 $offset = 0,
4290 $row_count = 25,
4291 $smartGroupCache = TRUE,
4292 $count = FALSE,
4293 $skipPermissions = TRUE
4294 ) {
4295
4296 $query = new CRM_Contact_BAO_Query(
4297 $params, $returnProperties,
4298 NULL, TRUE, FALSE, 1,
4299 $skipPermissions,
4300 TRUE, $smartGroupCache
4301 );
4302
4303 //this should add a check for view deleted if permissions are enabled
4304 if ($skipPermissions) {
4305 $query->_skipDeleteClause = TRUE;
4306 }
4307 $query->generatePermissionClause(FALSE, $count);
4308
4309 // note : this modifies _fromClause and _simpleFromClause
4310 $query->includePseudoFieldsJoin($sort);
4311
4312 list($select, $from, $where, $having) = $query->query($count);
4313
4314 $options = $query->_options;
4315 if (!empty($query->_permissionWhereClause)) {
4316 if (empty($where)) {
4317 $where = "WHERE $query->_permissionWhereClause";
4318 }
4319 else {
4320 $where = "$where AND $query->_permissionWhereClause";
4321 }
4322 }
4323
4324 $sql = "$select $from $where $having";
4325
4326 // add group by
4327 if ($query->_useGroupBy) {
4328 $sql .= ' GROUP BY contact_a.id';
4329 }
4330 if (!empty($sort)) {
4331 $sort = CRM_Utils_Type::escape($sort, 'String');
4332 $sql .= " ORDER BY $sort ";
4333 }
4334 if ($row_count > 0 && $offset >= 0) {
4335 $offset = CRM_Utils_Type::escape($offset, 'Int');
4336 $rowCount = CRM_Utils_Type::escape($row_count, 'Int');
4337 $sql .= " LIMIT $offset, $row_count ";
4338 }
4339
4340 $dao = CRM_Core_DAO::executeQuery($sql);
4341
4342 $values = array();
4343 while ($dao->fetch()) {
4344 if ($count) {
4345 $noRows = $dao->rowCount;
4346 $dao->free();
4347 return array($noRows, NULL);
4348 }
4349 $val = $query->store($dao);
4350 $convertedVals = $query->convertToPseudoNames($dao, TRUE);
4351
4352 if (!empty($convertedVals)) {
4353 $val = array_replace_recursive($val, $convertedVals);
4354 }
4355 $values[$dao->contact_id] = $val;
4356 }
4357 $dao->free();
4358 return array($values, $options);
4359 }
4360
4361 /**
4362 * Get the actual custom field name by stripping off the appended string.
4363 *
4364 * The string could be _relative, _from, or _to
4365 *
4366 * @todo use metadata rather than convention to do this.
4367 *
4368 * @param string $parameterName
4369 * The name of the parameter submitted to the form.
4370 * e.g
4371 * custom_3_relative
4372 * custom_3_from
4373 *
4374 * @return string
4375 */
4376 public static function getCustomFieldName($parameterName) {
4377 if (substr($parameterName, -5, 5) == '_from') {
4378 return substr($parameterName, 0, strpos($parameterName, '_from'));
4379 }
4380 if (substr($parameterName, -9, 9) == '_relative') {
4381 return substr($parameterName, 0, strpos($parameterName, '_relative'));
4382 }
4383 if (substr($parameterName, -3, 3) == '_to') {
4384 return substr($parameterName, 0, strpos($parameterName, '_to'));
4385 }
4386 }
4387
4388 /**
4389 * Convert submitted values for relative custom date fields to query object format.
4390 *
4391 * The query will support the sqlOperator format so convert to that format.
4392 *
4393 * @param array $formValues
4394 * Submitted values.
4395 * @param array $params
4396 * Converted parameters for the query object.
4397 * @param string $values
4398 * Submitted value.
4399 * @param string $fieldName
4400 * Submitted field name. (Matches form field not DB field.)
4401 */
4402 protected static function convertCustomDateRelativeFields(&$formValues, &$params, $values, $fieldName) {
4403 if (empty($values)) {
4404 // e.g we might have relative set & from & to empty. The form flow is a bit funky &
4405 // this function gets called again after they fields have been converted which can get ugly.
4406 return;
4407 }
4408 $customFieldName = self::getCustomFieldName($fieldName);
4409
4410 if (substr($fieldName, -9, 9) == '_relative') {
4411 list($from, $to) = CRM_Utils_Date::getFromTo($values, NULL, NULL);
4412 }
4413 else {
4414 if ($fieldName == $customFieldName . '_to' && CRM_Utils_Array::value($customFieldName . '_from', $formValues)) {
4415 // Both to & from are set. We only need to acton one, choosing from.
4416 return;
4417 }
4418
4419 list($from, $to) = CRM_Utils_Date::getFromTo(
4420 NULL,
4421 (empty($formValues[$customFieldName . '_from']) ? NULL : $formValues[$customFieldName . '_from']),
4422 CRM_Utils_Array::value($customFieldName . '_to', $formValues)
4423 );
4424 }
4425
4426 if ($from) {
4427 if ($to) {
4428 $relativeFunction = array('BETWEEN' => array($from, $to));
4429 }
4430 else {
4431 $relativeFunction = array('>=' => $from);
4432 }
4433 }
4434 else {
4435 $relativeFunction = array('<=' => $to);
4436 }
4437 $params[] = array(
4438 $customFieldName,
4439 '=',
4440 $relativeFunction,
4441 0,
4442 0,
4443 );
4444 }
4445
4446 /**
4447 * Has this field already been reformatting to Query object syntax.
4448 *
4449 * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems
4450 * to come with associated double queries & is possibly damaging performance.
4451 *
4452 * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns
4453 * them as they are.
4454 *
4455 * @param mixed $values
4456 * Value in formValues for the field.
4457 *
4458 * @return bool;
4459 */
4460 protected static function isAlreadyProcessedForQueryFormat($values) {
4461 if (!is_array($values)) {
4462 return FALSE;
4463 }
4464 if (($operator = CRM_Utils_Array::value(1, $values)) == FALSE) {
4465 return FALSE;
4466 }
4467 return in_array($operator, CRM_Core_DAO::acceptedSQLOperators());
4468 }
4469
4470 /**
4471 * Create and query the db for an contact search.
4472 *
4473 * @param int $offset
4474 * The offset for the query.
4475 * @param int $rowCount
4476 * The number of rows to return.
4477 * @param string $sort
4478 * The order by string.
4479 * @param bool $count
4480 * Is this a count only query ?.
4481 * @param bool $includeContactIds
4482 * Should we include contact ids?.
4483 * @param bool $sortByChar
4484 * If true returns the distinct array of first characters for search results.
4485 * @param bool $groupContacts
4486 * If true, return only the contact ids.
4487 * @param bool $returnQuery
4488 * Should we return the query as a string.
4489 * @param string $additionalWhereClause
4490 * If the caller wants to further restrict the search (used for components).
4491 * @param null $sortOrder
4492 * @param string $additionalFromClause
4493 * Should be clause with proper joins, effective to reduce where clause load.
4494 *
4495 * @param bool $skipOrderAndLimit
4496 *
4497 * @return CRM_Core_DAO
4498 */
4499 public function searchQuery(
4500 $offset = 0, $rowCount = 0, $sort = NULL,
4501 $count = FALSE, $includeContactIds = FALSE,
4502 $sortByChar = FALSE, $groupContacts = FALSE,
4503 $returnQuery = FALSE,
4504 $additionalWhereClause = NULL, $sortOrder = NULL,
4505 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE
4506 ) {
4507
4508 if ($includeContactIds) {
4509 $this->_includeContactIds = TRUE;
4510 $this->_whereClause = $this->whereClause();
4511 }
4512
4513 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params);
4514
4515 // if we’re explicitly looking for a certain contact’s contribs, events, etc.
4516 // and that contact happens to be deleted, set $onlyDeleted to true
4517 foreach ($this->_params as $values) {
4518 $name = CRM_Utils_Array::value(0, $values);
4519 $op = CRM_Utils_Array::value(1, $values);
4520 $value = CRM_Utils_Array::value(2, $values);
4521 if ($name == 'contact_id' and $op == '=') {
4522 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) {
4523 $onlyDeleted = TRUE;
4524 }
4525 break;
4526 }
4527 }
4528
4529 // building the query string
4530 $groupBy = NULL;
4531 if (!$count) {
4532 if (isset($this->_groupByComponentClause)) {
4533 $groupBy = $this->_groupByComponentClause;
4534 }
4535 elseif ($this->_useGroupBy) {
4536 $groupBy = ' GROUP BY contact_a.id';
4537 }
4538 }
4539 if ($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY && (!$count)) {
4540 $groupBy = 'GROUP BY civicrm_activity.id ';
4541 }
4542
4543 $order = $orderBy = $limit = '';
4544 if (!$count) {
4545 $config = CRM_Core_Config::singleton();
4546 if ($config->includeOrderByClause ||
4547 isset($this->_distinctComponentClause)
4548 ) {
4549 if ($sort) {
4550 if (is_string($sort)) {
4551 $orderBy = $sort;
4552 }
4553 else {
4554 $orderBy = trim($sort->orderBy());
4555 }
4556 if (!empty($orderBy)) {
4557 // this is special case while searching for
4558 // change log CRM-1718
4559 if (preg_match('/sort_name/i', $orderBy)) {
4560 $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy);
4561 }
4562
4563 $orderBy = CRM_Utils_Type::escape($orderBy, 'String');
4564 $order = " ORDER BY $orderBy";
4565
4566 if ($sortOrder) {
4567 $sortOrder = CRM_Utils_Type::escape($sortOrder, 'String');
4568 $order .= " $sortOrder";
4569 }
4570
4571 // always add contact_a.id to the ORDER clause
4572 // so the order is deterministic
4573 if (strpos('contact_a.id', $order) === FALSE) {
4574 $order .= ", contact_a.id";
4575 }
4576 }
4577 }
4578 elseif ($sortByChar) {
4579 $order = " ORDER BY UPPER(LEFT(contact_a.sort_name, 1)) asc";
4580 }
4581 else {
4582 $order = " ORDER BY contact_a.sort_name asc, contact_a.id";
4583 }
4584 }
4585
4586 // hack for order clause
4587 if ($order) {
4588 $fieldStr = trim(str_replace('ORDER BY', '', $order));
4589 $fieldOrder = explode(' ', $fieldStr);
4590 $field = $fieldOrder[0];
4591
4592 if ($field) {
4593 switch ($field) {
4594 case 'city':
4595 case 'postal_code':
4596 $this->_whereTables["civicrm_address"] = 1;
4597 $order = str_replace($field, "civicrm_address.{$field}", $order);
4598 break;
4599
4600 case 'country':
4601 case 'state_province':
4602 $this->_whereTables["civicrm_{$field}"] = 1;
4603 $order = str_replace($field, "civicrm_{$field}.name", $order);
4604 break;
4605
4606 case 'email':
4607 $this->_whereTables["civicrm_email"] = 1;
4608 $order = str_replace($field, "civicrm_email.{$field}", $order);
4609 break;
4610
4611 default:
4612 //CRM-12565 add "`" around $field if it is a pseudo constant
4613 foreach ($this->_pseudoConstantsSelect as $key => $value) {
4614 if (!empty($value['element']) && $value['element'] == $field) {
4615 $order = str_replace($field, "`{$field}`", $order);
4616 }
4617 }
4618 }
4619 $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode);
4620 $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode);
4621 }
4622 }
4623
4624 if ($rowCount > 0 && $offset >= 0) {
4625 $offset = CRM_Utils_Type::escape($offset, 'Int');
4626 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
4627 $limit = " LIMIT $offset, $rowCount ";
4628 }
4629 }
4630
4631 // CRM-15231
4632 $this->_sort = $sort;
4633
4634 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);
4635
4636 if ($additionalWhereClause) {
4637 $where = $where . ' AND ' . $additionalWhereClause;
4638 }
4639
4640 //additional from clause should be w/ proper joins.
4641 if ($additionalFromClause) {
4642 $from .= "\n" . $additionalFromClause;
4643 }
4644
4645 // if we are doing a transform, do it here
4646 // use the $from, $where and $having to get the contact ID
4647 if ($this->_displayRelationshipType) {
4648 $this->filterRelatedContacts($from, $where, $having);
4649 }
4650
4651 if ($skipOrderAndLimit) {
4652 $query = "$select $from $where $having $groupBy";
4653 }
4654 else {
4655 $query = "$select $from $where $having $groupBy $order $limit";
4656 }
4657
4658 if ($returnQuery) {
4659 return $query;
4660 }
4661 if ($count) {
4662 return CRM_Core_DAO::singleValueQuery($query);
4663 }
4664
4665 $dao = CRM_Core_DAO::executeQuery($query);
4666 if ($groupContacts) {
4667 $ids = array();
4668 while ($dao->fetch()) {
4669 $ids[] = $dao->id;
4670 }
4671 return implode(',', $ids);
4672 }
4673
4674 return $dao;
4675 }
4676
4677 /**
4678 * Fetch a list of contacts from the prev/next cache for displaying a search results page
4679 *
4680 * @param string $cacheKey
4681 * @param int $offset
4682 * @param int $rowCount
4683 * @param bool $includeContactIds
4684 * @return CRM_Core_DAO
4685 */
4686 public function getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds) {
4687 $this->_includeContactIds = $includeContactIds;
4688 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params);
4689 list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
4690 $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);
4691 $order = " ORDER BY pnc.id";
4692 $groupBy = " GROUP BY contact_a.id";
4693 $limit = " LIMIT $offset, $rowCount";
4694 $query = "$select $from $where $groupBy $order $limit";
4695
4696 return CRM_Core_DAO::executeQuery($query);
4697 }
4698
4699 /**
4700 * Populate $this->_permissionWhereClause with permission related clause and update other
4701 * query related properties.
4702 *
4703 * Function calls ACL permission class and hooks to filter the query appropriately
4704 *
4705 * Note that these 2 params were in the code when extracted from another function
4706 * and a second round extraction would be to make them properties of the class
4707 *
4708 * @param bool $onlyDeleted
4709 * Only get deleted contacts.
4710 * @param bool $count
4711 * Return Count only.
4712 */
4713 public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) {
4714 if (!$this->_skipPermission) {
4715 $this->_permissionWhereClause = CRM_ACL_API::whereClause(
4716 CRM_Core_Permission::VIEW,
4717 $this->_tables,
4718 $this->_whereTables,
4719 NULL,
4720 $onlyDeleted,
4721 $this->_skipDeleteClause
4722 );
4723
4724 // regenerate fromClause since permission might have added tables
4725 if ($this->_permissionWhereClause) {
4726 //fix for row count in qill (in contribute/membership find)
4727 if (!$count) {
4728 $this->_useDistinct = TRUE;
4729 }
4730 //CRM-15231
4731 $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode);
4732 $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode);
4733 // note : this modifies _fromClause and _simpleFromClause
4734 $this->includePseudoFieldsJoin($this->_sort);
4735 }
4736 }
4737 else {
4738 // add delete clause if needed even if we are skipping permission
4739 // CRM-7639
4740 if (!$this->_skipDeleteClause) {
4741 if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) {
4742 $this->_permissionWhereClause = '(contact_a.is_deleted)';
4743 }
4744 else {
4745 // CRM-6181
4746 $this->_permissionWhereClause = '(contact_a.is_deleted = 0)';
4747 }
4748 }
4749 }
4750 }
4751
4752 /**
4753 * @param $val
4754 */
4755 public function setSkipPermission($val) {
4756 $this->_skipPermission = $val;
4757 }
4758
4759 /**
4760 * @param null $context
4761 *
4762 * @return array
4763 */
4764 public function &summaryContribution($context = NULL) {
4765 list($innerselect, $from, $where, $having) = $this->query(TRUE);
4766
4767 // hack $select
4768 $select = "
4769 SELECT COUNT( conts.total_amount ) as total_count,
4770 SUM( conts.total_amount ) as total_amount,
4771 AVG( conts.total_amount ) as total_avg,
4772 conts.total_amount as amount,
4773 conts.currency as currency";
4774 if ($this->_permissionWhereClause) {
4775 $where .= " AND " . $this->_permissionWhereClause;
4776 }
4777 if ($context == 'search') {
4778 $where .= " AND contact_a.is_deleted = 0 ";
4779 }
4780
4781 // make sure contribution is completed - CRM-4989
4782 $completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 ";
4783
4784 $summary = array();
4785 $summary['total'] = array();
4786 $summary['total']['count'] = $summary['total']['amount'] = $summary['total']['avg'] = "n/a";
4787 $innerQuery = "SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
4788 civicrm_contribution.currency $from $completedWhere";
4789
4790 $query = "$select FROM (
4791 $innerQuery GROUP BY civicrm_contribution.id
4792 ) as conts
4793 GROUP BY currency";
4794
4795 $dao = CRM_Core_DAO::executeQuery($query);
4796
4797 $summary['total']['count'] = 0;
4798 $summary['total']['amount'] = $summary['total']['avg'] = array();
4799 while ($dao->fetch()) {
4800 $summary['total']['count'] += $dao->total_count;
4801 $summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency);
4802 $summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency);
4803 }
4804
4805 $orderBy = 'ORDER BY civicrm_contribution_total_amount_count DESC';
4806 $groupBy = 'GROUP BY currency, civicrm_contribution.total_amount';
4807 $modeSQL = "$select, conts.civicrm_contribution_total_amount_count as civicrm_contribution_total_amount_count FROM ($innerQuery
4808 $groupBy $orderBy) as conts
4809 GROUP BY currency";
4810
4811 $summary['total']['mode'] = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL);
4812
4813 $medianSQL = "{$from} {$completedWhere}";
4814 $summary['total']['median'] = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, 'civicrm_contribution');
4815 $summary['total']['currencyCount'] = count($summary['total']['median']);
4816
4817 if (!empty($summary['total']['amount'])) {
4818 $summary['total']['amount'] = implode(',&nbsp;', $summary['total']['amount']);
4819 $summary['total']['avg'] = implode(',&nbsp;', $summary['total']['avg']);
4820 $summary['total']['mode'] = implode(',&nbsp;', $summary['total']['mode']);
4821 $summary['total']['median'] = implode(',&nbsp;', $summary['total']['median']);
4822 }
4823 else {
4824 $summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0;
4825 }
4826
4827 // soft credit summary
4828 if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) {
4829 $softCreditWhere = "{$completedWhere} AND civicrm_contribution_soft.id IS NOT NULL";
4830 $query = "
4831 $select FROM (
4832 SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency $from $softCreditWhere
4833 GROUP BY civicrm_contribution_soft.id
4834 ) as conts
4835 GROUP BY currency";
4836 $dao = CRM_Core_DAO::executeQuery($query);
4837 $summary['soft_credit']['count'] = 0;
4838 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = array();
4839 while ($dao->fetch()) {
4840 $summary['soft_credit']['count'] += $dao->total_count;
4841 $summary['soft_credit']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency);
4842 $summary['soft_credit']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency);
4843 }
4844 if (!empty($summary['soft_credit']['amount'])) {
4845 $summary['soft_credit']['amount'] = implode(',&nbsp;', $summary['soft_credit']['amount']);
4846 $summary['soft_credit']['avg'] = implode(',&nbsp;', $summary['soft_credit']['avg']);
4847 }
4848 else {
4849 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0;
4850 }
4851 }
4852
4853 // hack $select
4854 //@todo - this could be one query using the IF in mysql - eg
4855 // SELECT sum(total_completed), sum(count_completed), sum(count_cancelled), sum(total_cancelled) FROM (
4856 // SELECT civicrm_contribution.total_amount, civicrm_contribution.currency ,
4857 // IF(civicrm_contribution.contribution_status_id = 1, 1, 0 ) as count_completed,
4858 // IF(civicrm_contribution.contribution_status_id = 1, total_amount, 0 ) as total_completed,
4859 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, 1, 0 ) as count_cancelled,
4860 // IF(civicrm_contribution.cancel_date IS NOT NULL = 1, total_amount, 0 ) as total_cancelled
4861 // FROM civicrm_contact contact_a
4862 // LEFT JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id
4863 // WHERE ( ... where clause....
4864 // AND (civicrm_contribution.cancel_date IS NOT NULL OR civicrm_contribution.contribution_status_id = 1)
4865 // ) as conts
4866
4867 $select = "
4868 SELECT COUNT( conts.total_amount ) as cancel_count,
4869 SUM( conts.total_amount ) as cancel_amount,
4870 AVG( conts.total_amount ) as cancel_avg,
4871 conts.currency as currency";
4872
4873 $where .= " AND civicrm_contribution.cancel_date IS NOT NULL ";
4874 if ($context == 'search') {
4875 $where .= " AND contact_a.is_deleted = 0 ";
4876 }
4877
4878 $query = "$select FROM (
4879 SELECT civicrm_contribution.total_amount, civicrm_contribution.currency $from $where
4880 GROUP BY civicrm_contribution.id
4881 ) as conts
4882 GROUP BY currency";
4883
4884 $dao = CRM_Core_DAO::executeQuery($query);
4885
4886 if ($dao->N <= 1) {
4887 if ($dao->fetch()) {
4888 $summary['cancel']['count'] = $dao->cancel_count;
4889 $summary['cancel']['amount'] = $dao->cancel_amount;
4890 $summary['cancel']['avg'] = $dao->cancel_avg;
4891 }
4892 }
4893 else {
4894 $summary['cancel']['count'] = 0;
4895 $summary['cancel']['amount'] = $summary['cancel']['avg'] = array();
4896 while ($dao->fetch()) {
4897 $summary['cancel']['count'] += $dao->cancel_count;
4898 $summary['cancel']['amount'][] = CRM_Utils_Money::format($dao->cancel_amount, $dao->currency);
4899 $summary['cancel']['avg'][] = CRM_Utils_Money::format($dao->cancel_avg, $dao->currency);
4900 }
4901 $summary['cancel']['amount'] = implode(',&nbsp;', $summary['cancel']['amount']);
4902 $summary['cancel']['avg'] = implode(',&nbsp;', $summary['cancel']['avg']);
4903 }
4904
4905 return $summary;
4906 }
4907
4908 /**
4909 * Getter for the qill object.
4910 *
4911 * @return string
4912 */
4913 public function qill() {
4914 return $this->_qill;
4915 }
4916
4917 /**
4918 * Default set of return default hier return properties.
4919 *
4920 * @return array
4921 */
4922 public static function &defaultHierReturnProperties() {
4923 if (!isset(self::$_defaultHierReturnProperties)) {
4924 self::$_defaultHierReturnProperties = array(
4925 'home_URL' => 1,
4926 'image_URL' => 1,
4927 'legal_identifier' => 1,
4928 'external_identifier' => 1,
4929 'contact_type' => 1,
4930 'contact_sub_type' => 1,
4931 'sort_name' => 1,
4932 'display_name' => 1,
4933 'nick_name' => 1,
4934 'first_name' => 1,
4935 'middle_name' => 1,
4936 'last_name' => 1,
4937 'prefix_id' => 1,
4938 'suffix_id' => 1,
4939 'formal_title' => 1,
4940 'communication_style_id' => 1,
4941 'email_greeting' => 1,
4942 'postal_greeting' => 1,
4943 'addressee' => 1,
4944 'birth_date' => 1,
4945 'gender_id' => 1,
4946 'preferred_communication_method' => 1,
4947 'do_not_phone' => 1,
4948 'do_not_email' => 1,
4949 'do_not_mail' => 1,
4950 'do_not_sms' => 1,
4951 'do_not_trade' => 1,
4952 'location' => array(
4953 '1' => array(
4954 'location_type' => 1,
4955 'street_address' => 1,
4956 'city' => 1,
4957 'state_province' => 1,
4958 'postal_code' => 1,
4959 'postal_code_suffix' => 1,
4960 'country' => 1,
4961 'phone-Phone' => 1,
4962 'phone-Mobile' => 1,
4963 'phone-Fax' => 1,
4964 'phone-1' => 1,
4965 'phone-2' => 1,
4966 'phone-3' => 1,
4967 'im-1' => 1,
4968 'im-2' => 1,
4969 'im-3' => 1,
4970 'email-1' => 1,
4971 'email-2' => 1,
4972 'email-3' => 1,
4973 ),
4974 '2' => array(
4975 'location_type' => 1,
4976 'street_address' => 1,
4977 'city' => 1,
4978 'state_province' => 1,
4979 'postal_code' => 1,
4980 'postal_code_suffix' => 1,
4981 'country' => 1,
4982 'phone-Phone' => 1,
4983 'phone-Mobile' => 1,
4984 'phone-1' => 1,
4985 'phone-2' => 1,
4986 'phone-3' => 1,
4987 'im-1' => 1,
4988 'im-2' => 1,
4989 'im-3' => 1,
4990 'email-1' => 1,
4991 'email-2' => 1,
4992 'email-3' => 1,
4993 ),
4994 ),
4995 );
4996 }
4997 return self::$_defaultHierReturnProperties;
4998 }
4999
5000 /**
5001 * Build query for a date field.
5002 *
5003 * @param array $values
5004 * @param string $tableName
5005 * @param string $fieldName
5006 * @param string $dbFieldName
5007 * @param string $fieldTitle
5008 * @param bool $appendTimeStamp
5009 */
5010 public function dateQueryBuilder(
5011 &$values, $tableName, $fieldName,
5012 $dbFieldName, $fieldTitle,
5013 $appendTimeStamp = TRUE
5014 ) {
5015 list($name, $op, $value, $grouping, $wildcard) = $values;
5016
5017 if ($name == "{$fieldName}_low" ||
5018 $name == "{$fieldName}_high"
5019 ) {
5020 if (isset($this->_rangeCache[$fieldName]) || !$value) {
5021 return;
5022 }
5023 $this->_rangeCache[$fieldName] = 1;
5024
5025 $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL;
5026
5027 if ($name == $fieldName . '_low') {
5028 $firstOP = '>=';
5029 $firstPhrase = ts('greater than or equal to');
5030 $firstDate = CRM_Utils_Date::processDate($value);
5031
5032 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5033 if (!empty($secondValues) && $secondValues[2]) {
5034 $secondOP = '<=';
5035 $secondPhrase = ts('less than or equal to');
5036 $secondValue = $secondValues[2];
5037
5038 if ($appendTimeStamp && strlen($secondValue) == 10) {
5039 $secondValue .= ' 23:59:59';
5040 }
5041 $secondDate = CRM_Utils_Date::processDate($secondValue);
5042 }
5043 }
5044 elseif ($name == $fieldName . '_high') {
5045 $firstOP = '<=';
5046 $firstPhrase = ts('less than or equal to');
5047
5048 if ($appendTimeStamp && strlen($value) == 10) {
5049 $value .= ' 23:59:59';
5050 }
5051 $firstDate = CRM_Utils_Date::processDate($value);
5052
5053 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5054 if (!empty($secondValues) && $secondValues[2]) {
5055 $secondOP = '>=';
5056 $secondPhrase = ts('greater than or equal to');
5057 $secondValue = $secondValues[2];
5058 $secondDate = CRM_Utils_Date::processDate($secondValue);
5059 }
5060 }
5061
5062 if (!$appendTimeStamp) {
5063 $firstDate = substr($firstDate, 0, 8);
5064 }
5065 $firstDateFormat = CRM_Utils_Date::customFormat($firstDate);
5066
5067 if ($secondDate) {
5068 if (!$appendTimeStamp) {
5069 $secondDate = substr($secondDate, 0, 8);
5070 }
5071 $secondDateFormat = CRM_Utils_Date::customFormat($secondDate);
5072 }
5073
5074 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5075 if ($secondDate) {
5076 $this->_where[$grouping][] = "
5077 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5078 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5079 ";
5080 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\"";
5081 }
5082 else {
5083 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5084 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
5085 }
5086 }
5087
5088 if ($name == $fieldName) {
5089 //In Get API, for operators other then '=' the $value is in array(op => value) format
5090 if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
5091 $op = key($value);
5092 $value = $value[$op];
5093 }
5094
5095 $date = $format = NULL;
5096 if (strstr($op, 'IN')) {
5097 $format = array();
5098 foreach ($value as &$date) {
5099 $date = CRM_Utils_Date::processDate($date);
5100 if (!$appendTimeStamp) {
5101 $date = substr($date, 0, 8);
5102 }
5103 $format[] = CRM_Utils_Date::customFormat($date);
5104 }
5105 $date = "('" . implode("','", $value) . "')";
5106 $format = implode(', ', $format);
5107 }
5108 elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) {
5109 $date = CRM_Utils_Date::processDate($value);
5110 if (!$appendTimeStamp) {
5111 $date = substr($date, 0, 8);
5112 }
5113 $format = CRM_Utils_Date::customFormat($date);
5114 $date = "'$date'";
5115 }
5116
5117 if ($date) {
5118 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op $date";
5119 }
5120 else {
5121 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op";
5122 }
5123
5124 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5125
5126 $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
5127 $this->_qill[$grouping][] = "$fieldTitle $op $format";
5128 }
5129 }
5130
5131 /**
5132 * @param $values
5133 * @param string $tableName
5134 * @param string $fieldName
5135 * @param string $dbFieldName
5136 * @param $fieldTitle
5137 * @param null $options
5138 */
5139 public function numberRangeBuilder(
5140 &$values,
5141 $tableName, $fieldName,
5142 $dbFieldName, $fieldTitle,
5143 $options = NULL
5144 ) {
5145 list($name, $op, $value, $grouping, $wildcard) = $values;
5146
5147 if ($name == "{$fieldName}_low" ||
5148 $name == "{$fieldName}_high"
5149 ) {
5150 if (isset($this->_rangeCache[$fieldName])) {
5151 return;
5152 }
5153 $this->_rangeCache[$fieldName] = 1;
5154
5155 $secondOP = $secondPhrase = $secondValue = NULL;
5156
5157 if ($name == "{$fieldName}_low") {
5158 $firstOP = '>=';
5159 $firstPhrase = ts('greater than');
5160
5161 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5162 if (!empty($secondValues)) {
5163 $secondOP = '<=';
5164 $secondPhrase = ts('less than');
5165 $secondValue = $secondValues[2];
5166 }
5167 }
5168 else {
5169 $firstOP = '<=';
5170 $firstPhrase = ts('less than');
5171
5172 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5173 if (!empty($secondValues)) {
5174 $secondOP = '>=';
5175 $secondPhrase = ts('greater than');
5176 $secondValue = $secondValues[2];
5177 }
5178 }
5179
5180 if ($secondOP) {
5181 $this->_where[$grouping][] = "
5182 ( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND
5183 ( {$tableName}.{$dbFieldName} $secondOP {$secondValue} )
5184 ";
5185 $displayValue = $options ? $options[$value] : $value;
5186 $secondDisplayValue = $options ? $options[$secondValue] : $secondValue;
5187
5188 $this->_qill[$grouping][]
5189 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5190 }
5191 else {
5192 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}";
5193 $displayValue = $options ? $options[$value] : $value;
5194 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5195 }
5196 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5197
5198 return;
5199 }
5200
5201 if ($name == $fieldName) {
5202 $op = '=';
5203 $phrase = '=';
5204
5205 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}";
5206
5207 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5208 $displayValue = $options ? $options[$value] : $value;
5209 $this->_qill[$grouping][] = "$fieldTitle - $phrase \"$displayValue\"";
5210 }
5211 }
5212
5213
5214 /**
5215 * @param $values
5216 * @param string $tableName
5217 * @param string $fieldName
5218 * @param string $dbFieldName
5219 * @param $fieldTitle
5220 * @param null $options
5221 */
5222 public function ageRangeQueryBuilder(
5223 &$values,
5224 $tableName, $fieldName,
5225 $dbFieldName, $fieldTitle,
5226 $options = NULL
5227 ) {
5228 list($name, $op, $value, $grouping, $wildcard) = $values;
5229
5230 $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping);
5231 $asofDate = NULL; // will be treated as current day
5232 if ($asofDateValues) {
5233 $asofDate = CRM_Utils_Date::processDate($asofDateValues[2]);
5234 $asofDateFormat = CRM_Utils_Date::customFormat(substr($asofDate, 0, 8));
5235 $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat;
5236 }
5237
5238 if ($name == "{$fieldName}_low" ||
5239 $name == "{$fieldName}_high"
5240 ) {
5241 if (isset($this->_rangeCache[$fieldName])) {
5242 return;
5243 }
5244 $this->_rangeCache[$fieldName] = 1;
5245
5246 $secondOP = $secondPhrase = $secondValue = NULL;
5247
5248 if ($name == "{$fieldName}_low") {
5249 $firstPhrase = ts('greater than or equal to');
5250 // NB: age > X means date of birth < Y
5251 $firstOP = '<=';
5252 $firstDate = self::calcDateFromAge($asofDate, $value, 'min');
5253
5254 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5255 if (!empty($secondValues)) {
5256 $secondOP = '>=';
5257 $secondPhrase = ts('less than or equal to');
5258 $secondValue = $secondValues[2];
5259 $secondDate = self::calcDateFromAge($asofDate, $secondValue, 'max');
5260 }
5261 }
5262 else {
5263 $firstOP = '>=';
5264 $firstPhrase = ts('less than or equal to');
5265 $firstDate = self::calcDateFromAge($asofDate, $value, 'max');
5266
5267 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5268 if (!empty($secondValues)) {
5269 $secondOP = '<=';
5270 $secondPhrase = ts('greater than or equal to');
5271 $secondValue = $secondValues[2];
5272 $secondDate = self::calcDateFromAge($asofDate, $secondValue, 'min');
5273 }
5274 }
5275
5276 if ($secondOP) {
5277 $this->_where[$grouping][] = "
5278 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5279 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5280 ";
5281 $displayValue = $options ? $options[$value] : $value;
5282 $secondDisplayValue = $options ? $options[$secondValue] : $secondValue;
5283
5284 $this->_qill[$grouping][]
5285 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5286 }
5287 else {
5288 $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5289 $displayValue = $options ? $options[$value] : $value;
5290 $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5291 }
5292 $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
5293 return;
5294 }
5295 }
5296
5297 public static function calcDateFromAge($asofDate, $age, $type) {
5298 $date = new DateTime($asofDate);
5299 if ($type == "min") {
5300 // minimum age is $age: dob <= date - age "235959"
5301 $date->sub(new DateInterval("P" . $age . "Y"));
5302 return $date->format('Ymd') . "235959";
5303 }
5304 else {
5305 // max age is $age: dob >= date - (age + 1y) + 1d "000000"
5306 $date->sub(new DateInterval("P" . ($age + 1) . "Y"))->add(new DateInterval("P1D"));
5307 return $date->format('Ymd') . "000000";
5308 }
5309 }
5310
5311 /**
5312 * Given the field name, operator, value & its data type
5313 * builds the where Clause for the query
5314 * used for handling 'IS NULL'/'IS NOT NULL' operators
5315 *
5316 * @param string $field
5317 * Fieldname.
5318 * @param string $op
5319 * Operator.
5320 * @param string $value
5321 * Value.
5322 * @param string $dataType
5323 * Data type of the field.
5324 *
5325 * @return string
5326 * Where clause for the query.
5327 */
5328 public static function buildClause($field, $op, $value = NULL, $dataType = NULL) {
5329 $op = trim($op);
5330 $clause = "$field $op";
5331
5332 switch ($op) {
5333 case 'IS NULL':
5334 case 'IS NOT NULL':
5335 return $clause;
5336
5337 case 'IS EMPTY':
5338 $clause = " (NULLIF($field, '') IS NULL) ";
5339 return $clause;
5340
5341 case 'IS NOT EMPTY':
5342 $clause = " (NULLIF($field, '') IS NOT NULL) ";
5343 return $clause;
5344
5345 case 'IN':
5346 case 'NOT IN':
5347 // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter.
5348 if (!empty($value) && is_array($value) && !array_key_exists($op, $value)) {
5349 $value = array($op => $value);
5350 }
5351
5352 default:
5353 if (empty($dataType)) {
5354 $dataType = 'String';
5355 }
5356 if (is_array($value)) {
5357 //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more
5358 // widely used and consistent across the codebase
5359 // adding this here won't accept the search functions which don't submit an array
5360 if (($queryString = CRM_Core_DAO::createSqlFilter($field, $value, $dataType)) != FALSE) {
5361
5362 return $queryString;
5363 }
5364
5365 // 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))
5366 // but we got only array(2,5) from the form.
5367 // We could get away with keeping this in 4.6 if we make it such that it throws an enotice in 4.7 so
5368 // people have to de-slopify it.
5369 if (!empty($value[0])) {
5370 $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReport;
5371 if (($queryString = CRM_Core_DAO::createSqlFilter($field, array($op => $value), $dataType)) != FALSE) {
5372 return $queryString;
5373 }
5374 }
5375 else {
5376 $op = 'IN';
5377 $dragonPlace = $iAmAnIntentionalENoticeThatWarnsOfAProblemYouShouldReportUsingOldFormat;
5378 if (($queryString = CRM_Core_DAO::createSqlFilter($field, array($op => array_keys($value)), $dataType)) != FALSE) {
5379 return $queryString;
5380 }
5381 }
5382 }
5383
5384 $value = CRM_Utils_Type::escape($value, $dataType);
5385 // if we don't have a dataType we should assume
5386 if ($dataType == 'String' || $dataType == 'Text') {
5387 $value = "'" . strtolower($value) . "'";
5388 }
5389 return "$clause $value";
5390 }
5391 }
5392
5393 /**
5394 * @param bool $reset
5395 *
5396 * @return array
5397 */
5398 public function openedSearchPanes($reset = FALSE) {
5399 if (!$reset || empty($this->_whereTables)) {
5400 return self::$_openedPanes;
5401 }
5402
5403 // pane name to table mapper
5404 $panesMapper = array(
5405 ts('Contributions') => 'civicrm_contribution',
5406 ts('Memberships') => 'civicrm_membership',
5407 ts('Events') => 'civicrm_participant',
5408 ts('Relationships') => 'civicrm_relationship',
5409 ts('Activities') => 'civicrm_activity',
5410 ts('Pledges') => 'civicrm_pledge',
5411 ts('Cases') => 'civicrm_case',
5412 ts('Grants') => 'civicrm_grant',
5413 ts('Address Fields') => 'civicrm_address',
5414 ts('Notes') => 'civicrm_note',
5415 ts('Change Log') => 'civicrm_log',
5416 ts('Mailings') => 'civicrm_mailing_event_queue',
5417 );
5418 CRM_Contact_BAO_Query_Hook::singleton()->getPanesMapper($panesMapper);
5419
5420 foreach (array_keys($this->_whereTables) as $table) {
5421 if ($panName = array_search($table, $panesMapper)) {
5422 self::$_openedPanes[$panName] = TRUE;
5423 }
5424 }
5425
5426 return self::$_openedPanes;
5427 }
5428
5429 /**
5430 * @param $operator
5431 */
5432 public function setOperator($operator) {
5433 $validOperators = array('AND', 'OR');
5434 if (!in_array($operator, $validOperators)) {
5435 $operator = 'AND';
5436 }
5437 $this->_operator = $operator;
5438 }
5439
5440 /**
5441 * @return string
5442 */
5443 public function getOperator() {
5444 return $this->_operator;
5445 }
5446
5447 /**
5448 * @param $from
5449 * @param $where
5450 * @param $having
5451 */
5452 public function filterRelatedContacts(&$from, &$where, &$having) {
5453 static $_rTypeProcessed = NULL;
5454 static $_rTypeFrom = NULL;
5455 static $_rTypeWhere = NULL;
5456
5457 if (!$_rTypeProcessed) {
5458 $_rTypeProcessed = TRUE;
5459
5460 // create temp table with contact ids
5461 $tableName = CRM_Core_DAO::createTempTableName('civicrm_transform', TRUE);
5462 $sql = "CREATE TEMPORARY TABLE $tableName ( contact_id int primary key) ENGINE=HEAP";
5463 CRM_Core_DAO::executeQuery($sql);
5464
5465 $sql = "
5466 REPLACE INTO $tableName ( contact_id )
5467 SELECT contact_a.id
5468 $from
5469 $where
5470 $having
5471 ";
5472 CRM_Core_DAO::executeQuery($sql);
5473
5474 $qillMessage = ts('Contacts with a Relationship Type of: ');
5475 $rTypes = CRM_Core_PseudoConstant::relationshipType();
5476
5477 if (is_numeric($this->_displayRelationshipType)) {
5478 $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType]['label_a_b'];
5479 $_rTypeFrom = "
5480 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )
5481 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )
5482 ";
5483 $_rTypeWhere = "
5484 WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}
5485 AND displayRelType.is_active = 1
5486 ";
5487 }
5488 else {
5489 list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType);
5490 if ($dirOne == 'a') {
5491 $relationshipTypeLabel = $rTypes[$relType]['label_a_b'];
5492 $_rTypeFrom .= "
5493 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )
5494 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )
5495 ";
5496 }
5497 else {
5498 $relationshipTypeLabel = $rTypes[$relType]['label_b_a'];
5499 $_rTypeFrom .= "
5500 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )
5501 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )
5502 ";
5503 }
5504 $_rTypeWhere = "
5505 WHERE displayRelType.relationship_type_id = $relType
5506 AND displayRelType.is_active = 1
5507 ";
5508 }
5509 $this->_qill[0][] = $qillMessage . "'" . $relationshipTypeLabel . "'";
5510 }
5511
5512 if (!empty($this->_permissionWhereClause)) {
5513 $_rTypeWhere .= "AND $this->_permissionWhereClause";
5514 }
5515
5516 if (strpos($from, $_rTypeFrom) === FALSE) {
5517 // lets replace all the INNER JOIN's in the $from so we dont exclude other data
5518 // this happens when we have an event_type in the quert (CRM-7969)
5519 $from = str_replace("INNER JOIN", "LEFT JOIN", $from);
5520 $from .= $_rTypeFrom;
5521 $where = $_rTypeWhere;
5522 }
5523
5524 $having = NULL;
5525 }
5526
5527 /**
5528 * @param $op
5529 *
5530 * @return bool
5531 */
5532 public static function caseImportant($op) {
5533 return
5534 in_array($op, array('LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ? FALSE : TRUE;
5535 }
5536
5537 /**
5538 * @param $returnProperties
5539 * @param $prefix
5540 *
5541 * @return bool
5542 */
5543 public static function componentPresent(&$returnProperties, $prefix) {
5544 foreach ($returnProperties as $name => $dontCare) {
5545 if (substr($name, 0, strlen($prefix)) == $prefix) {
5546 return TRUE;
5547 }
5548 }
5549 return FALSE;
5550 }
5551
5552 /**
5553 * Builds the necessary structures for all fields that are similar to option value look-ups.
5554 *
5555 * @param string $name
5556 * the name of the field.
5557 * @param string $op
5558 * the sql operator, this function should handle ALL SQL operators.
5559 * @param string $value
5560 * depends on the operator and who's calling the query builder.
5561 * @param int $grouping
5562 * the index where to place the where clause.
5563 * @param $selectValues
5564 * The key value pairs for this element. This allows us to use this function for things besides option-value pairs.
5565 * @param array $field
5566 * an array that contains various properties of the field identified by $name.
5567 * @param string $label
5568 * The label for this field element.
5569 * @param string $dataType
5570 * The data type for this element.
5571 * @param bool $useIDsOnly
5572 */
5573 public function optionValueQuery(
5574 $name,
5575 $op,
5576 $value,
5577 $grouping,
5578 $daoName = NULL,
5579 $field,
5580 $label,
5581 $dataType = 'String',
5582 $useIDsOnly = FALSE
5583 ) {
5584
5585 $pseudoFields = array(
5586 'email_greeting',
5587 'postal_greeting',
5588 'addressee',
5589 'gender_id',
5590 'prefix_id',
5591 'suffix_id',
5592 'communication_style_id',
5593 );
5594
5595 if ($useIDsOnly) {
5596 list($tableName, $fieldName) = explode('.', $field['where'], 2);
5597 if ($tableName == 'civicrm_contact') {
5598 $wc = "contact_a.$fieldName";
5599 }
5600 else {
5601 $wc = "$tableName.id";
5602 }
5603 }
5604 else {
5605 $wc = self::caseImportant($op) ? "LOWER({$field['where']})" : "{$field['where']}";
5606 }
5607 if (in_array($name, $pseudoFields)) {
5608 if (!in_array($name, array('gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'))) {
5609 $wc = "contact_a.{$name}_id";
5610 }
5611 $dataType = 'Positive';
5612 $value = (!$value) ? 0 : $value;
5613 }
5614 if ($name == "world_region") {
5615 $field['name'] = $name;
5616 }
5617
5618 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($daoName, $field['name'], $value, $op);
5619 $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal));
5620 $this->_where[$grouping][] = self::buildClause($wc, $op, $value, $dataType);
5621 }
5622
5623 /**
5624 * Check and explode a user defined numeric string into an array
5625 * this was the protocol used by search builder in the old old days before we had
5626 * super nice js widgets to do the hard work
5627 *
5628 * @param string $string
5629 * @param string $dataType
5630 * The dataType we should check for the values, default integer.
5631 *
5632 * @return bool|array
5633 * false if string does not match the pattern
5634 * array of numeric values if string does match the pattern
5635 */
5636 public static function parseSearchBuilderString($string, $dataType = 'Integer') {
5637 $string = trim($string);
5638 if (substr($string, 0, 1) != '(' || substr($string, -1, 1) != ')') {
5639 Return FALSE;
5640 }
5641
5642 $string = substr($string, 1, -1);
5643 $values = explode(',', $string);
5644 if (empty($values)) {
5645 return FALSE;
5646 }
5647
5648 $returnValues = array();
5649 foreach ($values as $v) {
5650 if ($dataType == 'Integer' && !is_numeric($v)) {
5651 return FALSE;
5652 }
5653 elseif ($dataType == 'String' && !is_string($v)) {
5654 return FALSE;
5655 }
5656 $returnValues[] = trim($v);
5657 }
5658
5659 if (empty($returnValues)) {
5660 return FALSE;
5661 }
5662
5663 return $returnValues;
5664 }
5665
5666 /**
5667 * Convert the pseudo constants id's to their names
5668 *
5669 * @param CRM_Core_DAO $dao
5670 * @param bool $return
5671 * @param bool $usedForAPI
5672 *
5673 * @return array|NULL
5674 */
5675 public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) {
5676 if (empty($this->_pseudoConstantsSelect)) {
5677 return NULL;
5678 }
5679 $values = array();
5680 foreach ($this->_pseudoConstantsSelect as $key => $value) {
5681 if (!empty($this->_pseudoConstantsSelect[$key]['sorting'])) {
5682 continue;
5683 }
5684
5685 if (is_object($dao) && property_exists($dao, $value['idCol'])) {
5686 $val = $dao->$value['idCol'];
5687
5688 if (CRM_Utils_System::isNull($val)) {
5689 $dao->$key = NULL;
5690 }
5691 elseif ($baoName = CRM_Utils_Array::value('bao', $value, NULL)) {
5692 //preserve id value
5693 $idColumn = "{$key}_id";
5694 $dao->$idColumn = $val;
5695
5696 if ($key == 'state_province_name') {
5697 $dao->$value['pseudoField'] = $dao->$key = CRM_Core_PseudoConstant::stateProvinceAbbreviation($val);
5698 }
5699 else {
5700 $dao->$value['pseudoField'] = $dao->$key = CRM_Core_PseudoConstant::getLabel($baoName, $value['pseudoField'], $val);
5701 }
5702 }
5703 elseif ($value['pseudoField'] == 'state_province_abbreviation') {
5704 $dao->$key = CRM_Core_PseudoConstant::stateProvinceAbbreviation($val);
5705 }
5706 // FIX ME: we should potentially move this to component Query and write a wrapper function that
5707 // handles pseudoconstant fixes for all component
5708 elseif (in_array($value['pseudoField'], array('participant_role_id', 'participant_role'))) {
5709 $viewValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $val);
5710
5711 if ($value['pseudoField'] == 'participant_role') {
5712 $pseudoOptions = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'role_id');
5713 foreach ($viewValues as $k => $v) {
5714 $viewValues[$k] = $pseudoOptions[$v];
5715 }
5716 }
5717 $dao->$key = ($usedForAPI && count($viewValues) > 1) ? $viewValues : implode(', ', $viewValues);
5718 }
5719 else {
5720 $labels = CRM_Core_OptionGroup::values($value['pseudoField']);
5721 $dao->$key = $labels[$val];
5722 }
5723
5724 // return converted values in array format
5725 if ($return) {
5726 if (strpos($key, '-') !== FALSE) {
5727 $keyVal = explode('-', $key);
5728 $current = &$values;
5729 $lastElement = array_pop($keyVal);
5730 foreach ($keyVal as $v) {
5731 if (!array_key_exists($v, $current)) {
5732 $current[$v] = array();
5733 }
5734 $current = &$current[$v];
5735 }
5736 $current[$lastElement] = $dao->$key;
5737 }
5738 else {
5739 $values[$key] = $dao->$key;
5740 }
5741 }
5742 }
5743 }
5744 return $values;
5745 }
5746
5747 /**
5748 * Include pseudo fields LEFT JOIN.
5749 * @param string|array $sort can be a object or string
5750 *
5751 * @return array|NULL
5752 */
5753 public function includePseudoFieldsJoin($sort) {
5754 if (!$sort || empty($this->_pseudoConstantsSelect)) {
5755 return NULL;
5756 }
5757 $sort = is_string($sort) ? $sort : $sort->orderBy();
5758 $present = array();
5759
5760 foreach ($this->_pseudoConstantsSelect as $name => $value) {
5761 if (!empty($value['table'])) {
5762 $regex = "/({$value['table']}\.|{$name})/";
5763 if (preg_match($regex, $sort)) {
5764 $this->_elemnt[$value['element']] = 1;
5765 $this->_select[$value['element']] = $value['select'];
5766 $this->_pseudoConstantsSelect[$name]['sorting'] = 1;
5767 $present[$value['table']] = $value['join'];
5768 }
5769 }
5770 }
5771 $presentSimpleFrom = $present;
5772
5773 if (array_key_exists('civicrm_worldregion', $this->_whereTables) &&
5774 array_key_exists('civicrm_country', $presentSimpleFrom)
5775 ) {
5776 unset($presentSimpleFrom['civicrm_country']);
5777 }
5778 if (array_key_exists('civicrm_worldregion', $this->_tables) &&
5779 array_key_exists('civicrm_country', $present)
5780 ) {
5781 unset($present['civicrm_country']);
5782 }
5783
5784 $presentClause = $presentSimpleFromClause = NULL;
5785 if (!empty($present)) {
5786 $presentClause = implode(' ', $present);
5787 }
5788 if (!empty($presentSimpleFrom)) {
5789 $presentSimpleFromClause = implode(' ', $presentSimpleFrom);
5790 }
5791
5792 $this->_fromClause = $this->_fromClause . $presentClause;
5793 $this->_simpleFromClause = $this->_simpleFromClause . $presentSimpleFromClause;
5794
5795 return array($presentClause, $presentSimpleFromClause);
5796 }
5797
5798 /**
5799 * Build qill for field.
5800 *
5801 * Qill refers to the query detail visible on the UI.
5802 *
5803 * @param string $daoName
5804 * @param string $fieldName
5805 * @param mixed $fieldValue
5806 * @param string $op
5807 * @param array $pseudoExtraParam
5808 * @param int $type
5809 * Type of the field per CRM_Utils_Type
5810 *
5811 * @return array
5812 */
5813 public static function buildQillForFieldValue(
5814 $daoName,
5815 $fieldName,
5816 $fieldValue,
5817 $op,
5818 $pseudoExtraParam = array(),
5819 $type = CRM_Utils_Type::T_STRING
5820 ) {
5821 $qillOperators = CRM_Core_SelectValues::getSearchBuilderOperators();
5822
5823 // if Operator chosen is NULL/EMPTY then
5824 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
5825 return array(CRM_Utils_Array::value($op, $qillOperators, $op), '');
5826 }
5827
5828 if ($fieldName == 'activity_type_id') {
5829 $pseudoOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
5830 }
5831 elseif ($fieldName == 'country_id') {
5832 $pseduoOptions = CRM_Core_PseudoConstant::country();
5833 }
5834 elseif ($fieldName == 'county_id') {
5835 $pseduoOptions = CRM_Core_PseudoConstant::county();
5836 }
5837 elseif ($fieldName == 'world_region') {
5838 $pseduoOptions = CRM_Core_PseudoConstant::worldRegion();
5839 }
5840 elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') {
5841 $pseudoOptions = CRM_Event_BAO_Event::getEvents(0, $fieldValue, TRUE, TRUE, TRUE);
5842 }
5843 elseif ($fieldName == 'contribution_product_id') {
5844 $pseudoOptions = CRM_Contribute_PseudoConstant::products();
5845 }
5846 elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
5847 $pseudoOptions = CRM_Core_PseudoConstant::group();
5848 }
5849 elseif ($fieldName == 'country_id') {
5850 $pseudoOptions = CRM_Core_PseudoConstant::country();
5851 }
5852 elseif ($daoName) {
5853 $pseudoOptions = CRM_Core_PseudoConstant::get($daoName, $fieldName, $pseudoExtraParam);
5854 }
5855
5856 //API usually have fieldValue format as array(operator => array(values)),
5857 //so we need to separate operator out of fieldValue param
5858 if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
5859 $op = key($fieldValue);
5860 $fieldValue = $fieldValue[$op];
5861 }
5862
5863 if (is_array($fieldValue)) {
5864 $qillString = array();
5865 if (!empty($pseudoOptions)) {
5866 foreach ((array) $fieldValue as $val) {
5867 $qillString[] = CRM_Utils_Array::value($val, $pseudoOptions, $val);
5868 }
5869 $fieldValue = implode(', ', $qillString);
5870 }
5871 else {
5872 if ($type == CRM_Utils_Type::T_DATE) {
5873 foreach ($fieldValue as $index => $value) {
5874 $fieldValue[$index] = CRM_Utils_Date::customFormat($value);
5875 }
5876 }
5877 $separator = ', ';
5878 // @todo - this is a bit specific (one operator).
5879 // However it is covered by a unit test so can be altered later with
5880 // some confidence.
5881 if ($op == 'BETWEEN') {
5882 $separator = ' AND ';
5883 }
5884 $fieldValue = implode($separator, $fieldValue);
5885 }
5886 }
5887 elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) {
5888 $fieldValue = $pseudoOptions[$fieldValue];
5889 }
5890 elseif ($type === CRM_Utils_Type::T_DATE) {
5891 $fieldValue = CRM_Utils_Date::customFormat($fieldValue);
5892 }
5893
5894 return array(CRM_Utils_Array::value($op, $qillOperators, $op), $fieldValue);
5895 }
5896
5897 }