e44a938e6aa334fc628af40ed7c84baea811af17
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / reports / class.tx_reports_reports_status.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 $status = array();
55 $content = '';
56
57 foreach ($this->statusProviders as $statusProviderId => $statusProvidersList) {
58 $status[$statusProviderId] = array();
59 foreach ($statusProvidersList as $statusProvider) {
60 $statuses = $statusProvider->getStatus();
61 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
62 }
63 }
64
65 $content .= '<p class="help">'
66 . $GLOBALS['LANG']->getLL('status_report_explanation')
67 . '</p>';
68
69 return $content . $this->renderStatus($status);
70 }
71
72 /**
73 * Gets all registered status providers and creates instances of them.
74 *
75 * @return void
76 */
77 protected function getStatusProviders() {
78 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
79 $this->statusProviders[$key] = array();
80 foreach ($statusProvidersList as $statusProvider) {
81 $statusProviderInstance = t3lib_div::makeInstance($statusProvider);
82 if ($statusProviderInstance instanceof tx_reports_StatusProvider) {
83 $this->statusProviders[$key][] = $statusProviderInstance;
84 }
85 }
86 }
87 }
88
89 /**
90 * Renders the system's status
91 *
92 * @param array An array of statuses as returned by the available status providers
93 * @return string The system status as an HTML table
94 */
95 protected function renderStatus(array $statusCollection) {
96 $content = '';
97 $template = '
98 <div class="typo3-message message-###CLASS###">
99 <div class="header-container">
100 <div class="message-header message-left">###HEADER###</div>
101 <div class="message-header message-right">###STATUS###</div>
102 </div>
103 <div class="message-body">###CONTENT###</div>
104 </div>';
105
106 $statuses = $this->sortStatusProviders($statusCollection);
107
108 foreach($statuses as $provider => $providerStatus) {
109 $providerState = $this->sortStatuses($providerStatus);
110
111 $id = str_replace(' ', '-', $provider);
112 if (isset($GLOBALS['BE_USER']->uc['reports']['states'][$id]) && $GLOBALS['BE_USER']->uc['reports']['states'][$id]) {
113 $collapsedStyle = 'style="display:none"';
114 $collapsedClass = 'collapsed';
115 } else {
116 $collapsedStyle = '';
117 $collapsedClass = 'expanded';
118 }
119
120
121 $classes = array(
122 tx_reports_reports_status_Status::NOTICE => 'notice',
123 tx_reports_reports_status_Status::INFO => 'information',
124 tx_reports_reports_status_Status::OK => 'ok',
125 tx_reports_reports_status_Status::WARNING => 'warning',
126 tx_reports_reports_status_Status::ERROR => 'error',
127 );
128
129 $icon[tx_reports_reports_status_Status::WARNING] = '<img' . t3lib_iconWorks::skinImg($this->doc->backPath, 'gfx/warning.png', 'width="16" height="16"') . ' />';
130 $icon[tx_reports_reports_status_Status::ERROR] = '<img' . t3lib_iconWorks::skinImg($this->doc->backPath, 'gfx/error.png', 'width="16" height="16"') . ' />';
131 $messages = '';
132 $headerIcon = '';
133 $sectionSeverity = 0;
134
135 foreach ($providerState as $status) {
136 $severity = $status->getSeverity();
137 $sectionSeverity = $severity > $sectionSeverity ? $severity : $sectionSeverity;
138 $messages .= strtr($template, array(
139 '###CLASS###' => $classes[$severity],
140 '###HEADER###' => $status->getTitle(),
141 '###STATUS###' => $status->getValue(),
142 '###CONTENT###' => $status->getMessage(),
143 ));
144 }
145 if ($sectionSeverity > 0) {
146 $headerIcon = $icon[$sectionSeverity];
147 }
148 $content .= '<h2 id="' . $id . '" class="section-header ' . $collapsedClass . '">' . $headerIcon . $provider . '</h2>
149 <div ' . $collapsedStyle . '>' . $messages . '</div>';
150 }
151 return $content;
152 }
153
154 /**
155 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
156 *
157 * @param array A collection of statuses (with providers)
158 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
159 */
160 protected function sortStatusProviders(array $statusCollection) {
161 // Extract the primary status collections, i.e. the status groups
162 // that must appear on top of the status report
163 // Change their keys to localized collection titles
164 $primaryStatuses = array(
165 $GLOBALS['LANG']->getLL('status_typo3') => $statusCollection['typo3'],
166 $GLOBALS['LANG']->getLL('status_system') => $statusCollection['system'],
167 $GLOBALS['LANG']->getLL('status_security') => $statusCollection['security'],
168 $GLOBALS['LANG']->getLL('status_configuration') => $statusCollection['configuration']
169 );
170 unset(
171 $statusCollection['typo3'],
172 $statusCollection['system'],
173 $statusCollection['security'],
174 $statusCollection['configuration']
175 );
176 // Assemble list of secondary status collections with left-over collections
177 // Change their keys using localized labels if available
178 $secondaryStatuses = array();
179 foreach ($statusCollection as $statusProviderId => $collection) {
180 $label = '';
181 if (strpos($statusProviderId, 'LLL:') === 0) {
182 // Label provided by extension
183 $label = $GLOBALS['LANG']->sL($statusProviderId);
184 } else {
185 // Generic label
186 $label = $GLOBALS['LANG']->getLL('status_' . $statusProviderId);
187 }
188 $providerLabel = (empty($label)) ? $statusProviderId : $label;
189 $secondaryStatuses[$providerLabel] = $collection;
190 }
191 // Sort the secondary status collections alphabetically
192 ksort($secondaryStatuses);
193 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
194
195 return $orderedStatusCollection;
196 }
197
198 /**
199 * Sorts the statuses by severity
200 *
201 * @param array A collection of statuses per provider
202 * @return array The collection of statuses sorted by severity
203 */
204 protected function sortStatuses(array $statusCollection) {
205 $statuses = array();
206 $sortTitle = array();
207
208 foreach ($statusCollection as $status) {
209 if ($status->getTitle() === 'TYPO3') {
210 $header = $status;
211 continue;
212 }
213
214 $statuses[] = $status;
215 $sortTitle[] = $status->getSeverity();
216 }
217 array_multisort($sortTitle, SORT_DESC, $statuses);
218
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 /**
227 * saves the section toggle state in the backend user's uc
228 *
229 * @param array array of parameters from the AJAX interface, currently unused
230 * @param TYPO3AJAX object of type TYPO3AJAX
231 * @return void
232 */
233
234 public function saveCollapseState(array $params, TYPO3AJAX $ajaxObj) {
235 $item = t3lib_div::_POST('item');
236 $state = (bool)t3lib_div::_POST('state');
237
238 $GLOBALS['BE_USER']->uc['reports']['states'][$item] = $state;
239 $GLOBALS['BE_USER']->writeUC();
240 }
241 }
242
243
244 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']) {
245 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']);
246 }
247
248 ?>