6b1471afa26b9f265cb0f4c18ad08fe92085db80
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / FlexFormService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Utilities to process flexForms
19 */
20 class FlexFormService implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * Parses the flexForm content and converts it to an array
24 * The resulting array will be multi-dimensional, as a value "bla.blubb"
25 * results in two levels, and a value "bla.blubb.bla" results in three levels.
26 *
27 * Note: multi-language flexForms are not supported yet
28 *
29 * @param string $flexFormContent flexForm xml string
30 * @param string $languagePointer language pointer used in the flexForm
31 * @param string $valuePointer value pointer used in the flexForm
32 * @return array the processed array
33 */
34 public function convertFlexFormContentToArray($flexFormContent, $languagePointer = 'lDEF', $valuePointer = 'vDEF')
35 {
36 $settings = [];
37 $flexFormArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormContent);
38 $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : [];
39 foreach (array_values($flexFormArray) as $languages) {
40 if (!is_array($languages[$languagePointer])) {
41 continue;
42 }
43 foreach ($languages[$languagePointer] as $valueKey => $valueDefinition) {
44 if (strpos($valueKey, '.') === false) {
45 $settings[$valueKey] = $this->walkFlexFormNode($valueDefinition, $valuePointer);
46 } else {
47 $valueKeyParts = explode('.', $valueKey);
48 $currentNode = &$settings;
49 foreach ($valueKeyParts as $valueKeyPart) {
50 $currentNode = &$currentNode[$valueKeyPart];
51 }
52 if (is_array($valueDefinition)) {
53 if (array_key_exists($valuePointer, $valueDefinition)) {
54 $currentNode = $valueDefinition[$valuePointer];
55 } else {
56 $currentNode = $this->walkFlexFormNode($valueDefinition, $valuePointer);
57 }
58 } else {
59 $currentNode = $valueDefinition;
60 }
61 }
62 }
63 }
64 return $settings;
65 }
66
67 /**
68 * Parses a flexForm node recursively and takes care of sections etc
69 *
70 * @param array $nodeArray The flexForm node to parse
71 * @param string $valuePointer The valuePointer to use for value retrieval
72 * @return array
73 */
74 public function walkFlexFormNode($nodeArray, $valuePointer = 'vDEF')
75 {
76 if (is_array($nodeArray)) {
77 $return = [];
78 foreach ($nodeArray as $nodeKey => $nodeValue) {
79 if ($nodeKey === $valuePointer) {
80 return $nodeValue;
81 }
82 if (in_array($nodeKey, ['el', '_arrayContainer'])) {
83 return $this->walkFlexFormNode($nodeValue, $valuePointer);
84 }
85 if ($nodeKey[0] === '_') {
86 continue;
87 }
88 if (strpos($nodeKey, '.')) {
89 $nodeKeyParts = explode('.', $nodeKey);
90 $currentNode = &$return;
91 $nodeKeyPartsCount = count($nodeKeyParts);
92 for ($i = 0; $i < $nodeKeyPartsCount - 1; $i++) {
93 $currentNode = &$currentNode[$nodeKeyParts[$i]];
94 }
95 $newNode = [next($nodeKeyParts) => $nodeValue];
96 $subVal = $this->walkFlexFormNode($newNode, $valuePointer);
97 $currentNode[key($subVal)] = current($subVal);
98 } elseif (is_array($nodeValue)) {
99 if (array_key_exists($valuePointer, $nodeValue)) {
100 $return[$nodeKey] = $nodeValue[$valuePointer];
101 } else {
102 $return[$nodeKey] = $this->walkFlexFormNode($nodeValue, $valuePointer);
103 }
104 } else {
105 $return[$nodeKey] = $nodeValue;
106 }
107 }
108 return $return;
109 }
110 return $nodeArray;
111 }
112 }