From de19515f04567db78bd41d5bff68a88bfb8c2a22 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 19:12:21 +0200 Subject: started with the compatibility layer --- inc/Form/Element.php | 10 ++- inc/Form/Form.php | 27 ++++++-- inc/Form/HTMLElement.php | 48 +++++++++++++ inc/Form/InputElement.php | 34 +++++++--- inc/Form/LegacyForm.php | 156 +++++++++++++++++++++++++++++++++++++++++++ inc/Form/TextareaElement.php | 1 + 6 files changed, 260 insertions(+), 16 deletions(-) create mode 100644 inc/Form/HTMLElement.php create mode 100644 inc/Form/LegacyForm.php diff --git a/inc/Form/Element.php b/inc/Form/Element.php index 3fd170a80..7938ee009 100644 --- a/inc/Form/Element.php +++ b/inc/Form/Element.php @@ -1,7 +1,6 @@ attributes = $attributes; } + /** + * Type of this element + * + * @return string + */ + public function getType() { + return $this->type; + } + /** * Gets or sets an attribute * diff --git a/inc/Form/Form.php b/inc/Form/Form.php index dc502e021..19cc05065 100644 --- a/inc/Form/Form.php +++ b/inc/Form/Form.php @@ -92,7 +92,7 @@ class Form extends Element { * @param int $pos * @return InputElement */ - public function addTextInput($name, $label, $pos = -1) { + public function addTextInput($name, $label = '', $pos = -1) { return $this->addElement(new InputElement('text', $name, $label), $pos); } @@ -104,7 +104,7 @@ class Form extends Element { * @param int $pos * @return InputElement */ - public function addPasswordInput($name, $label, $pos = -1) { + public function addPasswordInput($name, $label = '', $pos = -1) { return $this->addElement(new InputElement('password', $name, $label), $pos); } @@ -116,7 +116,7 @@ class Form extends Element { * @param int $pos * @return CheckableElement */ - public function addRadioButton($name, $label, $pos = -1) { + public function addRadioButton($name, $label = '', $pos = -1) { return $this->addElement(new CheckableElement('radio', $name, $label), $pos); } @@ -128,7 +128,7 @@ class Form extends Element { * @param int $pos * @return CheckableElement */ - public function addCheckbox($name, $label, $pos = -1) { + public function addCheckbox($name, $label = '', $pos = -1) { return $this->addElement(new CheckableElement('checkbox', $name, $label), $pos); } @@ -140,16 +140,33 @@ class Form extends Element { * @param int $pos * @return TextareaElement */ - public function addTextarea($name, $label, $pos = -1) { + public function addTextarea($name, $label = '', $pos = -1) { return $this->addElement(new TextareaElement($name, $label), $pos); } + /** + * Add fixed HTML to the form + * + * @param $html + * @param int $pos + * @return Element + */ + public function addHTML($html, $pos = -1) { + return $this->addElement(new HTMLElement($html), $pos); + } + + protected function balanceFieldsets() { + //todo implement! + } + /** * The HTML representation of the whole form * * @return string */ public function toHTML() { + $this->balanceFieldsets(); + $html = '
attrs()) . '>' . DOKU_LF; foreach($this->hidden as $name => $value) { diff --git a/inc/Form/HTMLElement.php b/inc/Form/HTMLElement.php new file mode 100644 index 000000000..06f27d736 --- /dev/null +++ b/inc/Form/HTMLElement.php @@ -0,0 +1,48 @@ +val($html); + } + + /** + * Get or set the element's content + * + * @param null|string $html + * @return string|$this + */ + public function val($html = null) { + if($html !== null) { + $this->html = $html; + return $this; + } + return $this->html; + } + + /** + * The HTML representation of this element + * + * @return string + */ + public function toHTML() { + return $this->val(); + } +} diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 4f644c0f1..939d911c1 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -4,7 +4,8 @@ namespace dokuwiki\Form; /** * Class InputElement * - * Base class for all input elements. Uses a wrapping label. + * Base class for all input elements. Uses a wrapping label when label + * text is given. * * @todo figure out how to make wrapping or related label configurable * @package dokuwiki\Form @@ -13,7 +14,7 @@ class InputElement extends Element { /** * @var Label */ - protected $label; + protected $label = null; /** * @var bool if the element should reflect posted values @@ -25,10 +26,19 @@ class InputElement extends Element { * @param string $name The name of this form element * @param string $label The label text for this element */ - public function __construct($type, $name, $label) { + public function __construct($type, $name, $label = '') { parent::__construct($type, array('name' => $name)); $this->attr('name', $name); - $this->label = new Label($label); + if($label) $this->label = new Label($label); + } + + /** + * Returns the label element if there's one set + * + * @return Label|null + */ + public function getLabel() { + return $this->label; } /** @@ -52,7 +62,7 @@ class InputElement extends Element { * @return string|$this */ public function id($id = null) { - $this->label->attr('for', $id); + if($this->label) $this->label->attr('for', $id); return parent::id($id); } @@ -65,7 +75,7 @@ class InputElement extends Element { * @return $this */ public function addClass($class) { - $this->label->addClass($class); + if($this->label) $this->label->addClass($class); return parent::addClass($class); } @@ -138,9 +148,13 @@ class InputElement extends Element { * @return string */ public function toHTML() { - return ''; + if($this->label) { + return ''; + } else { + return $this->mainElementHTML(); + } } } diff --git a/inc/Form/LegacyForm.php b/inc/Form/LegacyForm.php new file mode 100644 index 000000000..edd263ee7 --- /dev/null +++ b/inc/Form/LegacyForm.php @@ -0,0 +1,156 @@ +params); + + $this->hidden = $oldform->_hidden; + + foreach($oldform->_content as $element) { + list($ctl, $attr) = $this->parseLegacyAttr($element); + + if(is_array($element)) { + switch($ctl['elem']) { + case 'wikitext': + $this->addTextarea('wikitext') + ->attrs($attr) + ->id('wiki__text') + ->val($ctl['text']) + ->addClass($ctl['class']); + break; + case 'textfield': + $this->addTextInput($ctl['name'], $ctl['text']) + ->attrs($attr) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; + case 'passwordfield': + $this->addPasswordInput($ctl['name'], $ctl['text']) + ->attrs($attr) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; + case 'checkboxfield': + $this->addCheckbox($ctl['name'], $ctl['text']) + ->attrs($attr) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; + case 'radiofield': + $this->addRadioButton($ctl['name'], $ctl['text']) + ->attrs($attr) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; + + case 'tag': + case 'opentag': + case 'closetag': + case 'openfieldset': + case 'closefieldset': + case 'button': + case 'field': + case 'fieldright': + case 'filefield': + case 'menufield': + case 'listboxfield': + throw new \UnexpectedValueException('Unsupported legacy field ' . $ctl['elem']); + break; + default: + throw new \UnexpectedValueException('Unknown legacy field ' . $ctl['elem']); + + } + } else { + $this->addHTML($element); + } + } + + } + + /** + * Parses out what is the elements attributes and what is control info + * + * @param array $legacy + * @return array + */ + protected function parseLegacyAttr($legacy) { + $attributes = array(); + $control = array(); + + foreach($legacy as $key => $val) { + if($key{0} == '_') { + $control[substr($key, 1)] = $val; + } elseif($key == 'name') { + $control[$key] = $val; + } elseif($key == 'id') { + $control[$key] = $val; + } else { + $attributes[$key] = $val; + } + } + + return array($control, $attributes); + } + + /** + * Translates our types to the legacy types + * + * @param string $type + * @return string + */ + protected function legacyType($type) { + static $types = array( + 'text' => 'textfield', + 'password' => 'passwordfield', + 'checkbox' => 'checkboxfield', + 'radio' => 'radiofield', + ); + if(isset($types[$type])) return $types[$type]; + return $type; + } + + /** + * Creates an old legacy form from this modern form's data + * + * @return \Doku_Form + */ + public function toLegacy() { + $this->balanceFieldsets(); + + $legacy = new \Doku_Form($this->attrs()); + $legacy->_hidden = $this->hidden; + foreach($this->elements as $element) { + if(is_a($element, 'dokuwiki\Form\HTMLElement')) { + $legacy->_content[] = $element->toHTML(); + } elseif(is_a($element, 'dokuwiki\Form\InputElement')) { + /** @var InputElement $element */ + $data = $element->attrs(); + $data['_elem'] = $this->legacyType($element->getType()); + $label = $element->getLabel(); + if($label) { + $data['_class'] = $label->attr('class'); + } + $legacy->_content[] = $data; + } + } + + return $legacy; + } +} diff --git a/inc/Form/TextareaElement.php b/inc/Form/TextareaElement.php index a8486266f..9d461fdf5 100644 --- a/inc/Form/TextareaElement.php +++ b/inc/Form/TextareaElement.php @@ -18,6 +18,7 @@ class TextareaElement extends InputElement { */ public function __construct($name, $label) { parent::__construct('textarea', $name, $label); + $this->attr('dir', 'auto'); } /** -- cgit v1.2.3