TYPO3 CMS  TYPO3_7-6
AbstractController.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 
20 
24 abstract class AbstractController extends ActionController
25 {
29  const TIMEFRAME_THISWEEK = 0;
30 
34  const TIMEFRAME_LASTWEEK = 1;
35 
40 
44  const TIMEFRAME_THISMONTH = 10;
45 
49  const TIMEFRAME_LASTMONTH = 11;
50 
55 
59  const TIMEFRAME_CUSTOM = 30;
60 
66  protected $isInPageContext = false;
67 
73  protected $pageId = 0;
74 
78  protected $logEntryRepository = null;
79 
83  protected $view;
84 
88  public function injectLogEntryRepository(\TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository)
89  {
90  $this->logEntryRepository = $logEntryRepository;
91  }
92 
99  protected function initializeView(ViewInterface $view)
100  {
101  if ($view instanceof BackendTemplateView) {
102  parent::initializeView($view);
103  $view->getModuleTemplate()->getPageRenderer()->loadExtJS();
104  $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/DateTimePicker');
105  }
106  }
107 
112  public function initializeAction()
113  {
114  if ($this->isInPageContext === false) {
115  $this->defaultViewObjectName = BackendTemplateView::class;
116  }
117  }
118 
125  public function initializeIndexAction()
126  {
127  // @TODO: Extbase backend modules rely on frontend TypoScript for view, persistence
128  // and settings. Thus, we need a TypoScript root template, that then loads the
129  // ext_typoscript_setup.txt file of this module. This is nasty, but can not be
130  // circumvented until there is a better solution in extbase.
131  // For now we throw an exception if no settings are detected.
132  if (empty($this->settings)) {
133  throw new \RuntimeException(
134  'No settings detected. This usually happens if there is no frontend TypoScript template with root flag set. Please create one.',
135  1333650506
136  );
137  }
138  if (!isset($this->settings['dateFormat'])) {
139  $this->settings['dateFormat'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? 'm-d-Y' : 'd-m-Y';
140  }
141  if (!isset($this->settings['timeFormat'])) {
142  $this->settings['timeFormat'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
143  }
144  }
145 
152  public function indexAction(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint = null)
153  {
154  // Constraint object handling:
155  // If there is none from GET, try to get it from BE user data, else create new
156  if ($constraint === null) {
157  $constraint = $this->getConstraintFromBeUserData();
158  if ($constraint === null) {
159  $constraint = $this->objectManager->get(\TYPO3\CMS\Belog\Domain\Model\Constraint::class);
160  }
161  } else {
162  $this->persistConstraintInBeUserData($constraint);
163  }
164  $constraint->setIsInPageContext($this->isInPageContext);
165  $constraint->setPageId($this->pageId);
166  $this->setStartAndEndTimeFromTimeSelector($constraint);
167  $this->forceWorkspaceSelectionIfInWorkspace($constraint);
168  $logEntries = $this->logEntryRepository->findByConstraint($constraint);
169  $groupedLogEntries = $this->groupLogEntriesByPageAndDay($logEntries, $constraint->getGroupByPage());
170  $this->view->assign('groupedLogEntries', $groupedLogEntries)->assign('constraint', $constraint)->assign('userGroups', $this->createUserAndGroupListForSelectOptions())->assign('workspaces', $this->createWorkspaceListForSelectOptions())->assign('pageDepths', $this->createPageDepthOptions());
171  }
172 
178  protected function getConstraintFromBeUserData()
179  {
180  $serializedConstraint = $GLOBALS['BE_USER']->getModuleData(get_class($this));
181  if (!is_string($serializedConstraint) || empty($serializedConstraint)) {
182  return null;
183  }
184  return @unserialize($serializedConstraint);
185  }
186 
193  protected function persistConstraintInBeUserData(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
194  {
195  $GLOBALS['BE_USER']->pushModuleData(get_class($this), serialize($constraint));
196  }
197 
212  protected function groupLogEntriesByPageAndDay(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $logEntries, $groupByPage = false)
213  {
214  $targetStructure = [];
216  foreach ($logEntries as $entry) {
217  // Create page split list or flat list
218  if ($groupByPage) {
219  $pid = $entry->getEventPid();
220  } else {
221  $pid = -1;
222  }
223  // Create array if it is not defined yet
224  if (!is_array($targetStructure[$pid])) {
225  $targetStructure[$pid] = [];
226  }
227  // Get day timestamp of log entry and create sub array if needed
228  $timestampDay = strtotime(strftime('%d.%m.%Y', $entry->getTstamp()));
229  if (!is_array($targetStructure[$pid][$timestampDay])) {
230  $targetStructure[$pid][$timestampDay] = [];
231  }
232  // Add row
233  $targetStructure[$pid][$timestampDay][] = $entry;
234  }
235  ksort($targetStructure);
236  return $targetStructure;
237  }
238 
247  {
248  $userGroupArray = [];
249  // Two meta entries: 'all' and 'self'
250  $userGroupArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('allUsers', 'Belog');
251  $userGroupArray[-1] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('self', 'Belog');
252  // List of groups, key is gr-'uid'
254  foreach ($groups as $group) {
255  $userGroupArray['gr-' . $group['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
256  }
257  // List of users, key is us-'uid'
259  foreach ($users as $user) {
260  $userGroupArray['us-' . $user['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
261  }
262  return $userGroupArray;
263  }
264 
270  protected function createWorkspaceListForSelectOptions()
271  {
272  if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
273  return [];
274  }
275  $workspaceArray = [];
276  // Two meta entries: 'all' and 'live'
277  $workspaceArray[-99] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('any', 'Belog');
278  $workspaceArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', 'Belog');
279  $workspaces = $this->objectManager->get(\TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class)->findAll();
281  foreach ($workspaces as $workspace) {
282  $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
283  }
284  return $workspaceArray;
285  }
286 
295  protected function forceWorkspaceSelectionIfInWorkspace(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
296  {
297  if ($GLOBALS['BE_USER']->workspace !== 0) {
298  $constraint->setWorkspaceUid($GLOBALS['BE_USER']->workspace);
299  $this->view->assign('showWorkspaceSelector', false);
300  } else {
301  $this->view->assign('showWorkspaceSelector', true);
302  }
303  }
304 
311  protected function createPageDepthOptions()
312  {
313  $options = [
314  0 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_0', 'lang'),
315  1 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_1', 'lang'),
316  2 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_2', 'lang'),
317  3 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_3', 'lang'),
318  4 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_4', 'lang'),
319  999 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi', 'lang')
320  ];
321  return $options;
322  }
323 
330  protected function setStartAndEndTimeFromTimeSelector(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
331  {
332  $startTime = 0;
333  $endTime = $GLOBALS['EXEC_TIME'];
334  // @TODO: Refactor this construct
335  switch ($constraint->getTimeFrame()) {
336  case self::TIMEFRAME_THISWEEK:
337  // This week
338  $week = (date('w') ?: 7) - 1;
339  $startTime = mktime(0, 0, 0) - $week * 3600 * 24;
340  break;
341  case self::TIMEFRAME_LASTWEEK:
342  // Last week
343  $week = (date('w') ?: 7) - 1;
344  $startTime = mktime(0, 0, 0) - ($week + 7) * 3600 * 24;
345  $endTime = mktime(0, 0, 0) - $week * 3600 * 24;
346  break;
347  case self::TIMEFRAME_LASTSEVENDAYS:
348  // Last 7 days
349  $startTime = mktime(0, 0, 0) - 7 * 3600 * 24;
350  break;
351  case self::TIMEFRAME_THISMONTH:
352  // This month
353  $startTime = mktime(0, 0, 0, date('m'), 1);
354  break;
355  case self::TIMEFRAME_LASTMONTH:
356  // Last month
357  $startTime = mktime(0, 0, 0, date('m') - 1, 1);
358  $endTime = mktime(0, 0, 0, date('m'), 1);
359  break;
360  case self::TIMEFRAME_LAST31DAYS:
361  // Last 31 days
362  $startTime = mktime(0, 0, 0) - 31 * 3600 * 24;
363  break;
364  case self::TIMEFRAME_CUSTOM:
365  $startTime = $constraint->getStartTimestamp();
366  if ($constraint->getEndTimestamp() > $constraint->getStartTimestamp()) {
367  $endTime = $constraint->getEndTimestamp();
368  } else {
369  $endTime = $GLOBALS['EXEC_TIME'];
370  }
371  break;
372  default:
373  }
374  $constraint->setStartTimestamp($startTime);
375  $constraint->setEndTimestamp($endTime);
376  }
377 }
static translate($key, $extensionName, $arguments=null)
injectLogEntryRepository(\TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository)
static getGroupNames($fields='title, uid', $where='')
forceWorkspaceSelectionIfInWorkspace(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
static getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
persistConstraintInBeUserData(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
setStartAndEndTimeFromTimeSelector(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
indexAction(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint=null)