1de19515fSAndreas Gohr<?php 29d01c1d9SSatoshi Sahara 3de19515fSAndreas Gohrnamespace dokuwiki\Form; 4de19515fSAndreas Gohr 5de19515fSAndreas Gohr/** 6de19515fSAndreas Gohr * Class LegacyForm 7de19515fSAndreas Gohr * 8de19515fSAndreas Gohr * Provides a compatibility layer to the old Doku_Form API 9de19515fSAndreas Gohr * 10de19515fSAndreas Gohr * This can be used to work with the modern API on forms provided by old events for 11de19515fSAndreas Gohr * example. When you start new forms, just use Form\Form 12de19515fSAndreas Gohr * 13de19515fSAndreas Gohr * @package dokuwiki\Form 14de19515fSAndreas Gohr */ 159d01c1d9SSatoshi Saharaclass LegacyForm extends Form 169d01c1d9SSatoshi Sahara{ 17de19515fSAndreas Gohr /** 18de19515fSAndreas Gohr * Creates a new modern form from an old legacy Doku_Form 19de19515fSAndreas Gohr * 20de19515fSAndreas Gohr * @param \Doku_Form $oldform 21de19515fSAndreas Gohr */ 229d01c1d9SSatoshi Sahara public function __construct(\Doku_Form $oldform) 239d01c1d9SSatoshi Sahara { 24de19515fSAndreas Gohr parent::__construct($oldform->params); 25de19515fSAndreas Gohr 26de19515fSAndreas Gohr $this->hidden = $oldform->_hidden; 27de19515fSAndreas Gohr 28de19515fSAndreas Gohr foreach ($oldform->_content as $element) { 29*6fd0861fSAndreas Gohr [$ctl, $attr] = $this->parseLegacyAttr($element); 30de19515fSAndreas Gohr 31de19515fSAndreas Gohr if (is_array($element)) { 32de19515fSAndreas Gohr switch ($ctl['elem']) { 33de19515fSAndreas Gohr case 'wikitext': 34de19515fSAndreas Gohr $this->addTextarea('wikitext') 35de19515fSAndreas Gohr ->attrs($attr) 36de19515fSAndreas Gohr ->id('wiki__text') 37de19515fSAndreas Gohr ->val($ctl['text']) 38de19515fSAndreas Gohr ->addClass($ctl['class']); 39de19515fSAndreas Gohr break; 40de19515fSAndreas Gohr case 'textfield': 41de19515fSAndreas Gohr $this->addTextInput($ctl['name'], $ctl['text']) 42de19515fSAndreas Gohr ->attrs($attr) 43de19515fSAndreas Gohr ->id($ctl['id']) 44de19515fSAndreas Gohr ->addClass($ctl['class']); 45de19515fSAndreas Gohr break; 46de19515fSAndreas Gohr case 'passwordfield': 47de19515fSAndreas Gohr $this->addPasswordInput($ctl['name'], $ctl['text']) 48de19515fSAndreas Gohr ->attrs($attr) 49de19515fSAndreas Gohr ->id($ctl['id']) 50de19515fSAndreas Gohr ->addClass($ctl['class']); 51de19515fSAndreas Gohr break; 52de19515fSAndreas Gohr case 'checkboxfield': 53de19515fSAndreas Gohr $this->addCheckbox($ctl['name'], $ctl['text']) 54de19515fSAndreas Gohr ->attrs($attr) 55de19515fSAndreas Gohr ->id($ctl['id']) 56de19515fSAndreas Gohr ->addClass($ctl['class']); 57de19515fSAndreas Gohr break; 58de19515fSAndreas Gohr case 'radiofield': 59de19515fSAndreas Gohr $this->addRadioButton($ctl['name'], $ctl['text']) 60de19515fSAndreas Gohr ->attrs($attr) 61de19515fSAndreas Gohr ->id($ctl['id']) 62de19515fSAndreas Gohr ->addClass($ctl['class']); 63de19515fSAndreas Gohr break; 64de19515fSAndreas Gohr case 'tag': 6564744a10SAndreas Gohr $this->addTag($ctl['tag']) 6664744a10SAndreas Gohr ->attrs($attr) 6764744a10SAndreas Gohr ->attr('name', $ctl['name']) 6864744a10SAndreas Gohr ->id($ctl['id']) 6964744a10SAndreas Gohr ->addClass($ctl['class']); 7064744a10SAndreas Gohr break; 71de19515fSAndreas Gohr case 'opentag': 7264744a10SAndreas Gohr $this->addTagOpen($ctl['tag']) 7364744a10SAndreas Gohr ->attrs($attr) 7464744a10SAndreas Gohr ->attr('name', $ctl['name']) 7564744a10SAndreas Gohr ->id($ctl['id']) 7664744a10SAndreas Gohr ->addClass($ctl['class']); 7764744a10SAndreas Gohr break; 78de19515fSAndreas Gohr case 'closetag': 7964744a10SAndreas Gohr $this->addTagClose($ctl['tag']); 8064744a10SAndreas Gohr break; 81de19515fSAndreas Gohr case 'openfieldset': 8264744a10SAndreas Gohr $this->addFieldsetOpen($ctl['legend']) 8364744a10SAndreas Gohr ->attrs($attr) 8464744a10SAndreas Gohr ->attr('name', $ctl['name']) 8564744a10SAndreas Gohr ->id($ctl['id']) 8664744a10SAndreas Gohr ->addClass($ctl['class']); 8764744a10SAndreas Gohr break; 88de19515fSAndreas Gohr case 'closefieldset': 8964744a10SAndreas Gohr $this->addFieldsetClose(); 9064744a10SAndreas Gohr break; 91de19515fSAndreas Gohr case 'button': 92de19515fSAndreas Gohr case 'field': 93de19515fSAndreas Gohr case 'fieldright': 94de19515fSAndreas Gohr case 'filefield': 95de19515fSAndreas Gohr case 'menufield': 96de19515fSAndreas Gohr case 'listboxfield': 97de19515fSAndreas Gohr throw new \UnexpectedValueException('Unsupported legacy field ' . $ctl['elem']); 98de19515fSAndreas Gohr default: 99de19515fSAndreas Gohr throw new \UnexpectedValueException('Unknown legacy field ' . $ctl['elem']); 100de19515fSAndreas Gohr } 101de19515fSAndreas Gohr } else { 102de19515fSAndreas Gohr $this->addHTML($element); 103de19515fSAndreas Gohr } 104de19515fSAndreas Gohr } 105de19515fSAndreas Gohr } 106de19515fSAndreas Gohr 107de19515fSAndreas Gohr /** 108de19515fSAndreas Gohr * Parses out what is the elements attributes and what is control info 109de19515fSAndreas Gohr * 110de19515fSAndreas Gohr * @param array $legacy 111de19515fSAndreas Gohr * @return array 112de19515fSAndreas Gohr */ 1139d01c1d9SSatoshi Sahara protected function parseLegacyAttr($legacy) 1149d01c1d9SSatoshi Sahara { 115*6fd0861fSAndreas Gohr $attributes = []; 116*6fd0861fSAndreas Gohr $control = []; 117de19515fSAndreas Gohr 118de19515fSAndreas Gohr foreach ($legacy as $key => $val) { 1192401f18dSSyntaxseed if ($key[0] == '_') { 120de19515fSAndreas Gohr $control[substr($key, 1)] = $val; 121de19515fSAndreas Gohr } elseif ($key == 'name') { 122de19515fSAndreas Gohr $control[$key] = $val; 123de19515fSAndreas Gohr } elseif ($key == 'id') { 124de19515fSAndreas Gohr $control[$key] = $val; 125de19515fSAndreas Gohr } else { 126de19515fSAndreas Gohr $attributes[$key] = $val; 127de19515fSAndreas Gohr } 128de19515fSAndreas Gohr } 129de19515fSAndreas Gohr 130*6fd0861fSAndreas Gohr return [$control, $attributes]; 131de19515fSAndreas Gohr } 132de19515fSAndreas Gohr 133de19515fSAndreas Gohr /** 134de19515fSAndreas Gohr * Translates our types to the legacy types 135de19515fSAndreas Gohr * 136de19515fSAndreas Gohr * @param string $type 137de19515fSAndreas Gohr * @return string 138de19515fSAndreas Gohr */ 1399d01c1d9SSatoshi Sahara protected function legacyType($type) 1409d01c1d9SSatoshi Sahara { 141*6fd0861fSAndreas Gohr static $types = [ 142de19515fSAndreas Gohr 'text' => 'textfield', 143de19515fSAndreas Gohr 'password' => 'passwordfield', 144de19515fSAndreas Gohr 'checkbox' => 'checkboxfield', 145de19515fSAndreas Gohr 'radio' => 'radiofield', 14664744a10SAndreas Gohr 'tagopen' => 'opentag', 14764744a10SAndreas Gohr 'tagclose' => 'closetag', 14864744a10SAndreas Gohr 'fieldsetopen' => 'openfieldset', 149*6fd0861fSAndreas Gohr 'fieldsetclose' => 'closefieldset' 150*6fd0861fSAndreas Gohr ]; 151*6fd0861fSAndreas Gohr return $types[$type] ?? $type; 152de19515fSAndreas Gohr } 153de19515fSAndreas Gohr 154de19515fSAndreas Gohr /** 155de19515fSAndreas Gohr * Creates an old legacy form from this modern form's data 156de19515fSAndreas Gohr * 157de19515fSAndreas Gohr * @return \Doku_Form 158de19515fSAndreas Gohr */ 1599d01c1d9SSatoshi Sahara public function toLegacy() 1609d01c1d9SSatoshi Sahara { 161de19515fSAndreas Gohr $this->balanceFieldsets(); 162de19515fSAndreas Gohr 163de19515fSAndreas Gohr $legacy = new \Doku_Form($this->attrs()); 164de19515fSAndreas Gohr $legacy->_hidden = $this->hidden; 165de19515fSAndreas Gohr foreach ($this->elements as $element) { 166de19515fSAndreas Gohr if (is_a($element, 'dokuwiki\Form\HTMLElement')) { 167de19515fSAndreas Gohr $legacy->_content[] = $element->toHTML(); 168de19515fSAndreas Gohr } elseif (is_a($element, 'dokuwiki\Form\InputElement')) { 169de19515fSAndreas Gohr /** @var InputElement $element */ 170de19515fSAndreas Gohr $data = $element->attrs(); 171de19515fSAndreas Gohr $data['_elem'] = $this->legacyType($element->getType()); 172de19515fSAndreas Gohr $label = $element->getLabel(); 173*6fd0861fSAndreas Gohr if ($label instanceof LabelElement) { 174de19515fSAndreas Gohr $data['_class'] = $label->attr('class'); 175de19515fSAndreas Gohr } 176de19515fSAndreas Gohr $legacy->_content[] = $data; 177de19515fSAndreas Gohr } 178de19515fSAndreas Gohr } 179de19515fSAndreas Gohr 180de19515fSAndreas Gohr return $legacy; 181de19515fSAndreas Gohr } 182de19515fSAndreas Gohr} 183