class CRM_Contact_BAO_Query

This is the heart of the search query building mechanism.

Constants

NO_RETURN_PROPERTIES

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_CONTACTS

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_CONTRIBUTE

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_MEMBER

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_EVENT

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_GRANT

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_PLEDGEBANK

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_PLEDGE

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_CASE

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_ACTIVITY

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_CAMPAIGN

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_MAILING

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

MODE_ALL

The various search modes.

As of February 2017, entries not present for 4, 32, 64, 1024.

MODE_ALL seems to be out of sync with the available constants; if this is intentionally excluding MODE_MAILING then that may bear documenting?

Likewise if there's reason for the missing modes (4, 32, 64 etc).

Properties

static array $_defaultReturnProperties The default set of return properties.
static array $_defaultHierReturnProperties The default set of hier return properties.
array $_params The set of input params.
$_cfIDs
$_paramLookup
$_sort
array $_returnProperties The set of output params
array $_select The select clause
array $_element The name of the elements that are in the select clause used to extract the values.
array $_tables The tables involved in the query.
array $_whereTables The table involved in the where clause.
array $_where Array of WHERE clause components.
string $_whereClause The WHERE clause as a string.
string $_permissionWhereClause Additional WHERE clause for permissions.
string $_fromClause The from string
string $_permissionFromClause Additional permission from clause
string $_simpleFromClause The from clause for the simple select and alphabetical select
string $_having The having values
array $_qill The english language version of the query
array $_fields All the fields that could potentially be involved in this query
array $_options The cache to translate the option values into labels.
$_search Are we in search mode.
boolean $_skipPermission Should we skip permission checking.
boolean $_skipDeleteClause Should we skip adding of delete clause.
boolean $_strict Are we in strict mode (use equality over LIKE)
string $_operator What operator to use to group the clauses.
$_mode
boolean $_primaryLocation Should we only search on primary location.
boolean $_includeContactIds Are contact ids part of the query.
boolean $_smartGroupCache Should we use the smart group cache.
string $_displayRelationshipType Should we display contacts with a specific relationship type.
Object $_customQuery Reference to the query object for custom values.
boolean $_useDistinct Should we enable the distinct clause, used if we are including more than one group
$_useGroupBy Should we just display one contact record
static array $_relType The relationship type direction
static array $_activityRole The activity role
static array $_considerCompActivities Consider the component activity type during activity search.
static array $_withContactActivitiesOnly Consider with contact activities only, during activity search.
string $_distinctComponentClause Use distinct component clause for component searches
$_rowCountClause
string $_groupByComponentClause Use groupBy component clause for component searches
static array $_openedPanes Track open panes, useful in advance search
array $_locationSpecificCustomFields For search builder - which custom fields are location-dependent
static array $_dependencies The tables which have a dependency on location and/or address
static $_locationSpecificFields List of location specific fields.
protected $_rangeCache Remember if we handle either end of a number or date range so we can skip the other
protected Boolean $_relationshipValuesAdded Set to true when $this->relationship is run to avoid adding twice
static String $_relationshipTempTable Set to the name of the temp table if one has been created
$_pseudoConstantsSelect

Methods

__construct(array $params = NULL, array $returnProperties = NULL, array $fields = NULL, bool $includeContactIds = FALSE, bool $strict = FALSE, bool|int $mode = 1, bool $skipPermission = FALSE, bool $searchDescendentGroups = TRUE, bool $smartGroupCache = TRUE, null $displayRelationshipType = NULL, string $operator = 'AND', string $apiEntity = NULL)

Class constructor which also does all the work.

initialize(string $apiEntity = NULL)

Function which actually does all the work for the constructor.

buildParamsLookup()

Function for same purpose as convertFormValues.

addSpecialFields($apiEntity)

Some composite fields do not appear in the fields array hack to make them part of the query.

selectClause(string $apiEntity = NULL)

Given a list of conditions in params and a list of desired return Properties generate the required select and from clauses. Note that since the where clause introduces new tables, the initial attempt also retrieves all variables used in the params list

addHierarchicalElements()

If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.

addMultipleElements()

If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.

array
query(bool $count = FALSE, bool $sortByChar = FALSE, bool $groupContacts = FALSE, bool $onlyDeleted = FALSE)

Generate the query based on what type of query we need.

mixed
getWhereValues(string $name, mixed $grouping)

Get where values from the parameters.

