diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index 8486a18bc826..5de044b1b7a6 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -88,4 +88,80 @@ 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 $messages; + } + 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')] = [ + 'title' => CRM_Contact_BAO_SavedSearch::getName($group['id'], 'title'), + 'cfid' => $customFieldID, + 'ssid' => $group['id'], + ]; + } + } + } + } + + if (!empty($problematicSG)) { + $html = ''; + foreach ($problematicSG as $id => $field) { + if (!empty($field['cfid'])) { + try { + $customField = civicrm_api3('CustomField', 'getsingle', [ + 'sequential' => 1, + 'id' => $field['cfid'], + ]); + $fieldName = ts(' %2 ', [ + 1 => CRM_Utils_System::url('civicrm/admin/custom/group/field/update', + "action=update&reset=1&gid={$customField['custom_group_id']}&id={$field['cfid']}", TRUE + ), + 2 => $customField['label'], + ]); + } + catch (Exception $e) { + $fieldName = ' - Deleted - '; + } + } + $groupEdit = ' '; + $groupConfig = ' '; + $html .= "{$id} - {$field['title']} {$groupEdit} {$groupConfig}{$fieldName}"; + } + + $message = "

The following smart groups include custom fields which are disabled/deleted from the database. This may cause errors on group page. + You might need to edit their search criteria and update them to clean outdated fields from saved search OR disable them in order to fix the error.

+

+ + $html +
GroupCustom Field

+ "; + + $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; + } + }