[TASK] Streamline structure of form extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Factory / TyposcriptToJson.php
1 <?php
2 declare(encoding = 'utf-8');
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Patrick Broens (patrick@patrickbroens.nl)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Typoscript to JSON converter
29 *
30 * Takes the incoming Typoscript and converts it to Json
31 *
32 * @author Patrick Broens <patrick@patrickbroens.nl>
33 * @package TYPO3
34 * @subpackage form
35 */
36 class tx_form_domain_factory_typoscripttojson {
37 /**
38 * Constructor
39 *
40 * @return void
41 * @author Patrick Broens <patrick@patrickbroens.nl>
42 */
43 public function __construct() {
44 }
45
46 /**
47 * Convert TypoScript string to JSON
48 *
49 * @param string $typoscript TypoScript string containing all configuration for the form
50 * @return string The JSON for the form
51 * @author Patrick Broens <patrick@patrickbroens.nl>
52 */
53 public function convert($typoscript) {
54 $this->setValidationRules($typoscript);
55 $jsonObject = $this->createElement('form', $typoscript);
56
57 return $jsonObject;
58 }
59
60 /**
61 * Create element by loading class
62 * and instantiating the object
63 *
64 * @param string $class Type of element
65 * @param array $arguments Configuration array
66 * @return object
67 * @author Patrick Broens <patrick@patrickbroens.nl>
68 */
69 public function createElement($class, array $arguments = array()) {
70 $class = strtolower((string) $class);
71 $className = 'tx_form_domain_model_json_' . $class;
72
73 $this->addValidationRules($arguments);
74
75 $object = t3lib_div::makeInstance($className);
76 $object->setParameters($arguments);
77
78 if ($object->childElementsAllowed()) {
79 $this->getChildElementsByIntegerKey($object, $arguments);
80 }
81
82 return $object;
83 }
84
85 /**
86 * Rendering of a "numerical array" of Form objects from TypoScript
87 * Creates new object for each element found
88 *
89 * @param array $arguments Configuration array
90 * @return void
91 * @author Patrick Broens <patrick@patrickbroens.nl>
92 */
93 private function getChildElementsByIntegerKey(&$parentElement, $typoscript) {
94 if (is_array($typoscript)) {
95 $keys = t3lib_TStemplate::sortedKeyList($typoscript);
96 foreach ($keys as $key) {
97 $class = $typoscript[$key];
98 if (intval($key) && !strstr($key, '.')) {
99 if(isset($typoscript[$key . '.'])) {
100 $elementArguments = $typoscript[$key . '.'];
101 } else {
102 $elementArguments = array();
103 }
104 $this->setElementType($parentElement, $class, $elementArguments);
105 }
106 }
107 }
108 }
109
110 /**
111 * Set the element type of the object
112 *
113 * Checks if the typoscript object is part of the FORM or has a predefined
114 * class for name or header object
115 *
116 * @param object $parentElement The parent object
117 * @param string $class A predefined class
118 * @param array $arguments Configuration array
119 * @return void
120 * @author Patrick Broens <patrick@patrickbroens.nl>
121 */
122 private function setElementType(&$parentElement, $class, array $arguments) {
123 if (in_array($class, tx_form_Common::getInstance()->getFormObjects())) {
124 if (strstr($arguments['class'], 'predefined-name')) {
125 $class = 'NAME';
126 }
127 $this->addElement($parentElement, $class, $arguments);
128 } elseif (strstr($arguments['class'], 'content-header')) {
129 $class = 'HEADER';
130 $this->addElement($parentElement, $class, $arguments);
131 }
132 }
133
134 /**
135 * Add child object to this element
136 *
137 * @param object The parent object
138 * @param string $class Type of element
139 * @param array $arguments Configuration array
140 * @return void
141 * @author Patrick Broens <patrick@patrickbroens.nl>
142 */
143 public function addElement(&$parentElement, $class, array $arguments) {
144 $element = $this->createElement($class, $arguments);
145 $parentElement->addElement($element);
146 }
147
148 /**
149 * Set the validation rules
150 *
151 * @param array $typoscript Configuration array
152 * @return void
153 * @author Patrick Broens <patrick@patrickbroens.nl>
154 */
155 private function setValidationRules($typoscript) {
156 if (isset($typoscript['rules.']) && is_array($typoscript['rules.'])) {
157 $this->validationRules = $typoscript['rules.'];
158 }
159 }
160
161 /**
162 * Add validation rules to an element if available
163 *
164 * In TypoScript the validation rules belong to the form and are connected
165 * to the elements by name. However, in the wizard, they are added to the
166 * element for usability
167 *
168 * @param array $arguments The element arguments
169 * @return void
170 * @author Patrick Broens <patrick@patrickbroens.nl>
171 */
172 private function addValidationRules(&$arguments) {
173 $validationRulesAvailable = FALSE;
174
175 if (!empty($this->validationRules) && isset($arguments['name'])) {
176 foreach ($this->validationRules as $key => $ruleName) {
177 if (intval($key) && !strstr($key, '.')) {
178 $ruleConfiguration = array();
179 if(isset($this->validationRules[$key . '.'])) {
180 $ruleConfiguration = $this->validationRules[$key . '.'];
181 if (isset($ruleConfiguration['element']) && $ruleConfiguration['element'] === $arguments['name']) {
182 $arguments['validation'][$ruleName] = $ruleConfiguration;
183 }
184 }
185 }
186 }
187 }
188 }
189 }
190 ?>