c32367c3180850af0c51295dc251a6ebd245f759
[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 */
82 protected function getStatusProviders() {
83 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
84 $this->statusProviders[$key] = array();
85
86 foreach ($statusProvidersList as $statusProvider) {
87 $statusProviderInstance = t3lib_div::makeInstance($statusProvider);
88
89 if ($statusProviderInstance instanceof tx_reports_StatusProvider) {
90 $this->statusProviders[$key][] = $statusProviderInstance;
91 }
92 }
93 }
94 }
95
96 /**
97 * Runs through all status providers and returns all statuses collected.
98 *
99 * @return array An array of tx_reports_reports_status_Status objects
100 */
101 public function getSystemStatus() {
102 $status = array();
103
104 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
105 $status[$statusProviderId] = array();
106
107 foreach ($statusProviderList as $statusProvider) {
108 $statuses = $statusProvider->getStatus();
109 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
110 }
111 }
112
113 return $status;
114 }
115
116 /**
117 * Determines the highest severity from the given statuses.
118 *
119 * @param array An array of tx_reports_reports_status_Status objects.
120 * @return integer The highest severity found from the statuses.
121 */
122 public function getHighestSeverity(array $statusCollection) {
123 $highestSeverity = tx_reports_reports_status_Status::NOTICE;
124
125 foreach ($statusCollection as $statusProvider => $providerStatuses) {
126 foreach ($providerStatuses as $status) {
127 if ($status->getSeverity() > $highestSeverity) {
128 $highestSeverity = $status->getSeverity();
129 }
130
131 // reached the highest severity level, no need to go on
132 if ($highestSeverity == tx_reports_reports_status_Status::ERROR) {
133 break;
134 }
135 }
136 }
137
138 return $highestSeverity;
139 }
140
141 /**
142 * Renders the system's status
143 *
144 * @param array 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 // TODO refactor into separate methods, status list and single status
150
151 $content = '';
152 $template = '
153 <div class="typo3-message message-###CLASS###">
154 <div class="header-container">
155 <div class="message-header message-left">###HEADER###</div>
156 <div class="message-header message-right">###STATUS###</div>
157 </div>
158 <div class="message-body">###CONTENT###</div>
159 </div>';
160
161 $statuses = $this->sortStatusProviders($statusCollection);
162
163 foreach($statuses as $provider => $providerStatus) {
164 $providerState = $this->sortStatuses($providerStatus);
165
166 $id = str_replace(' ', '-', $provider);
167 if (isset($GLOBALS['BE_USER']->uc['reports']['states'][$id]) && $GLOBALS['BE_USER']->uc['reports']['states'][$id]) {
168 $collapsedStyle = 'style="display: none"';
169 $collapsedClass = 'collapsed';
170 } else {
171 $collapsedStyle = '';
172 $collapsedClass = 'expanded';
173 }
174
175 $classes = array(
176 tx_reports_reports_status_Status::NOTICE => 'notice',
177 tx_reports_reports_status_Status::INFO => 'information',
178 tx_reports_reports_status_Status::OK => 'ok',
179 tx_reports_reports_status_Status::WARNING => 'warning',
180 tx_reports_reports_status_Status::ERROR => 'error',
181 );
182
183 $icon[tx_reports_reports_status_Status::WARNING] = t3lib_iconWorks::getSpriteIcon('status-dialog-warning');
184 $icon[tx_reports_reports_status_Status::ERROR] = t3lib_iconWorks::getSpriteIcon('status-dialog-error');
185 $messages = '';
186 $headerIcon = '';
187 $sectionSeverity = 0;
188
189 foreach ($providerState as $status) {
190 $severity = $status->getSeverity();
191 $sectionSeverity = $severity > $sectionSeverity ? $severity : $sectionSeverity;
192 $messages .= strtr($template, array(
193 '###CLASS###' => $classes[$severity],
194 '###HEADER###' => $status->getTitle(),
195 '###STATUS###' => $status->getValue(),
196 '###CONTENT###' => $status->getMessage(),
197 ));
198 }
199 if ($sectionSeverity > 0) {
200 $headerIcon = $icon[$sectionSeverity];
201 }
202 $content .= '<h2 id="' . $id . '" class="section-header ' . $collapsedClass . '">' . $headerIcon . $provider . '</h2>
203 <div ' . $collapsedStyle . '>' . $messages . '</div>';
204 }
205 return $content;
206 }
207
208 /**
209 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
210 *
211 * @param array A collection of statuses (with providers)
212 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
213 */
214 protected function sortStatusProviders(array $statusCollection) {
215 // Extract the primary status collections, i.e. the status groups
216 // that must appear on top of the status report
217 // Change their keys to localized collection titles
218 $primaryStatuses = array(
219 $GLOBALS['LANG']->getLL('status_typo3') => $statusCollection['typo3'],
220 $GLOBALS['LANG']->getLL('status_system') => $statusCollection['system'],
221 $GLOBALS['LANG']->getLL('status_security') => $statusCollection['security'],
222 $GLOBALS['LANG']->getLL('status_configuration') => $statusCollection['configuration']
223 );
224 unset(
225 $statusCollection['typo3'],
226 $statusCollection['system'],
227 $statusCollection['security'],
228 $statusCollection['configuration']
229 );
230
231 // Assemble list of secondary status collections with left-over collections
232 // Change their keys using localized labels if available
233 // TODO extract into getLabel() method
234 $secondaryStatuses = array();
235 foreach ($statusCollection as $statusProviderId => $collection) {
236 $label = '';
237
238 if (strpos($statusProviderId, 'LLL:') === 0) {
239 // Label provided by extension
240 $label = $GLOBALS['LANG']->sL($statusProviderId);
241 } else {
242 // Generic label
243 $label = $GLOBALS['LANG']->getLL('status_' . $statusProviderId);
244 }
245
246 $providerLabel = (empty($label)) ? $statusProviderId : $label;
247 $secondaryStatuses[$providerLabel] = $collection;
248 }
249 // Sort the secondary status collections alphabetically
250 ksort($secondaryStatuses);
251 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
252
253 return $orderedStatusCollection;
254 }
255
256 /**
257 * Sorts the statuses by severity
258 *
259 * @param array A collection of statuses per provider
260 * @return array The collection of statuses sorted by severity
261 */
262 protected function sortStatuses(array $statusCollection) {
263 $statuses = array();
264 $sortTitle = array();
265
266 foreach ($statusCollection as $status) {
267 if ($status->getTitle() === 'TYPO3') {
268 $header = $status;
269 continue;
270 }
271
272 $statuses[] = $status;
273 $sortTitle[] = $status->getSeverity();
274 }
275 array_multisort($sortTitle, SORT_DESC, $statuses);
276
277 // making sure that the core version information is always on the top
278 if (is_object($header)) {
279 array_unshift($statuses, $header);
280 }
281 return $statuses;
282 }
283
284 /**
285 * Saves the section toggle state in the backend user's uc.
286 *
287 * @param array Array of parameters from the AJAX interface, currently unused
288 * @param TYPO3AJAX Object of type TYPO3AJAX
289 */
290
291 public function saveCollapseState(array $params, TYPO3AJAX $ajaxObj) {
292 $item = t3lib_div::_POST('item');
293 $state = (bool)t3lib_div::_POST('state');
294
295 $GLOBALS['BE_USER']->uc['reports']['states'][$item] = $state;
296 $GLOBALS['BE_USER']->writeUC();
297 }
298 }
299
300
301 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']) {
302 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']);
303 }
304
305 ?>