static 
fixDateValues(bool $relative, string $from, string $to)

Fix date values.

static array
convertFormValues(array $formValues, int $wildcard, bool $useEquals = FALSE, string $apiEntity = NULL, array $entityReferenceFields = array())

Convert values from form-appropriate to query-object appropriate.

static 
legacyConvertFormValues(string $id, array|int $values)

Function to support legacy format for groups and tags.

static array|null
fixWhereValues(int $id, array $values, int $wildcard, bool $useEquals = FALSE, string $apiEntity = NULL)

Fix values from query from/to something no-one cared enough to document.

whereClauseSingle(array $values, string $apiEntity = NULL)

Get the where clause for a single field.

string
whereClause(string $apiEntity = NULL)

Given a list of conditions in params generate the required where clause.

restWhere(array $values)

Generate where clause for any parameters not already handled.

static array
getLocationTableName($where, $locType)

No description

array
store(CRM_Core_DAO $dao)

Given a result dao, extract the values and return that array

array
tables()

Getter for tables array.

array
whereTables()

Sometimes used to create the from clause, but, not reliably, set this AND set tables.

static string
getWhereClause(array $params, array $fields, array $tables, $whereTables, bool $strict = FALSE)

Generate the where clause (used in match contacts and permissions)

static string
fromClause(array $tables, array $inner = NULL, array $right = NULL, bool $primaryLocation = TRUE, int $mode = 1, string|NULL $apiEntity = NULL)

Create the from clause.

deletedContacts(array $values)

WHERE / QILL clause for deleted_contacts

contactType($values)

Where / qill clause for contact_type

contactSubType(array $values)

Where / qill clause for contact_sub_type.

includeContactSubTypes($value, $grouping, string $op = 'LIKE')

No description

group($values)

Where / qill clause for groups.

array
getGroupsFromTypeCriteria($value)

Function translates selection of group type into a list of groups.

null|string
addGroupContactCache(array $groups, string $tableAlias = NULL, string $joinTable = "contact_a", string $op)

No description

ufUser($values)

Where / qill clause for cms users

tagSearch(array $values)

All tag search specific.

tag(array $values)

Where / qill clause for tag.

notes(array $values)

Where/qill clause for notes

bool
nameNullOrEmptyOp(string $name, $op, $grouping)

No description

sortName(array $values)

Where / qill clause for sort_name

greetings(array $values)

Where/qill clause for greeting fields.

email(array $values, string $apiEntity)

Where / qill clause for email

phone_numeric(array $values)

Where / qill clause for phone number

phone_option_group(array $values)

Where / qill clause for phone type/location

street_address(array $values)

Where / qill clause for street_address.

street_number(array $values)

Where / qill clause for street_unit.

sortByCharacter(array $values)

Where / qill clause for sorting by character.

includeContactIDs()

Where / qill clause for including contact ids.

postalCode(array $values)

Where / qill clause for postal code.

string
locationType(array $values, null $status = NULL)

Where / qill clause for location type.

array|NULL
country($values, bool $fromStateProvince = TRUE)

No description

string
county(array $values, null $status = NULL)

Where / qill clause for county (if present).

string
stateProvince(array $values, null $status = NULL)

Where / qill clause for state/province AND country (if present).

changeLog(array $values)

Where / qill clause for change log.

modifiedDates($values)

No description

demographics($values)

No description

privacy($values)

No description

privacyOptions($values)

No description

preferredCommunication($values)

No description

relationship(array $values)

Where / qill clause for relationship.

addRelationshipDateClauses(string $grouping, array $where)

Add start & end date criteria in

static array
defaultReturnProperties(int $mode = 1)

Default set of return properties.

static string|NULL
getPrimaryCondition(int $value)

Get primary condition for a sql clause.

static string
getQuery(array $params = NULL, array $returnProperties = NULL, bool $count = FALSE)

Wrapper for a simple search query.

static array
apiQuery(array $params = NULL, array $returnProperties = NULL, null $fields = NULL, string $sort = NULL, int $offset, int $row_count = 25, bool $smartGroupCache = TRUE, bool $count = FALSE, bool $skipPermissions = TRUE, int $mode = CRM_Contact_BAO_Query::MODE_CONTACTS, string $apiEntity = NULL)

These are stub comments as this function needs more explanation - particularly in terms of how it relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.

static string
getCustomFieldName(string $parameterName)

