minor code beautification
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / reports / class.tx_reports_reports_status.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * The status report
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage reports
32 *
33 * $Id$
34 */
35 class tx_reports_reports_Status implements tx_reports_Report {
36
37 protected $statusProviders = array();
38
39 /**
40 * constructor for class tx_reports_report_Status
41 */
42 public function __construct() {
43 $this->getStatusProviders();
44
45 $GLOBALS['LANG']->includeLLFile('EXT:reports/reports/locallang.xml');
46 }
47
48 /**
49 * Takes care of creating / rendering the status report
50 *
51 * @return string The status report as HTML
52 */
53 public function getReport() {
54 $content = '';
55
56 foreach ($this->statusProviders as $statusProviderId => $statusProvidersList) {
57 $status[$statusProviderId] = array();
58 foreach ($statusProvidersList as $statusProvider) {
59 $statuses = $statusProvider->getStatus();
60 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
61 }
62 }
63
64 $status = $this->getSystemStatus();
65 $highestSeverity = $this->getHighestSeverity($status);
66
67 // updating the registry
68 $registry = t3lib_div::makeInstance('t3lib_Registry');
69 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
70
71 $content .= '<p class="help">'
72 . $GLOBALS['LANG']->getLL('status_report_explanation')
73 . '</p>';
74
75 return $content . $this->renderStatus($status);
76 }
77
78 /**
79 * Gets all registered status providers and creates instances of them.
80 *
81 * @return void
82 */
83 protected function getStatusProviders() {
84 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
85 $this->statusProviders[$key] = array();
86
87 foreach ($statusProvidersList as $statusProvider) {
88 $statusProviderInstance = t3lib_div::makeInstance($statusProvider);
89
90 if ($statusProviderInstance instanceof tx_reports_StatusProvider) {
91 $this->statusProviders[$key][] = $statusProviderInstance;
92 }
93 }
94 }
95 }
96
97 /**
98 * Runs through all status providers and returns all statuses collected.
99 *
100 * @return array An array of tx_reports_reports_status_Status objects
101 */
102 public function getSystemStatus() {
103 $status = array();
104
105 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
106 $status[$statusProviderId] = array();
107
108 foreach ($statusProviderList as $statusProvider) {
109 $statuses = $statusProvider->getStatus();
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 An array of tx_reports_reports_status_Status objects.
121 * @return integer The highest severity found from the statuses.
122 */
123 public function getHighestSeverity(array $statusCollection) {
124 $highestSeverity = tx_reports_reports_status_Status::NOTICE;
125
126 foreach ($statusCollection as $statusProvider => $providerStatuses) {
127 foreach ($providerStatuses as $status) {
128 if ($status->getSeverity() > $highestSeverity) {
129 $highestSeverity = $status->getSeverity();
130 }
131
132 // reached the highest severity level, no need to go on
133 if ($highestSeverity == tx_reports_reports_status_Status::ERROR) {
134 break;
135 }
136 }
137 }
138
139 return $highestSeverity;
140 }
141
142 /**
143 * Renders the system's status
144 *
145 * @param array An array of statuses as returned by the available status providers
146 * @return string The system status as an HTML table
147 */
148 protected function renderStatus(array $statusCollection) {
149
150 // TODO refactor into separate methods, status list and single status
151
152 $content = '';
153 $template = '
154 <div class="typo3-message message-###CLASS###">
155 <div class="header-container">
156 <div class="message-header message-left">###HEADER###</div>
157 <div class="message-header message-right">###STATUS###</div>
158 </div>
159 <div class="message-body">###CONTENT###</div>
160 </div>';
161
162 $statuses = $this->sortStatusProviders($statusCollection);
163
164 foreach($statuses as $provider => $providerStatus) {
165 $providerState = $this->sortStatuses($providerStatus);
166
167 $id = str_replace(' ', '-', $provider);
168 if (isset($GLOBALS['BE_USER']->uc['reports']['states'][$id]) && $GLOBALS['BE_USER']->uc['reports']['states'][$id]) {
169 $collapsedStyle = 'style="display: none"';
170 $collapsedClass = 'collapsed';
171 } else {
172 $collapsedStyle = '';
173 $collapsedClass = 'expanded';
174 }
175
176 $classes = array(
177 tx_reports_reports_status_Status::NOTICE => 'notice',
178 tx_reports_reports_status_Status::INFO => 'information',
179 tx_reports_reports_status_Status::OK => 'ok',
180 tx_reports_reports_status_Status::WARNING => 'warning',
181 tx_reports_reports_status_Status::ERROR => 'error',
182 );
183
184 $icon[tx_reports_reports_status_Status::WARNING] = t3lib_iconWorks::getSpriteIcon('status-dialog-warning');
185 $icon[tx_reports_reports_status_Status::ERROR] = t3lib_iconWorks::getSpriteIcon('status-dialog-error');
186 $messages = '';
187 $headerIcon = '';
188 $sectionSeverity = 0;
189
190 foreach ($providerState as $status) {
191 $severity = $status->getSeverity();
192 $sectionSeverity = $severity > $sectionSeverity ? $severity : $sectionSeverity;
193 $messages .= strtr($template, array(
194 '###CLASS###' => $classes[$severity],
195 '###HEADER###' => $status->getTitle(),
196 '###STATUS###' => $status->getValue(),
197 '###CONTENT###' => $status->getMessage(),
198 ));
199 }
200 if ($sectionSeverity > 0) {
201 $headerIcon = $icon[$sectionSeverity];
202 }
203 $content .= '<h2 id="' . $id . '" class="section-header ' . $collapsedClass . '">' . $headerIcon . $provider . '</h2>
204 <div ' . $collapsedStyle . '>' . $messages . '</div>';
205 }
206 return $content;
207 }
208
209 /**
210 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
211 *
212 * @param array A collection of statuses (with providers)
213 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
214 */
215 protected function sortStatusProviders(array $statusCollection) {
216 // Extract the primary status collections, i.e. the status groups
217 // that must appear on top of the status report
218 // Change their keys to localized collection titles
219 $primaryStatuses = array(
220 $GLOBALS['LANG']->getLL('status_typo3') => $statusCollection['typo3'],
221 $GLOBALS['LANG']->getLL('status_system') => $statusCollection['system'],
222 $GLOBALS['LANG']->getLL('status_security') => $statusCollection['security'],
223 $GLOBALS['LANG']->getLL('status_configuration') => $statusCollection['configuration']
224 );
225 unset(
226 $statusCollection['typo3'],
227 $statusCollection['system'],
228 $statusCollection['security'],
229 $statusCollection['configuration']
230 );
231
232 // Assemble list of secondary status collections with left-over collections
233 // Change their keys using localized labels if available
234 // TODO extract into getLabel() method
235 $secondaryStatuses = array();
236 foreach ($statusCollection as $statusProviderId => $collection) {
237 $label = '';
238
239 if (strpos($statusProviderId, 'LLL:') === 0) {
240 // Label provided by extension
241 $label = $GLOBALS['LANG']->sL($statusProviderId);
242 } else {
243 // Generic label
244 $label = $GLOBALS['LANG']->getLL('status_' . $statusProviderId);
245 }
246
247 $providerLabel = (empty($label)) ? $statusProviderId : $label;
248 $secondaryStatuses[$providerLabel] = $collection;
249 }
250 // Sort the secondary status collections alphabetically
251 ksort($secondaryStatuses);
252 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
253
254 return $orderedStatusCollection;
255 }
256
257 /**
258 * Sorts the statuses by severity
259 *
260 * @param array A collection of statuses per provider
261 * @return array The collection of statuses sorted by severity
262 */
263 protected function sortStatuses(array $statusCollection) {
264 $statuses = array();
265 $sortTitle = array();
266
267 foreach ($statusCollection as $status) {
268 if ($status->getTitle() === 'TYPO3') {
269 $header = $status;
270 continue;
271 }
272
273 $statuses[] = $status;
274 $sortTitle[] = $status->getSeverity();
275 }
276 array_multisort($sortTitle, SORT_DESC, $statuses);
277
278 // making sure that the core version information is always on the top
279 if (is_object($header)) {
280 array_unshift($statuses, $header);
281 }
282 return $statuses;
283 }
284
285 /**
286 * saves the section toggle state in the backend user's uc
287 *
288 * @param array array of parameters from the AJAX interface, currently unused
289 * @param TYPO3AJAX object of type TYPO3AJAX
290 * @return void
291 */
292
293 public function saveCollapseState(array $params, TYPO3AJAX $ajaxObj) {
294 $item = t3lib_div::_POST('item');
295 $state = (bool)t3lib_div::_POST('state');
296
297 $GLOBALS['BE_USER']->uc['reports']['states'][$item] = $state;
298 $GLOBALS['BE_USER']->writeUC();
299 }
300 }
301
302
303 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']) {
304 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']);
305 }
306
307 ?>