TYPO3 CMS  TYPO3_8-7
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 
24 
31 {
35  protected $openDocs = [];
36 
40  protected $recentDocs = [];
41 
45  public function __construct()
46  {
47  $this->loadDocsFromUserSession();
48  }
49 
55  public function checkAccess()
56  {
57  $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
58  return (int)$conf['value'] !== 1;
59  }
60 
64  public function loadDocsFromUserSession()
65  {
66  $backendUser = $this->getBackendUser();
67  $openDocs = $backendUser->getModuleData('FormEngine', 'ses');
68  if ($openDocs !== null) {
69  list($this->openDocs, ) = $openDocs;
70  }
71  $this->recentDocs = $backendUser->getModuleData('opendocs::recent') ?: [];
72  }
73 
79  public function getItem()
80  {
81  $view = $this->getFluidTemplateObject('ToolbarItem.html');
82  $view->assign('numDocs', count($this->openDocs));
83  return $view->render();
84  }
85 
91  public function getDropDown()
92  {
93  $view = $this->getFluidTemplateObject('DropDown.html');
94  $view->assignMultiple([
95  'openDocuments' => $this->getMenuEntries($this->openDocs),
96  // If there are "recent documents" in the list, add them
97  'recentDocuments' => $this->getMenuEntries($this->recentDocs)
98  ]);
99  return $view->render();
100  }
101 
108  protected function getMenuEntries(array $documents): array
109  {
110  $entries = [];
111  foreach ($documents as $md5sum => $document) {
112  $menuEntry = $this->getMenuEntry($document, $md5sum);
113  if (is_array($menuEntry)) {
114  $entries[] = $menuEntry;
115  }
116  }
117  return $entries;
118  }
119 
127  protected function getMenuEntry($document, $md5sum)
128  {
129  $table = $document[3]['table'];
130  $uid = $document[3]['uid'];
131  $record = BackendUtility::getRecordWSOL($table, $uid);
132  if (!is_array($record)) {
133  // Record seems to be deleted
134  return null;
135  }
136  $result = [];
137  $result['table'] = $table;
138  $result['record'] = $record;
139  $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
140  $result['label'] = $label;
141  $link = BackendUtility::getModuleUrl('record_edit') . '&' . $document[2];
142  $pageId = (int)$document[3]['uid'];
143  if ($document[3]['table'] !== 'pages') {
144  $pageId = (int)$document[3]['pid'];
145  }
146  $onClickCode = 'jump(' . GeneralUtility::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
147  $result['onClickCode'] = $onClickCode;
148  $result['md5sum'] = $md5sum;
149  return $result;
150  }
151 
157  public function getAdditionalAttributes()
158  {
159  return [];
160  }
161 
167  public function hasDropDown()
168  {
169  return true;
170  }
171 
172  /*******************
173  *** HOOKS ***
174  *******************/
181  public function updateNumberOfOpenDocsHook(&$params)
182  {
183  $params['JScode'] = '
184  if (top && top.TYPO3.OpendocsMenu) {
185  top.TYPO3.OpendocsMenu.updateMenu();
186  }
187  ';
188  }
189 
190  /******************
191  *** AJAX CALLS ***
192  ******************/
200  public function closeDocument(ServerRequestInterface $request, ResponseInterface $response)
201  {
202  $md5sum = isset($request->getParsedBody()['md5sum']) ? $request->getParsedBody()['md5sum'] : $request->getQueryParams()['md5sum'];
203  if ($md5sum && isset($this->openDocs[$md5sum])) {
204  $backendUser = $this->getBackendUser();
205  // Add the document to be closed to the recent documents
206  $this->recentDocs = array_merge([$md5sum => $this->openDocs[$md5sum]], $this->recentDocs);
207  // Allow a maximum of 8 recent documents
208  if (count($this->recentDocs) > 8) {
209  $this->recentDocs = array_slice($this->recentDocs, 0, 8);
210  }
211  // Remove it from the list of the open documents, and store the status
212  unset($this->openDocs[$md5sum]);
213  list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
214  $backendUser->pushModuleData('FormEngine', [$this->openDocs, $docDat]);
215  $backendUser->pushModuleData('opendocs::recent', $this->recentDocs);
216  }
217  return $this->renderMenu($request, $response);
218  }
219 
227  public function renderMenu(ServerRequestInterface $request, ResponseInterface $response)
228  {
229  $response->getBody()->write($this->getDropDown());
230  return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
231  }
232 
238  public function getIndex()
239  {
240  return 30;
241  }
242 
248  protected function getBackendUser()
249  {
250  return $GLOBALS['BE_USER'];
251  }
252 
259  protected function getFluidTemplateObject(string $filename): StandaloneView
260  {
261  $view = GeneralUtility::makeInstance(StandaloneView::class);
262  $view->setLayoutRootPaths(['EXT:opendocs/Resources/Private/Layouts']);
263  $view->setPartialRootPaths([
264  'EXT:backend/Resources/Private/Partials/ToolbarItems',
265  'EXT:opendocs/Resources/Private/Partials/ToolbarItems'
266  ]);
267  $view->setTemplateRootPaths(['EXT:opendocs/Resources/Private/Templates/ToolbarItems']);
268 
269  $view->setTemplate($filename);
270 
271  $view->getRequest()->setControllerExtensionName('Opendocs');
272  return $view;
273  }
274 }
static getRecordWSOL( $table, $uid, $fields=' *', $where='', $useDeleteClause=true, $unsetMovePointers=false)
renderMenu(ServerRequestInterface $request, ResponseInterface $response)
static makeInstance($className,... $constructorArguments)
closeDocument(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']