Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchKit - Add API filter for contacts in groups and smart groups #20507

Merged
merged 2 commits into from
Jun 7, 2021

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Jun 4, 2021

Overview

Adds a filter-style field to search for contacts in (or not in) one or more groups.

Before

Not possible to search for contacts in groups.

After

Regular groups, smart groups & nested groups all work as SearchKit filters.

image

Technical Details

Adds some new concepts in APIv4 getFields:

  1. Now a field can have a "type". The types are:
    • Field: core field
    • Custom: custom field
    • Filter: field that can only be used in the Where clause
    • Extra: any other ad-hoc api field
  2. Fields can now have sql_filters which are php callbacks to generate custom sql.
  3. Fields can specify which operators they support.

@civibot
Copy link

civibot bot commented Jun 4, 2021

(Standard links)

@civibot civibot bot added the master label Jun 4, 2021
Comment on lines +62 to +76
public static function getContactGroupSql(string $fieldAlias, string $operator, $value, Api4SelectQuery $query, int $depth): string {
$tempTable = \CRM_Utils_SQL_TempTable::build();
$tempTable->createWithColumns('contact_id INT');
$tableName = $tempTable->getName();
\CRM_Contact_BAO_GroupContactCache::populateTemporaryTableWithContactsInGroups($value, $tableName);
// SQL optimization - use INNER JOIN if the base table is Contact & this clause is not nested
if ($fieldAlias === '`a`.`id`' && $operator === "IN" && !$depth) {
$query->getQuery()->join($tableName, "INNER JOIN `$tableName` ON $fieldAlias = `$tableName`.contact_id");
return '1';
}
// Else use IN or NOT IN (this filter only supports those 2 operators)
else {
return "$fieldAlias $operator (SELECT contact_id FROM `$tableName`)";
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton this is the meat of it. The rest is to support using a callback like this to supply sql for a field.

@seamuslee001
Copy link
Contributor

Test fails I believe relate here

@eileenmcnaughton
Copy link
Contributor

Stacktrace
api\v4\Action\PseudoconstantTest::testCustomOptions
CRM_Core_Exception: One of parameters (value: ) is not of the type CommaSeparatedIntegers

@colemanw colemanw force-pushed the apiSmartGroups branch 2 times, most recently from 9155973 to 2841601 Compare June 5, 2021 01:34
@eileenmcnaughton
Copy link
Contributor

@colemanw is this testable via search kit now? (In which case I should test by trying to create searches on our testing server to give it some performance work to do)

@colemanw
Copy link
Member Author

colemanw commented Jun 5, 2021

@eileenmcnaughton yes have fun :)

@eileenmcnaughton
Copy link
Contributor

Ok - even if this is merged post rc forking (which seems likely based on current test fails) - I'll deploy it with the 5.39 rc to our site so it gets a good kicking

@colemanw colemanw force-pushed the apiSmartGroups branch 2 times, most recently from c097f9b to 6017503 Compare June 5, 2021 15:52
Adds 'type' property to API getFields to distinguish regular fields
from custom fields, extra fields and filters.

Implements `Contact.groups` as a filter, which internally adds a temp-table
and incorporates it into the query.
@eileenmcnaughton
Copy link
Contributor

This worked in my tests so far -I'm going to keep testing but I think anything else can be as a follow up as I think the approach won't get us in trouble - ie the surface area of this change is limited to adding the group filter to the api contract.

I do think we need a push into documentation next....

@eileenmcnaughton eileenmcnaughton merged commit 8f630a4 into civicrm:master Jun 7, 2021
@eileenmcnaughton eileenmcnaughton deleted the apiSmartGroups branch June 7, 2021 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants