forked from franzholz/div2007
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.tx_div2007_configurations.php
304 lines (286 loc) · 9.61 KB
/
class.tx_div2007_configurations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* A class to load, transport and deliver the setup parameters.
*
* Copyright (c) 2006-2011 Elmar Hinz
*
* LICENSE:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package TYPO3
* @subpackage div2007
* @author Elmar Hinz <elmar.hinz@team-red.net>
* @copyright 2006-2011 Elmar Hinz
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @since 0.1
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* A class to load, transport and deliver the setup parameters.
*
* Member of the central quad: $controller, $parameters, $configurations, $context.<br>
* Address it from every controlled object as: <samp>$this->controller->configurations</samp>
*
* All configuration parameters should be accessed from all other classes by a
* $configurations object. Either use this class directly or as parent for your
* own inherited class.
*
* Configuration is loaded from TS. Flexform configuration overwrites it.
*
* 1. setTypoScriptConfiguration(): loads the TS array given to controller->main() by the outer framework.
* 2. setFlexFormConfiguration(): loads flexform data (xml or array) into the object
*
* Details:
*
* 1. The $configurationArray of rendered TypoScript that is handeled to the main function
* of plugins by the outer framework:
* <samp>tx_myextension_controller->main($input, $configurationArray)</samp>.
* 2. The flexform configuration to gives the enduser the option to overwrite some of the previous
* settings.
*
* Depends on: tx_div2007, tx_div2007_object <br>
* Instantiated in: $this->controller <br>
* Used by: almoust every class
*
* @author Elmar Hinz <elmar.hinz@team-red.net>
* @package TYPO3
* @subpackage div2007
*/
// deprecated: will be removed in 2024
class tx_div2007_configurations extends tx_div2007_object {
// -------------------------------------------------------------------------------------
// Setters
// -------------------------------------------------------------------------------------
/**
* Load a (local) configuration array into the object
*
* An configuration array of rendered TypoScript like it is handeled to the main function
* of plugins by the outer framework: tx_myextension_controller->main($out, $configurationArray).
*
* @param array direct setup input in form of a renderd TS array
* @return void
*/
public function setTypoScriptConfiguration ($configuration) {
if(is_array($configuration['configurations.'])) {
$configuration = $configuration['configurations.'];
}
foreach((array)$configuration as $key => $value) {
$this->set($key, $value);
}
}
/**
* Load flexformdata into the object
*
* Takes a xml string or an already rendered array.
* Typically it would come from the field tt_content.pi_flexform
*
* This configuration assumes unique key names for the fields.
* The names of the sheets are of no relevance.
* (If you need a more sophisticated solution simply write a your
* own loader function in an inherited class.)
*
* @param mixed xml or rendered flexform array
* @return void
*/
public function setFlexFormConfiguration ($xmlOrArray) {
$languagePointer = 'lDEF'; // we don't support languages here for now
$valuePointer = 'vDEF'; // also hardcoded here
if (!$xmlOrArray) {
return false;
}
// Converting flexform data into array if neccessary
if (is_array($xmlOrArray)) {
$array = $xmlOrArray;
} else {
$array = GeneralUtility::xml2array($xmlOrArray);
}
$data = $array['data'];
//
foreach((array) $data as $sheet => $languages) {
foreach((array) $languages[$languagePointer] as $key => $def) {
$this->set($key, $def[$valuePointer]);
}
}
}
// -------------------------------------------------------------------------------------
// Getters
// -------------------------------------------------------------------------------------
/**
* Get a value or an array by providing a relative pathKey
*
* The provided pathKey is relative to the part of the TS-Setup you have loaded. Examples:
*
* <pre>
* Absolute Setup: 'plugin.tx_myextension.configuration.parts.10.id = 33'
* Loaded Path: 'plugin.tx_myextension.configuration.'
* Resulting relative pathKey of a value: 'parts.10.id'
* Resulting relative pathKey of an array: 'parts.'
* Resulting relative pathKey of an array: 'parts.10.'
* </pre>
*
* Mind: To query an array end with a DOT. To query a single value end without DOT.
*
* @param string relative setupPath
*/
public function get ($pathKey): array|string {
return $this->_queryArrayByPath($this->getArrayCopy(), $pathKey);
}
/**
* Get an array by providing a relative pathKey
*
* The provided pathKey is relative to the part of the TS-Setup you have loaded. Examples:
*
* @see tx_div2007_configurations::get()
* @param string relative setupPath
* @param string pattern used to split the result into an array
* @return array
*/
public function getExploded ($pathKey, $pattern = '/[\s,]+/') {
return (array) preg_split($pattern, $this->get($pathKey));
}
/**
* Query a uniform hash from a dataset like setup
*
* <code>
* persons.10.id = 103
* persons.10.firstName = Peter
* persons.10.surName = Posters
* persons.10.yearOfBirth = 1973
* persons.20.id = 104
* persons.20.firstName = Susan
* persons.20.surName = Sunny
* persons.20.yearOfBirth = 1965
* persons.30.id = 105
* persons.30.firstName = Mary
* persons.30.surName = Martins
* persons.30.yearOfBirth = 1989
*
* usage: $configurations->queryHash('persons.', 'firstName', 'yearOfBirth');
* result: array('Peter' => '1973', 'Susan' => '1965', 'Mary' => '1989');
* </code>
*
* @param string relative pathKey
* @param string key of of the wanted key
* @param string key of of the wanted value
* @return array wanted Hash (key-value-pairs)
*/
public function queryHash ($pathKey, $keyName, $valueName) {
$selection = $this->get($pathKey);
$array = array();
foreach($selection as $set) {
$array[$set[$keyName]] = $set[$valueName];
}
return $array;
}
/**
* Query a single dataset from a list of datasets by a key entry
*
* <code>
* persons.10.id = 103
* persons.10.firstName = Peter
* persons.10.surName = Posters
* persons.10.yearOfBirth = 1973
* persons.20.id = 104
* persons.20.firstName = Susan
* persons.20.surName = Sunny
* persons.20.yearOfBirth = 1965
* persons.30.id = 105
* persons.30.firstName = Mary
* persons.30.surName = Martins
* persons.30.yearOfBirth = 1989
*
* usage: $configurations->queryDataSet('persons.', 'id', 104);
* result: array('id' => '104', 'firstName' => 'Susan', 'surName' => 'Sunny', 'yearOfBirth' => '1965')
* </code>
*
* @param string relative pathKey
* @param string key of key
* @param string value of key
* @return array wanted dataset
*/
public function queryDataSet ($path, $key, $value) {
$selection = $this->get($path);
foreach($selection as $set) {
if ($set[$key] == $value) {
return $set;
}
}
}
/**
* Query a single data from a list of datasets by a combination of key entries
*
* <code>
* persons.10.id = 103
* persons.10.firstName = Peter
* persons.10.surName = Posters
* persons.10.yearOfBirth = 1973
* persons.20.id = 104
* persons.20.firstName = Susan
* persons.20.surName = Sunny
* persons.20.yearOfBirth = 1965
* persons.30.id = 105
* persons.30.firstName = Mary
* persons.30.surName = Martins
* persons.30.yearOfBirth = 1989
*
* usage: $configurations->queryData('persons.', 'id', 104, 'yearOfBirth');
* result: '1965'
* </code>
*
* @param string relative pathKey
* @param string key of key
* @param string value of key
* @param string key of the wanted result value
* @return string wanted value
*/
public function queryData ($path, $key, $value, $wanted) {
$selection = $this->get($path);
foreach($selection as $set) {
if ($set[$key] == $value) {
return $set[$wanted];
}
}
}
// -------------------------------------------------------------------------------------
// Private functions
// -------------------------------------------------------------------------------------
/**
* ...
*
* @param string ...
* @param string ...
* @return array ...
* @access private
*/
public function _queryArrayByPath ($array, $path) {
$pathArray = explode('.', trim($path));
for($i = 0; $i < count($pathArray); $i++) {
if ($i < (count($pathArray) -1 )) {
$array = $array[$pathArray[$i] . '.'];
} elseif(empty($pathArray[$i])) {
// It ends with a dot. We return the rest of the array
return $array;
} else {
// It endes without a dot. We return the value.
return $array[$pathArray[$i]];
}
}
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/div2007/class.tx_div2007_configurations.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/div2007/class.tx_div2007_configurations.php']);
}
?>