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] Further divide savedSearchParam loading into the sql functions #20954

Merged
merged 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 33 additions & 14 deletions CRM/Contact/BAO/GroupContactCache.php
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'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@eileenmcnaughton is this right why is it buried in form_values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - why is it! But, it is. that form_values field saved to the db is what holds it

$sql = self::getCustomSearchSQL($savedSearchID, $savedSearch, $addSelect, $excludeClause);
}
else {
$sql = self::getQueryObjectSQL($savedSearchID, $ssParams, $addSelect, $excludeClause);
$sql = self::getQueryObjectSQL($savedSearchID, $savedSearch, $addSelect, $excludeClause);
}
}
}
Expand Down