[BUGFIX] Remove duplicate docheader in Web->Info->Log
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / Controller / AbstractController.php
1 <?php
2 namespace TYPO3\CMS\Belog\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
17 use TYPO3\CMS\Core\Page\PageRenderer;
18 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
19 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
20 use TYPO3\CMS\Backend\View\BackendTemplateView;
21
22 /**
23 * Abstract class to show log entries from sys_log
24 */
25 abstract class AbstractController extends ActionController {
26
27 /**
28 * @var int
29 */
30 const TIMEFRAME_THISWEEK = 0;
31
32 /**
33 * @var int
34 */
35 const TIMEFRAME_LASTWEEK = 1;
36
37 /**
38 * @var int
39 */
40 const TIMEFRAME_LASTSEVENDAYS = 2;
41
42 /**
43 * @var int
44 */
45 const TIMEFRAME_THISMONTH = 10;
46
47 /**
48 * @var int
49 */
50 const TIMEFRAME_LASTMONTH = 11;
51
52 /**
53 * @var int
54 */
55 const TIMEFRAME_LAST31DAYS = 12;
56
57 /**
58 * @var int
59 */
60 const TIMEFRAME_CUSTOM = 30;
61
62 /**
63 * Whether plugin is running in page context (sub module of Web > Info)
64 *
65 * @var bool
66 */
67 protected $isInPageContext = FALSE;
68
69 /**
70 * Page ID in page context
71 *
72 * @var int
73 */
74 protected $pageId = 0;
75
76 /**
77 * @var \TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository
78 */
79 protected $logEntryRepository = NULL;
80
81 /**
82 * @var BackendTemplateView
83 */
84 protected $view;
85
86 /**
87 * @param \TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository
88 */
89 public function injectLogEntryRepository(\TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository) {
90 $this->logEntryRepository = $logEntryRepository;
91 }
92
93 /**
94 * Initialize the view
95 *
96 * @param ViewInterface $view The view
97 * @return void
98 */
99 protected function initializeView(ViewInterface $view) {
100 if ($view instanceof BackendTemplateView) {
101 parent::initializeView($view);
102 $view->getModuleTemplate()->getPageRenderer()->loadExtJS();
103 $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/DateTimePicker');
104 }
105 }
106
107 /**
108 * init all actions
109 * @return void
110 */
111 public function initializeAction() {
112 if ($this->isInPageContext === FALSE) {
113 $this->defaultViewObjectName = BackendTemplateView::class;
114 }
115 }
116
117 /**
118 * Initialize index action
119 *
120 * @return void
121 * @throws \RuntimeException
122 */
123 public function initializeIndexAction() {
124 // @TODO: Extbase backend modules rely on frontend TypoScript for view, persistence
125 // and settings. Thus, we need a TypoScript root template, that then loads the
126 // ext_typoscript_setup.txt file of this module. This is nasty, but can not be
127 // circumvented until there is a better solution in extbase.
128 // For now we throw an exception if no settings are detected.
129 if (empty($this->settings)) {
130 throw new \RuntimeException(
131 'No settings detected. This usually happens if there is no frontend TypoScript template with root flag set. Please create one.',
132 1333650506
133 );
134 }
135 if (!isset($this->settings['dateFormat'])) {
136 $this->settings['dateFormat'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? 'm-d-Y' : 'd-m-Y';
137 }
138 if (!isset($this->settings['timeFormat'])) {
139 $this->settings['timeFormat'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
140 }
141 }
142
143 /**
144 * Show general information and the installed modules
145 *
146 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
147 * @return void
148 */
149 public function indexAction(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint = NULL) {
150 // Constraint object handling:
151 // If there is none from GET, try to get it from BE user data, else create new
152 if ($constraint === NULL) {
153 $constraint = $this->getConstraintFromBeUserData();
154 if ($constraint === NULL) {
155 $constraint = $this->objectManager->get(\TYPO3\CMS\Belog\Domain\Model\Constraint::class);
156 }
157 } else {
158 $this->persistConstraintInBeUserData($constraint);
159 }
160 $constraint->setIsInPageContext($this->isInPageContext);
161 $constraint->setPageId($this->pageId);
162 $this->setStartAndEndTimeFromTimeSelector($constraint);
163 $this->forceWorkspaceSelectionIfInWorkspace($constraint);
164 $logEntries = $this->logEntryRepository->findByConstraint($constraint);
165 $groupedLogEntries = $this->groupLogEntriesByPageAndDay($logEntries, $constraint->getGroupByPage());
166 $this->view->assign('groupedLogEntries', $groupedLogEntries)->assign('constraint', $constraint)->assign('userGroups', $this->createUserAndGroupListForSelectOptions())->assign('workspaces', $this->createWorkspaceListForSelectOptions())->assign('pageDepths', $this->createPageDepthOptions());
167 }
168
169 /**
170 * Get module states (the constraint object) from user data
171 *
172 * @return \TYPO3\CMS\Belog\Domain\Model\Constraint|NULL
173 */
174 protected function getConstraintFromBeUserData() {
175 $serializedConstraint = $GLOBALS['BE_USER']->getModuleData(get_class($this));
176 if (!is_string($serializedConstraint) || empty($serializedConstraint)) {
177 return NULL;
178 }
179 return @unserialize($serializedConstraint);
180 }
181
182 /**
183 * Save current constraint object in be user settings (uC)
184 *
185 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
186 * @return void
187 */
188 protected function persistConstraintInBeUserData(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
189 $GLOBALS['BE_USER']->pushModuleData(get_class($this), serialize($constraint));
190 }
191
192 /**
193 * Create a sorted array for day and page view from
194 * the query result of the sys log repository.
195 *
196 * If group by page is FALSE, pid is always -1 (will render a flat list),
197 * otherwise the output is splitted by pages.
198 * '12345' is a sub array to split entries by day, number is first second of day
199 *
200 * [pid][dayTimestamp][items]
201 *
202 * @param \TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\TYPO3\CMS\Belog\Domain\Model\LogEntry> $logEntries
203 * @param bool $groupByPage Whether or not log entries should be grouped by page
204 * @return array
205 */
206 protected function groupLogEntriesByPageAndDay(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $logEntries, $groupByPage = FALSE) {
207 $targetStructure = array();
208 /** @var $entry \TYPO3\CMS\Belog\Domain\Model\LogEntry */
209 foreach ($logEntries as $entry) {
210 // Create page split list or flat list
211 if ($groupByPage) {
212 $pid = $entry->getEventPid();
213 } else {
214 $pid = -1;
215 }
216 // Create array if it is not defined yet
217 if (!is_array($targetStructure[$pid])) {
218 $targetStructure[$pid] = array();
219 }
220 // Get day timestamp of log entry and create sub array if needed
221 $timestampDay = strtotime(strftime('%d.%m.%Y', $entry->getTstamp()));
222 if (!is_array($targetStructure[$pid][$timestampDay])) {
223 $targetStructure[$pid][$timestampDay] = array();
224 }
225 // Add row
226 $targetStructure[$pid][$timestampDay][] = $entry;
227 }
228 ksort($targetStructure);
229 return $targetStructure;
230 }
231
232 /**
233 * Create options for the user / group drop down.
234 * This is not moved to a repository by intention to not mix up this 'meta' data
235 * with real repository work
236 *
237 * @return array Key is the option name, value its label
238 */
239 protected function createUserAndGroupListForSelectOptions() {
240 $userGroupArray = array();
241 // Two meta entries: 'all' and 'self'
242 $userGroupArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('allUsers', 'Belog');
243 $userGroupArray[-1] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('self', 'Belog');
244 // List of groups, key is gr-'uid'
245 $groups = \TYPO3\CMS\Backend\Utility\BackendUtility::getGroupNames();
246 foreach ($groups as $group) {
247 $userGroupArray['gr-' . $group['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
248 }
249 // List of users, key is us-'uid'
250 $users = \TYPO3\CMS\Backend\Utility\BackendUtility::getUserNames();
251 foreach ($users as $user) {
252 $userGroupArray['us-' . $user['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
253 }
254 return $userGroupArray;
255 }
256
257 /**
258 * Create options for the workspace selector
259 *
260 * @return array Key is uid of workspace, value its label
261 */
262 protected function createWorkspaceListForSelectOptions() {
263 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
264 return array();
265 }
266 $workspaceArray = array();
267 // Two meta entries: 'all' and 'live'
268 $workspaceArray[-99] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('any', 'Belog');
269 $workspaceArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', 'Belog');
270 $workspaces = $this->objectManager->get(\TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class)->findAll();
271 /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
272 foreach ($workspaces as $workspace) {
273 $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
274 }
275 return $workspaceArray;
276 }
277
278 /**
279 * If the user is in a workspace different than LIVE,
280 * we force to show only log entries from the selected workspace,
281 * and the workspace selector is not shown.
282 *
283 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
284 * @return void
285 */
286 protected function forceWorkspaceSelectionIfInWorkspace(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
287 if ($GLOBALS['BE_USER']->workspace !== 0) {
288 $constraint->setWorkspaceUid($GLOBALS['BE_USER']->workspace);
289 $this->view->assign('showWorkspaceSelector', FALSE);
290 } else {
291 $this->view->assign('showWorkspaceSelector', TRUE);
292 }
293 }
294
295 /**
296 * Create options for the 'depth of page levels' selector.
297 * This is shown if the module is displayed in page -> info
298 *
299 * @return array Key is depth identifier (1 = One level), value the localized select option label
300 */
301 protected function createPageDepthOptions() {
302 $options = array(
303 0 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_0', 'lang'),
304 1 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_1', 'lang'),
305 2 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_2', 'lang'),
306 3 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_3', 'lang'),
307 4 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_4', 'lang'),
308 999 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi', 'lang')
309 );
310 return $options;
311 }
312
313 /**
314 * Calculate the start- and end timestamp from the different time selector options
315 *
316 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
317 * @return void
318 */
319 protected function setStartAndEndTimeFromTimeSelector(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
320 $startTime = 0;
321 $endTime = $GLOBALS['EXEC_TIME'];
322 // @TODO: Refactor this construct
323 switch ($constraint->getTimeFrame()) {
324 case self::TIMEFRAME_THISWEEK:
325 // This week
326 $week = (date('w') ?: 7) - 1;
327 $startTime = mktime(0, 0, 0) - $week * 3600 * 24;
328 break;
329 case self::TIMEFRAME_LASTWEEK:
330 // Last week
331 $week = (date('w') ?: 7) - 1;
332 $startTime = mktime(0, 0, 0) - ($week + 7) * 3600 * 24;
333 $endTime = mktime(0, 0, 0) - $week * 3600 * 24;
334 break;
335 case self::TIMEFRAME_LASTSEVENDAYS:
336 // Last 7 days
337 $startTime = mktime(0, 0, 0) - 7 * 3600 * 24;
338 break;
339 case self::TIMEFRAME_THISMONTH:
340 // This month
341 $startTime = mktime(0, 0, 0, date('m'), 1);
342 break;
343 case self::TIMEFRAME_LASTMONTH:
344 // Last month
345 $startTime = mktime(0, 0, 0, date('m') - 1, 1);
346 $endTime = mktime(0, 0, 0, date('m'), 1);
347 break;
348 case self::TIMEFRAME_LAST31DAYS:
349 // Last 31 days
350 $startTime = mktime(0, 0, 0) - 31 * 3600 * 24;
351 break;
352 case self::TIMEFRAME_CUSTOM:
353 $startTime = $constraint->getStartTimestamp();
354 if ($constraint->getEndTimestamp() > $constraint->getStartTimestamp()) {
355 $endTime = $constraint->getEndTimestamp();
356 } else {
357 $endTime = $GLOBALS['EXEC_TIME'];
358 }
359 break;
360 default:
361 }
362 $constraint->setStartTimestamp($startTime);
363 $constraint->setEndTimestamp($endTime);
364 }
365 }