[BUGFIX] Make daterange filtering in EXT:belog work again
[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\Backend\View\BackendTemplateView;
18 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
19 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
20
21 /**
22 * Abstract class to show log entries from sys_log
23 */
24 abstract class AbstractController extends ActionController
25 {
26 /**
27 * @var int
28 */
29 const TIMEFRAME_THISWEEK = 0;
30
31 /**
32 * @var int
33 */
34 const TIMEFRAME_LASTWEEK = 1;
35
36 /**
37 * @var int
38 */
39 const TIMEFRAME_LASTSEVENDAYS = 2;
40
41 /**
42 * @var int
43 */
44 const TIMEFRAME_THISMONTH = 10;
45
46 /**
47 * @var int
48 */
49 const TIMEFRAME_LASTMONTH = 11;
50
51 /**
52 * @var int
53 */
54 const TIMEFRAME_LAST31DAYS = 12;
55
56 /**
57 * @var int
58 */
59 const TIMEFRAME_CUSTOM = 30;
60
61 /**
62 * Whether plugin is running in page context (sub module of Web > Info)
63 *
64 * @var bool
65 */
66 protected $isInPageContext = false;
67
68 /**
69 * Page ID in page context
70 *
71 * @var int
72 */
73 protected $pageId = 0;
74
75 /**
76 * @var \TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository
77 */
78 protected $logEntryRepository = null;
79
80 /**
81 * @var BackendTemplateView
82 */
83 protected $view;
84
85 /**
86 * @param \TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository
87 */
88 public function injectLogEntryRepository(\TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository)
89 {
90 $this->logEntryRepository = $logEntryRepository;
91 }
92
93 /**
94 * Initialize the view
95 *
96 * @param ViewInterface $view The view
97 */
98 protected function initializeView(ViewInterface $view)
99 {
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 */
110 public function initializeAction()
111 {
112 if ($this->isInPageContext === false) {
113 $this->defaultViewObjectName = BackendTemplateView::class;
114 }
115 }
116
117 /**
118 * Initialize index action
119 *
120 * @throws \RuntimeException
121 */
122 public function initializeIndexAction()
123 {
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 */
148 public function indexAction(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint = null)
149 {
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 {
176 $serializedConstraint = $GLOBALS['BE_USER']->getModuleData(get_class($this));
177 if (!is_string($serializedConstraint) || empty($serializedConstraint)) {
178 return null;
179 }
180 return @unserialize($serializedConstraint);
181 }
182
183 /**
184 * Save current constraint object in be user settings (uC)
185 *
186 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
187 */
188 protected function persistConstraintInBeUserData(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
189 {
190 $GLOBALS['BE_USER']->pushModuleData(get_class($this), serialize($constraint));
191 }
192
193 /**
194 * Create a sorted array for day and page view from
195 * the query result of the sys log repository.
196 *
197 * If group by page is FALSE, pid is always -1 (will render a flat list),
198 * otherwise the output is splitted by pages.
199 * '12345' is a sub array to split entries by day, number is first second of day
200 *
201 * [pid][dayTimestamp][items]
202 *
203 * @param \TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\TYPO3\CMS\Belog\Domain\Model\LogEntry> $logEntries
204 * @param bool $groupByPage Whether or not log entries should be grouped by page
205 * @return array
206 */
207 protected function groupLogEntriesByPageAndDay(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $logEntries, $groupByPage = false)
208 {
209 $targetStructure = [];
210 /** @var $entry \TYPO3\CMS\Belog\Domain\Model\LogEntry */
211 foreach ($logEntries as $entry) {
212 // Create page split list or flat list
213 if ($groupByPage) {
214 $pid = $entry->getEventPid();
215 } else {
216 $pid = -1;
217 }
218 // Create array if it is not defined yet
219 if (!is_array($targetStructure[$pid])) {
220 $targetStructure[$pid] = [];
221 }
222 // Get day timestamp of log entry and create sub array if needed
223 $timestampDay = strtotime(strftime('%d.%m.%Y', $entry->getTstamp()));
224 if (!is_array($targetStructure[$pid][$timestampDay])) {
225 $targetStructure[$pid][$timestampDay] = [];
226 }
227 // Add row
228 $targetStructure[$pid][$timestampDay][] = $entry;
229 }
230 ksort($targetStructure);
231 return $targetStructure;
232 }
233
234 /**
235 * Create options for the user / group drop down.
236 * This is not moved to a repository by intention to not mix up this 'meta' data
237 * with real repository work
238 *
239 * @return array Key is the option name, value its label
240 */
241 protected function createUserAndGroupListForSelectOptions()
242 {
243 $userGroupArray = [];
244 // Two meta entries: 'all' and 'self'
245 $userGroupArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('allUsers', 'Belog');
246 $userGroupArray[-1] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('self', 'Belog');
247 // List of groups, key is gr-'uid'
248 $groups = \TYPO3\CMS\Backend\Utility\BackendUtility::getGroupNames();
249 foreach ($groups as $group) {
250 $userGroupArray['gr-' . $group['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
251 }
252 // List of users, key is us-'uid'
253 $users = \TYPO3\CMS\Backend\Utility\BackendUtility::getUserNames();
254 foreach ($users as $user) {
255 $userGroupArray['us-' . $user['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
256 }
257 return $userGroupArray;
258 }
259
260 /**
261 * Create options for the workspace selector
262 *
263 * @return array Key is uid of workspace, value its label
264 */
265 protected function createWorkspaceListForSelectOptions()
266 {
267 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
268 return [];
269 }
270 $workspaceArray = [];
271 // Two meta entries: 'all' and 'live'
272 $workspaceArray[-99] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('any', 'Belog');
273 $workspaceArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', 'Belog');
274 $workspaces = $this->objectManager->get(\TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class)->findAll();
275 /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
276 foreach ($workspaces as $workspace) {
277 $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
278 }
279 return $workspaceArray;
280 }
281
282 /**
283 * If the user is in a workspace different than LIVE,
284 * we force to show only log entries from the selected workspace,
285 * and the workspace selector is not shown.
286 *
287 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
288 */
289 protected function forceWorkspaceSelectionIfInWorkspace(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
290 {
291 if ($GLOBALS['BE_USER']->workspace !== 0) {
292 $constraint->setWorkspaceUid($GLOBALS['BE_USER']->workspace);
293 $this->view->assign('showWorkspaceSelector', false);
294 } else {
295 $this->view->assign('showWorkspaceSelector', true);
296 }
297 }
298
299 /**
300 * Create options for the 'depth of page levels' selector.
301 * This is shown if the module is displayed in page -> info
302 *
303 * @return array Key is depth identifier (1 = One level), value the localized select option label
304 */
305 protected function createPageDepthOptions()
306 {
307 $options = [
308 0 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0', 'lang'),
309 1 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1', 'lang'),
310 2 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2', 'lang'),
311 3 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3', 'lang'),
312 4 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4', 'lang'),
313 999 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi', 'lang')
314 ];
315 return $options;
316 }
317
318 /**
319 * Calculate the start- and end timestamp from the different time selector options
320 *
321 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
322 */
323 protected function setStartAndEndTimeFromTimeSelector(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
324 {
325 $startTime = 0;
326 $endTime = $GLOBALS['EXEC_TIME'];
327 // @TODO: Refactor this construct
328 switch ($constraint->getTimeFrame()) {
329 case self::TIMEFRAME_THISWEEK:
330 // This week
331 $week = (date('w') ?: 7) - 1;
332 $startTime = mktime(0, 0, 0) - $week * 3600 * 24;
333 break;
334 case self::TIMEFRAME_LASTWEEK:
335 // Last week
336 $week = (date('w') ?: 7) - 1;
337 $startTime = mktime(0, 0, 0) - ($week + 7) * 3600 * 24;
338 $endTime = mktime(0, 0, 0) - $week * 3600 * 24;
339 break;
340 case self::TIMEFRAME_LASTSEVENDAYS:
341 // Last 7 days
342 $startTime = mktime(0, 0, 0) - 7 * 3600 * 24;
343 break;
344 case self::TIMEFRAME_THISMONTH:
345 // This month
346 $startTime = mktime(0, 0, 0, date('m'), 1);
347 break;
348 case self::TIMEFRAME_LASTMONTH:
349 // Last month
350 $startTime = mktime(0, 0, 0, date('m') - 1, 1);
351 $endTime = mktime(0, 0, 0, date('m'), 1);
352 break;
353 case self::TIMEFRAME_LAST31DAYS:
354 // Last 31 days
355 $startTime = mktime(0, 0, 0) - 31 * 3600 * 24;
356 break;
357 case self::TIMEFRAME_CUSTOM:
358 $startDate = $constraint->getManualDateStart();
359 $endDate = $constraint->getManualDateStop();
360 $endTime = $GLOBALS['EXEC_TIME'];
361 if (!$startDate) {
362 $startDate = \DateTime::createFromFormat(
363 'U',
364 0
365 );
366 $constraint->setManualDateStart(
367 $startDate
368 );
369 }
370
371 if (!$endDate) {
372 $endDate = \DateTime::createFromFormat(
373 'U',
374 $endTime
375 )->setTimezone(new \DateTimeZone(date_default_timezone_get()));
376 $constraint->setManualDateStop(
377 $endDate
378 );
379 }
380 $startTime = $startDate->getTimestamp();
381 if ($endDate->getTimestamp() > $startDate->getTimestamp()) {
382 $endTime = $endDate->getTimestamp();
383 }
384
385 break;
386 default:
387 }
388 $constraint->setStartTimestamp($startTime);
389 $constraint->setEndTimestamp($endTime);
390 }
391 }