74e3fd97e74f2f37b94706f72b1fb6c1780b990f
[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\Utility\GeneralUtility;
22 use TYPO3\CMS\Fluid\View\StandaloneView;
23
24 /**
25 * Alist of all open documents
26 */
27 class OpendocsToolbarItem implements ToolbarItemInterface
28 {
29 /**
30 * @var array
31 */
32 protected $openDocs;
33
34 /**
35 * @var array
36 */
37 protected $recentDocs;
38
39 /**
40 * Constructor
41 */
42 public function __construct()
43 {
44 $this->loadDocsFromUserSession();
45 }
46
47 /**
48 * Checks whether the user has access to this toolbar item
49 *
50 * @return bool TRUE if user has access, FALSE if not
51 */
52 public function checkAccess()
53 {
54 $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
55 return $conf['value'] != 1;
56 }
57
58 /**
59 * Loads the opened and recently opened documents from the user
60 *
61 * @return void
62 */
63 public function loadDocsFromUserSession()
64 {
65 $backendUser = $this->getBackendUser();
66 list($this->openDocs, ) = $backendUser->getModuleData('FormEngine', 'ses') ?: [];
67 $this->recentDocs = $backendUser->getModuleData('opendocs::recent') ?: [];
68 }
69
70 /**
71 * Render toolbar icon
72 *
73 * @return string HTML
74 */
75 public function getItem()
76 {
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'
81 ));
82 $view->assign('numDocs', count($this->openDocs));
83 return $view->render();
84 }
85
86 /**
87 * Render drop down
88 *
89 * @return string HTML
90 */
91 public function getDropDown()
92 {
93 $assigns = [];
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);
99
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'
104 ));
105 $view->assignMultiple($assigns);
106 return $view->render();
107 }
108
109 /**
110 * Get menu entries for all eligible records
111 *
112 * @param $documents
113 * @return array
114 */
115 protected function getMenuEntries(array $documents) : array
116 {
117 $entries = [];
118 foreach ($documents as $md5sum => $recentDocument) {
119 $menuEntry = $this->getMenuEntry($recentDocument, $md5sum);
120 if ($menuEntry !== '') {
121 $entries[] = $menuEntry;
122 }
123 }
124 return $entries;
125 }
126
127 /**
128 * Returns the data for a recent or open document
129 *
130 * @param array $document
131 * @param string $md5sum
132 * @return array The data of a recent or closed document
133 */
134 protected function getMenuEntry($document, $md5sum)
135 {
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
141 return '';
142 }
143 $result = [];
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'];
152 }
153 $onClickCode = 'jump(' . GeneralUtility::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
154 $result['onClickCode'] = $onClickCode;
155 $result['md5sum'] = $md5sum;
156 return $result;
157 }
158
159 /**
160 * No additional attributes
161 *
162 * @return string List item HTML attibutes
163 */
164 public function getAdditionalAttributes()
165 {
166 return [];
167 }
168
169 /**
170 * This item has a drop down
171 *
172 * @return bool
173 */
174 public function hasDropDown()
175 {
176 return true;
177 }
178
179 /*******************
180 *** HOOKS ***
181 *******************/
182 /**
183 * Called as a hook in \TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal, calls a JS function to change
184 * the number of opened documents
185 *
186 * @param array $params
187 * @param unknown_type $ref
188 * @return string list item HTML attributes
189 */
190 public function updateNumberOfOpenDocsHook(&$params, $ref)
191 {
192 $params['JScode'] = '
193 if (top && top.TYPO3.OpendocsMenu) {
194 top.TYPO3.OpendocsMenu.updateMenu();
195 }
196 ';
197 }
198
199 /******************
200 *** AJAX CALLS ***
201 ******************/
202 /**
203 * Closes a document in the session and
204 *
205 * @param ServerRequestInterface $request
206 * @param ResponseInterface $response
207 * @return ResponseInterface
208 */
209 public function closeDocument(ServerRequestInterface $request, ResponseInterface $response)
210 {
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);
219 }
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);
225 }
226 return $this->renderMenu($request, $response);
227 }
228
229 /**
230 * Renders the menu so that it can be returned as response to an AJAX call
231 *
232 * @param ServerRequestInterface $request
233 * @param ResponseInterface $response
234 * @return ResponseInterface
235 */
236 public function renderMenu(ServerRequestInterface $request, ResponseInterface $response)
237 {
238 $response->getBody()->write($this->getDropDown());
239 return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
240 }
241
242 /**
243 * Position relative to others
244 *
245 * @return int
246 */
247 public function getIndex()
248 {
249 return 30;
250 }
251
252 /**
253 * Returns the current BE user.
254 *
255 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
256 */
257 protected function getBackendUser()
258 {
259 return $GLOBALS['BE_USER'];
260 }
261 }