TYPO3 CMS  TYPO3_8-7
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 
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 
110  public function initializeAction()
111  {
112  if ($this->isInPageContext === false) {
113  $this->defaultViewObjectName = BackendTemplateView::class;
114  }
115  }
116 
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 
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 
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 
188  protected function persistConstraintInBeUserData(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint)
189  {
190  $GLOBALS['BE_USER']->pushModuleData(get_class($this), serialize($constraint));
191  }
192 
207  protected function groupLogEntriesByPageAndDay(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $logEntries, $groupByPage = false)
208  {
209  $targetStructure = [];
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 
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'
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'
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 
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();
276  foreach ($workspaces as $workspace) {
277  $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
278  }
279  return $workspaceArray;
280  }
281 
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 
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 
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 }
injectLogEntryRepository(\TYPO3\CMS\Belog\Domain\Repository\LogEntryRepository $logEntryRepository)
static translate($key, $extensionName=null, $arguments=null)
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)