TYPO3 CMS  TYPO3_7-6
JsonToTypoScript.php
Go to the documentation of this file.
1 <?php
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 
23 {
29  protected $elementId = 0;
30 
37  protected $validationRules = [];
38 
44  protected $validationRulesCounter = 1;
45 
55  public function convert($json)
56  {
57  $elements = json_decode((string)$json, true);
58  $typoscriptArray = [];
59  $typoscript = null;
60  $this->convertToTyposcriptArray($elements, $typoscriptArray);
61  if ($typoscriptArray['10.'] && is_array($typoscriptArray['10.']) && !empty($typoscriptArray['10.'])) {
62  $typoscript = $this->typoscriptArrayToString($typoscriptArray['10.']);
63  }
64  return $typoscript;
65  }
66 
76  protected function convertToTyposcriptArray(array $elements, array &$parent, $childrenWithParentName = false)
77  {
78  if (is_array($elements)) {
79  $elementCounter = 10;
80  foreach ($elements as $element) {
81  if ($element['xtype']) {
82  $this->elementId++;
83  switch ($element['xtype']) {
84  case 'typo3-form-wizard-elements-basic-button':
85 
86  case 'typo3-form-wizard-elements-basic-checkbox':
87 
88  case 'typo3-form-wizard-elements-basic-fileupload':
89 
90  case 'typo3-form-wizard-elements-basic-hidden':
91 
92  case 'typo3-form-wizard-elements-basic-password':
93 
94  case 'typo3-form-wizard-elements-basic-radio':
95 
96  case 'typo3-form-wizard-elements-basic-reset':
97 
98  case 'typo3-form-wizard-elements-basic-select':
99 
100  case 'typo3-form-wizard-elements-basic-submit':
101 
102  case 'typo3-form-wizard-elements-basic-textarea':
103 
104  case 'typo3-form-wizard-elements-basic-textline':
105 
106  case 'typo3-form-wizard-elements-predefined-email':
107 
108  case 'typo3-form-wizard-elements-content-header':
109 
110  case 'typo3-form-wizard-elements-content-textblock':
111  $this->getDefaultElementSetup($element, $parent, $elementCounter, $childrenWithParentName);
112  break;
113  case 'typo3-form-wizard-elements-basic-fieldset':
114 
115  case 'typo3-form-wizard-elements-predefined-name':
116  $this->getDefaultElementSetup($element, $parent, $elementCounter);
117  $this->getContainer($element, $parent, $elementCounter);
118  break;
119  case 'typo3-form-wizard-elements-predefined-checkboxgroup':
120 
121  case 'typo3-form-wizard-elements-predefined-radiogroup':
122  $this->getDefaultElementSetup($element, $parent, $elementCounter);
123  $this->getContainer($element, $parent, $elementCounter, true);
124  break;
125  case 'typo3-form-wizard-elements-basic-form':
126  $this->getDefaultElementSetup($element, $parent, $elementCounter);
127  $this->getContainer($element, $parent, $elementCounter);
128  $this->getForm($element, $parent, $elementCounter);
129  break;
130  default:
131 
132  }
133  }
134  $elementCounter = $elementCounter + 10;
135  }
136  }
137  }
138 
148  protected function getContainer(array $element, array &$parent, $elementCounter, $childrenWithParentName = false)
149  {
150  if ($element['elementContainer'] && $element['elementContainer']['items']) {
151  $this->convertToTyposcriptArray($element['elementContainer']['items'], $parent[$elementCounter . '.'], $childrenWithParentName);
152  }
153  }
154 
167  protected function getForm(array $element, array &$parent, $elementCounter)
168  {
169  // @todo Put at the top of the form
170  if (!empty($this->validationRules)) {
171  $parent[$elementCounter . '.']['rules'] = $this->validationRules;
172  }
173  }
174 
187  protected function getDefaultElementSetup(array $element, array &$parent, $elementCounter, $childrenWithParentName = false)
188  {
189  $contentObjectType = $this->getContentObjectType($element);
190  if (is_null($contentObjectType) === false) {
191  $parent[$elementCounter] = $contentObjectType;
192  $parent[$elementCounter . '.'] = [];
193  if ($element['configuration']) {
194  $this->setConfiguration($element, $parent, $elementCounter, $childrenWithParentName);
195  }
196  }
197  }
198 
205  protected function getContentObjectType(array $element)
206  {
207  $contentObjectType = null;
208  $shortXType = str_replace('typo3-form-wizard-elements-', '', $element['xtype']);
209  list($category, $type) = explode('-', $shortXType);
210  switch ($category) {
211  case 'basic':
212  $contentObjectType = strtoupper($type);
213  break;
214  case 'predefined':
215  switch ($type) {
216  case 'checkboxgroup':
217 
218  case 'radiogroup':
219  $contentObjectType = strtoupper($type);
220  break;
221  case 'email':
222  $contentObjectType = 'TEXTLINE';
223  break;
224  case 'name':
225  $contentObjectType = 'FIELDSET';
226  }
227  break;
228  case 'content':
229  switch ($type) {
230  case 'header':
231 
232  case 'textblock':
233  $contentObjectType = strtoupper($type);
234  }
235  default:
236 
237  }
238  return $contentObjectType;
239  }
240 
251  protected function setConfiguration(array $element, array &$parent, $elementCounter, $childrenWithParentName = false)
252  {
253  foreach ($element['configuration'] as $key => $value) {
254  switch ($key) {
255  case 'attributes':
256  $this->setAttributes($value, $parent, $elementCounter, $childrenWithParentName);
257  break;
258  case 'confirmation':
259  $this->setConfirmation($value, $parent, $elementCounter);
260  break;
261  case 'filters':
262  $this->setFilters($value, $parent, $elementCounter);
263  break;
264  case 'label':
265  $this->setLabel($value, $parent, $elementCounter);
266  break;
267  case 'layout':
268  $this->setLayout($element, $value, $parent, $elementCounter);
269  break;
270  case 'legend':
271  $this->setLegend($value, $parent, $elementCounter);
272  break;
273  case 'options':
274  $this->setOptions($element, $value, $parent, $elementCounter);
275  break;
276  case 'postProcessor':
277  $this->setPostProcessor($value, $parent, $elementCounter);
278  break;
279  case 'prefix':
280  $this->setPrefix($value, $parent, $elementCounter);
281  break;
282  case 'validation':
283  $this->setValidationRules($element, $value);
284  break;
285  case 'various':
286  $this->setVarious($element, $value, $parent, $elementCounter);
287  break;
288  default:
289 
290  }
291  }
292  }
293 
303  protected function setAttributes(array $attributes, array &$parent, $elementCounter, $childrenWithParentName = false)
304  {
305  foreach ($attributes as $key => $value) {
306  if ($key === 'name' && $value === '' && !$childrenWithParentName) {
307  $value = $this->elementId;
308  }
309  if ($value != '') {
310  $parent[$elementCounter . '.'][$key] = $value;
311  }
312  }
313  }
314 
325  protected function setConfirmation($confirmation, array &$parent, $elementCounter)
326  {
327  $parent[$elementCounter . '.']['confirmation'] = $confirmation;
328  }
329 
338  protected function setFilters(array $filters, array &$parent, $elementCounter)
339  {
340  if (!empty($filters)) {
341  $parent[$elementCounter . '.']['filters'] = [];
342  $filterCounter = 1;
343  foreach ($filters as $name => $filterConfiguration) {
344  $parent[$elementCounter . '.']['filters'][$filterCounter] = $name;
345  $parent[$elementCounter . '.']['filters'][$filterCounter . '.'] = $filterConfiguration;
346  $filterCounter++;
347  }
348  }
349  }
350 
359  protected function setLabel(array $label, array &$parent, $elementCounter)
360  {
361  if ($label['value'] != '') {
362  $parent[$elementCounter . '.']['label.']['value'] = $label['value'];
363  }
364  }
365 
379  protected function setLayout(array $element, $value, array &$parent, $elementCounter)
380  {
381  switch ($element['xtype']) {
382  case 'typo3-form-wizard-elements-basic-button':
383 
384  case 'typo3-form-wizard-elements-basic-fileupload':
385 
386  case 'typo3-form-wizard-elements-basic-password':
387 
388  case 'typo3-form-wizard-elements-basic-reset':
389 
390  case 'typo3-form-wizard-elements-basic-submit':
391 
392  case 'typo3-form-wizard-elements-basic-textline':
393  if ($value === 'back') {
394  $parent[$elementCounter . '.']['layout'] = '<input />' . LF . '<label />';
395  }
396  break;
397  case 'typo3-form-wizard-elements-basic-checkbox':
398 
399  case 'typo3-form-wizard-elements-basic-radio':
400  if ($value === 'front') {
401  $parent[$elementCounter . '.']['layout'] = '<label />' . LF . '<input />';
402  }
403  break;
404  case 'typo3-form-wizard-elements-basic-select':
405  if ($value === 'back') {
406  $parent[$elementCounter . '.']['layout'] = '<select>' . LF . '<elements />' . LF . '</select>' . LF . '<label />';
407  }
408  break;
409  case 'typo3-form-wizard-elements-basic-textarea':
410  if ($value === 'back') {
411  $parent[$elementCounter . '.']['layout'] = '<textarea />' . LF . '<label />';
412  }
413  break;
414  default:
415 
416  }
417  }
418 
427  protected function setLegend(array $legend, array &$parent, $elementCounter)
428  {
429  if ($legend['value'] != '') {
430  $parent[$elementCounter . '.']['legend.']['value'] = $legend['value'];
431  }
432  }
433 
447  protected function setOptions(array $element, array $options, array &$parent, $elementCounter)
448  {
449  if (is_array($options) && $element['xtype'] === 'typo3-form-wizard-elements-basic-select') {
450  $optionCounter = 10;
451  foreach ($options as $option) {
452  $parent[$elementCounter . '.'][$optionCounter] = 'OPTION';
453  $parent[$elementCounter . '.'][$optionCounter . '.']['text'] = $option['text'];
454  if (isset($option['attributes'])) {
455  $parent[$elementCounter . '.'][$optionCounter . '.'] += $option['attributes'];
456  }
457  $optionCounter = $optionCounter + 10;
458  }
459  }
460  }
461 
470  protected function setPostProcessor(array $postProcessors, array &$parent, $elementCounter)
471  {
472  if (!empty($postProcessors)) {
473  $parent[$elementCounter . '.']['postProcessor'] = [];
474  $postProcessorCounter = 1;
475  foreach ($postProcessors as $name => $postProcessorConfiguration) {
476  $parent[$elementCounter . '.']['postProcessor'][$postProcessorCounter] = $name;
477  $parent[$elementCounter . '.']['postProcessor'][$postProcessorCounter . '.'] = $postProcessorConfiguration;
478  $postProcessorCounter++;
479  }
480  }
481  }
482 
493  protected function setPrefix($prefix, array &$parent, $elementCounter)
494  {
495  $parent[$elementCounter . '.']['prefix'] = $prefix;
496  }
497 
508  protected function setValidationRules(array $element, array $validationRules)
509  {
510  foreach ($validationRules as $name => $ruleConfiguration) {
511  if (isset($element['configuration']['attributes']['name']) && $element['configuration']['attributes']['name'] != '') {
512  $ruleConfiguration['element'] = $element['configuration']['attributes']['name'];
513  } elseif (isset($element['configuration']['various']['name']) && $element['configuration']['various']['name'] != '') {
514  $ruleConfiguration['element'] = $element['configuration']['various']['name'];
515  } else {
516  $ruleConfiguration['element'] = $this->elementId;
517  }
518  $this->validationRules[$this->validationRulesCounter] = $name;
519  $this->validationRules[$this->validationRulesCounter . '.'] = $ruleConfiguration;
520  $this->validationRulesCounter++;
521  }
522  }
523 
533  protected function setVarious(array $element, array $various, array &$parent, $elementCounter)
534  {
535  foreach ($various as $key => $value) {
536  switch ($key) {
537  case 'headingSize':
538 
539  case 'content':
540 
541  case 'text':
542 
543  case 'name':
544  $parent[$elementCounter . '.'][$key] = (string)$value;
545  break;
546  }
547  }
548  }
549 
560  protected function typoscriptArrayToString(array $typoscriptArray, $addKey = '', $tabCount = -1)
561  {
562  $typoscript = '';
563  if ($addKey != '') {
564  $typoscript .= str_repeat(TAB, $tabCount) . str_replace('.', '', $addKey) . ' {' . LF;
565  }
566  $tabCount++;
567  foreach ($typoscriptArray as $key => $value) {
568  if (!is_array($value)) {
569  if (strstr($value, LF)) {
570  $typoscript .= str_repeat(TAB, $tabCount) . $key . ' (' . LF;
571  if ($key !== 'text') {
572  $value = str_replace(LF, LF . str_repeat(TAB, ($tabCount + 1)), $value);
573  $typoscript .= str_repeat(TAB, ($tabCount + 1)) . $value . LF;
574  } else {
575  $typoscript .= $value . LF;
576  }
577  $typoscript .= str_repeat(TAB, $tabCount) . ')' . LF;
578  } else {
579  $typoscript .= str_repeat(TAB, $tabCount) . $key . ' = ' . $value . LF;
580  }
581  } else {
582  $typoscript .= $this->typoscriptArrayToString($value, $key, $tabCount);
583  }
584  }
585  if ($addKey != '') {
586  $tabCount--;
587  $typoscript .= str_repeat(TAB, $tabCount) . '}' . LF;
588  }
589  return $typoscript;
590  }
591 }
setVarious(array $element, array $various, array &$parent, $elementCounter)
setPrefix($prefix, array &$parent, $elementCounter)
setConfirmation($confirmation, array &$parent, $elementCounter)
setLayout(array $element, $value, array &$parent, $elementCounter)
setLabel(array $label, array &$parent, $elementCounter)
setLegend(array $legend, array &$parent, $elementCounter)
convertToTyposcriptArray(array $elements, array &$parent, $childrenWithParentName=false)
setValidationRules(array $element, array $validationRules)
getContainer(array $element, array &$parent, $elementCounter, $childrenWithParentName=false)
setConfiguration(array $element, array &$parent, $elementCounter, $childrenWithParentName=false)
setAttributes(array $attributes, array &$parent, $elementCounter, $childrenWithParentName=false)
setPostProcessor(array $postProcessors, array &$parent, $elementCounter)
getForm(array $element, array &$parent, $elementCounter)
typoscriptArrayToString(array $typoscriptArray, $addKey='', $tabCount=-1)
setOptions(array $element, array $options, array &$parent, $elementCounter)
getDefaultElementSetup(array $element, array &$parent, $elementCounter, $childrenWithParentName=false)
setFilters(array $filters, array &$parent, $elementCounter)