[TASK] Remove unused OpendocsToolbarItem->getDatabaseConnection
[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\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Page\PageRenderer;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Alist of all open documents
27 */
28 class OpendocsToolbarItem implements ToolbarItemInterface
29 {
30 /**
31 * @var array
32 */
33 protected $openDocs;
34
35 /**
36 * @var array
37 */
38 protected $recentDocs;
39
40 /**
41 * @var IconFactory
42 */
43 protected $iconFactory;
44
45 /**
46 * Constructor
47 */
48 public function __construct()
49 {
50 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
51 $this->getLanguageService()->includeLLFile('EXT:opendocs/Resources/Private/Language/locallang.xlf');
52 $this->loadDocsFromUserSession();
53 $pageRenderer = $this->getPageRenderer();
54 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Opendocs/Toolbar/OpendocsMenu');
55 }
56
57 /**
58 * Checks whether the user has access to this toolbar item
59 *
60 * @return bool TRUE if user has access, FALSE if not
61 */
62 public function checkAccess()
63 {
64 $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
65 return $conf['value'] != 1;
66 }
67
68 /**
69 * Loads the opened and recently opened documents from the user
70 *
71 * @return void
72 */
73 public function loadDocsFromUserSession()
74 {
75 $backendUser = $this->getBackendUser();
76 list($this->openDocs, ) = $backendUser->getModuleData('FormEngine', 'ses');
77 $this->recentDocs = $backendUser->getModuleData('opendocs::recent');
78 }
79
80 /**
81 * Render toolbar icon
82 *
83 * @return string HTML
84 */
85 public function getItem()
86 {
87 $numDocs = count($this->openDocs);
88 $title = htmlspecialchars($this->getLanguageService()->getLL('toolbaritem'));
89
90 $opendocsMenu = array();
91 $opendocsMenu[] = '<span title="' . $title . '">' . $this->iconFactory->getIcon('apps-toolbar-menu-opendocs', Icon::SIZE_SMALL)->render('inline') . '</span>';
92 $opendocsMenu[] = '<span class="badge" id="tx-opendocs-counter">' . $numDocs . '</span>';
93
94 return implode(LF, $opendocsMenu);
95 }
96
97 /**
98 * Render drop down
99 *
100 * @return string HTML
101 */
102 public function getDropDown()
103 {
104 $languageService = $this->getLanguageService();
105 $openDocuments = $this->openDocs;
106 $recentDocuments = $this->recentDocs;
107 $entries = array();
108 if (!empty($openDocuments)) {
109 $entries[] = '<li class="dropdown-header">' . htmlspecialchars($languageService->getLL('open_docs')) . '</li>';
110 $i = 0;
111 foreach ($openDocuments as $md5sum => $openDocument) {
112 $i++;
113 $entries[] = $this->renderMenuEntry($openDocument, $md5sum, false, $i == 1);
114 }
115 $entries[] = '<li class="divider"></li>';
116 }
117 // If there are "recent documents" in the list, add them
118 if (!empty($recentDocuments)) {
119 $entries[] = '<li class="dropdown-header">' . htmlspecialchars($languageService->getLL('recent_docs')) . '</li>';
120 $i = 0;
121 foreach ($recentDocuments as $md5sum => $recentDocument) {
122 $i++;
123 $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, true, $i == 1);
124 }
125 }
126 if (!empty($entries)) {
127 $content = '<ul class="dropdown-list">' . implode('', $entries) . '</ul>';
128 } else {
129 $content = '<p>' . htmlspecialchars($languageService->getLL('no_docs')) . '</p>';
130 }
131 return $content;
132 }
133
134 /**
135 * Returns the recent documents list as an array
136 *
137 * @param array $document
138 * @param string $md5sum
139 * @param bool $isRecentDoc
140 * @param bool $isFirstDoc
141 * @return array All recent documents as list-items
142 */
143 protected function renderMenuEntry($document, $md5sum, $isRecentDoc = false, $isFirstDoc = false)
144 {
145 $table = $document[3]['table'];
146 $uid = $document[3]['uid'];
147 $record = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL($table, $uid);
148 if (!is_array($record)) {
149 // Record seems to be deleted
150 return '';
151 }
152 $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
153 $icon = $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render();
154 $link = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('record_edit') . '&' . $document[2];
155 $pageId = (int)$document[3]['uid'];
156 if ($document[3]['table'] !== 'pages') {
157 $pageId = (int)$document[3]['pid'];
158 }
159 $onClickCode = 'jump(' . GeneralUtility::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
160 if (!$isRecentDoc) {
161 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc'));
162 // Open document
163 $closeIcon = $this->iconFactory->getIcon('actions-close', Icon::SIZE_SMALL)->render('inline');
164 $entry = '
165 <li class="opendoc">
166 <a href="#" class="dropdown-list-link dropdown-link-list-add-close" onclick="' . htmlspecialchars($onClickCode) . '" target="content">' . $icon . ' ' . $label . '</a>
167 <a href="#" class="dropdown-list-link-close" data-opendocsidentifier="' . $md5sum . '" title="' . $title . '">' . $closeIcon . '</a>
168 </li>';
169 } else {
170 // Recently used document
171 $entry = '
172 <li>
173 <a href="#" class="dropdown-list-link" onclick="' . htmlspecialchars($onClickCode) . '" target="content">' . $icon . ' ' . $label . '</a>
174 </li>';
175 }
176 return $entry;
177 }
178
179 /**
180 * No additional attributes
181 *
182 * @return string List item HTML attibutes
183 */
184 public function getAdditionalAttributes()
185 {
186 return array();
187 }
188
189 /**
190 * This item has a drop down
191 *
192 * @return bool
193 */
194 public function hasDropDown()
195 {
196 return true;
197 }
198
199 /*******************
200 *** HOOKS ***
201 *******************/
202 /**
203 * Called as a hook in \TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal, calls a JS function to change
204 * the number of opened documents
205 *
206 * @param array $params
207 * @param unknown_type $ref
208 * @return string list item HTML attributes
209 */
210 public function updateNumberOfOpenDocsHook(&$params, $ref)
211 {
212 $params['JScode'] = '
213 if (top && top.TYPO3.OpendocsMenu) {
214 top.TYPO3.OpendocsMenu.updateMenu();
215 }
216 ';
217 }
218
219 /******************
220 *** AJAX CALLS ***
221 ******************/
222 /**
223 * Closes a document in the session and
224 *
225 * @param ServerRequestInterface $request
226 * @param ResponseInterface $response
227 * @return ResponseInterface
228 */
229 public function closeDocument(ServerRequestInterface $request, ResponseInterface $response)
230 {
231 $backendUser = $this->getBackendUser();
232 $md5sum = isset($request->getParsedBody()['md5sum']) ? $request->getParsedBody()['md5sum'] : $request->getQueryParams()['md5sum'];
233 if ($md5sum && isset($this->openDocs[$md5sum])) {
234 // Add the document to be closed to the recent documents
235 $this->recentDocs = array_merge(array($md5sum => $this->openDocs[$md5sum]), $this->recentDocs);
236 // Allow a maximum of 8 recent documents
237 if (count($this->recentDocs) > 8) {
238 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
239 }
240 // Remove it from the list of the open documents, and store the status
241 unset($this->openDocs[$md5sum]);
242 list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
243 $backendUser->pushModuleData('FormEngine', array($this->openDocs, $docDat));
244 $backendUser->pushModuleData('opendocs::recent', $this->recentDocs);
245 }
246 return $this->renderMenu($request, $response);
247 }
248
249 /**
250 * Renders the menu so that it can be returned as response to an AJAX call
251 *
252 * @param ServerRequestInterface $request
253 * @param ResponseInterface $response
254 * @return ResponseInterface
255 */
256 public function renderMenu(ServerRequestInterface $request, ResponseInterface $response)
257 {
258 $response->getBody()->write($this->getDropDown());
259 $response = $response->withHeader('Content-Type', 'html');
260 return $response;
261 }
262
263 /**
264 * Position relative to others
265 *
266 * @return int
267 */
268 public function getIndex()
269 {
270 return 30;
271 }
272
273 /**
274 * Returns the current BE user.
275 *
276 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
277 */
278 protected function getBackendUser()
279 {
280 return $GLOBALS['BE_USER'];
281 }
282
283 /**
284 * Returns current PageRenderer
285 *
286 * @return PageRenderer
287 */
288 protected function getPageRenderer()
289 {
290 return GeneralUtility::makeInstance(PageRenderer::class);
291 }
292
293 /**
294 * Returns LanguageService
295 *
296 * @return \TYPO3\CMS\Lang\LanguageService
297 */
298 protected function getLanguageService()
299 {
300 return $GLOBALS['LANG'];
301 }
302
303 }