‪TYPO3CMS  11.5
ButtonBarHook.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;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 
38 {
39  private const ‪TABLE_NAME = 'sys_note';
40  private const ‪ALLOWED_MODULES = ['web_layout', 'web_list', 'web_info'];
41 
51  public function ‪getButtons(array $params, ‪ButtonBar $buttonBar): array
52  {
53  $buttons = $params['buttons'];
54  $request = $this->‪getRequest();
55 
56  $id = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
57  $route = $request->getAttribute('route');
58  $normalizedParams = $request->getAttribute('normalizedParams');
59  $pageTSconfig = BackendUtility::getPagesTSconfig($id);
60 
61  if (!$id
62  || $route === null
63  || $normalizedParams === null
64  || !empty($pageTSconfig['mod.']['SHARED.']['disableSysNoteButton'])
65  || !$this->‪canCreateNewRecord($id)
66  || !in_array($route->getOption('moduleName'), self::ALLOWED_MODULES, true)
67  || ($route->getOption('moduleName') === 'web_list' && !$this->isCreationAllowed($pageTSconfig['mod.']['web_list.'] ?? []))
68  ) {
69  return $buttons;
70  }
71 
72  $uri = (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
73  'record_edit',
74  [
75  'edit' => [
76  self::TABLE_NAME => [
77  $id => 'new',
78  ],
79  ],
80  'returnUrl' => $normalizedParams->getRequestUri(),
81  ]
82  );
83 
84  $buttons[‪ButtonBar::BUTTON_POSITION_RIGHT][2][] = $buttonBar
86  ->‪setTitle(htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:sys_note/Resources/Private/Language/locallang.xlf:new_internal_note')))
87  ->setIcon(GeneralUtility::makeInstance(IconFactory::class)->getIcon('sysnote-type-0', ‪Icon::SIZE_SMALL))
88  ->setHref($uri);
89 
90  ksort($buttons[‪ButtonBar::BUTTON_POSITION_RIGHT]);
91 
92  return $buttons;
93  }
94 
101  protected function ‪canCreateNewRecord(int $id): bool
102  {
103  $tableConfiguration = ‪$GLOBALS['TCA'][‪self::TABLE_NAME]['ctrl'];
104  $pageRow = BackendUtility::getRecord('pages', $id);
105  $backendUser = $this->‪getBackendUserAuthentication();
106 
107  return !($pageRow === null
108  || ($tableConfiguration['readOnly'] ?? false)
109  || ($tableConfiguration['hideTable'] ?? false)
110  || ($tableConfiguration['is_static'] ?? false)
111  || (($tableConfiguration['adminOnly'] ?? false) && !$backendUser->isAdmin())
112  || !$backendUser->doesUserHaveAccess($pageRow, ‪Permission::CONTENT_EDIT)
113  || !$backendUser->check('tables_modify', self::TABLE_NAME)
114  || !$backendUser->workspaceCanCreateNewRecord(self::TABLE_NAME));
115  }
116 
123  protected function ‪isCreationAllowed(array $modTSconfig): bool
124  {
125  $allowedNewTables = ‪GeneralUtility::trimExplode(',', $modTSconfig['allowedNewTables'] ?? '', true);
126  $deniedNewTables = ‪GeneralUtility::trimExplode(',', $modTSconfig['deniedNewTables'] ?? '', true);
127 
128  return ($allowedNewTables === [] && $deniedNewTables === [])
129  || (!in_array(self::TABLE_NAME, $deniedNewTables)
130  && ($allowedNewTables === [] || in_array(self::TABLE_NAME, $allowedNewTables)));
131  }
132 
133  protected function ‪getRequest(): ServerRequestInterface
134  {
135  return ‪$GLOBALS['TYPO3_REQUEST'];
136  }
137 
139  {
140  return ‪$GLOBALS['BE_USER'];
141  }
142 
144  {
145  return ‪$GLOBALS['LANG'];
146  }
147 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:32
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook
Definition: ButtonBarHook.php:38
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\isCreationAllowed
‪bool isCreationAllowed(array $modTSconfig)
Definition: ButtonBarHook.php:123
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\canCreateNewRecord
‪bool canCreateNewRecord(int $id)
Definition: ButtonBarHook.php:101
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\getButtons
‪array getButtons(array $params, ButtonBar $buttonBar)
Definition: ButtonBarHook.php:51
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\SysNote\Hook
Definition: ButtonBarHook.php:18
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\getLanguageService
‪getLanguageService()
Definition: ButtonBarHook.php:143
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\ALLOWED_MODULES
‪const ALLOWED_MODULES
Definition: ButtonBarHook.php:40
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\makeLinkButton
‪LinkButton makeLinkButton()
Definition: ButtonBar.php:121
‪TYPO3\CMS\Core\Type\Bitmask\Permission\CONTENT_EDIT
‪const CONTENT_EDIT
Definition: Permission.php:55
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\getBackendUserAuthentication
‪getBackendUserAuthentication()
Definition: ButtonBarHook.php:138
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\TABLE_NAME
‪const TABLE_NAME
Definition: ButtonBarHook.php:39
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Template\Components\AbstractControl\setTitle
‪$this setTitle($title)
Definition: AbstractControl.php:128
‪TYPO3\CMS\SysNote\Hook\ButtonBarHook\getRequest
‪getRequest()
Definition: ButtonBarHook.php:133
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_RIGHT
‪const BUTTON_POSITION_RIGHT
Definition: ButtonBar.php:41