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