TYPO3 CMS  TYPO3_7-6
GridDataService.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 
21 
26 {
27  const SIGNAL_GenerateDataArray_BeforeCaching = 'generateDataArray.beforeCaching';
28  const SIGNAL_GenerateDataArray_PostProcesss = 'generateDataArray.postProcess';
29  const SIGNAL_GetDataArray_PostProcesss = 'getDataArray.postProcess';
30  const SIGNAL_SortDataArray_PostProcesss = 'sortDataArray.postProcess';
31 
32  const GridColumn_Collection = 'Workspaces_Collection';
33  const GridColumn_CollectionLevel = 'Workspaces_CollectionLevel';
34  const GridColumn_CollectionParent = 'Workspaces_CollectionParent';
35  const GridColumn_CollectionCurrent = 'Workspaces_CollectionCurrent';
36  const GridColumn_CollectionChildren = 'Workspaces_CollectionChildren';
37 
43  protected $currentWorkspace = null;
44 
50  protected $dataArray = [];
51 
57  protected $sort = '';
58 
64  protected $sortDir = '';
65 
69  protected $workspacesCache = null;
70 
74  protected $systemLanguages;
75 
79  protected $integrityService;
80 
90  public function generateGridListFromVersions($versions, $parameter, $currentWorkspace)
91  {
92  // Read the given parameters from grid. If the parameter is not set use default values.
93  $filterTxt = isset($parameter->filterTxt) ? $parameter->filterTxt : '';
94  $start = isset($parameter->start) ? (int)$parameter->start : 0;
95  $limit = isset($parameter->limit) ? (int)$parameter->limit : 30;
96  $this->sort = isset($parameter->sort) ? $parameter->sort : 't3ver_oid';
97  $this->sortDir = isset($parameter->dir) ? $parameter->dir : 'ASC';
98  if (is_int($currentWorkspace)) {
99  $this->currentWorkspace = $currentWorkspace;
100  } else {
101  throw new \InvalidArgumentException('No such workspace defined');
102  }
103  $data = [];
104  $data['data'] = [];
105  $this->generateDataArray($versions, $filterTxt);
106  $data['total'] = count($this->dataArray);
107  $data['data'] = $this->getDataArray($start, $limit);
108  return $data;
109  }
110 
118  protected function generateDataArray(array $versions, $filterTxt)
119  {
120  $workspaceAccess = $GLOBALS['BE_USER']->checkWorkspace($GLOBALS['BE_USER']->workspace);
121  $swapStage = $workspaceAccess['publish_access'] & 1 ? \TYPO3\CMS\Workspaces\Service\StagesService::STAGE_PUBLISH_ID : 0;
122  $swapAccess = $GLOBALS['BE_USER']->workspacePublishAccess($GLOBALS['BE_USER']->workspace) && $GLOBALS['BE_USER']->workspaceSwapAccess();
125  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
126  // check for dataArray in cache
127  if ($this->getDataArrayFromCache($versions, $filterTxt) === false) {
129  $stagesObj = GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\StagesService::class);
130  $defaultGridColumns = [
131  self::GridColumn_Collection => 0,
132  self::GridColumn_CollectionLevel => 0,
133  self::GridColumn_CollectionParent => '',
134  self::GridColumn_CollectionCurrent => '',
135  self::GridColumn_CollectionChildren => 0,
136  ];
137  foreach ($versions as $table => $records) {
138  $hiddenField = $this->getTcaEnableColumnsFieldName($table, 'disabled');
139  $isRecordTypeAllowedToModify = $GLOBALS['BE_USER']->check('tables_modify', $table);
140 
141  foreach ($records as $record) {
142  $origRecord = BackendUtility::getRecord($table, $record['t3ver_oid']);
143  $versionRecord = BackendUtility::getRecord($table, $record['uid']);
144  $combinedRecord = \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord::createFromArrays($table, $origRecord, $versionRecord);
145  $this->getIntegrityService()->checkElement($combinedRecord);
146 
147  if ($hiddenField !== null) {
148  $recordState = $this->workspaceState($versionRecord['t3ver_state'], $origRecord[$hiddenField], $versionRecord[$hiddenField]);
149  } else {
150  $recordState = $this->workspaceState($versionRecord['t3ver_state']);
151  }
152 
153  $isDeletedPage = $table == 'pages' && $recordState == 'deleted';
154  $viewUrl = \TYPO3\CMS\Workspaces\Service\WorkspaceService::viewSingleRecord($table, $record['uid'], $origRecord, $versionRecord);
155  $versionArray = [];
156  $versionArray['table'] = $table;
157  $versionArray['id'] = $table . ':' . $record['uid'];
158  $versionArray['uid'] = $record['uid'];
159  $versionArray['workspace'] = $versionRecord['t3ver_id'];
160  $versionArray = array_merge($versionArray, $defaultGridColumns);
161  $versionArray['label_Workspace'] = htmlspecialchars(BackendUtility::getRecordTitle($table, $versionRecord));
162  $versionArray['label_Live'] = htmlspecialchars(BackendUtility::getRecordTitle($table, $origRecord));
163  $versionArray['label_Stage'] = htmlspecialchars($stagesObj->getStageTitle($versionRecord['t3ver_stage']));
164  $tempStage = $stagesObj->getNextStage($versionRecord['t3ver_stage']);
165  $versionArray['label_nextStage'] = htmlspecialchars($stagesObj->getStageTitle($tempStage['uid']));
166  $tempStage = $stagesObj->getPrevStage($versionRecord['t3ver_stage']);
167  $versionArray['label_prevStage'] = htmlspecialchars($stagesObj->getStageTitle($tempStage['uid']));
168  $versionArray['path_Live'] = htmlspecialchars(BackendUtility::getRecordPath($record['livepid'], '', 999));
169  $versionArray['path_Workspace'] = htmlspecialchars(BackendUtility::getRecordPath($record['wspid'], '', 999));
170  $versionArray['workspace_Title'] = htmlspecialchars(\TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($versionRecord['t3ver_wsid']));
171  $versionArray['workspace_Tstamp'] = $versionRecord['tstamp'];
172  $versionArray['workspace_Formated_Tstamp'] = BackendUtility::datetime($versionRecord['tstamp']);
173  $versionArray['t3ver_wsid'] = $versionRecord['t3ver_wsid'];
174  $versionArray['t3ver_oid'] = $record['t3ver_oid'];
175  $versionArray['livepid'] = $record['livepid'];
176  $versionArray['stage'] = $versionRecord['t3ver_stage'];
177  $versionArray['icon_Live'] = $iconFactory->getIconForRecord($table, $origRecord, Icon::SIZE_SMALL)->render();
178  $versionArray['icon_Workspace'] = $iconFactory->getIconForRecord($table, $versionRecord, Icon::SIZE_SMALL)->render();
179  $languageValue = $this->getLanguageValue($table, $versionRecord);
180  $versionArray['languageValue'] = $languageValue;
181  $versionArray['language'] = [
182  'icon' => $iconFactory->getIcon($this->getSystemLanguageValue($languageValue, 'flagIcon'), Icon::SIZE_SMALL)->render()
183  ];
184  $versionArray['allowedAction_nextStage'] = $isRecordTypeAllowedToModify && $stagesObj->isNextStageAllowedForUser($versionRecord['t3ver_stage']);
185  $versionArray['allowedAction_prevStage'] = $isRecordTypeAllowedToModify && $stagesObj->isPrevStageAllowedForUser($versionRecord['t3ver_stage']);
186  if ($swapAccess && $swapStage != 0 && $versionRecord['t3ver_stage'] == $swapStage) {
187  $versionArray['allowedAction_swap'] = $isRecordTypeAllowedToModify && $stagesObj->isNextStageAllowedForUser($swapStage);
188  } elseif ($swapAccess && $swapStage == 0) {
189  $versionArray['allowedAction_swap'] = $isRecordTypeAllowedToModify;
190  } else {
191  $versionArray['allowedAction_swap'] = false;
192  }
193  $versionArray['allowedAction_delete'] = $isRecordTypeAllowedToModify;
194  // preview and editing of a deleted page won't work ;)
195  $versionArray['allowedAction_view'] = !$isDeletedPage && $viewUrl;
196  $versionArray['allowedAction_edit'] = $isRecordTypeAllowedToModify && !$isDeletedPage;
197  $versionArray['allowedAction_editVersionedPage'] = $isRecordTypeAllowedToModify && !$isDeletedPage;
198  $versionArray['state_Workspace'] = $recordState;
199 
200  $versionArray = array_merge(
201  $versionArray,
202  $this->getAdditionalColumnService()->getData($combinedRecord)
203  );
204 
205  if ($filterTxt == '' || $this->isFilterTextInVisibleColumns($filterTxt, $versionArray)) {
206  $versionIdentifier = $versionArray['id'];
207  $this->dataArray[$versionIdentifier] = $versionArray;
208  }
209  }
210  }
211  // Suggested slot method:
212  // methodName(\TYPO3\CMS\Workspaces\Service\GridDataService $gridData, array $dataArray, array $versions)
213  list($this->dataArray, $versions) = $this->emitSignal(self::SIGNAL_GenerateDataArray_BeforeCaching, $this->dataArray, $versions);
214  // Enrich elements after everything has been processed:
215  foreach ($this->dataArray as &$element) {
216  $identifier = $element['table'] . ':' . $element['t3ver_oid'];
217  $element['integrity'] = [
218  'status' => $this->getIntegrityService()->getStatusRepresentation($identifier),
219  'messages' => htmlspecialchars($this->getIntegrityService()->getIssueMessages($identifier, true))
220  ];
221  }
222  $this->setDataArrayIntoCache($versions, $filterTxt);
223  }
224  // Suggested slot method:
225  // methodName(\TYPO3\CMS\Workspaces\Service\GridDataService $gridData, array $dataArray, array $versions)
226  list($this->dataArray, $versions) = $this->emitSignal(self::SIGNAL_GenerateDataArray_PostProcesss, $this->dataArray, $versions);
227  $this->sortDataArray();
229  }
230 
237  protected function resolveDataArrayDependencies()
238  {
239  $collectionService = $this->getDependencyCollectionService();
240  $dependencyResolver = $collectionService->getDependencyResolver();
241 
242  foreach ($this->dataArray as $dataElement) {
243  $dependencyResolver->addElement($dataElement['table'], $dataElement['uid']);
244  }
245 
246  $this->dataArray = $collectionService->process($this->dataArray);
247  }
248 
256  protected function getDataArray($start, $limit)
257  {
258  $dataArrayPart = [];
259  $dataArrayCount = count($this->dataArray);
260  $end = ($start + $limit < $dataArrayCount ? $start + $limit : $dataArrayCount);
261 
262  // Ensure that there are numerical indexes
263  $this->dataArray = array_values(($this->dataArray));
264  for ($i = $start; $i < $end; $i++) {
265  $dataArrayPart[] = $this->dataArray[$i];
266  }
267 
268  // Ensure that collections are not cut for the pagination
269  if (!empty($this->dataArray[$i][self::GridColumn_Collection])) {
270  $collectionIdentifier = $this->dataArray[$i][self::GridColumn_Collection];
271  for ($i = $i + 1; $i < $dataArrayCount && $collectionIdentifier === $this->dataArray[$i][self::GridColumn_Collection]; $i++) {
272  $dataArrayPart[] = $this->dataArray[$i];
273  }
274  }
275 
276  // Suggested slot method:
277  // methodName(\TYPO3\CMS\Workspaces\Service\GridDataService $gridData, array $dataArray, $start, $limit, array $dataArrayPart)
278  list($this->dataArray, $start, $limit, $dataArrayPart) = $this->emitSignal(self::SIGNAL_GetDataArray_PostProcesss, $this->dataArray, $start, $limit, $dataArrayPart);
279  return $dataArrayPart;
280  }
281 
288  {
289  $this->workspacesCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('workspaces_cache');
290  }
291 
298  protected function setDataArrayIntoCache(array $versions, $filterTxt)
299  {
300  $hash = $this->calculateHash($versions, $filterTxt);
301  $this->workspacesCache->set($hash, $this->dataArray, [$this->currentWorkspace, 'user_' . $GLOBALS['BE_USER']->user['uid']]);
302  }
303 
311  protected function getDataArrayFromCache(array $versions, $filterTxt)
312  {
313  $cacheEntry = false;
314  $hash = $this->calculateHash($versions, $filterTxt);
315  $content = $this->workspacesCache->get($hash);
316  if ($content !== false) {
317  $this->dataArray = $content;
318  $cacheEntry = true;
319  }
320  return $cacheEntry;
321  }
322 
330  protected function calculateHash(array $versions, $filterTxt)
331  {
332  $hashArray = [
333  $GLOBALS['BE_USER']->workspace,
334  $GLOBALS['BE_USER']->user['uid'],
335  $versions,
336  $filterTxt,
337  $this->sort,
340  ];
341  $hash = md5(serialize($hashArray));
342  return $hash;
343  }
344 
351  protected function sortDataArray()
352  {
353  if (is_array($this->dataArray)) {
354  switch ($this->sort) {
355  case 'uid':
356  case 'change':
357  case 'workspace_Tstamp':
358  case 't3ver_oid':
359  case 'liveid':
360  case 'livepid':
361  case 'languageValue':
362  uasort($this->dataArray, [$this, 'intSort']);
363  break;
364  case 'label_Workspace':
365  case 'label_Live':
366  case 'label_Stage':
367  case 'workspace_Title':
368  case 'path_Live':
369  // case 'path_Workspace': This is the first sorting attribute
370  uasort($this->dataArray, [$this, 'stringSort']);
371  break;
372  default:
373  // Do nothing
374  }
375  } else {
376  GeneralUtility::sysLog(
377  'Try to sort "' . $this->sort . '" in "TYPO3\\CMS\\Workspaces\\Service\\GridDataService::sortDataArray" but $this->dataArray is empty! This might be the Bug #26422 which could not reproduced yet.',
378  'workspaces',
380  );
381  }
382  // Suggested slot method:
383  // methodName(\TYPO3\CMS\Workspaces\Service\GridDataService $gridData, array $dataArray, $sortColumn, $sortDirection)
384  list($this->dataArray, $this->sort, $this->sortDir) = $this->emitSignal(self::SIGNAL_SortDataArray_PostProcesss, $this->dataArray, $this->sort, $this->sortDir);
385  }
386 
394  protected function intSort(array $a, array $b)
395  {
396  if (!$this->isSortable($a, $b)) {
397  return 0;
398  }
399  // First sort by using the page-path in current workspace
400  $path_cmp = strcasecmp($a['path_Workspace'], $b['path_Workspace']);
401  if ($path_cmp < 0) {
402  return $path_cmp;
403  } elseif ($path_cmp == 0) {
404  if ($a[$this->sort] == $b[$this->sort]) {
405  return 0;
406  }
407  if ($this->sortDir == 'ASC') {
408  return $a[$this->sort] < $b[$this->sort] ? -1 : 1;
409  } elseif ($this->sortDir == 'DESC') {
410  return $a[$this->sort] > $b[$this->sort] ? -1 : 1;
411  }
412  } elseif ($path_cmp > 0) {
413  return $path_cmp;
414  }
415  return 0;
416  }
417 
425  protected function stringSort($a, $b)
426  {
427  if (!$this->isSortable($a, $b)) {
428  return 0;
429  }
430  $path_cmp = strcasecmp($a['path_Workspace'], $b['path_Workspace']);
431  if ($path_cmp < 0) {
432  return $path_cmp;
433  } elseif ($path_cmp == 0) {
434  if ($a[$this->sort] == $b[$this->sort]) {
435  return 0;
436  }
437  if ($this->sortDir == 'ASC') {
438  return strcasecmp($a[$this->sort], $b[$this->sort]);
439  } elseif ($this->sortDir == 'DESC') {
440  return strcasecmp($a[$this->sort], $b[$this->sort]) * -1;
441  }
442  } elseif ($path_cmp > 0) {
443  return $path_cmp;
444  }
445  return 0;
446  }
447 
457  protected function isSortable(array $a, array $b)
458  {
459  return
460  $a[self::GridColumn_CollectionLevel] === 0 && $b[self::GridColumn_CollectionLevel] === 0
461  || $a[self::GridColumn_CollectionParent] === $b[self::GridColumn_CollectionParent]
462  ;
463  }
464 
473  protected function isFilterTextInVisibleColumns($filterText, array $versionArray)
474  {
475  if (is_array($GLOBALS['BE_USER']->uc['moduleData']['Workspaces'][$GLOBALS['BE_USER']->workspace]['columns'])) {
476  foreach ($GLOBALS['BE_USER']->uc['moduleData']['Workspaces'][$GLOBALS['BE_USER']->workspace]['columns'] as $column => $value) {
477  if (isset($value['hidden']) && isset($column) && isset($versionArray[$column])) {
478  if ($value['hidden'] == 0) {
479  switch ($column) {
480  case 'workspace_Tstamp':
481  if (stripos($versionArray['workspace_Formated_Tstamp'], $filterText) !== false) {
482  return true;
483  }
484  break;
485  case 'change':
486  if (stripos(strval($versionArray[$column]), str_replace('%', '', $filterText)) !== false) {
487  return true;
488  }
489  break;
490  default:
491  if (stripos(strval($versionArray[$column]), $filterText) !== false) {
492  return true;
493  }
494  }
495  }
496  }
497  }
498  }
499  return false;
500  }
501 
510  protected function workspaceState($stateId, $hiddenOnline = false, $hiddenOffline = false)
511  {
512  $hiddenState = null;
513  if ($hiddenOnline == 0 && $hiddenOffline == 1) {
514  $hiddenState = 'hidden';
515  } elseif ($hiddenOnline == 1 && $hiddenOffline == 0) {
516  $hiddenState = 'unhidden';
517  }
518  switch ($stateId) {
519  case -1:
520  $state = 'new';
521  break;
522  case 2:
523  $state = 'deleted';
524  break;
525  case 4:
526  $state = 'moved';
527  break;
528  default:
529  $state = ($hiddenState ?: 'modified');
530  }
531  return $state;
532  }
533 
541  protected function getTcaEnableColumnsFieldName($table, $type)
542  {
543  $fieldName = null;
544 
545  if (!(empty($GLOBALS['TCA'][$table]['ctrl']['enablecolumns'][$type]))) {
546  $fieldName = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns'][$type];
547  }
548 
549  return $fieldName;
550  }
551 
560  protected function getLanguageValue($table, array $record)
561  {
562  $languageValue = 0;
564  $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
565  if (!empty($record[$languageField])) {
566  $languageValue = $record[$languageField];
567  }
568  }
569  return $languageValue;
570  }
571 
580  protected function getSystemLanguageValue($id, $key)
581  {
582  $value = null;
583  $systemLanguages = $this->getSystemLanguages();
584  if (!empty($systemLanguages[$id][$key])) {
585  $value = $systemLanguages[$id][$key];
586  }
587  return $value;
588  }
589 
595  public function getSystemLanguages()
596  {
597  if (!isset($this->systemLanguages)) {
599  $translateTools = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider::class);
600  $this->systemLanguages = $translateTools->getSystemLanguages();
601  }
602  return $this->systemLanguages;
603  }
604 
610  protected function getIntegrityService()
611  {
612  if (!isset($this->integrityService)) {
613  $this->integrityService = GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\IntegrityService::class);
614  }
616  }
617 
624  protected function emitSignal($signalName)
625  {
626  // Arguments are always ($this, [method argument], [method argument], ...)
627  $signalArguments = array_merge([$this], array_slice(func_get_args(), 1));
628  $slotReturn = $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Workspaces\Service\GridDataService::class, $signalName, $signalArguments);
629  return array_slice($slotReturn, 1);
630  }
631 
635  protected function getDependencyCollectionService()
636  {
637  return GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\Dependency\CollectionService::class);
638  }
639 
643  protected function getAdditionalColumnService()
644  {
645  return $this->getObjectManager()->get(\TYPO3\CMS\Workspaces\Service\AdditionalColumnService::class);
646  }
647 
651  protected function getSignalSlotDispatcher()
652  {
653  return $this->getObjectManager()->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
654  }
655 
659  protected function getObjectManager()
660  {
661  return GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
662  }
663 }
static viewSingleRecord($table, $uid, array $liveRecord=null, array $versionRecord=null)
static createFromArrays($table, array $liveRow, array $versionRow)
workspaceState($stateId, $hiddenOnline=false, $hiddenOffline=false)
generateGridListFromVersions($versions, $parameter, $currentWorkspace)
static getRecordTitle($table, $row, $prep=false, $forceResult=true)
calculateHash(array $versions, $filterTxt)
isFilterTextInVisibleColumns($filterText, array $versionArray)
static getRecordPath($uid, $clause, $titleLimit, $fullTitleLimit=0)
setDataArrayIntoCache(array $versions, $filterTxt)
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getDataArrayFromCache(array $versions, $filterTxt)