Get the actual custom field name by stripping off the appended string.

static 
convertCustomRelativeFields(array $formValues, array $params, string $values, string $fieldName)

Convert submitted values for relative custom fields to query object format.

static bool
isCustomDateField($fieldName)

Are we dealing with custom field of type date.

static bool;
isAlreadyProcessedForQueryFormat(mixed $values)

Has this field already been reformatting to Query object syntax.

static 
filterCountryFromValuesIfStateExists(array $formValues)

If the state and country are passed remove state.

static string
appendAnyValueToSelect(array $selectClauses, array $groupBy)

For some special cases, grouping by subset of select fields becomes mandatory.

static string
getGroupByFromSelectColumns(array $selectClauses, array $groupBy = NULL)

Include Select columns in groupBy clause.

searchQuery(int $offset, int $rowCount, string|CRM_Utils_Sort $sort = NULL, bool $count = FALSE, bool $includeContactIds = FALSE, bool $sortByChar = FALSE, bool $groupContacts = FALSE, bool $returnQuery = FALSE, string $additionalWhereClause = NULL, null $sortOrder = NULL, string $additionalFromClause = NULL, bool $skipOrderAndLimit = FALSE)

Create and query the db for an contact search.

getCachedContacts(string $cacheKey, int $offset, int $rowCount, bool $includeContactIds)

Fetch a list of contacts from the prev/next cache for displaying a search results page

generatePermissionClause(bool $onlyDeleted = FALSE, bool $count = FALSE)

Populate $this->_permissionWhereClause with permission related clause and update other query related properties.

setSkipPermission($val)

No description

array
summaryContribution(null $context = NULL)

No description

string
qill()

Getter for the qill object.

static array
defaultHierReturnProperties()

Default set of return default hier return properties.

dateQueryBuilder(array $values, string $tableName, string $fieldName, string $dbFieldName, string $fieldTitle, bool $appendTimeStamp = TRUE)

Build query for a date field.

numberRangeBuilder($values, string $tableName, string $fieldName, string $dbFieldName, $fieldTitle, null $options = NULL)

No description

ageRangeQueryBuilder($values, string $tableName, string $fieldName, string $dbFieldName, $fieldTitle, null $options = NULL)

No description

static string
calcDateFromAge(string $asofDate, int $age, string $type)

Calculate date from age.

static string
buildClause(string $field, string $op, string $value = NULL, string $dataType = NULL)

Given the field name, operator, value & its data type builds the where Clause for the query used for handling 'IS NULL'/'IS NOT NULL' operators

array
openedSearchPanes(bool $reset = FALSE)

No description

setOperator($operator)

No description

string
getOperator()

No description

filterRelatedContacts($from, $where, $having)

No description

static bool
caseImportant($op)

See CRM-19811 for why this is database hurty without apparent benefit.

static bool
componentPresent($returnProperties, $prefix)

No description

optionValueQuery(string $name, string $op, string $value, int $grouping, string $daoName = NULL, array $field, string $label, string $dataType = 'String', bool $useIDsOnly = FALSE)

Builds the necessary structures for all fields that are similar to option value look-ups.

static bool|array
parseSearchBuilderString(string $string, string $dataType = 'Integer')

Check and explode a user defined numeric string into an array this was the protocol used by search builder in the old old days before we had super nice js widgets to do the hard work

array|NULL
convertToPseudoNames(CRM_Core_DAO $dao, bool $return = FALSE, bool $usedForAPI = FALSE)

Convert the pseudo constants id's to their names

array|NULL
includePseudoFieldsJoin(string|array $sort)

Include pseudo fields LEFT JOIN.

static array
buildQillForFieldValue(string $daoName, string $fieldName, mixed $fieldValue, string $op, array $pseudoExtraParam = array(), int $type = CRM_Utils_Type::T_STRING)

Build qill for field.

static string
getWildCardedValue(bool $wildcard, string $op, string $value)

Alter value to reflect wildcard settings.

static 
processSpecialFormValue(array $formValues, array $specialFields, array $changeNames = array())

Process special fields of Search Form in OK (Operator in Key) format

array
prepareOrderBy(string|CRM_Utils_Sort $sort, bool $sortByChar, null $sortOrder, string $additionalFromClause)

Parse and assimilate the various sort options.

string
convertGroupIDStringToLabelString(CRM_Core_DAO $dao, string $val)

