diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index 8486a18bc826..30b20aaab443 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -88,4 +88,54 @@ public function checkMissingLogTables() { return $messages; } + /** + * @return array + */ + public function checkSmartGroupCustomFieldCriteria() { + $messages = $problematicSG = []; + $customFieldIds = array_keys(CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE)); + $smartGroups = civicrm_api3('SavedSearch', 'get', [ + 'sequential' => 1, + 'options' => ['limit' => 0], + ]); + if (empty($smartGroups['values'])) { + return; + } + foreach ($smartGroups['values'] as $group) { + if (empty($group['form_values'])) { + continue; + } + foreach ($group['form_values'] as $formValues) { + if (substr($formValues[0], 0, 7) == 'custom_') { + list(, $customFieldID) = explode('custom_', $formValues[0]); + if (!in_array($customFieldID, $customFieldIds)) { + $problematicSG[CRM_Contact_BAO_SavedSearch::getName($group['id'], 'id')] = CRM_Contact_BAO_SavedSearch::getName($group['id'], 'title'); + } + } + } + } + + if (!empty($problematicSG)) { + $html = ''; + foreach ($problematicSG as $id => $groupName) { + $html .= "{$id}{$groupName}"; + } + $message = "

The following smart groups include custom fields which are disabled/deleted from the database. This may cause errors on group page.

+

+ + $html +
IDGroup Title

+

You might need to revisit these groups and disable them in order to fix the error.

"; + $msg = new CRM_Utils_Check_Message( + __FUNCTION__, + ts($message), + ts('Disabled/Deleted fields on Smart Groups'), + \Psr\Log\LogLevel::WARNING, + 'fa-server' + ); + $messages[] = $msg; + } + return $messages; + } + }