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