‪TYPO3CMS  9.5
BackendLogController.php
Go to the documentation of this file.
1 <?php
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 
24 
30 {
34  private const ‪TIMEFRAME_THISWEEK = 0;
35 
39  private const ‪TIMEFRAME_LASTWEEK = 1;
40 
44  private const ‪TIMEFRAME_LASTSEVENDAYS = 2;
45 
49  private const ‪TIMEFRAME_THISMONTH = 10;
50 
54  private const ‪TIMEFRAME_LASTMONTH = 11;
55 
59  private const ‪TIMEFRAME_LAST31DAYS = 12;
60 
64  private const ‪TIMEFRAME_CUSTOM = 30;
65 
69  protected ‪$logEntryRepository;
70 
74  protected ‪$view;
75 
79  public function ‪injectLogEntryRepository(\‪TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository ‪$logEntryRepository)
80  {
81  $this->logEntryRepository = ‪$logEntryRepository;
82  }
83 
87  public function ‪initializeListAction()
88  {
89  if (!isset($this->settings['dateFormat'])) {
90  $this->settings['dateFormat'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? 'm-d-Y' : 'd-m-Y';
91  }
92  if (!isset($this->settings['timeFormat'])) {
93  $this->settings['timeFormat'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
94  }
95  $constraintConfiguration = $this->arguments->getArgument('constraint')->getPropertyMappingConfiguration();
96  $constraintConfiguration->allowAllProperties();
97  }
98 
106  public function ‪listAction(Constraint $constraint = null, int $pageId = null, string $layout = 'Default')
107  {
108  // Constraint object handling:
109  // If there is none from GET, try to get it from BE user data, else create new
110  if ($constraint === null) {
111  $constraint = $this->‪getConstraintFromBeUserData();
112  } else {
113  $this->‪persistConstraintInBeUserData($constraint);
114  }
115  $constraint->setPageId($pageId);
117  $this->‪setStartAndEndTimeFromTimeSelector($constraint);
118  $this->‪forceWorkspaceSelectionIfInWorkspace($constraint);
119  $logEntries = $this->logEntryRepository->findByConstraint($constraint);
120  $groupedLogEntries = $this->‪groupLogEntriesByPageAndDay($logEntries, $constraint->getGroupByPage());
121  $this->view->assignMultiple([
122  'pageId' => $pageId,
123  'layout' => $layout,
124  'groupedLogEntries' => $groupedLogEntries,
125  'constraint' => $constraint,
126  'userGroups' => $this->‪createUserAndGroupListForSelectOptions(),
127  'workspaces' => $this->‪createWorkspaceListForSelectOptions(),
128  'pageDepths' => $this->‪createPageDepthOptions(),
129  ]);
130  }
131 
138  public function ‪deleteMessageAction(int $errorUid)
139  {
141  $logEntry = $this->logEntryRepository->findByUid($errorUid);
142  if (!$logEntry) {
143  $this->‪addFlashMessage(‪LocalizationUtility::translate('actions.delete.noRowFound', 'belog'), '', ‪AbstractMessage::WARNING);
144  $this->‪redirect('list');
145  }
146  $numberOfDeletedRows = $this->logEntryRepository->deleteByMessageDetails($logEntry);
147  $this->‪addFlashMessage(sprintf(‪LocalizationUtility::translate('actions.delete.message', 'belog'), $numberOfDeletedRows));
148  $this->‪redirect('list');
149  }
150 
156  protected function ‪getConstraintFromBeUserData()
157  {
158  $serializedConstraint = ‪$GLOBALS['BE_USER']->getModuleData(static::class);
159  $constraint = null;
160  if (is_string($serializedConstraint) && !empty($serializedConstraint)) {
161  $constraint = @unserialize($serializedConstraint, ['allowed_classes' => [Constraint::class, \DateTime::class]]);
162  }
163  return $constraint ?: $this->objectManager->get(Constraint::class);
164  }
165 
171  protected function ‪persistConstraintInBeUserData(Constraint $constraint)
172  {
173  ‪$GLOBALS['BE_USER']->pushModuleData(static::class, serialize($constraint));
174  }
175 
181  protected function ‪resetConstraintsOnMemoryExhaustionError()
182  {
183  $reservedMemory = new \SplFixedArray(187500); // 3M
184  register_shutdown_function(function () use (&$reservedMemory) {
185  $reservedMemory = null; // free the reserved memory
186  $error = error_get_last();
187  if (strpos($error['message'], 'Allowed memory size of') !== false) {
188  $constraint = $this->objectManager->get(Constraint::class);
189  $this->‪persistConstraintInBeUserData($constraint);
190  }
191  });
192  }
193 
208  protected function ‪groupLogEntriesByPageAndDay(QueryResultInterface $logEntries, $groupByPage = false)
209  {
210  $targetStructure = [];
212  foreach ($logEntries as $entry) {
213  // Create page split list or flat list
214  if ($groupByPage) {
215  $pid = $entry->getEventPid();
216  } else {
217  $pid = -1;
218  }
219  // Create array if it is not defined yet
220  if (!is_array($targetStructure[$pid])) {
221  $targetStructure[$pid] = [];
222  }
223  // Get day timestamp of log entry and create sub array if needed
224  $timestampDay = strtotime(strftime('%d.%m.%Y', $entry->getTstamp()));
225  if (!is_array($targetStructure[$pid][$timestampDay])) {
226  $targetStructure[$pid][$timestampDay] = [];
227  }
228  // Add row
229  $targetStructure[$pid][$timestampDay][] = $entry;
230  }
231  ksort($targetStructure);
232  return $targetStructure;
233  }
234 
242  protected function ‪createUserAndGroupListForSelectOptions()
243  {
244  $userGroupArray = [];
245  // Two meta entries: 'all' and 'self'
246  $userGroupArray[0] = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('allUsers', 'Belog');
247  $userGroupArray[-1] = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('self', 'Belog');
248  // List of groups, key is gr-'uid'
250  foreach ($groups as $group) {
251  $userGroupArray['gr-' . $group['uid']] = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
252  }
253  // List of users, key is us-'uid'
255  foreach ($users as $user) {
256  $userGroupArray['us-' . $user['uid']] = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
257  }
258  return $userGroupArray;
259  }
260 
266  protected function ‪createWorkspaceListForSelectOptions()
267  {
268  if (!\‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
269  return [];
270  }
271  $workspaceArray = [];
272  // Two meta entries: 'all' and 'live'
273  $workspaceArray[-99] = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('any', 'Belog');
274  $workspaceArray[0] = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', 'Belog');
275  $workspaces = $this->objectManager->get(\‪TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class)->findAll();
277  foreach ($workspaces as $workspace) {
278  $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
279  }
280  return $workspaceArray;
281  }
282 
290  protected function ‪forceWorkspaceSelectionIfInWorkspace(Constraint $constraint)
291  {
292  if (‪$GLOBALS['BE_USER']->workspace !== 0) {
293  $constraint->setWorkspaceUid(‪$GLOBALS['BE_USER']->workspace);
294  $this->view->assign('showWorkspaceSelector', false);
295  } else {
296  $this->view->assign('showWorkspaceSelector', true);
297  }
298  }
299 
306  protected function ‪createPageDepthOptions()
307  {
308  $options = [
309  0 => ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0', 'lang'),
310  1 => ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1', 'lang'),
311  2 => ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2', 'lang'),
312  3 => ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3', 'lang'),
313  4 => ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4', 'lang'),
314  999 => ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi', 'lang')
315  ];
316  return $options;
317  }
318 
324  protected function ‪setStartAndEndTimeFromTimeSelector(Constraint $constraint)
325  {
326  $startTime = 0;
327  $endTime = ‪$GLOBALS['EXEC_TIME'];
328  // @TODO: Refactor this construct
329  switch ($constraint->getTimeFrame()) {
331  // This week
332  $week = (date('w') ?: 7) - 1;
333  $startTime = mktime(0, 0, 0) - $week * 3600 * 24;
334  break;
336  // Last week
337  $week = (date('w') ?: 7) - 1;
338  $startTime = mktime(0, 0, 0) - ($week + 7) * 3600 * 24;
339  $endTime = mktime(0, 0, 0) - $week * 3600 * 24;
340  break;
342  // Last 7 days
343  $startTime = mktime(0, 0, 0) - 7 * 3600 * 24;
344  break;
346  // This month
347  $startTime = mktime(0, 0, 0, date('m'), 1);
348  break;
350  // Last month
351  $startTime = mktime(0, 0, 0, date('m') - 1, 1);
352  $endTime = mktime(0, 0, 0, date('m'), 1);
353  break;
355  // Last 31 days
356  $startTime = mktime(0, 0, 0) - 31 * 3600 * 24;
357  break;
359  $startTime = $constraint->getManualDateStart() ? $constraint->getManualDateStart()->getTimestamp() : 0;
360  $endTime = $constraint->getManualDateStop() ? $constraint->getManualDateStop()->getTimestamp() : 0;
361  if ($endTime <= $startTime) {
362  $endTime = ‪$GLOBALS['EXEC_TIME'];
363  }
364  break;
365  default:
366  }
367  $constraint->setStartTimestamp($startTime);
368  $constraint->setEndTimestamp($endTime);
369  }
370 }
‪TYPO3\CMS\Belog\Controller\BackendLogController\$view
‪BackendTemplateView $view
Definition: BackendLogController.php:72
‪TYPO3\CMS\Belog\Controller\BackendLogController\initializeListAction
‪initializeListAction()
Definition: BackendLogController.php:85
‪TYPO3\CMS\Core\Messaging\AbstractMessage
Definition: AbstractMessage.php:24
‪TYPO3\CMS\Belog\Controller\BackendLogController\createUserAndGroupListForSelectOptions
‪array createUserAndGroupListForSelectOptions()
Definition: BackendLogController.php:240
‪TYPO3\CMS\Belog\Controller\BackendLogController\injectLogEntryRepository
‪injectLogEntryRepository(\TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository)
Definition: BackendLogController.php:77
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LASTSEVENDAYS
‪const TIMEFRAME_LASTSEVENDAYS
Definition: BackendLogController.php:44
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:29
‪TYPO3\CMS\Belog\Controller\BackendLogController\forceWorkspaceSelectionIfInWorkspace
‪forceWorkspaceSelectionIfInWorkspace(Constraint $constraint)
Definition: BackendLogController.php:288
‪TYPO3
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setEndTimestamp
‪setEndTimestamp($timestamp)
Definition: Constraint.php:238
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LASTMONTH
‪const TIMEFRAME_LASTMONTH
Definition: BackendLogController.php:54
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setWorkspaceUid
‪setWorkspaceUid($workspace)
Definition: Constraint.php:138
‪TYPO3\CMS\Belog\Controller\BackendLogController
Definition: BackendLogController.php:30
‪TYPO3\CMS\Belog\Domain\Model\Constraint\setStartTimestamp
‪setStartTimestamp($timestamp)
Definition: Constraint.php:218
‪TYPO3\CMS\Belog\Controller
Definition: BackendLogController.php:2
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_CUSTOM
‪const TIMEFRAME_CUSTOM
Definition: BackendLogController.php:64
‪TYPO3\CMS\Backend\Utility\BackendUtility\getUserNames
‪static array getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
Definition: BackendUtility.php:1003
‪TYPO3\CMS\Belog\Controller\BackendLogController\groupLogEntriesByPageAndDay
‪array groupLogEntriesByPageAndDay(QueryResultInterface $logEntries, $groupByPage=false)
Definition: BackendLogController.php:206
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Belog\Controller\BackendLogController\setStartAndEndTimeFromTimeSelector
‪setStartAndEndTimeFromTimeSelector(Constraint $constraint)
Definition: BackendLogController.php:322
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate($key, $extensionName=null, $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:63
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_THISWEEK
‪const TIMEFRAME_THISWEEK
Definition: BackendLogController.php:34
‪TYPO3\CMS\Belog\Controller\BackendLogController\deleteMessageAction
‪deleteMessageAction(int $errorUid)
Definition: BackendLogController.php:136
‪TYPO3\CMS\Backend\Utility\BackendUtility\getGroupNames
‪static array getGroupNames($fields='title, uid', $where='')
Definition: BackendUtility.php:1020
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: AbstractController.php:284
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getTimeFrame
‪int getTimeFrame()
Definition: Constraint.php:168
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getManualDateStart
‪DateTime getManualDateStart()
Definition: Constraint.php:308
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:21
‪TYPO3\CMS\Belog\Domain\Model\LogEntry
Definition: LogEntry.php:25
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_THISMONTH
‪const TIMEFRAME_THISMONTH
Definition: BackendLogController.php:49
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LASTWEEK
‪const TIMEFRAME_LASTWEEK
Definition: BackendLogController.php:39
‪TYPO3\CMS\Backend\View\BackendTemplateView
Definition: BackendTemplateView.php:27
‪TYPO3\CMS\Belog\Controller\BackendLogController\createPageDepthOptions
‪array createPageDepthOptions()
Definition: BackendLogController.php:304
‪TYPO3\CMS\Belog\Controller\BackendLogController\persistConstraintInBeUserData
‪persistConstraintInBeUserData(Constraint $constraint)
Definition: BackendLogController.php:169
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:31
‪TYPO3\CMS\Belog\Controller\BackendLogController\createWorkspaceListForSelectOptions
‪array createWorkspaceListForSelectOptions()
Definition: BackendLogController.php:264
‪TYPO3\CMS\Belog\Controller\BackendLogController\listAction
‪listAction(Constraint $constraint=null, int $pageId=null, string $layout='Default')
Definition: BackendLogController.php:104
‪TYPO3\CMS\Belog\Controller\BackendLogController\resetConstraintsOnMemoryExhaustionError
‪resetConstraintsOnMemoryExhaustionError()
Definition: BackendLogController.php:179
‪TYPO3\CMS\Belog\Domain\Model\Constraint
Definition: Constraint.php:22
‪TYPO3\CMS\Belog\Controller\BackendLogController\$logEntryRepository
‪TYPO3 CMS Belog Domain Repository LogEntryRepository $logEntryRepository
Definition: BackendLogController.php:68
‪TYPO3\CMS\Belog\Domain\Model\Constraint\getManualDateStop
‪DateTime getManualDateStop()
Definition: Constraint.php:328
‪TYPO3\CMS\Belog\Controller\BackendLogController\TIMEFRAME_LAST31DAYS
‪const TIMEFRAME_LAST31DAYS
Definition: BackendLogController.php:59
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)
Definition: AbstractController.php:154
‪TYPO3\CMS\Belog\Controller\BackendLogController\getConstraintFromBeUserData
‪Constraint getConstraintFromBeUserData()
Definition: BackendLogController.php:154