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