[TASK] Re-work/simplify copyright header in PHP files - Part 8
[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 * Utilities to process flexForms
18 */
19 class FlexFormService implements \TYPO3\CMS\Core\SingletonInterface {
20
21 /**
22 * Parses the flexForm content and converts it to an array
23 * The resulting array will be multi-dimensional, as a value "bla.blubb"
24 * results in two levels, and a value "bla.blubb.bla" results in three levels.
25 *
26 * Note: multi-language flexForms are not supported yet
27 *
28 * @param string $flexFormContent flexForm xml string
29 * @param string $languagePointer language pointer used in the flexForm
30 * @param string $valuePointer value pointer used in the flexForm
31 * @return array the processed array
32 */
33 public function convertFlexFormContentToArray($flexFormContent, $languagePointer = 'lDEF', $valuePointer = 'vDEF') {
34 $settings = array();
35 $flexFormArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormContent);
36 $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : array();
37 foreach (array_values($flexFormArray) as $languages) {
38 if (!is_array($languages[$languagePointer])) {
39 continue;
40 }
41 foreach ($languages[$languagePointer] as $valueKey => $valueDefinition) {
42 if (strpos($valueKey, '.') === FALSE) {
43 $settings[$valueKey] = $this->walkFlexFormNode($valueDefinition, $valuePointer);
44 } else {
45 $valueKeyParts = explode('.', $valueKey);
46 $currentNode = &$settings;
47 foreach ($valueKeyParts as $valueKeyPart) {
48 $currentNode = &$currentNode[$valueKeyPart];
49 }
50 if (is_array($valueDefinition)) {
51 if (array_key_exists($valuePointer, $valueDefinition)) {
52 $currentNode = $valueDefinition[$valuePointer];
53 } else {
54 $currentNode = $this->walkFlexFormNode($valueDefinition, $valuePointer);
55 }
56 } else {
57 $currentNode = $valueDefinition;
58 }
59 }
60 }
61 }
62 return $settings;
63 }
64
65 /**
66 * Parses a flexForm node recursively and takes care of sections etc
67 *
68 * @param array $nodeArray The flexForm node to parse
69 * @param string $valuePointer The valuePointer to use for value retrieval
70 * @return array
71 */
72 public function walkFlexFormNode($nodeArray, $valuePointer = 'vDEF') {
73 if (is_array($nodeArray)) {
74 $return = array();
75 foreach ($nodeArray as $nodeKey => $nodeValue) {
76 if ($nodeKey === $valuePointer) {
77 return $nodeValue;
78 }
79 if (in_array($nodeKey, array('el', '_arrayContainer'))) {
80 return $this->walkFlexFormNode($nodeValue, $valuePointer);
81 }
82 if ($nodeKey[0] === '_') {
83 continue;
84 }
85 if (strpos($nodeKey, '.')) {
86 $nodeKeyParts = explode('.', $nodeKey);
87 $currentNode = &$return;
88 for ($i = 0; $i < count($nodeKeyParts) - 1; $i++) {
89 $currentNode = &$currentNode[$nodeKeyParts[$i]];
90 }
91 $newNode = array(next($nodeKeyParts) => $nodeValue);
92 $currentNode = $this->walkFlexFormNode($newNode, $valuePointer);
93 } elseif (is_array($nodeValue)) {
94 if (array_key_exists($valuePointer, $nodeValue)) {
95 $return[$nodeKey] = $nodeValue[$valuePointer];
96 } else {
97 $return[$nodeKey] = $this->walkFlexFormNode($nodeValue, $valuePointer);
98 }
99 } else {
100 $return[$nodeKey] = $nodeValue;
101 }
102 }
103 return $return;
104 }
105 return $nodeArray;
106 }
107 }