2 namespace TYPO3\CMS\Opendocs\Backend\ToolbarItems
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
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\Utility\GeneralUtility
;
22 use TYPO3\CMS\Fluid\View\StandaloneView
;
25 * Alist of all open documents
27 class OpendocsToolbarItem
implements ToolbarItemInterface
32 protected $openDocs = [];
37 protected $recentDocs = [];
42 public function __construct()
44 $this->loadDocsFromUserSession();
48 * Checks whether the user has access to this toolbar item
50 * @return bool TRUE if user has access, FALSE if not
52 public function checkAccess()
54 $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
55 return $conf['value'] != 1;
59 * Loads the opened and recently opened documents from the user
63 public function loadDocsFromUserSession()
65 $backendUser = $this->getBackendUser();
66 list($this->openDocs
, ) = $backendUser->getModuleData('FormEngine', 'ses') ?
: [];
67 $this->recentDocs
= $backendUser->getModuleData('opendocs::recent') ?
: [];
75 public function getItem()
77 // Rendering of the output via fluid
78 $view = GeneralUtility
::makeInstance(StandaloneView
::class);
79 $view->setTemplatePathAndFilename(GeneralUtility
::getFileAbsFileName(
80 'EXT:opendocs/Resources/Private/Templates/ToolbarItem.html'
82 $view->assign('numDocs', count($this->openDocs
));
83 return $view->render();
91 public function getDropDown()
94 $openDocuments = $this->openDocs
;
95 $recentDocuments = $this->recentDocs
;
96 $assigns['openDocuments'] = $this->getMenuEntries($openDocuments);
97 // If there are "recent documents" in the list, add them
98 $assigns['recentDocuments'] = $this->getMenuEntries($recentDocuments);
100 // Rendering of the output via fluid
101 $view = GeneralUtility
::makeInstance(StandaloneView
::class);
102 $view->setTemplatePathAndFilename(GeneralUtility
::getFileAbsFileName(
103 'EXT:opendocs/Resources/Private/Templates/DropDown.html'
105 $view->assignMultiple($assigns);
106 return $view->render();
110 * Get menu entries for all eligible records
115 protected function getMenuEntries(array $documents) : array
118 foreach ($documents as $md5sum => $recentDocument) {
119 $menuEntry = $this->getMenuEntry($recentDocument, $md5sum);
120 if ($menuEntry !== '') {
121 $entries[] = $menuEntry;
128 * Returns the data for a recent or open document
130 * @param array $document
131 * @param string $md5sum
132 * @return array The data of a recent or closed document
134 protected function getMenuEntry($document, $md5sum)
136 $table = $document[3]['table'];
137 $uid = $document[3]['uid'];
138 $record = BackendUtility
::getRecordWSOL($table, $uid);
139 if (!is_array($record)) {
140 // Record seems to be deleted
144 $result['table'] = $table;
145 $result['record'] = $record;
146 $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
147 $result['label'] = $label;
148 $link = BackendUtility
::getModuleUrl('record_edit') . '&' . $document[2];
149 $pageId = (int)$document[3]['uid'];
150 if ($document[3]['table'] !== 'pages') {
151 $pageId = (int)$document[3]['pid'];
153 $onClickCode = 'jump(' . GeneralUtility
::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
154 $result['onClickCode'] = $onClickCode;
155 $result['md5sum'] = $md5sum;
160 * No additional attributes
162 * @return string List item HTML attibutes
164 public function getAdditionalAttributes()
170 * This item has a drop down
174 public function hasDropDown()
183 * Called as a hook in \TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal, calls a JS function to change
184 * the number of opened documents
186 * @param array $params
187 * @param unknown_type $ref
188 * @return string list item HTML attributes
190 public function updateNumberOfOpenDocsHook(&$params, $ref)
192 $params['JScode'] = '
193 if (top && top.TYPO3.OpendocsMenu) {
194 top.TYPO3.OpendocsMenu.updateMenu();
203 * Closes a document in the session and
205 * @param ServerRequestInterface $request
206 * @param ResponseInterface $response
207 * @return ResponseInterface
209 public function closeDocument(ServerRequestInterface
$request, ResponseInterface
$response)
211 $backendUser = $this->getBackendUser();
212 $md5sum = isset($request->getParsedBody()['md5sum']) ?
$request->getParsedBody()['md5sum'] : $request->getQueryParams()['md5sum'];
213 if ($md5sum && isset($this->openDocs
[$md5sum])) {
214 // Add the document to be closed to the recent documents
215 $this->recentDocs
= array_merge([$md5sum => $this->openDocs
[$md5sum]], $this->recentDocs
);
216 // Allow a maximum of 8 recent documents
217 if (count($this->recentDocs
) > 8) {
218 $this->recentDocs
= array_slice($this->recentDocs
, 0, 8);
220 // Remove it from the list of the open documents, and store the status
221 unset($this->openDocs
[$md5sum]);
222 list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
223 $backendUser->pushModuleData('FormEngine', [$this->openDocs
, $docDat]);
224 $backendUser->pushModuleData('opendocs::recent', $this->recentDocs
);
226 return $this->renderMenu($request, $response);
230 * Renders the menu so that it can be returned as response to an AJAX call
232 * @param ServerRequestInterface $request
233 * @param ResponseInterface $response
234 * @return ResponseInterface
236 public function renderMenu(ServerRequestInterface
$request, ResponseInterface
$response)
238 $response->getBody()->write($this->getDropDown());
239 return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
243 * Position relative to others
247 public function getIndex()
253 * Returns the current BE user.
255 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
257 protected function getBackendUser()
259 return $GLOBALS['BE_USER'];