Completed
Push — master ( dfc64f...43826b )
by Fabien
51:13
created

LanguageService::getDataService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Fab\Vidi\Language;
3
4
/*
5
 * This file is part of the Fab/Vidi project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use Fab\Vidi\Service\DataService;
12
use Fab\Vidi\Utility\BackendUtility;
13
use TYPO3\CMS\Core\SingletonInterface;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use Fab\Vidi\Domain\Model\Content;
16
use Fab\Vidi\Tca\Tca;
17
18
/**
19
 * A class for handling language in the Backend.
20
 */
21
class LanguageService implements SingletonInterface
22
{
23
24
    /**
25
     * @var array
26
     */
27
    protected $languages;
28
29
    /**
30
     * @var array
31
     */
32
    protected $defaultIcon;
33
34
    /**
35
     * Store the localized records to boost up performance.
36
     *
37
     * @var array
38
     */
39
    protected $localizedRecordStorage;
40
41
    /**
42
     * Returns available language records.
43
     * The method stores the records in the property to speed up the process as the method can be often called.
44
     *
45
     * @return array
46
     */
47
    public function getLanguages()
48
    {
49
        if ($this->languages === null) {
50
            $this->languages = $this->getDataService()->getRecords('sys_language');
51
        }
52
        return $this->languages;
53
    }
54
55
    /**
56
     * Returns a localized record according to a Content object and a language identifier.
57
     * Notice! This method does not overlay anything but simply returns the raw localized record.
58
     *
59
     * @param Content $object
60
     * @param int $language
61
     * @return Content
62
     */
63
    public function getLocalizedContent(Content $object, $language)
64
    {
65
66
        // We want to cache data per Content object. Retrieve the Object hash.
67
        $objectHash = spl_object_hash($object);
68
69
        // Initialize the storage
70
        if (empty($this->localizedRecordStorage[$objectHash])) {
71
            $this->localizedRecordStorage[$objectHash] = [];
72
        }
73
74
        if (empty($this->localizedRecordStorage[$objectHash][$language])) {
75
76
            $localizedRecord = $this->getDataService()->getRecord(
77
                $object->getDataType(),
78
                [
79
                    Tca::table($object)->getLanguageParentField() => $object->getUid(), // e.g. l10n_parent
80
                    Tca::table($object)->getLanguageField() => $language, // e.g. sys_language_uid
81
                ]
82
            );
83
84
            if ($localizedRecord) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $localizedRecord of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
                $localizedContent = GeneralUtility::makeInstance(\Fab\Vidi\Domain\Model\Content::class, $object->getDataType(), $localizedRecord);
86
                $this->localizedRecordStorage[$objectHash][$language] = $localizedContent;
87
            } else {
88
                $this->localizedRecordStorage[$objectHash][$language] = []; // We want an array at least, even if empty.
89
            }
90
        }
91
92
        return $this->localizedRecordStorage[$objectHash][$language];
93
    }
94
95
    /**
96
     * Tell whether the given Content object has a localization.
97
     *
98
     * @param Content $object
99
     * @param int $language
100
     * @return string
101
     */
102
    public function hasLocalization(Content $object, $language)
103
    {
104
        $localizedRecord = $this->getLocalizedContent($object, $language);
105
        return !empty($localizedRecord);
106
    }
107
108
    /**
109
     * Returns a localized field according to a Content object and a language identifier.
110
     * Notice! If there is not translation, simply returns an empty string.
111
     *
112
     * @param Content $object
113
     * @param int $language
114
     * @param string $fieldName
115
     * @return string
116
     */
117
    public function getLocalizedFieldName(Content $object, $language, $fieldName)
118
    {
119
        $localizedRecord = $this->getLocalizedContent($object, $language);
120
        return empty($localizedRecord) ? '' : $localizedRecord[$fieldName];
121
    }
122
123
    /**
124
     * Returns the default language configured by TSConfig.
125
     *
126
     * @return array
127
     */
128
    public function getDefaultFlag()
129
    {
130
131
        if ($this->defaultIcon === null) {
132
133
            $defaultFlag = ''; // default value
134
135
            $tsConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig(0, 'mod.SHARED');
136
137
            // Fallback non sprite-configuration
138
            if (($pos = strrpos($tsConfig['properties']['defaultLanguageFlag'], '.')) !== false) {
139
                $defaultFlag = substr($tsConfig['properties']['defaultLanguageFlag'], 0, $pos);
140
            }
141
142
            $this->defaultIcon = $defaultFlag;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaultFlag of type string is incompatible with the declared type array of property $defaultIcon.

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...
143
        }
144
145
        return $this->defaultIcon;
146
    }
147
148
    /**
149
     * Returns whether the system includes language records.
150
     *
151
     * @return bool
152
     */
153
    public function hasLanguages()
154
    {
155
        $languages = $this->getLanguages();
156
        return !empty($languages);
157
    }
158
159
    /**
160
     * Tell whether the given language exists.
161
     *
162
     * @param int $language
163
     * @return bool
164
     */
165
    public function languageExists($language)
166
    {
167
        $languages = $this->getLanguages();
168
169
        $LanguageExists = false;
170
        foreach ($languages as $_language) {
171
            if ((int)$_language['uid'] === (int)$language) {
172
                $LanguageExists = true;
173
                break;
174
            }
175
        }
176
177
        return $LanguageExists;
178
    }
179
180
    /**
181
     * @return object|DataService
182
     */
183
    protected function getDataService(): DataService
184
    {
185
        return GeneralUtility::makeInstance(DataService::class);
186
    }
187
188
}
189