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