2 declare(strict_types
= 1);
3 namespace TYPO3\CMS\Opendocs\Service
;
6 * This file is part of the TYPO3 CMS project.
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication
;
21 * Access to open and recent documents
23 class OpenDocumentService
26 * @var BackendUserAuthentication
28 protected $backendUser;
32 * @param BackendUserAuthentication|null $backendUser
34 public function __construct(BackendUserAuthentication
$backendUser = null
)
36 $this->backendUser
= $backendUser ?
: $GLOBALS['BE_USER'];
40 * Get the list of open documents for the current user
44 public function getOpenDocuments(): array
47 $sessionOpenDocuments = $this->backendUser
->getModuleData('FormEngine', 'ses');
49 if ($sessionOpenDocuments !== null
) {
50 $openDocuments = $sessionOpenDocuments[0];
53 return $openDocuments;
57 * Get the list of recent documents for the current user
61 public function getRecentDocuments(): array
63 return $this->backendUser
->getModuleData('opendocs::recent') ?
: [];
67 * Close a document and add it to the list of recent documents
69 * @param string $identifier a document identifier (MD5 hash)
71 public function closeDocument(string $identifier): void
73 $openDocuments = $this->getOpenDocuments();
75 if (!isset($openDocuments[$identifier])) {
79 $document = $openDocuments[$identifier];
80 unset($openDocuments[$identifier]);
82 $this->storeOpenDocuments($openDocuments);
83 $this->addToRecentDocuments($identifier, $document);
87 * Store a list of open documents
89 * @param array $openDocuments
91 protected function storeOpenDocuments(array $openDocuments): void
93 list(, $lastOpenDocumentIdentifier) = $this->backendUser
->getModuleData('FormEngine', 'ses');
94 $this->backendUser
->pushModuleData('FormEngine', [$openDocuments, $lastOpenDocumentIdentifier]);
98 * Add a document to the list of recent documents
100 * @param string $identifier identifier of the document
101 * @param array $document document data
103 protected function addToRecentDocuments(string $identifier, array $document): void
105 $recentDocuments = $this->getRecentDocuments();
106 $recentDocuments = array_merge(
107 [$identifier => $document],
111 // Allow a maximum of 8 recent documents
112 if (count($recentDocuments) > 8) {
113 $recentDocuments = array_slice($recentDocuments, 0, 8);
116 $this->backendUser
->pushModuleData('opendocs::recent', $recentDocuments);