3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This is the heart of the search query building mechanism.
37 class CRM_Contact_BAO_Query
{
40 * The various search modes.
42 * As of February 2017, entries not present for 4, 32, 64, 1024.
44 * MODE_ALL seems to be out of sync with the available constants;
45 * if this is intentionally excluding MODE_MAILING then that may
48 * Likewise if there's reason for the missing modes (4, 32, 64 etc).
53 NO_RETURN_PROPERTIES
= 'CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES',
59 MODE_CONTACTSRELATED
= 32,
62 MODE_PLEDGEBANK
= 256,
72 * Constants for search operators
75 SEARCH_OPERATOR_AND
= 'AND',
76 SEARCH_OPERATOR_OR
= 'OR';
79 * The default set of return properties.
83 static $_defaultReturnProperties = NULL;
86 * The default set of hier return properties.
90 static $_defaultHierReturnProperties;
93 * The set of input params.
101 public $_paramLookup;
106 * The set of output params
110 public $_returnProperties;
120 * The name of the elements that are in the select clause
121 * used to extract the values.
128 * The tables involved in the query.
135 * The table involved in the where clause.
139 public $_whereTables;
142 * Array of WHERE clause components.
149 * The WHERE clause as a string.
153 public $_whereClause;
156 * Additional WHERE clause for permissions.
160 public $_permissionWhereClause;
170 * Additional permission from clause
174 public $_permissionFromClause;
177 * The from clause for the simple select and alphabetical
182 public $_simpleFromClause;
192 * The english language version of the query
199 * All the fields that could potentially be involved in
207 * The cache to translate the option values into labels.
214 * Are we in search mode.
218 public $_search = TRUE;
221 * Should we skip permission checking.
225 public $_skipPermission = FALSE;
228 * Should we skip adding of delete clause.
232 public $_skipDeleteClause = FALSE;
235 * Are we in strict mode (use equality over LIKE)
239 public $_strict = FALSE;
242 * What operator to use to group the clauses.
246 public $_operator = 'AND';
251 * Should we only search on primary location.
255 public $_primaryLocation = TRUE;
258 * Are contact ids part of the query.
262 public $_includeContactIds = FALSE;
265 * Should we use the smart group cache.
269 public $_smartGroupCache = TRUE;
272 * Should we display contacts with a specific relationship type.
276 public $_displayRelationshipType = NULL;
279 * Reference to the query object for custom values.
283 public $_customQuery;
286 * Should we enable the distinct clause, used if we are including
287 * more than one group
291 public $_useDistinct = FALSE;
294 * Should we just display one contact record
296 public $_useGroupBy = FALSE;
299 * The relationship type direction
310 static $_activityRole;
313 * Consider the component activity type
314 * during activity search.
318 static $_considerCompActivities;
321 * Consider with contact activities only,
322 * during activity search.
326 static $_withContactActivitiesOnly;
329 * Use distinct component clause for component searches
333 public $_distinctComponentClause;
335 public $_rowCountClause;
338 * Use groupBy component clause for component searches
342 public $_groupByComponentClause;
345 * Track open panes, useful in advance search
349 public static $_openedPanes = array();
352 * For search builder - which custom fields are location-dependent
355 public $_locationSpecificCustomFields = array();
358 * The tables which have a dependency on location and/or address
362 static $_dependencies = array(
363 'civicrm_state_province' => 1,
364 'civicrm_country' => 1,
365 'civicrm_county' => 1,
366 'civicrm_address' => 1,
367 'civicrm_location_type' => 1,
371 * List of location specific fields.
373 static $_locationSpecificFields = array(
378 'supplemental_address_1',
379 'supplemental_address_2',
380 'supplemental_address_3',
383 'postal_code_suffix',
397 * Remember if we handle either end of a number or date range
398 * so we can skip the other
400 protected $_rangeCache = array();
402 * Set to true when $this->relationship is run to avoid adding twice
405 protected $_relationshipValuesAdded = FALSE;
408 * Set to the name of the temp table if one has been created
411 static $_relationshipTempTable = NULL;
413 public $_pseudoConstantsSelect = array();
415 public $_groupUniqueKey = NULL;
416 public $_groupKeys = [];
419 * Class constructor which also does all the work.
421 * @param array $params
422 * @param array $returnProperties
423 * @param array $fields
424 * @param bool $includeContactIds
425 * @param bool $strict
426 * @param bool|int $mode - mode the search is operating on
428 * @param bool $skipPermission
429 * @param bool $searchDescendentGroups
430 * @param bool $smartGroupCache
431 * @param null $displayRelationshipType
432 * @param string $operator
433 * @param string $apiEntity
434 * @param bool|NULL $primaryLocationOnly
436 public function __construct(
437 $params = NULL, $returnProperties = NULL, $fields = NULL,
438 $includeContactIds = FALSE, $strict = FALSE, $mode = 1,
439 $skipPermission = FALSE, $searchDescendentGroups = TRUE,
440 $smartGroupCache = TRUE, $displayRelationshipType = NULL,
443 $primaryLocationOnly = NULL
445 if ($primaryLocationOnly === NULL) {
446 $primaryLocationOnly = Civi
::settings()->get('searchPrimaryDetailsOnly');
448 $this->_primaryLocation
= $primaryLocationOnly;
449 $this->_params
= &$params;
450 if ($this->_params
== NULL) {
451 $this->_params
= array();
454 if ($returnProperties === self
::NO_RETURN_PROPERTIES
) {
455 $this->_returnProperties
= array();
457 elseif (empty($returnProperties)) {
458 $this->_returnProperties
= self
::defaultReturnProperties($mode);
461 $this->_returnProperties
= &$returnProperties;
464 $this->_includeContactIds
= $includeContactIds;
465 $this->_strict
= $strict;
466 $this->_mode
= $mode;
467 $this->_skipPermission
= $skipPermission;
468 $this->_smartGroupCache
= $smartGroupCache;
469 $this->_displayRelationshipType
= $displayRelationshipType;
470 $this->setOperator($operator);
473 $this->_fields
= &$fields;
474 $this->_search
= FALSE;
475 $this->_skipPermission
= TRUE;
478 $this->_fields
= CRM_Contact_BAO_Contact
::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission);
480 $fields = CRM_Core_Component
::getQueryFields(!$this->_skipPermission
);
481 unset($fields['note']);
482 $this->_fields
= array_merge($this->_fields
, $fields);
484 // add activity fields
485 $fields = CRM_Activity_BAO_Activity
::exportableFields();
486 $this->_fields
= array_merge($this->_fields
, $fields);
488 // add any fields provided by hook implementers
489 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
490 $this->_fields
= array_merge($this->_fields
, $extFields);
493 // basically do all the work once, and then reuse it
494 $this->initialize($apiEntity);
498 * Function which actually does all the work for the constructor.
500 * @param string $apiEntity
501 * The api entity being called.
502 * This sort-of duplicates $mode in a confusing way. Probably not by design.
504 public function initialize($apiEntity = NULL) {
505 $this->_select
= array();
506 $this->_element
= array();
507 $this->_tables
= array();
508 $this->_whereTables
= array();
509 $this->_where
= array();
510 $this->_qill
= array();
511 $this->_options
= array();
512 $this->_cfIDs
= array();
513 $this->_paramLookup
= array();
514 $this->_having
= array();
516 $this->_customQuery
= NULL;
518 // reset cached static variables - CRM-5803
519 self
::$_activityRole = NULL;
520 self
::$_considerCompActivities = NULL;
521 self
::$_withContactActivitiesOnly = NULL;
523 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
524 $this->_element
['contact_id'] = 1;
525 $this->_tables
['civicrm_contact'] = 1;
527 if (!empty($this->_params
)) {
528 $this->buildParamsLookup();
531 $this->_whereTables
= $this->_tables
;
533 $this->selectClause($apiEntity);
534 $this->_whereClause
= $this->whereClause($apiEntity);
535 if (array_key_exists('civicrm_contribution', $this->_whereTables
)) {
536 $component = 'contribution';
538 if (array_key_exists('civicrm_membership', $this->_whereTables
)) {
539 $component = 'membership';
541 if (isset($component)) {
542 // @todo should be if (isset($component && !$this->_skipPermission)
543 CRM_Financial_BAO_FinancialType
::buildPermissionedClause($this->_whereClause
, $component);
546 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
, $apiEntity);
547 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
549 $this->openedSearchPanes(TRUE);
553 * Function for same purpose as convertFormValues.
555 * Like convert form values this function exists to pre-Process parameters from the form.
557 * It is unclear why they are different functions & likely relates to advances search
558 * versus search builder.
560 * The direction we are going is having the form convert values to a standardised format &
561 * moving away from weird & wonderful where clause switches.
563 * Fix and handle contact deletion nicely.
565 * this code is primarily for search builder use case where different clauses can specify if they want deleted.
569 public function buildParamsLookup() {
570 $trashParamExists = FALSE;
571 $paramByGroup = array();
572 foreach ($this->_params
as $k => $param) {
573 if (!empty($param[0]) && $param[0] == 'contact_is_deleted') {
574 $trashParamExists = TRUE;
576 if (!empty($param[3])) {
577 $paramByGroup[$param[3]][$k] = $param;
581 if ($trashParamExists) {
582 $this->_skipDeleteClause
= TRUE;
584 //cycle through group sets and explicitly add trash param if not set
585 foreach ($paramByGroup as $setID => $set) {
587 !in_array(array('contact_is_deleted', '=', '1', $setID, '0'), $this->_params
) &&
588 !in_array(array('contact_is_deleted', '=', '0', $setID, '0'), $this->_params
)
590 $this->_params
[] = array(
591 'contact_is_deleted',
601 foreach ($this->_params
as $value) {
602 if (empty($value[0])) {
605 $cfID = CRM_Core_BAO_CustomField
::getKeyID($value[0]);
607 if (!array_key_exists($cfID, $this->_cfIDs
)) {
608 $this->_cfIDs
[$cfID] = array();
610 // Set wildcard value based on "and/or" selection
611 foreach ($this->_params
as $key => $param) {
612 if ($param[0] == $value[0] . '_operator') {
613 $value[4] = $param[2] == 'or';
617 $this->_cfIDs
[$cfID][] = $value;
620 if (!array_key_exists($value[0], $this->_paramLookup
)) {
621 $this->_paramLookup
[$value[0]] = array();
623 if ($value[0] !== 'group') {
624 // Just trying to unravel how group interacts here! This whole function is weird.
625 $this->_paramLookup
[$value[0]][] = $value;
631 * Some composite fields do not appear in the fields array hack to make them part of the query.
634 * The api entity being called.
635 * This sort-of duplicates $mode in a confusing way. Probably not by design.
637 public function addSpecialFields($apiEntity) {
638 static $special = array('contact_type', 'contact_sub_type', 'sort_name', 'display_name');
639 // if get called via Contact.get API having address_id as return parameter
640 if ($apiEntity == 'Contact') {
641 $special[] = 'address_id';
643 foreach ($special as $name) {
644 if (!empty($this->_returnProperties
[$name])) {
645 if ($name == 'address_id') {
646 $this->_tables
['civicrm_address'] = 1;
647 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
648 $this->_element
['address_id'] = 1;
651 $this->_select
[$name] = "contact_a.{$name} as $name";
652 $this->_element
[$name] = 1;
659 * Given a list of conditions in params and a list of desired
660 * return Properties generate the required select and from
661 * clauses. Note that since the where clause introduces new
662 * tables, the initial attempt also retrieves all variables used
665 * @param string $apiEntity
666 * The api entity being called.
667 * This sort-of duplicates $mode in a confusing way. Probably not by design.
669 public function selectClause($apiEntity = NULL) {
671 // @todo Tidy up this. This arises because 1) we are ignoring the $mode & adding a new
672 // param ($apiEntity) instead - presumably an oversight & 2 because
673 // contact is not implemented as a component.
674 $this->addSpecialFields($apiEntity);
676 foreach ($this->_fields
as $name => $field) {
677 // skip component fields
678 // there are done by the alter query below
679 // and need not be done on every field
680 // @todo remove these & handle using metadata - only obscure fields
681 // that are hack-added should need to be excluded from the main loop.
683 (substr($name, 0, 12) == 'participant_') ||
684 (substr($name, 0, 7) == 'pledge_') ||
685 (substr($name, 0, 5) == 'case_')
690 // redirect to activity select clause
692 (substr($name, 0, 9) == 'activity_') ||
693 ($name == 'parent_id')
695 CRM_Activity_BAO_Query
::select($this);
698 // if this is a hierarchical name, we ignore it
699 $names = explode('-', $name);
700 if (count($names) > 1 && isset($names[1]) && is_numeric($names[1])) {
704 // make an exception for special cases, to add the field in select clause
705 $makeException = FALSE;
707 //special handling for groups/tags
708 if (in_array($name, array('groups', 'tags', 'notes'))
709 && isset($this->_returnProperties
[substr($name, 0, -1)])
711 // @todo instead of setting make exception to get us into
712 // an if clause that has handling for these fields buried with in it
713 // move the handling to here.
714 $makeException = TRUE;
717 // since note has 3 different options we need special handling
718 // note / note_subject / note_body
719 if ($name == 'notes') {
720 foreach (array('note', 'note_subject', 'note_body') as $noteField) {
721 if (isset($this->_returnProperties
[$noteField])) {
722 $makeException = TRUE;
728 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
730 !empty($this->_paramLookup
[$name])
731 ||
!empty($this->_returnProperties
[$name])
732 ||
$this->pseudoConstantNameIsInReturnProperties($field, $name)
736 // add to cfIDs array if not present
737 if (!array_key_exists($cfID, $this->_cfIDs
)) {
738 $this->_cfIDs
[$cfID] = array();
741 elseif (isset($field['where'])) {
742 list($tableName, $fieldName) = explode('.', $field['where'], 2);
743 if (isset($tableName)) {
744 if (CRM_Utils_Array
::value($tableName, self
::$_dependencies)) {
745 $this->_tables
['civicrm_address'] = 1;
746 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
747 $this->_element
['address_id'] = 1;
750 if ($tableName == 'im_provider' ||
$tableName == 'email_greeting' ||
751 $tableName == 'postal_greeting' ||
$tableName == 'addressee'
753 if ($tableName == 'im_provider') {
754 CRM_Core_OptionValue
::select($this);
757 if (in_array($tableName,
758 array('email_greeting', 'postal_greeting', 'addressee'))) {
759 $this->_element
["{$name}_id"] = 1;
760 $this->_select
["{$name}_id"] = "contact_a.{$name}_id as {$name}_id";
761 $this->_pseudoConstantsSelect
[$name] = array('pseudoField' => $tableName, 'idCol' => "{$name}_id");
762 $this->_pseudoConstantsSelect
[$name]['select'] = "{$name}.{$fieldName} as $name";
763 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
765 if ($tableName == 'email_greeting') {
767 $this->_pseudoConstantsSelect
[$name]['join']
768 = " LEFT JOIN civicrm_option_group option_group_email_greeting ON (option_group_email_greeting.name = 'email_greeting')";
769 $this->_pseudoConstantsSelect
[$name]['join'] .=
770 " 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 ) ";
772 elseif ($tableName == 'postal_greeting') {
774 $this->_pseudoConstantsSelect
[$name]['join']
775 = " LEFT JOIN civicrm_option_group option_group_postal_greeting ON (option_group_postal_greeting.name = 'postal_greeting')";
776 $this->_pseudoConstantsSelect
[$name]['join'] .=
777 " 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 ) ";
779 elseif ($tableName == 'addressee') {
781 $this->_pseudoConstantsSelect
[$name]['join']
782 = " LEFT JOIN civicrm_option_group option_group_addressee ON (option_group_addressee.name = 'addressee')";
783 $this->_pseudoConstantsSelect
[$name]['join'] .=
784 " LEFT JOIN civicrm_option_value addressee ON (contact_a.addressee_id = addressee.value AND option_group_addressee.id = addressee.option_group_id ) ";
786 $this->_pseudoConstantsSelect
[$name]['table'] = $tableName;
789 $greetField = "{$name}_display";
790 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
791 $this->_element
[$greetField] = 1;
793 $greetField = "{$name}_custom";
794 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
795 $this->_element
[$greetField] = 1;
799 if (!in_array($tableName, array('civicrm_state_province', 'civicrm_country', 'civicrm_county'))) {
800 $this->_tables
[$tableName] = 1;
803 // also get the id of the tableName
804 $tName = substr($tableName, 8);
805 if (in_array($tName, array('country', 'state_province', 'county'))) {
806 if ($tName == 'state_province') {
807 $this->_pseudoConstantsSelect
['state_province_name'] = array(
808 'pseudoField' => "{$tName}",
809 'idCol' => "{$tName}_id",
810 'bao' => 'CRM_Core_BAO_Address',
811 'table' => "civicrm_{$tName}",
812 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
815 $this->_pseudoConstantsSelect
[$tName] = array(
816 'pseudoField' => 'state_province_abbreviation',
817 'idCol' => "{$tName}_id",
818 'table' => "civicrm_{$tName}",
819 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
823 $this->_pseudoConstantsSelect
[$name] = array(
824 'pseudoField' => "{$tName}_id",
825 'idCol' => "{$tName}_id",
826 'bao' => 'CRM_Core_BAO_Address',
827 'table' => "civicrm_{$tName}",
828 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
832 $this->_select
["{$tName}_id"] = "civicrm_address.{$tName}_id as {$tName}_id";
833 $this->_element
["{$tName}_id"] = 1;
835 elseif ($tName != 'contact') {
836 $this->_select
["{$tName}_id"] = "{$tableName}.id as {$tName}_id";
837 $this->_element
["{$tName}_id"] = 1;
840 //special case for phone
841 if ($name == 'phone') {
842 $this->_select
['phone_type_id'] = "civicrm_phone.phone_type_id as phone_type_id";
843 $this->_element
['phone_type_id'] = 1;
846 // if IM then select provider_id also
847 // to get "IM Service Provider" in a file to be exported, CRM-3140
849 $this->_select
['provider_id'] = "civicrm_im.provider_id as provider_id";
850 $this->_element
['provider_id'] = 1;
853 if ($tName == 'contact' && $fieldName == 'organization_name') {
854 // special case, when current employer is set for Individual contact
855 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name";
857 elseif ($tName == 'contact' && $fieldName === 'id') {
858 // Handled elsewhere, explicitly ignore. Possibly for all tables...
860 elseif (in_array($tName, array('country', 'county'))) {
861 $this->_pseudoConstantsSelect
[$name]['select'] = "{$field['where']} as `$name`";
862 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
864 elseif ($tName == 'state_province') {
865 $this->_pseudoConstantsSelect
[$tName]['select'] = "{$field['where']} as `$name`";
866 $this->_pseudoConstantsSelect
[$tName]['element'] = $name;
868 elseif (strpos($name, 'contribution_soft_credit') !== FALSE) {
869 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_params
)) {
870 $this->_select
[$name] = "{$field['where']} as `$name`";
873 elseif ($this->pseudoConstantNameIsInReturnProperties($field, $name)) {
874 $this->addPseudoconstantFieldToSelect($name);
877 $this->_select
[$name] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$name`");
879 if (!in_array($tName, array('state_province', 'country', 'county'))) {
880 $this->_element
[$name] = 1;
885 elseif ($name === 'tags') {
886 //@todo move this handling outside the big IF & ditch $makeException
887 $this->_useGroupBy
= TRUE;
888 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags";
889 $this->_element
[$name] = 1;
890 $this->_tables
['civicrm_tag'] = 1;
891 $this->_tables
['civicrm_entity_tag'] = 1;
893 elseif ($name === 'groups') {
894 //@todo move this handling outside the big IF & ditch $makeException
895 $this->_useGroupBy
= TRUE;
896 // Duplicates will be created here but better to sort them out in php land.
897 $this->_select
[$name] = "
899 GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')),
900 GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id)
903 $this->_element
[$name] = 1;
904 $this->_tables
['civicrm_group_contact'] = 1;
905 $this->_tables
['civicrm_group_contact_cache'] = 1;
906 $this->_pseudoConstantsSelect
["{$name}"] = array(
907 'pseudoField' => "groups",
911 elseif ($name === 'notes') {
912 //@todo move this handling outside the big IF & ditch $makeException
913 // if note field is subject then return subject else body of the note
914 $noteColumn = 'note';
915 if (isset($noteField) && $noteField == 'note_subject') {
916 $noteColumn = 'subject';
919 $this->_useGroupBy
= TRUE;
920 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_note.$noteColumn)) as notes";
921 $this->_element
[$name] = 1;
922 $this->_tables
['civicrm_note'] = 1;
924 elseif ($name === 'current_employer') {
925 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer";
926 $this->_element
[$name] = 1;
930 if ($cfID && !empty($field['is_search_range'])) {
931 // this is a custom field with range search enabled, so we better check for two/from values
932 if (!empty($this->_paramLookup
[$name . '_from'])) {
933 if (!array_key_exists($cfID, $this->_cfIDs
)) {
934 $this->_cfIDs
[$cfID] = array();
936 foreach ($this->_paramLookup
[$name . '_from'] as $pID => $p) {
937 // search in the cdID array for the same grouping
939 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
940 if ($c[3] == $p[3]) {
941 $this->_cfIDs
[$cfID][$cID][2]['from'] = $p[2];
946 $p[2] = array('from' => $p[2]);
947 $this->_cfIDs
[$cfID][] = $p;
951 if (!empty($this->_paramLookup
[$name . '_to'])) {
952 if (!array_key_exists($cfID, $this->_cfIDs
)) {
953 $this->_cfIDs
[$cfID] = array();
955 foreach ($this->_paramLookup
[$name . '_to'] as $pID => $p) {
956 // search in the cdID array for the same grouping
958 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
959 if ($c[4] == $p[4]) {
960 $this->_cfIDs
[$cfID][$cID][2]['to'] = $p[2];
965 $p[2] = array('to' => $p[2]);
966 $this->_cfIDs
[$cfID][] = $p;
973 // add location as hierarchical elements
974 $this->addHierarchicalElements();
976 // add multiple field like website
977 $this->addMultipleElements();
980 CRM_Core_Component
::alterQuery($this, 'select');
982 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'select');
984 if (!empty($this->_cfIDs
)) {
985 // @todo This function is the select function but instead of running 'select' it
986 // is running the whole query.
987 $this->_customQuery
= new CRM_Core_BAO_CustomQuery($this->_cfIDs
, TRUE, $this->_locationSpecificCustomFields
);
988 $this->_customQuery
->query();
989 $this->_select
= array_merge($this->_select
, $this->_customQuery
->_select
);
990 $this->_element
= array_merge($this->_element
, $this->_customQuery
->_element
);
991 $this->_tables
= array_merge($this->_tables
, $this->_customQuery
->_tables
);
992 $this->_whereTables
= array_merge($this->_whereTables
, $this->_customQuery
->_whereTables
);
993 $this->_options
= $this->_customQuery
->_options
;
998 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
1000 public function addHierarchicalElements() {
1001 if (empty($this->_returnProperties
['location'])) {
1004 if (!is_array($this->_returnProperties
['location'])) {
1008 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
1009 $processed = array();
1012 $addressCustomFields = CRM_Core_BAO_CustomField
::getFieldsForImport('Address');
1013 $addressCustomFieldIds = array();
1015 foreach ($this->_returnProperties
['location'] as $name => $elements) {
1016 $lCond = self
::getPrimaryCondition($name);
1019 $locationTypeId = array_search($name, $locationTypes);
1020 if ($locationTypeId === FALSE) {
1023 $lCond = "location_type_id = $locationTypeId";
1024 $this->_useDistinct
= TRUE;
1026 //commented for CRM-3256
1027 $this->_useGroupBy
= TRUE;
1030 $name = str_replace(' ', '_', $name);
1032 $tName = "$name-location_type";
1033 $ltName = "`$name-location_type`";
1034 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1035 $this->_select
["{$tName}"] = "`$tName`.name as `{$tName}`";
1036 $this->_element
["{$tName}_id"] = 1;
1037 $this->_element
["{$tName}"] = 1;
1039 $locationTypeName = $tName;
1040 $locationTypeJoin = array();
1042 $addAddress = FALSE;
1044 foreach ($elements as $elementFullName => $dontCare) {
1046 $elementName = $elementCmpName = $elementFullName;
1048 if (substr($elementCmpName, 0, 5) == 'phone') {
1049 $elementCmpName = 'phone';
1052 if (in_array($elementCmpName, array_keys($addressCustomFields))) {
1053 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($elementCmpName)) {
1054 $addressCustomFieldIds[$cfID][$name] = 1;
1057 //add address table only once
1058 if ((in_array($elementCmpName, self
::$_locationSpecificFields) ||
!empty($addressCustomFieldIds))
1060 && !in_array($elementCmpName, array('email', 'phone', 'im', 'openid'))
1062 $tName = "$name-address";
1063 $aName = "`$name-address`";
1064 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1065 $this->_element
["{$tName}_id"] = 1;
1066 $addressJoin = "\nLEFT JOIN civicrm_address $aName ON ($aName.contact_id = contact_a.id AND $aName.$lCond)";
1067 $this->_tables
[$tName] = $addressJoin;
1068 $locationTypeJoin[$tName] = " ( $aName.location_type_id = $ltName.id ) ";
1069 $processed[$aName] = 1;
1073 $cond = $elementType = '';
1074 if (strpos($elementName, '-') !== FALSE) {
1075 // this is either phone, email or IM
1076 list($elementName, $elementType) = explode('-', $elementName);
1078 if (($elementName != 'phone') && ($elementName != 'im')) {
1079 $cond = self
::getPrimaryCondition($elementType);
1081 // CRM-13011 : If location type is primary, do not restrict search to the phone
1082 // type id - we want the primary phone, regardless of what type it is.
1083 // Otherwise, restrict to the specified phone type for the given field.
1084 if ((!$cond) && ($elementName == 'phone')) {
1085 $cond = "phone_type_id = '$elementType'";
1087 elseif ((!$cond) && ($elementName == 'im')) {
1088 // IM service provider id, CRM-3140
1089 $cond = "provider_id = '$elementType'";
1091 $elementType = '-' . $elementType;
1094 $field = CRM_Utils_Array
::value($elementName, $this->_fields
);
1096 // hack for profile, add location id
1099 // fix for CRM-882( to handle phone types )
1100 !is_numeric($elementType)
1102 if (is_numeric($name)) {
1103 $field = CRM_Utils_Array
::value($elementName . "-Primary$elementType", $this->_fields
);
1106 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1109 elseif (is_numeric($name)) {
1110 //this for phone type to work
1111 if (in_array($elementName, array('phone', 'phone_ext'))) {
1112 $field = CRM_Utils_Array
::value($elementName . "-Primary" . $elementType, $this->_fields
);
1115 $field = CRM_Utils_Array
::value($elementName . "-Primary", $this->_fields
);
1119 //this is for phone type to work for profile edit
1120 if (in_array($elementName, array('phone', 'phone_ext'))) {
1121 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1124 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId", $this->_fields
);
1129 // Check if there is a value, if so also add to where Clause
1131 if ($this->_params
) {
1133 if (isset($locationTypeId)) {
1134 $nm .= "-$locationTypeId";
1136 if (!is_numeric($elementType)) {
1137 $nm .= "$elementType";
1140 foreach ($this->_params
as $id => $values) {
1141 if ((is_array($values) && $values[0] == $nm) ||
1142 (in_array($elementName, array('phone', 'im'))
1143 && (strpos($values[0], $nm) !== FALSE)
1153 if ($field && isset($field['where'])) {
1154 list($tableName, $fieldName) = explode('.', $field['where'], 2);
1155 $pf = substr($tableName, 8);
1156 $tName = $name . '-' . $pf . $elementType;
1157 if (isset($tableName)) {
1158 if ($tableName == 'civicrm_state_province' ||
$tableName == 'civicrm_country' ||
$tableName == 'civicrm_county') {
1159 $this->_select
["{$tName}_id"] = "{$aName}.{$pf}_id as `{$tName}_id`";
1162 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1165 $this->_element
["{$tName}_id"] = 1;
1166 if (substr($tName, -15) == '-state_province') {
1167 // FIXME: hack to fix CRM-1900
1168 $a = Civi
::settings()->get('address_format');
1170 if (substr_count($a, 'state_province_name') > 0) {
1171 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1172 'pseudoField' => "{$pf}_id",
1173 'idCol' => "{$tName}_id",
1174 'bao' => 'CRM_Core_BAO_Address',
1176 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.name as `{$name}-{$elementFullName}`";
1179 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1180 'pseudoField' => 'state_province_abbreviation',
1181 'idCol' => "{$tName}_id",
1183 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.abbreviation as `{$name}-{$elementFullName}`";
1187 if (substr($elementFullName, 0, 2) == 'im') {
1188 $provider = "{$name}-{$elementFullName}-provider_id";
1189 $this->_select
[$provider] = "`$tName`.provider_id as `{$name}-{$elementFullName}-provider_id`";
1190 $this->_element
[$provider] = 1;
1192 if ($pf == 'country' ||
$pf == 'county') {
1193 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = array(
1194 'pseudoField' => "{$pf}_id",
1195 'idCol' => "{$tName}_id",
1196 'bao' => 'CRM_Core_BAO_Address',
1198 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1201 $this->_select
["{$name}-{$elementFullName}"] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1205 if (in_array($pf, array('state_province', 'country', 'county'))) {
1206 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['element'] = "{$name}-{$elementFullName}";
1209 $this->_element
["{$name}-{$elementFullName}"] = 1;
1212 if (empty($processed["`$tName`"])) {
1213 $processed["`$tName`"] = 1;
1214 $newName = $tableName . '_' . $index;
1215 switch ($tableName) {
1216 case 'civicrm_phone':
1217 case 'civicrm_email':
1219 case 'civicrm_openid':
1221 $this->_tables
[$tName] = "\nLEFT JOIN $tableName `$tName` ON contact_a.id = `$tName`.contact_id";
1222 if ($tableName != 'civicrm_phone') {
1223 $this->_tables
[$tName] .= " AND `$tName`.$lCond";
1225 elseif (is_numeric($name)) {
1226 $this->_select
[$tName] = "IF (`$tName`.is_primary = $name, `$tName`.phone, NULL) as `$tName`";
1229 // this special case to add phone type
1231 $phoneTypeCondition = " AND `$tName`.$cond ";
1232 //gross hack to pickup corrupted data also, CRM-7603
1233 if (strpos($cond, 'phone_type_id') !== FALSE) {
1234 $phoneTypeCondition = " AND ( `$tName`.$cond OR `$tName`.phone_type_id IS NULL ) ";
1235 if (!empty($lCond)) {
1236 $phoneTypeCondition .= " AND ( `$tName`.$lCond ) ";
1239 $this->_tables
[$tName] .= $phoneTypeCondition;
1242 //build locationType join
1243 $locationTypeJoin[$tName] = " ( `$tName`.location_type_id = $ltName.id )";
1246 $this->_whereTables
[$tName] = $this->_tables
[$tName];
1250 case 'civicrm_state_province':
1251 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $tName;
1252 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1253 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.state_province_id";
1255 $this->_whereTables
["{$name}-address"] = $addressJoin;
1259 case 'civicrm_country':
1260 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1261 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1262 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.country_id";
1264 $this->_whereTables
["{$name}-address"] = $addressJoin;
1268 case 'civicrm_county':
1269 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1270 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1271 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.county_id";
1273 $this->_whereTables
["{$name}-address"] = $addressJoin;
1279 $this->_whereTables
["{$name}-address"] = $addressJoin;
1288 // add location type join
1289 $ltypeJoin = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locationTypeJoin) . " )";
1290 $this->_tables
[$locationTypeName] = $ltypeJoin;
1292 // table should be present in $this->_whereTables,
1293 // to add its condition in location type join, CRM-3939.
1294 if ($addWhereCount) {
1295 $locClause = array();
1296 foreach ($this->_whereTables
as $tableName => $clause) {
1297 if (!empty($locationTypeJoin[$tableName])) {
1298 $locClause[] = $locationTypeJoin[$tableName];
1302 if (!empty($locClause)) {
1303 $this->_whereTables
[$locationTypeName] = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locClause) . " )";
1308 if (!empty($addressCustomFieldIds)) {
1309 $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds);
1310 foreach ($addressCustomFieldIds as $cfID => $locTypeName) {
1311 foreach ($locTypeName as $name => $dnc) {
1312 $this->_locationSpecificCustomFields
[$cfID] = array($name, array_search($name, $locationTypes));
1313 $fieldName = "$name-custom_{$cfID}";
1314 $tName = "$name-address-custom-{$cfID}";
1315 $aName = "`$name-address-custom-{$cfID}`";
1316 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1317 $this->_element
["{$tName}_id"] = 1;
1318 $this->_select
[$fieldName] = "`$tName`.{$customQuery->_fields[$cfID]['column_name']} as `{$fieldName}`";
1319 $this->_element
[$fieldName] = 1;
1320 $this->_tables
[$tName] = "\nLEFT JOIN {$customQuery->_fields[$cfID]['table_name']} $aName ON ($aName.entity_id = `$name-address`.id)";
1327 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
1329 public function addMultipleElements() {
1330 if (empty($this->_returnProperties
['website'])) {
1333 if (!is_array($this->_returnProperties
['website'])) {
1337 foreach ($this->_returnProperties
['website'] as $key => $elements) {
1338 foreach ($elements as $elementFullName => $dontCare) {
1339 $tName = "website-{$key}-{$elementFullName}";
1340 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1341 $this->_select
["{$tName}"] = "`$tName`.url as `{$tName}`";
1342 $this->_element
["{$tName}_id"] = 1;
1343 $this->_element
["{$tName}"] = 1;
1345 $type = "website-{$key}-website_type_id";
1346 $this->_select
[$type] = "`$tName`.website_type_id as `{$type}`";
1347 $this->_element
[$type] = 1;
1348 $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website `$tName` ON (`$tName`.contact_id = contact_a.id AND `$tName`.website_type_id = $key )";
1354 * Generate the query based on what type of query we need.
1356 * @param bool $count
1357 * @param bool $sortByChar
1358 * @param bool $groupContacts
1359 * @param bool $onlyDeleted
1362 * sql query parts as an array
1364 public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE, $onlyDeleted = FALSE) {
1365 // build permission clause
1366 $this->generatePermissionClause($onlyDeleted, $count);
1369 if (isset($this->_rowCountClause
)) {
1370 $select = "SELECT {$this->_rowCountClause}";
1372 elseif (isset($this->_distinctComponentClause
)) {
1373 // we add distinct to get the right count for components
1374 // for the more complex result set, we use GROUP BY the same id
1376 $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount";
1379 $select = 'SELECT count(DISTINCT contact_a.id) as rowCount';
1381 $from = $this->_simpleFromClause
;
1382 if ($this->_useDistinct
) {
1383 $this->_useGroupBy
= TRUE;
1386 elseif ($sortByChar) {
1387 $select = 'SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name';
1388 $from = $this->_simpleFromClause
;
1390 elseif ($groupContacts) {
1391 $select = 'SELECT contact_a.id as id';
1392 if ($this->_useDistinct
) {
1393 $this->_useGroupBy
= TRUE;
1395 $from = $this->_simpleFromClause
;
1398 if (!empty($this->_paramLookup
['group'])) {
1400 list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup
['group'][0];
1402 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
1403 $this->_paramLookup
['group'][0][1] = key($value);
1406 // Presumably the lines below come into manage groups screen.
1407 // make sure there is only one element
1408 // this is used when we are running under smog and need to know
1409 // how the contact was added (CRM-1203)
1410 $groups = (array) CRM_Utils_Array
::value($this->_paramLookup
['group'][0][1], $this->_paramLookup
['group'][0][2], $this->_paramLookup
['group'][0][2]);
1411 if ((count($this->_paramLookup
['group']) == 1) &&
1412 (count($groups) == 1)
1414 $groupId = $groups[0];
1416 //check if group is saved search
1417 $group = new CRM_Contact_BAO_Group();
1418 $group->id
= $groupId;
1421 if (!isset($group->saved_search_id
)) {
1422 $tbName = "civicrm_group_contact";
1423 // CRM-17254 don't retrieve extra fields if contact_id is specifically requested
1424 // as this will add load to an intentionally light query.
1425 // ideally this code would be removed as it appears to be to support CRM-1203
1426 // and passing in the required returnProperties from the url would
1427 // make more sense that globally applying the requirements of one form.
1428 if (($this->_returnProperties
!= array('contact_id'))) {
1429 $this->_select
['group_contact_id'] = "$tbName.id as group_contact_id";
1430 $this->_element
['group_contact_id'] = 1;
1431 $this->_select
['status'] = "$tbName.status as status";
1432 $this->_element
['status'] = 1;
1436 $this->_useGroupBy
= TRUE;
1438 if ($this->_useDistinct
&& !isset($this->_distinctComponentClause
)) {
1439 if (!($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
)) {
1441 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
1442 $this->_useDistinct
= FALSE;
1443 $this->_useGroupBy
= TRUE;
1447 $select = $this->getSelect();
1448 $from = $this->_fromClause
;
1452 if (!empty($this->_whereClause
)) {
1453 $where = "WHERE {$this->_whereClause}";
1456 if (!empty($this->_permissionWhereClause
) && empty($this->_displayRelationshipType
)) {
1457 if (empty($where)) {
1458 $where = "WHERE $this->_permissionWhereClause";
1461 $where = "$where AND $this->_permissionWhereClause";
1466 if (!empty($this->_having
)) {
1467 foreach ($this->_having
as $havingSets) {
1468 foreach ($havingSets as $havingSet) {
1469 $havingValue[] = $havingSet;
1472 $having = ' HAVING ' . implode(' AND ', $havingValue);
1475 // if we are doing a transform, do it here
1476 // use the $from, $where and $having to get the contact ID
1477 if ($this->_displayRelationshipType
) {
1478 $this->filterRelatedContacts($from, $where, $having);
1481 return array($select, $from, $where, $having);
1485 * Get where values from the parameters.
1487 * @param string $name
1488 * @param mixed $grouping
1492 public function getWhereValues($name, $grouping) {
1494 foreach ($this->_params
as $values) {
1495 if ($values[0] == $name && $values[3] == $grouping) {
1506 * @param bool $relative
1507 * @param string $from
1510 public static function fixDateValues($relative, &$from, &$to) {
1512 list($from, $to) = CRM_Utils_Date
::getFromTo($relative, $from, $to);
1517 * Convert values from form-appropriate to query-object appropriate.
1519 * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax.
1520 * So, ideally we would convert all fields to look like
1523 * // Set the operator for legacy reasons, but it is ignored
1525 * // array in sql filter syntax
1526 * 2 => array('BETWEEN' => array(1,60),
1531 * There are some examples of the syntax in
1532 * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship
1534 * More notes at CRM_Core_DAO::createSQLFilter
1536 * and a list of supported operators in CRM_Core_DAO
1538 * @param array $formValues
1539 * @param int $wildcard
1540 * @param bool $useEquals
1542 * @param string $apiEntity
1544 * @param array $entityReferenceFields
1545 * Field names of any entity reference fields (which will need reformatting to IN syntax).
1549 public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL,
1550 $entityReferenceFields = array()) {
1552 if (empty($formValues)) {
1556 self
::filterCountryFromValuesIfStateExists($formValues);
1558 // Handle relative dates first
1559 foreach (array_keys($formValues) as $id) {
1560 if (preg_match('/_date_relative$/', $id) ||
1561 $id == 'event_relative' ||
1562 $id == 'case_from_relative' ||
1563 $id == 'case_to_relative' ||
1564 $id == 'participant_relative'
1566 if ($id == 'event_relative') {
1567 $fromRange = 'event_start_date_low';
1568 $toRange = 'event_end_date_high';
1570 elseif ($id == 'participant_relative') {
1571 $fromRange = 'participant_register_date_low';
1572 $toRange = 'participant_register_date_high';
1574 elseif ($id == 'case_from_relative') {
1575 $fromRange = 'case_from_start_date_low';
1576 $toRange = 'case_from_start_date_high';
1578 elseif ($id == 'case_to_relative') {
1579 $fromRange = 'case_to_end_date_low';
1580 $toRange = 'case_to_end_date_high';
1583 $dateComponent = explode('_date_relative', $id);
1584 $fromRange = "{$dateComponent[0]}_date_low";
1585 $toRange = "{$dateComponent[0]}_date_high";
1588 if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
1589 CRM_Contact_BAO_Query
::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
1594 foreach ($formValues as $id => $values) {
1595 if (self
::isAlreadyProcessedForQueryFormat($values)) {
1596 $params[] = $values;
1600 self
::legacyConvertFormValues($id, $values);
1602 // The form uses 1 field to represent two db fields
1603 if ($id == 'contact_type' && $values && (!is_array($values) ||
!array_intersect(array_keys($values), CRM_Core_DAO
::acceptedSQLOperators()))) {
1604 $contactType = array();
1606 foreach ((array) $values as $key => $type) {
1607 $types = explode('__', is_numeric($type) ?
$key : $type, 2);
1608 $contactType[$types[0]] = $types[0];
1609 // Add sub-type if specified
1610 if (!empty($types[1])) {
1611 $subType[$types[1]] = $types[1];
1614 $params[] = array('contact_type', 'IN', $contactType, 0, 0);
1616 $params[] = array('contact_sub_type', 'IN', $subType, 0, 0);
1619 elseif ($id == 'privacy') {
1620 if (is_array($formValues['privacy'])) {
1621 $op = !empty($formValues['privacy']['do_not_toggle']) ?
'=' : '!=';
1622 foreach ($formValues['privacy'] as $key => $value) {
1624 $params[] = array($key, $op, $value, 0, 0);
1629 elseif ($id == 'email_on_hold') {
1630 if ($onHoldValue = CRM_Utils_Array
::value('email_on_hold', $formValues)) {
1631 // onHoldValue should be 0 or 1 or an array. Some legacy groups may hold ''
1632 // so in 5.11 we have an extra if that should become redundant over time.
1633 // https://lab.civicrm.org/dev/core/issues/745
1634 // @todo this renaming of email_on_hold to on_hold needs revisiting
1635 // it preceeds recent changes but causes the default not to reload.
1636 $onHoldValue = array_filter((array) $onHoldValue, 'is_numeric');
1637 if (!empty($onHoldValue)) {
1638 $params[] = ['on_hold', 'IN', $onHoldValue, 0, 0];
1642 elseif (substr($id, 0, 7) == 'custom_'
1644 substr($id, -9, 9) == '_relative'
1645 ||
substr($id, -5, 5) == '_from'
1646 ||
substr($id, -3, 3) == '_to'
1649 self
::convertCustomRelativeFields($formValues, $params, $values, $id);
1651 elseif (preg_match('/_date_relative$/', $id) ||
1652 $id == 'event_relative' ||
1653 $id == 'case_from_relative' ||
1654 $id == 'case_to_relative' ||
1655 $id == 'participant_relative'
1657 // Already handled in previous loop
1660 elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
1662 $params[] = array($id, 'IN', explode(',', $values), 0, 0);
1665 $values = CRM_Contact_BAO_Query
::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity);
1670 $params[] = $values;
1677 * Function to support legacy format for groups and tags.
1680 * @param array|int $values
1683 public static function legacyConvertFormValues($id, &$values) {
1684 $legacyElements = array(
1689 'membership_type_id',
1690 'membership_status_id',
1692 if (in_array($id, $legacyElements) && is_array($values)) {
1693 // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
1694 // as per the recent Search fixes $values need to be in standard array(id1, id2) format
1695 $values = CRM_Utils_Array
::convertCheckboxFormatToArray($values);
1700 * Fix values from query from/to something no-one cared enough to document.
1703 * @param array $values
1704 * @param int $wildcard
1705 * @param bool $useEquals
1707 * @param string $apiEntity
1709 * @return array|null
1711 public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) {
1712 // skip a few search variables
1713 static $skipWhere = NULL;
1714 static $likeNames = NULL;
1717 // Change camelCase EntityName to lowercase with underscores
1718 $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity);
1720 // check if $value is in OK (Operator as Key) format as used by Get API
1721 if (CRM_Utils_System
::isNull($values)) {
1733 'display_relationship_type',
1737 if (in_array($id, $skipWhere) ||
1738 substr($id, 0, 4) == '_qf_' ||
1739 substr($id, 0, 7) == 'hidden_'
1745 (substr($id, 0, strlen($apiEntity)) != $apiEntity) &&
1746 (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_') &&
1747 (substr($id, 0, 7) != 'custom_')
1749 $id = $apiEntity . '_' . $id;
1753 $likeNames = array('sort_name', 'email', 'note', 'display_name');
1756 // email comes in via advanced search
1757 // so use wildcard always
1758 if ($id == 'email') {
1762 if (!$useEquals && in_array($id, $likeNames)) {
1763 $result = array($id, 'LIKE', $values, 0, 1);
1765 elseif (is_string($values) && strpos($values, '%') !== FALSE) {
1766 $result = array($id, 'LIKE', $values, 0, 0);
1768 elseif ($id == 'contact_type' ||
1769 (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO
::acceptedSQLOperators(), TRUE))
1771 $result = array($id, 'IN', $values, 0, $wildcard);
1774 $result = array($id, '=', $values, 0, $wildcard);
1781 * Get the where clause for a single field.
1783 * @param array $values
1784 * @param string $apiEntity
1786 public function whereClauseSingle(&$values, $apiEntity = NULL) {
1787 if ($this->isARelativeDateField($values[0])) {
1788 $this->buildRelativeDateQuery($values);
1792 // do not process custom fields or prefixed contact ids or component params
1793 if (CRM_Core_BAO_CustomField
::getKeyID($values[0]) ||
1794 (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) ||
1795 (substr($values[0], 0, 13) == 'contribution_') ||
1796 (substr($values[0], 0, 6) == 'event_') ||
1797 (substr($values[0], 0, 12) == 'participant_') ||
1798 (substr($values[0], 0, 7) == 'member_') ||
1799 (substr($values[0], 0, 6) == 'grant_') ||
1800 (substr($values[0], 0, 7) == 'pledge_') ||
1801 (substr($values[0], 0, 5) == 'case_') ||
1802 (substr($values[0], 0, 10) == 'financial_') ||
1803 (substr($values[0], 0, 8) == 'payment_') ||
1804 (substr($values[0], 0, 11) == 'membership_')
1809 // skip for hook injected fields / params
1810 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
1811 if (array_key_exists($values[0], $extFields)) {
1815 switch ($values[0]) {
1816 case 'deleted_contacts':
1817 $this->deletedContacts($values);
1820 case 'contact_sub_type':
1821 $this->contactSubType($values);
1826 $this->group($values);
1829 // case tag comes from find contacts
1831 $this->tagSearch($values);
1835 case 'contact_tags':
1836 $this->tag($values);
1841 case 'note_subject':
1842 $this->notes($values);
1846 $this->ufUser($values);
1850 case 'display_name':
1851 $this->sortName($values);
1855 case 'postal_greeting':
1856 case 'email_greeting':
1857 $this->greetings($values);
1862 $this->email($values, $apiEntity);
1865 case 'phone_numeric':
1866 $this->phone_numeric($values);
1869 case 'phone_phone_type_id':
1870 case 'phone_location_type_id':
1871 $this->phone_option_group($values);
1874 case 'street_address':
1875 $this->street_address($values);
1878 case 'street_number':
1879 $this->street_number($values);
1882 case 'sortByCharacter':
1883 $this->sortByCharacter($values);
1886 case 'location_type':
1887 $this->locationType($values);
1891 $this->county($values);
1894 case 'state_province':
1895 case 'state_province_id':
1896 case 'state_province_name':
1897 $this->stateProvince($values);
1902 $this->country($values, FALSE);
1906 case 'postal_code_low':
1907 case 'postal_code_high':
1908 $this->postalCode($values);
1911 case 'activity_date':
1912 case 'activity_date_low':
1913 case 'activity_date_high':
1914 case 'activity_date_time_low':
1915 case 'activity_date_time_high':
1916 case 'activity_role':
1917 case 'activity_status_id':
1918 case 'activity_status':
1919 case 'activity_priority':
1920 case 'activity_priority_id':
1921 case 'followup_parent_id':
1923 case 'source_contact_id':
1924 case 'activity_text':
1925 case 'activity_option':
1926 case 'test_activities':
1927 case 'activity_type_id':
1928 case 'activity_type':
1929 case 'activity_survey_id':
1930 case 'activity_tags':
1931 case 'activity_taglist':
1932 case 'activity_test':
1933 case 'activity_campaign_id':
1934 case 'activity_engagement_level':
1936 case 'activity_result':
1937 case 'source_contact':
1938 CRM_Activity_BAO_Query
::whereClauseSingle($values, $this);
1943 case 'birth_date_low':
1944 case 'birth_date_high':
1945 case 'deceased_date_low':
1946 case 'deceased_date_high':
1947 $this->demographics($values);
1950 case 'age_asof_date':
1951 // handled by demographics
1954 case 'log_date_low':
1955 case 'log_date_high':
1956 $this->modifiedDates($values);
1960 $this->changeLog($values);
1963 case 'do_not_phone':
1964 case 'do_not_email':
1967 case 'do_not_trade':
1969 $this->privacy($values);
1972 case 'privacy_options':
1973 $this->privacyOptions($values);
1976 case 'privacy_operator':
1977 case 'privacy_toggle':
1978 // these are handled by privacy options
1981 case 'preferred_communication_method':
1982 $this->preferredCommunication($values);
1985 case 'relation_type_id':
1986 case 'relation_start_date_high':
1987 case 'relation_start_date_low':
1988 case 'relation_end_date_high':
1989 case 'relation_end_date_low':
1990 case 'relation_active_period_date_high':
1991 case 'relation_active_period_date_low':
1992 case 'relation_target_name':
1993 case 'relation_status':
1994 case 'relation_date_low':
1995 case 'relation_date_high':
1996 $this->relationship($values);
1997 $this->_relationshipValuesAdded
= TRUE;
2000 case 'task_status_id':
2001 $this->task($values);
2005 // since this case is handled with the above
2008 case 'prox_distance':
2009 CRM_Contact_BAO_ProximityQuery
::process($this, $values);
2012 case 'prox_street_address':
2014 case 'prox_postal_code':
2015 case 'prox_state_province_id':
2016 case 'prox_country_id':
2017 case 'prox_geo_code_1':
2018 case 'prox_geo_code_2':
2019 // handled by the proximity_distance clause
2023 $this->restWhere($values);
2029 * Given a list of conditions in params generate the required where clause.
2031 * @param string $apiEntity
2035 public function whereClause($apiEntity = NULL) {
2036 $this->_where
[0] = array();
2037 $this->_qill
[0] = array();
2039 $this->includeContactIds();
2040 if (!empty($this->_params
)) {
2041 foreach (array_keys($this->_params
) as $id) {
2042 if (empty($this->_params
[$id][0])) {
2045 // check for both id and contact_id
2046 if ($this->_params
[$id][0] == 'id' ||
$this->_params
[$id][0] == 'contact_id') {
2047 $this->_where
[0][] = self
::buildClause("contact_a.id", $this->_params
[$id][1], $this->_params
[$id][2]);
2048 $field = CRM_Utils_Array
::value('id', $this->_fields
);
2049 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(
2050 'CRM_Contact_BAO_Contact',
2052 $this->_params
[$id][2],
2053 $this->_params
[$id][1]
2055 $this->_qill
[0][] = ts("%1 %2 %3", [
2056 1 => $field['title'],
2062 $this->whereClauseSingle($this->_params
[$id], $apiEntity);
2066 CRM_Core_Component
::alterQuery($this, 'where');
2068 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'where');
2071 if ($this->_customQuery
) {
2072 // Added following if condition to avoid the wrong value display for 'my account' / any UF info.
2073 // Hope it wont affect the other part of civicrm.. if it does please remove it.
2074 if (!empty($this->_customQuery
->_where
)) {
2075 $this->_where
= CRM_Utils_Array
::crmArrayMerge($this->_where
, $this->_customQuery
->_where
);
2078 $this->_qill
= CRM_Utils_Array
::crmArrayMerge($this->_qill
, $this->_customQuery
->_qill
);
2082 $andClauses = array();
2085 if (!empty($this->_where
)) {
2086 foreach ($this->_where
as $grouping => $values) {
2087 if ($grouping > 0 && !empty($values)) {
2088 $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) ';
2093 if (!empty($this->_where
[0])) {
2094 $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where
[0]) . ' ) ';
2096 if (!empty($clauses)) {
2097 $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
2100 if ($validClauses > 1) {
2101 $this->_useDistinct
= TRUE;
2105 return implode(' AND ', $andClauses);
2109 * Generate where clause for any parameters not already handled.
2111 * @param array $values
2115 public function restWhere(&$values) {
2116 $name = CRM_Utils_Array
::value(0, $values);
2117 $op = CRM_Utils_Array
::value(1, $values);
2118 $value = CRM_Utils_Array
::value(2, $values);
2119 $grouping = CRM_Utils_Array
::value(3, $values);
2120 $wildcard = CRM_Utils_Array
::value(4, $values);
2122 if (isset($grouping) && empty($this->_where
[$grouping])) {
2123 $this->_where
[$grouping] = array();
2126 $multipleFields = array('url');
2128 //check if the location type exists for fields
2130 $locType = explode('-', $name);
2132 if (!in_array($locType[0], $multipleFields)) {
2133 //add phone type if exists
2134 if (isset($locType[2]) && $locType[2]) {
2135 $locType[2] = CRM_Core_DAO
::escapeString($locType[2]);
2139 $field = CRM_Utils_Array
::value($name, $this->_fields
);
2142 $field = CRM_Utils_Array
::value($locType[0], $this->_fields
);
2151 $locationType = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
2152 if (isset($locType[1]) && is_numeric($locType[1])) {
2153 $lType = $locationType[$locType[1]];
2156 $field['title'] .= " ($lType)";
2159 if (substr($name, 0, 14) === 'state_province') {
2160 if (isset($locType[1]) && is_numeric($locType[1])) {
2162 $aName = "{$lType}-address";
2163 $where = "`$aName`.state_province_id";
2166 $where = "civicrm_address.state_province_id";
2169 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value);
2170 $this->_tables
[$aName] = $this->_whereTables
[$aName] = 1;
2171 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
2172 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2174 elseif (!empty($field['pseudoconstant'])) {
2175 $this->optionValueQuery(
2176 $name, $op, $value, $grouping,
2177 'CRM_Contact_DAO_Contact',
2183 if ($name == 'gender_id') {
2184 self
::$_openedPanes[ts('Demographics')] = TRUE;
2187 elseif (substr($name, 0, 7) === 'country' ||
substr($name, 0, 6) === 'county') {
2188 $name = (substr($name, 0, 7) === 'country') ?
"country_id" : "county_id";
2189 if (isset($locType[1]) && is_numeric($locType[1])) {
2191 $aName = "{$lType}-address";
2192 $where = "`$aName`.$name";
2195 $where = "civicrm_address.$name";
2198 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value, 'Positive');
2199 $this->_tables
[$aName] = $this->_whereTables
[$aName] = 1;
2201 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
2202 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $field['title'], 2 => $qillop, 3 => $qillVal));
2204 elseif ($name === 'world_region') {
2205 $this->optionValueQuery(
2206 $name, $op, $value, $grouping,
2214 elseif ($name === 'is_deceased') {
2215 $this->setQillAndWhere($name, $op, $value, $grouping, $field);
2216 self
::$_openedPanes[ts('Demographics')] = TRUE;
2218 elseif ($name === 'created_date' ||
$name === 'modified_date' ||
$name === 'deceased_date' ||
$name === 'birth_date') {
2219 $appendDateTime = TRUE;
2220 if ($name === 'deceased_date' ||
$name === 'birth_date') {
2221 $appendDateTime = FALSE;
2222 self
::$_openedPanes[ts('Demographics')] = TRUE;
2224 $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime);
2226 elseif ($name === 'contact_id') {
2227 if (is_int($value)) {
2228 $this->_where
[$grouping][] = self
::buildClause($field['where'], $op, $value);
2229 $this->_qill
[$grouping][] = "$field[title] $op $value";
2232 elseif ($name === 'name') {
2233 $value = CRM_Core_DAO
::escapeString($value);
2236 $value = self
::getWildCardedValue($wildcard, $op, $value);
2238 CRM_Core_Error
::deprecatedFunctionWarning('Untested code path');
2239 // @todo it's likely this code path is obsolete / never called. It is definitely not
2240 // passed through in our test suite.
2241 $this->_where
[$grouping][] = self
::buildClause($field['where'], $op, "'$value'");
2242 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2244 elseif ($name === 'current_employer') {
2247 $value = self
::getWildCardedValue($wildcard, $op, $value);
2249 $ceWhereClause = self
::buildClause("contact_a.organization_name", $op,
2252 $ceWhereClause .= " AND contact_a.contact_type = 'Individual'";
2253 $this->_where
[$grouping][] = $ceWhereClause;
2254 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2256 elseif ($name === 'email_greeting') {
2257 CRM_Core_Error
::deprecatedFunctionWarning('pass in email_greeting_id or email_greeting_display');
2258 $filterCondition = array('greeting_type' => 'email_greeting');
2259 $this->optionValueQuery(
2260 $name, $op, $value, $grouping,
2261 CRM_Core_PseudoConstant
::greeting($filterCondition),
2263 ts('Email Greeting')
2266 elseif ($name === 'postal_greeting') {
2267 CRM_Core_Error
::deprecatedFunctionWarning('pass in postal_greeting_id or postal_greeting_display');
2268 $filterCondition = array('greeting_type' => 'postal_greeting');
2269 $this->optionValueQuery(
2270 $name, $op, $value, $grouping,
2271 CRM_Core_PseudoConstant
::greeting($filterCondition),
2273 ts('Postal Greeting')
2276 elseif ($name === 'addressee') {
2277 CRM_Core_Error
::deprecatedFunctionWarning('pass in addressee_id or addressee_display');
2278 $filterCondition = array('greeting_type' => 'addressee');
2279 $this->optionValueQuery(
2280 $name, $op, $value, $grouping,
2281 CRM_Core_PseudoConstant
::greeting($filterCondition),
2286 elseif (substr($name, 0, 4) === 'url-') {
2287 $tName = 'civicrm_website';
2288 $this->_whereTables
[$tName] = $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )";
2289 $value = CRM_Core_DAO
::escapeString($value);
2292 $value = self
::getWildCardedValue($wildcard, $op, $value);
2295 $this->_where
[$grouping][] = $d = self
::buildClause('civicrm_website.url', $op, $value);
2296 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2298 elseif ($name === 'contact_is_deleted') {
2299 $this->setQillAndWhere('is_deleted', $op, $value, $grouping, $field);
2301 elseif (!empty($field['where'])) {
2303 if (!empty($field['type'])) {
2304 $type = CRM_Utils_Type
::typeToString($field['type']);
2307 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2309 if (isset($locType[1]) &&
2310 is_numeric($locType[1])
2314 //get the location name
2315 list($tName, $fldName) = self
::getLocationTableName($field['where'], $locType);
2316 $fieldName = "`$tName`.$fldName";
2318 // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
2319 $this->_tables
[$tName] = $this->_whereTables
[$tName] = 1;
2323 if ($tableName == 'civicrm_contact') {
2324 $fieldName = "contact_a.{$fieldName}";
2327 $fieldName = $field['where'];
2331 list($qillop, $qillVal) = self
::buildQillForFieldValue(NULL, $field['title'], $value, $op);
2332 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(
2333 1 => $field['title'],
2335 3 => (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) ?
$qillVal : "'$qillVal'"));
2337 if (is_array($value)) {
2338 // traditionally an array being passed has been a fatal error. We can take advantage of this to add support
2339 // for api style operators for functions that hit this point without worrying about regression
2340 // (the previous comments indicated the condition for hitting this point were unknown
2341 // per CRM-14743 we are adding modified_date & created_date operator support
2342 $operations = array_keys($value);
2343 foreach ($operations as $operator) {
2344 if (!in_array($operator, CRM_Core_DAO
::acceptedSQLOperators())) {
2345 //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators
2346 //so this condition will satisfy the search for now
2347 if (strpos($op, 'IN') !== FALSE) {
2348 $value = array($op => $value);
2350 // we don't know when this might happen
2352 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", array(1 => $operator)));
2356 $this->_where
[$grouping][] = CRM_Core_DAO
::createSQLFilter($fieldName, $value, $type);
2361 $value = self
::getWildCardedValue($wildcard, $op, $value);
2364 $this->_where
[$grouping][] = self
::buildClause($fieldName, $op, $value, $type);
2368 if ($setTables && isset($field['where'])) {
2369 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2370 if (isset($tableName)) {
2371 $this->_tables
[$tableName] = 1;
2372 $this->_whereTables
[$tableName] = 1;
2384 public static function getLocationTableName(&$where, &$locType) {
2385 if (isset($locType[1]) && is_numeric($locType[1])) {
2386 list($tbName, $fldName) = explode(".", $where);
2388 //get the location name
2389 $locationType = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
2390 $specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
2391 if (in_array($locType[0], $specialFields)) {
2392 //hack to fix / special handing for phone_ext
2393 if ($locType[0] == 'phone_ext') {
2394 $locType[0] = 'phone';
2396 if (isset($locType[2]) && $locType[2]) {
2397 $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}";
2400 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2403 elseif (in_array($locType[0],
2408 'street_number_suffix',
2410 'supplemental_address_1',
2411 'supplemental_address_2',
2412 'supplemental_address_3',
2415 'postal_code_suffix',
2421 //fix for search by profile with address fields.
2422 $tName = "{$locationType[$locType[1]]}-address";
2424 elseif (in_array($locType[0],
2432 $tName = "{$locationType[$locType[1]]}-email";
2434 elseif ($locType[0] == 'provider_id') {
2435 $tName = "{$locationType[$locType[1]]}-im";
2437 elseif ($locType[0] == 'openid') {
2438 $tName = "{$locationType[$locType[1]]}-openid";
2441 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2443 $tName = str_replace(' ', '_', $tName);
2444 return array($tName, $fldName);
2446 CRM_Core_Error
::fatal();
2450 * Given a result dao, extract the values and return that array
2452 * @param CRM_Core_DAO $dao
2455 * values for this query
2457 public function store($dao) {
2460 foreach ($this->_element
as $key => $dontCare) {
2461 if (property_exists($dao, $key)) {
2462 if (strpos($key, '-') !== FALSE) {
2463 $values = explode('-', $key);
2464 $lastElement = array_pop($values);
2466 $cnt = count($values);
2468 foreach ($values as $v) {
2469 if (!array_key_exists($v, $current)) {
2470 $current[$v] = array();
2472 //bad hack for im_provider
2473 if ($lastElement == 'provider_id') {
2474 if ($count < $cnt) {
2475 $current = &$current[$v];
2478 $lastElement = "{$v}_{$lastElement}";
2482 $current = &$current[$v];
2487 $current[$lastElement] = $dao->$key;
2490 $value[$key] = $dao->$key;
2498 * Getter for tables array.
2502 public function tables() {
2503 return $this->_tables
;
2507 * Sometimes used to create the from clause, but, not reliably, set
2508 * this AND set tables.
2510 * It's unclear the intent - there is a 'simpleFrom' clause which
2511 * takes whereTables into account & a fromClause which doesn't.
2513 * logic may have eroded?
2517 public function whereTables() {
2518 return $this->_whereTables
;
2522 * Generate the where clause (used in match contacts and permissions)
2524 * @param array $params
2525 * @param array $fields
2526 * @param array $tables
2527 * @param $whereTables
2528 * @param bool $strict
2532 public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) {
2533 $query = new CRM_Contact_BAO_Query($params, NULL, $fields,
2537 $tables = array_merge($query->tables(), $tables);
2538 $whereTables = array_merge($query->whereTables(), $whereTables);
2540 return $query->_whereClause
;
2544 * Create the from clause.
2546 * @param array $tables
2547 * Tables that need to be included in this from clause. If null,
2548 * return mimimal from clause (i.e. civicrm_contact).
2549 * @param array $inner
2550 * Tables that should be inner-joined.
2551 * @param array $right
2552 * Tables that should be right-joined.
2553 * @param bool $primaryLocation
2554 * Search on primary location. See note below.
2556 * Determines search mode based on bitwise MODE_* constants.
2557 * @param string|NULL $apiEntity
2558 * Determines search mode based on entity by string.
2560 * The $primaryLocation flag only seems to be used when
2561 * locationType() has been called. This may be a search option
2562 * exposed, or perhaps it's a "search all details" approach which
2563 * predates decoupling of location types and primary fields?
2565 * @see https://issues.civicrm.org/jira/browse/CRM-19967
2570 public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1, $apiEntity = NULL) {
2572 $from = ' FROM civicrm_contact contact_a';
2573 if (empty($tables)) {
2577 if (!empty($tables['civicrm_worldregion'])) {
2578 $tables = array_merge(array('civicrm_country' => 1), $tables);
2581 if ((!empty($tables['civicrm_state_province']) ||
!empty($tables['civicrm_country']) ||
2582 CRM_Utils_Array
::value('civicrm_county', $tables)
2583 ) && empty($tables['civicrm_address'])
2585 $tables = array_merge(array('civicrm_address' => 1),
2590 // add group_contact and group table is subscription history is present
2591 if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) {
2592 $tables = array_merge(array(
2593 'civicrm_group' => 1,
2594 'civicrm_group_contact' => 1,
2600 // to handle table dependencies of components
2601 CRM_Core_Component
::tableNames($tables);
2602 // to handle table dependencies of hook injected tables
2603 CRM_Contact_BAO_Query_Hook
::singleton()->setTableDependency($tables);
2605 //format the table list according to the weight
2606 $info = CRM_Core_TableHierarchy
::info();
2608 foreach ($tables as $key => $value) {
2610 if (strpos($key, '-') !== FALSE) {
2611 $keyArray = explode('-', $key);
2612 $k = CRM_Utils_Array
::value('civicrm_' . $keyArray[1], $info, 99);
2614 elseif (strpos($key, '_') !== FALSE) {
2615 $keyArray = explode('_', $key);
2616 if (is_numeric(array_pop($keyArray))) {
2617 $k = CRM_Utils_Array
::value(implode('_', $keyArray), $info, 99);
2620 $k = CRM_Utils_Array
::value($key, $info, 99);
2624 $k = CRM_Utils_Array
::value($key, $info, 99);
2626 $tempTable[$k . ".$key"] = $key;
2629 $newTables = array();
2630 foreach ($tempTable as $key) {
2631 $newTables[$key] = $tables[$key];
2634 $tables = $newTables;
2636 foreach ($tables as $name => $value) {
2641 if (!empty($inner[$name])) {
2644 elseif (!empty($right[$name])) {
2652 // if there is already a join statement in value, use value itself
2653 if (strpos($value, 'JOIN')) {
2654 $from .= " $value ";
2657 $from .= " $side JOIN $name ON ( $value ) ";
2662 $from .= self
::getEntitySpecificJoins($name, $mode, $side, $primaryLocation);
2668 * Get join statements for the from clause depending on entity type
2670 * @param string $name
2672 * @param string $side
2673 * @param string $primaryLocation
2676 protected static function getEntitySpecificJoins($name, $mode, $side, $primaryLocation) {
2677 $limitToPrimaryClause = $primaryLocation ?
"AND {$name}.is_primary = 1" : '';
2679 case 'civicrm_address':
2680 //CRM-14263 further handling of address joins further down...
2681 return " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id {$limitToPrimaryClause} )";
2683 case 'civicrm_state_province':
2684 // This is encountered when doing an export after having applied a 'sort' - it pretty much implies primary
2685 // but that will have been implied-in by the calling function.
2686 // test cover in testContactIDQuery
2687 return " $side JOIN civicrm_state_province ON ( civicrm_address.state_province_id = civicrm_state_province.id )";
2689 case 'civicrm_country':
2690 // This is encountered when doing an export after having applied a 'sort' - it pretty much implies primary
2691 // but that will have been implied-in by the calling function.
2692 // test cover in testContactIDQuery
2693 return " $side JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id )";
2695 case 'civicrm_phone':
2696 return " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id {$limitToPrimaryClause}) ";
2698 case 'civicrm_email':
2699 return " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id {$limitToPrimaryClause})";
2702 return " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id {$limitToPrimaryClause}) ";
2705 $from = " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) ";
2706 $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'";
2707 $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)";
2710 case 'civicrm_openid':
2711 return " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id {$limitToPrimaryClause} )";
2713 case 'civicrm_worldregion':
2714 // We can be sure from the calling function that country will already be joined in.
2715 // we really don't need world_region - we could use a pseudoconstant for it.
2716 return "$side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ";
2718 case 'civicrm_location_type':
2719 return " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id ";
2721 case 'civicrm_group':
2722 return " $side JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id ";
2724 case 'civicrm_group_contact':
2725 return " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id ";
2727 case 'civicrm_group_contact_cache':
2728 return " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id ";
2730 case 'civicrm_activity':
2731 case 'civicrm_activity_tag':
2732 case 'activity_type':
2733 case 'activity_status':
2735 case 'civicrm_activity_contact':
2736 case 'source_contact':
2737 case 'activity_priority':
2738 return CRM_Activity_BAO_Query
::from($name, $mode, $side);
2740 case 'civicrm_entity_tag':
2741 $from = " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact'";
2742 return "$from AND civicrm_entity_tag.entity_id = contact_a.id ) ";
2744 case 'civicrm_note':
2745 $from = " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact'";
2746 return "$from AND contact_a.id = civicrm_note.entity_id ) ";
2748 case 'civicrm_subscription_history':
2749 $from = " $side JOIN civicrm_subscription_history";
2750 $from .= " ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id";
2751 return "$from AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id";
2753 case 'civicrm_relationship':
2754 if (self
::$_relType == 'reciprocal') {
2755 if (self
::$_relationshipTempTable) {
2756 // we have a temptable to join on
2757 $tbl = self
::$_relationshipTempTable;
2758 return " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id";
2761 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)";
2762 $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)";
2766 elseif (self
::$_relType == 'b') {
2767 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )";
2768 return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
2771 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )";
2772 return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
2776 $from = " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')";
2777 return "$from INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)";
2780 return " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id ";
2782 case 'civicrm_grant':
2783 return CRM_Grant_BAO_Query
::from($name, $mode, $side);
2785 case 'civicrm_website':
2786 return " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id ";
2788 case 'civicrm_campaign':
2789 //Move to default case if not in either mode.
2790 if ($mode & CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
) {
2791 return CRM_Contribute_BAO_Query
::from($name, $mode, $side);
2793 elseif ($mode & CRM_Contact_BAO_Query
::MODE_MAILING
) {
2794 return CRM_Mailing_BAO_Query
::from($name, $mode, $side);
2796 elseif ($mode & CRM_Contact_BAO_Query
::MODE_CAMPAIGN
) {
2797 return CRM_Campaign_BAO_Query
::from($name, $mode, $side);
2801 $locationTypeName = '';
2802 if (strpos($name, '-address') != 0) {
2803 $locationTypeName = 'address';
2805 elseif (strpos($name, '-phone') != 0) {
2806 $locationTypeName = 'phone';
2808 elseif (strpos($name, '-email') != 0) {
2809 $locationTypeName = 'email';
2811 elseif (strpos($name, '-im') != 0) {
2812 $locationTypeName = 'im';
2814 elseif (strpos($name, '-openid') != 0) {
2815 $locationTypeName = 'openid';
2818 if ($locationTypeName) {
2819 //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
2820 $parts = explode('-', $name);
2821 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
2822 foreach ($locationTypes as $locationTypeID => $locationType) {
2823 if ($parts[0] == str_replace(' ', '_', $locationType)) {
2824 $locationID = $locationTypeID;
2827 $from = " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
2830 $from = CRM_Core_Component
::from($name, $mode, $side);
2832 $from .= CRM_Contact_BAO_Query_Hook
::singleton()->buildSearchfrom($name, $mode, $side);
2839 * WHERE / QILL clause for deleted_contacts
2841 * @param array $values
2843 public function deletedContacts($values) {
2844 list($_, $_, $value, $grouping, $_) = $values;
2846 // *prepend* to the relevant grouping as this is quite an important factor
2847 array_unshift($this->_qill
[$grouping], ts('Search in Trash'));
2852 * Where / qill clause for contact_type
2856 public function contactType(&$values) {
2857 list($name, $op, $value, $grouping, $wildcard) = $values;
2859 $subTypes = array();
2862 // account for search builder mapping multiple values
2863 if (!is_array($value)) {
2864 $values = self
::parseSearchBuilderString($value, 'String');
2865 if (is_array($values)) {
2866 $value = array_flip($values);
2870 if (is_array($value)) {
2871 foreach ($value as $k => $v) {
2876 if (strpos($k, CRM_Core_DAO
::VALUE_SEPARATOR
)) {
2877 list($contactType, $subType) = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $k, 2);
2880 if (!empty($subType)) {
2881 $subTypes[$subType] = 1;
2883 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2888 $contactTypeANDSubType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value, 2);
2889 $contactType = $contactTypeANDSubType[0];
2890 $subType = CRM_Utils_Array
::value(1, $contactTypeANDSubType);
2891 if (!empty($subType)) {
2892 $subTypes[$subType] = 1;
2894 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2898 if (!empty($clause)) {
2900 if ($op == 'IN' ||
$op == 'NOT IN') {
2901 $this->_where
[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')';
2904 $type = array_pop($clause);
2905 $this->_where
[$grouping][] = self
::buildClause("contact_a.contact_type", $op, $contactType);
2908 $this->_qill
[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill);
2910 if (!empty($subTypes)) {
2911 $this->includeContactSubTypes($subTypes, $grouping);
2917 * Where / qill clause for contact_sub_type.
2919 * @param array $values
2921 public function contactSubType(&$values) {
2922 list($name, $op, $value, $grouping, $wildcard) = $values;
2923 $this->includeContactSubTypes($value, $grouping, $op);
2931 public function includeContactSubTypes($value, $grouping, $op = 'LIKE') {
2933 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2935 $value = $value[$op];
2939 $alias = "contact_a.contact_sub_type";
2940 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
2942 $op = str_replace('IN', 'LIKE', $op);
2943 $op = str_replace('=', 'LIKE', $op);
2944 $op = str_replace('!', 'NOT ', $op);
2946 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
2947 $this->_where
[$grouping][] = self
::buildClause($alias, $op, $value, 'String');
2949 elseif (is_array($value)) {
2950 foreach ($value as $k => $v) {
2951 $clause[$k] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($v, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
2955 $clause[$value] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($value, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
2958 if (!empty($clause)) {
2959 $this->_where
[$grouping][] = "( " . implode(' OR ', $clause) . " )";
2961 $this->_qill
[$grouping][] = ts('Contact Subtype %1 ', array(1 => $qillOperators[$op])) . implode(' ' . ts('or') . ' ', array_keys($clause));
2965 * Where / qill clause for groups.
2969 public function group($values) {
2970 list($name, $op, $value, $grouping, $wildcard) = $values;
2972 // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first
2973 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2975 $value = $value[$op];
2978 // Replace pseudo operators from search builder
2979 $op = str_replace('EMPTY', 'NULL', $op);
2981 if (strpos($op, 'NULL')) {
2985 if (is_array($value) && count($value) > 1) {
2986 if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) {
2987 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", array(1 => $op)));
2989 $this->_useDistinct
= TRUE;
2992 if (isset($value)) {
2993 $value = CRM_Utils_Array
::value($op, $value, $value);
2996 if ($name == 'group_type') {
2997 $value = array_keys($this->getGroupsFromTypeCriteria($value));
3000 $regularGroupIDs = $smartGroupIDs = array();
3001 foreach ((array) $value as $id) {
3002 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
3003 $smartGroupIDs[] = $id;
3006 $regularGroupIDs[] = trim($id);
3010 $isNotOp = ($op == 'NOT IN' ||
$op == '!=');
3013 $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
3015 is_array($gcsValues[2])
3017 foreach ($gcsValues[2] as $k => $v) {
3019 $statii[] = "'" . CRM_Utils_Type
::escape($k, 'String') . "'";
3024 $statii[] = "'Added'";
3026 $groupClause = array();
3027 if (count($regularGroupIDs) ||
empty($value)) {
3028 // include child groups IDs if any
3029 $childGroupIds = (array) CRM_Contact_BAO_Group
::getChildGroupIds($regularGroupIDs);
3030 foreach ($childGroupIds as $key => $id) {
3031 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
3032 $smartGroupIDs[] = $id;
3033 unset($childGroupIds[$key]);
3036 if (count($childGroupIds)) {
3037 $regularGroupIDs = array_merge($regularGroupIDs, $childGroupIds);
3040 if (empty($regularGroupIDs)) {
3041 $regularGroupIDs = [0];
3044 // if $regularGroupIDs is populated with regular child group IDs
3045 // then change the mysql operator to desired
3046 if (count($regularGroupIDs) > 1) {
3047 $op = strpos($op, 'IN') ?
$op : ($op == '!=') ?
'NOT IN' : 'IN';
3050 if (!empty($regularGroupIDs)) {
3051 $groupIds = CRM_Utils_Type
::validate(
3052 implode(',', (array) $regularGroupIDs),
3053 'CommaSeparatedIntegers'
3056 $gcTable = "`civicrm_group_contact-" . uniqid() . "`";
3057 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
3059 if (strpos($op, 'IN') !== FALSE) {
3060 $clause = "{$gcTable}.group_id $op ( $groupIds ) ";
3062 elseif ($op == '!=') {
3063 $clause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds )";
3066 $clause = "{$gcTable}.group_id $op $groupIds ";
3068 $groupClause[] = "( {$clause} )";
3071 $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")";
3073 $this->_tables
[$gcTable] = $this->_whereTables
[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
3076 //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table
3077 $groupContactCacheClause = '';
3078 if (count($smartGroupIDs) ||
empty($value)) {
3079 $this->_groupUniqueKey
= uniqid();
3080 $this->_groupKeys
[] = $this->_groupUniqueKey
;
3081 $gccTableAlias = "civicrm_group_contact_cache_{$this->_groupUniqueKey}";
3082 $groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, $gccTableAlias, "contact_a", $op);
3083 if (!empty($groupContactCacheClause)) {
3085 $groupIds = implode(',', (array) $smartGroupIDs);
3086 $gcTable = "civicrm_group_contact_{$this->_groupUniqueKey}";
3087 $joinClause = array("contact_a.id = {$gcTable}.contact_id");
3088 $this->_tables
[$gcTable] = $this->_whereTables
[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
3089 if (strpos($op, 'IN') !== FALSE) {
3090 $groupClause[] = "{$gcTable}.group_id $op ( $groupIds ) AND {$gccTableAlias}.group_id IS NULL";
3093 $groupClause[] = "{$gcTable}.group_id $op $groupIds AND {$gccTableAlias}.group_id IS NULL";
3096 $groupClause[] = " ( {$groupContactCacheClause} ) ";
3100 $and = ($op == 'IS NULL') ?
' AND ' : ' OR ';
3101 if (!empty($groupClause)) {
3102 $this->_where
[$grouping][] = ' ( ' . implode($and, $groupClause) . ' ) ';
3105 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
3106 $this->_qill
[$grouping][] = ts("Group(s) %1 %2", array(1 => $qillop, 2 => $qillVal));
3107 if (strpos($op, 'NULL') === FALSE) {
3108 $this->_qill
[$grouping][] = ts("Group Status %1", array(1 => implode(' ' . ts('or') . ' ', $statii)));
3112 public function getGroupCacheTableKeys() {
3113 return $this->_groupKeys
;
3117 * Function translates selection of group type into a list of groups.
3122 public function getGroupsFromTypeCriteria($value) {
3123 $groupIds = array();
3124 foreach ((array) $value as $groupTypeValue) {
3125 $groupList = CRM_Core_PseudoConstant
::group($groupTypeValue);
3126 $groupIds = ($groupIds +
$groupList);
3132 * Prime smart group cache for smart groups in the search, and join
3133 * civicrm_group_contact_cache table into the query.
3135 * @param array $groups IDs of groups specified in search criteria.
3136 * @param string $tableAlias Alias to use for civicrm_group_contact_cache table.
3137 * @param string $joinTable Table on which to join civicrm_group_contact_cache
3138 * @param string $op SQL comparison operator (NULL, IN, !=, IS NULL, etc.)
3139 * @param string $joinColumn Column in $joinTable on which to join civicrm_group_contact_cache.contact_id
3141 * @return string WHERE clause component for smart group criteria.
3143 public function addGroupContactCache($groups, $tableAlias, $joinTable = "contact_a", $op, $joinColumn = 'id') {
3144 $isNullOp = (strpos($op, 'NULL') !== FALSE);
3145 $groupsIds = $groups;
3147 $operator = ['=' => 'IN', '!=' => 'NOT IN'];
3148 if (!empty($operator[$op]) && is_array($groups)) {
3149 $op = $operator[$op];
3151 if (!$isNullOp && !$groups) {
3154 elseif (strpos($op, 'IN') !== FALSE) {
3155 $groups = array($op => $groups);
3157 elseif (is_array($groups) && count($groups)) {
3158 $groups = array('IN' => $groups);
3161 // Find all the groups that are part of a saved search.
3162 $smartGroupClause = self
::buildClause("id", $op, $groups, 'Int');
3164 SELECT id, cache_date, saved_search_id, children
3166 WHERE $smartGroupClause
3167 AND ( saved_search_id != 0
3168 OR saved_search_id IS NOT NULL
3169 OR children IS NOT NULL )
3172 $group = CRM_Core_DAO
::executeQuery($sql);
3174 while ($group->fetch()) {
3175 $this->_useDistinct
= TRUE;
3176 if (!$this->_smartGroupCache ||
$group->cache_date
== NULL) {
3177 CRM_Contact_BAO_GroupContactCache
::load($group);
3180 if ($group->N
== 0 && $op != 'NOT IN') {
3184 $this->_tables
[$tableAlias] = $this->_whereTables
[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.{$joinColumn} = {$tableAlias}.contact_id ";
3186 if ($op == 'NOT IN') {
3187 return "{$tableAlias}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( " . implode(',', (array) $groupsIds) . " ) )";
3189 return self
::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
3193 * Where / qill clause for cms users
3197 public function ufUser(&$values) {
3198 list($name, $op, $value, $grouping, $wildcard) = $values;
3201 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3203 $this->_qill
[$grouping][] = ts('CMS User');
3205 elseif ($value == 0) {
3206 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3208 $this->_where
[$grouping][] = " civicrm_uf_match.contact_id IS NULL";
3209 $this->_qill
[$grouping][] = ts('Not a CMS User');
3214 * All tag search specific.
3216 * @param array $values
3218 public function tagSearch(&$values) {
3219 list($name, $op, $value, $grouping, $wildcard) = $values;
3222 $value = "%{$value}%";
3223 $escapedValue = CRM_Utils_Type
::escape("%{$value}%", 'String');
3225 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3226 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3228 $etTable = "`civicrm_entity_tag-" . uniqid() . "`";
3229 $tTable = "`civicrm_tag-" . uniqid() . "`";
3231 if ($useAllTagTypes[2]) {
3232 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3233 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id)
3234 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )";
3236 // search tag in cases
3237 $etCaseTable = "`civicrm_entity_case_tag-" . uniqid() . "`";
3238 $tCaseTable = "`civicrm_case_tag-" . uniqid() . "`";
3239 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3240 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3241 LEFT JOIN civicrm_case
3242 ON (civicrm_case_contact.case_id = civicrm_case.id
3243 AND civicrm_case.is_deleted = 0 )
3244 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id )
3245 LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )";
3246 // search tag in activities
3247 $etActTable = "`civicrm_entity_act_tag-" . uniqid() . "`";
3248 $tActTable = "`civicrm_act_tag-" . uniqid() . "`";
3249 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
3250 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3252 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3253 = " LEFT JOIN civicrm_activity_contact
3254 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3255 LEFT JOIN civicrm_activity
3256 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3257 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3258 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id )
3259 LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )";
3261 $this->_where
[$grouping][] = "({$tTable}.name $op '" . $escapedValue . "' OR {$tCaseTable}.name $op '" . $escapedValue . "' OR {$tActTable}.name $op '" . $escapedValue . "')";
3262 $this->_qill
[$grouping][] = ts('Tag %1 %2', array(1 => $tagTypesText[2], 2 => $op)) . ' ' . $value;
3265 $etTable = "`civicrm_entity_tag-" . uniqid() . "`";
3266 $tTable = "`civicrm_tag-" . uniqid() . "`";
3267 $this->_tables
[$etTable] = $this->_whereTables
[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND
3268 {$etTable}.entity_table = 'civicrm_contact' )
3269 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) ";
3271 $this->_where
[$grouping][] = self
::buildClause("{$tTable}.name", $op, $value, 'String');
3272 $this->_qill
[$grouping][] = ts('Tagged %1', array(1 => $op)) . ' ' . $value;
3277 * Where / qill clause for tag.
3279 * @param array $values
3281 public function tag(&$values) {
3282 list($name, $op, $value, $grouping, $wildcard) = $values;
3284 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, array('onlyActive' => FALSE));
3285 // API/Search Builder format array(operator => array(values))
3286 if (is_array($value)) {
3287 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3289 $value = $value[$op];
3291 if (count($value) > 1) {
3292 $this->_useDistinct
= TRUE;
3296 // implode array, then remove all spaces
3297 $value = str_replace(' ', '', implode(',', (array) $value));
3298 if (!empty($value)) {
3299 $value = CRM_Utils_Type
::validate(
3301 'CommaSeparatedIntegers'
3305 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3306 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3308 $etTable = "`civicrm_entity_tag-" . uniqid() . "`";
3310 if ($useAllTagTypes[2]) {
3311 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3312 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3314 // search tag in cases
3315 $etCaseTable = "`civicrm_entity_case_tag-" . uniqid() . "`";
3316 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
3317 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3319 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3320 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3321 LEFT JOIN civicrm_case
3322 ON (civicrm_case_contact.case_id = civicrm_case.id
3323 AND civicrm_case.is_deleted = 0 )
3324 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) ";
3325 // search tag in activities
3326 $etActTable = "`civicrm_entity_act_tag-" . uniqid() . "`";
3327 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3328 = " LEFT JOIN civicrm_activity_contact
3329 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3330 LEFT JOIN civicrm_activity
3331 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3332 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3333 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) ";
3336 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3337 $this->_where
[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)";
3340 $this->_where
[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
3344 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3345 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3348 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3349 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3350 $op = str_replace('EMPTY', 'NULL', $op);
3351 $this->_where
[$grouping][] = "{$etTable}.tag_id $op";
3353 // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag
3354 elseif ($op == '!=') {
3355 $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') . ")";
3357 elseif ($op == '=' ||
strstr($op, 'IN')) {
3358 $op = ($op == '=') ?
'IN' : $op;
3359 $this->_where
[$grouping][] = "{$etTable}.tag_id $op ( $value )";
3362 $this->_qill
[$grouping][] = ts('Tagged %1 %2', array(1 => $qillop, 2 => $qillVal));
3366 * Where/qill clause for notes
3368 * @param array $values
3370 public function notes(&$values) {
3371 list($name, $op, $value, $grouping, $wildcard) = $values;
3373 $noteOptionValues = $this->getWhereValues('note_option', $grouping);
3374 $noteOption = CRM_Utils_Array
::value('2', $noteOptionValues, '6');
3375 $noteOption = ($name == 'note_body') ?
2 : (($name == 'note_subject') ?
3 : $noteOption);
3377 $this->_useDistinct
= TRUE;
3379 $this->_tables
['civicrm_note'] = $this->_whereTables
['civicrm_note']
3380 = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) ";
3383 $value = CRM_Core_DAO
::escapeString($n);
3385 if (strpos($value, '%') === FALSE) {
3386 $value = "%$value%";
3390 elseif ($op == 'IS NULL' ||
$op == 'IS NOT NULL') {
3396 if ($noteOption %
2 == 0) {
3397 $clauses[] = self
::buildClause('civicrm_note.note', $op, $value, 'String');
3398 $label = ts('Note: Body Only');
3400 if ($noteOption %
3 == 0) {
3401 $clauses[] = self
::buildClause('civicrm_note.subject', $op, $value, 'String');
3402 $label = $label ?
ts('Note: Body and Subject') : ts('Note: Subject Only');
3404 $this->_where
[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
3405 list($qillOp, $qillVal) = self
::buildQillForFieldValue(NULL, $name, $n, $op);
3406 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillOp, 3 => $qillVal));
3410 * @param string $name
3416 public function nameNullOrEmptyOp($name, $op, $grouping) {
3420 $this->_where
[$grouping][] = "contact_a.$name $op";
3421 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3425 $this->_where
[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')";
3426 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3429 case 'IS NOT EMPTY':
3430 $this->_where
[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')";
3431 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3440 * Where / qill clause for sort_name
3442 * @param array $values
3444 public function sortName(&$values) {
3445 list($fieldName, $op, $value, $grouping, $wildcard) = $values;
3447 // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY
3448 if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) {
3452 $input = $value = trim($value);
3454 if (!strlen($value)) {
3458 $config = CRM_Core_Config
::singleton();
3462 //By default, $sub elements should be joined together with OR statements (don't change this variable).
3465 $firstChar = substr($value, 0, 1);
3466 $lastChar = substr($value, -1, 1);
3467 $quotes = array("'", '"');
3468 // If string is quoted, strip quotes and otherwise don't alter it
3469 if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) {
3470 $value = trim($value, implode('', $quotes));
3472 // Replace spaces with wildcards for a LIKE operation
3473 // UNLESS string contains a comma (this exception is a tiny bit questionable)
3474 elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) {
3475 $value = str_replace(' ', '%', $value);
3477 $value = CRM_Core_DAO
::escapeString(trim($value));
3478 if (strlen($value)) {
3479 $fieldsub = array();
3480 $value = "'" . self
::getWildCardedValue($wildcard, $op, $value) . "'";
3481 if ($fieldName == 'sort_name') {
3482 $wc = "contact_a.sort_name";
3485 $wc = "contact_a.display_name";
3487 $fieldsub[] = " ( $wc $op $value )";
3488 if ($config->includeNickNameInName
) {
3489 $wc = "contact_a.nick_name";
3490 $fieldsub[] = " ( $wc $op $value )";
3492 if ($config->includeEmailInName
) {
3493 $fieldsub[] = " ( civicrm_email.email $op $value ) ";
3495 $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
3498 $sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
3500 $this->_where
[$grouping][] = $sub;
3501 if ($config->includeEmailInName
) {
3502 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3503 $this->_qill
[$grouping][] = ts('Name or Email') . " $op - '$input'";
3506 $this->_qill
[$grouping][] = ts('Name') . " $op - '$input'";
3511 * Where/qill clause for greeting fields.
3513 * @param array $values
3515 public function greetings(&$values) {
3516 list($name, $op, $value, $grouping, $wildcard) = $values;
3517 $name .= '_display';
3519 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
3520 $this->_qill
[$grouping][] = ts('Greeting %1 %2', array(1 => $qillop, 2 => $qillVal));
3521 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value, 'String');
3525 * Where / qill clause for email
3527 * @param array $values
3528 * @param string $apiEntity
3530 protected function email(&$values, $apiEntity) {
3531 list($name, $op, $value, $grouping, $wildcard) = $values;
3532 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3534 // CRM-18147: for Contact's GET API, email fieldname got appended with its entity as in {$apiEntiy}_{$name}
3535 // so following code is use build whereClause for contact's primart email id
3536 if (!empty($apiEntity)) {
3537 $dataType = 'String';
3538 if ($name == 'email_id') {
3539 $dataType = 'Integer';
3543 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.is_primary', '=', 1, 'Integer');
3544 $this->_where
[$grouping][] = self
::buildClause("civicrm_email.$name", $op, $value, $dataType);
3550 if (substr($n, 0, 1) == '"' &&
3551 substr($n, -1, 1) == '"'
3553 $n = substr($n, 1, -1);
3554 $value = CRM_Core_DAO
::escapeString($n);
3558 $value = self
::getWildCardedValue($wildcard, $op, $n);
3560 $this->_qill
[$grouping][] = ts('Email') . " $op '$n'";
3561 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, $value, 'String');
3564 $this->_qill
[$grouping][] = ts('Email') . " $op ";
3565 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, NULL, 'String');
3570 * Where / qill clause for phone number
3572 * @param array $values
3574 public function phone_numeric(&$values) {
3575 list($name, $op, $value, $grouping, $wildcard) = $values;
3576 // Strip non-numeric characters; allow wildcards
3577 $number = preg_replace('/[^\d%]/', '', $value);
3579 if (strpos($number, '%') === FALSE) {
3580 $number = "%$number%";
3583 $this->_qill
[$grouping][] = ts('Phone number contains') . " $number";
3584 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String');
3585 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3590 * Where / qill clause for phone type/location
3592 * @param array $values
3594 public function phone_option_group($values) {
3595 list($name, $op, $value, $grouping, $wildcard) = $values;
3596 $option = ($name == 'phone_phone_type_id' ?
'phone_type_id' : 'location_type_id');
3597 $options = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', $option);
3598 $optionName = $options[$value];
3599 $this->_qill
[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ?
ts('type') : ('location')) . " $op $optionName";
3600 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer');
3601 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3605 * Where / qill clause for street_address.
3607 * @param array $values
3609 public function street_address(&$values) {
3610 list($name, $op, $value, $grouping) = $values;
3619 if (strpos($value, '%') === FALSE) {
3620 // only add wild card if not there
3621 $value = "%{$value}%";
3624 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_address', $op, $value, 'String');
3625 $this->_qill
[$grouping][] = ts('Street') . " $op '$n'";
3628 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_address', $op, NULL, 'String');
3629 $this->_qill
[$grouping][] = ts('Street') . " $op ";
3632 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3636 * Where / qill clause for street_unit.
3638 * @param array $values
3640 public function street_number(&$values) {
3641 list($name, $op, $value, $grouping, $wildcard) = $values;
3649 if (strtolower($n) == 'odd') {
3650 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )";
3651 $this->_qill
[$grouping][] = ts('Street Number is odd');
3653 elseif (strtolower($n) == 'even') {
3654 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )";
3655 $this->_qill
[$grouping][] = ts('Street Number is even');
3659 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_number', $op, $value, 'String');
3660 $this->_qill
[$grouping][] = ts('Street Number') . " $op '$n'";
3663 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3667 * Where / qill clause for sorting by character.
3669 * @param array $values
3671 public function sortByCharacter(&$values) {
3672 list($name, $op, $value, $grouping, $wildcard) = $values;
3674 $name = trim($value);
3675 $cond = " contact_a.sort_name LIKE '" . CRM_Core_DAO
::escapeWildCardString($name) . "%'";
3676 $this->_where
[$grouping][] = $cond;
3677 $this->_qill
[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', array(1 => $name));
3681 * Where / qill clause for including contact ids.
3683 public function includeContactIDs() {
3684 if (!$this->_includeContactIds ||
empty($this->_params
)) {
3688 $contactIds = array();
3689 foreach ($this->_params
as $id => $values) {
3690 if (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
3691 $contactIds[] = substr($values[0], CRM_Core_Form
::CB_PREFIX_LEN
);
3694 CRM_Utils_Type
::validateAll($contactIds, 'Positive');
3695 if (!empty($contactIds)) {
3696 $this->_where
[0][] = " ( contact_a.id IN (" . implode(',', $contactIds) . " ) ) ";
3701 * Where / qill clause for postal code.
3703 * @param array $values
3705 public function postalCode(&$values) {
3706 // skip if the fields dont have anything to do with postal_code
3707 if (empty($this->_fields
['postal_code'])) {
3711 list($name, $op, $value, $grouping, $wildcard) = $values;
3713 // Handle numeric postal code range searches properly by casting the column as numeric
3714 if (is_numeric($value)) {
3715 $field = "IF (civicrm_address.postal_code REGEXP '^[0-9]{1,10}$', CAST(civicrm_address.postal_code AS UNSIGNED), 0)";
3716 $val = CRM_Utils_Type
::escape($value, 'Integer');
3719 $field = 'civicrm_address.postal_code';
3720 // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string.
3721 if (!is_array($value)) {
3722 $val = CRM_Utils_Type
::escape($value, 'String');
3725 // Do we need to escape values here? I would expect buildClause does.
3730 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3732 if ($name == 'postal_code') {
3733 $this->_where
[$grouping][] = self
::buildClause($field, $op, $val, 'String');
3734 $this->_qill
[$grouping][] = ts('Postal code') . " {$op} {$value}";
3736 elseif ($name == 'postal_code_low') {
3737 $this->_where
[$grouping][] = " ( $field >= '$val' ) ";
3738 $this->_qill
[$grouping][] = ts('Postal code greater than or equal to \'%1\'', array(1 => $value));
3740 elseif ($name == 'postal_code_high') {
3741 $this->_where
[$grouping][] = " ( $field <= '$val' ) ";
3742 $this->_qill
[$grouping][] = ts('Postal code less than or equal to \'%1\'', array(1 => $value));
3747 * Where / qill clause for location type.
3749 * @param array $values
3750 * @param null $status
3754 public function locationType(&$values, $status = NULL) {
3755 list($name, $op, $value, $grouping, $wildcard) = $values;
3757 if (is_array($value)) {
3758 $this->_where
[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')';
3759 $this->_tables
['civicrm_address'] = 1;
3760 $this->_whereTables
['civicrm_address'] = 1;
3762 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
3764 foreach ($value as $id) {
3765 $names[] = $locationType[$id];
3768 $this->_primaryLocation
= FALSE;
3771 $this->_qill
[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3774 return implode(' ' . ts('or') . ' ', $names);
3781 * @param bool $fromStateProvince
3783 * @return array|NULL
3785 public function country(&$values, $fromStateProvince = TRUE) {
3786 list($name, $op, $value, $grouping, $wildcard) = $values;
3788 if (!$fromStateProvince) {
3789 $stateValues = $this->getWhereValues('state_province', $grouping);
3790 if (!empty($stateValues)) {
3791 // return back to caller if there are state province values
3792 // since that handles this case
3797 $countryClause = $countryQill = NULL;
3798 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ||
($values && !empty($value))) {
3799 $this->_tables
['civicrm_address'] = 1;
3800 $this->_whereTables
['civicrm_address'] = 1;
3802 $countryClause = self
::buildClause('civicrm_address.country_id', $op, $value, 'Positive');
3803 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, 'country_id', $value, $op);
3804 $countryQill = ts("%1 %2 %3", array(1 => 'Country', 2 => $qillop, 3 => $qillVal));
3806 if (!$fromStateProvince) {
3807 $this->_where
[$grouping][] = $countryClause;
3808 $this->_qill
[$grouping][] = $countryQill;
3812 if ($fromStateProvince) {
3813 if (!empty($countryClause)) {
3816 " ...AND... " . $countryQill,
3820 return array(NULL, NULL);
3826 * Where / qill clause for county (if present).
3828 * @param array $values
3829 * @param null $status
3833 public function county(&$values, $status = NULL) {
3834 list($name, $op, $value, $grouping, $wildcard) = $values;
3836 if (!is_array($value)) {
3837 // force the county to be an array
3838 $value = array($value);
3841 // check if the values are ids OR names of the counties
3842 $inputFormat = 'id';
3843 foreach ($value as $v) {
3844 if (!is_numeric($v)) {
3845 $inputFormat = 'name';
3853 elseif ($op == '!=') {
3857 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3858 $op = str_replace('EMPTY', 'NULL', $op);
3861 if (in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
3862 $clause = "civicrm_address.county_id $op";
3864 elseif ($inputFormat == 'id') {
3865 $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')';
3867 $county = CRM_Core_PseudoConstant
::county();
3868 foreach ($value as $id) {
3869 $names[] = CRM_Utils_Array
::value($id, $county);
3873 $inputClause = array();
3874 $county = CRM_Core_PseudoConstant
::county();
3875 foreach ($value as $name) {
3876 $name = trim($name);
3877 $inputClause[] = CRM_Utils_Array
::key($name, $county);
3879 $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')';
3882 $this->_tables
['civicrm_address'] = 1;
3883 $this->_whereTables
['civicrm_address'] = 1;
3885 $this->_where
[$grouping][] = $clause;
3887 $this->_qill
[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3890 return implode(' ' . ts('or') . ' ', $names);
3895 * Where / qill clause for state/province AND country (if present).
3897 * @param array $values
3898 * @param null $status
3902 public function stateProvince(&$values, $status = NULL) {
3903 list($name, $op, $value, $grouping, $wildcard) = $values;
3905 $stateClause = self
::buildClause('civicrm_address.state_province_id', $op, $value, 'Positive');
3906 $this->_tables
['civicrm_address'] = 1;
3907 $this->_whereTables
['civicrm_address'] = 1;
3909 $countryValues = $this->getWhereValues('country', $grouping);
3910 list($countryClause, $countryQill) = $this->country($countryValues, TRUE);
3911 if ($countryClause) {
3912 $clause = "( $stateClause AND $countryClause )";
3915 $clause = $stateClause;
3918 $this->_where
[$grouping][] = $clause;
3919 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
3921 $this->_qill
[$grouping][] = ts("State/Province %1 %2 %3", array(1 => $qillop, 2 => $qillVal, 3 => $countryQill));
3924 return implode(' ' . ts('or') . ' ', $qillVal) . $countryQill;
3929 * Where / qill clause for change log.
3931 * @param array $values
3933 public function changeLog(&$values) {
3934 list($name, $op, $value, $grouping, $wildcard) = $values;
3936 $targetName = $this->getWhereValues('changed_by', $grouping);
3941 $name = trim($targetName[2]);
3942 $name = CRM_Core_DAO
::escapeString($name);
3943 $name = $targetName[4] ?
"%$name%" : $name;
3944 $this->_where
[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
3945 $this->_tables
['civicrm_log'] = $this->_whereTables
['civicrm_log'] = 1;
3946 $fieldTitle = ts('Added By');
3947 foreach ($this->_params
as $params) {
3948 if ($params[0] == 'log_date') {
3949 if ($params[2] == 2) {
3950 $fieldTitle = ts('Modified By');
3955 list($qillop, $qillVal) = self
::buildQillForFieldValue(NULL, 'changed_by', $name, 'LIKE');
3956 $this->_qill
[$grouping][] = ts("%1 %2 '%3'", [
3966 public function modifiedDates($values) {
3967 $this->_useDistinct
= TRUE;
3969 // CRM-11281, default to added date if not set
3970 $fieldTitle = ts('Added Date');
3971 $fieldName = 'created_date';
3972 foreach (array_keys($this->_params
) as $id) {
3973 if ($this->_params
[$id][0] == 'log_date') {
3974 if ($this->_params
[$id][2] == 2) {
3975 $fieldTitle = ts('Modified Date');
3976 $fieldName = 'modified_date';
3981 $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle);
3983 self
::$_openedPanes[ts('Change Log')] = TRUE;
3989 public function demographics(&$values) {
3990 list($name, $op, $value, $grouping, $wildcard) = $values;
3992 if (($name == 'age_low') ||
($name == 'age_high')) {
3993 $this->ageRangeQueryBuilder($values,
3994 'contact_a', 'age', 'birth_date', ts('Age')
3997 elseif (($name == 'birth_date_low') ||
($name == 'birth_date_high')) {
3999 $this->dateQueryBuilder($values,
4000 'contact_a', 'birth_date', 'birth_date', ts('Birth Date')
4003 elseif (($name == 'deceased_date_low') ||
($name == 'deceased_date_high')) {
4005 $this->dateQueryBuilder($values,
4006 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date')
4010 self
::$_openedPanes[ts('Demographics')] = TRUE;
4016 public function privacy(&$values) {
4017 list($name, $op, $value, $grouping) = $values;
4018 //fixed for profile search listing CRM-4633
4019 if (strpbrk($value, "[")) {
4020 $value = "'{$value}'";
4022 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
4025 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
4027 $field = CRM_Utils_Array
::value($name, $this->_fields
);
4028 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
4029 $title = $field ?
$field['title'] : $name;
4030 $this->_qill
[$grouping][] = "$title $op $value";
4036 public function privacyOptions($values) {
4037 list($name, $op, $value, $grouping, $wildcard) = $values;
4039 if (empty($value) ||
!is_array($value)) {
4043 // get the operator and toggle values
4044 $opValues = $this->getWhereValues('privacy_operator', $grouping);
4047 strtolower($opValues[2] == 'AND')
4049 // @todo this line is logially unreachable
4053 $toggleValues = $this->getWhereValues('privacy_toggle', $grouping);
4055 if ($toggleValues &&
4056 $toggleValues[2] == 2
4063 foreach ($value as $dontCare => $pOption) {
4064 $clauses[] = " ( contact_a.{$pOption} = 1 ) ";
4065 $field = CRM_Utils_Array
::value($pOption, $this->_fields
);
4066 $title = $field ?
$field['title'] : $pOption;
4067 $qill[] = " $title = 1 ";
4070 $this->_where
[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )';
4071 $this->_qill
[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )';
4077 public function preferredCommunication(&$values) {
4078 list($name, $op, $value, $grouping, $wildcard) = $values;
4080 if (!is_array($value)) {
4081 $value = str_replace(array('(', ')'), '', explode(",", $value));
4083 elseif (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
4085 $value = $value[$op];
4087 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
4089 if (self
::caseImportant($op)) {
4090 $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value);
4091 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
4092 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
4095 $this->_where
[$grouping][] = self
::buildClause("contact_a.preferred_communication_method", $op, $value);
4096 $this->_qill
[$grouping][] = ts('Preferred Communication Method %1 %2', array(1 => $qillop, 2 => $qillVal));
4100 * Where / qill clause for relationship.
4102 * @param array $values
4104 public function relationship(&$values) {
4105 list($name, $op, $value, $grouping, $wildcard) = $values;
4106 if ($this->_relationshipValuesAdded
) {
4109 // also get values array for relation_target_name
4110 // for relationship search we always do wildcard
4111 $relationType = $this->getWhereValues('relation_type_id', $grouping);
4112 $targetName = $this->getWhereValues('relation_target_name', $grouping);
4113 $relStatus = $this->getWhereValues('relation_status', $grouping);
4114 $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
4116 $nameClause = $name = NULL;
4118 $name = trim($targetName[2]);
4119 if (substr($name, 0, 1) == '"' &&
4120 substr($name, -1, 1) == '"'
4122 $name = substr($name, 1, -1);
4123 $name = CRM_Core_DAO
::escapeString($name);
4124 $nameClause = "= '$name'";
4127 $name = CRM_Core_DAO
::escapeString($name);
4128 $nameClause = "LIKE '%{$name}%'";
4132 $relTypes = $relTypesIds = array();
4133 if (!empty($relationType)) {
4134 $relationType[2] = (array) $relationType[2];
4135 foreach ($relationType[2] as $relType) {
4136 $rel = explode('_', $relType);
4137 self
::$_relType = $rel[1];
4138 $params = array('id' => $rel[0]);
4139 $typeValues = array();
4140 $rTypeValue = CRM_Contact_BAO_RelationshipType
::retrieve($params, $typeValues);
4141 if (!empty($rTypeValue)) {
4142 if ($rTypeValue->name_a_b
== $rTypeValue->name_b_a
) {
4143 // if we don't know which end of the relationship we are dealing with we'll create a temp table
4144 self
::$_relType = 'reciprocal';
4146 $relTypesIds[] = $rel[0];
4147 $relTypes[] = $relType;
4152 // if we are creating a temp table we build our own where for the relationship table
4153 $relationshipTempTable = NULL;
4154 if (self
::$_relType == 'reciprocal') {
4156 self
::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO
::createTempTableName('civicrm_rel');
4158 $where[$grouping][] = " sort_name $nameClause ";
4160 $groupJoinTable = "civicrm_relationship";
4161 $groupJoinColumn = "contact_id_alt";
4164 $where = &$this->_where
;
4166 $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )";
4168 $groupJoinTable = "contact_b";
4169 $groupJoinColumn = "id";
4171 $allRelationshipType = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
4172 if ($nameClause ||
!$targetGroup) {
4173 if (!empty($relationType)) {
4175 foreach ($relTypes as $rel) {
4176 if (!empty($relQill)) {
4179 $relQill .= $allRelationshipType[$rel];
4181 $this->_qill
[$grouping][] = 'Relationship Type(s) ' . $relQill . " $name";
4184 $this->_qill
[$grouping][] = $name;
4188 //check to see if the target contact is in specified group
4190 //add contacts from static groups
4191 $this->_tables
['civicrm_relationship_group_contact'] = $this->_whereTables
['civicrm_relationship_group_contact']
4192 = " LEFT JOIN civicrm_group_contact civicrm_relationship_group_contact ON civicrm_relationship_group_contact.contact_id = {$groupJoinTable}.{$groupJoinColumn} AND civicrm_relationship_group_contact.status = 'Added'";
4193 $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" .
4194 implode(",", $targetGroup[2]) . ") ) ";
4196 //add contacts from saved searches
4197 $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", $groupJoinTable, $op, $groupJoinColumn);
4199 //set the group where clause
4201 $groupWhere[] = "( " . $ssWhere . " )";
4203 $this->_where
[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
4205 //Get the names of the target groups for the qill
4206 $groupNames = CRM_Core_PseudoConstant
::group();
4207 $qillNames = array();
4208 foreach ($targetGroup[2] as $groupId) {
4209 if (array_key_exists($groupId, $groupNames)) {
4210 $qillNames[] = $groupNames[$groupId];
4213 if (!empty($relationType)) {
4215 foreach ($relTypes as $rel) {
4216 if (!empty($relQill)) {
4219 $relQill .= CRM_Utils_Array
::value($rel, $allRelationshipType);
4221 $this->_qill
[$grouping][] = 'Relationship Type(s) ' . $relQill . " ( " . implode(", ", $qillNames) . " )";
4224 $this->_qill
[$grouping][] = implode(", ", $qillNames);
4228 // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
4229 $today = date('Ymd');
4230 //check for active, inactive and all relation status
4231 if ($relStatus[2] == 0) {
4232 $where[$grouping][] = "(
4233 civicrm_relationship.is_active = 1 AND
4234 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
4235 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
4237 $this->_qill
[$grouping][] = ts('Relationship - Active and Current');
4239 elseif ($relStatus[2] == 1) {
4240 $where[$grouping][] = "(
4241 civicrm_relationship.is_active = 0 OR
4242 civicrm_relationship.end_date < {$today} OR
4243 civicrm_relationship.start_date > {$today}
4245 $this->_qill
[$grouping][] = ts('Relationship - Inactive or not Current');
4249 if (in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
)) {
4252 $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
4254 $this->addRelationshipPermissionClauses($grouping, $where);
4255 $this->addRelationshipDateClauses($grouping, $where);
4256 $this->addRelationshipActivePeriodClauses($grouping, $where);
4257 if (!empty($relTypes)) {
4258 $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')';
4260 $this->_tables
['civicrm_relationship'] = $this->_whereTables
['civicrm_relationship'] = 1;
4261 $this->_useDistinct
= TRUE;
4262 $this->_relationshipValuesAdded
= TRUE;
4263 // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
4265 if ($relationshipTempTable) {
4267 if (!empty($where[$grouping])) {
4268 $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
4269 $whereClause = str_replace('contact_b', 'c', $whereClause);
4272 CREATE TEMPORARY TABLE {$relationshipTempTable}
4274 `contact_id` int(10) unsigned NOT NULL DEFAULT '0',
4275 `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0',
4276 KEY `contact_id` (`contact_id`),
4277 KEY `contact_id_alt` (`contact_id_alt`)
4279 (SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id
4280 FROM civicrm_relationship
4281 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
4284 (SELECT contact_id_a as contact_id, contact_id_b as contact_id_alt, civicrm_relationship.id
4285 FROM civicrm_relationship
4286 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id
4289 CRM_Core_DAO
::executeQuery($sql);
4294 * Add relationship permission criteria to where clause.
4296 * @param string $grouping
4297 * @param array $where Array to add "where" criteria to, in case you are generating a temp table.
4298 * Not the main query.
4300 public function addRelationshipPermissionClauses($grouping, &$where) {
4301 $relPermission = $this->getWhereValues('relation_permission', $grouping);
4302 if ($relPermission) {
4303 if (!is_array($relPermission[2])) {
4304 // this form value was scalar in previous versions of Civi
4305 $relPermission[2] = array($relPermission[2]);
4307 $where[$grouping][] = "(civicrm_relationship.is_permission_a_b IN (" . implode(",", $relPermission[2]) . "))";
4309 $allRelationshipPermissions = CRM_Contact_BAO_Relationship
::buildOptions('is_permission_a_b');
4311 $relPermNames = array_intersect_key($allRelationshipPermissions, array_flip($relPermission[2]));
4312 $this->_qill
[$grouping][] = ts('Permissioned Relationships') . ' - ' . implode(' OR ', $relPermNames);
4317 * Add start & end date criteria in
4318 * @param string $grouping
4319 * @param array $where
4320 * = array to add where clauses to, in case you are generating a temp table.
4321 * not the main query.
4323 public function addRelationshipDateClauses($grouping, &$where) {
4324 $dateValues = array();
4330 foreach ($dateTypes as $dateField) {
4331 $dateValueLow = $this->getWhereValues('relation_' . $dateField . '_low', $grouping);
4332 $dateValueHigh = $this->getWhereValues('relation_' . $dateField . '_high', $grouping);
4333 if (!empty($dateValueLow)) {
4334 $date = date('Ymd', strtotime($dateValueLow[2]));
4335 $where[$grouping][] = "civicrm_relationship.$dateField >= $date";
4336 $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);
4338 if (!empty($dateValueHigh)) {
4339 $date = date('Ymd', strtotime($dateValueHigh[2]));
4340 $where[$grouping][] = "civicrm_relationship.$dateField <= $date";
4341 $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);
4347 * Add start & end active period criteria in
4348 * @param string $grouping
4349 * @param array $where
4350 * = array to add where clauses to, in case you are generating a temp table.
4351 * not the main query.
4353 public function addRelationshipActivePeriodClauses($grouping, &$where) {
4354 $dateValues = array();
4355 $dateField = 'active_period_date';
4357 $dateValueLow = $this->getWhereValues('relation_active_period_date_low', $grouping);
4358 $dateValueHigh = $this->getWhereValues('relation_active_period_date_high', $grouping);
4359 $dateValueLowFormated = $dateValueHighFormated = NULL;
4360 if (!empty($dateValueLow) && !empty($dateValueHigh)) {
4361 $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2]));
4362 $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2]));
4363 $this->_qill
[$grouping][] = (ts('Relationship was active between')) . " " . CRM_Utils_Date
::customFormat($dateValueLowFormated) . " and " . CRM_Utils_Date
::customFormat($dateValueHighFormated);
4365 elseif (!empty($dateValueLow)) {
4366 $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2]));
4367 $this->_qill
[$grouping][] = (ts('Relationship was active after')) . " " . CRM_Utils_Date
::customFormat($dateValueLowFormated);
4369 elseif (!empty($dateValueHigh)) {
4370 $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2]));
4371 $this->_qill
[$grouping][] = (ts('Relationship was active before')) . " " . CRM_Utils_Date
::customFormat($dateValueHighFormated);
4374 if ($activePeriodClauses = self
::getRelationshipActivePeriodClauses($dateValueLowFormated, $dateValueHighFormated, TRUE)) {
4375 $where[$grouping][] = $activePeriodClauses;
4380 * Get start & end active period criteria
4382 public static function getRelationshipActivePeriodClauses($from, $to, $forceTableName) {
4383 $tableName = $forceTableName ?
'civicrm_relationship.' : '';
4384 if (!is_null($from) && !is_null($to)) {
4385 return '(((' . $tableName . 'start_date >= ' . $from . ' AND ' . $tableName . 'start_date <= ' . $to . ') OR
4386 (' . $tableName . 'end_date >= ' . $from . ' AND ' . $tableName . 'end_date <= ' . $to . ') OR
4387 (' . $tableName . 'start_date <= ' . $from . ' AND ' . $tableName . 'end_date >= ' . $to . ' )) OR
4388 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4389 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . ') OR
4390 (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))';
4392 elseif (!is_null($from)) {
4393 return '((' . $tableName . 'start_date >= ' . $from . ') OR
4394 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4395 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . '))';
4397 elseif (!is_null($to)) {
4398 return '((' . $tableName . 'start_date <= ' . $to . ') OR
4399 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4400 (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))';
4405 * Default set of return properties.
4410 * derault return properties
4412 public static function &defaultReturnProperties($mode = 1) {
4413 if (!isset(self
::$_defaultReturnProperties)) {
4414 self
::$_defaultReturnProperties = array();
4417 if (!isset(self
::$_defaultReturnProperties[$mode])) {
4418 // add activity return properties
4419 if ($mode & CRM_Contact_BAO_Query
::MODE_ACTIVITY
) {
4420 self
::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query
::defaultReturnProperties($mode, FALSE);
4423 self
::$_defaultReturnProperties[$mode] = CRM_Core_Component
::defaultReturnProperties($mode, FALSE);
4426 if (empty(self
::$_defaultReturnProperties[$mode])) {
4427 self
::$_defaultReturnProperties[$mode] = array(
4430 'legal_identifier' => 1,
4431 'external_identifier' => 1,
4432 'contact_type' => 1,
4433 'contact_sub_type' => 1,
4435 'display_name' => 1,
4436 'preferred_mail_format' => 1,
4443 'formal_title' => 1,
4444 'communication_style_id' => 1,
4447 'street_address' => 1,
4448 'supplemental_address_1' => 1,
4449 'supplemental_address_2' => 1,
4450 'supplemental_address_3' => 1,
4453 'postal_code_suffix' => 1,
4454 'state_province' => 1,
4456 'world_region' => 1,
4463 'household_name' => 1,
4464 'organization_name' => 1,
4465 'deceased_date' => 1,
4470 'current_employer' => 1,
4471 // FIXME: should we use defaultHierReturnProperties() for the below?
4472 'do_not_email' => 1,
4475 'do_not_phone' => 1,
4476 'do_not_trade' => 1,
4478 'contact_is_deleted' => 1,
4479 'preferred_communication_method' => 1,
4480 'preferred_language' => 1,
4484 return self
::$_defaultReturnProperties[$mode];
4488 * Get primary condition for a sql clause.
4492 * @return string|NULL
4494 public static function getPrimaryCondition($value) {
4495 if (is_numeric($value)) {
4496 $value = (int ) $value;
4497 return ($value == 1) ?
'is_primary = 1' : 'is_primary = 0';
4503 * Wrapper for a simple search query.
4505 * @param array $params
4506 * @param array $returnProperties
4507 * @param bool $count
4511 public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) {
4512 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
4513 list($select, $from, $where, $having) = $query->query();
4514 $groupBy = ($query->_useGroupBy
) ?
'GROUP BY contact_a.id' : '';
4516 $query = "$select $from $where $groupBy $having";
4521 * These are stub comments as this function needs more explanation - particularly in terms of how it
4522 * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.
4524 * This function was originally written as a wrapper for the api query but is called from multiple places
4525 * in the core code directly so the name is misleading. This function does not use the searchQuery function
4526 * but it is unclear as to whehter that is historical or there is a reason
4527 * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function
4529 * @param array $params
4530 * @param array $returnProperties
4531 * @param null $fields
4532 * @param string $sort
4533 * @param int $offset
4534 * @param int $row_count
4535 * @param bool $smartGroupCache
4536 * ?? update smart group cache?.
4537 * @param bool $count
4538 * Return count obnly.
4539 * @param bool $skipPermissions
4540 * Should permissions be ignored or should the logged in user's permissions be applied.
4542 * This basically correlates to the component.
4543 * @param string $apiEntity
4544 * The api entity being called.
4545 * This sort-of duplicates $mode in a confusing way. Probably not by design.
4547 * @param bool|null $primaryLocationOnly
4550 public static function apiQuery(
4552 $returnProperties = NULL,
4557 $smartGroupCache = TRUE,
4559 $skipPermissions = TRUE,
4560 $mode = CRM_Contact_BAO_Query
::MODE_CONTACTS
,
4562 $primaryLocationOnly = NULL
4565 $query = new CRM_Contact_BAO_Query(
4566 $params, $returnProperties,
4567 NULL, TRUE, FALSE, $mode,
4569 TRUE, $smartGroupCache,
4571 $apiEntity, $primaryLocationOnly
4574 //this should add a check for view deleted if permissions are enabled
4575 if ($skipPermissions) {
4576 $query->_skipDeleteClause
= TRUE;
4578 $query->generatePermissionClause(FALSE, $count);
4580 // note : this modifies _fromClause and _simpleFromClause
4581 $query->includePseudoFieldsJoin($sort);
4583 list($select, $from, $where, $having) = $query->query($count);
4585 $options = $query->_options
;
4586 if (!empty($query->_permissionWhereClause
)) {
4587 if (empty($where)) {
4588 $where = "WHERE $query->_permissionWhereClause";
4591 $where = "$where AND $query->_permissionWhereClause";
4595 $sql = "$select $from $where $having";
4597 // add group by only when API action is not getcount
4598 // otherwise query fetches incorrect count
4599 if ($query->_useGroupBy
&& !$count) {
4600 $sql .= self
::getGroupByFromSelectColumns($query->_select
, 'contact_a.id');
4602 if (!empty($sort)) {
4603 $sort = CRM_Utils_Type
::escape($sort, 'String');
4604 $sql .= " ORDER BY $sort ";
4606 if ($row_count > 0 && $offset >= 0) {
4607 $offset = CRM_Utils_Type
::escape($offset, 'Int');
4608 $row_count = CRM_Utils_Type
::escape($row_count, 'Int');
4609 $sql .= " LIMIT $offset, $row_count ";
4612 $dao = CRM_Core_DAO
::executeQuery($sql);
4614 // @todo derive this from the component class rather than hard-code two options.
4615 $entityIDField = ($mode == CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
) ?
'contribution_id' : 'contact_id';
4618 while ($dao->fetch()) {
4620 $noRows = $dao->rowCount
;
4621 return array($noRows, NULL);
4623 $val = $query->store($dao);
4624 $convertedVals = $query->convertToPseudoNames($dao, TRUE, TRUE);
4626 if (!empty($convertedVals)) {
4627 $val = array_replace_recursive($val, $convertedVals);
4629 $values[$dao->$entityIDField] = $val;
4631 return array($values, $options);
4635 * Get the actual custom field name by stripping off the appended string.
4637 * The string could be _relative, _from, or _to
4639 * @todo use metadata rather than convention to do this.
4641 * @param string $parameterName
4642 * The name of the parameter submitted to the form.
4649 public static function getCustomFieldName($parameterName) {
4650 if (substr($parameterName, -5, 5) == '_from') {
4651 return substr($parameterName, 0, strpos($parameterName, '_from'));
4653 if (substr($parameterName, -9, 9) == '_relative') {
4654 return substr($parameterName, 0, strpos($parameterName, '_relative'));
4656 if (substr($parameterName, -3, 3) == '_to') {
4657 return substr($parameterName, 0, strpos($parameterName, '_to'));
4662 * Convert submitted values for relative custom fields to query object format.
4664 * The query will support the sqlOperator format so convert to that format.
4666 * @param array $formValues
4668 * @param array $params
4669 * Converted parameters for the query object.
4670 * @param string $values
4672 * @param string $fieldName
4673 * Submitted field name. (Matches form field not DB field.)
4675 protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) {
4676 if (empty($values)) {
4677 // e.g we might have relative set & from & to empty. The form flow is a bit funky &
4678 // this function gets called again after they fields have been converted which can get ugly.
4681 $customFieldName = self
::getCustomFieldName($fieldName);
4683 if (substr($fieldName, -9, 9) == '_relative') {
4684 list($from, $to) = CRM_Utils_Date
::getFromTo($values, NULL, NULL);
4687 if ($fieldName == $customFieldName . '_to' && CRM_Utils_Array
::value($customFieldName . '_from', $formValues)) {
4688 // Both to & from are set. We only need to acton one, choosing from.
4692 $from = CRM_Utils_Array
::value($customFieldName . '_from', $formValues, NULL);
4693 $to = CRM_Utils_Array
::value($customFieldName . '_to', $formValues, NULL);
4695 if (self
::isCustomDateField($customFieldName)) {
4696 list($from, $to) = CRM_Utils_Date
::getFromTo(NULL, $from, $to);
4702 $relativeFunction = array('BETWEEN' => array($from, $to));
4705 $relativeFunction = array('>=' => $from);
4709 $relativeFunction = array('<=' => $to);
4721 * Are we dealing with custom field of type date.
4727 public static function isCustomDateField($fieldName) {
4728 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) == FALSE) {
4731 if ('Date' == civicrm_api3('CustomField', 'getvalue', array('id' => $customFieldID, 'return' => 'data_type'))) {
4738 * Has this field already been reformatting to Query object syntax.
4740 * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems
4741 * to come with associated double queries & is possibly damaging performance.
4743 * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns
4746 * @param mixed $values
4747 * Value in formValues for the field.
4751 public static function isAlreadyProcessedForQueryFormat($values) {
4752 if (!is_array($values)) {
4755 if (($operator = CRM_Utils_Array
::value(1, $values)) == FALSE) {
4758 return in_array($operator, CRM_Core_DAO
::acceptedSQLOperators());
4762 * If the state and country are passed remove state.
4764 * Country is implicit from the state, but including both results in
4765 * a poor query as there is no combined index on state AND country.
4769 * @param array $formValues
4771 public static function filterCountryFromValuesIfStateExists(&$formValues) {
4772 if (!empty($formValues['country']) && !empty($formValues['state_province'])) {
4773 // The use of array map sanitises the data by ensuring we are dealing with integers.
4774 $states = implode(', ', array_map('intval', $formValues['state_province']));
4775 $countryList = CRM_Core_DAO
::singleValueQuery(
4776 "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE id IN ($states)"
4778 if ($countryList == $formValues['country']) {
4779 unset($formValues['country']);
4785 * For some special cases, grouping by subset of select fields becomes mandatory.
4786 * Hence, full_group_by mode is handled by appending any_value
4787 * keyword to select fields not present in groupBy
4789 * @param array $selectClauses
4790 * @param array $groupBy - Columns already included in GROUP By clause.
4791 * @param string $aggregateFunction
4795 public static function appendAnyValueToSelect($selectClauses, $groupBy, $aggregateFunction = 'ANY_VALUE') {
4796 if (!CRM_Utils_SQL
::disableFullGroupByMode()) {
4797 $groupBy = array_map('trim', (array) $groupBy);
4798 $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i';
4799 foreach ($selectClauses as $key => &$val) {
4800 list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
4801 // append ANY_VALUE() keyword
4802 if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
4803 $val = ($aggregateFunction == 'GROUP_CONCAT') ?
4804 str_replace($selectColumn, "$aggregateFunction(DISTINCT {$selectColumn})", $val) :
4805 str_replace($selectColumn, "$aggregateFunction({$selectColumn})", $val);
4810 return "SELECT " . implode(', ', $selectClauses) . " ";
4814 * For some special cases, where if non-aggregate ORDER BY columns are not present in GROUP BY
4815 * on full_group_by mode, then append the those missing columns to GROUP BY clause
4816 * keyword to select fields not present in groupBy
4818 * @param string $groupBy - GROUP BY clause where missing ORDER BY columns will be appended if not present
4819 * @param array $orderBys - ORDER BY sub-clauses
4822 public static function getGroupByFromOrderBy(&$groupBy, $orderBys) {
4823 if (!CRM_Utils_SQL
::disableFullGroupByMode()) {
4824 foreach ($orderBys as $orderBy) {
4825 $orderBy = str_ireplace(array(' DESC', ' ASC', '`'), '', $orderBy); // remove sort syntax from ORDER BY clauses if present
4826 // if ORDER BY column is not present in GROUP BY then append it to end
4827 if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1 && !strstr($groupBy, $orderBy)) {
4828 $groupBy .= ", {$orderBy}";
4835 * Include Select columns in groupBy clause.
4837 * @param array $selectClauses
4838 * @param array $groupBy - Columns already included in GROUP By clause.
4842 public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
4843 $groupBy = (array) $groupBy;
4844 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
4845 $sqlMode = CRM_Core_DAO
::singleValueQuery('SELECT @@sql_mode');
4847 //return if ONLY_FULL_GROUP_BY is not enabled.
4848 if (CRM_Utils_SQL
::supportsFullGroupBy() && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) {
4849 $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i';
4850 foreach ($selectClauses as $key => $val) {
4851 $aliasArray = preg_split('/ as /i', $val);
4852 // if more than 1 alias we need to split by ','.
4853 if (count($aliasArray) > 2) {
4854 $aliasArray = preg_split('/,/', $val);
4855 foreach ($aliasArray as $key => $value) {
4856 $alias = current(preg_split('/ as /i', $value));
4857 if (!in_array($alias, $groupBy) && preg_match($regexToExclude, trim($alias)) !== 1) {
4858 $groupBy[] = $alias;
4863 list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
4864 $dateRegex = '/^(DATE_FORMAT|DATE_ADD|CASE)/i';
4865 $tableName = current(explode('.', $selectColumn));
4866 $primaryKey = "{$tableName}.id";
4867 // exclude columns which are already included in groupBy and aggregate functions from select
4868 // CRM-18439 - Also exclude the columns which are functionally dependent on columns in $groupBy (MySQL 5.7+)
4869 if (!in_array($selectColumn, $groupBy) && !in_array($primaryKey, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
4870 if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
4871 $groupBy[] = $alias;
4874 $groupBy[] = $selectColumn;
4881 if (!empty($groupBy)) {
4882 return " GROUP BY " . implode(', ', $groupBy);
4888 * Create and query the db for an contact search.
4890 * @param int $offset
4891 * The offset for the query.
4892 * @param int $rowCount
4893 * The number of rows to return.
4894 * @param string|CRM_Utils_Sort $sort
4895 * The order by string.
4896 * @param bool $count
4897 * Is this a count only query ?.
4898 * @param bool $includeContactIds
4899 * Should we include contact ids?.
4900 * @param bool $sortByChar
4901 * If true returns the distinct array of first characters for search results.
4902 * @param bool $groupContacts
4903 * If true, return only the contact ids.
4904 * @param bool $returnQuery
4905 * Should we return the query as a string.
4906 * @param string $additionalWhereClause
4907 * If the caller wants to further restrict the search (used for components).
4908 * @param null $sortOrder
4909 * @param string $additionalFromClause
4910 * Should be clause with proper joins, effective to reduce where clause load.
4912 * @param bool $skipOrderAndLimit
4914 * @return CRM_Core_DAO
4916 public function searchQuery(
4917 $offset = 0, $rowCount = 0, $sort = NULL,
4918 $count = FALSE, $includeContactIds = FALSE,
4919 $sortByChar = FALSE, $groupContacts = FALSE,
4920 $returnQuery = FALSE,
4921 $additionalWhereClause = NULL, $sortOrder = NULL,
4922 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE
4925 $query = $this->getSearchSQL($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause, $skipOrderAndLimit);
4931 return CRM_Core_DAO
::singleValueQuery($query);
4934 $dao = CRM_Core_DAO
::executeQuery($query);
4936 // We can always call this - it will only re-enable if it was originally enabled.
4937 CRM_Core_DAO
::reenableFullGroupByMode();
4939 if ($groupContacts) {
4941 while ($dao->fetch()) {
4944 return implode(',', $ids);
4951 * Create and query the db for a contact search.
4953 * @return CRM_Core_DAO
4955 public function alphabetQuery() {
4956 $query = $this->getSearchSQL(NULL, NULL, NULL, FALSE, FALSE, TRUE);
4958 $dao = CRM_Core_DAO
::executeQuery($query);
4960 // We can always call this - it will only re-enable if it was originally enabled.
4961 CRM_Core_DAO
::reenableFullGroupByMode();
4967 * Fetch a list of contacts for displaying a search results page
4969 * @param array $cids
4970 * List of contact IDs
4971 * @param bool $includeContactIds
4972 * @return CRM_Core_DAO
4974 public function getCachedContacts($cids, $includeContactIds) {
4975 CRM_Utils_Type
::validateAll($cids, 'Positive');
4976 $this->_includeContactIds
= $includeContactIds;
4977 $onlyDeleted = in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params
);
4978 list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
4979 $select .= sprintf(", (%s) AS _wgt", $this->createSqlCase('contact_a.id', $cids));
4980 $where .= sprintf(' AND contact_a.id IN (%s)', implode(',', $cids));
4981 $order = 'ORDER BY _wgt';
4982 $groupBy = $this->_useGroupBy ?
' GROUP BY contact_a.id' : '';
4984 $query = "$select $from $where $groupBy $order $limit";
4986 return CRM_Core_DAO
::executeQuery($query);
4990 * Construct a SQL CASE expression.
4992 * @param string $idCol
4993 * The name of a column with ID's (eg 'contact_a.id').
4994 * @param array $cids
4995 * Array(int $weight => int $id).
4997 * CASE WHEN id=123 THEN 1 WHEN id=456 THEN 2 END
4999 private function createSqlCase($idCol, $cids) {
5001 foreach ($cids as $weight => $cid) {
5002 $buf .= " WHEN $idCol = $cid THEN $weight \n";
5009 * Populate $this->_permissionWhereClause with permission related clause and update other
5010 * query related properties.
5012 * Function calls ACL permission class and hooks to filter the query appropriately
5014 * Note that these 2 params were in the code when extracted from another function
5015 * and a second round extraction would be to make them properties of the class
5017 * @param bool $onlyDeleted
5018 * Only get deleted contacts.
5019 * @param bool $count
5020 * Return Count only.
5022 public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) {
5023 if (!$this->_skipPermission
) {
5024 $this->_permissionWhereClause
= CRM_ACL_API
::whereClause(
5025 CRM_Core_Permission
::VIEW
,
5027 $this->_whereTables
,
5030 $this->_skipDeleteClause
5033 // regenerate fromClause since permission might have added tables
5034 if ($this->_permissionWhereClause
) {
5035 //fix for row count in qill (in contribute/membership find)
5037 $this->_useDistinct
= TRUE;
5040 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
5041 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
5042 // note : this modifies _fromClause and _simpleFromClause
5043 $this->includePseudoFieldsJoin($this->_sort
);
5047 // add delete clause if needed even if we are skipping permission
5049 if (!$this->_skipDeleteClause
) {
5050 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
5051 $this->_permissionWhereClause
= '(contact_a.is_deleted)';
5055 $this->_permissionWhereClause
= '(contact_a.is_deleted = 0)';
5064 public function setSkipPermission($val) {
5065 $this->_skipPermission
= $val;
5069 * @param null $context
5073 public function summaryContribution($context = NULL) {
5074 list($innerselect, $from, $where, $having) = $this->query(TRUE);
5075 if ($this->_permissionWhereClause
) {
5076 $where .= " AND " . $this->_permissionWhereClause
;
5078 if ($context == 'search') {
5079 $where .= " AND contact_a.is_deleted = 0 ";
5082 $this->appendFinancialTypeWhereAndFromToQueryStrings($where, $from);
5084 $summary = ['total' => []];
5085 $this->addBasicStatsToSummary($summary, $where, $from);
5087 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled()) {
5088 $this->addBasicSoftCreditStatsToStats($summary, $where, $from);
5091 $this->addBasicCancelStatsToSummary($summary, $where, $from);
5097 * Append financial ACL limits to the query from & where clauses, if applicable.
5099 * @param string $where
5100 * @param string $from
5102 public function appendFinancialTypeWhereAndFromToQueryStrings(&$where, &$from) {
5103 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
5106 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
5107 if (!empty($financialTypes)) {
5108 $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL";
5109 $from .= " LEFT JOIN civicrm_line_item li
5110 ON civicrm_contribution.id = li.contribution_id AND
5111 li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") ";
5114 $where .= " AND civicrm_contribution.financial_type_id IN (0)";
5119 * Getter for the qill object.
5123 public function qill() {
5124 return $this->_qill
;
5128 * Default set of return default hier return properties.
5132 public static function &defaultHierReturnProperties() {
5133 if (!isset(self
::$_defaultHierReturnProperties)) {
5134 self
::$_defaultHierReturnProperties = array(
5137 'legal_identifier' => 1,
5138 'external_identifier' => 1,
5139 'contact_type' => 1,
5140 'contact_sub_type' => 1,
5142 'display_name' => 1,
5149 'formal_title' => 1,
5150 'communication_style_id' => 1,
5151 'email_greeting' => 1,
5152 'postal_greeting' => 1,
5156 'preferred_communication_method' => 1,
5157 'do_not_phone' => 1,
5158 'do_not_email' => 1,
5161 'do_not_trade' => 1,
5162 'location' => array(
5164 'location_type' => 1,
5165 'street_address' => 1,
5167 'state_province' => 1,
5169 'postal_code_suffix' => 1,
5172 'phone-Mobile' => 1,
5185 'location_type' => 1,
5186 'street_address' => 1,
5188 'state_province' => 1,
5190 'postal_code_suffix' => 1,
5193 'phone-Mobile' => 1,
5207 return self
::$_defaultHierReturnProperties;
5211 * Build query for a date field.
5213 * @param array $values
5214 * @param string $tableName
5215 * @param string $fieldName
5216 * @param string $dbFieldName
5217 * @param string $fieldTitle
5218 * @param bool $appendTimeStamp
5219 * @param string $dateFormat
5221 public function dateQueryBuilder(
5222 &$values, $tableName, $fieldName,
5223 $dbFieldName, $fieldTitle,
5224 $appendTimeStamp = TRUE,
5225 $dateFormat = 'YmdHis'
5227 list($name, $op, $value, $grouping, $wildcard) = $values;
5229 if ($name == "{$fieldName}_low" ||
5230 $name == "{$fieldName}_high"
5232 if (isset($this->_rangeCache
[$fieldName]) ||
!$value) {
5235 $this->_rangeCache
[$fieldName] = 1;
5237 $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL;
5239 if ($name == $fieldName . '_low') {
5241 $firstPhrase = ts('greater than or equal to');
5242 $firstDate = CRM_Utils_Date
::processDate($value, NULL, FALSE, $dateFormat);
5244 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5245 if (!empty($secondValues) && $secondValues[2]) {
5247 $secondPhrase = ts('less than or equal to');
5248 $secondValue = $secondValues[2];
5250 if ($appendTimeStamp && strlen($secondValue) == 10) {
5251 $secondValue .= ' 23:59:59';
5253 $secondDate = CRM_Utils_Date
::processDate($secondValue, NULL, FALSE, $dateFormat);
5256 elseif ($name == $fieldName . '_high') {
5258 $firstPhrase = ts('less than or equal to');
5260 if ($appendTimeStamp && strlen($value) == 10) {
5261 $value .= ' 23:59:59';
5263 $firstDate = CRM_Utils_Date
::processDate($value, NULL, FALSE, $dateFormat);
5265 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5266 if (!empty($secondValues) && $secondValues[2]) {
5268 $secondPhrase = ts('greater than or equal to');
5269 $secondValue = $secondValues[2];
5270 $secondDate = CRM_Utils_Date
::processDate($secondValue, NULL, FALSE, $dateFormat);
5274 if (!$appendTimeStamp) {
5275 $firstDate = substr($firstDate, 0, 8);
5277 $firstDateFormat = CRM_Utils_Date
::customFormat($firstDate);
5280 if (!$appendTimeStamp) {
5281 $secondDate = substr($secondDate, 0, 8);
5283 $secondDateFormat = CRM_Utils_Date
::customFormat($secondDate);
5286 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5288 $this->_where
[$grouping][] = "
5289 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5290 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5292 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\"";
5295 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5296 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
5300 if ($name == $fieldName) {
5301 //In Get API, for operators other then '=' the $value is in array(op => value) format
5302 if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
5304 $value = $value[$op];
5307 $date = $format = NULL;
5308 if (strstr($op, 'IN')) {
5310 foreach ($value as &$date) {
5311 $date = CRM_Utils_Date
::processDate($date, NULL, FALSE, $dateFormat);
5312 if (!$appendTimeStamp) {
5313 $date = substr($date, 0, 8);
5315 $format[] = CRM_Utils_Date
::customFormat($date);
5317 $date = "('" . implode("','", $value) . "')";
5318 $format = implode(', ', $format);
5320 elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) {
5321 $date = CRM_Utils_Date
::processDate($value, NULL, FALSE, $dateFormat);
5322 if (!$appendTimeStamp) {
5323 $date = substr($date, 0, 8);
5325 $format = CRM_Utils_Date
::customFormat($date);
5330 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op $date";
5333 $this->_where
[$grouping][] = self
::buildClause("{$tableName}.{$dbFieldName}", $op);
5336 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5338 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
5339 $this->_qill
[$grouping][] = "$fieldTitle $op $format";
5345 * @param string $tableName
5346 * @param string $fieldName
5347 * @param string $dbFieldName
5348 * @param $fieldTitle
5349 * @param null $options
5351 public function numberRangeBuilder(
5353 $tableName, $fieldName,
5354 $dbFieldName, $fieldTitle,
5357 list($name, $op, $value, $grouping, $wildcard) = $values;
5359 if ($name == "{$fieldName}_low" ||
5360 $name == "{$fieldName}_high"
5362 if (isset($this->_rangeCache
[$fieldName])) {
5365 $this->_rangeCache
[$fieldName] = 1;
5367 $secondOP = $secondPhrase = $secondValue = NULL;
5369 if ($name == "{$fieldName}_low") {
5371 $firstPhrase = ts('greater than');
5373 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5374 if (!empty($secondValues)) {
5376 $secondPhrase = ts('less than');
5377 $secondValue = $secondValues[2];
5382 $firstPhrase = ts('less than');
5384 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5385 if (!empty($secondValues)) {
5387 $secondPhrase = ts('greater than');
5388 $secondValue = $secondValues[2];
5393 $this->_where
[$grouping][] = "
5394 ( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND
5395 ( {$tableName}.{$dbFieldName} $secondOP {$secondValue} )
5397 $displayValue = $options ?
$options[$value] : $value;
5398 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5400 $this->_qill
[$grouping][]
5401 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5404 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}";
5405 $displayValue = $options ?
$options[$value] : $value;
5406 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5408 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5413 if ($name == $fieldName) {
5417 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}";
5419 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5420 $displayValue = $options ?
$options[$value] : $value;
5421 $this->_qill
[$grouping][] = "$fieldTitle - $phrase \"$displayValue\"";
5428 * @param string $tableName
5429 * @param string $fieldName
5430 * @param string $dbFieldName
5431 * @param $fieldTitle
5432 * @param null $options
5434 public function ageRangeQueryBuilder(
5436 $tableName, $fieldName,
5437 $dbFieldName, $fieldTitle,
5440 list($name, $op, $value, $grouping, $wildcard) = $values;
5442 $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping);
5443 $asofDate = NULL; // will be treated as current day
5444 if ($asofDateValues) {
5445 $asofDate = CRM_Utils_Date
::processDate($asofDateValues[2]);
5446 $asofDateFormat = CRM_Utils_Date
::customFormat(substr($asofDate, 0, 8));
5447 $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat;
5450 if ($name == "{$fieldName}_low" ||
5451 $name == "{$fieldName}_high"
5453 if (isset($this->_rangeCache
[$fieldName])) {
5456 $this->_rangeCache
[$fieldName] = 1;
5458 $secondOP = $secondPhrase = $secondValue = NULL;
5460 if ($name == "{$fieldName}_low") {
5461 $firstPhrase = ts('greater than or equal to');
5462 // NB: age > X means date of birth < Y
5464 $firstDate = self
::calcDateFromAge($asofDate, $value, 'min');
5466 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5467 if (!empty($secondValues)) {
5469 $secondPhrase = ts('less than or equal to');
5470 $secondValue = $secondValues[2];
5471 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'max');
5476 $firstPhrase = ts('less than or equal to');
5477 $firstDate = self
::calcDateFromAge($asofDate, $value, 'max');
5479 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5480 if (!empty($secondValues)) {
5482 $secondPhrase = ts('greater than or equal to');
5483 $secondValue = $secondValues[2];
5484 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'min');
5489 $this->_where
[$grouping][] = "
5490 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5491 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5493 $displayValue = $options ?
$options[$value] : $value;
5494 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5496 $this->_qill
[$grouping][]
5497 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5500 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5501 $displayValue = $options ?
$options[$value] : $value;
5502 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5504 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5510 * Calculate date from age.
5512 * @param string $asofDate
5514 * @param string $type
5518 public static function calcDateFromAge($asofDate, $age, $type) {
5519 $date = new DateTime($asofDate);
5520 if ($type == "min") {
5521 // minimum age is $age: dob <= date - age "235959"
5522 $date->sub(new DateInterval("P" . $age . "Y"));
5523 return $date->format('Ymd') . "235959";
5526 // max age is $age: dob >= date - (age + 1y) + 1d "000000"
5527 $date->sub(new DateInterval("P" . ($age +
1) . "Y"))->add(new DateInterval("P1D"));
5528 return $date->format('Ymd') . "000000";
5533 * Given the field name, operator, value & its data type
5534 * builds the where Clause for the query
5535 * used for handling 'IS NULL'/'IS NOT NULL' operators
5537 * @param string $field
5541 * @param string $value
5543 * @param string $dataType
5544 * Data type of the field.
5547 * Where clause for the query.
5549 public static function buildClause($field, $op, $value = NULL, $dataType = NULL) {
5551 $clause = "$field $op";
5559 $clause = ($dataType == 'Date') ?
" $field IS NULL " : " (NULLIF($field, '') IS NULL) ";
5562 case 'IS NOT EMPTY':
5563 $clause = ($dataType == 'Date') ?
" $field IS NOT NULL " : " (NULLIF($field, '') IS NOT NULL) ";
5567 return " {$clause} BINARY '{$value}' ";
5571 // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter.
5572 if (!empty($value) && (!is_array($value) ||
!array_key_exists($op, $value))) {
5573 $value = array($op => (array) $value);
5577 if (empty($dataType) ||
$dataType == 'Date') {
5578 $dataType = 'String';
5580 if (is_array($value)) {
5581 //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more
5582 // widely used and consistent across the codebase
5583 // adding this here won't accept the search functions which don't submit an array
5584 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, $value, $dataType)) != FALSE) {
5586 return $queryString;
5588 if (!empty($value[0]) && $op === 'BETWEEN') {
5589 CRM_Core_Error
::deprecatedFunctionWarning('Fix search input params');
5590 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, array($op => $value), $dataType)) != FALSE) {
5591 return $queryString;
5594 throw new CRM_Core_Exception(ts('Failed to interpret input for search'));
5597 $value = CRM_Utils_Type
::escape($value, $dataType);
5598 // if we don't have a dataType we should assume
5599 if ($dataType == 'String' ||
$dataType == 'Text') {
5600 $value = "'" . $value . "'";
5602 return "$clause $value";
5607 * @param bool $reset
5611 public function openedSearchPanes($reset = FALSE) {
5612 if (!$reset ||
empty($this->_whereTables
)) {
5613 return self
::$_openedPanes;
5616 // pane name to table mapper
5617 $panesMapper = array(
5618 ts('Contributions') => 'civicrm_contribution',
5619 ts('Memberships') => 'civicrm_membership',
5620 ts('Events') => 'civicrm_participant',
5621 ts('Relationships') => 'civicrm_relationship',
5622 ts('Activities') => 'civicrm_activity',
5623 ts('Pledges') => 'civicrm_pledge',
5624 ts('Cases') => 'civicrm_case',
5625 ts('Grants') => 'civicrm_grant',
5626 ts('Address Fields') => 'civicrm_address',
5627 ts('Notes') => 'civicrm_note',
5628 ts('Change Log') => 'civicrm_log',
5629 ts('Mailings') => 'civicrm_mailing',
5631 CRM_Contact_BAO_Query_Hook
::singleton()->getPanesMapper($panesMapper);
5633 foreach (array_keys($this->_whereTables
) as $table) {
5634 if ($panName = array_search($table, $panesMapper)) {
5635 self
::$_openedPanes[$panName] = TRUE;
5639 return self
::$_openedPanes;
5645 public function setOperator($operator) {
5646 $validOperators = array('AND', 'OR');
5647 if (!in_array($operator, $validOperators)) {
5650 $this->_operator
= $operator;
5656 public function getOperator() {
5657 return $this->_operator
;
5665 public function filterRelatedContacts(&$from, &$where, &$having) {
5666 if (!isset(Civi
::$statics[__CLASS__
]['related_contacts_filter'])) {
5667 Civi
::$statics[__CLASS__
]['related_contacts_filter'] = array();
5669 $_rTempCache =& Civi
::$statics[__CLASS__
]['related_contacts_filter'];
5670 // since there only can be one instance of this filter in every query
5671 // skip if filter has already applied
5672 foreach ($_rTempCache as $acache) {
5673 foreach ($acache['queries'] as $aqcache) {
5674 if (strpos($from, $aqcache['from']) !== FALSE) {
5680 $arg_sig = sha1("$from $where $having");
5681 if (isset($_rTempCache[$arg_sig])) {
5682 $cache = $_rTempCache[$arg_sig];
5685 // create temp table with contact ids
5686 $tableName = CRM_Core_DAO
::createTempTableName('civicrm_transform', TRUE);
5688 $sql = "CREATE TEMPORARY TABLE $tableName ( contact_id int primary key) ENGINE=HEAP";
5689 CRM_Core_DAO
::executeQuery($sql);
5692 REPLACE INTO $tableName ( contact_id )
5698 CRM_Core_DAO
::executeQuery($sql);
5700 $cache = array('tableName' => $tableName, 'queries' => array());
5701 $_rTempCache[$arg_sig] = $cache;
5703 // upsert the query depending on relationship type
5704 if (isset($cache['queries'][$this->_displayRelationshipType
])) {
5705 $qcache = $cache['queries'][$this->_displayRelationshipType
];
5708 $tableName = $cache['tableName'];
5713 $rTypes = CRM_Core_PseudoConstant
::relationshipType();
5714 if (is_numeric($this->_displayRelationshipType
)) {
5715 $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType
]['label_a_b'];
5717 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )
5718 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )
5720 $qcache['where'] = "
5721 WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}
5722 AND displayRelType.is_active = 1
5726 list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType
);
5727 if ($dirOne == 'a') {
5728 $relationshipTypeLabel = $rTypes[$relType]['label_a_b'];
5729 $qcache['from'] .= "
5730 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )
5731 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )
5735 $relationshipTypeLabel = $rTypes[$relType]['label_b_a'];
5736 $qcache['from'] .= "
5737 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )
5738 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )
5741 $qcache['where'] = "
5742 WHERE displayRelType.relationship_type_id = $relType
5743 AND displayRelType.is_active = 1
5746 $qcache['relTypeLabel'] = $relationshipTypeLabel;
5747 $_rTempCache[$arg_sig]['queries'][$this->_displayRelationshipType
] = $qcache;
5749 $qillMessage = ts('Contacts with a Relationship Type of: ');
5750 $iqill = $qillMessage . "'" . $qcache['relTypeLabel'] . "'";
5751 if (!is_array($this->_qill
[0]) ||
!in_array($iqill, $this->_qill
[0])) {
5752 $this->_qill
[0][] = $iqill;
5754 if (strpos($from, $qcache['from']) === FALSE) {
5755 // lets replace all the INNER JOIN's in the $from so we dont exclude other data
5756 // this happens when we have an event_type in the quert (CRM-7969)
5757 $from = str_replace("INNER JOIN", "LEFT JOIN", $from);
5758 $from .= $qcache['from'];
5759 $where = $qcache['where'];
5760 if (!empty($this->_permissionWhereClause
)) {
5761 $where .= "AND $this->_permissionWhereClause";
5769 * See CRM-19811 for why this is database hurty without apparent benefit.
5775 public static function caseImportant($op) {
5777 in_array($op, array('LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) ?
FALSE : TRUE;
5781 * @param $returnProperties
5786 public static function componentPresent(&$returnProperties, $prefix) {
5787 foreach ($returnProperties as $name => $dontCare) {
5788 if (substr($name, 0, strlen($prefix)) == $prefix) {
5796 * Builds the necessary structures for all fields that are similar to option value look-ups.
5798 * @param string $name
5799 * the name of the field.
5801 * the sql operator, this function should handle ALL SQL operators.
5802 * @param string $value
5803 * depends on the operator and who's calling the query builder.
5804 * @param int $grouping
5805 * the index where to place the where clause.
5806 * @param string $daoName
5808 * @param array $field
5809 * an array that contains various properties of the field identified by $name.
5810 * @param string $label
5811 * The label for this field element.
5812 * @param string $dataType
5813 * The data type for this element.
5814 * @param bool $useIDsOnly
5816 public function optionValueQuery(
5824 $dataType = 'String',
5828 $pseudoFields = array(
5835 'communication_style_id',
5839 list($tableName, $fieldName) = explode('.', $field['where'], 2);
5840 if ($tableName == 'civicrm_contact') {
5841 $wc = "contact_a.$fieldName";
5844 // Special handling for on_hold, so that we actually use the 'where'
5845 // property in order to limit the query by the on_hold status of the email,
5846 // instead of using email.id which would be nonsensical.
5847 if ($field['name'] == 'on_hold') {
5848 $wc = "{$field['where']}";
5851 $wc = "$tableName.id";
5856 CRM_Core_Error
::deprecatedFunctionWarning('pass $ids to this method');
5857 $wc = "{$field['where']}";
5859 if (in_array($name, $pseudoFields)) {
5860 if (!in_array($name, array('gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'))) {
5861 $wc = "contact_a.{$name}_id";
5863 $dataType = 'Positive';
5864 $value = (!$value) ?
0 : $value;
5866 if ($name == "world_region") {
5867 $field['name'] = $name;
5870 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue($daoName, $field['name'], $value, $op);
5871 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal));
5872 $this->_where
[$grouping][] = self
::buildClause($wc, $op, $value, $dataType);
5876 * Check and explode a user defined numeric string into an array
5877 * this was the protocol used by search builder in the old old days before we had
5878 * super nice js widgets to do the hard work
5880 * @param string $string
5881 * @param string $dataType
5882 * The dataType we should check for the values, default integer.
5884 * @return bool|array
5885 * false if string does not match the pattern
5886 * array of numeric values if string does match the pattern
5888 public static function parseSearchBuilderString($string, $dataType = 'Integer') {
5889 $string = trim($string);
5890 if (substr($string, 0, 1) != '(' ||
substr($string, -1, 1) != ')') {
5894 $string = substr($string, 1, -1);
5895 $values = explode(',', $string);
5896 if (empty($values)) {
5900 $returnValues = array();
5901 foreach ($values as $v) {
5902 if ($dataType == 'Integer' && !is_numeric($v)) {
5905 elseif ($dataType == 'String' && !is_string($v)) {
5908 $returnValues[] = trim($v);
5911 if (empty($returnValues)) {
5915 return $returnValues;
5919 * Convert the pseudo constants id's to their names
5921 * @param CRM_Core_DAO $dao
5922 * @param bool $return
5923 * @param bool $usedForAPI
5925 * @return array|NULL
5927 public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) {
5928 if (empty($this->_pseudoConstantsSelect
)) {
5932 foreach ($this->_pseudoConstantsSelect
as $key => $value) {
5933 if (!empty($this->_pseudoConstantsSelect
[$key]['sorting'])) {
5937 if (is_object($dao) && property_exists($dao, $value['idCol'])) {
5938 $val = $dao->{$value['idCol']};
5939 if ($key == 'groups') {
5940 $dao->groups
= $this->convertGroupIDStringToLabelString($dao, $val);
5944 if (CRM_Utils_System
::isNull($val)) {
5947 elseif (!empty($value['pseudoconstant'])) {
5948 // If pseudoconstant is set that is kind of defacto for 'we have a bit more info about this'
5949 // and we can use the metadata to figure it out.
5950 // ideally this bit of IF will absorb & replace all the rest in time as we move to
5951 // more metadata based choices.
5952 if (strpos($val, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE) {
5953 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($val, CRM_Core_DAO
::VALUE_SEPARATOR
));
5954 foreach ($dbValues as $pseudoValue) {
5955 $convertedValues[] = CRM_Core_PseudoConstant
::getLabel($value['bao'], $value['idCol'], $pseudoValue);
5958 $dao->$key = ($usedForAPI) ?
$convertedValues : implode(', ', $convertedValues);
5959 $realFieldName = CRM_Utils_Array
::value('field_name', $this->_pseudoConstantsSelect
[$key]);
5960 if ($usedForAPI && $realFieldName) {
5961 // normally we would see 2 fields returned for pseudoConstants. An exception is
5962 // preferred_communication_method where there is no id-variant.
5963 // For the api we prioritise getting the real data returned.
5964 // over the resolved version
5965 $dao->$realFieldName = $dbValues;
5970 // This is basically the same as the default but since we have the bao we can use
5971 // a cached function.
5972 $dao->$key = CRM_Core_PseudoConstant
::getLabel($value['bao'], $value['idCol'], $val);
5975 elseif ($baoName = CRM_Utils_Array
::value('bao', $value, NULL)) {
5977 $idColumn = "{$key}_id";
5978 $dao->$idColumn = $val;
5980 if ($key == 'state_province_name') {
5981 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::stateProvince($val);
5984 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::getLabel($baoName, $value['pseudoField'], $val);
5987 elseif ($value['pseudoField'] == 'state_province_abbreviation') {
5988 $dao->$key = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($val);
5990 // @todo handle this in the section above for pseudoconstants.
5991 elseif (in_array($value['pseudoField'], array('participant_role_id', 'participant_role'))) {
5992 // @todo define bao on this & merge into the above condition.
5993 $viewValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $val);
5995 if ($value['pseudoField'] == 'participant_role') {
5996 $pseudoOptions = CRM_Core_PseudoConstant
::get('CRM_Event_DAO_Participant', 'role_id');
5997 foreach ($viewValues as $k => $v) {
5998 $viewValues[$k] = $pseudoOptions[$v];
6001 $dao->$key = ($usedForAPI && count($viewValues) > 1) ?
$viewValues : implode(', ', $viewValues);
6004 $labels = CRM_Core_OptionGroup
::values($value['pseudoField']);
6005 $dao->$key = $labels[$val];
6008 // return converted values in array format
6010 if (strpos($key, '-') !== FALSE) {
6011 $keyVal = explode('-', $key);
6012 $current = &$values;
6013 $lastElement = array_pop($keyVal);
6014 foreach ($keyVal as $v) {
6015 if (!array_key_exists($v, $current)) {
6016 $current[$v] = array();
6018 $current = &$current[$v];
6020 $current[$lastElement] = $dao->$key;
6023 $values[$key] = $dao->$key;
6030 'gender_id' => 'gender',
6031 'prefix_id' => 'individual_prefix',
6032 'suffix_id' => 'individual_suffix',
6033 'communication_style_id' => 'communication_style',
6034 ) as $realField => $labelField) {
6035 // This is a temporary routine for handling these fields while
6036 // we figure out how to handled them based on metadata in
6037 /// export and search builder. CRM-19815, CRM-19830.
6038 if (isset($dao->$realField) && is_numeric($dao->$realField) && isset($dao->$labelField)) {
6039 $dao->$realField = $dao->$labelField;
6047 * Include pseudo fields LEFT JOIN.
6048 * @param string|array $sort can be a object or string
6050 * @return array|NULL
6052 public function includePseudoFieldsJoin($sort) {
6053 if (!$sort ||
empty($this->_pseudoConstantsSelect
)) {
6056 $sort = is_string($sort) ?
$sort : $sort->orderBy();
6059 foreach ($this->_pseudoConstantsSelect
as $name => $value) {
6060 if (!empty($value['table'])) {
6061 $regex = "/({$value['table']}\.|{$name})/";
6062 if (preg_match($regex, $sort)) {
6063 $this->_elemnt
[$value['element']] = 1;
6064 $this->_select
[$value['element']] = $value['select'];
6065 $this->_pseudoConstantsSelect
[$name]['sorting'] = 1;
6066 $present[$value['table']] = $value['join'];
6070 $presentSimpleFrom = $present;
6072 if (array_key_exists('civicrm_worldregion', $this->_whereTables
) &&
6073 array_key_exists('civicrm_country', $presentSimpleFrom)
6075 unset($presentSimpleFrom['civicrm_country']);
6077 if (array_key_exists('civicrm_worldregion', $this->_tables
) &&
6078 array_key_exists('civicrm_country', $present)
6080 unset($present['civicrm_country']);
6083 $presentClause = $presentSimpleFromClause = NULL;
6084 if (!empty($present)) {
6085 $presentClause = implode(' ', $present);
6087 if (!empty($presentSimpleFrom)) {
6088 $presentSimpleFromClause = implode(' ', $presentSimpleFrom);
6091 $this->_fromClause
= $this->_fromClause
. $presentClause;
6092 $this->_simpleFromClause
= $this->_simpleFromClause
. $presentSimpleFromClause;
6094 return array($presentClause, $presentSimpleFromClause);
6098 * Build qill for field.
6100 * Qill refers to the query detail visible on the UI.
6102 * @param string $daoName
6103 * @param string $fieldName
6104 * @param mixed $fieldValue
6106 * @param array $pseudoExtraParam
6108 * Type of the field per CRM_Utils_Type
6112 public static function buildQillForFieldValue(
6117 $pseudoExtraParam = array(),
6118 $type = CRM_Utils_Type
::T_STRING
6120 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
6122 //API usually have fieldValue format as array(operator => array(values)),
6123 //so we need to separate operator out of fieldValue param
6124 if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
6125 $op = key($fieldValue);
6126 $fieldValue = $fieldValue[$op];
6129 // if Operator chosen is NULL/EMPTY then
6130 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
6131 return array(CRM_Utils_Array
::value($op, $qillOperators, $op), '');
6134 if ($fieldName == 'activity_type_id') {
6135 $pseudoOptions = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
6137 elseif ($fieldName == 'country_id') {
6138 $pseudoOptions = CRM_Core_PseudoConstant
::country();
6140 elseif ($fieldName == 'county_id') {
6141 $pseudoOptions = CRM_Core_PseudoConstant
::county();
6143 elseif ($fieldName == 'world_region') {
6144 $pseudoOptions = CRM_Core_PseudoConstant
::worldRegion();
6146 elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') {
6147 $checkPermission = CRM_Utils_Array
::value('check_permission', $pseudoExtraParam, TRUE);
6148 $pseudoOptions = CRM_Event_BAO_Event
::getEvents(0, $fieldValue, TRUE, $checkPermission, TRUE);
6150 elseif ($fieldName == 'contribution_product_id') {
6151 $pseudoOptions = CRM_Contribute_PseudoConstant
::products();
6153 elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
6154 $pseudoOptions = CRM_Core_PseudoConstant
::group();
6156 elseif ($daoName == 'CRM_Batch_BAO_EntityBatch' && $fieldName == 'batch_id') {
6157 $pseudoOptions = CRM_Contribute_PseudoConstant
::batch();
6160 $pseudoOptions = CRM_Core_PseudoConstant
::get($daoName, $fieldName, $pseudoExtraParam);
6163 if (is_array($fieldValue)) {
6164 $qillString = array();
6165 if (!empty($pseudoOptions)) {
6166 foreach ((array) $fieldValue as $val) {
6167 $qillString[] = CRM_Utils_Array
::value($val, $pseudoOptions, $val);
6169 $fieldValue = implode(', ', $qillString);
6172 if ($type == CRM_Utils_Type
::T_DATE
) {
6173 foreach ($fieldValue as $index => $value) {
6174 $fieldValue[$index] = CRM_Utils_Date
::customFormat($value);
6178 // @todo - this is a bit specific (one operator).
6179 // However it is covered by a unit test so can be altered later with
6181 if ($op == 'BETWEEN') {
6182 $separator = ' AND ';
6184 $fieldValue = implode($separator, $fieldValue);
6187 elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) {
6188 $fieldValue = $pseudoOptions[$fieldValue];
6190 elseif ($type === CRM_Utils_Type
::T_DATE
) {
6191 $fieldValue = CRM_Utils_Date
::customFormat($fieldValue);
6194 return array(CRM_Utils_Array
::value($op, $qillOperators, $op), $fieldValue);
6198 * Alter value to reflect wildcard settings.
6200 * The form will have tried to guess whether this is a good field to wildcard but there is
6201 * also a site-wide setting that specifies whether it is OK to append the wild card to the beginning
6202 * or only the end of the string
6204 * @param bool $wildcard
6205 * This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard.
6207 * Generally '=' or 'LIKE'.
6208 * @param string $value
6209 * The search string.
6213 public static function getWildCardedValue($wildcard, $op, $value) {
6214 if ($wildcard && $op == 'LIKE') {
6215 if (CRM_Core_Config
::singleton()->includeWildCardInName
&& (substr($value, 0, 1) != '%')) {
6228 * Process special fields of Search Form in OK (Operator in Key) format
6230 * @param array $formValues
6231 * @param array $specialFields
6232 * Special params to be processed
6233 * @param array $changeNames
6234 * Array of fields whose name should be changed
6236 public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = array()) {
6237 // Array of special fields whose value are considered only for NULL or EMPTY operators
6238 $nullableFields = array('contribution_batch_id');
6240 foreach ($specialFields as $element) {
6241 $value = CRM_Utils_Array
::value($element, $formValues);
6243 if (is_array($value)) {
6244 if (in_array($element, array_keys($changeNames))) {
6245 unset($formValues[$element]);
6246 $element = $changeNames[$element];
6248 $formValues[$element] = array('IN' => $value);
6250 elseif (in_array($value, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
6251 $formValues[$element] = array($value => 1);
6253 elseif (!in_array($element, $nullableFields)) {
6254 // if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
6255 $isWilcard = strstr($value, '%') ?
FALSE : CRM_Core_Config
::singleton()->includeWildCardInName
;
6256 $formValues[$element] = array('LIKE' => self
::getWildCardedValue($isWilcard, 'LIKE', $value));
6263 * Parse and assimilate the various sort options.
6265 * Side-effect: if sorting on a common column from a related table (`city`, `postal_code`,
6266 * `email`), the related table may be joined automatically.
6268 * At time of writing, this code is deeply flawed and should be rewritten. For the moment,
6269 * it's been extracted to a standalone function.
6271 * @param string|CRM_Utils_Sort $sort
6272 * The order by string.
6273 * @param bool $sortByChar
6274 * If true returns the distinct array of first characters for search results.
6275 * @param null $sortOrder
6276 * Who knows? Hu knows. He who knows Hu knows who.
6277 * @param string $additionalFromClause
6278 * Should be clause with proper joins, effective to reduce where clause load.
6280 * list(string $orderByClause, string $additionalFromClause).
6282 protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause) {
6286 if (CRM_Core_Config
::singleton()->includeOrderByClause ||
6287 isset($this->_distinctComponentClause
)
6290 if (is_string($sort)) {
6294 $orderBy = trim($sort->orderBy());
6296 // Deliberately remove the backticks again, as they mess up the evil
6297 // string munging below. This balanced by re-escaping before use.
6298 $orderBy = str_replace('`', '', $orderBy);
6300 if (!empty($orderBy)) {
6301 // this is special case while searching for
6302 // change log CRM-1718
6303 if (preg_match('/sort_name/i', $orderBy)) {
6304 $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy);
6308 $orderBy .= " $sortOrder";
6311 // always add contact_a.id to the ORDER clause
6312 // so the order is deterministic
6313 if (strpos('contact_a.id', $orderBy) === FALSE) {
6314 $orderBy .= ", contact_a.id";
6318 elseif ($sortByChar) {
6319 $orderBy = " sort_name asc";
6322 $orderBy = " contact_a.sort_name ASC, contact_a.id";
6326 return [NULL, $additionalFromClause];
6328 // Remove this here & add it at the end for simplicity.
6329 $order = trim($orderBy);
6330 $orderByArray = explode(',', $order);
6332 foreach ($orderByArray as $orderByClause) {
6333 $orderByClauseParts = explode(' ', trim($orderByClause));
6334 $field = $orderByClauseParts[0];
6335 $direction = isset($orderByClauseParts[1]) ?
$orderByClauseParts[1] : 'asc';
6336 $fieldSpec = $this->getMetadataForRealField($field);
6338 if ($this->_returnProperties
=== []) {
6339 if (!empty($fieldSpec['table_name']) && !isset($this->_tables
[$fieldSpec['table_name']])) {
6340 $this->_tables
[$fieldSpec['table_name']] = 1;
6341 $order = $fieldSpec['where'] . ' ' . $direction;
6347 case 'placeholder-will-remove-next-pr-but-jenkins-will-not-accept-without-and-removing-switch-will-make-hard-to-read':
6351 $cfID = CRM_Core_BAO_CustomField
::getKeyID($field);
6352 // add to cfIDs array if not present
6353 if (!empty($cfID) && !array_key_exists($cfID, $this->_cfIDs
)) {
6354 $this->_cfIDs
[$cfID] = array();
6355 $this->_customQuery
= new CRM_Core_BAO_CustomQuery($this->_cfIDs
, TRUE, $this->_locationSpecificCustomFields
);
6356 $this->_customQuery
->query();
6357 $this->_select
= array_merge($this->_select
, $this->_customQuery
->_select
);
6358 $this->_tables
= array_merge($this->_tables
, $this->_customQuery
->_tables
);
6361 // By replacing the join to the option value table with the mysql construct
6362 // ORDER BY field('contribution_status_id', 2,1,4)
6363 // we can remove a join. In the case of the option value join it is
6364 /// a join known to cause slow queries.
6365 // @todo cover other pseudoconstant types. Limited to option group ones & Foreign keys
6366 // matching an id+name parrern in the
6367 // first instance for scope reasons. They require slightly different handling as the column (label)
6368 // is not declared for them.
6369 // @todo so far only integer fields are being handled. If we add string fields we need to look at
6371 $pseudoConstantMetadata = CRM_Utils_Array
::value('pseudoconstant', $fieldSpec, FALSE);
6372 if (!empty($pseudoConstantMetadata)
6374 if (!empty($pseudoConstantMetadata['optionGroupName'])
6375 ||
$this->isPseudoFieldAnFK($fieldSpec)
6377 $sortedOptions = $fieldSpec['bao']::buildOptions($fieldSpec['name'], NULL, [
6378 'orderColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoConstantMetadata, 'label'),
6380 $fieldIDsInOrder = implode(',', array_keys($sortedOptions));
6381 // Pretty sure this validation ALSO happens in the order clause & this can't be reached but...
6382 // this might give some early warning.
6383 CRM_Utils_Type
::validate($fieldIDsInOrder, 'CommaSeparatedIntegers');
6384 $order = str_replace("$field", "field({$fieldSpec['name']},$fieldIDsInOrder)", $order);
6386 //CRM-12565 add "`" around $field if it is a pseudo constant
6387 // This appears to be for 'special' fields like locations with appended numbers or hyphens .. maybe.
6388 if (!empty($pseudoConstantMetadata['element']) && $pseudoConstantMetadata['element'] == $field) {
6389 $order = str_replace($field, "`{$field}`", $order);
6395 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6396 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6398 // The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
6399 // clause. But this magic helper which forgivingly reescapes ORDER BY.
6400 // Note: $sortByChar implies that $order was hard-coded/trusted, so it can do funky things.
6402 return array(' ORDER BY ' . $order, $additionalFromClause);
6405 $order = CRM_Utils_Type
::escape($order, 'MysqlOrderBy');
6406 return array(' ORDER BY ' . $order, $additionalFromClause);
6411 * Convert a string of group IDs to a string of group labels.
6413 * The original string may include duplicates and groups the user does not have
6414 * permission to see.
6416 * @param CRM_Core_DAO $dao
6417 * @param string $val
6421 public function convertGroupIDStringToLabelString(&$dao, $val) {
6422 $groupIDs = explode(',', $val);
6423 // Note that groups that the user does not have permission to will be excluded (good).
6424 $groups = array_intersect_key(CRM_Core_PseudoConstant
::group(), array_flip($groupIDs));
6425 return implode(', ', $groups);
6430 * Set the qill and where properties for a field.
6432 * This function is intended as a short-term function to encourage refactoring
6433 * & re-use - but really we should just have less special-casing.
6435 * @param string $name
6437 * @param string|array $value
6438 * @param string $grouping
6439 * @param string $field
6441 public function setQillAndWhere($name, $op, $value, $grouping, $field) {
6442 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value);
6443 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
6444 $this->_qill
[$grouping][] = ts("%1 %2 %3", array(
6445 1 => $field['title'],
6452 * Has the pseudoconstant of the field been requested.
6454 * For example if the field is payment_instrument_id then it
6455 * has been requested if either payment_instrument_id or payment_instrument
6456 * have been requested. Payment_instrument is the option groun name field value.
6458 * @param array $field
6459 * @param string $fieldName
6460 * The unique name of the field - ie. the one it will be aliased to in the query.
6464 private function pseudoConstantNameIsInReturnProperties($field, $fieldName = NULL) {
6465 $realField = $this->getMetadataForRealField($fieldName);
6466 if (!isset($realField['pseudoconstant'])) {
6469 $pseudoConstant = $realField['pseudoconstant'];
6470 if (empty($pseudoConstant['optionGroupName']) &&
6471 CRM_Utils_Array
::value('labelColumn', $pseudoConstant) !== 'name') {
6472 // We are increasing our pseudoconstant handling - but still very cautiously,
6473 // hence the check for labelColumn === name
6477 if (!empty($pseudoConstant['optionGroupName']) && CRM_Utils_Array
::value($pseudoConstant['optionGroupName'], $this->_returnProperties
)) {
6480 if (CRM_Utils_Array
::value($fieldName, $this->_returnProperties
)) {
6483 // Is this still required - the above goes off the unique name. Test with things like
6484 // communication_preferences & prefix_id.
6485 if (CRM_Utils_Array
::value($field['name'], $this->_returnProperties
)) {
6492 * Get Select Clause.
6496 public function getSelect() {
6497 $select = "SELECT ";
6498 if (isset($this->_distinctComponentClause
)) {
6499 $select .= "{$this->_distinctComponentClause}, ";
6501 $select .= implode(', ', $this->_select
);
6506 * Add basic statistics to the summary.
6508 * @param array $summary
6509 * @param string $where
6510 * @param string $from
6514 protected function addBasicStatsToSummary(&$summary, $where, $from) {
6515 $summary['total']['count'] = 0;
6516 $summary['total']['amount'] = $summary['total']['avg'] = [];
6519 SELECT COUNT( conts.total_amount ) as total_count,
6520 SUM( conts.total_amount ) as total_amount,
6521 AVG( conts.total_amount ) as total_avg,
6522 conts.currency as currency
6524 SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
6525 civicrm_contribution.currency
6527 $where AND civicrm_contribution.contribution_status_id = 1
6528 GROUP BY civicrm_contribution.id
6532 $dao = CRM_Core_DAO
::executeQuery($query);
6534 while ($dao->fetch()) {
6535 $summary['total']['count'] +
= $dao->total_count
;
6536 $summary['total']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
6537 $summary['total']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
6540 if (!empty($summary['total']['amount'])) {
6541 $summary['total']['amount'] = implode(', ', $summary['total']['amount']);
6542 $summary['total']['avg'] = implode(', ', $summary['total']['avg']);
6545 $summary['total']['amount'] = $summary['total']['avg'] = 0;
6551 * Add basic soft credit statistics to summary array.
6553 * @param array $summary
6554 * @param string $where
6555 * @param string $from
6557 protected function addBasicSoftCreditStatsToStats(&$summary, $where, $from) {
6559 SELECT COUNT( conts.total_amount ) as total_count,
6560 SUM( conts.total_amount ) as total_amount,
6561 AVG( conts.total_amount ) as total_avg,
6562 conts.currency as currency
6564 SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency
6566 $where AND civicrm_contribution.contribution_status_id = 1 AND civicrm_contribution_soft.id IS NOT NULL
6567 GROUP BY civicrm_contribution_soft.id
6571 $dao = CRM_Core_DAO
::executeQuery($query);
6572 $summary['soft_credit']['count'] = 0;
6573 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = [];
6574 while ($dao->fetch()) {
6575 $summary['soft_credit']['count'] +
= $dao->total_count
;
6576 $summary['soft_credit']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
6577 $summary['soft_credit']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
6579 if (!empty($summary['soft_credit']['amount'])) {
6580 $summary['soft_credit']['amount'] = implode(', ', $summary['soft_credit']['amount']);
6581 $summary['soft_credit']['avg'] = implode(', ', $summary['soft_credit']['avg']);
6584 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0;
6589 * Add basic stats about cancelled contributions to the summary.
6591 * @param array $summary
6592 * @param string $where
6593 * @param string $from
6595 protected function addBasicCancelStatsToSummary(&$summary, $where, $from) {
6597 SELECT COUNT( conts.total_amount ) as cancel_count,
6598 SUM( conts.total_amount ) as cancel_amount,
6599 AVG( conts.total_amount ) as cancel_avg,
6600 conts.currency as currency
6602 SELECT civicrm_contribution.total_amount, civicrm_contribution.currency
6604 $where AND civicrm_contribution.cancel_date IS NOT NULL
6605 GROUP BY civicrm_contribution.id
6609 $dao = CRM_Core_DAO
::executeQuery($query);
6612 if ($dao->fetch()) {
6613 $summary['cancel']['count'] = $dao->cancel_count
;
6614 $summary['cancel']['amount'] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
6615 $summary['cancel']['avg'] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
6619 $summary['cancel']['count'] = 0;
6620 $summary['cancel']['amount'] = $summary['cancel']['avg'] = [];
6621 while ($dao->fetch()) {
6622 $summary['cancel']['count'] +
= $dao->cancel_count
;
6623 $summary['cancel']['amount'][] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
6624 $summary['cancel']['avg'][] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
6626 $summary['cancel']['amount'] = implode(', ', $summary['cancel']['amount']);
6627 $summary['cancel']['avg'] = implode(', ', $summary['cancel']['avg']);
6632 * Create the sql query for an contact search.
6634 * @param int $offset
6635 * The offset for the query.
6636 * @param int $rowCount
6637 * The number of rows to return.
6638 * @param string|CRM_Utils_Sort $sort
6639 * The order by string.
6640 * @param bool $count
6641 * Is this a count only query ?.
6642 * @param bool $includeContactIds
6643 * Should we include contact ids?.
6644 * @param bool $sortByChar
6645 * If true returns the distinct array of first characters for search results.
6646 * @param bool $groupContacts
6647 * If true, return only the contact ids.
6648 * @param string $additionalWhereClause
6649 * If the caller wants to further restrict the search (used for components).
6650 * @param null $sortOrder
6651 * @param string $additionalFromClause
6652 * Should be clause with proper joins, effective to reduce where clause load.
6654 * @param bool $skipOrderAndLimit
6657 public function getSearchSQL(
6658 $offset = 0, $rowCount = 0, $sort = NULL,
6659 $count = FALSE, $includeContactIds = FALSE,
6660 $sortByChar = FALSE, $groupContacts = FALSE,
6661 $additionalWhereClause = NULL, $sortOrder = NULL,
6662 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE) {
6664 $sqlParts = $this->getSearchSQLParts($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause);
6666 if ($skipOrderAndLimit) {
6667 CRM_Core_Error
::deprecatedFunctionWarning('skipOrderAndLimit is deprected - call getSearchSQLParts & construct it in the calling function');
6668 $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']}";
6671 $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']} {$sqlParts['order_by']} {$sqlParts['limit']}";
6677 * Get the component parts of the search query as an array.
6679 * @param int $offset
6680 * The offset for the query.
6681 * @param int $rowCount
6682 * The number of rows to return.
6683 * @param string|CRM_Utils_Sort $sort
6684 * The order by string.
6685 * @param bool $count
6686 * Is this a count only query ?.
6687 * @param bool $includeContactIds
6688 * Should we include contact ids?.
6689 * @param bool $sortByChar
6690 * If true returns the distinct array of first characters for search results.
6691 * @param bool $groupContacts
6692 * If true, return only the contact ids.
6693 * @param string $additionalWhereClause
6694 * If the caller wants to further restrict the search (used for components).
6695 * @param null $sortOrder
6696 * @param string $additionalFromClause
6697 * Should be clause with proper joins, effective to reduce where clause load.
6701 public function getSearchSQLParts($offset = 0, $rowCount = 0, $sort = NULL,
6702 $count = FALSE, $includeContactIds = FALSE,
6703 $sortByChar = FALSE, $groupContacts = FALSE,
6704 $additionalWhereClause = NULL, $sortOrder = NULL,
6705 $additionalFromClause = NULL) {
6706 if ($includeContactIds) {
6707 $this->_includeContactIds
= TRUE;
6708 $this->_whereClause
= $this->whereClause();
6710 $onlyDeleted = in_array([
6718 // if we’re explicitly looking for a certain contact’s contribs, events, etc.
6719 // and that contact happens to be deleted, set $onlyDeleted to true
6720 foreach ($this->_params
as $values) {
6721 $name = CRM_Utils_Array
::value(0, $values);
6722 $op = CRM_Utils_Array
::value(1, $values);
6723 $value = CRM_Utils_Array
::value(2, $values);
6724 if ($name == 'contact_id' and $op == '=') {
6725 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) {
6726 $onlyDeleted = TRUE;
6732 // building the query string
6733 $groupBy = $groupByCols = NULL;
6735 if (isset($this->_groupByComponentClause
)) {
6736 $groupByCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause
));
6737 $groupByCols = explode(', ', $groupByCols);
6739 elseif ($this->_useGroupBy
) {
6740 $groupByCols = ['contact_a.id'];
6743 if ($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
&& (!$count)) {
6744 $groupByCols = ['civicrm_activity.id'];
6746 if (!empty($groupByCols)) {
6747 $groupBy = " GROUP BY " . implode(', ', $groupByCols);
6750 $order = $orderBy = '';
6752 list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
6754 // Two cases where we are disabling FGB (FULL_GROUP_BY_MODE):
6755 // 1. Expecting the search query to return all the first single letter characters of contacts ONLY, but when FGB is enabled
6756 // MySQL expect the columns present in GROUP BY, must be present in SELECT clause and that results into error, needless to have other columns.
6757 // 2. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT
6758 // clause. This will impact the search query output.
6759 $disableFullGroupByMode = ($sortByChar ||
!empty($groupBy) ||
$groupContacts);
6761 if ($disableFullGroupByMode) {
6762 CRM_Core_DAO
::disableFullGroupByMode();
6766 $this->_sort
= $sort;
6769 $this->includePseudoFieldsJoin($sort);
6771 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);
6773 if ($additionalWhereClause) {
6774 $where = $where . ' AND ' . $additionalWhereClause;
6777 //additional from clause should be w/ proper joins.
6778 if ($additionalFromClause) {
6779 $from .= "\n" . $additionalFromClause;
6782 // if we are doing a transform, do it here
6783 // use the $from, $where and $having to get the contact ID
6784 if ($this->_displayRelationshipType
) {
6785 $this->filterRelatedContacts($from, $where, $having);
6787 $limit = (!$count && $rowCount) ?
" LIMIT " . CRM_Utils_Type
::escape($offset, 'Int') . ", " . CRM_Utils_Type
::escape($rowCount, 'Int') : '';
6790 'select' => $select,
6793 'order_by' => $order,
6794 'group_by' => $groupBy,
6795 'having' => $having,
6801 * Get the metadata for a given field.
6803 * @param string $fieldName
6807 protected function getMetadataForField($fieldName) {
6808 if ($fieldName === 'contact_a.id') {
6809 // This seems to be the only anomaly.
6812 $pseudoField = isset($this->_pseudoConstantsSelect
[$fieldName]) ?
$this->_pseudoConstantsSelect
[$fieldName] : [];
6813 $field = isset($this->_fields
[$fieldName]) ?
$this->_fields
[$fieldName] : $pseudoField;
6814 $field = array_merge($field, $pseudoField);
6815 if (!empty($field) && empty($field['name'])) {
6816 // standardising field formatting here - over time we can phase out variants.
6817 // all paths using this currently unit tested
6818 $field['name'] = CRM_Utils_Array
::value('field_name', $field, CRM_Utils_Array
::value('idCol', $field, $fieldName));
6824 * Get the metadata for a given field, returning the 'real field' if it is a pseudofield.
6826 * @param string $fieldName
6830 protected function getMetadataForRealField($fieldName) {
6831 $field = $this->getMetadataForField($fieldName);
6832 if (!empty($field['is_pseudofield_for'])) {
6833 $field = $this->getMetadataForField($field['is_pseudofield_for']);
6834 $field['pseudofield_name'] = $fieldName;
6836 elseif (!empty($field['pseudoconstant'])) {
6837 if (!empty($field['pseudoconstant']['optionGroupName'])) {
6838 $field['pseudofield_name'] = $field['pseudoconstant']['optionGroupName'];
6839 if (empty($field['table_name'])) {
6840 if (!empty($field['where'])) {
6841 $field['table_name'] = explode('.', $field['where'])[0];
6844 $field['table_name'] = 'civicrm_contact';
6853 * If we have a field that is better rendered via the pseudoconstant handled them here.
6855 * Rather than joining in the additional table we render the option value on output.
6857 * @todo - so far this applies to a narrow range of pseudocontants. We are adding them
6858 * carefully with test coverage but aim to extend.
6860 * @param string $name
6862 protected function addPseudoconstantFieldToSelect($name) {
6863 $field = $this->getMetadataForRealField($name);
6864 $realFieldName = $field['name'];
6865 $pseudoFieldName = CRM_Utils_Array
::value('pseudofield_name', $field);
6866 if ($pseudoFieldName) {
6867 // @todo - we don't really need to build this array now we have metadata more available with getMetadataForField fn.
6868 $this->_pseudoConstantsSelect
[$pseudoFieldName] = [
6869 'pseudoField' => $pseudoFieldName,
6870 'idCol' => $realFieldName,
6871 'field_name' => $field['name'],
6872 'bao' => $field['bao'],
6873 'pseudoconstant' => $field['pseudoconstant'],
6877 $this->_tables
[$field['table_name']] = 1;
6878 $this->_element
[$realFieldName] = 1;
6879 $this->_select
[$field['name']] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$realFieldName`");
6883 * Is this pseudofield a foreign key constraint.
6885 * We are trying to cautiously expand our pseudoconstant handling. This check allows us
6886 * to extend to a narrowly defined type (and then only if the pseudofield is in the fields
6887 * array which is done for contributions which are mostly handled as pseudoconstants.
6893 protected function isPseudoFieldAnFK($fieldSpec) {
6894 if (empty($fieldSpec['FKClassName'])
6895 || CRM_Utils_Array
::value('keyColumn', $fieldSpec['pseudoconstant']) !== 'id'
6896 || CRM_Utils_Array
::value('labelColumn', $fieldSpec['pseudoconstant']) !== 'name') {
6903 * Is the field a relative date field.
6905 * @param string $fieldName
6909 protected function isARelativeDateField($fieldName) {
6910 if (substr($fieldName, -9, 9) !== '_relative') {
6913 $realField = substr($fieldName, 0, strlen($fieldName) - 9);
6914 return isset($this->_fields
[$realField]);
6920 protected function buildRelativeDateQuery(&$values) {
6921 $value = CRM_Utils_Array
::value(2, $values);
6922 if (empty($value)) {
6925 $fieldName = substr($values[0], 0, strlen($values[0]) - 9);
6926 $fieldSpec = $this->_fields
[$fieldName];
6927 $tableName = $fieldSpec['table_name'];
6928 $filters = CRM_Core_OptionGroup
::values('relative_date_filters');
6929 $grouping = CRM_Utils_Array
::value(3, $values);
6930 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
6932 $dates = CRM_Utils_Date
::getFromTo($value, NULL, NULL);
6933 if (empty($dates[0])) {
6934 // ie. no start date we only have end date
6935 $this->_where
[$grouping][] = $fieldSpec['where'] . " <= '{$dates[1]}'";
6937 $this->_qill
[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("to %1", [
6938 CRM_Utils_Date
::customFormat($dates[1]),
6941 elseif (empty($dates[1])) {
6942 // ie. no end date we only have start date
6943 $this->_where
[$grouping][] = $fieldSpec['where'] . " >= '{$dates[1]}'";
6945 $this->_qill
[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("from %1", [
6946 CRM_Utils_Date
::customFormat($dates[0]),
6950 // we have start and end dates.
6951 $this->_where
[$grouping][] = $fieldSpec['where'] . " BETWEEN '{$dates[0]}' AND '{$dates[1]}'";
6953 $this->_qill
[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("between %1 and %2", [
6954 CRM_Utils_Date
::customFormat($dates[0]),
6955 CRM_Utils_Date
::customFormat($dates[1]),