Merge pull request #16610 from mattwire/settingstrait_datepicker
[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 array
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 array
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 array
95 */
96 public function checkSmartGroupCustomFieldCriteria() {
97 $messages = $problematicSG = [];
98 $customFieldIds = array_keys(CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE));
99 try {
100 $smartGroups = civicrm_api3('SavedSearch', 'get', [
101 'sequential' => 1,
102 'options' => ['limit' => 0],
103 ]);
104 }
105 catch (CiviCRM_API3_Exception $e) {
106 $messages[] = new CRM_Utils_Check_Message(
107 __FUNCTION__,
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,
111 'fa-server'
112 );
113 return $messages;
114 }
115 if (empty($smartGroups['values'])) {
116 return $messages;
117 }
118 foreach ($smartGroups['values'] as $group) {
119 if (empty($group['form_values'])) {
120 continue;
121 }
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'],
130 ];
131 }
132 }
133 }
134 }
135
136 if (!empty($problematicSG)) {
137 $html = '';
138 foreach ($problematicSG as $id => $field) {
139 if (!empty($field['cfid'])) {
140 try {
141 $customField = civicrm_api3('CustomField', 'getsingle', [
142 'sequential' => 1,
143 'id' => $field['cfid'],
144 ]);
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
148 ),
149 2 => $customField['label'],
150 ]);
151 }
152 catch (CiviCRM_API3_Exception $e) {
153 $fieldName = ' <span style="color:red"> - Deleted - </span> ';
154 }
155 }
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>";
159 }
160
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>
164 </tr></thead><tbody>
165 $html
166 </tbody></table></p>
167 ";
168
169 $msg = new CRM_Utils_Check_Message(
170 __FUNCTION__,
171 ts($message),
172 ts('Disabled/Deleted fields on Smart Groups'),
173 \Psr\Log\LogLevel::WARNING,
174 'fa-server'
175 );
176 $messages[] = $msg;
177 }
178 return $messages;
179 }
180
181 }