TYPO3 CMS  TYPO3_6-2
AbstractController.php
Go to the documentation of this file.
1 <?php
3 
23 
27  const TIMEFRAME_THISWEEK = 0;
28 
32  const TIMEFRAME_LASTWEEK = 1;
33 
38 
42  const TIMEFRAME_THISMONTH = 10;
43 
47  const TIMEFRAME_LASTMONTH = 11;
48 
53 
57  const TIMEFRAME_CUSTOM = 30;
58 
64  protected $isInPageContext = FALSE;
65 
71  protected $pageId = 0;
72 
77  protected $logEntryRepository = NULL;
78 
85  public function initializeIndexAction() {
86  // @TODO: Extbase backend modules rely on frontend TypoScript for view, persistence
87  // and settings. Thus, we need a TypoScript root template, that then loads the
88  // ext_typoscript_setup.txt file of this module. This is nasty, but can not be
89  // circumvented until there is a better solution in extbase.
90  // For now we throw an exception if no settings are detected.
91  if (empty($this->settings)) {
92  throw new \RuntimeException(
93  'No settings detected. This usually happens if there is no frontend TypoScript template with root flag set. Please create one.',
94  1333650506
95  );
96  }
97  if (!isset($this->settings['dateFormat'])) {
98  $this->settings['dateFormat'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'];
99  }
100  if (!isset($this->settings['timeFormat'])) {
101  $this->settings['timeFormat'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
102  }
103  // @TODO: The dateTime property mapper throws exceptions that cannot be caught
104  // if the property is not given in the expected format. This is documented with
105  // issue http://forge.typo3.org/issues/33861. Depending on the outcome of the
106  // ticket, the code below might have to be adapted again.
107  // @TODO: There is a second solution to hint the property mapper with fluid on the expected
108  // format: <f:form.hidden property="manualDateStart.dateFormat" value="..." />. This
109  // could make the method below obsolete.
110  $this->configurePropertyMapperForDateTimeFormat($this->arguments['constraint']->getPropertyMappingConfiguration()->forProperty('manualDateStart'));
111  $this->configurePropertyMapperForDateTimeFormat($this->arguments['constraint']->getPropertyMappingConfiguration()->forProperty('manualDateStop'));
112  }
113 
120  public function indexAction(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint = NULL) {
121  // Constraint object handling:
122  // If there is none from GET, try to get it from BE user data, else create new
123  if ($constraint === NULL) {
124  $constraint = $this->getConstraintFromBeUserData();
125  if ($constraint === NULL) {
126  $constraint = $this->objectManager->get('TYPO3\\CMS\\Belog\\Domain\\Model\\Constraint');
127  }
128  } else {
129  $this->persistConstraintInBeUserData($constraint);
130  }
131  $constraint->setIsInPageContext($this->isInPageContext);
132  $constraint->setPageId($this->pageId);
133  $this->setStartAndEndTimeFromTimeSelector($constraint);
134  $this->forceWorkspaceSelectionIfInWorkspace($constraint);
135  $logEntries = $this->logEntryRepository->findByConstraint($constraint);
136  $groupedLogEntries = $this->groupLogEntriesByPageAndDay($logEntries, $constraint->getGroupByPage());
137  $this->view->assign('groupedLogEntries', $groupedLogEntries)->assign('constraint', $constraint)->assign('userGroups', $this->createUserAndGroupListForSelectOptions())->assign('workspaces', $this->createWorkspaceListForSelectOptions())->assign('pageDepths', $this->createPageDepthOptions());
138  }
139 
145  protected function getConstraintFromBeUserData() {
146  $serializedConstraint = $GLOBALS['BE_USER']->getModuleData(get_class($this));
147  if (!is_string($serializedConstraint) || empty($serializedConstraint)) {
148  return NULL;
149  }
150  return @unserialize($serializedConstraint);
151  }
152 
159  protected function persistConstraintInBeUserData(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
160  $GLOBALS['BE_USER']->pushModuleData(get_class($this), serialize($constraint));
161  }
162 
177  protected function groupLogEntriesByPageAndDay(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $logEntries, $groupByPage = FALSE) {
178  $targetStructure = array();
180  foreach ($logEntries as $entry) {
181  // Create page split list or flat list
182  if ($groupByPage) {
183  $pid = $entry->getEventPid();
184  } else {
185  $pid = -1;
186  }
187  // Create array if it is not defined yet
188  if (!is_array($targetStructure[$pid])) {
189  $targetStructure[$pid] = array();
190  }
191  // Get day timestamp of log entry and create sub array if needed
192  $timestampDay = strtotime(strftime('%d.%m.%Y', $entry->getTstamp()));
193  if (!is_array($targetStructure[$pid][$timestampDay])) {
194  $targetStructure[$pid][$timestampDay] = array();
195  }
196  // Add row
197  $targetStructure[$pid][$timestampDay][] = $entry;
198  }
199  ksort($targetStructure);
200  return $targetStructure;
201  }
202 
209  protected function configurePropertyMapperForDateTimeFormat(\TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMapperDate) {
210  $propertyMapperDate->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, $this->settings['dateFormat'] . ' ' . $this->settings['timeFormat']);
211  }
212 
221  $userGroupArray = array();
222  // Two meta entries: 'all' and 'self'
223  $userGroupArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('allUsers', 'Belog');
224  $userGroupArray[-1] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('self', 'Belog');
225  // List of groups, key is gr-'uid'
227  foreach ($groups as $group) {
228  $userGroupArray['gr-' . $group['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('group', 'Belog') . ' ' . $group['title'];
229  }
230  // List of users, key is us-'uid'
232  foreach ($users as $user) {
233  $userGroupArray['us-' . $user['uid']] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('user', 'Belog') . ' ' . $user['username'];
234  }
235  return $userGroupArray;
236  }
237 
243  protected function createWorkspaceListForSelectOptions() {
244  if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
245  return array();
246  }
247  $workspaceArray = array();
248  // Two meta entries: 'all' and 'live'
249  $workspaceArray[-99] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('any', 'Belog');
250  $workspaceArray[0] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', 'Belog');
251  $workspaces = $this->objectManager->get('TYPO3\\CMS\\Belog\\Domain\\Repository\\WorkspaceRepository')->findAll();
253  foreach ($workspaces as $workspace) {
254  $workspaceArray[$workspace->getUid()] = $workspace->getUid() . ': ' . $workspace->getTitle();
255  }
256  return $workspaceArray;
257  }
258 
267  protected function forceWorkspaceSelectionIfInWorkspace(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
268  if ($GLOBALS['BE_USER']->workspace !== 0) {
269  $constraint->setWorkspaceUid($GLOBALS['BE_USER']->workspace);
270  $this->view->assign('showWorkspaceSelector', FALSE);
271  } else {
272  $this->view->assign('showWorkspaceSelector', TRUE);
273  }
274  }
275 
282  protected function createPageDepthOptions() {
283  $options = array(
284  0 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_mod_web_info.xlf:depth_0', 'lang'),
285  1 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_mod_web_info.xlf:depth_1', 'lang'),
286  2 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_mod_web_info.xlf:depth_2', 'lang'),
287  3 => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:lang/locallang_mod_web_info.xlf:depth_3', 'lang')
288  );
289  return $options;
290  }
291 
298  protected function setStartAndEndTimeFromTimeSelector(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
299  $startTime = 0;
300  $endTime = $GLOBALS['EXEC_TIME'];
301  // @TODO: Refactor this construct
302  switch ($constraint->getTimeFrame()) {
303  case self::TIMEFRAME_THISWEEK:
304  // This week
305  $week = (date('w') ?: 7) - 1;
306  $startTime = mktime(0, 0, 0) - $week * 3600 * 24;
307  break;
308  case self::TIMEFRAME_LASTWEEK:
309  // Last week
310  $week = (date('w') ?: 7) - 1;
311  $startTime = mktime(0, 0, 0) - ($week + 7) * 3600 * 24;
312  $endTime = mktime(0, 0, 0) - $week * 3600 * 24;
313  break;
314  case self::TIMEFRAME_LASTSEVENDAYS:
315  // Last 7 days
316  $startTime = mktime(0, 0, 0) - 7 * 3600 * 24;
317  break;
318  case self::TIMEFRAME_THISMONTH:
319  // This month
320  $startTime = mktime(0, 0, 0, date('m'), 1);
321  break;
322  case self::TIMEFRAME_LASTMONTH:
323  // Last month
324  $startTime = mktime(0, 0, 0, date('m') - 1, 1);
325  $endTime = mktime(0, 0, 0, date('m'), 1);
326  break;
327  case self::TIMEFRAME_LAST31DAYS:
328  // Last 31 days
329  $startTime = mktime(0, 0, 0) - 31 * 3600 * 24;
330  break;
331  case self::TIMEFRAME_CUSTOM:
332  if ($constraint->getManualDateStart() instanceof \DateTime) {
333  $startTime = $constraint->getManualDateStart()->format('U');
334  if ($constraint->getManualDateStop() instanceof \DateTime) {
335  $manualEndTime = $constraint->getManualDateStop()->format('U');
336  if ($manualEndTime > $startTime) {
337  $endTime = $manualEndTime;
338  }
339  } else {
340  $endTime = $GLOBALS['EXEC_TIME'];
341  }
342  }
343  break;
344  default:
345 
346  }
347  $constraint->setStartTimestamp($startTime);
348  $constraint->setEndTimestamp($endTime);
349  }
350 
351 }
configurePropertyMapperForDateTimeFormat(\TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMapperDate)
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)
indexAction(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint=NULL)
static translate($key, $extensionName, $arguments=NULL)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]