[TASK] Fix CGL violations against SelfMemberReference
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / Status.php
1 <?php
2 namespace TYPO3\CMS\Reports\Report\Status;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * The status report
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage reports
32 */
33 class Status implements \TYPO3\CMS\Reports\ReportInterface {
34
35 /**
36 * @var array
37 */
38 protected $statusProviders = array();
39
40 /**
41 * Constructor for class tx_reports_report_Status
42 */
43 public function __construct() {
44 $this->getStatusProviders();
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 $status = $this->getSystemStatus();
56 $highestSeverity = $this->getHighestSeverity($status);
57 // Updating the registry
58 $registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry');
59 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
60 $content .= '<p class="help">' . $GLOBALS['LANG']->getLL('status_report_explanation') . '</p>';
61 return $content . $this->renderStatus($status);
62 }
63
64 /**
65 * Gets all registered status providers and creates instances of them.
66 *
67 * @return void
68 */
69 protected function getStatusProviders() {
70 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
71 $this->statusProviders[$key] = array();
72 foreach ($statusProvidersList as $statusProvider) {
73 $statusProviderInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($statusProvider);
74 if ($statusProviderInstance instanceof \TYPO3\CMS\Reports\StatusProviderInterface) {
75 $this->statusProviders[$key][] = $statusProviderInstance;
76 }
77 }
78 }
79 }
80
81 /**
82 * Runs through all status providers and returns all statuses collected.
83 *
84 * @return array An array of tx_reports_reports_status_Status objects
85 */
86 public function getSystemStatus() {
87 $status = array();
88 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
89 $status[$statusProviderId] = array();
90 foreach ($statusProviderList as $statusProvider) {
91 $statuses = $statusProvider->getStatus();
92 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
93 }
94 }
95 return $status;
96 }
97
98 /**
99 * Determines the highest severity from the given statuses.
100 *
101 * @param array $statusCollection An array of tx_reports_reports_status_Status objects.
102 * @return integer The highest severity found from the statuses.
103 */
104 public function getHighestSeverity(array $statusCollection) {
105 $highestSeverity = \TYPO3\CMS\Reports\Status::NOTICE;
106 foreach ($statusCollection as $statusProvider => $providerStatuses) {
107 /** @var $status \TYPO3\CMS\Reports\Status */
108 foreach ($providerStatuses as $status) {
109 if ($status->getSeverity() > $highestSeverity) {
110 $highestSeverity = $status->getSeverity();
111 }
112 // Reached the highest severity level, no need to go on
113 if ($highestSeverity == \TYPO3\CMS\Reports\Status::ERROR) {
114 break;
115 }
116 }
117 }
118 return $highestSeverity;
119 }
120
121 /**
122 * Renders the system's status
123 *
124 * @param array $statusCollection An array of statuses as returned by the available status providers
125 * @return string The system status as an HTML table
126 */
127 protected function renderStatus(array $statusCollection) {
128 // TODO refactor into separate methods, status list and single status
129 $content = '';
130 $template = '
131 <div class="typo3-message message-###CLASS###">
132 <div class="header-container">
133 <div class="message-header message-left">###HEADER###</div>
134 <div class="message-header message-right">###STATUS###</div>
135 </div>
136 <div class="message-body">###CONTENT###</div>
137 </div>';
138 $statuses = $this->sortStatusProviders($statusCollection);
139 foreach ($statuses as $provider => $providerStatus) {
140 $providerState = $this->sortStatuses($providerStatus);
141 $id = str_replace(' ', '-', $provider);
142 $classes = array(
143 \TYPO3\CMS\Reports\Status::NOTICE => 'notice',
144 \TYPO3\CMS\Reports\Status::INFO => 'information',
145 \TYPO3\CMS\Reports\Status::OK => 'ok',
146 \TYPO3\CMS\Reports\Status::WARNING => 'warning',
147 \TYPO3\CMS\Reports\Status::ERROR => 'error'
148 );
149 $icon[\TYPO3\CMS\Reports\Status::WARNING] = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-dialog-warning');
150 $icon[\TYPO3\CMS\Reports\Status::ERROR] = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-dialog-error');
151 $messages = '';
152 $headerIcon = '';
153 $sectionSeverity = 0;
154 /** @var $status \TYPO3\CMS\Reports\Status */
155 foreach ($providerState as $status) {
156 $severity = $status->getSeverity();
157 $sectionSeverity = $severity > $sectionSeverity ? $severity : $sectionSeverity;
158 $messages .= strtr($template, array(
159 '###CLASS###' => $classes[$severity],
160 '###HEADER###' => $status->getTitle(),
161 '###STATUS###' => $status->getValue(),
162 '###CONTENT###' => $status->getMessage()
163 ));
164 }
165 if ($sectionSeverity > 0) {
166 $headerIcon = $icon[$sectionSeverity];
167 }
168 $content .= $GLOBALS['TBE_TEMPLATE']->collapseableSection($headerIcon . $provider, $messages, $id, 'reports.states');
169 }
170 return $content;
171 }
172
173 /**
174 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
175 *
176 * @param array $statusCollection A collection of statuses (with providers)
177 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
178 */
179 protected function sortStatusProviders(array $statusCollection) {
180 // Extract the primary status collections, i.e. the status groups
181 // that must appear on top of the status report
182 // Change their keys to localized collection titles
183 $primaryStatuses = array(
184 $GLOBALS['LANG']->getLL('status_typo3') => $statusCollection['typo3'],
185 $GLOBALS['LANG']->getLL('status_system') => $statusCollection['system'],
186 $GLOBALS['LANG']->getLL('status_security') => $statusCollection['security'],
187 $GLOBALS['LANG']->getLL('status_configuration') => $statusCollection['configuration']
188 );
189 unset($statusCollection['typo3'], $statusCollection['system'], $statusCollection['security'], $statusCollection['configuration']);
190 // Assemble list of secondary status collections with left-over collections
191 // Change their keys using localized labels if available
192 // TODO extract into getLabel() method
193 $secondaryStatuses = array();
194 foreach ($statusCollection as $statusProviderId => $collection) {
195 $label = '';
196 if (strpos($statusProviderId, 'LLL:') === 0) {
197 // Label provided by extension
198 $label = $GLOBALS['LANG']->sL($statusProviderId);
199 } else {
200 // Generic label
201 $label = $GLOBALS['LANG']->getLL('status_' . $statusProviderId);
202 }
203 $providerLabel = empty($label) ? $statusProviderId : $label;
204 $secondaryStatuses[$providerLabel] = $collection;
205 }
206 // Sort the secondary status collections alphabetically
207 ksort($secondaryStatuses);
208 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
209 return $orderedStatusCollection;
210 }
211
212 /**
213 * Sorts the statuses by severity
214 *
215 * @param array $statusCollection A collection of statuses per provider
216 * @return array The collection of statuses sorted by severity
217 */
218 protected function sortStatuses(array $statusCollection) {
219 $statuses = array();
220 $sortTitle = array();
221 /** @var $status \TYPO3\CMS\Reports\Status */
222 foreach ($statusCollection as $status) {
223 if ($status->getTitle() === 'TYPO3') {
224 $header = $status;
225 continue;
226 }
227 $statuses[] = $status;
228 $sortTitle[] = $status->getSeverity();
229 }
230 array_multisort($sortTitle, SORT_DESC, $statuses);
231 // Making sure that the core version information is always on the top
232 if (is_object($header)) {
233 array_unshift($statuses, $header);
234 }
235 return $statuses;
236 }
237
238 }
239
240
241 ?>