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