From 64744a10c5578602141ae2977274eec3fcff1f44 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 8 May 2015 20:37:06 +0200 Subject: more elements and work on the legacy support --- inc/Form/FieldsetCloseElement.php | 64 ++++++++++++++++++++++++++++++++++++++ inc/Form/FieldsetOpenElement.php | 36 ++++++++++++++++++++++ inc/Form/Form.php | 65 ++++++++++++++++++++++++++++++++++++++- inc/Form/HTMLElement.php | 23 ++------------ inc/Form/Label.php | 25 ++------------- inc/Form/LegacyForm.php | 27 +++++++++++++++- inc/Form/TagCloseElement.php | 30 ++++++++++++++++++ inc/Form/TagElement.php | 29 +++++++++++++++++ inc/Form/TagOpenElement.php | 30 ++++++++++++++++++ inc/Form/ValueElement.php | 45 +++++++++++++++++++++++++++ 10 files changed, 329 insertions(+), 45 deletions(-) create mode 100644 inc/Form/FieldsetCloseElement.php create mode 100644 inc/Form/FieldsetOpenElement.php create mode 100644 inc/Form/TagCloseElement.php create mode 100644 inc/Form/TagElement.php create mode 100644 inc/Form/TagOpenElement.php create mode 100644 inc/Form/ValueElement.php diff --git a/inc/Form/FieldsetCloseElement.php b/inc/Form/FieldsetCloseElement.php new file mode 100644 index 000000000..0de84e251 --- /dev/null +++ b/inc/Form/FieldsetCloseElement.php @@ -0,0 +1,64 @@ +type = 'fieldsetopen'; + } + + /** + * The HTML representation of this element + * + * @return string + */ + public function toHTML() { + $html = '
attrs()).'>'; + $legend = $this->val(); + if($legend) $html .= DOKU_LF.''.hsc($legend).''; + return $html; + } +} diff --git a/inc/Form/Form.php b/inc/Form/Form.php index 19cc05065..420399fb1 100644 --- a/inc/Form/Form.php +++ b/inc/Form/Form.php @@ -67,6 +67,8 @@ class Form extends Element { return $this; } + #region Element adding functions + /** * Adds an element to the end of the form * @@ -149,12 +151,73 @@ class Form extends Element { * * @param $html * @param int $pos - * @return Element + * @return HTMLElement */ public function addHTML($html, $pos = -1) { return $this->addElement(new HTMLElement($html), $pos); } + /** + * Add a closed HTML tag to the form + * + * @param $tag + * @param int $pos + * @return TagElement + */ + public function addTag($tag, $pos = -1) { + return $this->addElement(new TagElement($tag), $pos); + } + + /** + * Add an open HTML tag to the form + * + * Be sure to close it again! + * + * @param $tag + * @param int $pos + * @return TagOpenElement + */ + public function addTagOpen($tag, $pos = -1) { + return $this->addElement(new TagOpenElement($tag), $pos); + } + + /** + * Add a closing HTML tag to the form + * + * Be sure it had been opened before + * + * @param $tag + * @param int $pos + * @return TagCloseElement + */ + public function addTagClose($tag, $pos = -1) { + return $this->addElement(new TagCloseElement($tag), $pos); + } + + + /** + * Open a Fieldset + * + * @param $legend + * @param int $pos + * @return FieldsetOpenElement + */ + public function addFieldsetOpen($legend='', $pos = -1) { + return $this->addElement(new FieldsetOpenElement($legend), $pos); + } + + /** + * Close a fieldset + * + * @param int $pos + * @return TagCloseElement + */ + public function addFieldsetClose($pos = -1) { + return $this->addElement(new FieldsetCloseElement(), $pos); + } + + #endregion + protected function balanceFieldsets() { //todo implement! } diff --git a/inc/Form/HTMLElement.php b/inc/Form/HTMLElement.php index 06f27d736..591cf472f 100644 --- a/inc/Form/HTMLElement.php +++ b/inc/Form/HTMLElement.php @@ -8,33 +8,14 @@ namespace dokuwiki\Form; * * @package dokuwiki\Form */ -class HTMLElement extends Element { +class HTMLElement extends ValueElement { - /** - * @var string the raw HTML held by this element - */ - protected $html = ''; /** * @param string $html */ public function __construct($html) { - parent::__construct('html'); - $this->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; + parent::__construct('html', $html); } /** diff --git a/inc/Form/Label.php b/inc/Form/Label.php index c8a862613..8dcd7cd5f 100644 --- a/inc/Form/Label.php +++ b/inc/Form/Label.php @@ -5,11 +5,7 @@ namespace dokuwiki\Form; * Class Label * @package dokuwiki\Form */ -class Label extends Element { - /** - * @var string the actual label text - */ - public $label = ''; +class Label extends ValueElement { /** * Creates a new Label @@ -17,22 +13,7 @@ class Label extends Element { * @param string $label */ public function __construct($label) { - parent::__construct('label'); - $this->label = $label; - } - - /** - * Get or set the element's label text - * - * @param null|string $value - * @return string|$this - */ - public function val($value = null) { - if($value !== null) { - $this->label = $value; - return $this; - } - return $this->label; + parent::__construct('label', $label); } /** @@ -41,6 +22,6 @@ class Label extends Element { * @return string */ public function toHTML() { - return ''; + return ''; } } diff --git a/inc/Form/LegacyForm.php b/inc/Form/LegacyForm.php index edd263ee7..1b47ba204 100644 --- a/inc/Form/LegacyForm.php +++ b/inc/Form/LegacyForm.php @@ -59,12 +59,33 @@ class LegacyForm extends Form { ->id($ctl['id']) ->addClass($ctl['class']); break; - case 'tag': + $this->addTag($ctl['tag']) + ->attrs($attr) + ->attr('name', $ctl['name']) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; case 'opentag': + $this->addTagOpen($ctl['tag']) + ->attrs($attr) + ->attr('name', $ctl['name']) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; case 'closetag': + $this->addTagClose($ctl['tag']); + break; case 'openfieldset': + $this->addFieldsetOpen($ctl['legend']) + ->attrs($attr) + ->attr('name', $ctl['name']) + ->id($ctl['id']) + ->addClass($ctl['class']); + break; case 'closefieldset': + $this->addFieldsetClose(); + break; case 'button': case 'field': case 'fieldright': @@ -121,6 +142,10 @@ class LegacyForm extends Form { 'password' => 'passwordfield', 'checkbox' => 'checkboxfield', 'radio' => 'radiofield', + 'tagopen' => 'opentag', + 'tagclose' => 'closetag', + 'fieldsetopen' => 'openfieldset', + 'fieldsetclose' => 'closefieldset', ); if(isset($types[$type])) return $types[$type]; return $type; diff --git a/inc/Form/TagCloseElement.php b/inc/Form/TagCloseElement.php new file mode 100644 index 000000000..896945b97 --- /dev/null +++ b/inc/Form/TagCloseElement.php @@ -0,0 +1,30 @@ +val().'>'; + } +} diff --git a/inc/Form/TagElement.php b/inc/Form/TagElement.php new file mode 100644 index 000000000..ea5144c9c --- /dev/null +++ b/inc/Form/TagElement.php @@ -0,0 +1,29 @@ +val().' '.buildAttributes($this->attrs()).' />'; + } +} diff --git a/inc/Form/TagOpenElement.php b/inc/Form/TagOpenElement.php new file mode 100644 index 000000000..0afe97b45 --- /dev/null +++ b/inc/Form/TagOpenElement.php @@ -0,0 +1,30 @@ +val().' '.buildAttributes($this->attrs()).'>'; + } +} diff --git a/inc/Form/ValueElement.php b/inc/Form/ValueElement.php new file mode 100644 index 000000000..753704c70 --- /dev/null +++ b/inc/Form/ValueElement.php @@ -0,0 +1,45 @@ +val($value); + } + + /** + * Get or set the element's value + * + * @param null|string $value + * @return string|$this + */ + public function val($value = null) { + if($value !== null) { + $this->value = $value; + return $this; + } + return $this->value; + } + +} -- cgit v1.2.3