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

DrawingConfiguration::getNewLanguageOptions()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 35
c 1
b 0
f 0
nc 13
nop 0
dl 0
loc 56
rs 8.4266

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Backend\View\Drawing;
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\Core\Authentication\BackendUserAuthentication;
22
use TYPO3\CMS\Core\Database\ConnectionPool;
23
use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
24
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
25
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
26
use TYPO3\CMS\Core\Localization\LanguageService;
27
use TYPO3\CMS\Core\Site\Entity\NullSite;
28
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
29
use TYPO3\CMS\Core\Site\SiteFinder;
30
use TYPO3\CMS\Core\Type\Bitmask\Permission;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Drawing Configuration
35
 *
36
 * Attached to BackendLayout as storage for configuration options which
37
 * determine how a page layout is rendered. Contains settings for active
38
 * language, show-hidden, site languages etc. and returns TCA labels for
39
 * tt_content fields and CTypes.
40
 *
41
 * Corresponds to legacy public properties from PageLayoutView.
42
 */
43
class DrawingConfiguration
44
{
45
    /**
46
     * @var bool
47
     */
48
    protected $defaultLanguageBinding = true;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $languageMode = false;
54
55
    /**
56
     * @var array
57
     */
58
    protected $languageColumns = [];
59
60
    /**
61
     * @var int
62
     */
63
    protected $languageColumnsPointer = 0;
64
65
    /**
66
     * @var bool
67
     */
68
    protected $showHidden = true;
69
70
    /**
71
     * @var array
72
     */
73
    protected $activeColumns = [1, 0, 2, 3];
74
75
    /**
76
     * @var array
77
     */
78
    protected $contentTypeLabels = [];
79
80
    /**
81
     * @var array
82
     */
83
    protected $itemLabels = [];
84
85
    /**
86
     * @var int
87
     */
88
    protected $pageId = 0;
89
90
    /**
91
     * @var array
92
     */
93
    protected $pageRecord = [];
94
95
    /**
96
     * @var SiteLanguage[]
97
     */
98
    protected $siteLanguages = [];
99
100
    /**
101
     * @var bool
102
     */
103
    protected $showNewContentWizard = true;
104
105
    public function getDefaultLanguageBinding(): bool
106
    {
107
        return $this->defaultLanguageBinding;
108
    }
109
110
    public function setDefaultLanguageBinding(bool $defaultLanguageBinding): void
111
    {
112
        $this->defaultLanguageBinding = $defaultLanguageBinding;
113
    }
114
115
    public function getLanguageMode(): bool
116
    {
117
        return $this->languageMode;
118
    }
119
120
    public function setLanguageMode(bool $languageMode): void
121
    {
122
        $this->languageMode = $languageMode;
123
    }
124
125
    public function getLanguageColumns(): array
126
    {
127
        if ($this->languageColumnsPointer) {
128
            return [0 => 0, $this->languageColumnsPointer => $this->languageColumnsPointer];
129
        }
130
        return $this->languageColumns;
131
    }
132
133
    public function setLanguageColumns(array $languageColumns): void
134
    {
135
        $this->languageColumns = $languageColumns;
136
    }
137
138
    public function getLanguageColumnsPointer(): int
139
    {
140
        return $this->languageColumnsPointer;
141
    }
142
143
    public function setLanguageColumnsPointer(int $languageColumnsPointer): void
144
    {
145
        $this->languageColumnsPointer = $languageColumnsPointer;
146
    }
147
148
    public function getShowHidden(): bool
149
    {
150
        return $this->showHidden;
151
    }
152
153
    public function setShowHidden(bool $showHidden): void
154
    {
155
        $this->showHidden = $showHidden;
156
    }
157
158
    public function getActiveColumns(): array
159
    {
160
        return $this->activeColumns;
161
    }
162
163
    public function setActiveColumns(array $activeColumns): void
164
    {
165
        $this->activeColumns = $activeColumns;
166
    }
167
168
    public function getContentTypeLabels(): array
169
    {
170
        if (empty($this->contentTypeLabels)) {
171
            foreach ($GLOBALS['TCA']['tt_content']['columns']['CType']['config']['items'] as $val) {
172
                $this->contentTypeLabels[$val[1]] = $this->getLanguageService()->sL($val[0]);
173
            }
174
        }
175
        return $this->contentTypeLabels;
176
    }
177
178
    public function getItemLabels(): array
179
    {
180
        if (empty($this->itemLabels)) {
181
            foreach ($GLOBALS['TCA']['tt_content']['columns'] as $name => $val) {
182
                $this->itemLabels[$name] = $this->getLanguageService()->sL($val['label']);
183
            }
184
        }
185
        return $this->itemLabels;
186
    }
187
188
    public function getPageId(): int
189
    {
190
        return $this->pageId;
191
    }
192
193
    public function setPageId(int $pageId): void
194
    {
195
        $this->pageId = $pageId;
196
        $this->pageRecord = BackendUtility::getRecordWSOL('pages', $pageId);
197
    }
198
199
    public function getPageRecord(): array
200
    {
201
        return $this->pageRecord;
202
    }
203
204
    /**
205
     * @return SiteLanguage[]
206
     */
207
    public function getSiteLanguages(): array
208
    {
209
        if (empty($this->setSiteLanguages)) {
210
            try {
211
                $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($this->pageId);
212
            } catch (SiteNotFoundException $e) {
213
                $site = new NullSite();
214
            }
215
            $this->siteLanguages = $site->getAvailableLanguages($this->getBackendUser(), false, $this->pageId);
216
        }
217
        return $this->siteLanguages;
218
    }
219
220
    public function getSiteLanguage(int $languageUid): ?SiteLanguage
221
    {
222
        return $this->getSiteLanguages()[$languageUid] ?? null;
223
    }
224
225
    public function getShowNewContentWizard(): bool
226
    {
227
        return $this->showNewContentWizard;
228
    }
229
230
    public function setShowNewContentWizard(bool $showNewContentWizard): void
231
    {
232
        $this->showNewContentWizard = $showNewContentWizard;
233
    }
234
235
    public function getLocalizedPageTitle(): string
236
    {
237
        if ($this->getLanguageColumnsPointer()) {
238
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
239
                ->getQueryBuilderForTable('pages');
240
            $queryBuilder->getRestrictions()
241
                ->removeAll()
242
                ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
243
                ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
244
            $localizedPage = $queryBuilder
245
                ->select('*')
246
                ->from('pages')
247
                ->where(
248
                    $queryBuilder->expr()->eq(
249
                        $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
250
                        $queryBuilder->createNamedParameter($this->getPageId(), \PDO::PARAM_INT)
251
                    ),
252
                    $queryBuilder->expr()->eq(
253
                        $GLOBALS['TCA']['pages']['ctrl']['languageField'],
254
                        $queryBuilder->createNamedParameter($this->getLanguageColumnsPointer(), \PDO::PARAM_INT)
255
                    )
256
                )
257
                ->setMaxResults(1)
258
                ->execute()
259
                ->fetch();
260
            BackendUtility::workspaceOL('pages', $localizedPage);
261
            return $localizedPage['title'];
262
        }
263
        return $this->getPageRecord()['title'];
264
    }
265
266
    public function isPageEditable(): bool
267
    {
268
        if ($this->getBackendUser()->isAdmin()) {
269
            return true;
270
        }
271
        $pageRecord = $this->getPageRecord();
272
        return !$pageRecord['editlock'] && $this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::PAGE_EDIT);
273
    }
