Merge pull request #19354 from demeritcowboy/php74-more-more
[civicrm-core.git] / CRM / Utils / Check / Component / Schema.php
CommitLineData
49186f94
AS
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
49186f94 5 | |
bc77d7c0
TO
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 |
49186f94
AS
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
49186f94
AS
16 */
17class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component {
18
19 /**
a5e6535c 20 * Check defined indices exist.
21 *
02b3ba91 22 * @return CRM_Utils_Check_Message[]
a5e6535c 23 * @throws \CiviCRM_API3_Exception
49186f94
AS
24 */
25 public function checkIndices() {
be2fb01f 26 $messages = [];
bb90f230
AH
27
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.
30 return $messages;
31
a5e6535c 32 $missingIndices = civicrm_api3('System', 'getmissingindices', [])['values'];
3d4602c3 33 if ($missingIndices) {
f4f835ed 34 $html = '';
3d4602c3 35 foreach ($missingIndices as $tableName => $indices) {
f4f835ed
JP
36 foreach ($indices as $index) {
37 $fields = implode(', ', $index['field']);
38 $html .= "<tr><td>{$tableName}</td><td>{$index['name']}</td><td>$fields</td>";
39 }
40 }
3d4602c3
JP
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>
f4f835ed
JP
43 </tr></thead><tbody>
44 $html
45 </tbody></table></p>";
531b0c6c 46 $msg = new CRM_Utils_Check_Message(
49186f94 47 __FUNCTION__,
f4f835ed 48 ts($message),
63809327 49 ts('Performance warning: Missing indices'),
49186f94
AS
50 \Psr\Log\LogLevel::WARNING,
51 'fa-server'
52 );
531b0c6c 53 $msg->addAction(
d758aa97 54 ts('Update Indices'),
531b0c6c
CW
55 ts('Update all database indices now? This may take a few minutes and cause a noticeable performance lag for all users while running.'),
56 'api3',
be2fb01f 57 ['System', 'updateindexes']
531b0c6c
CW
58 );
59 $messages[] = $msg;
49186f94
AS
60 }
61 return $messages;
62 }
63
74044663 64 /**
02b3ba91 65 * @return CRM_Utils_Check_Message[]
74044663
JP
66 */
67 public function checkMissingLogTables() {
be2fb01f 68 $messages = [];
74044663
JP
69 $logging = new CRM_Logging_Schema();
70 $missingLogTables = $logging->getMissingLogTables();
71
26943048 72 if (Civi::settings()->get('logging') && $missingLogTables) {
74044663
JP
73 $msg = new CRM_Utils_Check_Message(
74 __FUNCTION__,
cb721356 75 ts("You don't have logging enabled on some tables. This may cause errors on performing insert/update operation on them."),
74044663
JP
76 ts('Missing Log Tables'),
77 \Psr\Log\LogLevel::WARNING,
78 'fa-server'
79 );
80 $msg->addAction(
cb721356
JP
81 ts('Create Missing Log Tables'),
82 ts('Create missing log tables now? This may take few minutes.'),
74044663 83 'api3',
be2fb01f 84 ['System', 'createmissinglogtables']
74044663
JP
85 );
86 $messages[] = $msg;
87 }
88 return $messages;
89 }
90
5405c118 91 /**
b5a09f67 92 * Check that no smart groups exist that contain deleted custom fields.
93 *
02b3ba91 94 * @return CRM_Utils_Check_Message[]
5405c118
JP
95 */
96 public function checkSmartGroupCustomFieldCriteria() {
1fbda76b 97 if (CRM_Core_BAO_Domain::isDBUpdateRequired()) {
98 // Do not run this check when the db has not been updated as it might fail on non-updated schema issues.
99 return [];
100 }
5405c118
JP
101 $messages = $problematicSG = [];
102 $customFieldIds = array_keys(CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE));
b5a09f67 103 try {
104 $smartGroups = civicrm_api3('SavedSearch', 'get', [
105 'sequential' => 1,
106 'options' => ['limit' => 0],
107 ]);
108 }
109 catch (CiviCRM_API3_Exception $e) {
110 $messages[] = new CRM_Utils_Check_Message(
111 __FUNCTION__,
112 ts('The smart group check was unable to run. This is likely to because a database upgrade is pending.'),
113 ts('Smart Group check did not run'),
114 \Psr\Log\LogLevel::INFO,
115 'fa-server'
116 );
117 return $messages;
118 }
5405c118
JP
119 if (empty($smartGroups['values'])) {
120 return $messages;
121 }
122 foreach ($smartGroups['values'] as $group) {
123 if (empty($group['form_values'])) {
124 continue;
125 }
126 foreach ($group['form_values'] as $formValues) {
b5a09f67 127 if (isset($formValues[0]) && (strpos($formValues[0], 'custom_') === 0)) {
691297b0
JG
128 list(, $customFieldID) = explode('_', $formValues[0]);
129 if (!in_array((int) $customFieldID, $customFieldIds, TRUE)) {
5405c118
JP
130 $problematicSG[CRM_Contact_BAO_SavedSearch::getName($group['id'], 'id')] = [
131 'title' => CRM_Contact_BAO_SavedSearch::getName($group['id'], 'title'),
132 'cfid' => $customFieldID,
133 'ssid' => $group['id'],
134 ];
135 }
136 }
137 }
138 }
139
140 if (!empty($problematicSG)) {
141 $html = '';
142 foreach ($problematicSG as $id => $field) {
143 if (!empty($field['cfid'])) {
144 try {
145 $customField = civicrm_api3('CustomField', 'getsingle', [
146 'sequential' => 1,
147 'id' => $field['cfid'],
148 ]);
149 $fieldName = ts('<a href="%1" title="Edit Custom Field"> %2 </a>', [
150 1 => CRM_Utils_System::url('civicrm/admin/custom/group/field/update',
151 "action=update&reset=1&gid={$customField['custom_group_id']}&id={$field['cfid']}", TRUE
152 ),
153 2 => $customField['label'],
154 ]);
155 }
b5a09f67 156 catch (CiviCRM_API3_Exception $e) {
5405c118
JP
157 $fieldName = ' <span style="color:red"> - Deleted - </span> ';
158 }
159 }
13a3d214
AH
160 $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>';
161 $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>';
5405c118
JP
162 $html .= "<tr><td>{$id} - {$field['title']} </td><td>{$groupEdit} {$groupConfig}</td><td class='disabled'>{$fieldName}</td>";
163 }
164
165 $message = "<p>The following smart groups include custom fields which are disabled/deleted from the database. This may cause errors on group page.
166 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>
167 <p><table><thead><tr><th>Group</th><th></th><th>Custom Field</th>
168 </tr></thead><tbody>
169 $html
170 </tbody></table></p>
171 ";
172
173 $msg = new CRM_Utils_Check_Message(
174 __FUNCTION__,
175 ts($message),
176 ts('Disabled/Deleted fields on Smart Groups'),
177 \Psr\Log\LogLevel::WARNING,
178 'fa-server'
179 );
180 $messages[] = $msg;
181 }
182 return $messages;
183 }
184
02b3ba91
CW
185 /**
186 * @return CRM_Utils_Check_Message[]
187 */
9a894bf1
SL
188 public function checkMoneyValueFormatConfig() {
189 $messages = [];
190 if (CRM_Core_Config::singleton()->moneyvalueformat !== '%!i') {
191 $msg = new CRM_Utils_Check_Message(
192 __FUNCTION__,
6a595a7d
AH
193 ts(
194 '<p>The Monetary Value Display format is a deprecated setting, and this site has a non-standard format. Please report your configuration on <a href="%1">this Gitlab issue</a>.',
195 [1 => 'https://lab.civicrm.org/dev/core/-/issues/1494']
196 ),
197 ts('Deprecated monetary value display format configuration'),
9a894bf1
SL
198 \Psr\Log\LogLevel::WARNING,
199 'fa-server'
200 );
201 $messages[] = $msg;
202 }
203 return $messages;
204 }
205
49186f94 206}