[CLEANUP] Code cleanup in ext:reports
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / Status.php
1 <?php
2 namespace TYPO3\CMS\Reports\Report\Status;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Registry;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Lang\LanguageService;
20 use TYPO3\CMS\Reports\ExtendedStatusProviderInterface;
21 use TYPO3\CMS\Reports\ReportInterface;
22 use TYPO3\CMS\Reports\Status as ReportStatus;
23 use TYPO3\CMS\Reports\StatusProviderInterface;
24
25 /**
26 * The status report
27 */
28 class Status implements ReportInterface
29 {
30 /**
31 * @var StatusProviderInterface[][]
32 */
33 protected $statusProviders = array();
34
35 /**
36 * Constructor for class tx_reports_report_Status
37 */
38 public function __construct()
39 {
40 $this->getStatusProviders();
41 $this->getLanguageService()->includeLLFile('EXT:reports/Resources/Private/Language/locallang_reports.xlf');
42 }
43
44 /**
45 * Takes care of creating / rendering the status report
46 *
47 * @return string The status report as HTML
48 */
49 public function getReport()
50 {
51 $content = '';
52 $status = $this->getSystemStatus();
53 $highestSeverity = $this->getHighestSeverity($status);
54 // Updating the registry
55 $registry = GeneralUtility::makeInstance(Registry::class);
56 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
57 $content .= '<p class="lead">' . $this->getLanguageService()->getLL('status_report_explanation') . '</p>';
58 return $content . $this->renderStatus($status);
59 }
60
61 /**
62 * Gets all registered status providers and creates instances of them.
63 *
64 * @return void
65 */
66 protected function getStatusProviders()
67 {
68 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
69 $this->statusProviders[$key] = array();
70 foreach ($statusProvidersList as $statusProvider) {
71 $statusProviderInstance = GeneralUtility::makeInstance($statusProvider);
72 if ($statusProviderInstance instanceof StatusProviderInterface) {
73 $this->statusProviders[$key][] = $statusProviderInstance;
74 }
75 }
76 }
77 }
78
79 /**
80 * Runs through all status providers and returns all statuses collected.
81 *
82 * @return \TYPO3\CMS\Reports\Status[]
83 */
84 public function getSystemStatus()
85 {
86 $status = array();
87 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
88 $status[$statusProviderId] = array();
89 foreach ($statusProviderList as $statusProvider) {
90 $statuses = $statusProvider->getStatus();
91 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
92 }
93 }
94 return $status;
95 }
96
97 /**
98 * Runs through all status providers and returns all statuses collected, which are detailed.
99 *
100 * @return \TYPO3\CMS\Reports\Status[]
101 */
102 public function getDetailedSystemStatus()
103 {
104 $status = array();
105 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
106 $status[$statusProviderId] = array();
107 foreach ($statusProviderList as $statusProvider) {
108 if ($statusProvider instanceof ExtendedStatusProviderInterface) {
109 $statuses = $statusProvider->getDetailedStatus();
110 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
111 }
112 }
113 }
114 return $status;
115 }
116
117 /**
118 * Determines the highest severity from the given statuses.
119 *
120 * @param array $statusCollection An array of \TYPO3\CMS\Reports\Status objects.
121 * @return int The highest severity found from the statuses.
122 */
123 public function getHighestSeverity(array $statusCollection)
124 {
125 $highestSeverity = ReportStatus::NOTICE;
126 foreach ($statusCollection as $statusProvider => $providerStatuses) {
127 /** @var ReportStatus $status */
128 foreach ($providerStatuses as $status) {
129 if ($status->getSeverity() > $highestSeverity) {
130 $highestSeverity = $status->getSeverity();
131 }
132 // Reached the highest severity level, no need to go on
133 if ($highestSeverity == ReportStatus::ERROR) {
134 break;
135 }
136 }
137 }
138 return $highestSeverity;
139 }
140
141 /**
142 * Renders the system's status
143 *
144 * @param array $statusCollection An array of statuses as returned by the available status providers
145 * @return string The system status as an HTML table
146 */
147 protected function renderStatus(array $statusCollection)
148 {
149 $content = '';
150 $template = '
151 <tr>
152 <td class="###CLASS### col-xs-6">###HEADER###</td>
153 <td class="###CLASS### col-xs-6">###STATUS###<br>###CONTENT###</td>
154 </tr>
155 ';
156 $statuses = $this->sortStatusProviders($statusCollection);
157 $id = 0;
158 foreach ($statuses as $provider => $providerStatus) {
159 $providerState = $this->sortStatuses($providerStatus);
160 $id++;
161 $classes = array(
162 ReportStatus::NOTICE => 'notice',
163 ReportStatus::INFO => 'info',
164 ReportStatus::OK => 'success',
165 ReportStatus::WARNING => 'warning',
166 ReportStatus::ERROR => 'danger'
167 );
168 $messages = '';
169 /** @var ReportStatus $status */
170 foreach ($providerState as $status) {
171 $severity = $status->getSeverity();
172 $messages .= strtr($template, array(
173 '###CLASS###' => $classes[$severity],
174 '###HEADER###' => $status->getTitle(),
175 '###STATUS###' => $status->getValue(),
176 '###CONTENT###' => $status->getMessage()
177 ));
178 }
179 $header = '<h2>' . $provider . '</h2>';
180 $table = '<table class="table table-striped table-hover">';
181 $table .= '<tbody>' . $messages . '</tbody>';
182 $table .= '</table>';
183
184 $content .= $header . $table;
185 }
186 return $content;
187 }
188
189 /**
190 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
191 *
192 * @param array $statusCollection A collection of statuses (with providers)
193 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
194 */
195 protected function sortStatusProviders(array $statusCollection)
196 {
197 // Extract the primary status collections, i.e. the status groups
198 // that must appear on top of the status report
199 // Change their keys to localized collection titles
200 $primaryStatuses = array(
201 $this->getLanguageService()->getLL('status_typo3') => $statusCollection['typo3'],
202 $this->getLanguageService()->getLL('status_system') => $statusCollection['system'],
203 $this->getLanguageService()->getLL('status_security') => $statusCollection['security'],
204 $this->getLanguageService()->getLL('status_configuration') => $statusCollection['configuration']
205 );
206 unset($statusCollection['typo3'], $statusCollection['system'], $statusCollection['security'], $statusCollection['configuration']);
207 // Assemble list of secondary status collections with left-over collections
208 // Change their keys using localized labels if available
209 // @todo extract into getLabel() method
210 $secondaryStatuses = array();
211 foreach ($statusCollection as $statusProviderId => $collection) {
212 $label = '';
213 if (strpos($statusProviderId, 'LLL:') === 0) {
214 // Label provided by extension
215 $label = $this->getLanguageService()->sL($statusProviderId);
216 } else {
217 // Generic label
218 $label = $this->getLanguageService()->getLL('status_' . $statusProviderId);
219 }
220 $providerLabel = empty($label) ? $statusProviderId : $label;
221 $secondaryStatuses[$providerLabel] = $collection;
222 }
223 // Sort the secondary status collections alphabetically
224 ksort($secondaryStatuses);
225 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
226 return $orderedStatusCollection;
227 }
228
229 /**
230 * Sorts the statuses by severity
231 *
232 * @param array $statusCollection A collection of statuses per provider
233 * @return array The collection of statuses sorted by severity
234 */
235 protected function sortStatuses(array $statusCollection)
236 {
237 $statuses = array();
238 $sortTitle = array();
239 $header = null;
240 /** @var ReportStatus $status */
241 foreach ($statusCollection as $status) {
242 if ($status->getTitle() === 'TYPO3') {
243 $header = $status;
244 continue;
245 }
246 $statuses[] = $status;
247 $sortTitle[] = $status->getSeverity();
248 }
249 array_multisort($sortTitle, SORT_DESC, $statuses);
250 // Making sure that the core version information is always on the top
251 if (is_object($header)) {
252 array_unshift($statuses, $header);
253 }
254 return $statuses;
255 }
256
257 /**
258 * @return LanguageService
259 */
260 protected function getLanguageService()
261 {
262 return $GLOBALS['LANG'];
263 }
264 }