‪TYPO3CMS  ‪main
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;
21 use Psr\Http\Message\ServerRequestInterface;
26 use TYPO3\CMS\Backend\Utility\BackendUtility;
30 
37 {
38  private ServerRequestInterface ‪$request;
39 
40  public function ‪__construct(
41  private readonly ‪OpenDocumentService $documentService,
42  private readonly ‪UriBuilder $uriBuilder,
43  private readonly ‪BackendViewFactory $backendViewFactory,
44  ) {}
45 
46  public function ‪setRequest(ServerRequestInterface ‪$request): void
47  {
48  $this->request = ‪$request;
49  }
50 
54  public function ‪checkAccess(): bool
55  {
56  return !(bool)($this->‪getBackendUser()->getTSConfig()['backendToolbarItem.']['tx_opendocs.']['disabled'] ?? false);
57  }
58 
62  public function ‪getItem(): string
63  {
64  $view = $this->backendViewFactory->create($this->request, ['typo3/cms-opendocs']);
65  return $view->render('ToolbarItems/ToolbarItem');
66  }
67 
71  public function ‪hasDropDown(): bool
72  {
73  return true;
74  }
75 
79  public function ‪getDropDown(): string
80  {
81  $view = $this->backendViewFactory->create($this->request, ['typo3/cms-opendocs']);
82  $view->assignMultiple([
83  'openDocuments' => $this->‪getMenuEntries($this->documentService->getOpenDocuments()),
84  // If there are "recent documents" in the list, add them
85  'recentDocuments' => $this->getMenuEntries($this->documentService->getRecentDocuments()),
86  ]);
87  return $view->render('ToolbarItems/DropDown');
88  }
89 
93  public function ‪getAdditionalAttributes(): array
94  {
95  return [];
96  }
97 
101  public function ‪getIndex(): int
102  {
103  return 30;
104  }
105 
110  public function ‪updateNumberOfOpenDocsHook(array &$params): void
111  {
113  'typo3:opendocs:updateRequested',
114  null,
115  true
116  );
117  }
118 
122  protected function ‪getMenuEntries(array $documents): array
123  {
124  $entries = [];
125  foreach ($documents as ‪$identifier => $document) {
126  $menuEntry = $this->‪getMenuEntry($document, ‪$identifier);
127  if (!empty($menuEntry)) {
128  $entries[] = $menuEntry;
129  }
130  }
131  return $entries;
132  }
133 
139  protected function ‪getMenuEntry(array $document, string ‪$identifier): array
140  {
141  $table = $document[3]['table'] ?? '';
142  ‪$uid = $document[3]['uid'] ?? 0;
143 
144  try {
145  ‪$record = BackendUtility::getRecordWSOL($table, ‪$uid);
146  } catch (TableNotFoundException) {
147  // This exception is caught in cases, when you have an recently opened document
148  // from an extension record (let's say a sys_note record) and then uninstall
149  // the extension and drop the DB table. After then, the DB table could
150  // not be found anymore and will throw an exception making the
151  // whole backend unusable.
152  ‪$record = null;
153  }
154 
155  if (!is_array(‪$record)) {
156  // Record seems to be deleted
157  return [];
158  }
159 
160  $result = [];
161  $result['table'] = $table;
162  $result['record'] = ‪$record;
163  $result['label'] = strip_tags(htmlspecialchars_decode($document[0]));
164  $uri = $this->uriBuilder->buildUriFromRoute('record_edit', ['returnUrl' => $document[4] ?? null]) . '&' . $document[2];
165  $pid = (int)$document[3]['pid'];
166 
167  if ($document[3]['table'] === 'pages') {
168  $pid = (int)$document[3]['uid'];
169  }
170 
171  $result['pid'] = $pid;
172  $result['uri'] = $uri;
173  $result['md5sum'] = ‪$identifier;
174 
175  return $result;
176  }
177 
179  {
180  return ‪$GLOBALS['BE_USER'];
181  }
182 }
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem
Definition: OpendocsToolbarItem.php:37
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\__construct
‪__construct(private readonly OpenDocumentService $documentService, private readonly UriBuilder $uriBuilder, private readonly BackendViewFactory $backendViewFactory,)
Definition: OpendocsToolbarItem.php:40
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getAdditionalAttributes
‪getAdditionalAttributes()
Definition: OpendocsToolbarItem.php:93
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems
Definition: OpendocsToolbarItem.php:18
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getMenuEntries
‪getMenuEntries(array $documents)
Definition: OpendocsToolbarItem.php:122
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: OpendocsToolbarItem.php:46
‪TYPO3\CMS\Backend\Domain\Model\Element\ImmediateActionElement\dispatchCustomEvent
‪static dispatchCustomEvent(string $name, array $details=null, bool $useTop=false)
Definition: ImmediateActionElement.php:53
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getDropDown
‪getDropDown()
Definition: OpendocsToolbarItem.php:79
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\hasDropDown
‪hasDropDown()
Definition: OpendocsToolbarItem.php:71
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getMenuEntry
‪array getMenuEntry(array $document, string $identifier)
Definition: OpendocsToolbarItem.php:139
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Opendocs\Service\OpenDocumentService
Definition: OpenDocumentService.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Toolbar\RequestAwareToolbarItemInterface
Definition: RequestAwareToolbarItemInterface.php:27
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\$request
‪ServerRequestInterface $request
Definition: OpendocsToolbarItem.php:38
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getIndex
‪getIndex()
Definition: OpendocsToolbarItem.php:101
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getItem
‪getItem()
Definition: OpendocsToolbarItem.php:62
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\checkAccess
‪checkAccess()
Definition: OpendocsToolbarItem.php:54
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\updateNumberOfOpenDocsHook
‪updateNumberOfOpenDocsHook(array &$params)
Definition: OpendocsToolbarItem.php:110
‪TYPO3\CMS\Backend\Domain\Model\Element\ImmediateActionElement
Definition: ImmediateActionElement.php:28
‪TYPO3\CMS\Opendocs\Backend\ToolbarItems\OpendocsToolbarItem\getBackendUser
‪getBackendUser()
Definition: OpendocsToolbarItem.php:178
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37