a76587f13e458e0f12d4ee09686399928262721d
[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
177 $classes = array(
178 tx_reports_reports_status_Status::NOTICE => 'notice',
179 tx_reports_reports_status_Status::INFO => 'information',
180 tx_reports_reports_status_Status::OK => 'ok',
181 tx_reports_reports_status_Status::WARNING => 'warning',
182 tx_reports_reports_status_Status::ERROR => 'error',
183 );
184
185 $icon[tx_reports_reports_status_Status::WARNING] = t3lib_iconWorks::getSpriteIcon('status-dialog-warning');
186 $icon[tx_reports_reports_status_Status::ERROR] = t3lib_iconWorks::getSpriteIcon('status-dialog-error');
187 $messages = '';
188 $headerIcon = '';
189 $sectionSeverity = 0;
190
191 foreach ($providerState as $status) {
192 $severity = $status->getSeverity();
193 $sectionSeverity = $severity > $sectionSeverity ? $severity : $sectionSeverity;
194 $messages .= strtr($template, array(
195 '###CLASS###' => $classes[$severity],
196 '###HEADER###' => $status->getTitle(),
197 '###STATUS###' => $status->getValue(),
198 '###CONTENT###' => $status->getMessage(),
199 ));
200 }
201 if ($sectionSeverity > 0) {
202 $headerIcon = $icon[$sectionSeverity];
203 }
204 $content .= '<h2 id="' . $id . '" class="section-header ' . $collapsedClass . '">' . $headerIcon . $provider . '</h2>
205 <div ' . $collapsedStyle . '>' . $messages . '</div>';
206 }
207 return $content;
208 }
209
210 /**
211 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
212 *
213 * @param array A collection of statuses (with providers)
214 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
215 */
216 protected function sortStatusProviders(array $statusCollection) {
217 // Extract the primary status collections, i.e. the status groups
218 // that must appear on top of the status report
219 // Change their keys to localized collection titles
220 $primaryStatuses = array(
221 $GLOBALS['LANG']->getLL('status_typo3') => $statusCollection['typo3'],
222 $GLOBALS['LANG']->getLL('status_system') => $statusCollection['system'],
223 $GLOBALS['LANG']->getLL('status_security') => $statusCollection['security'],
224 $GLOBALS['LANG']->getLL('status_configuration') => $statusCollection['configuration']
225 );
226 unset(
227 $statusCollection['typo3'],
228 $statusCollection['system'],
229 $statusCollection['security'],
230 $statusCollection['configuration']
231 );
232 // Assemble list of secondary status collections with left-over collections
233 // Change their keys using localized labels if available
234 $secondaryStatuses = array();
235 foreach ($statusCollection as $statusProviderId => $collection) {
236 $label = '';
237 if (strpos($statusProviderId, 'LLL:') === 0) {
238 // Label provided by extension
239 $label = $GLOBALS['LANG']->sL($statusProviderId);
240 } else {
241 // Generic label
242 $label = $GLOBALS['LANG']->getLL('status_' . $statusProviderId);
243 }
244 $providerLabel = (empty($label)) ? $statusProviderId : $label;
245 $secondaryStatuses[$providerLabel] = $collection;
246 }
247 // Sort the secondary status collections alphabetically
248 ksort($secondaryStatuses);
249 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
250
251 return $orderedStatusCollection;
252 }
253
254 /**
255 * Sorts the statuses by severity
256 *
257 * @param array A collection of statuses per provider
258 * @return array The collection of statuses sorted by severity
259 */
260 protected function sortStatuses(array $statusCollection) {
261 $statuses = array();
262 $sortTitle = array();
263
264 foreach ($statusCollection as $status) {
265 if ($status->getTitle() === 'TYPO3') {
266 $header = $status;
267 continue;
268 }
269
270 $statuses[] = $status;
271 $sortTitle[] = $status->getSeverity();
272 }
273 array_multisort($sortTitle, SORT_DESC, $statuses);
274
275 // making sure that the core version information is always on the top
276 if (is_object($header)) {
277 array_unshift($statuses, $header);
278 }
279 return $statuses;
280 }
281
282 /**
283 * saves the section toggle state in the backend user's uc
284 *
285 * @param array array of parameters from the AJAX interface, currently unused
286 * @param TYPO3AJAX object of type TYPO3AJAX
287 * @return void
288 */
289
290 public function saveCollapseState(array $params, TYPO3AJAX $ajaxObj) {
291 $item = t3lib_div::_POST('item');
292 $state = (bool)t3lib_div::_POST('state');
293
294 $GLOBALS['BE_USER']->uc['reports']['states'][$item] = $state;
295 $GLOBALS['BE_USER']->writeUC();
296 }
297 }
298
299
300 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']) {
301 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']);
302 }
303
304 ?>