2dafd2637f4a3737cfe80d03264f5dfc000640fc
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Classes / Service / OpenDocumentService.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Opendocs\Service;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
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.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
20 /**
21 * Access to open and recent documents
22 */
23 class OpenDocumentService
24 {
25 /**
26 * @var BackendUserAuthentication
27 */
28 protected $backendUser;
29
30 /**
31 * Constructor
32 */
33 public function __construct(BackendUserAuthentication $backendUser = null)
34 {
35 $this->backendUser = $backendUser ?: $GLOBALS['BE_USER'];
36 }
37
38 /**
39 * Get the list of open documents for the current user
40 *
41 * @return array
42 */
43 public function getOpenDocuments(): array
44 {
45 $openDocuments = [];
46 $sessionOpenDocuments = $this->backendUser->getModuleData('FormEngine', 'ses');
47
48 if ($sessionOpenDocuments !== null) {
49 $openDocuments = $sessionOpenDocuments[0];
50 }
51
52 return $openDocuments;
53 }
54
55 /**
56 * Get the list of recent documents for the current user
57 *
58 * @return array
59 */
60 public function getRecentDocuments(): array
61 {
62 return $this->backendUser->getModuleData('opendocs::recent') ?: [];
63 }
64
65 /**
66 * Close a document and add it to the list of recent documents
67 *
68 * @param string $identifier a document identifier (MD5 hash)
69 */
70 public function closeDocument(string $identifier): void
71 {
72 $openDocuments = $this->getOpenDocuments();
73
74 if (!isset($openDocuments[$identifier])) {
75 return;
76 }
77
78 $document = $openDocuments[$identifier];
79 unset($openDocuments[$identifier]);
80
81 $this->storeOpenDocuments($openDocuments);
82 $this->addToRecentDocuments($identifier, $document);
83 }
84
85 /**
86 * Store a list of open documents
87 *
88 * @param array $openDocuments
89 */
90 protected function storeOpenDocuments(array $openDocuments): void
91 {
92 list(, $lastOpenDocumentIdentifier) = $this->backendUser->getModuleData('FormEngine', 'ses');
93 $this->backendUser->pushModuleData('FormEngine', [$openDocuments, $lastOpenDocumentIdentifier]);
94 }
95
96 /**
97 * Add a document to the list of recent documents
98 *
99 * @param string $identifier identifier of the document
100 * @param array $document document data
101 */
102 protected function addToRecentDocuments(string $identifier, array $document): void
103 {
104 $recentDocuments = $this->getRecentDocuments();
105 $recentDocuments = array_merge(
106 [$identifier => $document],
107 $recentDocuments
108 );
109
110 // Allow a maximum of 8 recent documents
111 if (count($recentDocuments) > 8) {
112 $recentDocuments = array_slice($recentDocuments, 0, 8);
113 }
114
115 $this->backendUser->pushModuleData('opendocs::recent', $recentDocuments);
116 }
117 }