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#1471 - Add system status alert for deleted custom fields in … #16267

Merged
merged 1 commit into from
Jan 31, 2020
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
76 changes: 76 additions & 0 deletions CRM/Utils/Check/Component/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="%1" title="Edit Custom Field"> %2 </a>', [
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 = ' <span style="color:red"> - Deleted - </span> ';
}
}
$groupEdit = '<a href="' . CRM_Utils_System::url('civicrm/contact/search/advanced', "?reset=1&ssID={$field['ssid']}", TRUE) . '" title="' . ts('Edit search criteria') . '"> <i class="crm-i fa-pencil"></i> </a>';
$groupConfig = '<a href="' . CRM_Utils_System::url('civicrm/group', "?reset=1&action=update&id={$id}", TRUE) . '" title="' . ts('Group settings') . '"> <i class="crm-i fa-gear"></i> </a>';
$html .= "<tr><td>{$id} - {$field['title']} </td><td>{$groupEdit} {$groupConfig}</td><td class='disabled'>{$fieldName}</td>";
}

$message = "<p>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.</p>
<p><table><thead><tr><th>Group</th><th></th><th>Custom Field</th>
</tr></thead><tbody>
$html
</tbody></table></p>
";

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

}