‪TYPO3CMS  11.5
OpendocsToolbarItem.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Exception\TableNotFoundException;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 
35 {
39  protected ‪$documentService;
40 
47  {
48  $this->documentService = ‪$documentService ?: GeneralUtility::makeInstance(OpenDocumentService::class);
49  }
50 
56  public function ‪checkAccess(): bool
57  {
58  return !(bool)($this->‪getBackendUser()->‪getTSConfig()['backendToolbarItem.']['tx_opendocs.']['disabled'] ?? false);
59  }
60 
66  public function ‪getItem()
67  {
68  $view = $this->‪getFluidTemplateObject('ToolbarItem.html');
69 
70  return $view->render();
71  }
72 
78  public function ‪hasDropDown()
79  {
80  return true;
81  }
82 
88  public function ‪getDropDown()
89  {
90  $view = $this->‪getFluidTemplateObject('DropDown.html');
91  $view->assignMultiple([
92  'openDocuments' => $this->‪getMenuEntries($this->documentService->getOpenDocuments()),
93  // If there are "recent documents" in the list, add them
94  'recentDocuments' => $this->getMenuEntries($this->documentService->getRecentDocuments()),
95  ]);
96 
97  return $view->render();
98  }
99 
105  public function ‪getAdditionalAttributes()
106  {
107  return [];
108  }
109 
115  public function ‪getIndex()
116  {
117  return 30;
118  }
119 
126  public function ‪updateNumberOfOpenDocsHook(array &$params)
127  {
129  'typo3:opendocs:updateRequested',
130  null,
131  true
132  );
133  }
134 
141  protected function ‪getMenuEntries(array $documents): array
142  {
143  $entries = [];
144 
145  foreach ($documents as $identifier => $document) {
146  $menuEntry = $this->‪getMenuEntry($document, $identifier);
147 
148  if (!empty($menuEntry)) {
149  $entries[] = $menuEntry;
150  }
151  }
152 
153  return $entries;
154  }
155 
163  protected function ‪getMenuEntry(array $document, string $identifier): array
164  {
165  $table = $document[3]['table'] ?? '';
166  $uid = $document[3]['uid'] ?? 0;
167 
168  try {
169  $record = BackendUtility::getRecordWSOL($table, $uid);
170  } catch (TableNotFoundException $e) {
171  // This exception is caught in cases, when you have an recently opened document
172  // from an extension record (let's say a sys_note record) and then uninstall
173  // the extension and drop the DB table. After then, the DB table could
174  // not be found anymore and will throw an exception making the
175  // whole backend unusable.
176  $record = null;
177  }
178 
179  if (!is_array($record)) {
180  // Record seems to be deleted
181  return [];
182  }
183 
184  $result = [];
185  $result['table'] = $table;
186  $result['record'] = $record;
187  $result['label'] = strip_tags(htmlspecialchars_decode($document[0]));
188  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
189  $uri = (string)$uriBuilder->buildUriFromRoute('record_edit') . '&' . $document[2];
190  $pid = (int)$document[3]['pid'];
191 
192  if ($document[3]['table'] === 'pages') {
193  $pid = (int)$document[3]['uid'];
194  }
195 
196  $result['pid'] = $pid;
197  $result['uri'] = $uri;
198  $result['md5sum'] = $identifier;
199 
200  return $result;
201  }
202 
209  protected function ‪getFluidTemplateObject(string $filename): ‪StandaloneView
210  {
211  $view = GeneralUtility::makeInstance(StandaloneView::class);
212  $view->setLayoutRootPaths(['EXT:opendocs/Resources/Private/Layouts']);
213  $view->setPartialRootPaths([
214  'EXT:backend/Resources/Private/Partials/ToolbarItems',
215  'EXT:opendocs/Resources/Private/Partials/ToolbarItems',
216  ]);
217  $view->setTemplateRootPaths(['EXT:opendocs/Resources/Private/Templates/ToolbarItems']);
218  $view->setTemplate($filename);
219  $view->getRequest()->setControllerExtensionName('Opendocs');
220 
221  return $view;
222  }
223 
227  protected function ‪getBackendUser(): ‪BackendUserAuthentication
228  {
229  return ‪$GLOBALS['BE_USER'];
230  }
231 }
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem
Definition: OpendocsToolbarItem.php:35
‪TYPO3\CMS\Backend\Domain\Model\Element\ImmediateActionElement\dispatchCustomEvent
‪static dispatchCustomEvent(string $name, ?array $details=null, bool $useTop=false)
Definition: ImmediateActionElement.php:55
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\checkAccess
‪bool checkAccess()
Definition: OpendocsToolbarItem.php:55
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems
Definition: OpendocsToolbarItem.php:18
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig()
Definition: BackendUserAuthentication.php:1000
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getIndex
‪int getIndex()
Definition: OpendocsToolbarItem.php:114
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getDropDown
‪string getDropDown()
Definition: OpendocsToolbarItem.php:87
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\hasDropDown
‪bool hasDropDown()
Definition: OpendocsToolbarItem.php:77
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getItem
‪string getItem()
Definition: OpendocsToolbarItem.php:65
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\$documentService
‪OpenDocumentService $documentService
Definition: OpendocsToolbarItem.php:38
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getMenuEntry
‪array getMenuEntry(array $document, string $identifier)
Definition: OpendocsToolbarItem.php:162
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Opendocs\Service\OpenDocumentService
Definition: OpenDocumentService.php:27
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\__construct
‪__construct(?OpenDocumentService $documentService=null)
Definition: OpendocsToolbarItem.php:45
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename)
Definition: OpendocsToolbarItem.php:208
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: OpendocsToolbarItem.php:226
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\updateNumberOfOpenDocsHook
‪updateNumberOfOpenDocsHook(array &$params)
Definition: OpendocsToolbarItem.php:125
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Domain\Model\Element\ImmediateActionElement
Definition: ImmediateActionElement.php:28
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getMenuEntries
‪array getMenuEntries(array $documents)
Definition: OpendocsToolbarItem.php:140
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getAdditionalAttributes
‪array getAdditionalAttributes()
Definition: OpendocsToolbarItem.php:104