3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Check_Component_Schema
extends CRM_Utils_Check_Component
{
20 * Check defined indices exist.
23 * @throws \CiviCRM_API3_Exception
25 public function checkIndices() {
28 // CRM-21298: The "Update Indices" tool that this check suggests is
29 // unreliable. Bypass this check until CRM-20817 and CRM-20533 are resolved.
32 $missingIndices = civicrm_api3('System', 'getmissingindices', [])['values'];
33 if ($missingIndices) {
35 foreach ($missingIndices as $tableName => $indices) {
36 foreach ($indices as $index) {
37 $fields = implode(', ', $index['field']);
38 $html .= "<tr><td>{$tableName}</td><td>{$index['name']}</td><td>$fields</td>";
41 $message = "<p>The following tables have missing indices. Click 'Update Indices' button to create them.<p>
42 <p><table><thead><tr><th>Table Name</th><th>Key Name</th><th>Expected Indices</th>
45 </tbody></table></p>";
46 $msg = new CRM_Utils_Check_Message(
49 ts('Performance warning: Missing indices'),
50 \Psr\Log\LogLevel
::WARNING
,
55 ts('Update all database indices now? This may take a few minutes and cause a noticeable performance lag for all users while running.'),
57 ['System', 'updateindexes']
67 public function checkMissingLogTables() {
69 $logging = new CRM_Logging_Schema();
70 $missingLogTables = $logging->getMissingLogTables();
72 if (Civi
::settings()->get('logging') && $missingLogTables) {
73 $msg = new CRM_Utils_Check_Message(
75 ts("You don't have logging enabled on some tables. This may cause errors on performing insert/update operation on them."),
76 ts('Missing Log Tables'),
77 \Psr\Log\LogLevel
::WARNING
,
81 ts('Create Missing Log Tables'),
82 ts('Create missing log tables now? This may take few minutes.'),
84 ['System', 'createmissinglogtables']
92 * Check that no smart groups exist that contain deleted custom fields.
96 public function checkSmartGroupCustomFieldCriteria() {
97 $messages = $problematicSG = [];
98 $customFieldIds = array_keys(CRM_Core_BAO_CustomField
::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE));
100 $smartGroups = civicrm_api3('SavedSearch', 'get', [
102 'options' => ['limit' => 0],
105 catch (CiviCRM_API3_Exception
$e) {
106 $messages[] = new CRM_Utils_Check_Message(
108 ts('The smart group check was unable to run. This is likely to because a database upgrade is pending.'),
109 ts('Smart Group check did not run'),
110 \Psr\Log\LogLevel
::INFO
,
115 if (empty($smartGroups['values'])) {
118 foreach ($smartGroups['values'] as $group) {
119 if (empty($group['form_values'])) {
122 foreach ($group['form_values'] as $formValues) {
123 if (isset($formValues[0]) && (strpos($formValues[0], 'custom_') === 0)) {
124 list(, $customFieldID) = explode('_', $formValues[0]);
125 if (!in_array((int) $customFieldID, $customFieldIds, TRUE)) {
126 $problematicSG[CRM_Contact_BAO_SavedSearch
::getName($group['id'], 'id')] = [
127 'title' => CRM_Contact_BAO_SavedSearch
::getName($group['id'], 'title'),
128 'cfid' => $customFieldID,
129 'ssid' => $group['id'],
136 if (!empty($problematicSG)) {
138 foreach ($problematicSG as $id => $field) {
139 if (!empty($field['cfid'])) {
141 $customField = civicrm_api3('CustomField', 'getsingle', [
143 'id' => $field['cfid'],
145 $fieldName = ts('<a href="%1" title="Edit Custom Field"> %2 </a>', [
146 1 => CRM_Utils_System
::url('civicrm/admin/custom/group/field/update',
147 "action=update&reset=1&gid={$customField['custom_group_id']}&id={$field['cfid']}", TRUE
149 2 => $customField['label'],
152 catch (CiviCRM_API3_Exception
$e) {
153 $fieldName = ' <span style="color:red"> - Deleted - </span> ';
156 $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" aria-hidden="true"></i> </a>';
157 $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" aria-hidden="true"></i> </a>';
158 $html .= "<tr><td>{$id} - {$field['title']} </td><td>{$groupEdit} {$groupConfig}</td><td class='disabled'>{$fieldName}</td>";
161 $message = "<p>The following smart groups include custom fields which are disabled/deleted from the database. This may cause errors on group page.
162 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>
163 <p><table><thead><tr><th>Group</th><th></th><th>Custom Field</th>
169 $msg = new CRM_Utils_Check_Message(
172 ts('Disabled/Deleted fields on Smart Groups'),
173 \Psr\Log\LogLevel
::WARNING
,