[TASK] Fix CGL errors
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Controller / ReportController.php
1 <?php
2 namespace TYPO3\CMS\Reports\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Backend\View\BackendTemplateView;
17 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
20 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
21 use TYPO3\CMS\Lang\LanguageService;
22 use TYPO3\CMS\Reports\ReportInterface;
23
24 /**
25 * Reports controller
26 */
27 class ReportController extends ActionController
28 {
29 /**
30 * @var BackendTemplateView
31 */
32 protected $view;
33
34 /**
35 * BackendTemplateView Container
36 *
37 * @var BackendTemplateView
38 */
39 protected $defaultViewObjectName = BackendTemplateView::class;
40
41 /**
42 * Module name for the shortcut
43 *
44 * @var string
45 */
46 protected $shortcutName;
47
48 /**
49 * Redirect to the saved report
50 *
51 * @return void
52 */
53 public function initializeAction()
54 {
55 $vars = GeneralUtility::_GET('tx_reports_system_reportstxreportsm1');
56 if (!isset($vars['redirect']) && $vars['action'] !== 'index' && !isset($vars['extension']) && is_array($GLOBALS['BE_USER']->uc['reports']['selection'])) {
57 $previousSelection = $GLOBALS['BE_USER']->uc['reports']['selection'];
58 if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
59 $this->redirect('detail', 'Report', null, array(
60 'extension' => $previousSelection['extension'],
61 'report' => $previousSelection['report'],
62 'redirect' => 1,
63 ));
64 } else {
65 $this->redirect('index');
66 }
67 }
68 }
69
70 /**
71 * Initialize the view
72 *
73 * @param ViewInterface $view The view
74 *
75 * @return void
76 */
77 protected function initializeView(ViewInterface $view)
78 {
79 /** @var BackendTemplateView $view */
80 parent::initializeView($view);
81 $view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation([]);
82 $this->generateMenu();
83 $this->generateButtons();
84 }
85
86 /**
87 * Overview
88 *
89 * @return void
90 */
91 public function indexAction()
92 {
93 $this->view->assign(
94 'reports', $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']
95 );
96 $this->saveState();
97 }
98
99 /**
100 * Display a single report
101 *
102 * @param string $extension Extension
103 * @param string $report Report
104 *
105 * @return void
106 */
107 public function detailAction($extension, $report)
108 {
109 $content = ($error = '');
110 $reportClass = null;
111 if (
112 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension])
113 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension])
114 && isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report])
115 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report])
116 && isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'])
117 ) {
118 $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'];
119 }
120
121 // If extension has been uninstalled/removed redirect to index
122 if ($reportClass === null) {
123 $this->redirect('index');
124 }
125
126 $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
127 if ($reportInstance instanceof ReportInterface) {
128 $content = $reportInstance->getReport();
129 $this->saveState($extension, $report);
130 } else {
131 $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
132 }
133 $this->view->assignMultiple(array(
134 'content' => $content,
135 'error' => $error,
136 'report' => $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
137 ));
138 }
139
140 /**
141 * Generates the menu
142 */
143 protected function generateMenu()
144 {
145 $lang = $this->getLanguageService();
146 $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
147 $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
148 $menu->setIdentifier('WebFuncJumpMenu');
149 $menuItem = $menu
150 ->makeMenuItem()
151 ->setHref(
152 $this->uriBuilder->reset()->uriFor('index', null, 'Report')
153 )
154 ->setTitle($lang->getLL('reports_overview'));
155 $menu->addMenuItem($menuItem);
156 $this->shortcutName = $lang->getLL('reports_overview');
157 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
158 foreach ($reports as $reportName => $report) {
159 $menuItem = $menu
160 ->makeMenuItem()
161 ->setHref($this->uriBuilder->reset()->uriFor('detail',
162 array('extension' => $extKey, 'report' => $reportName), 'Report'))
163 ->setTitle($this->getLanguageService()->sL($report['title']));
164 if ($this->arguments->hasArgument('extension') && $this->arguments->hasArgument('report')) {
165 if ($this->arguments->getArgument('extension')->getValue() === $extKey && $this->arguments->getArgument('report')->getValue() === $reportName) {
166 $menuItem->setActive(true);
167 $this->shortcutName = $menuItem->getTitle();
168 }
169 }
170 $menu->addMenuItem($menuItem);
171 }
172 }
173 $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
174 }
175
176 /**
177 * Gets all buttons for the docheader
178 */
179 protected function generateButtons()
180 {
181 $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
182 $moduleName = $this->request->getPluginName();
183 $getVars = $this->request->hasArgument('getVars') ? $this->request->getArgument('getVars') : [];
184 $setVars = $this->request->hasArgument('setVars') ? $this->request->getArgument('setVars') : [];
185 if (count($getVars) === 0) {
186 $modulePrefix = strtolower('tx_' . $this->request->getControllerExtensionName() . '_' . $moduleName);
187 $getVars = array('id', 'M', $modulePrefix);
188 }
189 $shortcutButton = $buttonBar->makeShortcutButton()
190 ->setModuleName($moduleName)
191 ->setGetVariables($getVars)
192 ->setDisplayName($this->shortcutName)
193 ->setSetVariables($setVars);
194 $buttonBar->addButton($shortcutButton);
195 }
196
197 /**
198 * Save the selected report
199 *
200 * @param string $extension Extension name
201 * @param string $report Report name
202 *
203 * @return void
204 */
205 protected function saveState($extension = '', $report = '')
206 {
207 $GLOBALS['BE_USER']->uc['reports']['selection'] = array(
208 'extension' => $extension,
209 'report' => $report,
210 );
211 $GLOBALS['BE_USER']->writeUC();
212 }
213
214 /**
215 * @return BackendUserAuthentication
216 */
217 protected function getBackendUser()
218 {
219 return $GLOBALS['BE_USER'];
220 }
221
222 /**
223 * @return LanguageService
224 */
225 protected function getLanguageService()
226 {
227 return $GLOBALS['LANG'];
228 }
229 }