Completed
Push — master ( f66149...a7372c )
by
unknown
130:24 queued 111:56
created

LanguageColumn::getAllowTranslate()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 30
rs 8.8333
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Backend\View\BackendLayout\Grid;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Backend\Routing\UriBuilder;
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;
22
use TYPO3\CMS\Core\Imaging\Icon;
23
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Core\Versioning\VersionState;
26
27
/**
28
 * Language Column
29
 *
30
 * Object representation of a site language selected in the "page" module
31
 * to show translations of content elements.
32
 *
33
 * Contains getter methods to return various values associated with a single
34
 * language, e.g. localized page title, associated SiteLanguage instance,
35
 * edit URLs and link titles and so on.
36
 *
37
 * Stores a duplicated Grid object associated with the SiteLanguage.
38
 *
39
 * Accessed from Fluid templates - generated from within BackendLayout when
40
 * "page" module is in "languages" mode.
41
 */
42
class LanguageColumn extends AbstractGridObject
43
{
44
    /**
45
     * @var SiteLanguage
46
     */
47
    protected $siteLanguage;
48
49
    /**
50
     * @var array
51
     */
52
    protected $localizedPageRecord = [];
53
54
    /**
55
     * @var array
56
     */
57
    protected $defaultLanguageElements = [];
58
59
    /**
60
     * @var array
61
     */
62
    protected $flatContentOfLanguage = [];
63
64
    /**
65
     * @var GridColumn|null
66
     */
67
    protected $grid;
68
69
    public function __construct(BackendLayout $backendLayout, SiteLanguage $language, array $defaultLanguageElements)
70
    {
71
        parent::__construct($backendLayout);
72
        $this->siteLanguage = $language;
73
        $this->defaultLanguageElements = $defaultLanguageElements;
74
        if ($this->siteLanguage->getLanguageId() > 0) {
75
            $pageLocalizationRecord = BackendUtility::getRecordLocalization(
76
                'pages',
77
                $backendLayout->getDrawingConfiguration()->getPageId(),
78
                $language->getLanguageId()
79
            );
80
            if (is_array($pageLocalizationRecord)) {
81
                $pageLocalizationRecord = reset($pageLocalizationRecord);
82
            }
83
            BackendUtility::workspaceOL('pages', $pageLocalizationRecord);
84
            $this->localizedPageRecord = $pageLocalizationRecord;
85
        } else {
86
            $this->localizedPageRecord = $backendLayout->getDrawingConfiguration()->getPageRecord();
87
        }
88
89
        $contentFetcher = $backendLayout->getContentFetcher();
90
        $contentRecords = $contentFetcher->getContentRecordsPerColumn(null, $language->getLanguageId());
91
        if (!empty($contentRecords)) {
92
            $this->flatContentOfLanguage = array_merge(...$contentRecords);
93
        } else {
94
            $this->flatContentOfLanguage = [];
95
        }
96
    }
97
98
    public function getLocalizedPageRecord(): ?array
99
    {
100
        return $this->localizedPageRecord ?: null;
101
    }
102
103
    public function getSiteLanguage(): SiteLanguage
104
    {
105
        return $this->siteLanguage;
106
    }
107
108
    public function getGrid(): Grid
109
    {
110
        if (empty($this->grid)) {
111
            $this->grid = $this->backendLayout->getGrid();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->backendLayout->getGrid() of type TYPO3\CMS\Backend\View\BackendLayout\Grid\Grid is incompatible with the declared type TYPO3\CMS\Backend\View\B...ut\Grid\GridColumn|null of property $grid.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112
        }
113
        return $this->grid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->grid could return the type TYPO3\CMS\Backend\View\B...dLayout\Grid\GridColumn which is incompatible with the type-hinted return TYPO3\CMS\Backend\View\BackendLayout\Grid\Grid. Consider adding an additional type-check to rule them out.
Loading history...
114
    }
115
116
    public function getLanguageModeLabelClass(): string
117
    {
118
        $contentRecordsPerColumn = $this->backendLayout->getContentFetcher()->getFlatContentRecords();
119
        $translationData = $this->backendLayout->getContentFetcher()->getTranslationData($contentRecordsPerColumn, $this->siteLanguage->getLanguageId());
120
        return $translationData['mode'] === 'mixed' ? 'danger' : 'info';
121
    }
122
123
    public function getLanguageMode(): string
124
    {
125
        switch ($this->backendLayout->getLanguageModeIdentifier()) {
126
            case 'mixed':
127
                $languageMode = $this->getLanguageService()->getLL('languageModeMixed');
128
                break;
129
            case 'connected':
130
                $languageMode = $this->getLanguageService()->getLL('languageModeConnected');
131
                break;
132
            case 'free':
133
                $languageMode = $this->getLanguageService()->getLL('languageModeFree');
134
                break;
135
            default:
136
                $languageMode = '';
137
        }
138
        return $languageMode;
139
    }
140
141
    public function getPageIcon(): string
142
    {
143
        return BackendUtility::wrapClickMenuOnIcon(
144
            $this->iconFactory->getIconForRecord('pages', $this->localizedPageRecord, Icon::SIZE_SMALL)->render(),
145
            'pages',
146
            $this->localizedPageRecord['uid']
147
        );
148
    }
149
150
    public function getAllowTranslate(): bool
151
    {
152
        if ($this->siteLanguage->getLanguageId() === 0) {
153
            return false;
154
        }
155
156
        $localizationTsConfig = BackendUtility::getPagesTSconfig($this->backendLayout->getDrawingConfiguration()->getPageId())['mod.']['web_layout.']['localization.'] ?? [];
157
        $allowTranslate = (bool)($localizationTsConfig['enableTranslate'] ?? true);
158
        if (!$allowTranslate) {
159
            return false;
160
        }
161
162
        $translationData = $this->backendLayout->getContentFetcher()->getTranslationData($this->flatContentOfLanguage, $this->siteLanguage->getLanguageId());
163
        if (!empty($translationData)) {
164
            if (isset($translationData['hasStandAloneContent'])) {
165
                return false;
166
            }
167
        }
168
169
        $defaultLanguageUids = array_flip(array_column($this->defaultLanguageElements, 'uid'));
170
        $translatedLanguageUids = array_column($this->flatContentOfLanguage, 'l10n_source');
171
        if (empty($translatedLanguageUids)) {
172
            return true;
173
        }
174
175
        foreach ($translatedLanguageUids as $translatedUid) {
176
            unset($defaultLanguageUids[$translatedUid]);
177
        }
178
179
        return !empty($defaultLanguageUids);
180
    }
181
182
    public function getTranslationData(): array
183
    {
184
        $contentFetcher = $this->backendLayout->getContentFetcher();
185
        return $contentFetcher->getTranslationData($this->defaultLanguageElements, $this->siteLanguage->getLanguageId());
186
    }
187
188
    public function getAllowTranslateCopy(): bool
189
    {
190
        $localizationTsConfig = BackendUtility::getPagesTSconfig($this->backendLayout->getDrawingConfiguration()->getPageId())['mod.']['web_layout.']['localization.'] ?? [];
191
        $allowCopy = (bool)($localizationTsConfig['enableCopy'] ?? true);
192
        if (!empty($translationData)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $translationData seems to never exist and therefore empty should always be true.
Loading history...
193
            if (isset($translationData['hasStandAloneContent'])) {
194
                return false;
195
            }
196
            if (isset($translationData['hasTranslations'])) {
197
                $allowCopy = $allowCopy && !$translationData['hasTranslations'];
198
            }
199
        }
200
        return $allowCopy;
201
    }
202
203
    public function getTranslatePageTitle(): string
204
    {
205
        return $this->getLanguageService()->getLL('newPageContent_translate');
206
    }
207
208
    public function getAllowEditPage(): bool
209
    {
210
        return $this->getBackendUser()->check('tables_modify', 'pages');
211
    }
212
213
    public function getPageEditTitle(): string
214
    {
215
        return $this->getLanguageService()->getLL('edit');
216
    }
217
218
    public function getPageEditUrl(): string
219
    {
220
        $urlParameters = [
221
            'edit' => [
222
                'pages' => [
223
                    $this->localizedPageRecord['uid'] => 'edit'
224
                ]
225
            ],
226
            // Disallow manual adjustment of the language field for pages
227
            'overrideVals' => [
228
                'pages' => [
229
                    'sys_language_uid' => $this->siteLanguage->getLanguageId()
230
                ]
231
            ],
232
            'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
233
        ];
234
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
235
        return (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
236
    }
237
238
    public function getAllowViewPage(): bool
239
    {
240
        return !VersionState::cast($this->backendLayout->getDrawingConfiguration()->getPageRecord()['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER);
241
    }
242
243
    public function getViewPageLinkTitle(): string
244
    {
245
        return $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage');
246
    }
247
248
    public function getViewPageOnClick(): string
249
    {
250
        $pageId = $this->backendLayout->getDrawingConfiguration()->getPageId();
251
        return BackendUtility::viewOnClick(
252
            $pageId,
253
            '',
254
            BackendUtility::BEgetRootLine($pageId),
255
            '',
256
            '',
257
            '&L=' . $this->backendLayout->getDrawingConfiguration()->getLanguageColumnsPointer()
258
        );
259
    }
260
}
261