‪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 
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
38 
43 {
47  protected ‪$backendLayout;
48 
52  protected ‪$drawingConfiguration;
53 
57  protected ‪$contentFetcher;
58 
62  protected ‪$pageRecord;
63 
67  protected ‪$localizedPageRecord;
68 
72  protected ‪$pageId;
73 
77  protected ‪$siteLanguages;
78 
82  protected ‪$siteLanguage;
83 
87  protected ‪$site;
88 
95  protected ‪$contentTypeLabels = [];
96 
108  protected ‪$itemLabels = [];
109 
111  {
112  $this->pageId = (int)(‪$pageRecord['uid'] ?? 0);
113  try {
114  $this->site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($this->pageId);
115  } catch (‪SiteNotFoundException $e) {
116  $this->site = new ‪NullSite();
117  }
118  // TODO: retrieve implementation class names from Site?
119  $this->pageRecord = ‪$pageRecord;
120  $this->backendLayout = ‪$backendLayout;
121  $this->drawingConfiguration = GeneralUtility::makeInstance(DrawingConfiguration::class);
122  $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, $this);
123  $this->siteLanguages = $this->site->getAvailableLanguages($this->‪getBackendUser(), true, $this->pageId);
124  $this->siteLanguage = $this->site->getDefaultLanguage();
125  }
126 
127  public function ‪cloneForLanguage(‪SiteLanguage $language): self
128  {
129  $copy = clone $this;
130  $copy->setSiteLanguage($language);
131  return $copy;
132  }
133 
134  public function ‪setSiteLanguage(SiteLanguage ‪$siteLanguage): void
135  {
136  $this->siteLanguage = ‪$siteLanguage;
137  $languageId = ‪$siteLanguage->‪getLanguageId();
138  if ($languageId > 0) {
139  $pageLocalizationRecord = BackendUtility::getRecordLocalization(
140  'pages',
141  $this->‪getPageId(),
142  $languageId
143  );
144  $pageLocalizationRecord = reset($pageLocalizationRecord);
145  if (!empty($pageLocalizationRecord)) {
146  BackendUtility::workspaceOL('pages', $pageLocalizationRecord);
147  $this->localizedPageRecord = $pageLocalizationRecord ?: null;
148  }
149  }
150  }
151 
152  public function ‪getBackendLayout(): ‪BackendLayout
153  {
155  }
156 
158  {
160  }
161 
163  {
164  return ‪$GLOBALS['BE_USER'];
165  }
166 
167  public function ‪getPageRecord(): array
168  {
170  }
171 
172  public function ‪getPageId(): int
173  {
174  return ‪$this->pageId;
175  }
176 
180  public function ‪getSiteLanguages(): iterable
181  {
183  }
184 
188  public function ‪getLanguagesToShow(): iterable
189  {
190  $selectedLanguageId = $this->drawingConfiguration->getSelectedLanguageId();
191  if ($selectedLanguageId === -1) {
193  if (!isset(‪$languages[0])) {
194  // $languages may not contain the default (0) in case the user does not have access to it.
195  // However, as for selected pages, it should also be displayed readonly in the "all languages" view
196  ‪$languages = [
197  $this->site->getDefaultLanguage(),
198  ...$languages,
199  ];
200  }
201  return ‪$languages;
202  }
203  if ($selectedLanguageId > 0) {
204  // A specific language is selected; compose a list of default language plus selected language
205  return [
206  $this->site->getDefaultLanguage(),
207  $this->site->getLanguageById($selectedLanguageId),
208  ];
209  }
210  return [$this->site->getDefaultLanguage()];
211  }
212 
213  public function ‪getSiteLanguage(?int $languageId = null): ‪SiteLanguage
214  {
215  if ($languageId === null) {
216  return ‪$this->siteLanguage;
217  }
218  if ($languageId === -1) {
219  return $this->siteLanguages[-1];
220  }
221  return $this->site->getLanguageById($languageId);
222  }
223 
224  public function ‪isPageEditable(): bool
225  {
226  // TODO: refactor to page permissions container
227  if ($this->‪getBackendUser()->isAdmin()) {
228  return true;
229  }
230  ‪$pageRecord = $this->‪getPageRecord();
231  return !$pageRecord['editlock'] && $this->‪getBackendUser()->doesUserHaveAccess($pageRecord, ‪Permission::PAGE_EDIT);
232  }
233 
234  public function ‪getAllowNewContent(): bool
235  {
236  ‪$pageId = $this->‪getPageId();
237  $allowInconsistentLanguageHandling = (bool)(BackendUtility::getPagesTSconfig(‪$pageId)['mod.']['web_layout.']['allowInconsistentLanguageHandling'] ?? false);
238  if (!$allowInconsistentLanguageHandling && $this->‪getLanguageModeIdentifier() === 'connected') {
239  return false;
240  }
241  return true;
242  }
243 
244  public function ‪getContentTypeLabels(): array
245  {
246  if (empty($this->contentTypeLabels)) {
247  foreach (‪$GLOBALS['TCA']['tt_content']['columns']['CType']['config']['items'] as $val) {
248  $this->contentTypeLabels[$val['value']] = $this->‪getLanguageService()->sL($val['label']);
249  }
250  }
252  }
253 
254  public function ‪getItemLabels(): array
255  {
256  if (empty($this->itemLabels)) {
257  foreach (‪$GLOBALS['TCA']['tt_content']['columns'] as $name => $val) {
258  $this->itemLabels[$name] = $this->‪getLanguageService()->sL($val['label'] ?? '');
259  }
260  }
262  }
263 
264  public function ‪getLanguageModeLabelClass(): string
265  {
266  $languageId = $this->siteLanguage->getLanguageId();
267  $contentRecordsPerColumn = $this->contentFetcher->getFlatContentRecords($languageId);
268  $translationData = $this->contentFetcher->getTranslationData($contentRecordsPerColumn, $languageId);
269  return $translationData['mode'] === 'mixed' ? 'danger' : 'info';
270  }
271 
272  public function ‪getLanguageMode(): string
273  {
274  switch ($this->‪getLanguageModeIdentifier()) {
275  case 'mixed':
276  $languageMode = $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:languageModeMixed');
277  break;
278  case 'connected':
279  $languageMode = $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:languageModeConnected');
280  break;
281  case 'free':
282  $languageMode = $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:languageModeFree');
283  break;
284  default:
285  $languageMode = '';
286  }
287  return $languageMode;
288  }
289 
290  public function ‪getLanguageModeIdentifier(): string
291  {
292  $contentRecordsPerColumn = $this->contentFetcher->getContentRecordsPerColumn(null, $this->siteLanguage->getLanguageId());
293  $contentRecords = empty($contentRecordsPerColumn) ? [] : array_merge(...$contentRecordsPerColumn);
294  $translationData = $this->contentFetcher->getTranslationData($contentRecords, $this->siteLanguage->getLanguageId());
295  return $translationData['mode'] ?? '';
296  }
297 
298  public function ‪getNewLanguageOptions(): array
299  {
300  if (!$this->‪getBackendUser()->check('tables_modify', 'pages')) {
301  return [];
302  }
303  $id = $this->‪getPageId();
304 
305  // First, select all languages that are available for the current user
306  $availableTranslations = [];
307  foreach ($this->‪getSiteLanguages() as $language) {
308  if ($language->getLanguageId() <= 0) {
309  continue;
310  }
311  $availableTranslations[$language->getLanguageId()] = $language->getTitle();
312  }
313 
314  // Then, subtract the languages which are already on the page:
315  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
316  $queryBuilder->getRestrictions()->removeAll()
317  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
318  ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->‪getBackendUser()->workspace));
319  $queryBuilder->select('uid', ‪$GLOBALS['TCA']['pages']['ctrl']['languageField'])
320  ->from('pages')
321  ->where(
322  $queryBuilder->expr()->eq(
323  ‪$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
324  $queryBuilder->createNamedParameter($id, ‪Connection::PARAM_INT)
325  )
326  );
327  $statement = $queryBuilder->executeQuery();
328  while ($row = $statement->fetchAssociative()) {
329  unset($availableTranslations[(int)$row[‪$GLOBALS['TCA']['pages']['ctrl']['languageField']]]);
330  }
331  // If any languages are left, make selector:
332  $options = [];
333  if (!empty($availableTranslations)) {
334  $options[] = $this->‪getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:new_language');
335  foreach ($availableTranslations as $languageUid => $languageTitle) {
336  // Build localize command URL to DataHandler (tce_db)
337  // which redirects to FormEngine (record_edit)
338  // which, when finished editing should return back to the current page (returnUrl)
339  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
340  ‪$targetUrl = (string)$uriBuilder->buildUriFromRoute(
341  'tce_db',
342  [
343  'cmd' => [
344  'pages' => [
345  $id => [
346  'localize' => $languageUid,
347  ],
348  ],
349  ],
350  'redirect' => (string)$uriBuilder->buildUriFromRoute(
351  'record_edit',
352  [
353  'justLocalized' => 'pages:' . $id . ':' . $languageUid,
354  'returnUrl' => ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri(),
355  ]
356  ),
357  ]
358  );
359  $options[‪$targetUrl] = $languageTitle;
360  }
361  }
362  return $options;
363  }
364 
365  public function ‪getLocalizedPageTitle(): string
366  {
367  return $this->‪getLocalizedPageRecord()['title'] ?? $this->‪getPageRecord()['title'];
368  }
369 
370  public function ‪getLocalizedPageRecord(): ?array
371  {
373  }
374 
375  protected function ‪getLanguageService(): LanguageService
376  {
377  return ‪$GLOBALS['LANG'];
378  }
379 }
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguagesToShow
‪SiteLanguage[] getLanguagesToShow()
Definition: PageLayoutContext.php:177
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:50
‪TYPO3\CMS\Backend\View\PageLayoutContext\$pageId
‪int $pageId
Definition: PageLayoutContext.php:66
‪$languages
‪$languages
Definition: updateIsoDatabase.php:104
‪TYPO3\CMS\Backend\View
Definition: AuthenticationStyleInformation.php:18
‪TYPO3\CMS\Backend\View\PageLayoutContext\$itemLabels
‪array $itemLabels
Definition: PageLayoutContext.php:97
‪TYPO3\CMS\Backend\View\PageLayoutContext\getBackendLayout
‪getBackendLayout()
Definition: PageLayoutContext.php:141
‪TYPO3\CMS\Core\Site\Entity\NullSite
Definition: NullSite.php:32
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪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:279
‪TYPO3\CMS\Backend\View\PageLayoutContext\$contentFetcher
‪ContentFetcher $contentFetcher
Definition: PageLayoutContext.php:54
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguageService
‪getLanguageService()
Definition: PageLayoutContext.php:364
‪TYPO3\CMS\Backend\View\PageLayoutContext\$site
‪SiteInterface $site
Definition: PageLayoutContext.php:78
‪TYPO3\CMS\Backend\View\PageLayoutContext\getItemLabels
‪getItemLabels()
Definition: PageLayoutContext.php:243
‪TYPO3\CMS\Backend\View\PageLayoutContext\$localizedPageRecord
‪array null $localizedPageRecord
Definition: PageLayoutContext.php:62
‪TYPO3\CMS\Backend\View\PageLayoutContext\getAllowNewContent
‪getAllowNewContent()
Definition: PageLayoutContext.php:223
‪TYPO3\CMS\Backend\View\PageLayoutContext\getContentTypeLabels
‪getContentTypeLabels()
Definition: PageLayoutContext.php:233
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\View\PageLayoutContext\getSiteLanguages
‪SiteLanguage[] getSiteLanguages()
Definition: PageLayoutContext.php:169
‪TYPO3\CMS\Backend\View\PageLayoutContext\getPageRecord
‪getPageRecord()
Definition: PageLayoutContext.php:156
‪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:202
‪TYPO3\CMS\Backend\View\PageLayoutContext\$drawingConfiguration
‪DrawingConfiguration $drawingConfiguration
Definition: PageLayoutContext.php:50
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:60
‪TYPO3\CMS\Backend\View\PageLayoutContext\cloneForLanguage
‪cloneForLanguage(SiteLanguage $language)
Definition: PageLayoutContext.php:116
‪TYPO3\CMS\Backend\View\PageLayoutContext\$pageRecord
‪array $pageRecord
Definition: PageLayoutContext.php:58
‪TYPO3\CMS\Backend\View\Drawing\DrawingConfiguration
Definition: DrawingConfiguration.php:36
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLanguageModeLabelClass
‪getLanguageModeLabelClass()
Definition: PageLayoutContext.php:253
‪TYPO3\CMS\Backend\View\PageLayoutContext\getBackendUser
‪getBackendUser()
Definition: PageLayoutContext.php:151
‪TYPO3\CMS\Backend\View\PageLayoutContext\$siteLanguages
‪SiteLanguage[] $siteLanguages
Definition: PageLayoutContext.php:70
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:39
‪TYPO3\CMS\Backend\View\PageLayoutContext\__construct
‪__construct(array $pageRecord, BackendLayout $backendLayout)
Definition: PageLayoutContext.php:99
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLocalizedPageRecord
‪getLocalizedPageRecord()
Definition: PageLayoutContext.php:359
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\View\PageLayoutContext
Definition: PageLayoutContext.php:43
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Backend\View\PageLayoutContext\getPageId
‪getPageId()
Definition: PageLayoutContext.php:161
‪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:261
‪TYPO3\CMS\Backend\View\PageLayoutContext\$backendLayout
‪BackendLayout $backendLayout
Definition: PageLayoutContext.php:46
‪TYPO3\CMS\Backend\View\BackendLayout\BackendLayout
Definition: BackendLayout.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:48
‪TYPO3\CMS\Backend\View\PageLayoutContext\getDrawingConfiguration
‪getDrawingConfiguration()
Definition: PageLayoutContext.php:146
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Backend\View\PageLayoutContext\getLocalizedPageTitle
‪getLocalizedPageTitle()
Definition: PageLayoutContext.php:354
‪TYPO3\CMS\Backend\View\PageLayoutContext\$contentTypeLabels
‪array $contentTypeLabels
Definition: PageLayoutContext.php:85
‪TYPO3\CMS\Backend\View\PageLayoutContext\isPageEditable
‪isPageEditable()
Definition: PageLayoutContext.php:213
‪TYPO3\CMS\Backend\View\PageLayoutContext\getNewLanguageOptions
‪getNewLanguageOptions()
Definition: PageLayoutContext.php:287
‪TYPO3\CMS\Backend\View\PageLayoutContext\$siteLanguage
‪SiteLanguage $siteLanguage
Definition: PageLayoutContext.php:74
‪TYPO3\CMS\Backend\View\PageLayoutContext\setSiteLanguage
‪setSiteLanguage(SiteLanguage $siteLanguage)
Definition: PageLayoutContext.php:123
‪TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction
Definition: WorkspaceRestriction.php:39