[BUGFIX] Unused TDParams in ColumnsContentObject()
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / FlexFormService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Utilities to process flexForms
32 */
33 class FlexFormService implements \TYPO3\CMS\Core\SingletonInterface {
34
35 /**
36 * Parses the flexForm content and converts it to an array
37 * The resulting array will be multi-dimensional, as a value "bla.blubb"
38 * results in two levels, and a value "bla.blubb.bla" results in three levels.
39 *
40 * Note: multi-language flexForms are not supported yet
41 *
42 * @param string $flexFormContent flexForm xml string
43 * @param string $languagePointer language pointer used in the flexForm
44 * @param string $valuePointer value pointer used in the flexForm
45 * @return array the processed array
46 */
47 public function convertFlexFormContentToArray($flexFormContent, $languagePointer = 'lDEF', $valuePointer = 'vDEF') {
48 $settings = array();
49 $flexFormArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormContent);
50 $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : array();
51 foreach (array_values($flexFormArray) as $languages) {
52 if (!is_array($languages[$languagePointer])) {
53 continue;
54 }
55 foreach ($languages[$languagePointer] as $valueKey => $valueDefinition) {
56 if (strpos($valueKey, '.') === FALSE) {
57 $settings[$valueKey] = $this->walkFlexFormNode($valueDefinition, $valuePointer);
58 } else {
59 $valueKeyParts = explode('.', $valueKey);
60 $currentNode = &$settings;
61 foreach ($valueKeyParts as $valueKeyPart) {
62 $currentNode = &$currentNode[$valueKeyPart];
63 }
64 if (is_array($valueDefinition)) {
65 if (array_key_exists($valuePointer, $valueDefinition)) {
66 $currentNode = $valueDefinition[$valuePointer];
67 } else {
68 $currentNode = $this->walkFlexFormNode($valueDefinition, $valuePointer);
69 }
70 } else {
71 $currentNode = $valueDefinition;
72 }
73 }
74 }
75 }
76 return $settings;
77 }
78
79 /**
80 * Parses a flexForm node recursively and takes care of sections etc
81 *
82 * @param array $nodeArray The flexForm node to parse
83 * @param string $valuePointer The valuePointer to use for value retrieval
84 * @return array
85 */
86 public function walkFlexFormNode($nodeArray, $valuePointer = 'vDEF') {
87 if (is_array($nodeArray)) {
88 $return = array();
89 foreach ($nodeArray as $nodeKey => $nodeValue) {
90 if ($nodeKey === $valuePointer) {
91 return $nodeValue;
92 }
93 if (in_array($nodeKey, array('el', '_arrayContainer'))) {
94 return $this->walkFlexFormNode($nodeValue, $valuePointer);
95 }
96 if (substr($nodeKey, 0, 1) === '_') {
97 continue;
98 }
99 if (strpos($nodeKey, '.')) {
100 $nodeKeyParts = explode('.', $nodeKey);
101 $currentNode = &$return;
102 for ($i = 0; $i < count($nodeKeyParts) - 1; $i++) {
103 $currentNode = &$currentNode[$nodeKeyParts[$i]];
104 }
105 $newNode = array(next($nodeKeyParts) => $nodeValue);
106 $currentNode = $this->walkFlexFormNode($newNode, $valuePointer);
107 } elseif (is_array($nodeValue)) {
108 if (array_key_exists($valuePointer, $nodeValue)) {
109 $return[$nodeKey] = $nodeValue[$valuePointer];
110 } else {
111 $return[$nodeKey] = $this->walkFlexFormNode($nodeValue, $valuePointer);
112 }
113 } else {
114 $return[$nodeKey] = $nodeValue;
115 }
116 }
117 return $return;
118 }
119 return $nodeArray;
120 }
121 }