‪TYPO3CMS  ‪main
ButtonBarProvider.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 Psr\Http\Message\ServerRequestInterface;
25 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 use TYPO3\CMS\Core\Imaging\IconSize;
33 
40 {
41  private const ‪TABLE_NAME = 'sys_note';
42  private const ‪ALLOWED_MODULES = [
43  'web_layout',
44  'web_list',
45  'web_info_overview',
46  'web_info_translations',
47  'web_info_pagets',
48  ];
49 
55  #[AsEventListener('note-to-button-bar')]
56  public function ‪__invoke(‪ModifyButtonBarEvent $event): void
57  {
58  $buttons = $event->‪getButtons();
59  $request = $this->‪getRequest();
60 
61  $id = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
62  $module = $request->getAttribute('module');
63  $normalizedParams = $request->getAttribute('normalizedParams');
64  $pageTSconfig = BackendUtility::getPagesTSconfig($id);
65 
66  if (!$id
67  || $module === null
68  || $normalizedParams === null
69  || !empty($pageTSconfig['mod.']['SHARED.']['disableSysNoteButton'])
70  || !$this->‪canCreateNewRecord($id)
71  || !in_array($module->getIdentifier(), self::ALLOWED_MODULES, true)
72  || ($module->getIdentifier() === 'web_list' && !$this->isCreationAllowed($pageTSconfig['mod.']['web_list.'] ?? []))
73  ) {
74  return;
75  }
76 
77  $uri = (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
78  'record_edit',
79  [
80  'edit' => [
81  self::TABLE_NAME => [
82  $id => 'new',
83  ],
84  ],
85  'returnUrl' => $normalizedParams->getRequestUri(),
86  ]
87  );
88 
89  $buttons[‪ButtonBar::BUTTON_POSITION_RIGHT][2][] = $event->‪getButtonBar()
90  ->makeLinkButton()
91  ->setTitle(htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:sys_note/Resources/Private/Language/locallang.xlf:new_internal_note')))
92  ->setIcon(GeneralUtility::makeInstance(IconFactory::class)->getIcon('sysnote-type-0', IconSize::SMALL))
93  ->setHref($uri);
94 
95  ksort($buttons[‪ButtonBar::BUTTON_POSITION_RIGHT]);
96 
97  $event->‪setButtons($buttons);
98  }
99 
103  protected function ‪canCreateNewRecord(int $id): bool
104  {
105  $tableConfiguration = ‪$GLOBALS['TCA'][‪self::TABLE_NAME]['ctrl'];
106  $pageRow = BackendUtility::getRecord('pages', $id);
107  $backendUser = $this->‪getBackendUserAuthentication();
108 
109  return !($pageRow === null
110  || ($tableConfiguration['readOnly'] ?? false)
111  || ($tableConfiguration['hideTable'] ?? false)
112  || ($tableConfiguration['is_static'] ?? false)
113  || (($tableConfiguration['adminOnly'] ?? false) && !$backendUser->isAdmin())
114  || !$backendUser->doesUserHaveAccess($pageRow, ‪Permission::CONTENT_EDIT)
115  || !$backendUser->check('tables_modify', self::TABLE_NAME)
116  || !$backendUser->workspaceCanCreateNewRecord(self::TABLE_NAME));
117  }
118 
122  protected function ‪isCreationAllowed(array $modTSconfig): bool
123  {
124  $allowedNewTables = ‪GeneralUtility::trimExplode(',', $modTSconfig['allowedNewTables'] ?? '', true);
125  $deniedNewTables = ‪GeneralUtility::trimExplode(',', $modTSconfig['deniedNewTables'] ?? '', true);
126 
127  return ($allowedNewTables === [] && $deniedNewTables === [])
128  || (!in_array(self::TABLE_NAME, $deniedNewTables)
129  && ($allowedNewTables === [] || in_array(self::TABLE_NAME, $allowedNewTables)));
130  }
131 
132  protected function ‪getRequest(): ServerRequestInterface
133  {
134  return ‪$GLOBALS['TYPO3_REQUEST'];
135  }
136 
138  {
139  return ‪$GLOBALS['BE_USER'];
140  }
141 
143  {
144  return ‪$GLOBALS['LANG'];
145  }
146 }
‪TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent
Definition: ModifyButtonBarEvent.php:26
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:33
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\TABLE_NAME
‪const TABLE_NAME
Definition: ButtonBarProvider.php:41
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\getBackendUserAuthentication
‪getBackendUserAuthentication()
Definition: ButtonBarProvider.php:137
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider
Definition: ButtonBarProvider.php:40
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent\setButtons
‪setButtons(array $buttons)
Definition: ModifyButtonBarEvent.php:37
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\isCreationAllowed
‪isCreationAllowed(array $modTSconfig)
Definition: ButtonBarProvider.php:122
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\__invoke
‪__invoke(ModifyButtonBarEvent $event)
Definition: ButtonBarProvider.php:56
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\ALLOWED_MODULES
‪const ALLOWED_MODULES
Definition: ButtonBarProvider.php:42
‪TYPO3\CMS\Core\Type\Bitmask\Permission\CONTENT_EDIT
‪const CONTENT_EDIT
Definition: Permission.php:55
‪TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent\getButtons
‪getButtons()
Definition: ModifyButtonBarEvent.php:32
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\canCreateNewRecord
‪canCreateNewRecord(int $id)
Definition: ButtonBarProvider.php:103
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\getLanguageService
‪getLanguageService()
Definition: ButtonBarProvider.php:142
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_RIGHT
‪const BUTTON_POSITION_RIGHT
Definition: ButtonBar.php:42
‪TYPO3\CMS\SysNote\Provider\ButtonBarProvider\getRequest
‪getRequest()
Definition: ButtonBarProvider.php:132
‪TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent\getButtonBar
‪getButtonBar()
Definition: ModifyButtonBarEvent.php:42
‪TYPO3\CMS\SysNote\Provider
Definition: ButtonBarProvider.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822