TYPO3 CMS  TYPO3_7-6
OpendocsToolbarItem.php
Go to the documentation of this file.
1 <?php
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 
25 
30 {
34  protected $openDocs = [];
35 
39  protected $recentDocs = [];
40 
44  protected $iconFactory;
45 
49  public function __construct()
50  {
51  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
52  $this->getLanguageService()->includeLLFile('EXT:opendocs/Resources/Private/Language/locallang.xlf');
53  $this->loadDocsFromUserSession();
54  $pageRenderer = $this->getPageRenderer();
55  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Opendocs/Toolbar/OpendocsMenu');
56  }
57 
63  public function checkAccess()
64  {
65  $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
66  return $conf['value'] != 1;
67  }
68 
74  public function loadDocsFromUserSession()
75  {
76  $backendUser = $this->getBackendUser();
77  $openDocs = $backendUser->getModuleData('FormEngine', 'ses');
78  if ($openDocs !== null) {
79  list($this->openDocs, ) = $openDocs;
80  }
81  $this->recentDocs = $backendUser->getModuleData('opendocs::recent') ?: [];
82  }
83 
89  public function getItem()
90  {
91  $numDocs = count($this->openDocs);
92  $title = $this->getLanguageService()->getLL('toolbaritem', true);
93 
94  $opendocsMenu = [];
95  $opendocsMenu[] = '<span title="' . $title . '">' . $this->iconFactory->getIcon('apps-toolbar-menu-opendocs', Icon::SIZE_SMALL)->render('inline') . '</span>';
96  $opendocsMenu[] = '<span class="badge" id="tx-opendocs-counter">' . $numDocs . '</span>';
97 
98  return implode(LF, $opendocsMenu);
99  }
100 
106  public function getDropDown()
107  {
108  $languageService = $this->getLanguageService();
109  $openDocuments = $this->openDocs;
110  $recentDocuments = $this->recentDocs;
111  $entries = [];
112  if (!empty($openDocuments)) {
113  $entries[] = '<li class="dropdown-header">' . $languageService->getLL('open_docs', true) . '</li>';
114  $i = 0;
115  foreach ($openDocuments as $md5sum => $openDocument) {
116  $i++;
117  $entries[] = $this->renderMenuEntry($openDocument, $md5sum, false, $i == 1);
118  }
119  $entries[] = '<li class="divider"></li>';
120  }
121  // If there are "recent documents" in the list, add them
122  if (!empty($recentDocuments)) {
123  $entries[] = '<li class="dropdown-header">' . $languageService->getLL('recent_docs', true) . '</li>';
124  $i = 0;
125  foreach ($recentDocuments as $md5sum => $recentDocument) {
126  $i++;
127  $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, true, $i == 1);
128  }
129  }
130  if (!empty($entries)) {
131  $content = '<ul class="dropdown-list">' . implode('', $entries) . '</ul>';
132  } else {
133  $content = '<p>' . $languageService->getLL('no_docs', true) . '</p>';
134  }
135  return $content;
136  }
137 
147  protected function renderMenuEntry($document, $md5sum, $isRecentDoc = false, $isFirstDoc = false)
148  {
149  $table = $document[3]['table'];
150  $uid = $document[3]['uid'];
151  $record = BackendUtility::getRecordWSOL($table, $uid);
152  if (!is_array($record)) {
153  // Record seems to be deleted
154  return '';
155  }
156  $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
157  $icon = $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render();
158  $link = BackendUtility::getModuleUrl('record_edit')
159  . '&' . $document[2]
160  . '&returnUrl=' . rawurlencode(BackendUtility::getModuleUrl('web_list') . '&id=' . (int)$document[3]['pid']);
161  $pageId = (int)$document[3]['uid'];
162  if ($document[3]['table'] !== 'pages') {
163  $pageId = (int)$document[3]['pid'];
164  }
165  $onClickCode = 'jump(' . GeneralUtility::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
166  if (!$isRecentDoc) {
167  $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', true);
168  // Open document
169  $closeIcon = $this->iconFactory->getIcon('actions-close', Icon::SIZE_SMALL)->render('inline');
170  $entry = '
171  <li class="opendoc">
172  <a href="#" class="dropdown-list-link dropdown-link-list-add-close" onclick="' . htmlspecialchars($onClickCode) . '" target="content">' . $icon . ' ' . $label . '</a>
173  <a href="#" class="dropdown-list-link-close" data-opendocsidentifier="' . $md5sum . '" title="' . $title . '">' . $closeIcon . '</a>
174  </li>';
175  } else {
176  // Recently used document
177  $entry = '
178  <li>
179  <a href="#" class="dropdown-list-link" onclick="' . htmlspecialchars($onClickCode) . '" target="content">' . $icon . ' ' . $label . '</a>
180  </li>';
181  }
182  return $entry;
183  }
184 
190  public function getAdditionalAttributes()
191  {
192  return [];
193  }
194 
200  public function hasDropDown()
201  {
202  return true;
203  }
204 
205  /*******************
206  *** HOOKS ***
207  *******************/
216  public function updateNumberOfOpenDocsHook(&$params, $ref)
217  {
218  $params['JScode'] = '
219  if (top && top.TYPO3.OpendocsMenu) {
220  top.TYPO3.OpendocsMenu.updateMenu();
221  }
222  ';
223  }
224 
225  /******************
226  *** AJAX CALLS ***
227  ******************/
235  public function closeDocument(ServerRequestInterface $request, ResponseInterface $response)
236  {
237  $backendUser = $this->getBackendUser();
238  $md5sum = isset($request->getParsedBody()['md5sum']) ? $request->getParsedBody()['md5sum'] : $request->getQueryParams()['md5sum'];
239  if ($md5sum && isset($this->openDocs[$md5sum])) {
240  // Add the document to be closed to the recent documents
241  $this->recentDocs = array_merge([$md5sum => $this->openDocs[$md5sum]], $this->recentDocs);
242  // Allow a maximum of 8 recent documents
243  if (count($this->recentDocs) > 8) {
244  $this->recentDocs = array_slice($this->recentDocs, 0, 8);
245  }
246  // Remove it from the list of the open documents, and store the status
247  unset($this->openDocs[$md5sum]);
248  list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
249  $backendUser->pushModuleData('FormEngine', [$this->openDocs, $docDat]);
250  $backendUser->pushModuleData('opendocs::recent', $this->recentDocs);
251  }
252  return $this->renderMenu($request, $response);
253  }
254 
262  public function renderMenu(ServerRequestInterface $request, ResponseInterface $response)
263  {
264  $response->getBody()->write($this->getDropDown());
265  $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
266  return $response;
267  }
268 
274  public function getIndex()
275  {
276  return 30;
277  }
278 
284  protected function getBackendUser()
285  {
286  return $GLOBALS['BE_USER'];
287  }
288 
294  protected function getPageRenderer()
295  {
296  return GeneralUtility::makeInstance(PageRenderer::class);
297  }
298 
304  protected function getLanguageService()
305  {
306  return $GLOBALS['LANG'];
307  }
308 
314  protected function getDatabaseConnection()
315  {
316  return $GLOBALS['TYPO3_DB'];
317  }
318 }
renderMenu(ServerRequestInterface $request, ResponseInterface $response)
closeDocument(ServerRequestInterface $request, ResponseInterface $response)
renderMenuEntry($document, $md5sum, $isRecentDoc=false, $isFirstDoc=false)
$uid
Definition: server.php:38
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static getRecordWSOL($table, $uid, $fields=' *', $where='', $useDeleteClause=true, $unsetMovePointers=false)