TYPO3 CMS  TYPO3_6-2
JsonToTypoScript.php
Go to the documentation of this file.
1 <?php
3 
25 
31  protected $elementId = 0;
32 
39  protected $validationRules = array();
40 
46  protected $validationRulesCounter = 1;
47 
57  public function convert($json) {
58  $elements = json_decode((string) $json, TRUE);
59  $typoscriptArray = array();
60  $typoscript = NULL;
61  $this->convertToTyposcriptArray($elements, $typoscriptArray);
62  if ($typoscriptArray['10.'] && is_array($typoscriptArray['10.']) && !empty($typoscriptArray['10.'])) {
63  $typoscript = $this->typoscriptArrayToString($typoscriptArray['10.']);
64  }
65  return $typoscript;
66  }
67 
77  protected function convertToTyposcriptArray(array $elements, array &$parent, $childrenWithParentName = FALSE) {
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  if ($element['elementContainer'] && $element['elementContainer']['items']) {
150  $this->convertToTyposcriptArray($element['elementContainer']['items'], $parent[$elementCounter . '.'], $childrenWithParentName);
151  }
152  }
153 
166  protected function getForm(array $element, array &$parent, $elementCounter) {
167  // TODO: Put at the top of the form
168  if (!empty($this->validationRules)) {
169  $parent[$elementCounter . '.']['rules'] = $this->validationRules;
170  }
171  }
172 
185  protected function getDefaultElementSetup(array $element, array &$parent, $elementCounter, $childrenWithParentName = FALSE) {
186  $contentObjectType = $this->getContentObjectType($element);
187  if (is_null($contentObjectType) === FALSE) {
188  $parent[$elementCounter] = $contentObjectType;
189  $parent[$elementCounter . '.'] = array();
190  if ($element['configuration']) {
191  $this->setConfiguration($element, $parent, $elementCounter, $childrenWithParentName);
192  }
193  }
194  }
195 
202  protected function getContentObjectType(array $element) {
203  $contentObjectType = NULL;
204  $shortXType = str_replace('typo3-form-wizard-elements-', '', $element['xtype']);
205  list($category, $type) = explode('-', $shortXType);
206  switch ($category) {
207  case 'basic':
208  $contentObjectType = strtoupper($type);
209  break;
210  case 'predefined':
211  switch ($type) {
212  case 'checkboxgroup':
213 
214  case 'radiogroup':
215  $contentObjectType = strtoupper($type);
216  break;
217  case 'email':
218  $contentObjectType = 'TEXTLINE';
219  break;
220  case 'name':
221  $contentObjectType = 'FIELDSET';
222  }
223  break;
224  case 'content':
225  switch ($type) {
226  case 'header':
227 
228  case 'textblock':
229  $contentObjectType = strtoupper($type);
230  }
231  default:
232 
233  }
234  return $contentObjectType;
235  }
236 
247  protected function setConfiguration(array $element, array &$parent, $elementCounter, $childrenWithParentName = FALSE) {
248  foreach ($element['configuration'] as $key => $value) {
249  switch ($key) {
250  case 'attributes':
251  $this->setAttributes($value, $parent, $elementCounter, $childrenWithParentName);
252  break;
253  case 'confirmation':
254  $this->setConfirmation($value, $parent, $elementCounter);
255  break;
256  case 'filters':
257  $this->setFilters($value, $parent, $elementCounter);
258  break;
259  case 'label':
260  $this->setLabel($value, $parent, $elementCounter);
261  break;
262  case 'layout':
263  $this->setLayout($element, $value, $parent, $elementCounter);
264  break;
265  case 'legend':
266  $this->setLegend($value, $parent, $elementCounter);
267  break;
268  case 'options':
269  $this->setOptions($element, $value, $parent, $elementCounter);
270  break;
271  case 'postProcessor':
272  $this->setPostProcessor($value, $parent, $elementCounter);
273  break;
274  case 'prefix':
275  $this->setPrefix($value, $parent, $elementCounter);
276  break;
277  case 'validation':
278  $this->setValidationRules($element, $value);
279  break;
280  case 'various':
281  $this->setVarious($element, $value, $parent, $elementCounter);
282  break;
283  default:
284 
285  }
286  }
287  }
288 
298  protected function setAttributes(array $attributes, array &$parent, $elementCounter, $childrenWithParentName = FALSE) {
299  foreach ($attributes as $key => $value) {
300  if ($key === 'name' && $value === '' && !$childrenWithParentName) {
301  $value = $this->elementId;
302  }
303  if ($value != '' && $key != 'type') {
304  $parent[$elementCounter . '.'][$key] = $value;
305  }
306  }
307  }
308 
319  protected function setConfirmation($confirmation, array &$parent, $elementCounter) {
320  $parent[$elementCounter . '.']['confirmation'] = $confirmation;
321  }
322 
331  protected function setFilters(array $filters, array &$parent, $elementCounter) {
332  if (!empty($filters)) {
333  $parent[$elementCounter . '.']['filters'] = array();
334  $filterCounter = 1;
335  foreach ($filters as $name => $filterConfiguration) {
336  $parent[$elementCounter . '.']['filters'][$filterCounter] = $name;
337  $parent[$elementCounter . '.']['filters'][$filterCounter . '.'] = $filterConfiguration;
338  $filterCounter++;
339  }
340  }
341  }
342 
351  protected function setLabel(array $label, array &$parent, $elementCounter) {
352  if ($label['value'] != '') {
353  $parent[$elementCounter . '.']['label.']['value'] = $label['value'];
354  }
355  }
356 
370  protected function setLayout(array $element, $value, array &$parent, $elementCounter) {
371  switch ($element['xtype']) {
372  case 'typo3-form-wizard-elements-basic-button':
373 
374  case 'typo3-form-wizard-elements-basic-fileupload':
375 
376  case 'typo3-form-wizard-elements-basic-password':
377 
378  case 'typo3-form-wizard-elements-basic-reset':
379 
380  case 'typo3-form-wizard-elements-basic-submit':
381 
382  case 'typo3-form-wizard-elements-basic-textline':
383  if ($value === 'back') {
384  $parent[$elementCounter . '.']['layout'] = '<input />' . chr(10) . '<label />';
385  }
386  break;
387  case 'typo3-form-wizard-elements-basic-checkbox':
388 
389  case 'typo3-form-wizard-elements-basic-radio':
390  if ($value === 'front') {
391  $parent[$elementCounter . '.']['layout'] = '<label />' . chr(10) . '<input />';
392  }
393  break;
394  case 'typo3-form-wizard-elements-basic-select':
395  if ($value === 'back') {
396  $parent[$elementCounter . '.']['layout'] = '<select>' . chr(10) . '<elements />' . chr(10) . '</select>' . chr(10) . '<label />';
397  }
398  break;
399  case 'typo3-form-wizard-elements-basic-textarea':
400  if ($value === 'back') {
401  $parent[$elementCounter . '.']['layout'] = '<textarea />' . chr(10) . '<label />';
402  }
403  break;
404  default:
405 
406  }
407  }
408 
417  protected function setLegend(array $legend, array &$parent, $elementCounter) {
418  if ($legend['value'] != '') {
419  $parent[$elementCounter . '.']['legend.']['value'] = $legend['value'];
420  }
421  }
422 
436  protected function setOptions(array $element, array $options, array &$parent, $elementCounter) {
437  if (is_array($options) && $element['xtype'] === 'typo3-form-wizard-elements-basic-select') {
438  $optionCounter = 10;
439  foreach ($options as $option) {
440  $parent[$elementCounter . '.'][$optionCounter] = 'OPTION';
441  $parent[$elementCounter . '.'][$optionCounter . '.']['data'] = $option['data'];
442  if (isset($option['attributes'])) {
443  $parent[$elementCounter . '.'][$optionCounter . '.'] += $option['attributes'];
444  }
445  $optionCounter = $optionCounter + 10;
446  }
447  }
448  }
449 
458  protected function setPostProcessor(array $postProcessors, array &$parent, $elementCounter) {
459  if (!empty($postProcessors)) {
460  $parent[$elementCounter . '.']['postProcessor'] = array();
461  $postProcessorCounter = 1;
462  foreach ($postProcessors as $name => $postProcessorConfiguration) {
463  $parent[$elementCounter . '.']['postProcessor'][$postProcessorCounter] = $name;
464  $parent[$elementCounter . '.']['postProcessor'][$postProcessorCounter . '.'] = $postProcessorConfiguration;
465  $postProcessorCounter++;
466  }
467  }
468  }
469 
480  protected function setPrefix($prefix, array &$parent, $elementCounter) {
481  $parent[$elementCounter . '.']['prefix'] = $prefix;
482  }
483 
494  protected function setValidationRules(array $element, array $validationRules) {
495  foreach ($validationRules as $name => $ruleConfiguration) {
496  if (isset($element['configuration']['attributes']['name']) && $element['configuration']['attributes']['name'] != '') {
497  $ruleConfiguration['element'] = $element['configuration']['attributes']['name'];
498  } elseif (isset($element['configuration']['various']['name']) && $element['configuration']['various']['name'] != '') {
499  $ruleConfiguration['element'] = $element['configuration']['various']['name'];
500  } else {
501  $ruleConfiguration['element'] = $this->elementId;
502  }
503  $this->validationRules[$this->validationRulesCounter] = $name;
504  $this->validationRules[$this->validationRulesCounter . '.'] = $ruleConfiguration;
505  $this->validationRulesCounter++;
506  }
507  }
508 
518  protected function setVarious(array $element, array $various, array &$parent, $elementCounter) {
519  foreach ($various as $key => $value) {
520  switch ($key) {
521  case 'headingSize':
522 
523  case 'content':
524 
525  case 'name':
526  $parent[$elementCounter . '.'][$key] = (string) $value;
527  break;
528  }
529  }
530  }
531 
542  protected function typoscriptArrayToString(array $typoscriptArray, $addKey = '', $tabCount = -1) {
543  $typoscript = '';
544  if ($addKey != '') {
545  $typoscript .= str_repeat(chr(9), $tabCount) . str_replace('.', '', $addKey) . ' {' . chr(10);
546  }
547  $tabCount++;
548  foreach ($typoscriptArray as $key => $value) {
549  if (!is_array($value)) {
550  if (strstr($value, chr(10))) {
551  $typoscript .= str_repeat(chr(9), $tabCount) . $key . ' (' . chr(10);
552  $value = str_replace(chr(10), chr(10) . str_repeat(chr(9), ($tabCount + 1)), $value);
553  $typoscript .= str_repeat(chr(9), ($tabCount + 1)) . $value . chr(10);
554  $typoscript .= str_repeat(chr(9), $tabCount) . ')' . chr(10);
555  } else {
556  $typoscript .= str_repeat(chr(9), $tabCount) . $key . ' = ' . $value . chr(10);
557  }
558  } else {
559  $typoscript .= $this->typoscriptArrayToString($value, $key, $tabCount);
560  }
561  }
562  if ($addKey != '') {
563  $tabCount--;
564  $typoscript .= str_repeat(chr(9), $tabCount) . '}' . chr(10);
565  }
566  return $typoscript;
567  }
568 
569 }
getDefaultElementSetup(array $element, array &$parent, $elementCounter, $childrenWithParentName=FALSE)
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)
convertToTyposcriptArray(array $elements, array &$parent, $childrenWithParentName=FALSE)
setLabel(array $label, array &$parent, $elementCounter)
setLegend(array $legend, array &$parent, $elementCounter)
setValidationRules(array $element, array $validationRules)
setConfiguration(array $element, array &$parent, $elementCounter, $childrenWithParentName=FALSE)
setPostProcessor(array $postProcessors, array &$parent, $elementCounter)
getForm(array $element, array &$parent, $elementCounter)
getContainer(array $element, array &$parent, $elementCounter, $childrenWithParentName=FALSE)
typoscriptArrayToString(array $typoscriptArray, $addKey='', $tabCount=-1)
setAttributes(array $attributes, array &$parent, $elementCounter, $childrenWithParentName=FALSE)
setOptions(array $element, array $options, array &$parent, $elementCounter)
setFilters(array $filters, array &$parent, $elementCounter)