|
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\Core; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Service\ExtensionService; |
|
17
|
|
|
use Romm\Formz\Service\TypoScriptService; |
|
18
|
|
|
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; |
|
19
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
20
|
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; |
|
21
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
22
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
|
23
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
|
24
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; |
|
25
|
|
|
use TYPO3\CMS\Extbase\Service\EnvironmentService; |
|
26
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
|
27
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class containing general functions. |
|
31
|
|
|
*/ |
|
32
|
|
|
class Core implements SingletonInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Core |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $instance; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ObjectManagerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $objectManager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var TypoScriptService |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $typoScriptService; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var EnvironmentService |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $environmentService; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Contains the actual language key. |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $languageKey; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Core |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function get() |
|
65
|
|
|
{ |
|
66
|
|
|
if (null === self::$instance) { |
|
67
|
|
|
/** @var ObjectManager $objectManager */ |
|
68
|
|
|
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
|
69
|
|
|
|
|
70
|
|
|
self::$instance = $objectManager->get(self::class); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return self::$instance; |
|
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
|
|
|
if ($result === '' && $index !== '') { |
|
91
|
|
|
$result = $index; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Converts an array to a clean JSON string which can be used by JavaScript. |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $array |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function arrayToJavaScriptJson(array $array) |
|
104
|
|
|
{ |
|
105
|
|
|
return json_encode($array, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_TAG); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the current language key. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getLanguageKey() |
|
114
|
|
|
{ |
|
115
|
|
|
if (null === $this->languageKey) { |
|
116
|
|
|
$this->languageKey = 'default'; |
|
117
|
|
|
|
|
118
|
|
|
if ($this->environmentService->isEnvironmentInFrontendMode()) { |
|
119
|
|
|
$pageController = $this->getPageController(); |
|
120
|
|
|
|
|
121
|
|
|
if (isset($pageController->config['config']['language'])) { |
|
122
|
|
|
$this->languageKey = $pageController->config['config']['language']; |
|
123
|
|
|
} |
|
124
|
|
|
} else { |
|
125
|
|
|
$backendUser = $this->getBackendUser(); |
|
126
|
|
|
|
|
127
|
|
|
if (strlen($backendUser->uc['lang']) > 0) { |
|
128
|
|
|
$this->languageKey = $backendUser->uc['lang']; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->languageKey; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Will check if the TypoScript was indeed included, as it contains required |
|
138
|
|
|
* configuration to make the forms work properly. |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
public function isTypoScriptIncluded() |
|
143
|
|
|
{ |
|
144
|
|
|
return null !== $this->typoScriptService->getExtensionConfigurationFromPath('settings.typoScriptIncluded'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string|null $path If a string is given, it will be precessed by the extension relative path and returned. |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getExtensionRelativePath($path = null) |
|
152
|
|
|
{ |
|
153
|
|
|
$relativePath = ExtensionManagementUtility::siteRelPath('formz'); |
|
154
|
|
|
|
|
155
|
|
|
if ($this->environmentService->isEnvironmentInBackendMode()) { |
|
156
|
|
|
$relativePath = '../' . $relativePath; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return (null !== $path) |
|
160
|
|
|
? $relativePath . $path |
|
161
|
|
|
: $relativePath; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $path |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getResourceRelativePath($path) |
|
169
|
|
|
{ |
|
170
|
|
|
$relativePath = rtrim( |
|
171
|
|
|
PathUtility::getRelativePath( |
|
172
|
|
|
GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'), |
|
173
|
|
|
GeneralUtility::getFileAbsFileName($path) |
|
174
|
|
|
), |
|
175
|
|
|
'/' |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
if ($this->environmentService->isEnvironmentInBackendMode()) { |
|
179
|
|
|
$relativePath = '../' . $relativePath; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $relativePath; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sanitizes a string: lower case with dash separation. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $string |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function sanitizeString($string) |
|
192
|
|
|
{ |
|
193
|
|
|
$string = str_replace('_', '-', GeneralUtility::camelCaseToLowerCaseUnderscored($string)); |
|
194
|
|
|
|
|
195
|
|
|
while (strpos($string, '--')) { |
|
196
|
|
|
$string = str_replace('--', '-', $string); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $string; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Returns a unique hash for the context of the current request, depending |
|
204
|
|
|
* on whether the request comes from frontend or backend. |
|
205
|
|
|
* |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getContextHash() |
|
209
|
|
|
{ |
|
210
|
|
|
return ($this->environmentService->isEnvironmentInFrontendMode()) |
|
211
|
|
|
? 'fe-' . $this->getPageController()->id |
|
212
|
|
|
: 'be-' . $this->sanitizeString(GeneralUtility::_GET('M')); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Shortcut for object manager `get()` function. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $className |
|
219
|
|
|
* @return object |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function instantiate($className) |
|
222
|
|
|
{ |
|
223
|
|
|
$objectManager = self::get()->getObjectManager(); |
|
224
|
|
|
|
|
225
|
|
|
return call_user_func_array([$objectManager, 'get'], func_get_args()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return ObjectManagerInterface |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getObjectManager() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->objectManager; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param ObjectManagerInterface $objectManager |
|
238
|
|
|
*/ |
|
239
|
|
|
public function injectObjectManager(ObjectManagerInterface $objectManager) |
|
240
|
|
|
{ |
|
241
|
|
|
$this->objectManager = $objectManager; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param TypoScriptService $typoScriptService |
|
246
|
|
|
*/ |
|
247
|
|
|
public function injectTypoScriptService(TypoScriptService $typoScriptService) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->typoScriptService = $typoScriptService; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param EnvironmentService $environmentService |
|
254
|
|
|
*/ |
|
255
|
|
|
public function injectEnvironmentService(EnvironmentService $environmentService) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->environmentService = $environmentService; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @return TypoScriptFrontendController |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getPageController() |
|
264
|
|
|
{ |
|
265
|
|
|
return $GLOBALS['TSFE']; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return BackendUserAuthentication |
|
270
|
|
|
*/ |
|
271
|
|
|
public function getBackendUser() |
|
272
|
|
|
{ |
|
273
|
|
|
return $GLOBALS['BE_USER']; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|