Convert a string of group IDs to a string of group labels.

setQillAndWhere(string $name, string $op, string|array $value, string $grouping, string $field)

Set the qill and where properties for a field.

Details

at line 426
CRM_Contact_BAO_Query __construct(array $params = NULL, array $returnProperties = NULL, array $fields = NULL, bool $includeContactIds = FALSE, bool $strict = FALSE, bool|int $mode = 1, bool $skipPermission = FALSE, bool $searchDescendentGroups = TRUE, bool $smartGroupCache = TRUE, null $displayRelationshipType = NULL, string $operator = 'AND', string $apiEntity = NULL)

Class constructor which also does all the work.

Parameters

array $params
array $returnProperties
array $fields
bool $includeContactIds
bool $strict
bool|int $mode
  • mode the search is operating on
bool $skipPermission
bool $searchDescendentGroups
bool $smartGroupCache
null $displayRelationshipType
string $operator
string $apiEntity

Return Value

CRM_Contact_BAO_Query

at line 489
initialize(string $apiEntity = NULL)

Function which actually does all the work for the constructor.

Parameters

string $apiEntity The api entity being called. This sort-of duplicates $mode in a confusing way. Probably not by design.

at line 553
buildParamsLookup()

Function for same purpose as convertFormValues.

Like convert form values this function exists to pre-Process parameters from the form.

It is unclear why they are different functions & likely relates to advances search versus search builder.

The direction we are going is having the form convert values to a standardised format & moving away from weird & wonderful where clause switches.

Fix and handle contact deletion nicely.

this code is primarily for search builder use case where different clauses can specify if they want deleted.

CRM-11971

at line 621
addSpecialFields($apiEntity)

Some composite fields do not appear in the fields array hack to make them part of the query.

Parameters

$apiEntity The api entity being called. This sort-of duplicates $mode in a confusing way. Probably not by design.

at line 653
selectClause(string $apiEntity = NULL)

Given a list of conditions in params and a list of desired return Properties generate the required select and from clauses. Note that since the where clause introduces new tables, the initial attempt also retrieves all variables used in the params list

Parameters

string $apiEntity The api entity being called. This sort-of duplicates $mode in a confusing way. Probably not by design.

at line 997
addHierarchicalElements()

If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.

at line 1326
addMultipleElements()

If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values.

at line 1361
array query(bool $count = FALSE, bool $sortByChar = FALSE, bool $groupContacts = FALSE, bool $onlyDeleted = FALSE)

Generate the query based on what type of query we need.

Parameters

bool $count
bool $sortByChar
bool $groupContacts
bool $onlyDeleted

Return Value

array sql query parts as an array

at line 1493
mixed getWhereValues(string $name, mixed $grouping)

Get where values from the parameters.

Parameters

string $name
mixed $grouping

Return Value

mixed

at line 1511
static fixDateValues(bool $relative, string $from, string $to)

Fix date values.

Parameters

bool $relative
string $from
string $to

at line 1550
static array convertFormValues(array $formValues, int $wildcard, bool $useEquals = FALSE, string $apiEntity = NULL, array $entityReferenceFields = array())

Convert values from form-appropriate to query-object appropriate.

