b3e8a09f901a8aa2e6dd7fc3560aa6fcb5a1bef5
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Classes / Backend / ToolbarItems / OpendocsToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Opendocs\Backend\ToolbarItems;
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
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Fluid\View\StandaloneView;
24
25 /**
26 * Main functionality to render a list of all open documents in the top bar of the TYPO3 Backend
27 *
28 * This class also contains hooks and AJAX calls related to the toolbar item dynamic updating processing
29 */
30 class OpendocsToolbarItem implements ToolbarItemInterface
31 {
32 /**
33 * @var array
34 */
35 protected $openDocs = [];
36
37 /**
38 * @var array
39 */
40 protected $recentDocs = [];
41
42 /**
43 * Constructor
44 */
45 public function __construct()
46 {
47 $this->loadDocsFromUserSession();
48 }
49
50 /**
51 * Checks whether the user has access to this toolbar item
52 *
53 * @return bool TRUE if user has access, FALSE if not
54 */
55 public function checkAccess()
56 {
57 $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
58 return (int)$conf['value'] !== 1;
59 }
60
61 /**
62 * Loads the opened and recently opened documents from the user
63 */
64 public function loadDocsFromUserSession()
65 {
66 $backendUser = $this->getBackendUser();
67 $openDocs = $backendUser->getModuleData('FormEngine', 'ses');
68 if ($openDocs !== null) {
69 list($this->openDocs, ) = $openDocs;
70 }
71 $this->recentDocs = $backendUser->getModuleData('opendocs::recent') ?: [];
72 }
73
74 /**
75 * Render toolbar icon via Fluid
76 *
77 * @return string HTML
78 */
79 public function getItem()
80 {
81 $view = $this->getFluidTemplateObject('ToolbarItem.html');
82 $view->assign('numDocs', count($this->openDocs));
83 return $view->render();
84 }
85
86 /**
87 * Render drop down via Fluid
88 *
89 * @return string HTML
90 */
91 public function getDropDown()
92 {
93 $view = $this->getFluidTemplateObject('DropDown.html');
94 $view->assignMultiple([
95 'openDocuments' => $this->getMenuEntries($this->openDocs),
96 // If there are "recent documents" in the list, add them
97 'recentDocuments' => $this->getMenuEntries($this->recentDocs)
98 ]);
99 return $view->render();
100 }
101
102 /**
103 * Get menu entries for all eligible records
104 *
105 * @param array $documents
106 * @return array
107 */
108 protected function getMenuEntries(array $documents): array
109 {
110 $entries = [];
111 foreach ($documents as $md5sum => $document) {
112 $menuEntry = $this->getMenuEntry($document, $md5sum);
113 if (is_array($menuEntry)) {
114 $entries[] = $menuEntry;
115 }
116 }
117 return $entries;
118 }
119
120 /**
121 * Returns the data for a recent or open document
122 *
123 * @param array $document
124 * @param string $md5sum
125 * @return array The data of a recent or closed document, or null if no record was found (e.g. deleted)
126 */
127 protected function getMenuEntry($document, $md5sum)
128 {
129 $table = $document[3]['table'];
130 $uid = $document[3]['uid'];
131 $record = BackendUtility::getRecordWSOL($table, $uid);
132 if (!is_array($record)) {
133 // Record seems to be deleted
134 return null;
135 }
136 $result = [];
137 $result['table'] = $table;
138 $result['record'] = $record;
139 $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
140 $result['label'] = $label;
141 $link = BackendUtility::getModuleUrl('record_edit') . '&' . $document[2];
142 $pageId = (int)$document[3]['uid'];
143 if ($document[3]['table'] !== 'pages') {
144 $pageId = (int)$document[3]['pid'];
145 }
146 $onClickCode = 'jump(' . GeneralUtility::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
147 $result['onClickCode'] = $onClickCode;
148 $result['md5sum'] = $md5sum;
149 return $result;
150 }
151
152 /**
153 * No additional attributes
154 *
155 * @return string List item HTML attibutes
156 */
157 public function getAdditionalAttributes()
158 {
159 return [];
160 }
161
162 /**
163 * This item has a drop down
164 *
165 * @return bool
166 */
167 public function hasDropDown()
168 {
169 return true;
170 }
171
172 /*******************
173 *** HOOKS ***
174 *******************/
175 /**
176 * Called as a hook in \TYPO3\CMS\Backend\Utility\BackendUtility::getUpdateSignalCode, calls a JS function to change
177 * the number of opened documents
178 *
179 * @param array $params
180 */
181 public function updateNumberOfOpenDocsHook(&$params)
182 {
183 $params['JScode'] = '
184 if (top && top.TYPO3.OpendocsMenu) {
185 top.TYPO3.OpendocsMenu.updateMenu();
186 }
187 ';
188 }
189
190 /******************
191 *** AJAX CALLS ***
192 ******************/
193 /**
194 * Closes a document in the session and
195 *
196 * @param ServerRequestInterface $request
197 * @param ResponseInterface $response
198 * @return ResponseInterface
199 */
200 public function closeDocument(ServerRequestInterface $request, ResponseInterface $response)
201 {
202 $md5sum = isset($request->getParsedBody()['md5sum']) ? $request->getParsedBody()['md5sum'] : $request->getQueryParams()['md5sum'];
203 if ($md5sum && isset($this->openDocs[$md5sum])) {
204 $backendUser = $this->getBackendUser();
205 // Add the document to be closed to the recent documents
206 $this->recentDocs = array_merge([$md5sum => $this->openDocs[$md5sum]], $this->recentDocs);
207 // Allow a maximum of 8 recent documents
208 if (count($this->recentDocs) > 8) {
209 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
210 }
211 // Remove it from the list of the open documents, and store the status
212 unset($this->openDocs[$md5sum]);
213 list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
214 $backendUser->pushModuleData('FormEngine', [$this->openDocs, $docDat]);
215 $backendUser->pushModuleData('opendocs::recent', $this->recentDocs);
216 }
217 return $this->renderMenu($request, $response);
218 }
219
220 /**
221 * Renders the menu so that it can be returned as response to an AJAX call
222 *
223 * @param ServerRequestInterface $request
224 * @param ResponseInterface $response
225 * @return ResponseInterface
226 */
227 public function renderMenu(ServerRequestInterface $request, ResponseInterface $response)
228 {
229 $response->getBody()->write($this->getDropDown());
230 return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
231 }
232
233 /**
234 * Position relative to others
235 *
236 * @return int
237 */
238 public function getIndex()
239 {
240 return 30;
241 }
242
243 /**
244 * Returns the current BE user.
245 *
246 * @return BackendUserAuthentication
247 */
248 protected function getBackendUser()
249 {
250 return $GLOBALS['BE_USER'];
251 }
252
253 /**
254 * Returns a new standalone view, shorthand function
255 *
256 * @param string $filename Which templateFile should be used.
257 * @return StandaloneView
258 */
259 protected function getFluidTemplateObject(string $filename): StandaloneView
260 {
261 $view = GeneralUtility::makeInstance(StandaloneView::class);
262 $view->setLayoutRootPaths(['EXT:opendocs/Resources/Private/Layouts']);
263 $view->setPartialRootPaths([
264 'EXT:backend/Resources/Private/Partials/ToolbarItems',
265 'EXT:opendocs/Resources/Private/Partials/ToolbarItems'
266 ]);
267 $view->setTemplateRootPaths(['EXT:opendocs/Resources/Private/Templates/ToolbarItems']);
268
269 $view->setTemplate($filename);
270
271 $view->getRequest()->setControllerExtensionName('Opendocs');
272 return $view;
273 }
274 }