You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

183 lines
6.1 KiB

  1. <?php
  2. namespace dokuwiki\Form;
  3. /**
  4. * Class LegacyForm
  5. *
  6. * Provides a compatibility layer to the old Doku_Form API
  7. *
  8. * This can be used to work with the modern API on forms provided by old events for
  9. * example. When you start new forms, just use Form\Form
  10. *
  11. * @package dokuwiki\Form
  12. */
  13. class LegacyForm extends Form
  14. {
  15. /**
  16. * Creates a new modern form from an old legacy Doku_Form
  17. *
  18. * @param \Doku_Form $oldform
  19. */
  20. public function __construct(\Doku_Form $oldform)
  21. {
  22. parent::__construct($oldform->params);
  23. $this->hidden = $oldform->_hidden;
  24. foreach ($oldform->_content as $element) {
  25. [$ctl, $attr] = $this->parseLegacyAttr($element);
  26. if (is_array($element)) {
  27. switch ($ctl['elem']) {
  28. case 'wikitext':
  29. $this->addTextarea('wikitext')
  30. ->attrs($attr)
  31. ->id('wiki__text')
  32. ->val($ctl['text'])
  33. ->addClass($ctl['class']);
  34. break;
  35. case 'textfield':
  36. $this->addTextInput($ctl['name'], $ctl['text'])
  37. ->attrs($attr)
  38. ->id($ctl['id'])
  39. ->addClass($ctl['class']);
  40. break;
  41. case 'passwordfield':
  42. $this->addPasswordInput($ctl['name'], $ctl['text'])
  43. ->attrs($attr)
  44. ->id($ctl['id'])
  45. ->addClass($ctl['class']);
  46. break;
  47. case 'checkboxfield':
  48. $this->addCheckbox($ctl['name'], $ctl['text'])
  49. ->attrs($attr)
  50. ->id($ctl['id'])
  51. ->addClass($ctl['class']);
  52. break;
  53. case 'radiofield':
  54. $this->addRadioButton($ctl['name'], $ctl['text'])
  55. ->attrs($attr)
  56. ->id($ctl['id'])
  57. ->addClass($ctl['class']);
  58. break;
  59. case 'tag':
  60. $this->addTag($ctl['tag'])
  61. ->attrs($attr)
  62. ->attr('name', $ctl['name'])
  63. ->id($ctl['id'])
  64. ->addClass($ctl['class']);
  65. break;
  66. case 'opentag':
  67. $this->addTagOpen($ctl['tag'])
  68. ->attrs($attr)
  69. ->attr('name', $ctl['name'])
  70. ->id($ctl['id'])
  71. ->addClass($ctl['class']);
  72. break;
  73. case 'closetag':
  74. $this->addTagClose($ctl['tag']);
  75. break;
  76. case 'openfieldset':
  77. $this->addFieldsetOpen($ctl['legend'])
  78. ->attrs($attr)
  79. ->attr('name', $ctl['name'])
  80. ->id($ctl['id'])
  81. ->addClass($ctl['class']);
  82. break;
  83. case 'closefieldset':
  84. $this->addFieldsetClose();
  85. break;
  86. case 'button':
  87. case 'field':
  88. case 'fieldright':
  89. case 'filefield':
  90. case 'menufield':
  91. case 'listboxfield':
  92. throw new \UnexpectedValueException('Unsupported legacy field ' . $ctl['elem']);
  93. default:
  94. throw new \UnexpectedValueException('Unknown legacy field ' . $ctl['elem']);
  95. }
  96. } else {
  97. $this->addHTML($element);
  98. }
  99. }
  100. }
  101. /**
  102. * Parses out what is the elements attributes and what is control info
  103. *
  104. * @param array $legacy
  105. * @return array
  106. */
  107. protected function parseLegacyAttr($legacy)
  108. {
  109. $attributes = [];
  110. $control = [];
  111. foreach ($legacy as $key => $val) {
  112. if ($key[0] == '_') {
  113. $control[substr($key, 1)] = $val;
  114. } elseif ($key == 'name') {
  115. $control[$key] = $val;
  116. } elseif ($key == 'id') {
  117. $control[$key] = $val;
  118. } else {
  119. $attributes[$key] = $val;
  120. }
  121. }
  122. return [$control, $attributes];
  123. }
  124. /**
  125. * Translates our types to the legacy types
  126. *
  127. * @param string $type
  128. * @return string
  129. */
  130. protected function legacyType($type)
  131. {
  132. static $types = [
  133. 'text' => 'textfield',
  134. 'password' => 'passwordfield',
  135. 'checkbox' => 'checkboxfield',
  136. 'radio' => 'radiofield',
  137. 'tagopen' => 'opentag',
  138. 'tagclose' => 'closetag',
  139. 'fieldsetopen' => 'openfieldset',
  140. 'fieldsetclose' => 'closefieldset'
  141. ];
  142. return $types[$type] ?? $type;
  143. }
  144. /**
  145. * Creates an old legacy form from this modern form's data
  146. *
  147. * @return \Doku_Form
  148. */
  149. public function toLegacy()
  150. {
  151. $this->balanceFieldsets();
  152. $legacy = new \Doku_Form($this->attrs());
  153. $legacy->_hidden = $this->hidden;
  154. foreach ($this->elements as $element) {
  155. if (is_a($element, 'dokuwiki\Form\HTMLElement')) {
  156. $legacy->_content[] = $element->toHTML();
  157. } elseif (is_a($element, 'dokuwiki\Form\InputElement')) {
  158. /** @var InputElement $element */
  159. $data = $element->attrs();
  160. $data['_elem'] = $this->legacyType($element->getType());
  161. $label = $element->getLabel();
  162. if ($label instanceof LabelElement) {
  163. $data['_class'] = $label->attr('class');
  164. }
  165. $legacy->_content[] = $data;
  166. }
  167. }
  168. return $legacy;
  169. }
  170. }