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