LegacyForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. list($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. break;
  94. default:
  95. throw new \UnexpectedValueException('Unknown legacy field ' . $ctl['elem']);
  96. }
  97. } else {
  98. $this->addHTML($element);
  99. }
  100. }
  101. }
  102. /**
  103. * Parses out what is the elements attributes and what is control info
  104. *
  105. * @param array $legacy
  106. * @return array
  107. */
  108. protected function parseLegacyAttr($legacy)
  109. {
  110. $attributes = array();
  111. $control = array();
  112. foreach ($legacy as $key => $val) {
  113. if ($key[0] == '_') {
  114. $control[substr($key, 1)] = $val;
  115. } elseif($key == 'name') {
  116. $control[$key] = $val;
  117. } elseif($key == 'id') {
  118. $control[$key] = $val;
  119. } else {
  120. $attributes[$key] = $val;
  121. }
  122. }
  123. return array($control, $attributes);
  124. }
  125. /**
  126. * Translates our types to the legacy types
  127. *
  128. * @param string $type
  129. * @return string
  130. */
  131. protected function legacyType($type)
  132. {
  133. static $types = array(
  134. 'text' => 'textfield',
  135. 'password' => 'passwordfield',
  136. 'checkbox' => 'checkboxfield',
  137. 'radio' => 'radiofield',
  138. 'tagopen' => 'opentag',
  139. 'tagclose' => 'closetag',
  140. 'fieldsetopen' => 'openfieldset',
  141. 'fieldsetclose' => 'closefieldset',
  142. );
  143. if (isset($types[$type])) return $types[$type];
  144. return $type;
  145. }
  146. /**
  147. * Creates an old legacy form from this modern form's data
  148. *
  149. * @return \Doku_Form
  150. */
  151. public function toLegacy()
  152. {
  153. $this->balanceFieldsets();
  154. $legacy = new \Doku_Form($this->attrs());
  155. $legacy->_hidden = $this->hidden;
  156. foreach ($this->elements as $element) {
  157. if (is_a($element, 'dokuwiki\Form\HTMLElement')) {
  158. $legacy->_content[] = $element->toHTML();
  159. } elseif (is_a($element, 'dokuwiki\Form\InputElement')) {
  160. /** @var InputElement $element */
  161. $data = $element->attrs();
  162. $data['_elem'] = $this->legacyType($element->getType());
  163. $label = $element->getLabel();
  164. if ($label) {
  165. $data['_class'] = $label->attr('class');
  166. }
  167. $legacy->_content[] = $data;
  168. }
  169. }
  170. return $legacy;
  171. }
  172. }