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