Skip to content

Commit

Permalink
[Ref] simplify passed parameters
Browse files Browse the repository at this point in the history
I think if we want to make this some sort of hook or listener we don't really
want to pass out 'add select' and 'exclude clause' when they are just group id
with a bit of string. It seems cleaner to specify the sql within the function
  • Loading branch information
eileenmcnaughton committed Jul 26, 2021
1 parent c3f7cb2 commit 8c49cf3
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,23 +510,29 @@ public static function getCacheInvalidDateTime(): string {
* @param int $groupID - Group to invalidate
*/
public static function invalidateGroupContactCache($groupID): void {
CRM_Core_DAO::executeQuery("UPDATE civicrm_group
CRM_Core_DAO::executeQuery('UPDATE civicrm_group
SET cache_date = NULL
WHERE id = %1 AND (saved_search_id IS NOT NULL OR children IS NOT NULL)", [
WHERE id = %1 AND (saved_search_id IS NOT NULL OR children IS NOT NULL)', [
1 => [$groupID, 'Positive'],
]);
}

/**
* @param array $savedSearch
* @param string $addSelect
* @param string $excludeClause
* @param int $groupID
*
* @return string
* @throws API_Exception
* @throws \Civi\API\Exception\NotImplementedException
* @throws CRM_Core_Exception
*/
protected static function getApiSQL(array $savedSearch, string $addSelect, string $excludeClause) {
protected static function getApiSQL(array $savedSearch, int $groupID): string {
$excludeClause = "NOT IN (
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.status = 'Removed'
AND civicrm_group_contact.group_id = $groupID )";
$addSelect = "$groupID AS group_id";

$apiParams = $savedSearch['api_params'] + ['select' => ['id'], 'checkPermissions' => FALSE];
$idField = SqlExpression::convert($apiParams['select'][0], TRUE)->getAlias();
// Unless there's a HAVING clause, we don't care about other columns
Expand All @@ -553,15 +559,20 @@ protected static function getApiSQL(array $savedSearch, string $addSelect, strin
* We split it up and store custom class
* so temp tables are not destroyed if they are used
*
* @param int $savedSearchID
* @param array $savedSearch
* @param string $addSelect
* @param string $excludeClause
* @param int $groupID
*
* @return string
* @throws CRM_Core_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected static function getCustomSearchSQL($savedSearchID, array $savedSearch, string $addSelect, string $excludeClause) {
protected static function getCustomSearchSQL(array $savedSearch, int $groupID) {
$savedSearchID = $savedSearch['id'];
$excludeClause = "NOT IN (
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.status = 'Removed'
AND civicrm_group_contact.group_id = $groupID )";
$addSelect = "$groupID AS group_id";
$ssParams = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
// CRM-7021 rectify params to what proximity search expects if there is a value for prox_distance
if (!empty($ssParams)) {
Expand All @@ -581,16 +592,20 @@ protected static function getCustomSearchSQL($savedSearchID, array $savedSearch,
/**
* Get array of sql from a saved query object group.
*
* @param int $savedSearchID
* @param array $savedSearch
* @param string $addSelect
* @param string $excludeClause
* @param int $groupID
*
* @return string
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected static function getQueryObjectSQL($savedSearchID, array $savedSearch, string $addSelect, string $excludeClause): string {
protected static function getQueryObjectSQL(array $savedSearch, int $groupID): string {
$savedSearchID = $savedSearch['id'];
$excludeClause = "NOT IN (
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.status = 'Removed'
AND civicrm_group_contact.group_id = $groupID )";
$addSelect = "$groupID AS group_id";
$fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
//check if the saved search has mapping id
if ($savedSearch['mapping_id']) {
Expand Down Expand Up @@ -821,21 +836,15 @@ protected static function insertGroupContactsIntoTempTable(string $tempTableName
->execute()
->first();

$excludeClause = "NOT IN (
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.status = 'Removed'
AND civicrm_group_contact.group_id = $groupID )";
$addSelect = "$groupID AS group_id";

if ($savedSearch['api_entity']) {
$sql = self::getApiSQL($savedSearch, $addSelect, $excludeClause);
if ($savedSearch['api_entity']) {
$sql = self::getApiSQL($savedSearch, $groupID);
}
else {
if (!empty($savedSearch['form_values']['customSearchID'])) {
$sql = self::getCustomSearchSQL($savedSearchID, $savedSearch, $addSelect, $excludeClause);
$sql = self::getCustomSearchSQL($savedSearch, $groupID);
}
else {
$sql = self::getQueryObjectSQL($savedSearchID, $savedSearch, $addSelect, $excludeClause);
$sql = self::getQueryObjectSQL($savedSearch, $groupID);
}
}
}
Expand Down

0 comments on commit 8c49cf3

Please sign in to comment.