Passed
Push — master ( 03f78e...954a3f )
by
unknown
12:06
created

populateAvailableSiteLanguages()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 36
rs 8.6186
c 0
b 0
f 0
cc 7
nc 16
nop 1
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
18
namespace TYPO3\CMS\Core\Localization;
19
20
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21
use TYPO3\CMS\Core\Core\Environment;
22
use TYPO3\CMS\Core\Site\Entity\NullSite;
23
use TYPO3\CMS\Core\Site\SiteFinder;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Provides ItemProcFunc fields for special population of available TYPO3 system languages
28
 * @internal
29
 */
30
class TcaSystemLanguageCollector
31
{
32
    private Locales $locales;
33
34
    public function __construct(Locales $locales)
35
    {
36
        $this->locales = $locales;
37
    }
38
39
    /**
40
     * Populate languages and group by available languages of the Language packs
41
     */
42
    public function populateAvailableSystemLanguagesForBackend(array &$fieldInformation): void
43
    {
44
        $languageItems = $this->locales->getLanguages();
45
        unset($languageItems['default']);
46
        asort($languageItems);
47
        $installedLanguages = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? [];
48
        $availableLanguages = [];
49
        $unavailableLanguages = [];
50
        foreach ($languageItems as $typo3Language => $name) {
51
            $available = in_array($typo3Language, $installedLanguages, true) || is_dir(Environment::getLabelsPath() . '/' . $typo3Language);
52
            if ($available) {
53
                $availableLanguages[] = [$name, $typo3Language, '', 'installed'];
54
            } else {
55
                $unavailableLanguages[] = [$name, $typo3Language, '', 'unavailable'];
56
            }
57
        }
58
59
        // Ensure ordering of the items
60
        $fieldInformation['items'] = array_merge($fieldInformation['items'], $availableLanguages);
61
        $fieldInformation['items'] = array_merge($fieldInformation['items'], $unavailableLanguages);
62
    }
63
64
    /**
65
     * Provides a list of all languages available for ALL sites.
66
     * In case no site configuration can be found in the system,
67
     * a fallback is used to add at least the default language.
68
     *
69
     * Used by be_users and be_groups for their `allowed_languages` column.
70
     */
71
    public function populateAvailableSiteLanguages(array &$fieldInformation): void
72
    {
73
        $allLanguages = [];
74
        foreach ($this->getAllSites() as $site) {
75
            foreach ($site->getAllLanguages() as $language) {
76
                $languageId = $language->getLanguageId();
77
                if (isset($allLanguages[$languageId])) {
78
                    // Language already provided by another site, just add the label separately
79
                    $allLanguages[$languageId][0] .= ', ' . $language->getTitle() . ' [Site: ' . $site->getIdentifier() . ']';
80
                    continue;
81
                }
82
                $allLanguages[$languageId] = [
83
                    0 => $language->getTitle() . ' [Site: ' . $site->getIdentifier() . ']',
84
                    1 => $languageId,
85
                    2 => $language->getFlagIdentifier(),
86
                ];
87
            }
88
        }
89
90
        if ($allLanguages !== []) {
91
            ksort($allLanguages);
92
            foreach ($allLanguages as $item) {
93
                $fieldInformation['items'][] = $item;
94
            }
95
            return;
96
        }
97
98
        // Fallback if no site configuration exists
99
        $recordPid = (int)($fieldInformation['row']['pid'] ?? 0);
100
        $languages = (new NullSite())->getAvailableLanguages($this->getBackendUser(), false, $recordPid);
101
102
        foreach ($languages as $languageId => $language) {
103
            $fieldInformation['items'][] = [
104
                0 => $language->getTitle(),
105
                1 => $languageId,
106
                2 => $language->getFlagIdentifier(),
107
            ];
108
        }
109
    }
110
111
    protected function getAllSites(): array
112
    {
113
        return GeneralUtility::makeInstance(SiteFinder::class)->getAllSites();
114
    }
115
116
    protected function getBackendUser(): BackendUserAuthentication
117
    {
118
        return $GLOBALS['BE_USER'];
119
    }
120
}
121