|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PunktDe\Quickedit\Backend; |
| 6 | + |
| 7 | +/** |
| 8 | + * (c) 2020 https://punkt.de GmbH - Karlsruhe, Germany - https://punkt.de |
| 9 | + * All rights reserved. |
| 10 | + */ |
| 11 | + |
| 12 | +use TYPO3\CMS\Backend\Utility\BackendUtility; |
| 13 | +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; |
| 14 | +use TYPO3\CMS\Core\Page\PageRenderer; |
| 15 | +use TYPO3\CMS\Core\Type\Bitmask\Permission; |
| 16 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 17 | +use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
| 18 | +use TYPO3\CMS\Fluid\View\StandaloneView; |
| 19 | + |
| 20 | + |
| 21 | +class PageLayoutHeader |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var BackendUserAuthentication |
| 25 | + */ |
| 26 | + protected $backendUser; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var integer |
| 30 | + */ |
| 31 | + protected $pageUid; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + protected $pageRecord; |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + public function __construct() |
| 41 | + { |
| 42 | + $this->backendUser = $GLOBALS['BE_USER']; |
| 43 | + $this->pageUid = (int)GeneralUtility::_GET('id'); |
| 44 | + $this->pageRecord = BackendUtility::getRecord('pages', $this->pageUid); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function render(): string |
| 53 | + { |
| 54 | + if (!$this->toolbarIsEnabledForUser()) { |
| 55 | + return ''; |
| 56 | + } |
| 57 | + |
| 58 | + $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); |
| 59 | + /** @var PageRenderer $pageRenderer */ |
| 60 | + $pageRenderer->loadRequireJsModule('TYPO3/CMS/Quickedit/Quickedit'); |
| 61 | + |
| 62 | + $standaloneView = $this->initializeStandaloneView(); |
| 63 | + $standaloneView->assign('pageId', $this->pageUid); |
| 64 | + $standaloneView->assign('config', $this->getFieldConfigForPage()); |
| 65 | + $standaloneView->assign('isVisible', $this->isVisible()); |
| 66 | + |
| 67 | + return $standaloneView->render(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Checks if the toolbar is enabled or disabled. |
| 74 | + * Method checks current user setting, access rights to current page and pages in general, |
| 75 | + * user record and group records. |
| 76 | + * |
| 77 | + * @return bool |
| 78 | + */ |
| 79 | + protected function toolbarIsEnabledForUser(): bool |
| 80 | + { |
| 81 | + $isEnabled = true; |
| 82 | + |
| 83 | + if ((bool)$this->backendUser->uc['disableQuickeditInPageModule']) { |
| 84 | + $isEnabled = false; |
| 85 | + } |
| 86 | + |
| 87 | + if (!$this->backendUser->doesUserHaveAccess($this->pageRecord, Permission::PAGE_EDIT) || |
| 88 | + !$this->backendUser->check('tables_modify', 'pages')) { |
| 89 | + $isEnabled = false; |
| 90 | + } |
| 91 | + |
| 92 | + if ($this->backendUser->user['quickedit_disableToolbar']) { |
| 93 | + $isEnabled = false; |
| 94 | + } |
| 95 | + |
| 96 | + foreach ($this->backendUser->userGroups as $group) { |
| 97 | + if ($group['quickedit_disableToolbar']) { |
| 98 | + $isEnabled = false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return $isEnabled; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Initializes the view by setting template and partial paths |
| 109 | + * |
| 110 | + * @return StandaloneView |
| 111 | + */ |
| 112 | + protected function initializeStandaloneView(): StandaloneView |
| 113 | + { |
| 114 | + /** @var StandaloneView $standaloneView */ |
| 115 | + $standaloneView = GeneralUtility::makeInstance(StandaloneView::class); |
| 116 | + $templatesPath = GeneralUtility::getFileAbsFileName('EXT:quickedit/Resources/Private/Templates/Backend'); |
| 117 | + $templateFileName = 'Quickedit.html'; |
| 118 | + |
| 119 | + $standaloneView->setTemplateRootPaths( |
| 120 | + array($templatesPath) |
| 121 | + ); |
| 122 | + $standaloneView->setPartialRootPaths( |
| 123 | + array(GeneralUtility::getFileAbsFileName('EXT:quickedit/Resources/Private/Partials/Backend')) |
| 124 | + ); |
| 125 | + |
| 126 | + $standaloneView->setTemplatePathAndFilename($templatesPath . '/' . $templateFileName); |
| 127 | + |
| 128 | + return $standaloneView; |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * @return array |
| 135 | + */ |
| 136 | + protected function getFieldConfigForPage(): array |
| 137 | + { |
| 138 | + $configForPageType = $this->getConfigForCurrentPage(); |
| 139 | + |
| 140 | + if (is_array($configForPageType) && count($configForPageType) > 0) { |
| 141 | + foreach ($configForPageType as $key => &$singleConfig) { |
| 142 | + $singleConfig['fields'] = $this->prepareFieldsList($singleConfig['fields']); |
| 143 | + |
| 144 | + if ($singleConfig['fields'] === '') { |
| 145 | + unset($configForPageType[$key]); |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + if (strpos($singleConfig['label'], 'LLL') === 0) { |
| 150 | + $singleConfig['label'] = LocalizationUtility::translate($singleConfig['label']); |
| 151 | + } |
| 152 | + |
| 153 | + $this->processPreviewFields($singleConfig); |
| 154 | + } |
| 155 | + |
| 156 | + return $configForPageType; |
| 157 | + } |
| 158 | + |
| 159 | + return []; |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + /** |
| 165 | + * Get the Quickedit config for current doktype, sort groups by their number in config. |
| 166 | + * |
| 167 | + * @return array |
| 168 | + */ |
| 169 | + protected function getConfigForCurrentPage(): array |
| 170 | + { |
| 171 | + $pageTsConfig = BackendUtility::getPagesTSconfig($this->pageUid); |
| 172 | + $quickeditConfig = $pageTsConfig['mod.']['web_layout.']['PageTypes.']; |
| 173 | + $configForPageType = []; |
| 174 | + |
| 175 | + if (is_array($quickeditConfig) && array_key_exists($this->pageRecord['doktype'] . '.', $quickeditConfig)) { |
| 176 | + $configForPageType = $quickeditConfig[$this->pageRecord['doktype'] . '.']['config.']; |
| 177 | + ksort($configForPageType); |
| 178 | + } |
| 179 | + |
| 180 | + return $configForPageType; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + /** |
| 186 | + * Prepares list of configured fields, trims field names and checks access rights of backend user. |
| 187 | + * Returns a cleaned field list. |
| 188 | + * |
| 189 | + * @param $fields string |
| 190 | + * @return string |
| 191 | + */ |
| 192 | + protected function prepareFieldsList(string $fields): string |
| 193 | + { |
| 194 | + $fieldsArray = []; |
| 195 | + |
| 196 | + if ($fields !== '') { |
| 197 | + $fieldsArray = explode(',', $fields); |
| 198 | + $fieldsArray = array_map('trim', $fieldsArray); |
| 199 | + |
| 200 | + foreach ($fieldsArray as $index => $field) { |
| 201 | + if ($this->userHasAccessToField($field) === false) { |
| 202 | + unset($fieldsArray[$index]); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return implode(',', $fieldsArray); |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + /** |
| 213 | + * @param $field string |
| 214 | + * @return bool |
| 215 | + */ |
| 216 | + protected function userHasAccessToField(string $field): bool |
| 217 | + { |
| 218 | + return $field !== '' && (!array_key_exists('exclude', $GLOBALS['TCA']['pages']['columns'][$field]) || |
| 219 | + $GLOBALS['TCA']['pages']['columns'][$field]['exclude'] === 0 || |
| 220 | + $this->backendUser->check('non_exclude_fields', 'pages:' . $field)); |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + /** |
| 226 | + * Checks set previewFields and get the corresponding field labels and values for display in backend. |
| 227 | + * |
| 228 | + * @param $groupConfig array |
| 229 | + */ |
| 230 | + protected function processPreviewFields(array &$groupConfig): void |
| 231 | + { |
| 232 | + if (array_key_exists('previewFields', $groupConfig)) { |
| 233 | + $groupConfig['fieldValues'] = []; |
| 234 | + |
| 235 | + if ($groupConfig['previewFields'] === '*') { |
| 236 | + $groupConfig['previewFields'] = $groupConfig['fields']; |
| 237 | + } else { |
| 238 | + $groupConfig['previewFields'] = $this->prepareFieldsList($groupConfig['previewFields']); |
| 239 | + } |
| 240 | + |
| 241 | + $previewFieldsArray = explode(',', $groupConfig['previewFields']); |
| 242 | + |
| 243 | + foreach ($previewFieldsArray as $field) { |
| 244 | + if ($field !== '') { |
| 245 | + $groupConfig['fieldValues'][$field]['value'] = BackendUtility::getProcessedValue( |
| 246 | + 'pages', |
| 247 | + $field, |
| 248 | + $this->pageRecord[$field], |
| 249 | + 0, |
| 250 | + false, |
| 251 | + false, |
| 252 | + $this->pageUid |
| 253 | + ); |
| 254 | + |
| 255 | + $itemLabel = BackendUtility::getItemLabel('pages', $field); |
| 256 | + |
| 257 | + if (strpos($itemLabel, 'LLL') === 0) { |
| 258 | + $itemLabel = LocalizationUtility::translate($itemLabel); |
| 259 | + } |
| 260 | + |
| 261 | + $groupConfig['fieldValues'][$field]['label'] = $itemLabel; |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | + /** |
| 270 | + * Checks if user has set the toolbar to hidden by default in his user settings. |
| 271 | + * |
| 272 | + * If a user opens the toolbar the current status is saved and overrides the |
| 273 | + * default visibility of the toolbar for only that page! |
| 274 | + * |
| 275 | + * @return bool |
| 276 | + */ |
| 277 | + protected function isVisible(): bool |
| 278 | + { |
| 279 | + $isVisible = true; |
| 280 | + |
| 281 | + if (array_key_exists('quickeditDefaultHidden', $this->backendUser->uc)) { |
| 282 | + $isVisible = !(bool)$this->backendUser->uc['quickeditDefaultHidden']; |
| 283 | + } |
| 284 | + |
| 285 | + if (array_key_exists('quickedit', $this->backendUser->uc) && |
| 286 | + array_key_exists('visible', $this->backendUser->uc['quickedit']) && |
| 287 | + array_key_exists($this->pageUid, $this->backendUser->uc['quickedit']['visible'])) { |
| 288 | + $isVisible = (bool)$this->backendUser->uc['quickedit']['visible'][$this->pageUid]; |
| 289 | + } |
| 290 | + |
| 291 | + return $isVisible; |
| 292 | + } |
| 293 | +} |
0 commit comments