Skip to content

Commit

Permalink
Merge pull request #20954 from eileenmcnaughton/sql2
Browse files Browse the repository at this point in the history
dev/core#2721 [REF] Further divide savedSearchParam loading into the sql functions
  • Loading branch information
seamuslee001 authored Jul 27, 2021
2 parents ff6487d + c3f7cb2 commit 3fa3f9c
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 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 @@ -553,14 +554,19 @@ protected static function getApiSQL(array $savedSearch, string $addSelect, strin
* so temp tables are not destroyed if they are used
*
* @param int $savedSearchID
* @param array $ssParams
* @param array $savedSearch
* @param string $addSelect
* @param string $excludeClause
*
* @return string
* @throws CRM_Core_Exception
*/
protected static function getCustomSearchSQL($savedSearchID, array $ssParams, string $addSelect, string $excludeClause) {
protected static function getCustomSearchSQL($savedSearchID, array $savedSearch, string $addSelect, string $excludeClause) {
$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 @@ -576,15 +582,28 @@ 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 array $savedSearch
* @param string $addSelect
* @param string $excludeClause
*
* @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($savedSearchID, array $savedSearch, string $addSelect, string $excludeClause): string {
$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,27 +815,27 @@ 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);
$savedSearch = SavedSearch::get(FALSE)
->addWhere('id', '=', $savedSearchID)
->addSelect('*')
->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 (!empty($ssParams['api_entity'])) {
$sql = self::getApiSQL($ssParams, $addSelect, $excludeClause);
if ($savedSearch['api_entity']) {
$sql = self::getApiSQL($savedSearch, $addSelect, $excludeClause);
}
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);
if (!empty($savedSearch['form_values']['customSearchID'])) {
$sql = self::getCustomSearchSQL($savedSearchID, $savedSearch, $addSelect, $excludeClause);
}
else {
$sql = self::getQueryObjectSQL($savedSearchID, $ssParams, $addSelect, $excludeClause);
$sql = self::getQueryObjectSQL($savedSearchID, $savedSearch, $addSelect, $excludeClause);
}
}
}
Expand Down

0 comments on commit 3fa3f9c

Please sign in to comment.