|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fab\Vidi\Module; |
|
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 TYPO3\CMS\Core\SingletonInterface; |
|
13
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class used to retrieve module preferences. |
|
17
|
|
|
*/ |
|
18
|
|
|
class ModulePreferences implements SingletonInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $preferences; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tableName = 'tx_vidi_preference'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $key |
|
33
|
|
|
* @param string $dataType |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get($key, $dataType = '') |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
if (empty($dataType)) { |
|
40
|
|
|
$dataType = $this->getModuleLoader()->getDataType(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!$this->isLoaded($dataType)) { |
|
44
|
|
|
$this->load($dataType); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$value = empty($this->preferences[$dataType][$key]) ? null : $this->preferences[$dataType][$key]; |
|
48
|
|
|
return $value; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Tell whether the module is loaded. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $dataType |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function isLoaded($dataType) |
|
58
|
|
|
{ |
|
59
|
|
|
return !empty($this->preferences[$dataType]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $dataType |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getAll($dataType = '') |
|
67
|
|
|
{ |
|
68
|
|
|
|
|
69
|
|
|
if (empty($dataType)) { |
|
70
|
|
|
$dataType = $this->getModuleLoader()->getDataType(); |
|
71
|
|
|
} |
|
72
|
|
|
$this->load($dataType); |
|
73
|
|
|
return $this->preferences[$dataType]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get the md5 signature of the preferences. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $dataType |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getSignature($dataType = '') |
|
83
|
|
|
{ |
|
84
|
|
|
$preferences = $this->getAll($dataType); |
|
85
|
|
|
return md5(serialize($preferences)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Load preferences. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $dataType |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function load($dataType) |
|
95
|
|
|
{ |
|
96
|
|
|
|
|
97
|
|
|
// Fetch preferences from different sources and overlay them |
|
98
|
|
|
$databasePreferences = $this->fetchPreferencesFromDatabase($dataType); |
|
99
|
|
|
$generalPreferences = $this->fetchGlobalPreferencesFromTypoScript(); |
|
100
|
|
|
$specificPreferences = $this->fetchExtraPreferencesFromTypoScript($dataType); |
|
101
|
|
|
|
|
102
|
|
|
$preferences = array_merge($generalPreferences, $specificPreferences, $databasePreferences); |
|
103
|
|
|
$this->preferences[$dataType] = $preferences; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Save preferences |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $preferences |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public function save($preferences) |
|
113
|
|
|
{ |
|
114
|
|
|
$configurableParts = ConfigurablePart::getParts(); |
|
115
|
|
|
|
|
116
|
|
|
$dataType = $this->getModuleLoader()->getDataType(); |
|
117
|
|
|
$this->getDataService()->delete( |
|
118
|
|
|
$this->tableName, |
|
119
|
|
|
[ |
|
120
|
|
|
'data_type' => $dataType |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
$sanitizedPreferences = []; |
|
125
|
|
|
foreach ($preferences as $key => $value) { |
|
126
|
|
|
if (in_array($key, $configurableParts)) { |
|
127
|
|
|
$sanitizedPreferences[$key] = $value; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->getDataService()->insert( |
|
132
|
|
|
$this->tableName, |
|
133
|
|
|
[ |
|
134
|
|
|
'data_type' => $dataType, |
|
135
|
|
|
'preferences' => serialize($sanitizedPreferences), |
|
136
|
|
|
] |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $dataType |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public function fetchPreferencesFromDatabase($dataType) |
|
145
|
|
|
{ |
|
146
|
|
|
$preferences = []; |
|
147
|
|
|
$record = $this->getDataService()->getRecord( |
|
148
|
|
|
$this->tableName, |
|
149
|
|
|
[ |
|
150
|
|
|
'data_type' => $dataType |
|
151
|
|
|
] |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
if (!empty($record)) { |
|
155
|
|
|
$preferences = unserialize($record['preferences']); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $preferences; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns the module settings. |
|
163
|
|
|
* |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function fetchGlobalPreferencesFromTypoScript() |
|
167
|
|
|
{ |
|
168
|
|
|
$settings = $this->getSettings(); |
|
169
|
|
|
|
|
170
|
|
|
$configurableParts = ConfigurablePart::getParts(); |
|
171
|
|
|
$preferences = []; |
|
172
|
|
|
foreach ($settings as $key => $value) { |
|
173
|
|
|
if (in_array($key, $configurableParts)) { |
|
174
|
|
|
$preferences[$key] = $value; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $preferences; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the module settings. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $dataType |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function fetchExtraPreferencesFromTypoScript($dataType) |
|
188
|
|
|
{ |
|
189
|
|
|
$generalSettings = $this->getSettings(); |
|
190
|
|
|
|
|
191
|
|
|
$preferences = []; |
|
192
|
|
|
if (isset($generalSettings[$dataType . '.'])) { |
|
193
|
|
|
$settings = $generalSettings[$dataType . '.']; |
|
194
|
|
|
|
|
195
|
|
|
$configurableParts = ConfigurablePart::getParts(); |
|
196
|
|
|
foreach ($settings as $key => $value) { |
|
197
|
|
|
if (in_array($key, $configurableParts)) { |
|
198
|
|
|
$preferences[$key] = $value; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $preferences; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns the module settings. |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
View Code Duplication |
protected function getSettings() |
|
|
|
|
|
|
212
|
|
|
{ |
|
213
|
|
|
/** @var \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager $backendConfigurationManager */ |
|
214
|
|
|
$objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class); |
|
215
|
|
|
$backendConfigurationManager = $objectManager->get(\TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager::class); |
|
216
|
|
|
$configuration = $backendConfigurationManager->getTypoScriptSetup(); |
|
217
|
|
|
return $configuration['module.']['tx_vidi.']['settings.']; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return object|DataService |
|
222
|
|
|
*/ |
|
223
|
|
|
protected function getDataService(): DataService |
|
224
|
|
|
{ |
|
225
|
|
|
return GeneralUtility::makeInstance(DataService::class); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get the Vidi Module Loader. |
|
230
|
|
|
* |
|
231
|
|
|
* @return \Fab\Vidi\Module\ModuleLoader|object |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function getModuleLoader() |
|
234
|
|
|
{ |
|
235
|
|
|
return GeneralUtility::makeInstance(\Fab\Vidi\Module\ModuleLoader::class); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.