Cleanup #12245: Reports: Cleaned up API for providers in status report
[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 => $statusProvider) {
56 if (strcmp(substr($statusProviderId, 0, 4), 'LLL:')) {
57 $providerLabel = $GLOBALS['LANG']->getLL('status_' . $statusProviderId);
58 } else {
59 // label from extension
60 $providerLabel = $GLOBALS['LANG']->sL($statusProviderId);
61 }
62 $provider = $providerLabel ? $providerLabel : $statusProviderId;
63 $status[$provider] = $statusProvider->getStatus();
64 }
65
66 $content .= '<p class="help">'
67 . $GLOBALS['LANG']->getLL('status_report_explanation')
68 . '</p>';
69
70 return $content . $this->renderStatus($status);
71 }
72
73 /**
74 * Gets all registered status providers and creates instances of them.
75 *
76 * @return void
77 */
78 protected function getStatusProviders() {
79 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvider) {
80 $statusProviderInstance = t3lib_div::makeInstance($statusProvider);
81 if ($statusProviderInstance instanceof tx_reports_StatusProvider) {
82 $this->statusProviders[$key] = $statusProviderInstance;
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 adds "_install" provider 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 $systemStatus = array(
160 'TYPO3 System' => $statusCollection['Installation'],
161 'System' => $statusCollection['System'],
162 'Security' => $statusCollection['security'],
163 'Configuration' => $statusCollection['configuration'],
164 );
165 unset(
166 $statusCollection['Installation'],
167 $statusCollection['System'],
168 $statusCollection['security'],
169 $statusCollection['configuration']
170 );
171 ksort($statusCollection);
172 $statusCollection = array_merge($systemStatus, $statusCollection);
173
174 return $statusCollection;
175 }
176
177 /**
178 * Sorts the statuses by severity
179 *
180 * @param array A collection of statuses per provider
181 * @return array The collection of statuses sorted by severity
182 */
183 protected function sortStatuses(array $statusCollection) {
184 $statuses = array();
185 $sortTitle = array();
186
187 foreach ($statusCollection as $status) {
188 if ($status->getTitle() === 'TYPO3') {
189 $header = $status;
190 continue;
191 }
192
193 $statuses[] = $status;
194 $sortTitle[] = $status->getSeverity();
195 }
196 array_multisort($sortTitle, SORT_DESC, $statuses);
197
198 // making sure that the core version information is always on the top
199 if(is_object($header)) {
200 array_unshift($statuses, $header);
201 }
202 return $statuses;
203 }
204
205 /**
206 * saves the section toggle state in the backend user's uc
207 *
208 * @param array array of parameters from the AJAX interface, currently unused
209 * @param TYPO3AJAX object of type TYPO3AJAX
210 * @return void
211 */
212
213 public function saveCollapseState(array $params, TYPO3AJAX $ajaxObj) {
214 $item = t3lib_div::_POST('item');
215 $state = (bool)t3lib_div::_POST('state');
216
217 $GLOBALS['BE_USER']->uc['reports']['states'][$item] = $state;
218 $GLOBALS['BE_USER']->writeUC();
219 }
220 }
221
222
223 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']) {
224 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/reports/reports/class.tx_reports_reports_status.php']);
225 }
226
227 ?>