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 public static $_defaultReturnProperties = NULL;
86 * The default set of hier return properties.
90 public 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 * Fields hacked for legacy reasons.
209 * Generally where a field has a option group defining it's options we add them to
210 * the fields array as pseudofields - eg for gender we would add the key 'gender' to fields
211 * using CRM_Core_DAO::appendPseudoConstantsToFields($fields);
213 * The rendered results would hold an id in the gender_id field and the label in the pseudo 'Gender'
214 * field. The heading for the pseudofield would come form the the option group name & for the id field
217 * These fields are handled in a more legacy way - ie overwriting 'gender_id' with the label on output
218 * via the convertToPseudoNames function. Ideally we would convert them but they would then need to be fixed
219 * in some other places & there are also some issues around the name (ie. Gender currently has the label in the
220 * schema 'Gender' so adding a second 'Gender' field to search builder & export would be confusing and the standard is
221 * not fully agreed here.
225 protected $legacyHackedFields = [
226 'gender_id' => 'gender',
227 'prefix_id' => 'individual_prefix',
228 'suffix_id' => 'individual_suffix',
229 'communication_style_id' => 'communication_style',
233 * The cache to translate the option values into labels.
240 * Are we in search mode.
244 public $_search = TRUE;
247 * Should we skip permission checking.
251 public $_skipPermission = FALSE;
254 * Should we skip adding of delete clause.
258 public $_skipDeleteClause = FALSE;
261 * Are we in strict mode (use equality over LIKE)
265 public $_strict = FALSE;
268 * What operator to use to group the clauses.
272 public $_operator = 'AND';
277 * Should we only search on primary location.
281 public $_primaryLocation = TRUE;
284 * Are contact ids part of the query.
288 public $_includeContactIds = FALSE;
291 * Should we use the smart group cache.
295 public $_smartGroupCache = TRUE;
298 * Should we display contacts with a specific relationship type.
302 public $_displayRelationshipType = NULL;
305 * Reference to the query object for custom values.
309 public $_customQuery;
312 * Should we enable the distinct clause, used if we are including
313 * more than one group
317 public $_useDistinct = FALSE;
320 * Should we just display one contact record
323 public $_useGroupBy = FALSE;
326 * The relationship type direction
330 public static $_relType;
337 public static $_activityRole;
340 * Consider the component activity type
341 * during activity search.
345 public static $_considerCompActivities;
348 * Consider with contact activities only,
349 * during activity search.
353 public static $_withContactActivitiesOnly;
356 * Use distinct component clause for component searches
360 public $_distinctComponentClause;
362 public $_rowCountClause;
365 * Use groupBy component clause for component searches
369 public $_groupByComponentClause;
372 * Track open panes, useful in advance search
376 public static $_openedPanes = [];
379 * For search builder - which custom fields are location-dependent
382 public $_locationSpecificCustomFields = [];
385 * The tables which have a dependency on location and/or address
389 public static $_dependencies = [
390 'civicrm_state_province' => 1,
391 'civicrm_country' => 1,
392 'civicrm_county' => 1,
393 'civicrm_address' => 1,
394 'civicrm_location_type' => 1,
398 * List of location specific fields.
401 public static $_locationSpecificFields = [
406 'supplemental_address_1',
407 'supplemental_address_2',
408 'supplemental_address_3',
411 'postal_code_suffix',
425 * Remember if we handle either end of a number or date range
426 * so we can skip the other
429 protected $_rangeCache = [];
431 * Set to true when $this->relationship is run to avoid adding twice.
435 protected $_relationshipValuesAdded = FALSE;
438 * Set to the name of the temp table if one has been created.
442 public static $_relationshipTempTable = NULL;
444 public $_pseudoConstantsSelect = [];
446 public $_groupUniqueKey = NULL;
447 public $_groupKeys = [];
450 * Class constructor which also does all the work.
452 * @param array $params
453 * @param array $returnProperties
454 * @param array $fields
455 * @param bool $includeContactIds
456 * @param bool $strict
457 * @param bool|int $mode - mode the search is operating on
459 * @param bool $skipPermission
460 * @param bool $searchDescendentGroups
461 * @param bool $smartGroupCache
462 * @param null $displayRelationshipType
463 * @param string $operator
464 * @param string $apiEntity
465 * @param bool|null $primaryLocationOnly
467 public function __construct(
468 $params = NULL, $returnProperties = NULL, $fields = NULL,
469 $includeContactIds = FALSE, $strict = FALSE, $mode = 1,
470 $skipPermission = FALSE, $searchDescendentGroups = TRUE,
471 $smartGroupCache = TRUE, $displayRelationshipType = NULL,
474 $primaryLocationOnly = NULL
476 if ($primaryLocationOnly === NULL) {
477 $primaryLocationOnly = Civi
::settings()->get('searchPrimaryDetailsOnly');
479 $this->_primaryLocation
= $primaryLocationOnly;
480 $this->_params
= &$params;
481 if ($this->_params
== NULL) {
485 if ($returnProperties === self
::NO_RETURN_PROPERTIES
) {
486 $this->_returnProperties
= [];
488 elseif (empty($returnProperties)) {
489 $this->_returnProperties
= self
::defaultReturnProperties($mode);
492 $this->_returnProperties
= &$returnProperties;
495 $this->_includeContactIds
= $includeContactIds;
496 $this->_strict
= $strict;
497 $this->_mode
= $mode;
498 $this->_skipPermission
= $skipPermission;
499 $this->_smartGroupCache
= $smartGroupCache;
500 $this->_displayRelationshipType
= $displayRelationshipType;
501 $this->setOperator($operator);
504 $this->_fields
= &$fields;
505 $this->_search
= FALSE;
506 $this->_skipPermission
= TRUE;
509 $this->_fields
= CRM_Contact_BAO_Contact
::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission);
510 // The legacy hacked fields will output as a string rather than their underlying type.
511 foreach (array_keys($this->legacyHackedFields
) as $fieldName) {
512 $this->_fields
[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
515 $fields = CRM_Core_Component
::getQueryFields(!$this->_skipPermission
);
516 unset($fields['note']);
517 $this->_fields
= array_merge($this->_fields
, $fields);
519 // add activity fields
520 $fields = CRM_Activity_BAO_Activity
::exportableFields();
521 $this->_fields
= array_merge($this->_fields
, $fields);
523 // add any fields provided by hook implementers
524 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
525 $this->_fields
= array_merge($this->_fields
, $extFields);
528 // basically do all the work once, and then reuse it
529 $this->initialize($apiEntity);
533 * Function which actually does all the work for the constructor.
535 * @param string $apiEntity
536 * The api entity being called.
537 * This sort-of duplicates $mode in a confusing way. Probably not by design.
539 public function initialize($apiEntity = NULL) {
541 $this->_element
= [];
543 $this->_whereTables
= [];
546 $this->_options
= [];
548 $this->_paramLookup
= [];
551 $this->_customQuery
= NULL;
553 // reset cached static variables - CRM-5803
554 self
::$_activityRole = NULL;
555 self
::$_considerCompActivities = NULL;
556 self
::$_withContactActivitiesOnly = NULL;
558 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
559 $this->_element
['contact_id'] = 1;
560 $this->_tables
['civicrm_contact'] = 1;
562 if (!empty($this->_params
)) {
563 $this->buildParamsLookup();
566 $this->_whereTables
= $this->_tables
;
568 $this->selectClause($apiEntity);
569 $isForcePrimaryOnly = !empty($apiEntity);
570 $this->_whereClause
= $this->whereClause($isForcePrimaryOnly);
571 if (array_key_exists('civicrm_contribution', $this->_whereTables
)) {
572 $component = 'contribution';
574 if (array_key_exists('civicrm_membership', $this->_whereTables
)) {
575 $component = 'membership';
577 if (isset($component) && !$this->_skipPermission
) {
578 // Unit test coverage in api_v3_FinancialTypeACLTest::testGetACLContribution.
579 CRM_Financial_BAO_FinancialType
::buildPermissionedClause($this->_whereClause
, $component);
582 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
, $apiEntity);
583 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
585 $this->openedSearchPanes(TRUE);
589 * Function for same purpose as convertFormValues.
591 * Like convert form values this function exists to pre-Process parameters from the form.
593 * It is unclear why they are different functions & likely relates to advances search
594 * versus search builder.
596 * The direction we are going is having the form convert values to a standardised format &
597 * moving away from weird & wonderful where clause switches.
599 * Fix and handle contact deletion nicely.
601 * this code is primarily for search builder use case where different clauses can specify if they want deleted.
605 public function buildParamsLookup() {
606 $trashParamExists = FALSE;
608 foreach ($this->_params
as $k => $param) {
609 if (!empty($param[0]) && $param[0] == 'contact_is_deleted') {
610 $trashParamExists = TRUE;
612 if (!empty($param[3])) {
613 $paramByGroup[$param[3]][$k] = $param;
617 if ($trashParamExists) {
618 $this->_skipDeleteClause
= TRUE;
620 //cycle through group sets and explicitly add trash param if not set
621 foreach ($paramByGroup as $setID => $set) {
623 !in_array(['contact_is_deleted', '=', '1', $setID, '0'], $this->_params
) &&
624 !in_array(['contact_is_deleted', '=', '0', $setID, '0'], $this->_params
)
627 'contact_is_deleted',
637 foreach ($this->_params
as $value) {
638 if (empty($value[0])) {
641 $cfID = CRM_Core_BAO_CustomField
::getKeyID(str_replace('_relative', '', $value[0]));
643 if (!array_key_exists($cfID, $this->_cfIDs
)) {
644 $this->_cfIDs
[$cfID] = [];
646 // Set wildcard value based on "and/or" selection
647 foreach ($this->_params
as $key => $param) {
648 if ($param[0] == $value[0] . '_operator') {
649 $value[4] = $param[2] == 'or';
653 $this->_cfIDs
[$cfID][] = $value;
656 if (!array_key_exists($value[0], $this->_paramLookup
)) {
657 $this->_paramLookup
[$value[0]] = [];
659 if ($value[0] !== 'group') {
660 // Just trying to unravel how group interacts here! This whole function is weird.
661 $this->_paramLookup
[$value[0]][] = $value;
667 * Some composite fields do not appear in the fields array hack to make them part of the query.
670 * The api entity being called.
671 * This sort-of duplicates $mode in a confusing way. Probably not by design.
673 public function addSpecialFields($apiEntity) {
674 static $special = ['contact_type', 'contact_sub_type', 'sort_name', 'display_name'];
675 // if get called via Contact.get API having address_id as return parameter
676 if ($apiEntity == 'Contact') {
677 $special[] = 'address_id';
679 foreach ($special as $name) {
680 if (!empty($this->_returnProperties
[$name])) {
681 if ($name == 'address_id') {
682 $this->_tables
['civicrm_address'] = 1;
683 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
684 $this->_element
['address_id'] = 1;
687 $this->_select
[$name] = "contact_a.{$name} as $name";
688 $this->_element
[$name] = 1;
695 * Given a list of conditions in params and a list of desired
696 * return Properties generate the required select and from
697 * clauses. Note that since the where clause introduces new
698 * tables, the initial attempt also retrieves all variables used
701 * @param string $apiEntity
702 * The api entity being called.
703 * This sort-of duplicates $mode in a confusing way. Probably not by design.
705 public function selectClause($apiEntity = NULL) {
707 // @todo Tidy up this. This arises because 1) we are ignoring the $mode & adding a new
708 // param ($apiEntity) instead - presumably an oversight & 2 because
709 // contact is not implemented as a component.
710 $this->addSpecialFields($apiEntity);
712 foreach ($this->_fields
as $name => $field) {
713 // skip component fields
714 // there are done by the alter query below
715 // and need not be done on every field
716 // @todo remove these & handle using metadata - only obscure fields
717 // that are hack-added should need to be excluded from the main loop.
719 (substr($name, 0, 12) == 'participant_') ||
720 (substr($name, 0, 7) == 'pledge_') ||
721 (substr($name, 0, 5) == 'case_')
726 // redirect to activity select clause
728 (substr($name, 0, 9) == 'activity_') ||
729 ($name == 'parent_id')
731 CRM_Activity_BAO_Query
::select($this);
734 // if this is a hierarchical name, we ignore it
735 $names = explode('-', $name);
736 if (count($names) > 1 && isset($names[1]) && is_numeric($names[1])) {
740 // make an exception for special cases, to add the field in select clause
741 $makeException = FALSE;
743 //special handling for groups/tags
744 if (in_array($name, ['groups', 'tags', 'notes'])
745 && isset($this->_returnProperties
[substr($name, 0, -1)])
747 // @todo instead of setting make exception to get us into
748 // an if clause that has handling for these fields buried with in it
749 // move the handling to here.
750 $makeException = TRUE;
753 // since note has 3 different options we need special handling
754 // note / note_subject / note_body
755 if ($name == 'notes') {
756 foreach (['note', 'note_subject', 'note_body'] as $noteField) {
757 if (isset($this->_returnProperties
[$noteField])) {
758 $makeException = TRUE;
764 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
766 !empty($this->_paramLookup
[$name])
767 ||
!empty($this->_returnProperties
[$name])
768 ||
$this->pseudoConstantNameIsInReturnProperties($field, $name)
772 // add to cfIDs array if not present
773 if (!array_key_exists($cfID, $this->_cfIDs
)) {
774 $this->_cfIDs
[$cfID] = [];
777 elseif (isset($field['where'])) {
778 list($tableName, $fieldName) = explode('.', $field['where'], 2);
779 if (isset($tableName)) {
780 if (CRM_Utils_Array
::value($tableName, self
::$_dependencies)) {
781 $this->_tables
['civicrm_address'] = 1;
782 $this->_select
['address_id'] = 'civicrm_address.id as address_id';
783 $this->_element
['address_id'] = 1;
786 if ($tableName == 'im_provider' ||
$tableName == 'email_greeting' ||
787 $tableName == 'postal_greeting' ||
$tableName == 'addressee'
789 if ($tableName == 'im_provider') {
790 CRM_Core_OptionValue
::select($this);
793 if (in_array($tableName,
794 ['email_greeting', 'postal_greeting', 'addressee'])) {
795 $this->_element
["{$name}_id"] = 1;
796 $this->_select
["{$name}_id"] = "contact_a.{$name}_id as {$name}_id";
797 $this->_pseudoConstantsSelect
[$name] = ['pseudoField' => $tableName, 'idCol' => "{$name}_id"];
798 $this->_pseudoConstantsSelect
[$name]['select'] = "{$name}.{$fieldName} as $name";
799 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
801 if ($tableName == 'email_greeting') {
803 $this->_pseudoConstantsSelect
[$name]['join']
804 = " LEFT JOIN civicrm_option_group option_group_email_greeting ON (option_group_email_greeting.name = 'email_greeting')";
805 $this->_pseudoConstantsSelect
[$name]['join'] .=
806 " 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 ) ";
808 elseif ($tableName == 'postal_greeting') {
810 $this->_pseudoConstantsSelect
[$name]['join']
811 = " LEFT JOIN civicrm_option_group option_group_postal_greeting ON (option_group_postal_greeting.name = 'postal_greeting')";
812 $this->_pseudoConstantsSelect
[$name]['join'] .=
813 " 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 ) ";
815 elseif ($tableName == 'addressee') {
817 $this->_pseudoConstantsSelect
[$name]['join']
818 = " LEFT JOIN civicrm_option_group option_group_addressee ON (option_group_addressee.name = 'addressee')";
819 $this->_pseudoConstantsSelect
[$name]['join'] .=
820 " LEFT JOIN civicrm_option_value addressee ON (contact_a.addressee_id = addressee.value AND option_group_addressee.id = addressee.option_group_id ) ";
822 $this->_pseudoConstantsSelect
[$name]['table'] = $tableName;
825 $greetField = "{$name}_display";
826 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
827 $this->_element
[$greetField] = 1;
829 $greetField = "{$name}_custom";
830 $this->_select
[$greetField] = "contact_a.{$greetField} as {$greetField}";
831 $this->_element
[$greetField] = 1;
835 if (!in_array($tableName, ['civicrm_state_province', 'civicrm_country', 'civicrm_county'])) {
836 $this->_tables
[$tableName] = 1;
839 // also get the id of the tableName
840 $tName = substr($tableName, 8);
841 if (in_array($tName, ['country', 'state_province', 'county'])) {
842 if ($tName == 'state_province') {
843 $this->_pseudoConstantsSelect
['state_province_name'] = [
844 'pseudoField' => "{$tName}",
845 'idCol' => "{$tName}_id",
846 'bao' => 'CRM_Core_BAO_Address',
847 'table' => "civicrm_{$tName}",
848 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
851 $this->_pseudoConstantsSelect
[$tName] = [
852 'pseudoField' => 'state_province_abbreviation',
853 'idCol' => "{$tName}_id",
854 'table' => "civicrm_{$tName}",
855 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
859 $this->_pseudoConstantsSelect
[$name] = [
860 'pseudoField' => "{$tName}_id",
861 'idCol' => "{$tName}_id",
862 'bao' => 'CRM_Core_BAO_Address',
863 'table' => "civicrm_{$tName}",
864 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ",
868 $this->_select
["{$tName}_id"] = "civicrm_address.{$tName}_id as {$tName}_id";
869 $this->_element
["{$tName}_id"] = 1;
871 elseif ($tName != 'contact') {
872 $this->_select
["{$tName}_id"] = "{$tableName}.id as {$tName}_id";
873 $this->_element
["{$tName}_id"] = 1;
876 //special case for phone
877 if ($name == 'phone') {
878 $this->_select
['phone_type_id'] = "civicrm_phone.phone_type_id as phone_type_id";
879 $this->_element
['phone_type_id'] = 1;
882 // if IM then select provider_id also
883 // to get "IM Service Provider" in a file to be exported, CRM-3140
885 $this->_select
['provider_id'] = "civicrm_im.provider_id as provider_id";
886 $this->_element
['provider_id'] = 1;
889 if ($tName == 'contact' && $fieldName == 'organization_name') {
890 // special case, when current employer is set for Individual contact
891 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name";
893 elseif ($tName == 'contact' && $fieldName === 'id') {
894 // Handled elsewhere, explicitly ignore. Possibly for all tables...
896 elseif (in_array($tName, ['country', 'county'])) {
897 $this->_pseudoConstantsSelect
[$name]['select'] = "{$field['where']} as `$name`";
898 $this->_pseudoConstantsSelect
[$name]['element'] = $name;
900 elseif ($tName == 'state_province') {
901 $this->_pseudoConstantsSelect
[$tName]['select'] = "{$field['where']} as `$name`";
902 $this->_pseudoConstantsSelect
[$tName]['element'] = $name;
904 elseif (strpos($name, 'contribution_soft_credit') !== FALSE) {
905 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_params
)) {
906 $this->_select
[$name] = "{$field['where']} as `$name`";
909 elseif ($this->pseudoConstantNameIsInReturnProperties($field, $name)) {
910 $this->addPseudoconstantFieldToSelect($name);
913 $this->_select
[$name] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$name`");
915 if (!in_array($tName, ['state_province', 'country', 'county'])) {
916 $this->_element
[$name] = 1;
921 elseif ($name === 'tags') {
922 //@todo move this handling outside the big IF & ditch $makeException
923 $this->_useGroupBy
= TRUE;
924 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags";
925 $this->_element
[$name] = 1;
926 $this->_tables
['civicrm_tag'] = 1;
927 $this->_tables
['civicrm_entity_tag'] = 1;
929 elseif ($name === 'groups') {
930 //@todo move this handling outside the big IF & ditch $makeException
931 $this->_useGroupBy
= TRUE;
932 // Duplicates will be created here but better to sort them out in php land.
933 $this->_select
[$name] = "
935 GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')),
936 GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id)
939 $this->_element
[$name] = 1;
940 $this->_tables
['civicrm_group_contact'] = 1;
941 $this->_tables
['civicrm_group_contact_cache'] = 1;
942 $this->_pseudoConstantsSelect
["{$name}"] = [
943 'pseudoField' => "groups",
947 elseif ($name === 'notes') {
948 //@todo move this handling outside the big IF & ditch $makeException
949 // if note field is subject then return subject else body of the note
950 $noteColumn = 'note';
951 if (isset($noteField) && $noteField == 'note_subject') {
952 $noteColumn = 'subject';
955 $this->_useGroupBy
= TRUE;
956 $this->_select
[$name] = "GROUP_CONCAT(DISTINCT(civicrm_note.$noteColumn)) as notes";
957 $this->_element
[$name] = 1;
958 $this->_tables
['civicrm_note'] = 1;
960 elseif ($name === 'current_employer') {
961 $this->_select
[$name] = "IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer";
962 $this->_element
[$name] = 1;
966 if ($cfID && !empty($field['is_search_range'])) {
967 // this is a custom field with range search enabled, so we better check for two/from values
968 if (!empty($this->_paramLookup
[$name . '_from'])) {
969 if (!array_key_exists($cfID, $this->_cfIDs
)) {
970 $this->_cfIDs
[$cfID] = [];
972 foreach ($this->_paramLookup
[$name . '_from'] as $pID => $p) {
973 // search in the cdID array for the same grouping
975 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
976 if ($c[3] == $p[3]) {
977 $this->_cfIDs
[$cfID][$cID][2]['from'] = $p[2];
982 $p[2] = ['from' => $p[2]];
983 $this->_cfIDs
[$cfID][] = $p;
987 if (!empty($this->_paramLookup
[$name . '_to'])) {
988 if (!array_key_exists($cfID, $this->_cfIDs
)) {
989 $this->_cfIDs
[$cfID] = [];
991 foreach ($this->_paramLookup
[$name . '_to'] as $pID => $p) {
992 // search in the cdID array for the same grouping
994 foreach ($this->_cfIDs
[$cfID] as $cID => $c) {
995 if ($c[4] == $p[4]) {
996 $this->_cfIDs
[$cfID][$cID][2]['to'] = $p[2];
1001 $p[2] = ['to' => $p[2]];
1002 $this->_cfIDs
[$cfID][] = $p;
1009 // add location as hierarchical elements
1010 $this->addHierarchicalElements();
1012 // add multiple field like website
1013 $this->addMultipleElements();
1016 CRM_Core_Component
::alterQuery($this, 'select');
1018 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'select');
1020 if (!empty($this->_cfIDs
)) {
1021 // @todo This function is the select function but instead of running 'select' it
1022 // is running the whole query.
1023 $this->_customQuery
= new CRM_Core_BAO_CustomQuery($this->_cfIDs
, TRUE, $this->_locationSpecificCustomFields
);
1024 $this->_customQuery
->query();
1025 $this->_select
= array_merge($this->_select
, $this->_customQuery
->_select
);
1026 $this->_element
= array_merge($this->_element
, $this->_customQuery
->_element
);
1027 $this->_tables
= array_merge($this->_tables
, $this->_customQuery
->_tables
);
1028 $this->_whereTables
= array_merge($this->_whereTables
, $this->_customQuery
->_whereTables
);
1029 $this->_options
= $this->_customQuery
->_options
;
1034 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
1036 public function addHierarchicalElements() {
1037 if (empty($this->_returnProperties
['location'])) {
1040 if (!is_array($this->_returnProperties
['location'])) {
1044 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
1048 $addressCustomFields = CRM_Core_BAO_CustomField
::getFieldsForImport('Address');
1049 $addressCustomFieldIds = [];
1051 foreach ($this->_returnProperties
['location'] as $name => $elements) {
1052 $lCond = self
::getPrimaryCondition($name);
1053 $locationTypeId = is_numeric($name) ?
NULL : array_search($name, $locationTypes);
1056 if ($locationTypeId === FALSE) {
1059 $lCond = "location_type_id = $locationTypeId";
1060 $this->_useDistinct
= TRUE;
1062 //commented for CRM-3256
1063 $this->_useGroupBy
= TRUE;
1066 $name = str_replace(' ', '_', $name);
1067 $tName = "$name-location_type";
1068 $ltName = "`$name-location_type`";
1069 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1070 $this->_select
["{$tName}"] = "`$tName`.name as `{$tName}`";
1071 $this->_element
["{$tName}_id"] = 1;
1072 $this->_element
["{$tName}"] = 1;
1074 $locationTypeName = $tName;
1075 $locationTypeJoin = [];
1078 foreach ($elements as $elementFullName => $dontCare) {
1080 $elementName = $elementCmpName = $elementFullName;
1082 if (substr($elementCmpName, 0, 5) == 'phone') {
1083 $elementCmpName = 'phone';
1086 if (in_array($elementCmpName, array_keys($addressCustomFields))) {
1087 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($elementCmpName)) {
1088 $addressCustomFieldIds[$cfID][$name] = 1;
1091 // add address table - doesn't matter if we do it mutliple times - it's the same data
1092 // @todo ditch the double processing of addressJoin
1093 if ((in_array($elementCmpName, self
::$_locationSpecificFields) ||
!empty($addressCustomFieldIds))
1094 && !in_array($elementCmpName, ['email', 'phone', 'im', 'openid'])
1096 list($aName, $addressJoin) = $this->addAddressTable($name, $lCond);
1097 $locationTypeJoin[$tName] = " ( $aName.location_type_id = $ltName.id ) ";
1098 $processed[$aName] = 1;
1101 $cond = $elementType = '';
1102 if (strpos($elementName, '-') !== FALSE) {
1103 // this is either phone, email or IM
1104 list($elementName, $elementType) = explode('-', $elementName);
1106 if (($elementName != 'phone') && ($elementName != 'im')) {
1107 $cond = self
::getPrimaryCondition($elementType);
1109 // CRM-13011 : If location type is primary, do not restrict search to the phone
1110 // type id - we want the primary phone, regardless of what type it is.
1111 // Otherwise, restrict to the specified phone type for the given field.
1112 if ((!$cond) && ($elementName == 'phone')) {
1113 $cond = "phone_type_id = '$elementType'";
1115 elseif ((!$cond) && ($elementName == 'im')) {
1116 // IM service provider id, CRM-3140
1117 $cond = "provider_id = '$elementType'";
1119 $elementType = '-' . $elementType;
1122 $field = CRM_Utils_Array
::value($elementName, $this->_fields
);
1124 // hack for profile, add location id
1127 // fix for CRM-882( to handle phone types )
1128 !is_numeric($elementType)
1130 if (is_numeric($name)) {
1131 $field = CRM_Utils_Array
::value($elementName . "-Primary$elementType", $this->_fields
);
1134 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1137 elseif (is_numeric($name)) {
1138 //this for phone type to work
1139 if (in_array($elementName, ['phone', 'phone_ext'])) {
1140 $field = CRM_Utils_Array
::value($elementName . "-Primary" . $elementType, $this->_fields
);
1143 $field = CRM_Utils_Array
::value($elementName . "-Primary", $this->_fields
);
1147 //this is for phone type to work for profile edit
1148 if (in_array($elementName, ['phone', 'phone_ext'])) {
1149 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId$elementType", $this->_fields
);
1152 $field = CRM_Utils_Array
::value($elementName . "-$locationTypeId", $this->_fields
);
1157 // Check if there is a value, if so also add to where Clause
1159 if ($this->_params
) {
1161 if (isset($locationTypeId)) {
1162 $nm .= "-$locationTypeId";
1164 if (!is_numeric($elementType)) {
1165 $nm .= "$elementType";
1168 foreach ($this->_params
as $id => $values) {
1169 if ((is_array($values) && $values[0] == $nm) ||
1170 (in_array($elementName, ['phone', 'im'])
1171 && (strpos($values[0], $nm) !== FALSE)
1181 if ($field && isset($field['where'])) {
1182 list($tableName, $fieldName) = explode('.', $field['where'], 2);
1183 $pf = substr($tableName, 8);
1184 $tName = $name . '-' . $pf . $elementType;
1185 if (isset($tableName)) {
1186 if ($tableName == 'civicrm_state_province' ||
$tableName == 'civicrm_country' ||
$tableName == 'civicrm_county') {
1187 $this->_select
["{$tName}_id"] = "{$aName}.{$pf}_id as `{$tName}_id`";
1190 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1193 $this->_element
["{$tName}_id"] = 1;
1194 if (substr($tName, -15) == '-state_province') {
1195 // FIXME: hack to fix CRM-1900
1196 $a = Civi
::settings()->get('address_format');
1198 if (substr_count($a, 'state_province_name') > 0) {
1199 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = [
1200 'pseudoField' => "{$pf}_id",
1201 'idCol' => "{$tName}_id",
1202 'bao' => 'CRM_Core_BAO_Address',
1204 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.name as `{$name}-{$elementFullName}`";
1207 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = [
1208 'pseudoField' => 'state_province_abbreviation',
1209 'idCol' => "{$tName}_id",
1211 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.abbreviation as `{$name}-{$elementFullName}`";
1215 if (substr($elementFullName, 0, 2) == 'im') {
1216 $provider = "{$name}-{$elementFullName}-provider_id";
1217 $this->_select
[$provider] = "`$tName`.provider_id as `{$name}-{$elementFullName}-provider_id`";
1218 $this->_element
[$provider] = 1;
1220 if ($pf == 'country' ||
$pf == 'county') {
1221 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"] = [
1222 'pseudoField' => "{$pf}_id",
1223 'idCol' => "{$tName}_id",
1224 'bao' => 'CRM_Core_BAO_Address',
1226 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['select'] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1229 $this->_select
["{$name}-{$elementFullName}"] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`";
1233 if (in_array($pf, ['state_province', 'country', 'county'])) {
1234 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['element'] = "{$name}-{$elementFullName}";
1237 $this->_element
["{$name}-{$elementFullName}"] = 1;
1240 if (empty($processed["`$tName`"])) {
1241 $processed["`$tName`"] = 1;
1242 $newName = $tableName . '_' . $index;
1243 switch ($tableName) {
1244 case 'civicrm_phone':
1245 case 'civicrm_email':
1247 case 'civicrm_openid':
1249 $this->_tables
[$tName] = "\nLEFT JOIN $tableName `$tName` ON contact_a.id = `$tName`.contact_id";
1250 if ($tableName != 'civicrm_phone') {
1251 $this->_tables
[$tName] .= " AND `$tName`.$lCond";
1253 elseif (is_numeric($name)) {
1254 $this->_select
[$tName] = "IF (`$tName`.is_primary = $name, `$tName`.phone, NULL) as `$tName`";
1257 // this special case to add phone type
1259 $phoneTypeCondition = " AND `$tName`.$cond ";
1260 //gross hack to pickup corrupted data also, CRM-7603
1261 if (strpos($cond, 'phone_type_id') !== FALSE) {
1262 $phoneTypeCondition = " AND ( `$tName`.$cond OR `$tName`.phone_type_id IS NULL ) ";
1263 if (!empty($lCond)) {
1264 $phoneTypeCondition .= " AND ( `$tName`.$lCond ) ";
1267 $this->_tables
[$tName] .= $phoneTypeCondition;
1270 //build locationType join
1271 $locationTypeJoin[$tName] = " ( `$tName`.location_type_id = $ltName.id )";
1274 $this->_whereTables
[$tName] = $this->_tables
[$tName];
1278 case 'civicrm_state_province':
1279 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $tName;
1280 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1281 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.state_province_id";
1283 $this->_whereTables
["{$name}-address"] = $addressJoin;
1287 case 'civicrm_country':
1288 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1289 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1290 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.country_id";
1292 $this->_whereTables
["{$name}-address"] = $addressJoin;
1296 case 'civicrm_county':
1297 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['table'] = $newName;
1298 $this->_pseudoConstantsSelect
["{$name}-{$elementFullName}"]['join']
1299 = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.county_id";
1301 $this->_whereTables
["{$name}-address"] = $addressJoin;
1307 $this->_whereTables
["{$name}-address"] = $addressJoin;
1316 // add location type join
1317 $ltypeJoin = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locationTypeJoin) . " )";
1318 $this->_tables
[$locationTypeName] = $ltypeJoin;
1320 // table should be present in $this->_whereTables,
1321 // to add its condition in location type join, CRM-3939.
1322 if ($addWhereCount) {
1324 foreach ($this->_whereTables
as $tableName => $clause) {
1325 if (!empty($locationTypeJoin[$tableName])) {
1326 $locClause[] = $locationTypeJoin[$tableName];
1330 if (!empty($locClause)) {
1331 $this->_whereTables
[$locationTypeName] = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locClause) . " )";
1336 if (!empty($addressCustomFieldIds)) {
1337 $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds);
1338 foreach ($addressCustomFieldIds as $cfID => $locTypeName) {
1339 foreach ($locTypeName as $name => $dnc) {
1340 $this->_locationSpecificCustomFields
[$cfID] = [$name, array_search($name, $locationTypes)];
1341 $fieldName = "$name-custom_{$cfID}";
1342 $tName = "$name-address-custom-{$cfID}";
1343 $aName = "`$name-address-custom-{$cfID}`";
1344 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1345 $this->_element
["{$tName}_id"] = 1;
1346 $this->_select
[$fieldName] = "`$tName`.{$customQuery->_fields[$cfID]['column_name']} as `{$fieldName}`";
1347 $this->_element
[$fieldName] = 1;
1348 $this->_tables
[$tName] = "\nLEFT JOIN {$customQuery->_fields[$cfID]['table_name']} $aName ON ($aName.entity_id = `$name-address`.id)";
1355 * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.
1357 public function addMultipleElements() {
1358 if (empty($this->_returnProperties
['website'])) {
1361 if (!is_array($this->_returnProperties
['website'])) {
1365 foreach ($this->_returnProperties
['website'] as $key => $elements) {
1366 foreach ($elements as $elementFullName => $dontCare) {
1367 $tName = "website-{$key}-{$elementFullName}";
1368 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
1369 $this->_select
["{$tName}"] = "`$tName`.url as `{$tName}`";
1370 $this->_element
["{$tName}_id"] = 1;
1371 $this->_element
["{$tName}"] = 1;
1373 $type = "website-{$key}-website_type_id";
1374 $this->_select
[$type] = "`$tName`.website_type_id as `{$type}`";
1375 $this->_element
[$type] = 1;
1376 $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website `$tName` ON (`$tName`.contact_id = contact_a.id AND `$tName`.website_type_id = $key )";
1382 * Generate the query based on what type of query we need.
1384 * @param bool $count
1385 * @param bool $sortByChar
1386 * @param bool $groupContacts
1387 * @param bool $onlyDeleted
1390 * sql query parts as an array
1392 public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE, $onlyDeleted = FALSE) {
1393 // build permission clause
1394 $this->generatePermissionClause($onlyDeleted, $count);
1397 if (isset($this->_rowCountClause
)) {
1398 $select = "SELECT {$this->_rowCountClause}";
1400 elseif (isset($this->_distinctComponentClause
)) {
1401 // we add distinct to get the right count for components
1402 // for the more complex result set, we use GROUP BY the same id
1404 $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount";
1407 $select = 'SELECT count(DISTINCT contact_a.id) as rowCount';
1409 $from = $this->_simpleFromClause
;
1410 if ($this->_useDistinct
) {
1411 $this->_useGroupBy
= TRUE;
1414 elseif ($sortByChar) {
1415 // @fixme add the deprecated warning back in (it breaks CRM_Contact_SelectorTest::testSelectorQuery)
1416 // CRM_Core_Error::deprecatedFunctionWarning('sort by char is deprecated - use alphabetQuery method');
1417 $select = 'SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name';
1418 $from = $this->_simpleFromClause
;
1420 elseif ($groupContacts) {
1421 $select = 'SELECT contact_a.id as id';
1422 if ($this->_useDistinct
) {
1423 $this->_useGroupBy
= TRUE;
1425 $from = $this->_simpleFromClause
;
1428 if (!empty($this->_paramLookup
['group'])) {
1430 list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup
['group'][0];
1432 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
1433 $this->_paramLookup
['group'][0][1] = key($value);
1436 // Presumably the lines below come into manage groups screen.
1437 // make sure there is only one element
1438 // this is used when we are running under smog and need to know
1439 // how the contact was added (CRM-1203)
1440 $groups = (array) CRM_Utils_Array
::value($this->_paramLookup
['group'][0][1], $this->_paramLookup
['group'][0][2], $this->_paramLookup
['group'][0][2]);
1441 if ((count($this->_paramLookup
['group']) == 1) &&
1442 (count($groups) == 1)
1444 $groupId = $groups[0];
1446 //check if group is saved search
1447 $group = new CRM_Contact_BAO_Group();
1448 $group->id
= $groupId;
1451 if (!isset($group->saved_search_id
)) {
1452 $tbName = "civicrm_group_contact";
1453 // CRM-17254 don't retrieve extra fields if contact_id is specifically requested
1454 // as this will add load to an intentionally light query.
1455 // ideally this code would be removed as it appears to be to support CRM-1203
1456 // and passing in the required returnProperties from the url would
1457 // make more sense that globally applying the requirements of one form.
1458 if (($this->_returnProperties
!= ['contact_id'])) {
1459 $this->_select
['group_contact_id'] = "$tbName.id as group_contact_id";
1460 $this->_element
['group_contact_id'] = 1;
1461 $this->_select
['status'] = "$tbName.status as status";
1462 $this->_element
['status'] = 1;
1466 $this->_useGroupBy
= TRUE;
1468 if ($this->_useDistinct
&& !isset($this->_distinctComponentClause
)) {
1469 if (!($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
)) {
1471 $this->_select
['contact_id'] = 'contact_a.id as contact_id';
1472 $this->_useDistinct
= FALSE;
1473 $this->_useGroupBy
= TRUE;
1477 $select = $this->getSelect();
1478 $from = $this->_fromClause
;
1482 if (!empty($this->_whereClause
)) {
1483 $where = "WHERE {$this->_whereClause}";
1486 if (!empty($this->_permissionWhereClause
) && empty($this->_displayRelationshipType
)) {
1487 if (empty($where)) {
1488 $where = "WHERE $this->_permissionWhereClause";
1491 $where = "$where AND $this->_permissionWhereClause";
1496 if (!empty($this->_having
)) {
1497 foreach ($this->_having
as $havingSets) {
1498 foreach ($havingSets as $havingSet) {
1499 $havingValue[] = $havingSet;
1502 $having = ' HAVING ' . implode(' AND ', $havingValue);
1505 // if we are doing a transform, do it here
1506 // use the $from, $where and $having to get the contact ID
1507 if ($this->_displayRelationshipType
) {
1508 $this->filterRelatedContacts($from, $where, $having);
1511 return [$select, $from, $where, $having];
1515 * Get where values from the parameters.
1517 * @param string $name
1518 * @param mixed $grouping
1522 public function getWhereValues($name, $grouping) {
1524 foreach ($this->_params
as $values) {
1525 if ($values[0] == $name && $values[3] == $grouping) {
1536 * @param bool $relative
1537 * @param string $from
1540 public static function fixDateValues($relative, &$from, &$to) {
1542 list($from, $to) = CRM_Utils_Date
::getFromTo($relative, $from, $to);
1547 * Convert values from form-appropriate to query-object appropriate.
1549 * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax.
1550 * So, ideally we would convert all fields to look like
1553 * // Set the operator for legacy reasons, but it is ignored
1555 * // array in sql filter syntax
1556 * 2 => array('BETWEEN' => array(1,60),
1561 * There are some examples of the syntax in
1562 * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship
1564 * More notes at CRM_Core_DAO::createSQLFilter
1566 * and a list of supported operators in CRM_Core_DAO
1568 * @param array $formValues
1569 * @param int $wildcard
1570 * @param bool $useEquals
1572 * @param string $apiEntity
1574 * @param array $entityReferenceFields
1575 * Field names of any entity reference fields (which will need reformatting to IN syntax).
1579 public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL,
1580 $entityReferenceFields = []) {
1582 if (empty($formValues)) {
1586 self
::filterCountryFromValuesIfStateExists($formValues);
1587 // We shouldn't have to whitelist fields to not hack but here we are, for now.
1588 $nonLegacyDateFields = [
1589 'participant_register_date_relative',
1590 'receive_date_relative',
1591 'pledge_end_date_relative',
1592 'pledge_create_date_relative',
1593 'pledge_start_date_relative',
1594 'pledge_payment_scheduled_date_relative',
1595 'membership_join_date_relative',
1596 'membership_start_date_relative',
1597 'membership_end_date_relative',
1599 // Handle relative dates first
1600 foreach (array_keys($formValues) as $id) {
1602 !in_array($id, $nonLegacyDateFields) && (
1603 preg_match('/_date_relative$/', $id) ||
1604 $id == 'event_relative' ||
1605 $id == 'case_from_relative' ||
1606 $id == 'case_to_relative')
1608 if ($id == 'event_relative') {
1609 $fromRange = 'event_start_date_low';
1610 $toRange = 'event_end_date_high';
1612 elseif ($id == 'case_from_relative') {
1613 $fromRange = 'case_from_start_date_low';
1614 $toRange = 'case_from_start_date_high';
1616 elseif ($id == 'case_to_relative') {
1617 $fromRange = 'case_to_end_date_low';
1618 $toRange = 'case_to_end_date_high';
1621 $dateComponent = explode('_date_relative', $id);
1622 $fromRange = "{$dateComponent[0]}_date_low";
1623 $toRange = "{$dateComponent[0]}_date_high";
1626 if (array_key_exists($fromRange, $formValues) && array_key_exists($toRange, $formValues)) {
1627 CRM_Contact_BAO_Query
::fixDateValues($formValues[$id], $formValues[$fromRange], $formValues[$toRange]);
1632 foreach ($formValues as $id => $values) {
1633 if (self
::isAlreadyProcessedForQueryFormat($values)) {
1634 $params[] = $values;
1638 self
::legacyConvertFormValues($id, $values);
1640 // The form uses 1 field to represent two db fields
1641 if ($id == 'contact_type' && $values && (!is_array($values) ||
!array_intersect(array_keys($values), CRM_Core_DAO
::acceptedSQLOperators()))) {
1644 foreach ((array) $values as $key => $type) {
1645 $types = explode('__', is_numeric($type) ?
$key : $type, 2);
1646 $contactType[$types[0]] = $types[0];
1647 // Add sub-type if specified
1648 if (!empty($types[1])) {
1649 $subType[$types[1]] = $types[1];
1652 $params[] = ['contact_type', 'IN', $contactType, 0, 0];
1654 $params[] = ['contact_sub_type', 'IN', $subType, 0, 0];
1657 elseif ($id == 'privacy') {
1658 if (is_array($formValues['privacy'])) {
1659 $op = !empty($formValues['privacy']['do_not_toggle']) ?
'=' : '!=';
1660 foreach ($formValues['privacy'] as $key => $value) {
1662 $params[] = [$key, $op, $value, 0, 0];
1667 elseif ($id == 'email_on_hold') {
1668 if ($onHoldValue = CRM_Utils_Array
::value('email_on_hold', $formValues)) {
1669 // onHoldValue should be 0 or 1 or an array. Some legacy groups may hold ''
1670 // so in 5.11 we have an extra if that should become redundant over time.
1671 // https://lab.civicrm.org/dev/core/issues/745
1672 // @todo this renaming of email_on_hold to on_hold needs revisiting
1673 // it preceeds recent changes but causes the default not to reload.
1674 $onHoldValue = array_filter((array) $onHoldValue, 'is_numeric');
1675 if (!empty($onHoldValue)) {
1676 $params[] = ['on_hold', 'IN', $onHoldValue, 0, 0];
1680 elseif (substr($id, 0, 7) == 'custom_'
1682 substr($id, -5, 5) == '_from'
1683 ||
substr($id, -3, 3) == '_to'
1686 self
::convertCustomRelativeFields($formValues, $params, $values, $id);
1689 !in_array($id, $nonLegacyDateFields) && (
1690 preg_match('/_date_relative$/', $id) ||
1691 $id == 'event_relative' ||
1692 $id == 'case_from_relative' ||
1693 $id == 'case_to_relative')
1695 // Already handled in previous loop
1698 elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') !=
1700 $params[] = [$id, 'IN', explode(',', $values), 0, 0];
1703 $values = CRM_Contact_BAO_Query
::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity);
1708 $params[] = $values;
1715 * Function to support legacy format for groups and tags.
1718 * @param array|int $values
1721 public static function legacyConvertFormValues($id, &$values) {
1727 'membership_type_id',
1728 'membership_status_id',
1730 if (in_array($id, $legacyElements) && is_array($values)) {
1731 // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
1732 // as per the recent Search fixes $values need to be in standard array(id1, id2) format
1733 $values = CRM_Utils_Array
::convertCheckboxFormatToArray($values);
1738 * Fix values from query from/to something no-one cared enough to document.
1741 * @param array $values
1742 * @param int $wildcard
1743 * @param bool $useEquals
1745 * @param string $apiEntity
1747 * @return array|null
1749 public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) {
1750 // skip a few search variables
1751 static $skipWhere = NULL;
1752 static $likeNames = NULL;
1755 // Change camelCase EntityName to lowercase with underscores
1756 $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity);
1758 // check if $value is in OK (Operator as Key) format as used by Get API
1759 if (CRM_Utils_System
::isNull($values)) {
1771 'display_relationship_type',
1775 if (in_array($id, $skipWhere) ||
1776 substr($id, 0, 4) == '_qf_' ||
1777 substr($id, 0, 7) == 'hidden_'
1783 (substr($id, 0, strlen($apiEntity)) != $apiEntity) &&
1784 (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_') &&
1785 (substr($id, 0, 7) != 'custom_')
1787 $id = $apiEntity . '_' . $id;
1791 $likeNames = ['sort_name', 'email', 'note', 'display_name'];
1794 // email comes in via advanced search
1795 // so use wildcard always
1796 if ($id == 'email') {
1800 if (!$useEquals && in_array($id, $likeNames)) {
1801 $result = [$id, 'LIKE', $values, 0, 1];
1803 elseif (is_string($values) && strpos($values, '%') !== FALSE) {
1804 $result = [$id, 'LIKE', $values, 0, 0];
1806 elseif ($id == 'contact_type' ||
1807 (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO
::acceptedSQLOperators(), TRUE))
1809 $result = [$id, 'IN', $values, 0, $wildcard];
1812 $result = [$id, '=', $values, 0, $wildcard];
1819 * Get the where clause for a single field.
1821 * @param array $values
1822 * @param bool $isForcePrimaryOnly
1824 * @throws \CRM_Core_Exception
1826 public function whereClauseSingle(&$values, $isForcePrimaryOnly = FALSE) {
1827 if ($this->isARelativeDateField($values[0])) {
1828 $this->buildRelativeDateQuery($values);
1831 // @todo also handle _low, _high generically here with if ($query->buildDateRangeQuery($values)) {return}
1833 // do not process custom fields or prefixed contact ids or component params
1834 if (CRM_Core_BAO_CustomField
::getKeyID($values[0]) ||
1835 (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) ||
1836 (substr($values[0], 0, 13) == 'contribution_') ||
1837 (substr($values[0], 0, 6) == 'event_') ||
1838 (substr($values[0], 0, 12) == 'participant_') ||
1839 (substr($values[0], 0, 7) == 'member_') ||
1840 (substr($values[0], 0, 6) == 'grant_') ||
1841 (substr($values[0], 0, 7) == 'pledge_') ||
1842 (substr($values[0], 0, 5) == 'case_') ||
1843 (substr($values[0], 0, 10) == 'financial_') ||
1844 (substr($values[0], 0, 8) == 'payment_') ||
1845 (substr($values[0], 0, 11) == 'membership_')
1850 // skip for hook injected fields / params
1851 $extFields = CRM_Contact_BAO_Query_Hook
::singleton()->getFields();
1852 if (array_key_exists($values[0], $extFields)) {
1856 switch ($values[0]) {
1857 case 'deleted_contacts':
1858 $this->deletedContacts($values);
1861 case 'contact_sub_type':
1862 $this->contactSubType($values);
1867 $this->group($values);
1870 // case tag comes from find contacts
1872 $this->tagSearch($values);
1876 case 'contact_tags':
1877 $this->tag($values);
1882 case 'note_subject':
1883 $this->notes($values);
1887 $this->ufUser($values);
1891 case 'display_name':
1892 $this->sortName($values);
1896 case 'postal_greeting':
1897 case 'email_greeting':
1898 $this->greetings($values);
1903 $this->email($values, $isForcePrimaryOnly);
1906 case 'phone_numeric':
1907 $this->phone_numeric($values);
1910 case 'phone_phone_type_id':
1911 case 'phone_location_type_id':
1912 $this->phone_option_group($values);
1915 case 'street_address':
1916 $this->street_address($values);
1919 case 'street_number':
1920 $this->street_number($values);
1923 case 'sortByCharacter':
1924 $this->sortByCharacter($values);
1927 case 'location_type':
1928 $this->locationType($values);
1932 $this->county($values);
1935 case 'state_province':
1936 case 'state_province_id':
1937 case 'state_province_name':
1938 $this->stateProvince($values);
1943 $this->country($values, FALSE);
1947 case 'postal_code_low':
1948 case 'postal_code_high':
1949 $this->postalCode($values);
1952 case 'activity_date':
1953 case 'activity_date_low':
1954 case 'activity_date_high':
1955 case 'activity_date_time_low':
1956 case 'activity_date_time_high':
1957 case 'activity_role':
1958 case 'activity_status_id':
1959 case 'activity_status':
1960 case 'activity_priority':
1961 case 'activity_priority_id':
1962 case 'followup_parent_id':
1964 case 'source_contact_id':
1965 case 'activity_text':
1966 case 'activity_option':
1967 case 'test_activities':
1968 case 'activity_type_id':
1969 case 'activity_type':
1970 case 'activity_survey_id':
1971 case 'activity_tags':
1972 case 'activity_taglist':
1973 case 'activity_test':
1974 case 'activity_campaign_id':
1975 case 'activity_engagement_level':
1977 case 'activity_result':
1978 case 'source_contact':
1979 CRM_Activity_BAO_Query
::whereClauseSingle($values, $this);
1984 case 'birth_date_low':
1985 case 'birth_date_high':
1986 case 'deceased_date_low':
1987 case 'deceased_date_high':
1988 $this->demographics($values);
1991 case 'age_asof_date':
1992 // handled by demographics
1995 case 'log_date_low':
1996 case 'log_date_high':
1997 $this->modifiedDates($values);
2001 $this->changeLog($values);
2004 case 'do_not_phone':
2005 case 'do_not_email':
2008 case 'do_not_trade':
2010 $this->privacy($values);
2013 case 'privacy_options':
2014 $this->privacyOptions($values);
2017 case 'privacy_operator':
2018 case 'privacy_toggle':
2019 // these are handled by privacy options
2022 case 'preferred_communication_method':
2023 $this->preferredCommunication($values);
2026 case 'relation_type_id':
2027 case 'relation_start_date_high':
2028 case 'relation_start_date_low':
2029 case 'relation_end_date_high':
2030 case 'relation_end_date_low':
2031 case 'relation_active_period_date_high':
2032 case 'relation_active_period_date_low':
2033 case 'relation_target_name':
2034 case 'relation_status':
2035 case 'relation_date_low':
2036 case 'relation_date_high':
2037 $this->relationship($values);
2038 $this->_relationshipValuesAdded
= TRUE;
2041 case 'task_status_id':
2042 $this->task($values);
2046 // since this case is handled with the above
2049 case 'prox_distance':
2050 CRM_Contact_BAO_ProximityQuery
::process($this, $values);
2053 case 'prox_street_address':
2055 case 'prox_postal_code':
2056 case 'prox_state_province_id':
2057 case 'prox_country_id':
2058 case 'prox_geo_code_1':
2059 case 'prox_geo_code_2':
2060 // handled by the proximity_distance clause
2064 $this->restWhere($values);
2070 * Given a list of conditions in params generate the required where clause.
2072 * @param bool $isForcePrimaryEmailOnly
2075 * @throws \CRM_Core_Exception
2077 public function whereClause($isForcePrimaryEmailOnly = NULL) {
2078 $this->_where
[0] = [];
2079 $this->_qill
[0] = [];
2081 $this->includeContactIds();
2082 if (!empty($this->_params
)) {
2083 foreach (array_keys($this->_params
) as $id) {
2084 if (empty($this->_params
[$id][0])) {
2087 // check for both id and contact_id
2088 if ($this->_params
[$id][0] == 'id' ||
$this->_params
[$id][0] == 'contact_id') {
2089 $this->_where
[0][] = self
::buildClause("contact_a.id", $this->_params
[$id][1], $this->_params
[$id][2]);
2090 $field = CRM_Utils_Array
::value('id', $this->_fields
);
2091 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(
2092 'CRM_Contact_BAO_Contact',
2094 $this->_params
[$id][2],
2095 $this->_params
[$id][1]
2097 $this->_qill
[0][] = ts("%1 %2 %3", [
2098 1 => $field['title'],
2104 $this->whereClauseSingle($this->_params
[$id], $isForcePrimaryEmailOnly);
2108 CRM_Core_Component
::alterQuery($this, 'where');
2110 CRM_Contact_BAO_Query_Hook
::singleton()->alterSearchQuery($this, 'where');
2113 if ($this->_customQuery
) {
2114 // Added following if condition to avoid the wrong value display for 'my account' / any UF info.
2115 // Hope it wont affect the other part of civicrm.. if it does please remove it.
2116 if (!empty($this->_customQuery
->_where
)) {
2117 $this->_where
= CRM_Utils_Array
::crmArrayMerge($this->_where
, $this->_customQuery
->_where
);
2120 $this->_qill
= CRM_Utils_Array
::crmArrayMerge($this->_qill
, $this->_customQuery
->_qill
);
2127 if (!empty($this->_where
)) {
2128 foreach ($this->_where
as $grouping => $values) {
2129 if ($grouping > 0 && !empty($values)) {
2130 $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) ';
2135 if (!empty($this->_where
[0])) {
2136 $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where
[0]) . ' ) ';
2138 if (!empty($clauses)) {
2139 $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
2142 if ($validClauses > 1) {
2143 $this->_useDistinct
= TRUE;
2147 return implode(' AND ', $andClauses);
2151 * Generate where clause for any parameters not already handled.
2153 * @param array $values
2157 public function restWhere(&$values) {
2158 $name = CRM_Utils_Array
::value(0, $values);
2159 $op = CRM_Utils_Array
::value(1, $values);
2160 $value = CRM_Utils_Array
::value(2, $values);
2161 $grouping = CRM_Utils_Array
::value(3, $values);
2162 $wildcard = CRM_Utils_Array
::value(4, $values);
2164 if (isset($grouping) && empty($this->_where
[$grouping])) {
2165 $this->_where
[$grouping] = [];
2168 $multipleFields = ['url'];
2170 //check if the location type exists for fields
2172 $locType = explode('-', $name);
2174 if (!in_array($locType[0], $multipleFields)) {
2175 //add phone type if exists
2176 if (isset($locType[2]) && $locType[2]) {
2177 $locType[2] = CRM_Core_DAO
::escapeString($locType[2]);
2181 $field = CRM_Utils_Array
::value($name, $this->_fields
);
2184 $field = CRM_Utils_Array
::value($locType[0], $this->_fields
);
2187 // Strip any trailing _high & _low that might be appended.
2188 $realFieldName = str_replace(['_high', '_low'], '', $name);
2189 if (isset($this->_fields
[$realFieldName])) {
2190 $field = $this->_fields
[str_replace(['_high', '_low'], '', $realFieldName)];
2191 $this->dateQueryBuilder($values, $field['table_name'], $realFieldName, $realFieldName, $field['title']);
2199 $locationType = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
2200 if (isset($locType[1]) && is_numeric($locType[1])) {
2201 $lType = $locationType[$locType[1]];
2204 $field['title'] .= " ($lType)";
2207 if (substr($name, 0, 14) === 'state_province') {
2208 if (isset($locType[1]) && is_numeric($locType[1])) {
2210 $aName = "{$lType}-address";
2211 $where = "`$aName`.state_province_id";
2214 $where = "civicrm_address.state_province_id";
2217 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value);
2218 $this->_tables
[$aName] = $this->_whereTables
[$aName] = 1;
2219 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
2220 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['title'], 2 => $qillop, 3 => $qillVal]);
2222 elseif (!empty($field['pseudoconstant'])) {
2223 $this->optionValueQuery(
2224 $name, $op, $value, $grouping,
2225 'CRM_Contact_DAO_Contact',
2231 if ($name == 'gender_id') {
2232 self
::$_openedPanes[ts('Demographics')] = TRUE;
2235 elseif (substr($name, 0, 7) === 'country' ||
substr($name, 0, 6) === 'county') {
2236 $name = (substr($name, 0, 7) === 'country') ?
"country_id" : "county_id";
2237 if (isset($locType[1]) && is_numeric($locType[1])) {
2239 $aName = "{$lType}-address";
2240 $where = "`$aName`.$name";
2243 $where = "civicrm_address.$name";
2246 $this->_where
[$grouping][] = self
::buildClause($where, $op, $value, 'Positive');
2247 $this->_tables
[$aName] = $this->_whereTables
[$aName] = 1;
2249 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
2250 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $field['title'], 2 => $qillop, 3 => $qillVal]);
2252 elseif ($name === 'world_region') {
2253 $this->optionValueQuery(
2254 $name, $op, $value, $grouping,
2262 elseif ($name === 'is_deceased') {
2263 $this->setQillAndWhere($name, $op, $value, $grouping, $field);
2264 self
::$_openedPanes[ts('Demographics')] = TRUE;
2266 elseif ($name === 'created_date' ||
$name === 'modified_date' ||
$name === 'deceased_date' ||
$name === 'birth_date') {
2267 $appendDateTime = TRUE;
2268 if ($name === 'deceased_date' ||
$name === 'birth_date') {
2269 $appendDateTime = FALSE;
2270 self
::$_openedPanes[ts('Demographics')] = TRUE;
2272 $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime);
2274 elseif ($name === 'contact_id') {
2275 if (is_int($value)) {
2276 $this->_where
[$grouping][] = self
::buildClause($field['where'], $op, $value);
2277 $this->_qill
[$grouping][] = "$field[title] $op $value";
2280 elseif ($name === 'name') {
2281 $value = CRM_Core_DAO
::escapeString($value);
2284 $value = self
::getWildCardedValue($wildcard, $op, $value);
2286 CRM_Core_Error
::deprecatedFunctionWarning('Untested code path');
2287 // @todo it's likely this code path is obsolete / never called. It is definitely not
2288 // passed through in our test suite.
2289 $this->_where
[$grouping][] = self
::buildClause($field['where'], $op, "'$value'");
2290 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2292 elseif ($name === 'current_employer') {
2295 $value = self
::getWildCardedValue($wildcard, $op, $value);
2297 $ceWhereClause = self
::buildClause("contact_a.organization_name", $op,
2300 $ceWhereClause .= " AND contact_a.contact_type = 'Individual'";
2301 $this->_where
[$grouping][] = $ceWhereClause;
2302 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2304 elseif ($name === 'email_greeting') {
2305 CRM_Core_Error
::deprecatedFunctionWarning('pass in email_greeting_id or email_greeting_display');
2306 $filterCondition = ['greeting_type' => 'email_greeting'];
2307 $this->optionValueQuery(
2308 $name, $op, $value, $grouping,
2309 CRM_Core_PseudoConstant
::greeting($filterCondition),
2311 ts('Email Greeting')
2314 elseif ($name === 'postal_greeting') {
2315 CRM_Core_Error
::deprecatedFunctionWarning('pass in postal_greeting_id or postal_greeting_display');
2316 $filterCondition = ['greeting_type' => 'postal_greeting'];
2317 $this->optionValueQuery(
2318 $name, $op, $value, $grouping,
2319 CRM_Core_PseudoConstant
::greeting($filterCondition),
2321 ts('Postal Greeting')
2324 elseif ($name === 'addressee') {
2325 CRM_Core_Error
::deprecatedFunctionWarning('pass in addressee_id or addressee_display');
2326 $filterCondition = ['greeting_type' => 'addressee'];
2327 $this->optionValueQuery(
2328 $name, $op, $value, $grouping,
2329 CRM_Core_PseudoConstant
::greeting($filterCondition),
2334 elseif (substr($name, 0, 4) === 'url-') {
2335 $tName = 'civicrm_website';
2336 $this->_whereTables
[$tName] = $this->_tables
[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )";
2337 $value = CRM_Core_DAO
::escapeString($value);
2340 $value = self
::getWildCardedValue($wildcard, $op, $value);
2343 $this->_where
[$grouping][] = $d = self
::buildClause('civicrm_website.url', $op, $value);
2344 $this->_qill
[$grouping][] = "$field[title] $op \"$value\"";
2346 elseif ($name === 'contact_is_deleted') {
2347 $this->setQillAndWhere('is_deleted', $op, $value, $grouping, $field);
2349 elseif (!empty($field['where'])) {
2351 if (!empty($field['type'])) {
2352 $type = CRM_Utils_Type
::typeToString($field['type']);
2355 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2357 if (isset($locType[1]) &&
2358 is_numeric($locType[1])
2362 //get the location name
2363 list($tName, $fldName) = self
::getLocationTableName($field['where'], $locType);
2364 $fieldName = "`$tName`.$fldName";
2366 // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
2367 $this->_tables
[$tName] = $this->_whereTables
[$tName] = 1;
2371 if ($tableName == 'civicrm_contact') {
2372 $fieldName = "contact_a.{$fieldName}";
2375 $fieldName = $field['where'];
2379 list($qillop, $qillVal) = self
::buildQillForFieldValue(NULL, $field['title'], $value, $op);
2380 $this->_qill
[$grouping][] = ts("%1 %2 %3", [
2381 1 => $field['title'],
2383 3 => (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) ?
$qillVal : "'$qillVal'",
2386 if (is_array($value)) {
2387 // traditionally an array being passed has been a fatal error. We can take advantage of this to add support
2388 // for api style operators for functions that hit this point without worrying about regression
2389 // (the previous comments indicated the condition for hitting this point were unknown
2390 // per CRM-14743 we are adding modified_date & created_date operator support
2391 $operations = array_keys($value);
2392 foreach ($operations as $operator) {
2393 if (!in_array($operator, CRM_Core_DAO
::acceptedSQLOperators())) {
2394 //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators
2395 //so this condition will satisfy the search for now
2396 if (strpos($op, 'IN') !== FALSE) {
2397 $value = [$op => $value];
2399 // we don't know when this might happen
2401 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", [1 => $operator]));
2405 $this->_where
[$grouping][] = CRM_Core_DAO
::createSQLFilter($fieldName, $value, $type);
2410 $value = self
::getWildCardedValue($wildcard, $op, $value);
2413 $this->_where
[$grouping][] = self
::buildClause($fieldName, $op, $value, $type);
2417 if ($setTables && isset($field['where'])) {
2418 list($tableName, $fieldName) = explode('.', $field['where'], 2);
2419 if (isset($tableName)) {
2420 $this->_tables
[$tableName] = 1;
2421 $this->_whereTables
[$tableName] = 1;
2433 public static function getLocationTableName(&$where, &$locType) {
2434 if (isset($locType[1]) && is_numeric($locType[1])) {
2435 list($tbName, $fldName) = explode(".", $where);
2437 //get the location name
2438 $locationType = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
2439 $specialFields = ['email', 'im', 'phone', 'openid', 'phone_ext'];
2440 if (in_array($locType[0], $specialFields)) {
2441 //hack to fix / special handing for phone_ext
2442 if ($locType[0] == 'phone_ext') {
2443 $locType[0] = 'phone';
2445 if (isset($locType[2]) && $locType[2]) {
2446 $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}";
2449 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2452 elseif (in_array($locType[0],
2457 'street_number_suffix',
2459 'supplemental_address_1',
2460 'supplemental_address_2',
2461 'supplemental_address_3',
2464 'postal_code_suffix',
2470 //fix for search by profile with address fields.
2471 $tName = "{$locationType[$locType[1]]}-address";
2473 elseif (in_array($locType[0],
2481 $tName = "{$locationType[$locType[1]]}-email";
2483 elseif ($locType[0] == 'provider_id') {
2484 $tName = "{$locationType[$locType[1]]}-im";
2486 elseif ($locType[0] == 'openid') {
2487 $tName = "{$locationType[$locType[1]]}-openid";
2490 $tName = "{$locationType[$locType[1]]}-{$locType[0]}";
2492 $tName = str_replace(' ', '_', $tName);
2493 return [$tName, $fldName];
2495 CRM_Core_Error
::fatal();
2499 * Given a result dao, extract the values and return that array
2501 * @param CRM_Core_DAO $dao
2504 * values for this query
2506 public function store($dao) {
2509 foreach ($this->_element
as $key => $dontCare) {
2510 if (property_exists($dao, $key)) {
2511 if (strpos($key, '-') !== FALSE) {
2512 $values = explode('-', $key);
2513 $lastElement = array_pop($values);
2515 $cnt = count($values);
2517 foreach ($values as $v) {
2518 if (!array_key_exists($v, $current)) {
2521 //bad hack for im_provider
2522 if ($lastElement == 'provider_id') {
2523 if ($count < $cnt) {
2524 $current = &$current[$v];
2527 $lastElement = "{$v}_{$lastElement}";
2531 $current = &$current[$v];
2536 $current[$lastElement] = $dao->$key;
2539 $value[$key] = $dao->$key;
2547 * Getter for tables array.
2551 public function tables() {
2552 return $this->_tables
;
2556 * Sometimes used to create the from clause, but, not reliably, set
2557 * this AND set tables.
2559 * It's unclear the intent - there is a 'simpleFrom' clause which
2560 * takes whereTables into account & a fromClause which doesn't.
2562 * logic may have eroded?
2566 public function whereTables() {
2567 return $this->_whereTables
;
2571 * Generate the where clause (used in match contacts and permissions)
2573 * @param array $params
2574 * @param array $fields
2575 * @param array $tables
2576 * @param $whereTables
2577 * @param bool $strict
2581 public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) {
2582 $query = new CRM_Contact_BAO_Query($params, NULL, $fields,
2586 $tables = array_merge($query->tables(), $tables);
2587 $whereTables = array_merge($query->whereTables(), $whereTables);
2589 return $query->_whereClause
;
2593 * Create the from clause.
2595 * @param array $tables
2596 * Tables that need to be included in this from clause. If null,
2597 * return mimimal from clause (i.e. civicrm_contact).
2598 * @param array $inner
2599 * Tables that should be inner-joined.
2600 * @param array $right
2601 * Tables that should be right-joined.
2602 * @param bool $primaryLocation
2603 * Search on primary location. See note below.
2605 * Determines search mode based on bitwise MODE_* constants.
2606 * @param string|NULL $apiEntity
2607 * Determines search mode based on entity by string.
2609 * The $primaryLocation flag only seems to be used when
2610 * locationType() has been called. This may be a search option
2611 * exposed, or perhaps it's a "search all details" approach which
2612 * predates decoupling of location types and primary fields?
2614 * @see https://issues.civicrm.org/jira/browse/CRM-19967
2619 public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1, $apiEntity = NULL) {
2621 $from = ' FROM civicrm_contact contact_a';
2622 if (empty($tables)) {
2626 if (!empty($tables['civicrm_worldregion'])) {
2627 $tables = array_merge(['civicrm_country' => 1], $tables);
2630 if ((!empty($tables['civicrm_state_province']) ||
!empty($tables['civicrm_country']) ||
2631 CRM_Utils_Array
::value('civicrm_county', $tables)) && empty($tables['civicrm_address'])) {
2632 $tables = array_merge(['civicrm_address' => 1],
2637 // add group_contact and group table is subscription history is present
2638 if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) {
2639 $tables = array_merge([
2640 'civicrm_group' => 1,
2641 'civicrm_group_contact' => 1,
2647 // to handle table dependencies of components
2648 CRM_Core_Component
::tableNames($tables);
2649 // to handle table dependencies of hook injected tables
2650 CRM_Contact_BAO_Query_Hook
::singleton()->setTableDependency($tables);
2652 //format the table list according to the weight
2653 $info = CRM_Core_TableHierarchy
::info();
2655 foreach ($tables as $key => $value) {
2657 if (strpos($key, '-') !== FALSE) {
2658 $keyArray = explode('-', $key);
2659 $k = CRM_Utils_Array
::value('civicrm_' . $keyArray[1], $info, 99);
2661 elseif (strpos($key, '_') !== FALSE) {
2662 $keyArray = explode('_', $key);
2663 if (is_numeric(array_pop($keyArray))) {
2664 $k = CRM_Utils_Array
::value(implode('_', $keyArray), $info, 99);
2667 $k = CRM_Utils_Array
::value($key, $info, 99);
2671 $k = CRM_Utils_Array
::value($key, $info, 99);
2673 $tempTable[$k . ".$key"] = $key;
2677 foreach ($tempTable as $key) {
2678 $newTables[$key] = $tables[$key];
2681 $tables = $newTables;
2683 foreach ($tables as $name => $value) {
2688 if (!empty($inner[$name])) {
2691 elseif (!empty($right[$name])) {
2699 // if there is already a join statement in value, use value itself
2700 if (strpos($value, 'JOIN')) {
2701 $from .= " $value ";
2704 $from .= " $side JOIN $name ON ( $value ) ";
2709 $from .= ' ' . trim(self
::getEntitySpecificJoins($name, $mode, $side, $primaryLocation)) . ' ';
2715 * Get join statements for the from clause depending on entity type
2717 * @param string $name
2719 * @param string $side
2720 * @param string $primaryLocation
2723 protected static function getEntitySpecificJoins($name, $mode, $side, $primaryLocation) {
2724 $limitToPrimaryClause = $primaryLocation ?
"AND {$name}.is_primary = 1" : '';
2726 case 'civicrm_address':
2727 //CRM-14263 further handling of address joins further down...
2728 return " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id {$limitToPrimaryClause} )";
2730 case 'civicrm_state_province':
2731 // This is encountered when doing an export after having applied a 'sort' - it pretty much implies primary
2732 // but that will have been implied-in by the calling function.
2733 // test cover in testContactIDQuery
2734 return " $side JOIN civicrm_state_province ON ( civicrm_address.state_province_id = civicrm_state_province.id )";
2736 case 'civicrm_country':
2737 // This is encountered when doing an export after having applied a 'sort' - it pretty much implies primary
2738 // but that will have been implied-in by the calling function.
2739 // test cover in testContactIDQuery
2740 return " $side JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id )";
2742 case 'civicrm_phone':
2743 return " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id {$limitToPrimaryClause}) ";
2745 case 'civicrm_email':
2746 return " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id {$limitToPrimaryClause})";
2749 return " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id {$limitToPrimaryClause}) ";
2752 $from = " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) ";
2753 $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'";
2754 $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)";
2757 case 'civicrm_openid':
2758 return " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id {$limitToPrimaryClause} )";
2760 case 'civicrm_worldregion':
2761 // We can be sure from the calling function that country will already be joined in.
2762 // we really don't need world_region - we could use a pseudoconstant for it.
2763 return " $side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ";
2765 case 'civicrm_location_type':
2766 return " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id ";
2768 case 'civicrm_group':
2769 return " $side JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id ";
2771 case 'civicrm_group_contact':
2772 return " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id ";
2774 case 'civicrm_group_contact_cache':
2775 return " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id ";
2777 case 'civicrm_activity':
2778 case 'civicrm_activity_tag':
2779 case 'activity_type':
2780 case 'activity_status':
2782 case 'civicrm_activity_contact':
2783 case 'source_contact':
2784 case 'activity_priority':
2785 return CRM_Activity_BAO_Query
::from($name, $mode, $side);
2787 case 'civicrm_entity_tag':
2788 $from = " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact'";
2789 return "$from AND civicrm_entity_tag.entity_id = contact_a.id ) ";
2791 case 'civicrm_note':
2792 $from = " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact'";
2793 return "$from AND contact_a.id = civicrm_note.entity_id ) ";
2795 case 'civicrm_subscription_history':
2796 $from = " $side JOIN civicrm_subscription_history";
2797 $from .= " ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id";
2798 return "$from AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id";
2800 case 'civicrm_relationship':
2801 if (self
::$_relType == 'reciprocal') {
2802 if (self
::$_relationshipTempTable) {
2803 // we have a temptable to join on
2804 $tbl = self
::$_relationshipTempTable;
2805 return " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id";
2808 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)";
2809 $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)";
2813 elseif (self
::$_relType == 'b') {
2814 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )";
2815 return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
2818 $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )";
2819 return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
2823 $from = " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')";
2824 return "$from INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)";
2827 return " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id ";
2829 case 'civicrm_grant':
2830 return CRM_Grant_BAO_Query
::from($name, $mode, $side);
2832 case 'civicrm_website':
2833 return " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id ";
2835 case 'civicrm_campaign':
2836 //Move to default case if not in either mode.
2837 if ($mode & CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
) {
2838 return CRM_Contribute_BAO_Query
::from($name, $mode, $side);
2840 elseif ($mode & CRM_Contact_BAO_Query
::MODE_MAILING
) {
2841 return CRM_Mailing_BAO_Query
::from($name, $mode, $side);
2843 elseif ($mode & CRM_Contact_BAO_Query
::MODE_CAMPAIGN
) {
2844 return CRM_Campaign_BAO_Query
::from($name, $mode, $side);
2848 $locationTypeName = '';
2849 if (strpos($name, '-address') != 0) {
2850 $locationTypeName = 'address';
2852 elseif (strpos($name, '-phone') != 0) {
2853 $locationTypeName = 'phone';
2855 elseif (strpos($name, '-email') != 0) {
2856 $locationTypeName = 'email';
2858 elseif (strpos($name, '-im') != 0) {
2859 $locationTypeName = 'im';
2861 elseif (strpos($name, '-openid') != 0) {
2862 $locationTypeName = 'openid';
2865 if ($locationTypeName) {
2866 //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
2867 $parts = explode('-', $name);
2868 $locationTypes = CRM_Core_DAO_Address
::buildOptions('location_type_id', 'validate');
2869 foreach ($locationTypes as $locationTypeID => $locationType) {
2870 if ($parts[0] == str_replace(' ', '_', $locationType)) {
2871 $locationID = $locationTypeID;
2874 $from = " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID ";
2877 $from = CRM_Core_Component
::from($name, $mode, $side);
2879 $from .= CRM_Contact_BAO_Query_Hook
::singleton()->buildSearchfrom($name, $mode, $side);
2886 * WHERE / QILL clause for deleted_contacts
2888 * @param array $values
2890 public function deletedContacts($values) {
2891 list($_, $_, $value, $grouping, $_) = $values;
2893 // *prepend* to the relevant grouping as this is quite an important factor
2894 array_unshift($this->_qill
[$grouping], ts('Search in Trash'));
2899 * Where / qill clause for contact_type
2903 public function contactType(&$values) {
2904 list($name, $op, $value, $grouping, $wildcard) = $values;
2909 // account for search builder mapping multiple values
2910 if (!is_array($value)) {
2911 $values = self
::parseSearchBuilderString($value, 'String');
2912 if (is_array($values)) {
2913 $value = array_flip($values);
2917 if (is_array($value)) {
2918 foreach ($value as $k => $v) {
2923 if (strpos($k, CRM_Core_DAO
::VALUE_SEPARATOR
)) {
2924 list($contactType, $subType) = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $k, 2);
2927 if (!empty($subType)) {
2928 $subTypes[$subType] = 1;
2930 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2935 $contactTypeANDSubType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value, 2);
2936 $contactType = $contactTypeANDSubType[0];
2937 $subType = CRM_Utils_Array
::value(1, $contactTypeANDSubType);
2938 if (!empty($subType)) {
2939 $subTypes[$subType] = 1;
2941 $clause[$contactType] = "'" . CRM_Utils_Type
::escape($contactType, 'String') . "'";
2945 if (!empty($clause)) {
2947 if ($op == 'IN' ||
$op == 'NOT IN') {
2948 $this->_where
[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')';
2951 $type = array_pop($clause);
2952 $this->_where
[$grouping][] = self
::buildClause("contact_a.contact_type", $op, $contactType);
2955 $this->_qill
[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill);
2957 if (!empty($subTypes)) {
2958 $this->includeContactSubTypes($subTypes, $grouping);
2964 * Where / qill clause for contact_sub_type.
2966 * @param array $values
2968 public function contactSubType(&$values) {
2969 list($name, $op, $value, $grouping, $wildcard) = $values;
2970 $this->includeContactSubTypes($value, $grouping, $op);
2978 public function includeContactSubTypes($value, $grouping, $op = 'LIKE') {
2980 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
2982 $value = $value[$op];
2986 $alias = "contact_a.contact_sub_type";
2987 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
2989 $op = str_replace('IN', 'LIKE', $op);
2990 $op = str_replace('=', 'LIKE', $op);
2991 $op = str_replace('!', 'NOT ', $op);
2993 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
2994 $this->_where
[$grouping][] = self
::buildClause($alias, $op, $value, 'String');
2996 elseif (is_array($value)) {
2997 foreach ($value as $k => $v) {
2998 $clause[$k] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($v, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
3002 $clause[$value] = "($alias $op '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($value, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
3005 if (!empty($clause)) {
3006 $this->_where
[$grouping][] = "( " . implode(' OR ', $clause) . " )";
3008 $this->_qill
[$grouping][] = ts('Contact Subtype %1 ', [1 => $qillOperators[$op]]) . implode(' ' . ts('or') . ' ', array_keys($clause));
3012 * Where / qill clause for groups.
3016 * @throws \CRM_Core_Exception
3018 public function group($values) {
3019 list($name, $op, $value, $grouping, $wildcard) = $values;
3021 // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first
3022 if (is_array($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3024 $value = $value[$op];
3026 // Translate EMPTY to NULL as EMPTY is cannot be used in it's intended meaning here
3027 // so has to be 'squashed into' NULL. (ie. group membership cannot be '').
3028 // even one group might equate to multiple when looking at children so IN is simpler.
3029 // @todo - also look at != casting but there are rows below to review.
3032 'NOT EMPTY' => 'NOT NULL',
3035 if (isset($opReplacements[$op])) {
3036 $op = $opReplacements[$op];
3039 if (strpos($op, 'NULL')) {
3043 if (is_array($value) && count($value) > 1) {
3044 if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) {
3045 CRM_Core_Error
::fatal(ts("%1 is not a valid operator", [1 => $op]));
3047 $this->_useDistinct
= TRUE;
3050 if (isset($value)) {
3051 $value = CRM_Utils_Array
::value($op, $value, $value);
3054 if ($name == 'group_type') {
3055 $value = array_keys($this->getGroupsFromTypeCriteria($value));
3058 $regularGroupIDs = $smartGroupIDs = [];
3059 foreach ((array) $value as $id) {
3060 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
3061 $smartGroupIDs[] = (int) $id;
3064 $regularGroupIDs[] = (int) trim($id);
3067 $hasNonSmartGroups = count($regularGroupIDs);
3069 $isNotOp = ($op == 'NOT IN' ||
$op == '!=');
3071 $statusJoinClause = $this->getGroupStatusClause($grouping);
3072 // If we are searching for 'Removed' contacts then despite it being a smart group we only care about the group_contact table.
3073 $isGroupStatusSearch = (!empty($this->getSelectedGroupStatuses($grouping)) && $this->getSelectedGroupStatuses($grouping) !== ["'Added'"]);
3075 if ($hasNonSmartGroups ||
empty($value) ||
$isGroupStatusSearch) {
3076 // include child groups IDs if any
3077 $childGroupIds = (array) CRM_Contact_BAO_Group
::getChildGroupIds($regularGroupIDs);
3078 foreach ($childGroupIds as $key => $id) {
3079 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) {
3080 $smartGroupIDs[] = $id;
3081 unset($childGroupIds[$key]);
3084 if (count($childGroupIds)) {
3085 $regularGroupIDs = array_merge($regularGroupIDs, $childGroupIds);
3088 if (empty($regularGroupIDs)) {
3089 if ($isGroupStatusSearch) {
3090 $regularGroupIDs = $smartGroupIDs;
3092 // If it is still empty we want a filter that blocks all results.
3093 if (empty($regularGroupIDs)) {
3094 $regularGroupIDs = [0];
3098 $gcTable = "`civicrm_group_contact-" . uniqid() . "`";
3099 $joinClause = ["contact_a.id = {$gcTable}.contact_id"];
3101 // @todo consider just casting != to NOT IN & handling both together.
3104 if (!empty($regularGroupIDs)) {
3105 $groupIds = CRM_Utils_Type
::validate(
3106 implode(',', (array) $regularGroupIDs),
3107 'CommaSeparatedIntegers'
3110 $clause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds )";
3113 $clause = self
::buildClause("{$gcTable}.group_id", $op, $regularGroupIDs);
3115 $groupClause[] = "( {$clause} )";
3117 if ($statusJoinClause) {
3118 $joinClause[] = "{$gcTable}.$statusJoinClause";
3120 $this->_tables
[$gcTable] = $this->_whereTables
[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
3123 //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table
3124 // If we are only searching for Removed or Pending contacts we don't need to resolve the smart group
3125 // as that info is in the group_contact table.
3126 if ((count($smartGroupIDs) ||
empty($value)) && !$isGroupStatusSearch) {
3127 $this->_groupUniqueKey
= uniqid();
3128 $this->_groupKeys
[] = $this->_groupUniqueKey
;
3129 $gccTableAlias = "civicrm_group_contact_cache_{$this->_groupUniqueKey}";
3130 $groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, $gccTableAlias, "contact_a", $op);
3131 if (!empty($groupContactCacheClause)) {
3133 $groupIds = implode(',', (array) $smartGroupIDs);
3134 $gcTable = "civicrm_group_contact_{$this->_groupUniqueKey}";
3135 $joinClause = ["contact_a.id = {$gcTable}.contact_id"];
3136 $this->_tables
[$gcTable] = $this->_whereTables
[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
3137 if (strpos($op, 'IN') !== FALSE) {
3138 $groupClause[] = "{$gcTable}.group_id $op ( $groupIds ) AND {$gccTableAlias}.group_id IS NULL";
3141 $groupClause[] = "{$gcTable}.group_id $op $groupIds AND {$gccTableAlias}.group_id IS NULL";
3144 $groupClause[] = " ( {$groupContactCacheClause} ) ";
3148 $and = ($op == 'IS NULL') ?
' AND ' : ' OR ';
3149 if (!empty($groupClause)) {
3150 $this->_where
[$grouping][] = ' ( ' . implode($and, $groupClause) . ' ) ';
3153 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op);
3154 $this->_qill
[$grouping][] = ts("Group(s) %1 %2", [1 => $qillop, 2 => $qillVal]);
3155 if (strpos($op, 'NULL') === FALSE) {
3156 $this->_qill
[$grouping][] = ts("Group Status %1", [1 => implode(' ' . ts('or') . ' ', $this->getSelectedGroupStatuses($grouping))]);
3160 public function getGroupCacheTableKeys() {
3161 return $this->_groupKeys
;
3165 * Function translates selection of group type into a list of groups.
3170 public function getGroupsFromTypeCriteria($value) {
3172 foreach ((array) $value as $groupTypeValue) {
3173 $groupList = CRM_Core_PseudoConstant
::group($groupTypeValue);
3174 $groupIds = ($groupIds +
$groupList);
3180 * Prime smart group cache for smart groups in the search, and join
3181 * civicrm_group_contact_cache table into the query.
3183 * @param array $groups IDs of groups specified in search criteria.
3184 * @param string $tableAlias Alias to use for civicrm_group_contact_cache table.
3185 * @param string $joinTable Table on which to join civicrm_group_contact_cache
3186 * @param string $op SQL comparison operator (NULL, IN, !=, IS NULL, etc.)
3187 * @param string $joinColumn Column in $joinTable on which to join civicrm_group_contact_cache.contact_id
3189 * @return string WHERE clause component for smart group criteria.
3191 public function addGroupContactCache($groups, $tableAlias, $joinTable = "contact_a", $op, $joinColumn = 'id') {
3192 $isNullOp = (strpos($op, 'NULL') !== FALSE);
3193 $groupsIds = $groups;
3195 $operator = ['=' => 'IN', '!=' => 'NOT IN'];
3196 if (!empty($operator[$op]) && is_array($groups)) {
3197 $op = $operator[$op];
3199 if (!$isNullOp && !$groups) {
3202 elseif (strpos($op, 'IN') !== FALSE) {
3203 $groups = [$op => $groups];
3205 elseif (is_array($groups) && count($groups)) {
3206 $groups = ['IN' => $groups];
3209 // Find all the groups that are part of a saved search.
3210 $smartGroupClause = self
::buildClause("id", $op, $groups, 'Int');
3212 SELECT id, cache_date, saved_search_id, children
3214 WHERE $smartGroupClause
3215 AND ( saved_search_id != 0
3216 OR saved_search_id IS NOT NULL
3217 OR children IS NOT NULL )
3220 $group = CRM_Core_DAO
::executeQuery($sql);
3222 while ($group->fetch()) {
3223 $this->_useDistinct
= TRUE;
3224 if (!$this->_smartGroupCache ||
$group->cache_date
== NULL) {
3225 CRM_Contact_BAO_GroupContactCache
::load($group);
3228 if ($group->N
== 0 && $op != 'NOT IN') {
3232 $this->_tables
[$tableAlias] = $this->_whereTables
[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.{$joinColumn} = {$tableAlias}.contact_id ";
3234 if ($op == 'NOT IN') {
3235 return "{$tableAlias}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( " . implode(',', (array) $groupsIds) . " ) )";
3237 return self
::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
3241 * Where / qill clause for cms users
3245 public function ufUser(&$values) {
3246 list($name, $op, $value, $grouping, $wildcard) = $values;
3249 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3251 $this->_qill
[$grouping][] = ts('CMS User');
3253 elseif ($value == 0) {
3254 $this->_tables
['civicrm_uf_match'] = $this->_whereTables
['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id ';
3256 $this->_where
[$grouping][] = " civicrm_uf_match.contact_id IS NULL";
3257 $this->_qill
[$grouping][] = ts('Not a CMS User');
3262 * All tag search specific.
3264 * @param array $values
3266 public function tagSearch(&$values) {
3267 list($name, $op, $value, $grouping, $wildcard) = $values;
3270 $value = "%{$value}%";
3271 $escapedValue = CRM_Utils_Type
::escape("%{$value}%", 'String');
3273 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3274 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3276 $etTable = "`civicrm_entity_tag-" . uniqid() . "`";
3277 $tTable = "`civicrm_tag-" . uniqid() . "`";
3279 if ($useAllTagTypes[2]) {
3280 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3281 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id)
3282 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )";
3284 // search tag in cases
3285 $etCaseTable = "`civicrm_entity_case_tag-" . uniqid() . "`";
3286 $tCaseTable = "`civicrm_case_tag-" . uniqid() . "`";
3287 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3288 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3289 LEFT JOIN civicrm_case
3290 ON (civicrm_case_contact.case_id = civicrm_case.id
3291 AND civicrm_case.is_deleted = 0 )
3292 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id )
3293 LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )";
3294 // search tag in activities
3295 $etActTable = "`civicrm_entity_act_tag-" . uniqid() . "`";
3296 $tActTable = "`civicrm_act_tag-" . uniqid() . "`";
3297 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
3298 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3300 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3301 = " LEFT JOIN civicrm_activity_contact
3302 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3303 LEFT JOIN civicrm_activity
3304 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3305 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3306 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id )
3307 LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )";
3309 $this->_where
[$grouping][] = "({$tTable}.name $op '" . $escapedValue . "' OR {$tCaseTable}.name $op '" . $escapedValue . "' OR {$tActTable}.name $op '" . $escapedValue . "')";
3310 $this->_qill
[$grouping][] = ts('Tag %1 %2', [1 => $tagTypesText[2], 2 => $op]) . ' ' . $value;
3313 $etTable = "`civicrm_entity_tag-" . uniqid() . "`";
3314 $tTable = "`civicrm_tag-" . uniqid() . "`";
3315 $this->_tables
[$etTable] = $this->_whereTables
[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND
3316 {$etTable}.entity_table = 'civicrm_contact' )
3317 LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) ";
3319 $this->_where
[$grouping][] = self
::buildClause("{$tTable}.name", $op, $value, 'String');
3320 $this->_qill
[$grouping][] = ts('Tagged %1', [1 => $op]) . ' ' . $value;
3325 * Where / qill clause for tag.
3327 * @param array $values
3329 public function tag(&$values) {
3330 list($name, $op, $value, $grouping, $wildcard) = $values;
3332 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, ['onlyActive' => FALSE]);
3333 // API/Search Builder format array(operator => array(values))
3334 if (is_array($value)) {
3335 if (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
3337 $value = $value[$op];
3339 if (count($value) > 1) {
3340 $this->_useDistinct
= TRUE;
3344 // implode array, then remove all spaces
3345 $value = str_replace(' ', '', implode(',', (array) $value));
3346 if (!empty($value)) {
3347 $value = CRM_Utils_Type
::validate(
3349 'CommaSeparatedIntegers'
3353 $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping);
3354 $tagTypesText = $this->getWhereValues('tag_types_text', $grouping);
3356 $etTable = "`civicrm_entity_tag-" . uniqid() . "`";
3358 if ($useAllTagTypes[2]) {
3359 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3360 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3362 // search tag in cases
3363 $etCaseTable = "`civicrm_entity_case_tag-" . uniqid() . "`";
3364 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
3365 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
3367 $this->_tables
[$etCaseTable] = $this->_whereTables
[$etCaseTable]
3368 = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
3369 LEFT JOIN civicrm_case
3370 ON (civicrm_case_contact.case_id = civicrm_case.id
3371 AND civicrm_case.is_deleted = 0 )
3372 LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) ";
3373 // search tag in activities
3374 $etActTable = "`civicrm_entity_act_tag-" . uniqid() . "`";
3375 $this->_tables
[$etActTable] = $this->_whereTables
[$etActTable]
3376 = " LEFT JOIN civicrm_activity_contact
3377 ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} )
3378 LEFT JOIN civicrm_activity
3379 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
3380 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )
3381 LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) ";
3384 if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) {
3385 $this->_where
[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)";
3388 $this->_where
[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))";
3392 $this->_tables
[$etTable] = $this->_whereTables
[$etTable]
3393 = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') ";
3396 if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) {
3397 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3398 $op = str_replace('EMPTY', 'NULL', $op);
3399 $this->_where
[$grouping][] = "{$etTable}.tag_id $op";
3401 // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag
3402 elseif ($op == '!=') {
3403 $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') . ")";
3405 elseif ($op == '=' ||
strstr($op, 'IN')) {
3406 $op = ($op == '=') ?
'IN' : $op;
3407 $this->_where
[$grouping][] = "{$etTable}.tag_id $op ( $value )";
3410 $this->_qill
[$grouping][] = ts('Tagged %1 %2', [1 => $qillop, 2 => $qillVal]);
3414 * Where/qill clause for notes
3416 * @param array $values
3418 public function notes(&$values) {
3419 list($name, $op, $value, $grouping, $wildcard) = $values;
3421 $noteOptionValues = $this->getWhereValues('note_option', $grouping);
3422 $noteOption = CRM_Utils_Array
::value('2', $noteOptionValues, '6');
3423 $noteOption = ($name == 'note_body') ?
2 : (($name == 'note_subject') ?
3 : $noteOption);
3425 $this->_useDistinct
= TRUE;
3427 $this->_tables
['civicrm_note'] = $this->_whereTables
['civicrm_note']
3428 = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) ";
3431 $value = CRM_Core_DAO
::escapeString($n);
3433 if (strpos($value, '%') === FALSE) {
3434 $value = "%$value%";
3438 elseif ($op == 'IS NULL' ||
$op == 'IS NOT NULL') {
3444 if ($noteOption %
2 == 0) {
3445 $clauses[] = self
::buildClause('civicrm_note.note', $op, $value, 'String');
3446 $label = ts('Note: Body Only');
3448 if ($noteOption %
3 == 0) {
3449 $clauses[] = self
::buildClause('civicrm_note.subject', $op, $value, 'String');
3450 $label = $label ?
ts('Note: Body and Subject') : ts('Note: Subject Only');
3452 $this->_where
[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
3453 list($qillOp, $qillVal) = self
::buildQillForFieldValue(NULL, $name, $n, $op);
3454 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $label, 2 => $qillOp, 3 => $qillVal]);
3458 * @param string $name
3464 public function nameNullOrEmptyOp($name, $op, $grouping) {
3468 $this->_where
[$grouping][] = "contact_a.$name $op";
3469 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3473 $this->_where
[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')";
3474 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3477 case 'IS NOT EMPTY':
3478 $this->_where
[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')";
3479 $this->_qill
[$grouping][] = ts('Name') . ' ' . $op;
3488 * Where / qill clause for sort_name
3490 * @param array $values
3492 public function sortName(&$values) {
3493 list($fieldName, $op, $value, $grouping, $wildcard) = $values;
3495 // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY
3496 if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) {
3500 $input = $value = is_array($value) ?
trim($value['LIKE']) : trim($value);
3502 if (!strlen($value)) {
3506 $config = CRM_Core_Config
::singleton();
3510 //By default, $sub elements should be joined together with OR statements (don't change this variable).
3513 $firstChar = substr($value, 0, 1);
3514 $lastChar = substr($value, -1, 1);
3515 $quotes = ["'", '"'];
3516 // If string is quoted, strip quotes and otherwise don't alter it
3517 if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) {
3518 $value = trim($value, implode('', $quotes));
3520 // Replace spaces with wildcards for a LIKE operation
3521 // UNLESS string contains a comma (this exception is a tiny bit questionable)
3522 elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) {
3523 $value = str_replace(' ', '%', $value);
3525 $value = CRM_Core_DAO
::escapeString(trim($value));
3526 if (strlen($value)) {
3528 $value = "'" . self
::getWildCardedValue($wildcard, $op, $value) . "'";
3529 if ($fieldName == 'sort_name') {
3530 $wc = "contact_a.sort_name";
3533 $wc = "contact_a.display_name";
3535 $fieldsub[] = " ( $wc $op $value )";
3536 if ($config->includeNickNameInName
) {
3537 $wc = "contact_a.nick_name";
3538 $fieldsub[] = " ( $wc $op $value )";
3540 if ($config->includeEmailInName
) {
3541 $fieldsub[] = " ( civicrm_email.email $op $value ) ";
3543 $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) ';
3546 $sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
3548 $this->_where
[$grouping][] = $sub;
3549 if ($config->includeEmailInName
) {
3550 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3551 $this->_qill
[$grouping][] = ts('Name or Email') . " $op - '$input'";
3554 $this->_qill
[$grouping][] = ts('Name') . " $op - '$input'";
3559 * Where/qill clause for greeting fields.
3561 * @param array $values
3563 public function greetings(&$values) {
3564 list($name, $op, $value, $grouping, $wildcard) = $values;
3565 $name .= '_display';
3567 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
3568 $this->_qill
[$grouping][] = ts('Greeting %1 %2', [1 => $qillop, 2 => $qillVal]);
3569 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value, 'String');
3573 * Where / qill clause for email
3575 * @param array $values
3576 * @param string $isForcePrimaryOnly
3578 * @throws \CRM_Core_Exception
3580 protected function email(&$values, $isForcePrimaryOnly) {
3581 list($name, $op, $value, $grouping, $wildcard) = $values;
3582 $this->_tables
['civicrm_email'] = $this->_whereTables
['civicrm_email'] = 1;
3584 // CRM-18147: for Contact's GET API, email fieldname got appended with its entity as in {$apiEntiy}_{$name}
3585 // so following code is use build whereClause for contact's primart email id
3586 if (!empty($isForcePrimaryOnly)) {
3587 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.is_primary', '=', 1, 'Integer');
3589 // @todo - this should come from the $this->_fields array
3590 $dbName = $name === 'email_id' ?
'id' : $name;
3592 if (is_array($value) ||
$name === 'email_id') {
3593 $this->_qill
[$grouping][] = $this->getQillForField($name, $value, $op);
3594 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.' . $dbName, $op, $value, 'String');
3598 // Is this ever hit now? Ideally ensure always an array & handle above.
3601 if (substr($n, 0, 1) == '"' &&
3602 substr($n, -1, 1) == '"'
3604 $n = substr($n, 1, -1);
3605 $value = CRM_Core_DAO
::escapeString($n);
3609 $value = self
::getWildCardedValue($wildcard, $op, $n);
3611 $this->_qill
[$grouping][] = ts('Email') . " $op '$n'";
3612 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, $value, 'String');
3615 $this->_qill
[$grouping][] = ts('Email') . " $op ";
3616 $this->_where
[$grouping][] = self
::buildClause('civicrm_email.email', $op, NULL, 'String');
3621 * Where / qill clause for phone number
3623 * @param array $values
3625 public function phone_numeric(&$values) {
3626 list($name, $op, $value, $grouping, $wildcard) = $values;
3627 // Strip non-numeric characters; allow wildcards
3628 $number = preg_replace('/[^\d%]/', '', $value);
3630 if (strpos($number, '%') === FALSE) {
3631 $number = "%$number%";
3634 $this->_qill
[$grouping][] = ts('Phone number contains') . " $number";
3635 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String');
3636 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3641 * Where / qill clause for phone type/location
3643 * @param array $values
3645 public function phone_option_group($values) {
3646 list($name, $op, $value, $grouping, $wildcard) = $values;
3647 $option = ($name == 'phone_phone_type_id' ?
'phone_type_id' : 'location_type_id');
3648 $options = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', $option);
3649 $optionName = $options[$value];
3650 $this->_qill
[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ?
ts('type') : ('location')) . " $op $optionName";
3651 $this->_where
[$grouping][] = self
::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer');
3652 $this->_tables
['civicrm_phone'] = $this->_whereTables
['civicrm_phone'] = 1;
3656 * Where / qill clause for street_address.
3658 * @param array $values
3660 public function street_address(&$values) {
3661 list($name, $op, $value, $grouping) = $values;
3670 if (strpos($value, '%') === FALSE) {
3671 // only add wild card if not there
3672 $value = "%{$value}%";
3675 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_address', $op, $value, 'String');
3676 $this->_qill
[$grouping][] = ts('Street') . " $op '$n'";
3679 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_address', $op, NULL, 'String');
3680 $this->_qill
[$grouping][] = ts('Street') . " $op ";
3683 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3687 * Where / qill clause for street_unit.
3689 * @param array $values
3691 public function street_number(&$values) {
3692 list($name, $op, $value, $grouping, $wildcard) = $values;
3700 if (strtolower($n) == 'odd') {
3701 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )";
3702 $this->_qill
[$grouping][] = ts('Street Number is odd');
3704 elseif (strtolower($n) == 'even') {
3705 $this->_where
[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )";
3706 $this->_qill
[$grouping][] = ts('Street Number is even');
3710 $this->_where
[$grouping][] = self
::buildClause('civicrm_address.street_number', $op, $value, 'String');
3711 $this->_qill
[$grouping][] = ts('Street Number') . " $op '$n'";
3714 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3718 * Where / qill clause for sorting by character.
3720 * @param array $values
3722 public function sortByCharacter(&$values) {
3723 list($name, $op, $value, $grouping, $wildcard) = $values;
3725 $name = trim($value);
3726 $cond = " contact_a.sort_name LIKE '" . CRM_Core_DAO
::escapeWildCardString($name) . "%'";
3727 $this->_where
[$grouping][] = $cond;
3728 $this->_qill
[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', [1 => $name]);
3732 * Where / qill clause for including contact ids.
3734 public function includeContactIDs() {
3735 if (!$this->_includeContactIds ||
empty($this->_params
)) {
3740 foreach ($this->_params
as $id => $values) {
3741 if (substr($values[0], 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
3742 $contactIds[] = substr($values[0], CRM_Core_Form
::CB_PREFIX_LEN
);
3745 CRM_Utils_Type
::validateAll($contactIds, 'Positive');
3746 if (!empty($contactIds)) {
3747 $this->_where
[0][] = " ( contact_a.id IN (" . implode(',', $contactIds) . " ) ) ";
3752 * Where / qill clause for postal code.
3754 * @param array $values
3756 public function postalCode(&$values) {
3757 // skip if the fields dont have anything to do with postal_code
3758 if (empty($this->_fields
['postal_code'])) {
3762 list($name, $op, $value, $grouping, $wildcard) = $values;
3764 // Handle numeric postal code range searches properly by casting the column as numeric
3765 if (is_numeric($value)) {
3766 $field = "IF (civicrm_address.postal_code REGEXP '^[0-9]{1,10}$', CAST(civicrm_address.postal_code AS UNSIGNED), 0)";
3767 $val = CRM_Utils_Type
::escape($value, 'Integer');
3770 $field = 'civicrm_address.postal_code';
3771 // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string.
3772 if (!is_array($value)) {
3773 $val = CRM_Utils_Type
::escape($value, 'String');
3776 // Do we need to escape values here? I would expect buildClause does.
3781 $this->_tables
['civicrm_address'] = $this->_whereTables
['civicrm_address'] = 1;
3783 if ($name == 'postal_code') {
3784 $this->_where
[$grouping][] = self
::buildClause($field, $op, $val, 'String');
3785 $this->_qill
[$grouping][] = ts('Postal code') . " {$op} {$value}";
3787 elseif ($name == 'postal_code_low') {
3788 $this->_where
[$grouping][] = " ( $field >= '$val' ) ";
3789 $this->_qill
[$grouping][] = ts('Postal code greater than or equal to \'%1\'', [1 => $value]);
3791 elseif ($name == 'postal_code_high') {
3792 $this->_where
[$grouping][] = " ( $field <= '$val' ) ";
3793 $this->_qill
[$grouping][] = ts('Postal code less than or equal to \'%1\'', [1 => $value]);
3798 * Where / qill clause for location type.
3800 * @param array $values
3801 * @param null $status
3805 public function locationType(&$values, $status = NULL) {
3806 list($name, $op, $value, $grouping, $wildcard) = $values;
3808 if (is_array($value)) {
3809 $this->_where
[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')';
3810 $this->_tables
['civicrm_address'] = 1;
3811 $this->_whereTables
['civicrm_address'] = 1;
3813 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
3815 foreach ($value as $id) {
3816 $names[] = $locationType[$id];
3819 $this->_primaryLocation
= FALSE;
3822 $this->_qill
[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3825 return implode(' ' . ts('or') . ' ', $names);
3832 * @param bool $fromStateProvince
3834 * @return array|NULL
3836 public function country(&$values, $fromStateProvince = TRUE) {
3837 list($name, $op, $value, $grouping, $wildcard) = $values;
3839 if (!$fromStateProvince) {
3840 $stateValues = $this->getWhereValues('state_province', $grouping);
3841 if (!empty($stateValues)) {
3842 // return back to caller if there are state province values
3843 // since that handles this case
3848 $countryClause = $countryQill = NULL;
3849 if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY']) ||
($values && !empty($value))) {
3850 $this->_tables
['civicrm_address'] = 1;
3851 $this->_whereTables
['civicrm_address'] = 1;
3853 $countryClause = self
::buildClause('civicrm_address.country_id', $op, $value, 'Positive');
3854 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, 'country_id', $value, $op);
3855 $countryQill = ts("%1 %2 %3", [1 => 'Country', 2 => $qillop, 3 => $qillVal]);
3857 if (!$fromStateProvince) {
3858 $this->_where
[$grouping][] = $countryClause;
3859 $this->_qill
[$grouping][] = $countryQill;
3863 if ($fromStateProvince) {
3864 if (!empty($countryClause)) {
3867 " ...AND... " . $countryQill,
3871 return [NULL, NULL];
3877 * Where / qill clause for county (if present).
3879 * @param array $values
3880 * @param null $status
3884 public function county(&$values, $status = NULL) {
3885 list($name, $op, $value, $grouping, $wildcard) = $values;
3887 if (!is_array($value)) {
3888 // force the county to be an array
3892 // check if the values are ids OR names of the counties
3893 $inputFormat = 'id';
3894 foreach ($value as $v) {
3895 if (!is_numeric($v)) {
3896 $inputFormat = 'name';
3904 elseif ($op == '!=') {
3908 // this converts IS (NOT)? EMPTY to IS (NOT)? NULL
3909 $op = str_replace('EMPTY', 'NULL', $op);
3912 if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) {
3913 $clause = "civicrm_address.county_id $op";
3915 elseif ($inputFormat == 'id') {
3916 $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')';
3918 $county = CRM_Core_PseudoConstant
::county();
3919 foreach ($value as $id) {
3920 $names[] = CRM_Utils_Array
::value($id, $county);
3925 $county = CRM_Core_PseudoConstant
::county();
3926 foreach ($value as $name) {
3927 $name = trim($name);
3928 $inputClause[] = CRM_Utils_Array
::key($name, $county);
3930 $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')';
3933 $this->_tables
['civicrm_address'] = 1;
3934 $this->_whereTables
['civicrm_address'] = 1;
3936 $this->_where
[$grouping][] = $clause;
3938 $this->_qill
[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names);
3941 return implode(' ' . ts('or') . ' ', $names);
3946 * Where / qill clause for state/province AND country (if present).
3948 * @param array $values
3949 * @param null $status
3953 public function stateProvince(&$values, $status = NULL) {
3954 list($name, $op, $value, $grouping, $wildcard) = $values;
3956 $stateClause = self
::buildClause('civicrm_address.state_province_id', $op, $value, 'Positive');
3957 $this->_tables
['civicrm_address'] = 1;
3958 $this->_whereTables
['civicrm_address'] = 1;
3960 $countryValues = $this->getWhereValues('country', $grouping);
3961 list($countryClause, $countryQill) = $this->country($countryValues, TRUE);
3962 if ($countryClause) {
3963 $clause = "( $stateClause AND $countryClause )";
3966 $clause = $stateClause;
3969 $this->_where
[$grouping][] = $clause;
3970 list($qillop, $qillVal) = self
::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op);
3972 $this->_qill
[$grouping][] = ts("State/Province %1 %2 %3", [1 => $qillop, 2 => $qillVal, 3 => $countryQill]);
3975 return implode(' ' . ts('or') . ' ', $qillVal) . $countryQill;
3980 * Where / qill clause for change log.
3982 * @param array $values
3984 public function changeLog(&$values) {
3985 list($name, $op, $value, $grouping, $wildcard) = $values;
3987 $targetName = $this->getWhereValues('changed_by', $grouping);
3992 $name = trim($targetName[2]);
3993 $name = CRM_Core_DAO
::escapeString($name);
3994 $name = $targetName[4] ?
"%$name%" : $name;
3995 $this->_where
[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'";
3996 $this->_tables
['civicrm_log'] = $this->_whereTables
['civicrm_log'] = 1;
3997 $fieldTitle = ts('Added By');
3998 foreach ($this->_params
as $params) {
3999 if ($params[0] == 'log_date') {
4000 if ($params[2] == 2) {
4001 $fieldTitle = ts('Modified By');
4006 list($qillop, $qillVal) = self
::buildQillForFieldValue(NULL, 'changed_by', $name, 'LIKE');
4007 $this->_qill
[$grouping][] = ts("%1 %2 '%3'", [
4017 public function modifiedDates($values) {
4018 $this->_useDistinct
= TRUE;
4020 // CRM-11281, default to added date if not set
4021 $fieldTitle = ts('Added Date');
4022 $fieldName = 'created_date';
4023 foreach (array_keys($this->_params
) as $id) {
4024 if ($this->_params
[$id][0] == 'log_date') {
4025 if ($this->_params
[$id][2] == 2) {
4026 $fieldTitle = ts('Modified Date');
4027 $fieldName = 'modified_date';
4032 $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle);
4034 self
::$_openedPanes[ts('Change Log')] = TRUE;
4040 public function demographics(&$values) {
4041 list($name, $op, $value, $grouping, $wildcard) = $values;
4043 if (($name == 'age_low') ||
($name == 'age_high')) {
4044 $this->ageRangeQueryBuilder($values,
4045 'contact_a', 'age', 'birth_date', ts('Age')
4048 elseif (($name == 'birth_date_low') ||
($name == 'birth_date_high')) {
4050 $this->dateQueryBuilder($values,
4051 'contact_a', 'birth_date', 'birth_date', ts('Birth Date')
4054 elseif (($name == 'deceased_date_low') ||
($name == 'deceased_date_high')) {
4056 $this->dateQueryBuilder($values,
4057 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date')
4061 self
::$_openedPanes[ts('Demographics')] = TRUE;
4067 public function privacy(&$values) {
4068 list($name, $op, $value, $grouping) = $values;
4069 //fixed for profile search listing CRM-4633
4070 if (strpbrk($value, "[")) {
4071 $value = "'{$value}'";
4073 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
4076 $this->_where
[$grouping][] = "contact_a.{$name} $op $value";
4078 $field = CRM_Utils_Array
::value($name, $this->_fields
);
4079 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
4080 $title = $field ?
$field['title'] : $name;
4081 $this->_qill
[$grouping][] = "$title $op $value";
4087 public function privacyOptions($values) {
4088 list($name, $op, $value, $grouping, $wildcard) = $values;
4090 if (empty($value) ||
!is_array($value)) {
4094 // get the operator and toggle values
4095 $opValues = $this->getWhereValues('privacy_operator', $grouping);
4098 strtolower($opValues[2] == 'AND')
4100 // @todo this line is logially unreachable
4104 $toggleValues = $this->getWhereValues('privacy_toggle', $grouping);
4106 if ($toggleValues &&
4107 $toggleValues[2] == 2
4114 foreach ($value as $dontCare => $pOption) {
4115 $clauses[] = " ( contact_a.{$pOption} = 1 ) ";
4116 $field = CRM_Utils_Array
::value($pOption, $this->_fields
);
4117 $title = $field ?
$field['title'] : $pOption;
4118 $qill[] = " $title = 1 ";
4121 $this->_where
[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )';
4122 $this->_qill
[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )';
4128 public function preferredCommunication(&$values) {
4129 list($name, $op, $value, $grouping, $wildcard) = $values;
4131 if (!is_array($value)) {
4132 $value = str_replace(['(', ')'], '', explode(",", $value));
4134 elseif (in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
4136 $value = $value[$op];
4138 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op);
4140 if (self
::caseImportant($op)) {
4141 $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value);
4142 $op = (strstr($op, '!') ||
strstr($op, 'NOT')) ?
'NOT RLIKE' : 'RLIKE';
4143 $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]";
4146 $this->_where
[$grouping][] = self
::buildClause("contact_a.preferred_communication_method", $op, $value);
4147 $this->_qill
[$grouping][] = ts('Preferred Communication Method %1 %2', [1 => $qillop, 2 => $qillVal]);
4151 * Where / qill clause for relationship.
4153 * @param array $values
4155 public function relationship(&$values) {
4156 list($name, $op, $value, $grouping, $wildcard) = $values;
4157 if ($this->_relationshipValuesAdded
) {
4160 // also get values array for relation_target_name
4161 // for relationship search we always do wildcard
4162 $relationType = $this->getWhereValues('relation_type_id', $grouping);
4163 $targetName = $this->getWhereValues('relation_target_name', $grouping);
4164 $relStatus = $this->getWhereValues('relation_status', $grouping);
4165 $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
4167 $nameClause = $name = NULL;
4169 $name = trim($targetName[2]);
4170 if (substr($name, 0, 1) == '"' &&
4171 substr($name, -1, 1) == '"'
4173 $name = substr($name, 1, -1);
4174 $name = CRM_Core_DAO
::escapeString($name);
4175 $nameClause = "= '$name'";
4178 $name = CRM_Core_DAO
::escapeString($name);
4179 $nameClause = "LIKE '%{$name}%'";
4183 $relTypes = $relTypesIds = [];
4184 if (!empty($relationType)) {
4185 $relationType[2] = (array) $relationType[2];
4186 foreach ($relationType[2] as $relType) {
4187 $rel = explode('_', $relType);
4188 self
::$_relType = $rel[1];
4189 $params = ['id' => $rel[0]];
4191 $rTypeValue = CRM_Contact_BAO_RelationshipType
::retrieve($params, $typeValues);
4192 if (!empty($rTypeValue)) {
4193 if ($rTypeValue->name_a_b
== $rTypeValue->name_b_a
) {
4194 // if we don't know which end of the relationship we are dealing with we'll create a temp table
4195 self
::$_relType = 'reciprocal';
4197 $relTypesIds[] = $rel[0];
4198 $relTypes[] = $relType;
4203 // if we are creating a temp table we build our own where for the relationship table
4204 $relationshipTempTable = NULL;
4205 if (self
::$_relType == 'reciprocal') {
4207 self
::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO
::createTempTableName('civicrm_rel');
4209 $where[$grouping][] = " sort_name $nameClause ";
4211 $groupJoinTable = "civicrm_relationship";
4212 $groupJoinColumn = "contact_id_alt";
4215 $where = &$this->_where
;
4217 $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )";
4219 $groupJoinTable = "contact_b";
4220 $groupJoinColumn = "id";
4222 $allRelationshipType = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
4223 if ($nameClause ||
!$targetGroup) {
4224 if (!empty($relationType)) {
4226 foreach ($relTypes as $rel) {
4227 if (!empty($relQill)) {
4230 $relQill .= $allRelationshipType[$rel];
4232 $this->_qill
[$grouping][] = 'Relationship Type(s) ' . $relQill . " $name";
4235 $this->_qill
[$grouping][] = $name;
4239 //check to see if the target contact is in specified group
4241 //add contacts from static groups
4242 $this->_tables
['civicrm_relationship_group_contact'] = $this->_whereTables
['civicrm_relationship_group_contact']
4243 = " 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'";
4244 $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" .
4245 implode(",", $targetGroup[2]) . ") ) ";
4247 //add contacts from saved searches
4248 $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", $groupJoinTable, $op, $groupJoinColumn);
4250 //set the group where clause
4252 $groupWhere[] = "( " . $ssWhere . " )";
4254 $this->_where
[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
4256 //Get the names of the target groups for the qill
4257 $groupNames = CRM_Core_PseudoConstant
::group();
4259 foreach ($targetGroup[2] as $groupId) {
4260 if (array_key_exists($groupId, $groupNames)) {
4261 $qillNames[] = $groupNames[$groupId];
4264 if (!empty($relationType)) {
4266 foreach ($relTypes as $rel) {
4267 if (!empty($relQill)) {
4270 $relQill .= CRM_Utils_Array
::value($rel, $allRelationshipType);
4272 $this->_qill
[$grouping][] = 'Relationship Type(s) ' . $relQill . " ( " . implode(", ", $qillNames) . " )";
4275 $this->_qill
[$grouping][] = implode(", ", $qillNames);
4279 // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
4280 $today = date('Ymd');
4281 //check for active, inactive and all relation status
4282 if ($relStatus[2] == 0) {
4283 $where[$grouping][] = "(
4284 civicrm_relationship.is_active = 1 AND
4285 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
4286 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
4288 $this->_qill
[$grouping][] = ts('Relationship - Active and Current');
4290 elseif ($relStatus[2] == 1) {
4291 $where[$grouping][] = "(
4292 civicrm_relationship.is_active = 0 OR
4293 civicrm_relationship.end_date < {$today} OR
4294 civicrm_relationship.start_date > {$today}
4296 $this->_qill
[$grouping][] = ts('Relationship - Inactive or not Current');
4300 if (in_array(['deleted_contacts', '=', '1', '0', '0'], $this->_params
)) {
4303 $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
4305 $this->addRelationshipPermissionClauses($grouping, $where);
4306 $this->addRelationshipDateClauses($grouping, $where);
4307 $this->addRelationshipActivePeriodClauses($grouping, $where);
4308 if (!empty($relTypes)) {
4309 $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')';
4311 $this->_tables
['civicrm_relationship'] = $this->_whereTables
['civicrm_relationship'] = 1;
4312 $this->_useDistinct
= TRUE;
4313 $this->_relationshipValuesAdded
= TRUE;
4314 // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
4316 if ($relationshipTempTable) {
4318 if (!empty($where[$grouping])) {
4319 $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
4320 $whereClause = str_replace('contact_b', 'c', $whereClause);
4323 CREATE TEMPORARY TABLE {$relationshipTempTable}
4325 `contact_id` int(10) unsigned NOT NULL DEFAULT '0',
4326 `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0',
4327 KEY `contact_id` (`contact_id`),
4328 KEY `contact_id_alt` (`contact_id_alt`)
4330 (SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id
4331 FROM civicrm_relationship
4332 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
4335 (SELECT contact_id_a as contact_id, contact_id_b as contact_id_alt, civicrm_relationship.id
4336 FROM civicrm_relationship
4337 INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id
4340 CRM_Core_DAO
::executeQuery($sql);
4345 * Add relationship permission criteria to where clause.
4347 * @param string $grouping
4348 * @param array $where Array to add "where" criteria to, in case you are generating a temp table.
4349 * Not the main query.
4351 public function addRelationshipPermissionClauses($grouping, &$where) {
4352 $relPermission = $this->getWhereValues('relation_permission', $grouping);
4353 if ($relPermission) {
4354 if (!is_array($relPermission[2])) {
4355 // this form value was scalar in previous versions of Civi
4356 $relPermission[2] = [$relPermission[2]];
4358 $where[$grouping][] = "(civicrm_relationship.is_permission_a_b IN (" . implode(",", $relPermission[2]) . "))";
4360 $allRelationshipPermissions = CRM_Contact_BAO_Relationship
::buildOptions('is_permission_a_b');
4362 $relPermNames = array_intersect_key($allRelationshipPermissions, array_flip($relPermission[2]));
4363 $this->_qill
[$grouping][] = ts('Permissioned Relationships') . ' - ' . implode(' OR ', $relPermNames);
4368 * Add start & end date criteria in
4369 * @param string $grouping
4370 * @param array $where
4371 * = array to add where clauses to, in case you are generating a temp table.
4372 * not the main query.
4374 public function addRelationshipDateClauses($grouping, &$where) {
4381 foreach ($dateTypes as $dateField) {
4382 $dateValueLow = $this->getWhereValues('relation_' . $dateField . '_low', $grouping);
4383 $dateValueHigh = $this->getWhereValues('relation_' . $dateField . '_high', $grouping);
4384 if (!empty($dateValueLow)) {
4385 $date = date('Ymd', strtotime($dateValueLow[2]));
4386 $where[$grouping][] = "civicrm_relationship.$dateField >= $date";
4387 $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);
4389 if (!empty($dateValueHigh)) {
4390 $date = date('Ymd', strtotime($dateValueHigh[2]));
4391 $where[$grouping][] = "civicrm_relationship.$dateField <= $date";
4392 $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);
4398 * Add start & end active period criteria in
4399 * @param string $grouping
4400 * @param array $where
4401 * = array to add where clauses to, in case you are generating a temp table.
4402 * not the main query.
4404 public function addRelationshipActivePeriodClauses($grouping, &$where) {
4406 $dateField = 'active_period_date';
4408 $dateValueLow = $this->getWhereValues('relation_active_period_date_low', $grouping);
4409 $dateValueHigh = $this->getWhereValues('relation_active_period_date_high', $grouping);
4410 $dateValueLowFormated = $dateValueHighFormated = NULL;
4411 if (!empty($dateValueLow) && !empty($dateValueHigh)) {
4412 $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2]));
4413 $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2]));
4414 $this->_qill
[$grouping][] = (ts('Relationship was active between')) . " " . CRM_Utils_Date
::customFormat($dateValueLowFormated) . " and " . CRM_Utils_Date
::customFormat($dateValueHighFormated);
4416 elseif (!empty($dateValueLow)) {
4417 $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2]));
4418 $this->_qill
[$grouping][] = (ts('Relationship was active after')) . " " . CRM_Utils_Date
::customFormat($dateValueLowFormated);
4420 elseif (!empty($dateValueHigh)) {
4421 $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2]));
4422 $this->_qill
[$grouping][] = (ts('Relationship was active before')) . " " . CRM_Utils_Date
::customFormat($dateValueHighFormated);
4425 if ($activePeriodClauses = self
::getRelationshipActivePeriodClauses($dateValueLowFormated, $dateValueHighFormated, TRUE)) {
4426 $where[$grouping][] = $activePeriodClauses;
4431 * Get start & end active period criteria
4433 public static function getRelationshipActivePeriodClauses($from, $to, $forceTableName) {
4434 $tableName = $forceTableName ?
'civicrm_relationship.' : '';
4435 if (!is_null($from) && !is_null($to)) {
4436 return '(((' . $tableName . 'start_date >= ' . $from . ' AND ' . $tableName . 'start_date <= ' . $to . ') OR
4437 (' . $tableName . 'end_date >= ' . $from . ' AND ' . $tableName . 'end_date <= ' . $to . ') OR
4438 (' . $tableName . 'start_date <= ' . $from . ' AND ' . $tableName . 'end_date >= ' . $to . ' )) OR
4439 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4440 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . ') OR
4441 (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))';
4443 elseif (!is_null($from)) {
4444 return '((' . $tableName . 'start_date >= ' . $from . ') OR
4445 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4446 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . '))';
4448 elseif (!is_null($to)) {
4449 return '((' . $tableName . 'start_date <= ' . $to . ') OR
4450 (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR
4451 (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))';
4456 * Default set of return properties.
4461 * derault return properties
4463 public static function &defaultReturnProperties($mode = 1) {
4464 if (!isset(self
::$_defaultReturnProperties)) {
4465 self
::$_defaultReturnProperties = [];
4468 if (!isset(self
::$_defaultReturnProperties[$mode])) {
4469 // add activity return properties
4470 if ($mode & CRM_Contact_BAO_Query
::MODE_ACTIVITY
) {
4471 self
::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query
::defaultReturnProperties($mode, FALSE);
4474 self
::$_defaultReturnProperties[$mode] = CRM_Core_Component
::defaultReturnProperties($mode, FALSE);
4477 if (empty(self
::$_defaultReturnProperties[$mode])) {
4478 self
::$_defaultReturnProperties[$mode] = [
4481 'legal_identifier' => 1,
4482 'external_identifier' => 1,
4483 'contact_type' => 1,
4484 'contact_sub_type' => 1,
4486 'display_name' => 1,
4487 'preferred_mail_format' => 1,
4494 'formal_title' => 1,
4495 'communication_style_id' => 1,
4498 'street_address' => 1,
4499 'supplemental_address_1' => 1,
4500 'supplemental_address_2' => 1,
4501 'supplemental_address_3' => 1,
4504 'postal_code_suffix' => 1,
4505 'state_province' => 1,
4507 'world_region' => 1,
4514 'household_name' => 1,
4515 'organization_name' => 1,
4516 'deceased_date' => 1,
4521 'current_employer' => 1,
4522 // FIXME: should we use defaultHierReturnProperties() for the below?
4523 'do_not_email' => 1,
4526 'do_not_phone' => 1,
4527 'do_not_trade' => 1,
4529 'contact_is_deleted' => 1,
4530 'preferred_communication_method' => 1,
4531 'preferred_language' => 1,
4535 return self
::$_defaultReturnProperties[$mode];
4539 * Get primary condition for a sql clause.
4543 * @return string|NULL
4545 public static function getPrimaryCondition($value) {
4546 if (is_numeric($value)) {
4547 $value = (int ) $value;
4548 return ($value == 1) ?
'is_primary = 1' : 'is_primary = 0';
4554 * Wrapper for a simple search query.
4556 * @param array $params
4557 * @param array $returnProperties
4558 * @param bool $count
4562 public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) {
4563 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
4564 list($select, $from, $where, $having) = $query->query();
4565 $groupBy = ($query->_useGroupBy
) ?
'GROUP BY contact_a.id' : '';
4567 $query = "$select $from $where $groupBy $having";
4572 * These are stub comments as this function needs more explanation - particularly in terms of how it
4573 * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.
4575 * This function was originally written as a wrapper for the api query but is called from multiple places
4576 * in the core code directly so the name is misleading. This function does not use the searchQuery function
4577 * but it is unclear as to whehter that is historical or there is a reason
4578 * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function
4580 * @param array $params
4581 * @param array $returnProperties
4582 * @param null $fields
4583 * @param string $sort
4584 * @param int $offset
4585 * @param int $row_count
4586 * @param bool $smartGroupCache
4587 * ?? update smart group cache?.
4588 * @param bool $count
4589 * Return count obnly.
4590 * @param bool $skipPermissions
4591 * Should permissions be ignored or should the logged in user's permissions be applied.
4593 * This basically correlates to the component.
4594 * @param string $apiEntity
4595 * The api entity being called.
4596 * This sort-of duplicates $mode in a confusing way. Probably not by design.
4598 * @param bool|null $primaryLocationOnly
4601 public static function apiQuery(
4603 $returnProperties = NULL,
4608 $smartGroupCache = TRUE,
4610 $skipPermissions = TRUE,
4611 $mode = CRM_Contact_BAO_Query
::MODE_CONTACTS
,
4613 $primaryLocationOnly = NULL
4616 $query = new CRM_Contact_BAO_Query(
4617 $params, $returnProperties,
4618 NULL, TRUE, FALSE, $mode,
4620 TRUE, $smartGroupCache,
4622 $apiEntity, $primaryLocationOnly
4625 //this should add a check for view deleted if permissions are enabled
4626 if ($skipPermissions) {
4627 $query->_skipDeleteClause
= TRUE;
4629 $query->generatePermissionClause(FALSE, $count);
4631 // note : this modifies _fromClause and _simpleFromClause
4632 $query->includePseudoFieldsJoin($sort);
4634 list($select, $from, $where, $having) = $query->query($count);
4636 $options = $query->_options
;
4637 if (!empty($query->_permissionWhereClause
)) {
4638 if (empty($where)) {
4639 $where = "WHERE $query->_permissionWhereClause";
4642 $where = "$where AND $query->_permissionWhereClause";
4646 $sql = "$select $from $where $having";
4648 // add group by only when API action is not getcount
4649 // otherwise query fetches incorrect count
4650 if ($query->_useGroupBy
&& !$count) {
4651 $sql .= self
::getGroupByFromSelectColumns($query->_select
, 'contact_a.id');
4653 if (!empty($sort)) {
4654 $sort = CRM_Utils_Type
::escape($sort, 'String');
4655 $sql .= " ORDER BY $sort ";
4657 if ($row_count > 0 && $offset >= 0) {
4658 $offset = CRM_Utils_Type
::escape($offset, 'Int');
4659 $row_count = CRM_Utils_Type
::escape($row_count, 'Int');
4660 $sql .= " LIMIT $offset, $row_count ";
4663 $dao = CRM_Core_DAO
::executeQuery($sql);
4665 // @todo derive this from the component class rather than hard-code two options.
4666 $entityIDField = ($mode == CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
) ?
'contribution_id' : 'contact_id';
4669 while ($dao->fetch()) {
4671 $noRows = $dao->rowCount
;
4672 return [$noRows, NULL];
4674 $val = $query->store($dao);
4675 $convertedVals = $query->convertToPseudoNames($dao, TRUE, TRUE);
4677 if (!empty($convertedVals)) {
4678 $val = array_replace_recursive($val, $convertedVals);
4680 $values[$dao->$entityIDField] = $val;
4682 return [$values, $options];
4686 * Get the actual custom field name by stripping off the appended string.
4688 * The string could be _relative, _from, or _to
4690 * @todo use metadata rather than convention to do this.
4692 * @param string $parameterName
4693 * The name of the parameter submitted to the form.
4700 public static function getCustomFieldName($parameterName) {
4701 if (substr($parameterName, -5, 5) == '_from') {
4702 return substr($parameterName, 0, strpos($parameterName, '_from'));
4704 if (substr($parameterName, -9, 9) == '_relative') {
4705 return substr($parameterName, 0, strpos($parameterName, '_relative'));
4707 if (substr($parameterName, -3, 3) == '_to') {
4708 return substr($parameterName, 0, strpos($parameterName, '_to'));
4713 * Convert submitted values for relative custom fields to query object format.
4715 * The query will support the sqlOperator format so convert to that format.
4717 * @param array $formValues
4719 * @param array $params
4720 * Converted parameters for the query object.
4721 * @param string $values
4723 * @param string $fieldName
4724 * Submitted field name. (Matches form field not DB field.)
4726 protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) {
4727 if (empty($values)) {
4728 // e.g we might have relative set & from & to empty. The form flow is a bit funky &
4729 // this function gets called again after they fields have been converted which can get ugly.
4732 $customFieldName = self
::getCustomFieldName($fieldName);
4734 if (substr($fieldName, -9, 9) == '_relative') {
4735 list($from, $to) = CRM_Utils_Date
::getFromTo($values, NULL, NULL);
4738 if ($fieldName == $customFieldName . '_to' && CRM_Utils_Array
::value($customFieldName . '_from', $formValues)) {
4739 // Both to & from are set. We only need to acton one, choosing from.
4743 $from = CRM_Utils_Array
::value($customFieldName . '_from', $formValues, NULL);
4744 $to = CRM_Utils_Array
::value($customFieldName . '_to', $formValues, NULL);
4746 if (self
::isCustomDateField($customFieldName)) {
4747 list($from, $to) = CRM_Utils_Date
::getFromTo(NULL, $from, $to);
4753 $relativeFunction = ['BETWEEN' => [$from, $to]];
4756 $relativeFunction = ['>=' => $from];
4760 $relativeFunction = ['<=' => $to];
4772 * Are we dealing with custom field of type date.
4778 public static function isCustomDateField($fieldName) {
4779 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) == FALSE) {
4782 if ('Date' == civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldID, 'return' => 'data_type'])) {
4789 * Has this field already been reformatting to Query object syntax.
4791 * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems
4792 * to come with associated double queries & is possibly damaging performance.
4794 * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns
4797 * @param mixed $values
4798 * Value in formValues for the field.
4802 public static function isAlreadyProcessedForQueryFormat($values) {
4803 if (!is_array($values)) {
4806 if (($operator = CRM_Utils_Array
::value(1, $values)) == FALSE) {
4809 return in_array($operator, CRM_Core_DAO
::acceptedSQLOperators());
4813 * If the state and country are passed remove state.
4815 * Country is implicit from the state, but including both results in
4816 * a poor query as there is no combined index on state AND country.
4820 * @param array $formValues
4822 public static function filterCountryFromValuesIfStateExists(&$formValues) {
4823 if (!empty($formValues['country']) && !empty($formValues['state_province'])) {
4824 // The use of array map sanitises the data by ensuring we are dealing with integers.
4825 $states = implode(', ', array_map('intval', $formValues['state_province']));
4826 $countryList = CRM_Core_DAO
::singleValueQuery(
4827 "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE id IN ($states)"
4829 if ($countryList == $formValues['country']) {
4830 unset($formValues['country']);
4836 * For some special cases, grouping by subset of select fields becomes mandatory.
4837 * Hence, full_group_by mode is handled by appending any_value
4838 * keyword to select fields not present in groupBy
4840 * @param array $selectClauses
4841 * @param array $groupBy - Columns already included in GROUP By clause.
4842 * @param string $aggregateFunction
4846 public static function appendAnyValueToSelect($selectClauses, $groupBy, $aggregateFunction = 'ANY_VALUE') {
4847 if (!CRM_Utils_SQL
::disableFullGroupByMode()) {
4848 $groupBy = array_map('trim', (array) $groupBy);
4849 $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i';
4850 foreach ($selectClauses as $key => &$val) {
4851 list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
4852 // append ANY_VALUE() keyword
4853 if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
4854 $val = ($aggregateFunction == 'GROUP_CONCAT') ?
4855 str_replace($selectColumn, "$aggregateFunction(DISTINCT {$selectColumn})", $val) :
4856 str_replace($selectColumn, "$aggregateFunction({$selectColumn})", $val);
4861 return "SELECT " . implode(', ', $selectClauses) . " ";
4865 * For some special cases, where if non-aggregate ORDER BY columns are not present in GROUP BY
4866 * on full_group_by mode, then append the those missing columns to GROUP BY clause
4867 * keyword to select fields not present in groupBy
4869 * @param string $groupBy - GROUP BY clause where missing ORDER BY columns will be appended if not present
4870 * @param array $orderBys - ORDER BY sub-clauses
4873 public static function getGroupByFromOrderBy(&$groupBy, $orderBys) {
4874 if (!CRM_Utils_SQL
::disableFullGroupByMode()) {
4875 foreach ($orderBys as $orderBy) {
4876 // remove sort syntax from ORDER BY clauses if present
4877 $orderBy = str_ireplace([' DESC', ' ASC', '`'], '', $orderBy);
4878 // if ORDER BY column is not present in GROUP BY then append it to end
4879 if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1 && !strstr($groupBy, $orderBy)) {
4880 $groupBy .= ", {$orderBy}";
4887 * Include Select columns in groupBy clause.
4889 * @param array $selectClauses
4890 * @param array $groupBy - Columns already included in GROUP By clause.
4894 public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
4895 $groupBy = (array) $groupBy;
4896 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
4897 $sqlMode = CRM_Core_DAO
::singleValueQuery('SELECT @@sql_mode');
4899 //return if ONLY_FULL_GROUP_BY is not enabled.
4900 if (CRM_Utils_SQL
::supportsFullGroupBy() && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) {
4901 $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i';
4902 foreach ($selectClauses as $key => $val) {
4903 $aliasArray = preg_split('/ as /i', $val);
4904 // if more than 1 alias we need to split by ','.
4905 if (count($aliasArray) > 2) {
4906 $aliasArray = preg_split('/,/', $val);
4907 foreach ($aliasArray as $key => $value) {
4908 $alias = current(preg_split('/ as /i', $value));
4909 if (!in_array($alias, $groupBy) && preg_match($regexToExclude, trim($alias)) !== 1) {
4910 $groupBy[] = $alias;
4915 list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
4916 $dateRegex = '/^(DATE_FORMAT|DATE_ADD|CASE)/i';
4917 $tableName = current(explode('.', $selectColumn));
4918 $primaryKey = "{$tableName}.id";
4919 // exclude columns which are already included in groupBy and aggregate functions from select
4920 // CRM-18439 - Also exclude the columns which are functionally dependent on columns in $groupBy (MySQL 5.7+)
4921 if (!in_array($selectColumn, $groupBy) && !in_array($primaryKey, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
4922 if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
4923 $groupBy[] = $alias;
4926 $groupBy[] = $selectColumn;
4933 if (!empty($groupBy)) {
4934 return " GROUP BY " . implode(', ', $groupBy);
4940 * Create and query the db for an contact search.
4942 * @param int $offset
4943 * The offset for the query.
4944 * @param int $rowCount
4945 * The number of rows to return.
4946 * @param string|CRM_Utils_Sort $sort
4947 * The order by string.
4948 * @param bool $count
4949 * Is this a count only query ?.
4950 * @param bool $includeContactIds
4951 * Should we include contact ids?.
4952 * @param bool $sortByChar
4953 * If true returns the distinct array of first characters for search results.
4954 * @param bool $groupContacts
4955 * If true, return only the contact ids.
4956 * @param bool $returnQuery
4957 * Should we return the query as a string.
4958 * @param string $additionalWhereClause
4959 * If the caller wants to further restrict the search (used for components).
4960 * @param null $sortOrder
4961 * @param string $additionalFromClause
4962 * Should be clause with proper joins, effective to reduce where clause load.
4964 * @param bool $skipOrderAndLimit
4966 * @return CRM_Core_DAO
4968 public function searchQuery(
4969 $offset = 0, $rowCount = 0, $sort = NULL,
4970 $count = FALSE, $includeContactIds = FALSE,
4971 $sortByChar = FALSE, $groupContacts = FALSE,
4972 $returnQuery = FALSE,
4973 $additionalWhereClause = NULL, $sortOrder = NULL,
4974 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE
4977 $query = $this->getSearchSQL($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause, $skipOrderAndLimit);
4983 return CRM_Core_DAO
::singleValueQuery($query);
4986 $dao = CRM_Core_DAO
::executeQuery($query);
4988 // We can always call this - it will only re-enable if it was originally enabled.
4989 CRM_Core_DAO
::reenableFullGroupByMode();
4991 if ($groupContacts) {
4993 while ($dao->fetch()) {
4996 return implode(',', $ids);
5003 * Create and query the db for the list of all first letters used by contacts
5005 * @return CRM_Core_DAO
5007 public function alphabetQuery() {
5008 $sqlParts = $this->getSearchSQLParts(NULL, NULL, NULL, FALSE, FALSE, TRUE);
5009 $query = "SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name
5011 {$sqlParts['where']}
5012 {$sqlParts['having']}
5014 ORDER BY sort_name asc";
5015 $dao = CRM_Core_DAO
::executeQuery($query);
5020 * Fetch a list of contacts for displaying a search results page
5022 * @param array $cids
5023 * List of contact IDs
5024 * @param bool $includeContactIds
5025 * @return CRM_Core_DAO
5027 public function getCachedContacts($cids, $includeContactIds) {
5028 CRM_Core_DAO
::disableFullGroupByMode();
5029 CRM_Utils_Type
::validateAll($cids, 'Positive');
5030 $this->_includeContactIds
= $includeContactIds;
5031 $onlyDeleted = in_array(['deleted_contacts', '=', '1', '0', '0'], $this->_params
);
5032 list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
5033 $select .= sprintf(", (%s) AS _wgt", $this->createSqlCase('contact_a.id', $cids));
5034 $where .= sprintf(' AND contact_a.id IN (%s)', implode(',', $cids));
5035 $order = 'ORDER BY _wgt';
5036 $groupBy = $this->_useGroupBy ?
' GROUP BY contact_a.id' : '';
5038 $query = "$select $from $where $groupBy $order $limit";
5040 $result = CRM_Core_DAO
::executeQuery($query);
5041 CRM_Core_DAO
::reenableFullGroupByMode();
5046 * Construct a SQL CASE expression.
5048 * @param string $idCol
5049 * The name of a column with ID's (eg 'contact_a.id').
5050 * @param array $cids
5051 * Array(int $weight => int $id).
5053 * CASE WHEN id=123 THEN 1 WHEN id=456 THEN 2 END
5055 private function createSqlCase($idCol, $cids) {
5057 foreach ($cids as $weight => $cid) {
5058 $buf .= " WHEN $idCol = $cid THEN $weight \n";
5065 * Populate $this->_permissionWhereClause with permission related clause and update other
5066 * query related properties.
5068 * Function calls ACL permission class and hooks to filter the query appropriately
5070 * Note that these 2 params were in the code when extracted from another function
5071 * and a second round extraction would be to make them properties of the class
5073 * @param bool $onlyDeleted
5074 * Only get deleted contacts.
5075 * @param bool $count
5076 * Return Count only.
5078 public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) {
5079 if (!$this->_skipPermission
) {
5080 $this->_permissionWhereClause
= CRM_ACL_API
::whereClause(
5081 CRM_Core_Permission
::VIEW
,
5083 $this->_whereTables
,
5086 $this->_skipDeleteClause
5089 // regenerate fromClause since permission might have added tables
5090 if ($this->_permissionWhereClause
) {
5091 //fix for row count in qill (in contribute/membership find)
5093 $this->_useDistinct
= TRUE;
5096 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
5097 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
5098 // note : this modifies _fromClause and _simpleFromClause
5099 $this->includePseudoFieldsJoin($this->_sort
);
5103 // add delete clause if needed even if we are skipping permission
5105 if (!$this->_skipDeleteClause
) {
5106 if (CRM_Core_Permission
::check('access deleted contacts') and $onlyDeleted) {
5107 $this->_permissionWhereClause
= '(contact_a.is_deleted)';
5111 $this->_permissionWhereClause
= '(contact_a.is_deleted = 0)';
5120 public function setSkipPermission($val) {
5121 $this->_skipPermission
= $val;
5125 * @param null $context
5129 public function summaryContribution($context = NULL) {
5130 list($innerselect, $from, $where, $having) = $this->query(TRUE);
5131 if ($this->_permissionWhereClause
) {
5132 $where .= " AND " . $this->_permissionWhereClause
;
5134 if ($context == 'search') {
5135 $where .= " AND contact_a.is_deleted = 0 ";
5138 $this->appendFinancialTypeWhereAndFromToQueryStrings($where, $from);
5140 $summary = ['total' => []];
5141 $this->addBasicStatsToSummary($summary, $where, $from);
5143 if (CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled()) {
5144 $this->addBasicSoftCreditStatsToStats($summary, $where, $from);
5147 $this->addBasicCancelStatsToSummary($summary, $where, $from);
5153 * Append financial ACL limits to the query from & where clauses, if applicable.
5155 * @param string $where
5156 * @param string $from
5158 public function appendFinancialTypeWhereAndFromToQueryStrings(&$where, &$from) {
5159 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
5162 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
5163 if (!empty($financialTypes)) {
5164 $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL";
5165 $from .= " LEFT JOIN civicrm_line_item li
5166 ON civicrm_contribution.id = li.contribution_id AND
5167 li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") ";
5170 $where .= " AND civicrm_contribution.financial_type_id IN (0)";
5175 * Getter for the qill object.
5179 public function qill() {
5180 return $this->_qill
;
5184 * Default set of return default hier return properties.
5188 public static function &defaultHierReturnProperties() {
5189 if (!isset(self
::$_defaultHierReturnProperties)) {
5190 self
::$_defaultHierReturnProperties = [
5193 'legal_identifier' => 1,
5194 'external_identifier' => 1,
5195 'contact_type' => 1,
5196 'contact_sub_type' => 1,
5198 'display_name' => 1,
5205 'formal_title' => 1,
5206 'communication_style_id' => 1,
5207 'email_greeting' => 1,
5208 'postal_greeting' => 1,
5212 'preferred_communication_method' => 1,
5213 'do_not_phone' => 1,
5214 'do_not_email' => 1,
5217 'do_not_trade' => 1,
5220 'location_type' => 1,
5221 'street_address' => 1,
5223 'state_province' => 1,
5225 'postal_code_suffix' => 1,
5228 'phone-Mobile' => 1,
5241 'location_type' => 1,
5242 'street_address' => 1,
5244 'state_province' => 1,
5246 'postal_code_suffix' => 1,
5249 'phone-Mobile' => 1,
5263 return self
::$_defaultHierReturnProperties;
5267 * Build query for a date field.
5269 * @param array $values
5270 * @param string $tableName
5271 * @param string $fieldName
5272 * @param string $dbFieldName
5273 * @param string $fieldTitle
5274 * @param bool $appendTimeStamp
5275 * @param string $dateFormat
5277 public function dateQueryBuilder(
5278 $values, $tableName, $fieldName,
5279 $dbFieldName, $fieldTitle,
5280 $appendTimeStamp = TRUE,
5281 $dateFormat = 'YmdHis'
5283 // @todo - remove dateFormat - pretty sure it's never passed in...
5284 list($name, $op, $value, $grouping, $wildcard) = $values;
5286 if ($name == "{$fieldName}_low" ||
5287 $name == "{$fieldName}_high"
5289 if (isset($this->_rangeCache
[$fieldName]) ||
!$value) {
5292 $this->_rangeCache
[$fieldName] = 1;
5294 $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL;
5296 if ($name == $fieldName . '_low') {
5298 $firstPhrase = ts('greater than or equal to');
5299 $firstDate = CRM_Utils_Date
::processDate($value, NULL, FALSE, $dateFormat);
5301 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5302 if (!empty($secondValues) && $secondValues[2]) {
5304 $secondPhrase = ts('less than or equal to');
5305 $secondValue = $secondValues[2];
5307 if ($appendTimeStamp && strlen($secondValue) == 10) {
5308 $secondValue .= ' 23:59:59';
5310 $secondDate = CRM_Utils_Date
::processDate($secondValue, NULL, FALSE, $dateFormat);
5313 elseif ($name == $fieldName . '_high') {
5315 $firstPhrase = ts('less than or equal to');
5317 if ($appendTimeStamp && strlen($value) == 10) {
5318 $value .= ' 23:59:59';
5320 $firstDate = CRM_Utils_Date
::processDate($value, NULL, FALSE, $dateFormat);
5322 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5323 if (!empty($secondValues) && $secondValues[2]) {
5325 $secondPhrase = ts('greater than or equal to');
5326 $secondValue = $secondValues[2];
5327 $secondDate = CRM_Utils_Date
::processDate($secondValue, NULL, FALSE, $dateFormat);
5331 if (!$appendTimeStamp) {
5332 $firstDate = substr($firstDate, 0, 8);
5334 $firstDateFormat = CRM_Utils_Date
::customFormat($firstDate);
5337 if (!$appendTimeStamp) {
5338 $secondDate = substr($secondDate, 0, 8);
5340 $secondDateFormat = CRM_Utils_Date
::customFormat($secondDate);
5343 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5345 $this->_where
[$grouping][] = "
5346 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5347 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5349 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\"";
5352 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5353 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
5357 if ($name == $fieldName) {
5358 //In Get API, for operators other then '=' the $value is in array(op => value) format
5359 if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
5361 $value = $value[$op];
5364 $date = $format = NULL;
5365 if (strstr($op, 'IN')) {
5367 foreach ($value as &$date) {
5368 $date = CRM_Utils_Date
::processDate($date, NULL, FALSE, $dateFormat);
5369 if (!$appendTimeStamp) {
5370 $date = substr($date, 0, 8);
5372 $format[] = CRM_Utils_Date
::customFormat($date);
5374 $date = "('" . implode("','", $value) . "')";
5375 $format = implode(', ', $format);
5377 elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) {
5378 $date = CRM_Utils_Date
::processDate($value, NULL, FALSE, $dateFormat);
5379 if (!$appendTimeStamp) {
5380 $date = substr($date, 0, 8);
5382 $format = CRM_Utils_Date
::customFormat($date);
5387 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op $date";
5390 $this->_where
[$grouping][] = self
::buildClause("{$tableName}.{$dbFieldName}", $op);
5393 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5395 $op = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
5396 $this->_qill
[$grouping][] = "$fieldTitle $op $format";
5402 * @param string $tableName
5403 * @param string $fieldName
5404 * @param string $dbFieldName
5405 * @param $fieldTitle
5406 * @param null $options
5408 public function numberRangeBuilder(
5410 $tableName, $fieldName,
5411 $dbFieldName, $fieldTitle,
5414 list($name, $op, $value, $grouping, $wildcard) = $values;
5416 if ($name == "{$fieldName}_low" ||
5417 $name == "{$fieldName}_high"
5419 if (isset($this->_rangeCache
[$fieldName])) {
5422 $this->_rangeCache
[$fieldName] = 1;
5424 $secondOP = $secondPhrase = $secondValue = NULL;
5426 if ($name == "{$fieldName}_low") {
5428 $firstPhrase = ts('greater than');
5430 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5431 if (!empty($secondValues)) {
5433 $secondPhrase = ts('less than');
5434 $secondValue = $secondValues[2];
5439 $firstPhrase = ts('less than');
5441 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5442 if (!empty($secondValues)) {
5444 $secondPhrase = ts('greater than');
5445 $secondValue = $secondValues[2];
5450 $this->_where
[$grouping][] = "
5451 ( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND
5452 ( {$tableName}.{$dbFieldName} $secondOP {$secondValue} )
5454 $displayValue = $options ?
$options[$value] : $value;
5455 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5457 $this->_qill
[$grouping][]
5458 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5461 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}";
5462 $displayValue = $options ?
$options[$value] : $value;
5463 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5465 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5470 if ($name == $fieldName) {
5474 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}";
5476 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5477 $displayValue = $options ?
$options[$value] : $value;
5478 $this->_qill
[$grouping][] = "$fieldTitle - $phrase \"$displayValue\"";
5484 * @param string $tableName
5485 * @param string $fieldName
5486 * @param string $dbFieldName
5487 * @param $fieldTitle
5488 * @param null $options
5490 public function ageRangeQueryBuilder(
5492 $tableName, $fieldName,
5493 $dbFieldName, $fieldTitle,
5496 list($name, $op, $value, $grouping, $wildcard) = $values;
5498 $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping);
5499 // will be treated as current day
5501 if ($asofDateValues) {
5502 $asofDate = CRM_Utils_Date
::processDate($asofDateValues[2]);
5503 $asofDateFormat = CRM_Utils_Date
::customFormat(substr($asofDate, 0, 8));
5504 $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat;
5507 if ($name == "{$fieldName}_low" ||
5508 $name == "{$fieldName}_high"
5510 if (isset($this->_rangeCache
[$fieldName])) {
5513 $this->_rangeCache
[$fieldName] = 1;
5515 $secondOP = $secondPhrase = $secondValue = NULL;
5517 if ($name == "{$fieldName}_low") {
5518 $firstPhrase = ts('greater than or equal to');
5519 // NB: age > X means date of birth < Y
5521 $firstDate = self
::calcDateFromAge($asofDate, $value, 'min');
5523 $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping);
5524 if (!empty($secondValues)) {
5526 $secondPhrase = ts('less than or equal to');
5527 $secondValue = $secondValues[2];
5528 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'max');
5533 $firstPhrase = ts('less than or equal to');
5534 $firstDate = self
::calcDateFromAge($asofDate, $value, 'max');
5536 $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping);
5537 if (!empty($secondValues)) {
5539 $secondPhrase = ts('greater than or equal to');
5540 $secondValue = $secondValues[2];
5541 $secondDate = self
::calcDateFromAge($asofDate, $secondValue, 'min');
5546 $this->_where
[$grouping][] = "
5547 ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
5548 ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
5550 $displayValue = $options ?
$options[$value] : $value;
5551 $secondDisplayValue = $options ?
$options[$secondValue] : $secondValue;
5553 $this->_qill
[$grouping][]
5554 = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\"";
5557 $this->_where
[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
5558 $displayValue = $options ?
$options[$value] : $value;
5559 $this->_qill
[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\"";
5561 $this->_tables
[$tableName] = $this->_whereTables
[$tableName] = 1;
5567 * Calculate date from age.
5569 * @param string $asofDate
5571 * @param string $type
5575 public static function calcDateFromAge($asofDate, $age, $type) {
5576 $date = new DateTime($asofDate);
5577 if ($type == "min") {
5578 // minimum age is $age: dob <= date - age "235959"
5579 $date->sub(new DateInterval("P" . $age . "Y"));
5580 return $date->format('Ymd') . "235959";
5583 // max age is $age: dob >= date - (age + 1y) + 1d "000000"
5584 $date->sub(new DateInterval("P" . ($age +
1) . "Y"))->add(new DateInterval("P1D"));
5585 return $date->format('Ymd') . "000000";
5590 * Given the field name, operator, value & its data type
5591 * builds the where Clause for the query
5592 * used for handling 'IS NULL'/'IS NOT NULL' operators
5594 * @param string $field
5598 * @param string $value
5600 * @param string $dataType
5601 * Data type of the field.
5604 * Where clause for the query.
5606 public static function buildClause($field, $op, $value = NULL, $dataType = NULL) {
5608 $clause = "$field $op";
5616 $clause = ($dataType == 'Date') ?
" $field IS NULL " : " (NULLIF($field, '') IS NULL) ";
5619 case 'IS NOT EMPTY':
5620 $clause = ($dataType == 'Date') ?
" $field IS NOT NULL " : " (NULLIF($field, '') IS NOT NULL) ";
5624 return " {$clause} BINARY '{$value}' ";
5628 // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter.
5629 if (!empty($value) && (!is_array($value) ||
!array_key_exists($op, $value))) {
5630 $value = [$op => (array) $value];
5634 if (empty($dataType) ||
$dataType == 'Date') {
5635 $dataType = 'String';
5637 if (is_array($value)) {
5638 //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more
5639 // widely used and consistent across the codebase
5640 // adding this here won't accept the search functions which don't submit an array
5641 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, $value, $dataType)) != FALSE) {
5643 return $queryString;
5645 if (!empty($value[0]) && $op === 'BETWEEN') {
5646 CRM_Core_Error
::deprecatedFunctionWarning('Fix search input params');
5647 if (($queryString = CRM_Core_DAO
::createSqlFilter($field, [$op => $value], $dataType)) != FALSE) {
5648 return $queryString;
5651 throw new CRM_Core_Exception(ts('Failed to interpret input for search'));
5654 $value = CRM_Utils_Type
::escape($value, $dataType);
5655 // if we don't have a dataType we should assume
5656 if ($dataType == 'String' ||
$dataType == 'Text') {
5657 $value = "'" . $value . "'";
5659 return "$clause $value";
5664 * @param bool $reset
5668 public function openedSearchPanes($reset = FALSE) {
5669 if (!$reset ||
empty($this->_whereTables
)) {
5670 return self
::$_openedPanes;
5673 // pane name to table mapper
5675 ts('Contributions') => 'civicrm_contribution',
5676 ts('Memberships') => 'civicrm_membership',
5677 ts('Events') => 'civicrm_participant',
5678 ts('Relationships') => 'civicrm_relationship',
5679 ts('Activities') => 'civicrm_activity',
5680 ts('Pledges') => 'civicrm_pledge',
5681 ts('Cases') => 'civicrm_case',
5682 ts('Grants') => 'civicrm_grant',
5683 ts('Address Fields') => 'civicrm_address',
5684 ts('Notes') => 'civicrm_note',
5685 ts('Change Log') => 'civicrm_log',
5686 ts('Mailings') => 'civicrm_mailing',
5688 CRM_Contact_BAO_Query_Hook
::singleton()->getPanesMapper($panesMapper);
5690 foreach (array_keys($this->_whereTables
) as $table) {
5691 if ($panName = array_search($table, $panesMapper)) {
5692 self
::$_openedPanes[$panName] = TRUE;
5696 return self
::$_openedPanes;
5702 public function setOperator($operator) {
5703 $validOperators = ['AND', 'OR'];
5704 if (!in_array($operator, $validOperators)) {
5707 $this->_operator
= $operator;
5713 public function getOperator() {
5714 return $this->_operator
;
5722 public function filterRelatedContacts(&$from, &$where, &$having) {
5723 if (!isset(Civi
::$statics[__CLASS__
]['related_contacts_filter'])) {
5724 Civi
::$statics[__CLASS__
]['related_contacts_filter'] = [];
5726 $_rTempCache =& Civi
::$statics[__CLASS__
]['related_contacts_filter'];
5727 // since there only can be one instance of this filter in every query
5728 // skip if filter has already applied
5729 foreach ($_rTempCache as $acache) {
5730 foreach ($acache['queries'] as $aqcache) {
5731 if (strpos($from, $aqcache['from']) !== FALSE) {
5737 $arg_sig = sha1("$from $where $having");
5738 if (isset($_rTempCache[$arg_sig])) {
5739 $cache = $_rTempCache[$arg_sig];
5742 // create temp table with contact ids
5744 $tableName = CRM_Utils_SQL_TempTable
::build()->createWithColumns('contact_id int primary key')->setMemory(TRUE)->getName();
5747 REPLACE INTO $tableName ( contact_id )
5753 CRM_Core_DAO
::executeQuery($sql);
5755 $cache = ['tableName' => $tableName, 'queries' => []];
5756 $_rTempCache[$arg_sig] = $cache;
5758 // upsert the query depending on relationship type
5759 if (isset($cache['queries'][$this->_displayRelationshipType
])) {
5760 $qcache = $cache['queries'][$this->_displayRelationshipType
];
5763 $tableName = $cache['tableName'];
5768 $rTypes = CRM_Core_PseudoConstant
::relationshipType();
5769 if (is_numeric($this->_displayRelationshipType
)) {
5770 $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType
]['label_a_b'];
5772 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )
5773 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )
5775 $qcache['where'] = "
5776 WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}
5777 AND displayRelType.is_active = 1
5781 list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType
);
5782 if ($dirOne == 'a') {
5783 $relationshipTypeLabel = $rTypes[$relType]['label_a_b'];
5784 $qcache['from'] .= "
5785 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )
5786 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )
5790 $relationshipTypeLabel = $rTypes[$relType]['label_b_a'];
5791 $qcache['from'] .= "
5792 INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )
5793 INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )
5796 $qcache['where'] = "
5797 WHERE displayRelType.relationship_type_id = $relType
5798 AND displayRelType.is_active = 1
5801 $qcache['relTypeLabel'] = $relationshipTypeLabel;
5802 $_rTempCache[$arg_sig]['queries'][$this->_displayRelationshipType
] = $qcache;
5804 $qillMessage = ts('Contacts with a Relationship Type of: ');
5805 $iqill = $qillMessage . "'" . $qcache['relTypeLabel'] . "'";
5806 if (!is_array($this->_qill
[0]) ||
!in_array($iqill, $this->_qill
[0])) {
5807 $this->_qill
[0][] = $iqill;
5809 if (strpos($from, $qcache['from']) === FALSE) {
5810 // lets replace all the INNER JOIN's in the $from so we dont exclude other data
5811 // this happens when we have an event_type in the quert (CRM-7969)
5812 $from = str_replace("INNER JOIN", "LEFT JOIN", $from);
5813 $from .= $qcache['from'];
5814 $where = $qcache['where'];
5815 if (!empty($this->_permissionWhereClause
)) {
5816 $where .= "AND $this->_permissionWhereClause";
5824 * See CRM-19811 for why this is database hurty without apparent benefit.
5830 public static function caseImportant($op) {
5831 return in_array($op, ['LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY']) ?
FALSE : TRUE;
5835 * @param $returnProperties
5840 public static function componentPresent(&$returnProperties, $prefix) {
5841 foreach ($returnProperties as $name => $dontCare) {
5842 if (substr($name, 0, strlen($prefix)) == $prefix) {
5850 * Builds the necessary structures for all fields that are similar to option value look-ups.
5852 * @param string $name
5853 * the name of the field.
5855 * the sql operator, this function should handle ALL SQL operators.
5856 * @param string $value
5857 * depends on the operator and who's calling the query builder.
5858 * @param int $grouping
5859 * the index where to place the where clause.
5860 * @param string $daoName
5862 * @param array $field
5863 * an array that contains various properties of the field identified by $name.
5864 * @param string $label
5865 * The label for this field element.
5866 * @param string $dataType
5867 * The data type for this element.
5868 * @param bool $useIDsOnly
5870 public function optionValueQuery(
5878 $dataType = 'String',
5889 'communication_style_id',
5893 list($tableName, $fieldName) = explode('.', $field['where'], 2);
5894 if ($tableName == 'civicrm_contact') {
5895 $wc = "contact_a.$fieldName";
5898 // Special handling for on_hold, so that we actually use the 'where'
5899 // property in order to limit the query by the on_hold status of the email,
5900 // instead of using email.id which would be nonsensical.
5901 if ($field['name'] == 'on_hold') {
5902 $wc = "{$field['where']}";
5905 $wc = "$tableName.id";
5910 CRM_Core_Error
::deprecatedFunctionWarning('pass $ids to this method');
5911 $wc = "{$field['where']}";
5913 if (in_array($name, $pseudoFields)) {
5914 if (!in_array($name, ['gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'])) {
5915 $wc = "contact_a.{$name}_id";
5917 $dataType = 'Positive';
5918 $value = (!$value) ?
0 : $value;
5920 if ($name == "world_region") {
5921 $field['name'] = $name;
5924 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue($daoName, $field['name'], $value, $op);
5925 $this->_qill
[$grouping][] = ts("%1 %2 %3", [1 => $label, 2 => $qillop, 3 => $qillVal]);
5926 $this->_where
[$grouping][] = self
::buildClause($wc, $op, $value, $dataType);
5930 * Check and explode a user defined numeric string into an array
5931 * this was the protocol used by search builder in the old old days before we had
5932 * super nice js widgets to do the hard work
5934 * @param string $string
5935 * @param string $dataType
5936 * The dataType we should check for the values, default integer.
5938 * @return bool|array
5939 * false if string does not match the pattern
5940 * array of numeric values if string does match the pattern
5942 public static function parseSearchBuilderString($string, $dataType = 'Integer') {
5943 $string = trim($string);
5944 if (substr($string, 0, 1) != '(' ||
substr($string, -1, 1) != ')') {
5948 $string = substr($string, 1, -1);
5949 $values = explode(',', $string);
5950 if (empty($values)) {
5955 foreach ($values as $v) {
5956 if ($dataType == 'Integer' && !is_numeric($v)) {
5959 elseif ($dataType == 'String' && !is_string($v)) {
5962 $returnValues[] = trim($v);
5965 if (empty($returnValues)) {
5969 return $returnValues;
5973 * Convert the pseudo constants id's to their names
5975 * @param CRM_Core_DAO $dao
5976 * @param bool $return
5977 * @param bool $usedForAPI
5979 * @return array|NULL
5981 public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) {
5982 if (empty($this->_pseudoConstantsSelect
)) {
5986 foreach ($this->_pseudoConstantsSelect
as $key => $value) {
5987 if (!empty($this->_pseudoConstantsSelect
[$key]['sorting'])) {
5991 if (is_object($dao) && property_exists($dao, $value['idCol'])) {
5992 $val = $dao->{$value['idCol']};
5993 if ($key == 'groups') {
5994 $dao->groups
= $this->convertGroupIDStringToLabelString($dao, $val);
5998 if (CRM_Utils_System
::isNull($val)) {
6001 elseif (!empty($value['pseudoconstant'])) {
6002 // If pseudoconstant is set that is kind of defacto for 'we have a bit more info about this'
6003 // and we can use the metadata to figure it out.
6004 // ideally this bit of IF will absorb & replace all the rest in time as we move to
6005 // more metadata based choices.
6006 if (strpos($val, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE) {
6007 $dbValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($val, CRM_Core_DAO
::VALUE_SEPARATOR
));
6008 foreach ($dbValues as $pseudoValue) {
6009 $convertedValues[] = CRM_Core_PseudoConstant
::getLabel($value['bao'], $value['idCol'], $pseudoValue);
6012 $dao->$key = ($usedForAPI) ?
$convertedValues : implode(', ', $convertedValues);
6013 $realFieldName = CRM_Utils_Array
::value('field_name', $this->_pseudoConstantsSelect
[$key]);
6014 if ($usedForAPI && $realFieldName) {
6015 // normally we would see 2 fields returned for pseudoConstants. An exception is
6016 // preferred_communication_method where there is no id-variant.
6017 // For the api we prioritise getting the real data returned.
6018 // over the resolved version
6019 $dao->$realFieldName = $dbValues;
6024 // This is basically the same as the default but since we have the bao we can use
6025 // a cached function.
6026 $dao->$key = CRM_Core_PseudoConstant
::getLabel($value['bao'], $value['idCol'], $val);
6029 elseif ($baoName = CRM_Utils_Array
::value('bao', $value, NULL)) {
6031 $idColumn = "{$key}_id";
6032 $dao->$idColumn = $val;
6034 if ($key == 'state_province_name') {
6035 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::stateProvince($val);
6038 $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant
::getLabel($baoName, $value['pseudoField'], $val);
6041 elseif ($value['pseudoField'] == 'state_province_abbreviation') {
6042 $dao->$key = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($val);
6044 // @todo handle this in the section above for pseudoconstants.
6045 elseif (in_array($value['pseudoField'], ['participant_role_id', 'participant_role'])) {
6046 // @todo define bao on this & merge into the above condition.
6047 $viewValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $val);
6049 if ($value['pseudoField'] == 'participant_role') {
6050 $pseudoOptions = CRM_Core_PseudoConstant
::get('CRM_Event_DAO_Participant', 'role_id');
6051 foreach ($viewValues as $k => $v) {
6052 $viewValues[$k] = $pseudoOptions[$v];
6055 $dao->$key = ($usedForAPI && count($viewValues) > 1) ?
$viewValues : implode(', ', $viewValues);
6058 $labels = CRM_Core_OptionGroup
::values($value['pseudoField']);
6059 $dao->$key = $labels[$val];
6062 // return converted values in array format
6064 if (strpos($key, '-') !== FALSE) {
6065 $keyVal = explode('-', $key);
6066 $current = &$values;
6067 $lastElement = array_pop($keyVal);
6068 foreach ($keyVal as $v) {
6069 if (!array_key_exists($v, $current)) {
6072 $current = &$current[$v];
6074 $current[$lastElement] = $dao->$key;
6077 $values[$key] = $dao->$key;
6083 foreach ($this->legacyHackedFields
as $realField => $labelField) {
6084 // This is a temporary routine for handling these fields while
6085 // we figure out how to handled them based on metadata in
6086 /// export and search builder. CRM-19815, CRM-19830.
6087 if (isset($dao->$realField) && is_numeric($dao->$realField) && isset($dao->$labelField)) {
6088 $dao->$realField = $dao->$labelField;
6096 * Include pseudo fields LEFT JOIN.
6097 * @param string|array $sort can be a object or string
6099 * @return array|NULL
6101 public function includePseudoFieldsJoin($sort) {
6102 if (!$sort ||
empty($this->_pseudoConstantsSelect
)) {
6105 $sort = is_string($sort) ?
$sort : $sort->orderBy();
6108 foreach ($this->_pseudoConstantsSelect
as $name => $value) {
6109 if (!empty($value['table'])) {
6110 $regex = "/({$value['table']}\.|{$name})/";
6111 if (preg_match($regex, $sort)) {
6112 $this->_elemnt
[$value['element']] = 1;
6113 $this->_select
[$value['element']] = $value['select'];
6114 $this->_pseudoConstantsSelect
[$name]['sorting'] = 1;
6115 $present[$value['table']] = $value['join'];
6119 $presentSimpleFrom = $present;
6121 if (array_key_exists('civicrm_worldregion', $this->_whereTables
) &&
6122 array_key_exists('civicrm_country', $presentSimpleFrom)
6124 unset($presentSimpleFrom['civicrm_country']);
6126 if (array_key_exists('civicrm_worldregion', $this->_tables
) &&
6127 array_key_exists('civicrm_country', $present)
6129 unset($present['civicrm_country']);
6132 $presentClause = $presentSimpleFromClause = NULL;
6133 if (!empty($present)) {
6134 $presentClause = implode(' ', $present);
6136 if (!empty($presentSimpleFrom)) {
6137 $presentSimpleFromClause = implode(' ', $presentSimpleFrom);
6140 $this->_fromClause
= $this->_fromClause
. $presentClause;
6141 $this->_simpleFromClause
= $this->_simpleFromClause
. $presentSimpleFromClause;
6143 return [$presentClause, $presentSimpleFromClause];
6147 * Build qill for field.
6149 * Qill refers to the query detail visible on the UI.
6151 * @param string $daoName
6152 * @param string $fieldName
6153 * @param mixed $fieldValue
6155 * @param array $pseudoExtraParam
6157 * Type of the field per CRM_Utils_Type
6161 public static function buildQillForFieldValue(
6166 $pseudoExtraParam = [],
6167 $type = CRM_Utils_Type
::T_STRING
6169 $qillOperators = CRM_Core_SelectValues
::getSearchBuilderOperators();
6171 //API usually have fieldValue format as array(operator => array(values)),
6172 //so we need to separate operator out of fieldValue param
6173 if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
6174 $op = key($fieldValue);
6175 $fieldValue = $fieldValue[$op];
6178 // if Operator chosen is NULL/EMPTY then
6179 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
6180 return [CRM_Utils_Array
::value($op, $qillOperators, $op), ''];
6183 if ($fieldName == 'activity_type_id') {
6184 $pseudoOptions = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
6186 elseif ($fieldName == 'country_id') {
6187 $pseudoOptions = CRM_Core_PseudoConstant
::country();
6189 elseif ($fieldName == 'county_id') {
6190 $pseudoOptions = CRM_Core_PseudoConstant
::county();
6192 elseif ($fieldName == 'world_region') {
6193 $pseudoOptions = CRM_Core_PseudoConstant
::worldRegion();
6195 elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') {
6196 $checkPermission = CRM_Utils_Array
::value('check_permission', $pseudoExtraParam, TRUE);
6197 $pseudoOptions = CRM_Event_BAO_Event
::getEvents(0, $fieldValue, TRUE, $checkPermission, TRUE);
6199 elseif ($fieldName == 'contribution_product_id') {
6200 $pseudoOptions = CRM_Contribute_PseudoConstant
::products();
6202 elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') {
6203 $pseudoOptions = CRM_Core_PseudoConstant
::group();
6205 elseif ($daoName == 'CRM_Batch_BAO_EntityBatch' && $fieldName == 'batch_id') {
6206 $pseudoOptions = CRM_Contribute_PseudoConstant
::batch();
6209 $pseudoOptions = CRM_Core_PseudoConstant
::get($daoName, $fieldName, $pseudoExtraParam);
6212 if (is_array($fieldValue)) {
6214 if (!empty($pseudoOptions)) {
6215 foreach ((array) $fieldValue as $val) {
6216 $qillString[] = CRM_Utils_Array
::value($val, $pseudoOptions, $val);
6218 $fieldValue = implode(', ', $qillString);
6221 if ($type == CRM_Utils_Type
::T_DATE
) {
6222 foreach ($fieldValue as $index => $value) {
6223 $fieldValue[$index] = CRM_Utils_Date
::customFormat($value);
6227 // @todo - this is a bit specific (one operator).
6228 // However it is covered by a unit test so can be altered later with
6230 if ($op == 'BETWEEN') {
6231 $separator = ' AND ';
6233 $fieldValue = implode($separator, $fieldValue);
6236 elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) {
6237 $fieldValue = $pseudoOptions[$fieldValue];
6239 elseif ($type === CRM_Utils_Type
::T_DATE
) {
6240 $fieldValue = CRM_Utils_Date
::customFormat($fieldValue);
6243 return [CRM_Utils_Array
::value($op, $qillOperators, $op), $fieldValue];
6247 * Alter value to reflect wildcard settings.
6249 * The form will have tried to guess whether this is a good field to wildcard but there is
6250 * also a site-wide setting that specifies whether it is OK to append the wild card to the beginning
6251 * or only the end of the string
6253 * @param bool $wildcard
6254 * This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard.
6256 * Generally '=' or 'LIKE'.
6257 * @param string $value
6258 * The search string.
6262 public static function getWildCardedValue($wildcard, $op, $value) {
6263 if ($wildcard && $op == 'LIKE') {
6264 if (CRM_Core_Config
::singleton()->includeWildCardInName
&& (substr($value, 0, 1) != '%')) {
6277 * Process special fields of Search Form in OK (Operator in Key) format
6279 * @param array $formValues
6280 * @param array $specialFields
6281 * Special params to be processed
6282 * @param array $changeNames
6283 * Array of fields whose name should be changed
6285 public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = []) {
6286 // Array of special fields whose value are considered only for NULL or EMPTY operators
6287 $nullableFields = ['contribution_batch_id'];
6289 foreach ($specialFields as $element) {
6290 $value = CRM_Utils_Array
::value($element, $formValues);
6292 if (is_array($value)) {
6293 if (in_array($element, array_keys($changeNames))) {
6294 unset($formValues[$element]);
6295 $element = $changeNames[$element];
6297 $formValues[$element] = ['IN' => $value];
6299 elseif (in_array($value, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) {
6300 $formValues[$element] = [$value => 1];
6302 elseif (!in_array($element, $nullableFields)) {
6303 // if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
6304 $isWilcard = strstr($value, '%') ?
FALSE : CRM_Core_Config
::singleton()->includeWildCardInName
;
6305 $formValues[$element] = ['LIKE' => self
::getWildCardedValue($isWilcard, 'LIKE', $value)];
6312 * Parse and assimilate the various sort options.
6314 * Side-effect: if sorting on a common column from a related table (`city`, `postal_code`,
6315 * `email`), the related table may be joined automatically.
6317 * At time of writing, this code is deeply flawed and should be rewritten. For the moment,
6318 * it's been extracted to a standalone function.
6320 * @param string|CRM_Utils_Sort $sort
6321 * The order by string.
6322 * @param null $sortOrder
6323 * Who knows? Hu knows. He who knows Hu knows who.
6326 * list(string $orderByClause, string $additionalFromClause).
6328 protected function prepareOrderBy($sort, $sortOrder) {
6332 if (CRM_Core_Config
::singleton()->includeOrderByClause ||
6333 isset($this->_distinctComponentClause
)
6336 if (is_string($sort)) {
6340 $orderBy = trim($sort->orderBy());
6342 // Deliberately remove the backticks again, as they mess up the evil
6343 // string munging below. This balanced by re-escaping before use.
6344 $orderBy = str_replace('`', '', $orderBy);
6346 if (!empty($orderBy)) {
6347 // this is special case while searching for
6348 // change log CRM-1718
6349 if (preg_match('/sort_name/i', $orderBy)) {
6350 $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy);
6354 $orderBy .= " $sortOrder";
6357 // always add contact_a.id to the ORDER clause
6358 // so the order is deterministic
6359 if (strpos('contact_a.id', $orderBy) === FALSE) {
6360 $orderBy .= ", contact_a.id";
6365 $orderBy = " contact_a.sort_name ASC, contact_a.id";
6371 // Remove this here & add it at the end for simplicity.
6372 $order = trim($orderBy);
6373 $orderByArray = explode(',', $order);
6375 foreach ($orderByArray as $orderByClause) {
6376 $orderByClauseParts = explode(' ', trim($orderByClause));
6377 $field = $orderByClauseParts[0];
6378 $direction = isset($orderByClauseParts[1]) ?
$orderByClauseParts[1] : 'asc';
6379 $fieldSpec = $this->getMetadataForRealField($field);
6381 // This is a hacky add-in for primary address joins. Feel free to iterate as it is unit tested.
6382 // @todo much more cleanup on location handling in addHierarchical elements. Potentially
6383 // add keys to $this->fields to represent the actual keys for locations.
6384 if (empty($fieldSpec) && substr($field, 0, 2) === '1-') {
6385 $fieldSpec = $this->getMetadataForField(substr($field, 2));
6386 $this->addAddressTable('1-' . str_replace('civicrm_', '', $fieldSpec['table_name']), 'is_primary = 1');
6389 if ($this->_returnProperties
=== []) {
6390 if (!empty($fieldSpec['table_name']) && !isset($this->_tables
[$fieldSpec['table_name']])) {
6391 $this->_tables
[$fieldSpec['table_name']] = 1;
6392 $order = $fieldSpec['where'] . ' ' . $direction;
6396 $cfID = CRM_Core_BAO_CustomField
::getKeyID($field);
6397 // add to cfIDs array if not present
6398 if (!empty($cfID) && !array_key_exists($cfID, $this->_cfIDs
)) {
6399 $this->_cfIDs
[$cfID] = [];
6400 $this->_customQuery
= new CRM_Core_BAO_CustomQuery($this->_cfIDs
, TRUE, $this->_locationSpecificCustomFields
);
6401 $this->_customQuery
->query();
6402 $this->_select
= array_merge($this->_select
, $this->_customQuery
->_select
);
6403 $this->_tables
= array_merge($this->_tables
, $this->_customQuery
->_tables
);
6406 // By replacing the join to the option value table with the mysql construct
6407 // ORDER BY field('contribution_status_id', 2,1,4)
6408 // we can remove a join. In the case of the option value join it is
6409 /// a join known to cause slow queries.
6410 // @todo cover other pseudoconstant types. Limited to option group ones & Foreign keys
6411 // matching an id+name parrern in the
6412 // first instance for scope reasons. They require slightly different handling as the column (label)
6413 // is not declared for them.
6414 // @todo so far only integer fields are being handled. If we add string fields we need to look at
6416 $pseudoConstantMetadata = CRM_Utils_Array
::value('pseudoconstant', $fieldSpec, FALSE);
6417 if (!empty($pseudoConstantMetadata)
6419 if (!empty($pseudoConstantMetadata['optionGroupName'])
6420 ||
$this->isPseudoFieldAnFK($fieldSpec)
6422 // dev/core#1305 @todo this is not the right thing to do but for now avoid fatal error
6423 if (empty($fieldSpec['bao'])) {
6426 $sortedOptions = $fieldSpec['bao']::buildOptions($fieldSpec['name'], NULL, [
6427 'orderColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoConstantMetadata, 'label'),
6429 $fieldIDsInOrder = implode(',', array_keys($sortedOptions));
6430 // Pretty sure this validation ALSO happens in the order clause & this can't be reached but...
6431 // this might give some early warning.
6432 CRM_Utils_Type
::validate($fieldIDsInOrder, 'CommaSeparatedIntegers');
6433 $order = str_replace("$field", "field({$fieldSpec['name']},$fieldIDsInOrder)", $order);
6435 //CRM-12565 add "`" around $field if it is a pseudo constant
6436 // This appears to be for 'special' fields like locations with appended numbers or hyphens .. maybe.
6437 if (!empty($pseudoConstantMetadata['element']) && $pseudoConstantMetadata['element'] == $field) {
6438 $order = str_replace($field, "`{$field}`", $order);
6443 $this->_fromClause
= self
::fromClause($this->_tables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6444 $this->_simpleFromClause
= self
::fromClause($this->_whereTables
, NULL, NULL, $this->_primaryLocation
, $this->_mode
);
6446 // The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
6447 // clause. But this magic helper which forgivingly reescapes ORDER BY.
6449 $order = CRM_Utils_Type
::escape($order, 'MysqlOrderBy');
6450 return ' ORDER BY ' . $order;
6455 * Convert a string of group IDs to a string of group labels.
6457 * The original string may include duplicates and groups the user does not have
6458 * permission to see.
6460 * @param CRM_Core_DAO $dao
6461 * @param string $val
6465 public function convertGroupIDStringToLabelString(&$dao, $val) {
6466 $groupIDs = explode(',', $val);
6467 // Note that groups that the user does not have permission to will be excluded (good).
6468 $groups = array_intersect_key(CRM_Core_PseudoConstant
::group(), array_flip($groupIDs));
6469 return implode(', ', $groups);
6474 * Set the qill and where properties for a field.
6476 * This function is intended as a short-term function to encourage refactoring
6477 * & re-use - but really we should just have less special-casing.
6479 * @param string $name
6481 * @param string|array $value
6482 * @param string $grouping
6483 * @param string $field
6485 public function setQillAndWhere($name, $op, $value, $grouping, $field) {
6486 $this->_where
[$grouping][] = self
::buildClause("contact_a.{$name}", $op, $value);
6487 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
6488 $this->_qill
[$grouping][] = ts("%1 %2 %3", [
6489 1 => $field['title'],
6496 * Has the pseudoconstant of the field been requested.
6498 * For example if the field is payment_instrument_id then it
6499 * has been requested if either payment_instrument_id or payment_instrument
6500 * have been requested. Payment_instrument is the option groun name field value.
6502 * @param array $field
6503 * @param string $fieldName
6504 * The unique name of the field - ie. the one it will be aliased to in the query.
6508 private function pseudoConstantNameIsInReturnProperties($field, $fieldName = NULL) {
6509 $realField = $this->getMetadataForRealField($fieldName);
6510 if (!isset($realField['pseudoconstant'])) {
6513 $pseudoConstant = $realField['pseudoconstant'];
6514 if (empty($pseudoConstant['optionGroupName']) &&
6515 CRM_Utils_Array
::value('labelColumn', $pseudoConstant) !== 'name') {
6516 // We are increasing our pseudoconstant handling - but still very cautiously,
6517 // hence the check for labelColumn === name
6521 if (!empty($pseudoConstant['optionGroupName']) && CRM_Utils_Array
::value($pseudoConstant['optionGroupName'], $this->_returnProperties
)) {
6524 if (CRM_Utils_Array
::value($fieldName, $this->_returnProperties
)) {
6527 // Is this still required - the above goes off the unique name. Test with things like
6528 // communication_preferences & prefix_id.
6529 if (CRM_Utils_Array
::value($field['name'], $this->_returnProperties
)) {
6536 * Get Select Clause.
6540 public function getSelect() {
6541 $select = "SELECT ";
6542 if (isset($this->_distinctComponentClause
)) {
6543 $select .= "{$this->_distinctComponentClause}, ";
6545 $select .= implode(', ', $this->_select
);
6550 * Add basic statistics to the summary.
6552 * @param array $summary
6553 * @param string $where
6554 * @param string $from
6558 * @throws \CRM_Core_Exception
6560 protected function addBasicStatsToSummary(&$summary, $where, $from) {
6561 $summary['total']['count'] = 0;
6562 $summary['total']['amount'] = $summary['total']['avg'] = [];
6565 SELECT COUNT( conts.total_amount ) as total_count,
6566 SUM( conts.total_amount ) as total_amount,
6567 AVG( conts.total_amount ) as total_avg,
6568 conts.currency as currency
6570 SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
6571 civicrm_contribution.currency
6573 $where AND civicrm_contribution.contribution_status_id = 1
6574 GROUP BY civicrm_contribution.id
6578 $dao = CRM_Core_DAO
::executeQuery($query);
6580 while ($dao->fetch()) {
6581 $summary['total']['count'] +
= $dao->total_count
;
6582 $summary['total']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
6583 $summary['total']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
6586 if (!empty($summary['total']['amount'])) {
6587 $summary['total']['amount'] = implode(', ', $summary['total']['amount']);
6588 $summary['total']['avg'] = implode(', ', $summary['total']['avg']);
6591 $summary['total']['amount'] = $summary['total']['avg'] = 0;
6597 * Add basic soft credit statistics to summary array.
6599 * @param array $summary
6600 * @param string $where
6601 * @param string $from
6603 protected function addBasicSoftCreditStatsToStats(&$summary, $where, $from) {
6605 SELECT COUNT( conts.total_amount ) as total_count,
6606 SUM( conts.total_amount ) as total_amount,
6607 AVG( conts.total_amount ) as total_avg,
6608 conts.currency as currency
6610 SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency
6612 $where AND civicrm_contribution.contribution_status_id = 1 AND civicrm_contribution_soft.id IS NOT NULL
6613 GROUP BY civicrm_contribution_soft.id
6617 $dao = CRM_Core_DAO
::executeQuery($query);
6618 $summary['soft_credit']['count'] = 0;
6619 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = [];
6620 while ($dao->fetch()) {
6621 $summary['soft_credit']['count'] +
= $dao->total_count
;
6622 $summary['soft_credit']['amount'][] = CRM_Utils_Money
::format($dao->total_amount
, $dao->currency
);
6623 $summary['soft_credit']['avg'][] = CRM_Utils_Money
::format($dao->total_avg
, $dao->currency
);
6625 if (!empty($summary['soft_credit']['amount'])) {
6626 $summary['soft_credit']['amount'] = implode(', ', $summary['soft_credit']['amount']);
6627 $summary['soft_credit']['avg'] = implode(', ', $summary['soft_credit']['avg']);
6630 $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0;
6635 * Add basic stats about cancelled contributions to the summary.
6637 * @param array $summary
6638 * @param string $where
6639 * @param string $from
6641 protected function addBasicCancelStatsToSummary(&$summary, $where, $from) {
6643 SELECT COUNT( conts.total_amount ) as cancel_count,
6644 SUM( conts.total_amount ) as cancel_amount,
6645 AVG( conts.total_amount ) as cancel_avg,
6646 conts.currency as currency
6648 SELECT civicrm_contribution.total_amount, civicrm_contribution.currency
6650 $where AND civicrm_contribution.cancel_date IS NOT NULL
6651 GROUP BY civicrm_contribution.id
6655 $dao = CRM_Core_DAO
::executeQuery($query);
6658 if ($dao->fetch()) {
6659 $summary['cancel']['count'] = $dao->cancel_count
;
6660 $summary['cancel']['amount'] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
6661 $summary['cancel']['avg'] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
6665 $summary['cancel']['count'] = 0;
6666 $summary['cancel']['amount'] = $summary['cancel']['avg'] = [];
6667 while ($dao->fetch()) {
6668 $summary['cancel']['count'] +
= $dao->cancel_count
;
6669 $summary['cancel']['amount'][] = CRM_Utils_Money
::format($dao->cancel_amount
, $dao->currency
);
6670 $summary['cancel']['avg'][] = CRM_Utils_Money
::format($dao->cancel_avg
, $dao->currency
);
6672 $summary['cancel']['amount'] = implode(', ', $summary['cancel']['amount']);
6673 $summary['cancel']['avg'] = implode(', ', $summary['cancel']['avg']);
6678 * Create the sql query for an contact search.
6680 * @param int $offset
6681 * The offset for the query.
6682 * @param int $rowCount
6683 * The number of rows to return.
6684 * @param string|CRM_Utils_Sort $sort
6685 * The order by string.
6686 * @param bool $count
6687 * Is this a count only query ?.
6688 * @param bool $includeContactIds
6689 * Should we include contact ids?.
6690 * @param bool $sortByChar
6691 * If true returns the distinct array of first characters for search results.
6692 * @param bool $groupContacts
6693 * If true, return only the contact ids.
6694 * @param string $additionalWhereClause
6695 * If the caller wants to further restrict the search (used for components).
6696 * @param null $sortOrder
6697 * @param string $additionalFromClause
6698 * Should be clause with proper joins, effective to reduce where clause load.
6700 * @param bool $skipOrderAndLimit
6703 public function getSearchSQL(
6704 $offset = 0, $rowCount = 0, $sort = NULL,
6705 $count = FALSE, $includeContactIds = FALSE,
6706 $sortByChar = FALSE, $groupContacts = FALSE,
6707 $additionalWhereClause = NULL, $sortOrder = NULL,
6708 $additionalFromClause = NULL, $skipOrderAndLimit = FALSE) {
6710 $sqlParts = $this->getSearchSQLParts($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause);
6713 CRM_Core_Error
::deprecatedFunctionWarning('sort by char is deprecated - use alphabetQuery method');
6714 $sqlParts['order_by'] = 'ORDER BY sort_name asc';
6717 if ($skipOrderAndLimit) {
6718 CRM_Core_Error
::deprecatedFunctionWarning('skipOrderAndLimit is deprected - call getSearchSQLParts & construct it in the calling function');
6719 $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']}";
6722 $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']} {$sqlParts['order_by']} {$sqlParts['limit']}";
6728 * Get the component parts of the search query as an array.
6730 * @param int $offset
6731 * The offset for the query.
6732 * @param int $rowCount
6733 * The number of rows to return.
6734 * @param string|CRM_Utils_Sort $sort
6735 * The order by string.
6736 * @param bool $count
6737 * Is this a count only query ?.
6738 * @param bool $includeContactIds
6739 * Should we include contact ids?.
6740 * @param bool $sortByChar
6741 * If true returns the distinct array of first characters for search results.
6742 * @param bool $groupContacts
6743 * If true, return only the contact ids.
6744 * @param string $additionalWhereClause
6745 * If the caller wants to further restrict the search (used for components).
6746 * @param null $sortOrder
6747 * @param string $additionalFromClause
6748 * Should be clause with proper joins, effective to reduce where clause load.
6752 public function getSearchSQLParts($offset = 0, $rowCount = 0, $sort = NULL,
6753 $count = FALSE, $includeContactIds = FALSE,
6754 $sortByChar = FALSE, $groupContacts = FALSE,
6755 $additionalWhereClause = NULL, $sortOrder = NULL,
6756 $additionalFromClause = NULL) {
6757 if ($includeContactIds) {
6758 $this->_includeContactIds
= TRUE;
6759 $this->_whereClause
= $this->whereClause();
6761 $onlyDeleted = in_array([
6769 // if we’re explicitly looking for a certain contact’s contribs, events, etc.
6770 // and that contact happens to be deleted, set $onlyDeleted to true
6771 foreach ($this->_params
as $values) {
6772 $name = CRM_Utils_Array
::value(0, $values);
6773 $op = CRM_Utils_Array
::value(1, $values);
6774 $value = CRM_Utils_Array
::value(2, $values);
6775 if ($name == 'contact_id' and $op == '=') {
6776 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) {
6777 $onlyDeleted = TRUE;
6783 // building the query string
6784 $groupBy = $groupByCols = NULL;
6786 if (isset($this->_groupByComponentClause
)) {
6787 $groupByCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause
));
6788 $groupByCols = explode(', ', $groupByCols);
6790 elseif ($this->_useGroupBy
) {
6791 $groupByCols = ['contact_a.id'];
6794 if ($this->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
&& (!$count)) {
6795 $groupByCols = ['civicrm_activity.id'];
6797 if (!empty($groupByCols)) {
6798 $groupBy = " GROUP BY " . implode(', ', $groupByCols);
6801 $order = $orderBy = '';
6804 $order = $this->prepareOrderBy($sort, $sortOrder);
6807 // Cases where we are disabling FGB (FULL_GROUP_BY_MODE):
6808 // 1. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT
6809 // clause. This will impact the search query output.
6810 $disableFullGroupByMode = (!empty($groupBy) ||
$groupContacts);
6812 if ($disableFullGroupByMode) {
6813 CRM_Core_DAO
::disableFullGroupByMode();
6817 $this->_sort
= $sort;
6820 $this->includePseudoFieldsJoin($sort);
6822 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);
6824 if ($additionalWhereClause) {
6825 $where = $where . ' AND ' . $additionalWhereClause;
6828 //additional from clause should be w/ proper joins.
6829 if ($additionalFromClause) {
6830 $from .= "\n" . $additionalFromClause;
6833 // if we are doing a transform, do it here
6834 // use the $from, $where and $having to get the contact ID
6835 if ($this->_displayRelationshipType
) {
6836 $this->filterRelatedContacts($from, $where, $having);
6838 $limit = (!$count && $rowCount) ?
" LIMIT " . CRM_Utils_Type
::escape($offset, 'Int') . ", " . CRM_Utils_Type
::escape($rowCount, 'Int') : '';
6841 'select' => $select,
6844 'order_by' => $order,
6845 'group_by' => $groupBy,
6846 'having' => $having,
6852 * Get the metadata for a given field.
6854 * @param string $fieldName
6858 protected function getMetadataForField($fieldName) {
6859 if ($fieldName === 'contact_a.id') {
6860 // This seems to be the only anomaly.
6863 $pseudoField = isset($this->_pseudoConstantsSelect
[$fieldName]) ?
$this->_pseudoConstantsSelect
[$fieldName] : [];
6864 $field = isset($this->_fields
[$fieldName]) ?
$this->_fields
[$fieldName] : $pseudoField;
6865 $field = array_merge($field, $pseudoField);
6866 if (!empty($field) && empty($field['name'])) {
6867 // standardising field formatting here - over time we can phase out variants.
6868 // all paths using this currently unit tested
6869 $field['name'] = CRM_Utils_Array
::value('field_name', $field, CRM_Utils_Array
::value('idCol', $field, $fieldName));
6875 * Get the metadata for a given field, returning the 'real field' if it is a pseudofield.
6877 * @param string $fieldName
6881 protected function getMetadataForRealField($fieldName) {
6882 $field = $this->getMetadataForField($fieldName);
6883 if (!empty($field['is_pseudofield_for'])) {
6884 $field = $this->getMetadataForField($field['is_pseudofield_for']);
6885 $field['pseudofield_name'] = $fieldName;
6887 elseif (!empty($field['pseudoconstant'])) {
6888 if (!empty($field['pseudoconstant']['optionGroupName'])) {
6889 $field['pseudofield_name'] = $field['pseudoconstant']['optionGroupName'];
6890 if (empty($field['table_name'])) {
6891 if (!empty($field['where'])) {
6892 $field['table_name'] = explode('.', $field['where'])[0];
6895 $field['table_name'] = 'civicrm_contact';
6904 * If we have a field that is better rendered via the pseudoconstant handled them here.
6906 * Rather than joining in the additional table we render the option value on output.
6908 * @todo - so far this applies to a narrow range of pseudocontants. We are adding them
6909 * carefully with test coverage but aim to extend.
6911 * @param string $name
6913 protected function addPseudoconstantFieldToSelect($name) {
6914 $field = $this->getMetadataForRealField($name);
6915 $realFieldName = $field['name'];
6916 $pseudoFieldName = CRM_Utils_Array
::value('pseudofield_name', $field);
6917 if ($pseudoFieldName) {
6918 // @todo - we don't really need to build this array now we have metadata more available with getMetadataForField fn.
6919 $this->_pseudoConstantsSelect
[$pseudoFieldName] = [
6920 'pseudoField' => $pseudoFieldName,
6921 'idCol' => $realFieldName,
6922 'field_name' => $field['name'],
6923 'bao' => $field['bao'],
6924 'pseudoconstant' => $field['pseudoconstant'],
6928 $this->_tables
[$field['table_name']] = 1;
6929 $this->_element
[$realFieldName] = 1;
6930 $this->_select
[$field['name']] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$realFieldName`");
6934 * Is this pseudofield a foreign key constraint.
6936 * We are trying to cautiously expand our pseudoconstant handling. This check allows us
6937 * to extend to a narrowly defined type (and then only if the pseudofield is in the fields
6938 * array which is done for contributions which are mostly handled as pseudoconstants.
6944 protected function isPseudoFieldAnFK($fieldSpec) {
6945 if (empty($fieldSpec['FKClassName'])
6946 || CRM_Utils_Array
::value('keyColumn', $fieldSpec['pseudoconstant']) !== 'id'
6947 || CRM_Utils_Array
::value('labelColumn', $fieldSpec['pseudoconstant']) !== 'name') {
6954 * Is the field a relative date field.
6956 * @param string $fieldName
6960 protected function isARelativeDateField($fieldName) {
6961 if (substr($fieldName, -9, 9) !== '_relative') {
6964 $realField = substr($fieldName, 0, strlen($fieldName) - 9);
6965 return isset($this->_fields
[$realField]);
6969 * Get the specifications for the field, if available.
6971 * @param string $fieldName
6972 * Fieldname as displayed on the form.
6976 public function getFieldSpec($fieldName) {
6977 if (isset($this->_fields
[$fieldName])) {
6978 return $this->_fields
[$fieldName];
6980 $lowFieldName = str_replace('_low', '', $fieldName);
6981 if (isset($this->_fields
[$lowFieldName])) {
6982 return array_merge($this->_fields
[$lowFieldName], ['field_name' => $lowFieldName]);
6984 $highFieldName = str_replace('_high', '', $fieldName);
6985 if (isset($this->_fields
[$highFieldName])) {
6986 return array_merge($this->_fields
[$highFieldName], ['field_name' => $highFieldName]);
6991 public function buildWhereForDate() {
6996 * Is the field a relative date field.
6998 * @param string $fieldName
7002 protected function isADateRangeField($fieldName) {
7003 if (substr($fieldName, -4, 4) !== '_low' && substr($fieldName, -5, 5) !== '_high') {
7006 return !empty($this->getFieldSpec($fieldName));
7012 protected function buildRelativeDateQuery(&$values) {
7013 $value = CRM_Utils_Array
::value(2, $values);
7014 if (empty($value)) {
7017 $fieldName = substr($values[0], 0, strlen($values[0]) - 9);
7018 $fieldSpec = $this->_fields
[$fieldName];
7019 $tableName = $fieldSpec['table_name'];
7020 $filters = CRM_Core_OptionGroup
::values('relative_date_filters');
7021 $grouping = CRM_Utils_Array
::value(3, $values);
7022 // If the table value is already set for a custom field it will be more nuanced than just '1'.
7023 $this->_tables
[$tableName] = $this->_tables
[$tableName] ??
1;
7024 $this->_whereTables
[$tableName] = $this->_whereTables
[$tableName] ??
1;
7026 $dates = CRM_Utils_Date
::getFromTo($value, NULL, NULL);
7027 if (empty($dates[0])) {
7028 // ie. no start date we only have end date
7029 $this->_where
[$grouping][] = $fieldSpec['where'] . " <= '{$dates[1]}'";
7031 $this->_qill
[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("to %1", [
7032 CRM_Utils_Date
::customFormat($dates[1]),
7035 elseif (empty($dates[1])) {
7036 // ie. no end date we only have start date
7037 $this->_where
[$grouping][] = $fieldSpec['where'] . " >= '{$dates[1]}'";
7039 $this->_qill
[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("from %1", [
7040 CRM_Utils_Date
::customFormat($dates[0]),
7044 // we have start and end dates.
7045 $this->_where
[$grouping][] = $fieldSpec['where'] . " BETWEEN '{$dates[0]}' AND '{$dates[1]}'";
7047 $this->_qill
[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("between %1 and %2", [
7048 CRM_Utils_Date
::customFormat($dates[0]),
7049 CRM_Utils_Date
::customFormat($dates[1]),
7055 * Build the query for a date field if it is a _high or _low field.
7061 public function buildDateRangeQuery($values) {
7062 if ($this->isADateRangeField($values[0])) {
7063 $fieldSpec = $this->getFieldSpec($values[0]);
7064 $title = empty($fieldSpec['unique_title']) ?
$fieldSpec['title'] : $fieldSpec['unique_title'];
7065 $this->dateQueryBuilder($values, $fieldSpec['table_name'], $fieldSpec['field_name'], $fieldSpec['name'], $title);
7072 * Add the address table into the query.
7074 * @param string $tableKey
7075 * @param string $joinCondition
7081 protected function addAddressTable($tableKey, $joinCondition) {
7082 $tName = "$tableKey-address";
7083 $aName = "`$tableKey-address`";
7084 $this->_select
["{$tName}_id"] = "`$tName`.id as `{$tName}_id`";
7085 $this->_element
["{$tName}_id"] = 1;
7086 $addressJoin = "\nLEFT JOIN civicrm_address $aName ON ($aName.contact_id = contact_a.id AND $aName.$joinCondition)";
7087 $this->_tables
[$tName] = $addressJoin;
7096 * Get the clause for group status.
7098 * @param int $grouping
7102 protected function getGroupStatusClause($grouping) {
7103 $statuses = $this->getSelectedGroupStatuses($grouping);
7104 return "status IN (" . implode(', ', $statuses) . ")";
7108 * Get an array of the statuses that have been selected.
7110 * @param string $grouping
7114 protected function getSelectedGroupStatuses($grouping) {
7116 $gcsValues = $this->getWhereValues('group_contact_status', $grouping);
7118 is_array($gcsValues[2])
7120 foreach ($gcsValues[2] as $k => $v) {
7122 $statuses[] = "'" . CRM_Utils_Type
::escape($k, 'String') . "'";
7127 $statuses[] = "'Added'";
7133 * Get the qill value for the field.
7136 * @param array|int|string $value
7141 protected function getQillForField($name, $value, $op): string {
7142 list($qillop, $qillVal) = CRM_Contact_BAO_Query
::buildQillForFieldValue(NULL, $name, $value, $op);
7143 return (string) ts("%1 %2 %3", [