The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax. So, ideally we would convert all fields to look like array( 0 => $fieldName // Set the operator for legacy reasons, but it is ignored 1 => '=' // array in sql filter syntax 2 => array('BETWEEN' => array(1,60), 3 => null 4 => null );

There are some examples of the syntax in https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship

More notes at CRM_Core_DAO::createSQLFilter

and a list of supported operators in CRM_Core_DAO

Parameters

array $formValues
int $wildcard
bool $useEquals
string $apiEntity
array $entityReferenceFields Field names of any entity reference fields (which will need reformatting to IN syntax).

Return Value

array

at line 1675
static legacyConvertFormValues(string $id, array|int $values)

Function to support legacy format for groups and tags.

Parameters

string $id
array|int $values

at line 1703
static array|null fixWhereValues(int $id, array $values, int $wildcard, bool $useEquals = FALSE, string $apiEntity = NULL)

Fix values from query from/to something no-one cared enough to document.

Parameters

int $id
array $values
int $wildcard
bool $useEquals
string $apiEntity

Return Value

array|null

at line 1778
whereClauseSingle(array $values, string $apiEntity = NULL)

Get the where clause for a single field.

Parameters

array $values
string $apiEntity

at line 2016
string whereClause(string $apiEntity = NULL)

Given a list of conditions in params generate the required where clause.

Parameters

string $apiEntity

Return Value

string

at line 2084
restWhere(array $values)

Generate where clause for any parameters not already handled.

Parameters

array $values

Exceptions

Exception

at line 2358
static array getLocationTableName($where, $locType)

Parameters

$where
$locType

Return Value

array

Exceptions

Exception

at line 2431
array store(CRM_Core_DAO $dao)

Given a result dao, extract the values and return that array

Parameters

CRM_Core_DAO $dao

Return Value

array values for this query

at line 2476
array tables()

Getter for tables array.

Return Value

array

at line 2491
array whereTables()

Sometimes used to create the from clause, but, not reliably, set this AND set tables.

It's unclear the intent - there is a 'simpleFrom' clause which takes whereTables into account & a fromClause which doesn't.

logic may have eroded?

Return Value

array

at line 2506
static string getWhereClause(array $params, array $fields, array $tables, $whereTables, bool $strict = FALSE)

Generate the where clause (used in match contacts and permissions)

Parameters

array $params
array $fields
array $tables
$whereTables
bool $strict

Return Value

string

at line 2544
static string fromClause(array $tables, array $inner = NULL, array $right = NULL, bool $primaryLocation = TRUE, int $mode = 1, string|NULL $apiEntity = NULL)

Create the from clause.

Parameters

array $tables Tables that need to be included in this from clause. If null, return mimimal from clause (i.e. civicrm_contact).
array $inner Tables that should be inner-joined.
array $right Tables that should be right-joined.
bool $primaryLocation Search on primary location. See note below.
int $mode Determines search mode based on bitwise MODE_* constants.
string|NULL $apiEntity Determines search mode based on entity by string.

The $primaryLocation flag only seems to be used when locationType() has been called. This may be a search option exposed, or perhaps it's a "search all details" approach which predates decoupling of location types and primary fields?

Return Value

string the from clause

See also

https://issues.civicrm.org/jira/browse/CRM-19967

at line 2797
deletedContacts(array $values)

WHERE / QILL clause for deleted_contacts

Parameters

array $values

at line 2810
contactType($values)

Where / qill clause for contact_type

Parameters

$values

at line 2875
contactSubType(array $values)

Where / qill clause for contact_sub_type.

Parameters

array $values

at line 2885
includeContactSubTypes($value, $grouping, string $op = 'LIKE')

Parameters

$value
$grouping
string $op

at line 2923
group($values)

Where / qill clause for groups.

Parameters

$values

at line 3030
array getGroupsFromTypeCriteria($value)

Function translates selection of group type into a list of groups.

Parameters

$value

Return Value

array

at line 3047
null|string addGroupContactCache(array $groups, string $tableAlias = NULL, string $joinTable = "contact_a", string $op)

Parameters

array $groups
string $tableAlias
string $joinTable
string $op

Return Value

null|string

at line 3097
ufUser($values)

Where / qill clause for cms users

Parameters

$values

at line 3118
tagSearch(array $values)

All tag search specific.

Parameters

array $values

at line 3180
tag(array $values)

Where / qill clause for tag.

Parameters

array $values

at line 3261
notes(array $values)

Where/qill clause for notes

Parameters

array $values

at line 3308
bool nameNullOrEmptyOp(string $name, $op, $grouping)

Parameters

string $name
$op
$grouping

Return Value

bool

at line 3336
sortName(array $values)

Where / qill clause for sort_name

Parameters

array $values

at line 3412
greetings(array $values)

Where/qill clause for greeting fields.

Parameters

array $values

at line 3427
protected email(array $values, string $apiEntity)

Where / qill clause for email

Parameters

array $values
string $apiEntity

at line 3471
phone_numeric(array $values)

Where / qill clause for phone number

Parameters

array $values

at line 3491
phone_option_group(array $values)

Where / qill clause for phone type/location

Parameters

array $values

at line 3506
street_address(array $values)

Where / qill clause for street_address.

Parameters

array $values

at line 3539
street_number(array $values)

Where / qill clause for street_unit.

Parameters

array $values

at line 3572
sortByCharacter(array $values)

Where / qill clause for sorting by character.

Parameters

array $values

at line 3584
includeContactIDs()

Where / qill clause for including contact ids.

at line 3606
postalCode(array $values)

Where / qill clause for postal code.

Parameters

array $values

at line 3655
string locationType(array $values, null $status = NULL)

Where / qill clause for location type.

Parameters

array $values
null $status

Return Value

string

at line 3686
array|NULL country($values, bool $fromStateProvince = TRUE)

Parameters

$values
bool $fromStateProvince

Return Value

array|NULL

at line 3734
string county(array $values, null $status = NULL)

Where / qill clause for county (if present).

Parameters

array $values
null $status

Return Value

string

at line 3803
string stateProvince(array $values, null $status = NULL)

Where / qill clause for state/province AND country (if present).

Parameters

array $values
null $status

Return Value

string

at line 3834
changeLog(array $values)

Where / qill clause for change log.

Parameters

array $values

at line 3853
modifiedDates($values)

Parameters

$values

at line 3876
demographics($values)

Parameters

$values

at line 3903
privacy($values)

Parameters

$values

at line 3923
privacyOptions($values)

Parameters

$values

at line 3963
preferredCommunication($values)

Parameters

$values

at line 3990
relationship(array $values)

Where / qill clause for relationship.

Parameters

array $values

at line 4174
addRelationshipDateClauses(string $grouping, array $where)

Add start & end date criteria in

Parameters

string $grouping
array $where = array to add where clauses to, in case you are generating a temp table. not the main query.

at line 4205
static array defaultReturnProperties(int $mode = 1)

Default set of return properties.

Parameters

int $mode

Return Value

array derault return properties

at line 4287
static string|NULL getPrimaryCondition(int $value)

Get primary condition for a sql clause.

Parameters

int $value

Return Value

string|NULL

at line 4304
static string getQuery(array $params = NULL, array $returnProperties = NULL, bool $count = FALSE)

Wrapper for a simple search query.

Parameters

array $params
array $returnProperties
bool $count

Return Value

string

at line 4342
static array apiQuery(array $params = NULL, array $returnProperties = NULL, null $fields = NULL, string $sort = NULL, int $offset, int $row_count = 25, bool $smartGroupCache = TRUE, bool $count = FALSE, bool $skipPermissions = TRUE, int $mode = CRM_Contact_BAO_Query::MODE_CONTACTS, string $apiEntity = NULL)

These are stub comments as this function needs more explanation - particularly in terms of how it relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery.

This function was originally written as a wrapper for the api query but is called from multiple places in the core code directly so the name is misleading. This function does not use the searchQuery function but it is unclear as to whehter that is historical or there is a reason CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function

Parameters

array $params
array $returnProperties
null $fields
string $sort
int $offset
int $row_count
bool $smartGroupCache ?? update smart group cache?.
bool $count Return count obnly.
bool $skipPermissions Should permissions be ignored or should the logged in user's permissions be applied.
int $mode This basically correlates to the component.
string $apiEntity The api entity being called. This sort-of duplicates $mode in a confusing way. Probably not by design.

Return Value

array

at line 4441
static string getCustomFieldName(string $parameterName)

Get the actual custom field name by stripping off the appended string.

The string could be _relative, _from, or _to

Parameters

string $parameterName The name of the parameter submitted to the form. e.g custom_3_relative custom_3_from

Return Value

string

at line 4467
static protected convertCustomRelativeFields(array $formValues, array $params, string $values, string $fieldName)

Convert submitted values for relative custom fields to query object format.

The query will support the sqlOperator format so convert to that format.

Parameters

array $formValues Submitted values.
array $params Converted parameters for the query object.
string $values Submitted value.
string $fieldName Submitted field name. (Matches form field not DB field.)

at line 4519
static bool isCustomDateField($fieldName)

Are we dealing with custom field of type date.

Parameters

$fieldName

Return Value

bool

at line 4543
static bool; isAlreadyProcessedForQueryFormat(mixed $values)

Has this field already been reformatting to Query object syntax.

The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems to come with associated double queries & is possibly damaging performance.

However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns them as they are.

Parameters

mixed $values Value in formValues for the field.

Return Value

bool;

at line 4563
static filterCountryFromValuesIfStateExists(array $formValues)

If the state and country are passed remove state.

Country is implicit from the state, but including both results in a poor query as there is no combined index on state AND country.

CRM-18125

Parameters

array $formValues

at line 4586
static string appendAnyValueToSelect(array $selectClauses, array $groupBy)

For some special cases, grouping by subset of select fields becomes mandatory.

Hence, full_group_by mode is handled by appending any_value keyword to select fields not present in groupBy

Parameters

array $selectClauses
array $groupBy
  • Columns already included in GROUP By clause.

Return Value

string

at line 4619
static string getGroupByFromSelectColumns(array $selectClauses, array $groupBy = NULL)

Include Select columns in groupBy clause.

Parameters

array $selectClauses
array $groupBy
  • Columns already included in GROUP By clause.

Return Value

string

at line 4693
CRM_Core_DAO searchQuery(int $offset, int $rowCount, string|CRM_Utils_Sort $sort = NULL, bool $count = FALSE, bool $includeContactIds = FALSE, bool $sortByChar = FALSE, bool $groupContacts = FALSE, bool $returnQuery = FALSE, string $additionalWhereClause = NULL, null $sortOrder = NULL, string $additionalFromClause = NULL, bool $skipOrderAndLimit = FALSE)

Create and query the db for an contact search.

Parameters

int $offset The offset for the query.
int $rowCount The number of rows to return.
string|CRM_Utils_Sort $sort The order by string.
bool $count Is this a count only query ?.
bool $includeContactIds Should we include contact ids?.
bool $sortByChar If true returns the distinct array of first characters for search results.
bool $groupContacts If true, return only the contact ids.
bool $returnQuery Should we return the query as a string.
string $additionalWhereClause If the caller wants to further restrict the search (used for components).
null $sortOrder
string $additionalFromClause Should be clause with proper joins, effective to reduce where clause load.
bool $skipOrderAndLimit

Return Value

CRM_Core_DAO

at line 4815
CRM_Core_DAO getCachedContacts(string $cacheKey, int $offset, int $rowCount, bool $includeContactIds)

Fetch a list of contacts from the prev/next cache for displaying a search results page

Parameters

string $cacheKey
int $offset
int $rowCount
bool $includeContactIds

Return Value

CRM_Core_DAO

at line 4843
generatePermissionClause(bool $onlyDeleted = FALSE, bool $count = FALSE)

Populate $this->_permissionWhereClause with permission related clause and update other query related properties.

Function calls ACL permission class and hooks to filter the query appropriately

Note that these 2 params were in the code when extracted from another function and a second round extraction would be to make them properties of the class

Parameters

bool $onlyDeleted Only get deleted contacts.
bool $count Return Count only.

at line 4885
setSkipPermission($val)

Parameters

$val

at line 4894
array summaryContribution(null $context = NULL)

Parameters

null $context

Return Value

array

at line 5054
string qill()

Getter for the qill object.

Return Value

string

at line 5063
static array defaultHierReturnProperties()

Default set of return default hier return properties.

Return Value

array

at line 5151
dateQueryBuilder(array $values, string $tableName, string $fieldName, string $dbFieldName, string $fieldTitle, bool $appendTimeStamp = TRUE)

Build query for a date field.

Parameters

array $values
string $tableName
string $fieldName
string $dbFieldName
string $fieldTitle
bool $appendTimeStamp

at line 5280
numberRangeBuilder($values, string $tableName, string $fieldName, string $dbFieldName, $fieldTitle, null $options = NULL)

Parameters

$values
string $tableName
string $fieldName
string $dbFieldName
$fieldTitle
null $options

at line 5363
ageRangeQueryBuilder($values, string $tableName, string $fieldName, string $dbFieldName, $fieldTitle, null $options = NULL)

Parameters

$values
string $tableName
string $fieldName
string $dbFieldName
$fieldTitle
null $options

at line 5447
static string calcDateFromAge(string $asofDate, int $age, string $type)

Calculate date from age.

Parameters

string $asofDate
int $age
string $type

Return Value

string

at line 5478
static string buildClause(string $field, string $op, string $value = NULL, string $dataType = NULL)

Given the field name, operator, value & its data type builds the where Clause for the query used for handling 'IS NULL'/'IS NOT NULL' operators

Parameters

string $field Fieldname.
string $op Operator.
string $value Value.
string $dataType Data type of the field.

Return Value

string Where clause for the query.

at line 5550
array openedSearchPanes(bool $reset = FALSE)

Parameters

bool $reset

Return Value

array

at line 5584
setOperator($operator)

Parameters

$operator

at line 5595
string getOperator()

Return Value

string

at line 5604
filterRelatedContacts($from, $where, $having)

Parameters

$from
$where
$having

at line 5686
static bool caseImportant($op)

See CRM-19811 for why this is database hurty without apparent benefit.

Parameters

$op

Return Value

bool

at line 5697
static bool componentPresent($returnProperties, $prefix)

Parameters

$returnProperties
$prefix

Return Value

bool

at line 5727
optionValueQuery(string $name, string $op, string $value, int $grouping, string $daoName = NULL, array $field, string $label, string $dataType = 'String', bool $useIDsOnly = FALSE)

Builds the necessary structures for all fields that are similar to option value look-ups.

Parameters

string $name the name of the field.
string $op the sql operator, this function should handle ALL SQL operators.
string $value depends on the operator and who's calling the query builder.
int $grouping the index where to place the where clause.
string $daoName DAO Name.
array $field an array that contains various properties of the field identified by $name.
string $label The label for this field element.
string $dataType The data type for this element.
bool $useIDsOnly

at line 5791
static bool|array parseSearchBuilderString(string $string, string $dataType = 'Integer')

Check and explode a user defined numeric string into an array this was the protocol used by search builder in the old old days before we had super nice js widgets to do the hard work

Parameters

string $string
string $dataType The dataType we should check for the values, default integer.

Return Value

bool|array false if string does not match the pattern array of numeric values if string does match the pattern

at line 5830
array|NULL convertToPseudoNames(CRM_Core_DAO $dao, bool $return = FALSE, bool $usedForAPI = FALSE)

Convert the pseudo constants id's to their names

Parameters

CRM_Core_DAO $dao
bool $return
bool $usedForAPI

Return Value

array|NULL

at line 5955
array|NULL includePseudoFieldsJoin(string|array $sort)

Include pseudo fields LEFT JOIN.

Parameters

string|array $sort can be a object or string

Return Value

array|NULL

at line 6015
static array buildQillForFieldValue(string $daoName, string $fieldName, mixed $fieldValue, string $op, array $pseudoExtraParam = array(), int $type = CRM_Utils_Type::T_STRING)

Build qill for field.

Qill refers to the query detail visible on the UI.

Parameters

string $daoName
string $fieldName
mixed $fieldValue
string $op
array $pseudoExtraParam
int $type Type of the field per CRM_Utils_Type

Return Value

array

at line 6115
static string getWildCardedValue(bool $wildcard, string $op, string $value)

Alter value to reflect wildcard settings.

The form will have tried to guess whether this is a good field to wildcard but there is also a site-wide setting that specifies whether it is OK to append the wild card to the beginning or only the end of the string

Parameters

bool $wildcard This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard.
string $op Generally '=' or 'LIKE'.
string $value The search string.

Return Value

string

at line 6138
static processSpecialFormValue(array $formValues, array $specialFields, array $changeNames = array())

Process special fields of Search Form in OK (Operator in Key) format

Parameters

array $formValues
array $specialFields Special params to be processed
array $changeNames Array of fields whose name should be changed

at line 6184
protected array prepareOrderBy(string|CRM_Utils_Sort $sort, bool $sortByChar, null $sortOrder, string $additionalFromClause)

Parse and assimilate the various sort options.

Side-effect: if sorting on a common column from a related table (city, postal_code, email), the related table may be joined automatically.

At time of writing, this code is deeply flawed and should be rewritten. For the moment, it's been extracted to a standalone function.

Parameters

string|CRM_Utils_Sort $sort The order by string.
bool $sortByChar If true returns the distinct array of first characters for search results.
null $sortOrder Who knows? Hu knows. He who knows Hu knows who.
string $additionalFromClause Should be clause with proper joins, effective to reduce where clause load.

Return Value

array list(string $orderByClause, string $additionalFromClause).

at line 6323
string convertGroupIDStringToLabelString(CRM_Core_DAO $dao, string $val)

Convert a string of group IDs to a string of group labels.

The original string may include duplicates and groups the user does not have permission to see.

Parameters

CRM_Core_DAO $dao
string $val

Return Value

string

at line 6343
setQillAndWhere(string $name, string $op, string|array $value, string $grouping, string $field)

Set the qill and where properties for a field.

This function is intended as a short-term function to encourage refactoring & re-use - but really we should just have less special-casing.

Parameters

string $name
string $op
string|array $value
string $grouping
string $field