‪TYPO3CMS  ‪main
PageLayoutContext.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;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
37 
42 {
44  protected ?array ‪$localizedPageRecord = null;
45  protected int ‪$pageId;
46 
50  protected array ‪$siteLanguages = [];
52 
57  protected array ‪$contentTypeLabels = [];
58 
68  protected array ‪$itemLabels = [];
69 
70  public function ‪__construct(
71  protected readonly array $pageRecord,
72  protected readonly ‪BackendLayout $backendLayout,
73  protected readonly ‪SiteInterface $site,
74  protected readonly ‪DrawingConfiguration $drawingConfiguration,
75  protected readonly ServerRequestInterface $request
76  ) {
77  $this->pageId = (int)($pageRecord['uid'] ?? 0);
78  $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, $this);
79  $this->siteLanguages = $this->site->getAvailableLanguages($this->‪getBackendUser(), true, $this->pageId);
80  $this->siteLanguage = $this->site->getDefaultLanguage();
81  }
82 
83  public function ‪cloneForLanguage(‪SiteLanguage $language): self
84  {
85  $copy = clone $this;
86  $copy->setSiteLanguage($language);
87  return $copy;
88  }
89 
91  {
92  $this->siteLanguage = ‪$siteLanguage;
93  $languageId = ‪$siteLanguage->‪getLanguageId();
94  if ($languageId > 0) {
95  $pageLocalizationRecord = BackendUtility::getRecordLocalization(
96  'pages',
97  $this->‪getPageId(),
98  $languageId
99  );
100  $pageLocalizationRecord = reset($pageLocalizationRecord);
101  if (!empty($pageLocalizationRecord)) {
102  BackendUtility::workspaceOL('pages', $pageLocalizationRecord);
103  $this->localizedPageRecord = $pageLocalizationRecord ?: null;
104  }
105  }
106  }
107 
109  {
110  return $this->backendLayout;
111  }
112 
114  {
115  return $this->drawingConfiguration;
116  }
117 
119  {
120  return ‪$GLOBALS['BE_USER'];
121  }
122 
123  public function ‪getPageRecord(): array
124  {
125  return $this->pageRecord;
126  }
127 
128  public function ‪getPageId(): int
129  {
130  return ‪$this->pageId;
131  }
132 
136  public function ‪getSiteLanguages(): iterable
137  {
139  }
140 
144  public function ‪getLanguagesToShow(): iterable
145  {
146  $selectedLanguageId = $this->drawingConfiguration->getSelectedLanguageId();
147  if ($selectedLanguageId === -1) {
149  if (!isset(‪$languages[0])) {
150  // $languages may not contain the default (0) in case the user does not have access to it.
151  // However, as for selected pages, it should also be displayed readonly in the "all languages" view
152  ‪$languages = [
153  $this->site->getDefaultLanguage(),
154  ...$languages,
155  ];
156  }
157  return ‪$languages;
158  }
159  if ($selectedLanguageId > 0) {
160  // A specific language is selected; compose a list of default language plus selected language
161  return [
162  $this->site->getDefaultLanguage(),
163  $this->site->getLanguageById($selectedLanguageId),
164  ];
165  }
166  return [$this->site->getDefaultLanguage()];
167  }
168 
169  public function ‪getSiteLanguage(?int $languageId = null): ‪SiteLanguage
170  {
171  if ($languageId === null) {
172  return ‪$this->siteLanguage;
173  }
174  if ($languageId === -1) {
175  return $this->siteLanguages[-1];
176  }
177  return $this->site->getLanguageById($languageId);
178  }
179 
180  public function ‪isPageEditable(): bool
181  {
182  // TODO: refactor to page permissions container
183  if ($this->‪getBackendUser()->isAdmin()) {
184  return true;
185  }
186  return !$this->pageRecord['editlock'] && $this->‪getBackendUser()->doesUserHaveAccess($this->pageRecord, ‪Permission::PAGE_EDIT);
187  }
188 
189  public function ‪getAllowNewContent(): bool
190  {
191  $allowInconsistentLanguageHandling = $this->drawingConfiguration->getAllowInconsistentLanguageHandling();
192  if (!$allowInconsistentLanguageHandling && $this->‪getLanguageModeIdentifier() === 'connected') {
193  return false;
194  }
195  return true;
196  }
197 
198  public function ‪getContentTypeLabels(): array
199  {
200  if (empty($this->contentTypeLabels)) {
201  foreach (‪$GLOBALS['TCA']['tt_content']['columns']['CType']['config']['items'] as $val) {
202  $this->contentTypeLabels[$val['value']] = $this->‪getLanguageService()->sL($val['label']);
203  }
204  }
206  }
207 
208  public function ‪getItemLabels(): array
209  {
210  if (empty($this->itemLabels)) {
211  foreach (‪$GLOBALS['TCA']['tt_content']['columns'] as $name => $val) {
212  $this->itemLabels[$name] = $this->‪getLanguageService()->sL($val['label'] ?? '');
213  }
214  }
215  return ‪$this->itemLabels;
216  }
217 
218  public function ‪getLanguageModeLabelClass(): string
219  {
220  $languageId = $this->siteLanguage->getLanguageId();
221  $contentRecordsPerColumn = $this->contentFetcher->getFlatContentRecords($languageId);
222  $translationData = $this->contentFetcher->getTranslationData($contentRecordsPerColumn, $languageId);
223  return $translationData['mode'] === 'mixed' ? 'danger' : 'info';
224  }
225 
226  public function ‪getLanguageMode(): string
227  {
228  return match ($this->‪getLanguageModeIdentifier()) {
229  'mixed' => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:languageModeMixed'),
230  'connected' => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:languageModeConnected'),
231  'free' => $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:languageModeFree'),
232  default => '',
233  };
234  }
235 
236  public function ‪getLanguageModeIdentifier(): string
237  {
238  $contentRecordsPerColumn = $this->contentFetcher->getContentRecordsPerColumn(null, $this->siteLanguage->getLanguageId());
239  $contentRecords = empty($contentRecordsPerColumn) ? [] : array_merge(...$contentRecordsPerColumn);
240  $translationData = $this->contentFetcher->getTranslationData($contentRecords, $this->siteLanguage->getLanguageId());
241  return $translationData['mode'] ?? '';
242  }
243 
244  public function ‪getNewLanguageOptions(): array
245  {
246  if (!$this->‪getBackendUser()->check('tables_modify', 'pages')) {
247  return [];
248  }
249 
250  // First, select all languages that are available for the current user
251  $availableTranslations = [];
252  foreach ($this->‪getSiteLanguages() as $language) {
253  if ($language->getLanguageId() <= 0) {
254  continue;
255  }
256  $availableTranslations[$language->getLanguageId()] = $language->getTitle();
257  }
258 
259  // Then, subtract the languages which are already on the page:
260  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
261  $queryBuilder->getRestrictions()->removeAll()
262  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
263  ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->‪getBackendUser()->workspace));
264  $queryBuilder->select('*')
265  ->from('pages')
266  ->where(
267  $queryBuilder->expr()->eq(
268  ‪$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
269  $queryBuilder->createNamedParameter($this->pageId, ‪Connection::PARAM_INT)
270  )
271  );
272  $statement = $queryBuilder->executeQuery();
273  while ($row = $statement->fetchAssociative()) {
274  BackendUtility::workspaceOL('pages', $row, $this->‪getBackendUser()->workspace);
275  if ($row && VersionState::tryFrom($row['t3ver_state']) !== VersionState::DELETE_PLACEHOLDER) {
276  unset($availableTranslations[(int)$row[‪$GLOBALS['TCA']['pages']['ctrl']['languageField']]]);
277  }
278  }
279  // If any languages are left, make selector:
280  $options = [];
281  if (!empty($availableTranslations)) {
282  $options[] = $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:new_language');
283  foreach ($availableTranslations as $languageUid => $languageTitle) {
284  // Build localize command URL to DataHandler (tce_db)
285  // which redirects to FormEngine (record_edit)
286  // which, when finished editing should return back to the current page (returnUrl)
287  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
288  ‪$targetUrl = (string)$uriBuilder->buildUriFromRoute(
289  'tce_db',
290  [
291  'cmd' => [
292  'pages' => [
293  $this->pageId => [
294  'localize' => $languageUid,
295  ],
296  ],
297  ],
298  'redirect' => (string)$uriBuilder->buildUriFromRoute(
299  'record_edit',
300  [
301  'justLocalized' => 'pages:' . $this->pageId . ':' . $languageUid,
302  'returnUrl' => $this->‪getCurrentRequest()->getAttribute('normalizedParams')->getRequestUri(),
303  ]
304  ),
305  ]
306  );
307  $options[‪$targetUrl] = $languageTitle;
308  }
309  }
310  return $options;
311  }
312 
313  public function ‪getCurrentRequest(): ServerRequestInterface
314  {
315  return $this->request;
316  }
317 
318  public function ‪getLocalizedPageTitle(): string
319  {
320  return $this->localizedPageRecord['title'] ?? $this->pageRecord['title'];
321  }
322 
323  public function ‪getLocalizedPageRecord(): ?array
324  {
326  }
327 
329  {
330  return ‪$GLOBALS['LANG'];
331  }
332 }
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguagesToShow
‪SiteLanguage[] getLanguagesToShow()
Definition: PageLayoutContext.php:144
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Backend\View\PageLayoutContext\$pageId
‪int $pageId
Definition: PageLayoutContext.php:45
‪$languages
‪$languages
Definition: updateIsoDatabase.php:104
‪TYPO3\CMS\Core\Versioning\VersionState
‪VersionState
Definition: VersionState.php:22
‪TYPO3\CMS\Backend\View
Definition: AuthenticationStyleInformation.php:18
‪TYPO3\CMS\Backend\View\PageLayoutContext\$itemLabels
‪array $itemLabels
Definition: PageLayoutContext.php:68
‪TYPO3\CMS\Backend\View\PageLayoutContext\getBackendLayout
‪getBackendLayout()
Definition: PageLayoutContext.php:108
‪TYPO3\CMS\Redirects\Message\$targetUrl
‪identifier readonly UriInterface $targetUrl
Definition: RedirectWasHitMessage.php:33
‪TYPO3\CMS\Backend\View\BackendLayout\ContentFetcher
Definition: ContentFetcher.php:53
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguageModeIdentifier
‪getLanguageModeIdentifier()
Definition: PageLayoutContext.php:236
‪TYPO3\CMS\Backend\View\PageLayoutContext\$contentFetcher
‪ContentFetcher $contentFetcher
Definition: PageLayoutContext.php:43
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguageService
‪getLanguageService()
Definition: PageLayoutContext.php:328
‪TYPO3\CMS\Backend\View\PageLayoutContext\getItemLabels
‪getItemLabels()
Definition: PageLayoutContext.php:208
‪TYPO3\CMS\Backend\View\PageLayoutContext\getAllowNewContent
‪getAllowNewContent()
Definition: PageLayoutContext.php:189
‪TYPO3\CMS\Backend\View\PageLayoutContext\getContentTypeLabels
‪getContentTypeLabels()
Definition: PageLayoutContext.php:198
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\View\PageLayoutContext\getSiteLanguages
‪SiteLanguage[] getSiteLanguages()
Definition: PageLayoutContext.php:136
‪TYPO3\CMS\Backend\View\PageLayoutContext\getPageRecord
‪getPageRecord()
Definition: PageLayoutContext.php:123
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getLanguageId
‪getLanguageId()
Definition: SiteLanguage.php:169
‪TYPO3\CMS\Backend\View\PageLayoutContext\getSiteLanguage
‪getSiteLanguage(?int $languageId=null)
Definition: PageLayoutContext.php:169
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\View\PageLayoutContext\$localizedPageRecord
‪array $localizedPageRecord
Definition: PageLayoutContext.php:44
‪TYPO3\CMS\Backend\View\PageLayoutContext\cloneForLanguage
‪cloneForLanguage(SiteLanguage $language)
Definition: PageLayoutContext.php:83
‪TYPO3\CMS\Backend\View\Drawing\DrawingConfiguration
Definition: DrawingConfiguration.php:37
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguageModeLabelClass
‪getLanguageModeLabelClass()
Definition: PageLayoutContext.php:218
‪TYPO3\CMS\Backend\View\PageLayoutContext\__construct
‪__construct(protected readonly array $pageRecord, protected readonly BackendLayout $backendLayout, protected readonly SiteInterface $site, protected readonly DrawingConfiguration $drawingConfiguration, protected readonly ServerRequestInterface $request)
Definition: PageLayoutContext.php:70
‪TYPO3\CMS\Backend\View\PageLayoutContext\$siteLanguages
‪array $siteLanguages
Definition: PageLayoutContext.php:50
‪TYPO3\CMS\Backend\View\PageLayoutContext\getBackendUser
‪getBackendUser()
Definition: PageLayoutContext.php:118
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLocalizedPageRecord
‪getLocalizedPageRecord()
Definition: PageLayoutContext.php:323
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\View\PageLayoutContext
Definition: PageLayoutContext.php:42
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Backend\View\PageLayoutContext\getPageId
‪getPageId()
Definition: PageLayoutContext.php:128
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_EDIT
‪const PAGE_EDIT
Definition: Permission.php:40
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguageMode
‪getLanguageMode()
Definition: PageLayoutContext.php:226
‪TYPO3\CMS\Backend\View\BackendLayout\BackendLayout
Definition: BackendLayout.php:27
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Backend\View\PageLayoutContext\getDrawingConfiguration
‪getDrawingConfiguration()
Definition: PageLayoutContext.php:113
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLocalizedPageTitle
‪getLocalizedPageTitle()
Definition: PageLayoutContext.php:318
‪TYPO3\CMS\Backend\View\PageLayoutContext\$contentTypeLabels
‪array $contentTypeLabels
Definition: PageLayoutContext.php:57
‪TYPO3\CMS\Backend\View\PageLayoutContext\getCurrentRequest
‪getCurrentRequest()
Definition: PageLayoutContext.php:313
‪TYPO3\CMS\Backend\View\PageLayoutContext\isPageEditable
‪isPageEditable()
Definition: PageLayoutContext.php:180
‪TYPO3\CMS\Backend\View\PageLayoutContext\getNewLanguageOptions
‪getNewLanguageOptions()
Definition: PageLayoutContext.php:244
‪TYPO3\CMS\Backend\View\PageLayoutContext\$siteLanguage
‪SiteLanguage $siteLanguage
Definition: PageLayoutContext.php:51
‪TYPO3\CMS\Backend\View\PageLayoutContext\setSiteLanguage
‪setSiteLanguage(SiteLanguage $siteLanguage)
Definition: PageLayoutContext.php:90
‪TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction
Definition: WorkspaceRestriction.php:39