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