274
275
    public function getNewLanguageOptions(): array
276
    {
277
        if (!$this->getBackendUser()->check('tables_modify', 'pages')) {
278
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the type-hinted return array.
Loading history...
279
        }
280
        $id = $this->getPageId();
281
282
        // First, select all languages that are available for the current user
283
        $availableTranslations = [];
284
        foreach ($this->getSiteLanguages() as $language) {
285
            if ($language->getLanguageId() === 0) {
286
                continue;
287
            }
288
            $availableTranslations[$language->getLanguageId()] = $language->getTitle();
289
        }
290
291
        // Then, subtract the languages which are already on the page:
292
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
293
        $queryBuilder->getRestrictions()->removeAll()
294
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
295
            ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
296
        $queryBuilder->select('uid', $GLOBALS['TCA']['pages']['ctrl']['languageField'])
297
            ->from('pages')
298
            ->where(
299
                $queryBuilder->expr()->eq(
300
                    $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
301
                    $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)
302
                )
303
            );
304
        $statement = $queryBuilder->execute();
305
        while ($row = $statement->fetch()) {
306
            unset($availableTranslations[(int)$row[$GLOBALS['TCA']['pages']['ctrl']['languageField']]]);
307
        }
308
        // If any languages are left, make selector:
309
        $options = [];
310
        if (!empty($availableTranslations)) {
311
            $options[] = $this->getLanguageService()->getLL('new_language');
312
            foreach ($availableTranslations as $languageUid => $languageTitle) {
313
                // Build localize command URL to DataHandler (tce_db)
314
                // which redirects to FormEngine (record_edit)
315
                // which, when finished editing should return back to the current page (returnUrl)
316
                $parameters = [
317
                    'justLocalized' => 'pages:' . $id . ':' . $languageUid,
318
                    'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
319
                ];
320
                $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
321
                $redirectUrl = (string)$uriBuilder->buildUriFromRoute('record_edit', $parameters);
322
                $targetUrl = BackendUtility::getLinkToDataHandlerAction(
323
                    '&cmd[pages][' . $id . '][localize]=' . $languageUid,
324
                    $redirectUrl
325
                );
326
327
                $options[$targetUrl] = $languageTitle;
328
            }
329
        }
330
        return $options;
331
    }
332
333
    protected function getBackendUser(): BackendUserAuthentication
334
    {
335
        return $GLOBALS['BE_USER'];
336
    }
337
338
    protected function getLanguageService(): LanguageService
339
    {
340
        return $GLOBALS['LANG'];
341
    }
342
}
343