Completed
Push — cleanup-service ( 79a539...af4cdb )
by Romain
02:30
created

ContextService::translate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 3
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
18
use TYPO3\CMS\Core\SingletonInterface;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Service\EnvironmentService;
21
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22
23
class ContextService implements SingletonInterface
24
{
25
    use ExtendedFacadeInstanceTrait;
26
27
    /**
28
     * @var EnvironmentService
29
     */
30
    protected $environmentService;
31
32
    /**
33
     * @var TypoScriptService
34
     */
35
    protected $typoScriptService;
36
37
    /**
38
     * Returns a unique hash for the context of the current request, depending
39
     * on whether the request comes from frontend or backend.
40
     *
41
     * @return string
42
     */
43
    public function getContextHash()
44
    {
45
        return ($this->environmentService->isEnvironmentInFrontendMode())
46
            ? 'fe-' . Core::get()->getPageController()->id
47
            : 'be-' . StringService::get()->sanitizeString(GeneralUtility::_GET('M'));
48
    }
49
50
    /**
51
     * Returns the current language key.
52
     *
53
     * @return string
54
     */
55
    public function getLanguageKey()
56
    {
57
        $languageKey = 'unknown';
58
59
        if ($this->environmentService->isEnvironmentInFrontendMode()) {
60
            $pageController = Core::get()->getPageController();
61
62
            if (isset($pageController->config['config']['language'])) {
63
                $languageKey = $pageController->config['config']['language'];
64
            }
65
        } else {
66
            $backendUser = Core::get()->getBackendUser();
67
68
            if (strlen($backendUser->uc['lang']) > 0) {
69
                $languageKey = $backendUser->uc['lang'];
70
            }
71
        }
72
73
        return $languageKey;
74
    }
75
76
    /**
77
     * Translation handler. Does the same job as Extbase translation tools,
78
     * expect that if the index to the LLL reference is not found, the index is
79
     * returned (Extbase would have returned an empty string).
80
     *
81
     * @param    string $index        The index to the LLL reference.
82
     * @param    string $extensionKey Key of the extension containing the LLL reference.
83
     * @param    array  $arguments    Arguments passed over to vsprintf.
84
     * @return   string               The translated string.
85
     */
86
    public function translate($index, $extensionKey = null, $arguments = null)
87
    {
88
        $extensionKey = ($extensionKey) ?: ExtensionService::get()->getExtensionKey();
89
        $result = LocalizationUtility::translate($index, $extensionKey, $arguments);
90
91
        if ($result === '' && $index !== '') {
92
            $result = $index;
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Will check if the TypoScript was actually included, as it contains
100
     * required configuration to make the forms work properly.
101
     *
102
     * @return bool
103
     */
104
    public function isTypoScriptIncluded()
105
    {
106
        return null !== $this->typoScriptService->getExtensionConfigurationFromPath('settings.typoScriptIncluded');
107
    }
108
109
    /**
110
     * @param EnvironmentService $environmentService
111
     */
112
    public function injectEnvironmentService(EnvironmentService $environmentService)
113
    {
114
        $this->environmentService = $environmentService;
115
    }
116
117
    /**
118
     * @param TypoScriptService $typoScriptService
119
     */
120
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
121
    {
122
        $this->typoScriptService = $typoScriptService;
123
    }
124
}
125