TYPO3  7.6
OpendocsToolbarItem.php
Go to the documentation of this file.
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 
24 
29 {
33  protected $openDocs;
34 
38  protected $recentDocs;
39 
43  protected $iconFactory;
44 
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 
62  public function checkAccess()
63  {
64  $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
65  return $conf['value'] != 1;
66  }
67 
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 
85  public function getItem()
86  {
87  $numDocs = count($this->openDocs);
88  $title = $this->getLanguageService()->getLL('toolbaritem', true);
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 
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">' . $languageService->getLL('open_docs', true) . '</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">' . $languageService->getLL('recent_docs', true) . '</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>' . $languageService->getLL('no_docs', true) . '</p>';
130  }
131  return $content;
132  }
133 
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 = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', true);
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 
184  public function getAdditionalAttributes()
185  {
186  return array();
187  }
188 
194  public function hasDropDown()
195  {
196  return true;
197  }
198 
199 
200  /*******************
201  *** HOOKS ***
202  *******************/
211  public function updateNumberOfOpenDocsHook(&$params, $ref)
212  {
213  $params['JScode'] = '
214  if (top && top.TYPO3.OpendocsMenu) {
215  top.TYPO3.OpendocsMenu.updateMenu();
216  }
217  ';
218  }
219 
220  /******************
221  *** AJAX CALLS ***
222  ******************/
231  {
232  $backendUser = $this->getBackendUser();
233  $md5sum = isset($request->getParsedBody()['md5sum']) ? $request->getParsedBody()['md5sum'] : $request->getQueryParams()['md5sum'];
234  if ($md5sum && isset($this->openDocs[$md5sum])) {
235  // Add the document to be closed to the recent documents
236  $this->recentDocs = array_merge(array($md5sum => $this->openDocs[$md5sum]), $this->recentDocs);
237  // Allow a maximum of 8 recent documents
238  if (count($this->recentDocs) > 8) {
239  $this->recentDocs = array_slice($this->recentDocs, 0, 8);
240  }
241  // Remove it from the list of the open documents, and store the status
242  unset($this->openDocs[$md5sum]);
243  list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
244  $backendUser->pushModuleData('FormEngine', array($this->openDocs, $docDat));
245  $backendUser->pushModuleData('opendocs::recent', $this->recentDocs);
246  }
247  return $this->renderMenu($request, $response);
248  }
249 
258  {
259  $response->getBody()->write($this->getDropDown());
260  $response = $response->withHeader('Content-Type', 'html');
261  return $response;
262  }
263 
269  public function getIndex()
270  {
271  return 30;
272  }
273 
279  protected function getBackendUser()
280  {
281  return $GLOBALS['BE_USER'];
282  }
283 
289  protected function getPageRenderer()
290  {
291  return GeneralUtility::makeInstance(PageRenderer::class);
292  }
293 
299  protected function getLanguageService()
300  {
301  return $GLOBALS['LANG'];
302  }
303 
309  protected function getDatabaseConnection()
310  {
311  return $GLOBALS['TYPO3_DB'];
312  }
313 }