[CLEANUP] Code cleanup in ext:reports
[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('reports', $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']);
94 $this->saveState();
95 }
96
97 /**
98 * Display a single report
99 *
100 * @param string $extension Extension
101 * @param string $report Report
102 *
103 * @return void
104 */
105 public function detailAction($extension, $report)
106 {
107 $content = ($error = '');
108 $reportClass = null;
109 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension])
110 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension])
111 && isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report])
112 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report])
113 && isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'])
114 ) {
115 $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'];
116 }
117
118 // If extension has been uninstalled/removed redirect to index
119 if ($reportClass === null) {
120 $this->redirect('index');
121 }
122
123 $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
124 if ($reportInstance instanceof ReportInterface) {
125 $content = $reportInstance->getReport();
126 $this->saveState($extension, $report);
127 } else {
128 $error = $reportClass . ' does not implement the Report Interface which is necessary to be displayed here.';
129 }
130 $this->view->assignMultiple(array(
131 'content' => $content,
132 'error' => $error,
133 'report' => $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report],
134 ));
135 }
136
137 /**
138 * Generates the menu
139 *
140 * @return void
141 */
142 protected function generateMenu()
143 {
144 $lang = $this->getLanguageService();
145 $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
146 $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
147 $menu->setIdentifier('WebFuncJumpMenu');
148 $menuItem = $menu
149 ->makeMenuItem()
150 ->setHref(
151 $this->uriBuilder->reset()->uriFor('index', null, 'Report')
152 )
153 ->setTitle($lang->getLL('reports_overview'));
154 $menu->addMenuItem($menuItem);
155 $this->shortcutName = $lang->getLL('reports_overview');
156 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
157 foreach ($reports as $reportName => $report) {
158 $menuItem = $menu
159 ->makeMenuItem()
160 ->setHref($this->uriBuilder->reset()->uriFor('detail', array('extension' => $extKey, 'report' => $reportName), 'Report'))
161 ->setTitle($this->getLanguageService()->sL($report['title']));
162 if ($this->arguments->hasArgument('extension') && $this->arguments->hasArgument('report')) {
163 if ($this->arguments->getArgument('extension')->getValue() === $extKey && $this->arguments->getArgument('report')->getValue() === $reportName) {
164 $menuItem->setActive(true);
165 $this->shortcutName = $menuItem->getTitle();
166 }
167 }
168 $menu->addMenuItem($menuItem);
169 }
170 }
171 $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
172 }
173
174 /**
175 * Gets all buttons for the docheader
176 *
177 * @return void
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 $this->getBackendUser()->uc['reports']['selection'] = array(
208 'extension' => $extension,
209 'report' => $report,
210 );
211 $this->getBackendUser()->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 }