Merge pull request #19321 from colemanw/profileGetFieldsFix
[civicrm-core.git] / CRM / Utils / Check / Component / Schema.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component {
18
19 /**
20 * Check defined indices exist.
21 *
22 * @return CRM_Utils_Check_Message[]
23 * @throws \CiviCRM_API3_Exception
24 */
25 public function checkIndices() {
26 $messages = [];
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
32 $missingIndices = civicrm_api3('System', 'getmissingindices', [])['values'];
33 if ($missingIndices) {
34 $html = '';
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>";
39 }
40 }
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>
43 </tr></thead><tbody>
44 $html
45 </tbody></table></p>";
46 $msg = new CRM_Utils_Check_Message(
47 __FUNCTION__,
48 ts($message),
49 ts('Performance warning: Missing indices'),
50 \Psr\Log\LogLevel::WARNING,
51 'fa-server'
52 );
53 $msg->addAction(
54 ts('Update Indices'),
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',
57 ['System', 'updateindexes']
58 );
59 $messages[] = $msg;
60 }
61 return $messages;
62 }
63
64 /**
65 * @return CRM_Utils_Check_Message[]
66 */
67 public function checkMissingLogTables() {
68 $messages = [];
69 $logging = new CRM_Logging_Schema();
70 $missingLogTables = $logging->getMissingLogTables();
71
72 if (Civi::settings()->get('logging') && $missingLogTables) {
73 $msg = new CRM_Utils_Check_Message(
74 __FUNCTION__,
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,
78 'fa-server'
79 );
80 $msg->addAction(
81 ts('Create Missing Log Tables'),
82 ts('Create missing log tables now? This may take few minutes.'),
83 'api3',
84 ['System', 'createmissinglogtables']
85 );
86 $messages[] = $msg;
87 }
88 return $messages;
89 }
90
91 /**
92 * Check that no smart groups exist that contain deleted custom fields.
93 *
94 * @return CRM_Utils_Check_Message[]
95 */
96 public function checkSmartGroupCustomFieldCriteria() {
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 }
101 $messages = $problematicSG = [];
102 $customFieldIds = array_keys(CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE));
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 }
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) {
127 if (isset($formValues[0]) && (strpos($formValues[0], 'custom_') === 0)) {
128 list(, $customFieldID) = explode('_', $formValues[0]);
129 if (!in_array((int) $customFieldID, $customFieldIds, TRUE)) {
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 }
156 catch (CiviCRM_API3_Exception $e) {
157 $fieldName = ' <span style="color:red"> - Deleted - </span> ';
158 }
159 }
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>';
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
185 /**
186 * @return CRM_Utils_Check_Message[]
187 */
188 public function checkMoneyValueFormatConfig() {
189 $messages = [];
190 if (CRM_Core_Config::singleton()->moneyvalueformat !== '%!i') {
191 $msg = new CRM_Utils_Check_Message(
192 __FUNCTION__,
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'),
198 \Psr\Log\LogLevel::WARNING,
199 'fa-server'
200 );
201 $messages[] = $msg;
202 }
203 return $messages;
204 }
205
206 }