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

dev/core#2721 [Ref] simplify passed parameters #20955

Merged
merged 3 commits into from
Jul 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 62 additions & 36 deletions CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Civi\Api4\Group;
use Civi\Api4\Query\Api4SelectQuery;
use Civi\Api4\Query\SqlExpression;
use Civi\Api4\SavedSearch;

/**
*
Expand Down Expand Up @@ -509,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 @@ -552,15 +559,25 @@ 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 $ssParams
* @param string $addSelect
* @param string $excludeClause
* @param array $savedSearch
* @param int $groupID
*
* @return string
* @throws CRM_Core_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected static function getCustomSearchSQL($savedSearchID, array $ssParams, 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)) {
CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
}
$searchSQL = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID)->contactIDs();
$searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
if (strpos($searchSQL, 'WHERE') === FALSE) {
Expand All @@ -575,16 +592,33 @@ protected static function getCustomSearchSQL($savedSearchID, array $ssParams, st
/**
* Get array of sql from a saved query object group.
*
* @param int $savedSearchID
* @param array $ssParams
* @param string $addSelect
* @param string $excludeClause
* @param array $savedSearch
* @param int $groupID
*
* @return string
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected static function getQueryObjectSQL($savedSearchID, array $ssParams, string $addSelect, string $excludeClause) {
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']) {
$ssParams = CRM_Core_BAO_Mapping::formattedFields($fv);
}
else {
$ssParams = CRM_Contact_BAO_Query::convertFormValues($fv);
}
// CRM-7021 rectify params to what proximity search expects if there is a value for prox_distance
if (!empty($ssParams)) {
CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
}

$returnProperties = NULL;
if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
$fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
Expand Down Expand Up @@ -796,28 +830,20 @@ private static function updateCacheFromTempTable(CRM_Utils_SQL_TempTable $groupC
*/
protected static function insertGroupContactsIntoTempTable(string $tempTableName, int $groupID, ?int $savedSearchID, ?string $children): void {
if ($savedSearchID) {
$ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID);

$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 (!empty($ssParams['api_entity'])) {
$sql = self::getApiSQL($ssParams, $addSelect, $excludeClause);
$savedSearch = SavedSearch::get(FALSE)
->addWhere('id', '=', $savedSearchID)
->addSelect('*')
->execute()
->first();

if ($savedSearch['api_entity']) {
$sql = self::getApiSQL($savedSearch, $groupID);
}
elseif (!empty($savedSearch['form_values']['customSearchID'])) {
$sql = self::getCustomSearchSQL($savedSearch, $groupID);
}
else {
// CRM-7021 rectify params to what proximity search expects if there is a value for prox_distance
if (!empty($ssParams)) {
CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
}
if (isset($ssParams['customSearchID'])) {
$sql = self::getCustomSearchSQL($savedSearchID, $ssParams, $addSelect, $excludeClause);
}
else {
$sql = self::getQueryObjectSQL($savedSearchID, $ssParams, $addSelect, $excludeClause);
}
$sql = self::getQueryObjectSQL($savedSearch, $groupID);
}
}

Expand Down