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