summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2015-05-08 20:37:06 +0200
committerAndreas Gohr <andi@splitbrain.org>2015-05-08 20:37:06 +0200
commit64744a10c5578602141ae2977274eec3fcff1f44 (patch)
tree83c0f7bbac3ec45a05ce4da474aebb567ec27845
parentde19515f04567db78bd41d5bff68a88bfb8c2a22 (diff)
downloadrpg-64744a10c5578602141ae2977274eec3fcff1f44.tar.gz
rpg-64744a10c5578602141ae2977274eec3fcff1f44.tar.bz2
more elements and work on the legacy support
-rw-r--r--inc/Form/FieldsetCloseElement.php64
-rw-r--r--inc/Form/FieldsetOpenElement.php36
-rw-r--r--inc/Form/Form.php65
-rw-r--r--inc/Form/HTMLElement.php23
-rw-r--r--inc/Form/Label.php25
-rw-r--r--inc/Form/LegacyForm.php27
-rw-r--r--inc/Form/TagCloseElement.php30
-rw-r--r--inc/Form/TagElement.php29
-rw-r--r--inc/Form/TagOpenElement.php30
-rw-r--r--inc/Form/ValueElement.php45
10 files changed, 329 insertions, 45 deletions
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 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class FieldsetCloseElement
+ *
+ * Closes an open Fieldset
+ *
+ * @package dokuwiki\Form
+ */
+class FieldsetCloseElement extends TagCloseElement {
+
+ /**
+ * @param array $attributes
+ */
+ public function __construct($attributes = array()) {
+ parent::__construct('tagclose', $attributes);
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $class
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function addClass($class) {
+ throw new \BadMethodCallException('You can\t add classes to closing tag');
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $id
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function id($id = null) {
+ throw new \BadMethodCallException('You can\t add ID to closing tag');
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $name
+ * @param $value
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function attr($name, $value = null) {
+ throw new \BadMethodCallException('You can\t add attributes to closing tag');
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $attributes
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function attrs($attributes = null) {
+ throw new \BadMethodCallException('You can\t add attributes to closing tag');
+ }
+}
diff --git a/inc/Form/FieldsetOpenElement.php b/inc/Form/FieldsetOpenElement.php
new file mode 100644
index 000000000..a7de461fa
--- /dev/null
+++ b/inc/Form/FieldsetOpenElement.php
@@ -0,0 +1,36 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class FieldsetOpenElement
+ *
+ * Opens a Fieldset with an optional legend
+ *
+ * @package dokuwiki\Form
+ */
+class FieldsetOpenElement extends TagOpenElement {
+
+ /**
+ * @param string $legend
+ * @param array $attributes
+ */
+ public function __construct($legend='', $attributes = array()) {
+ // this is a bit messy and we just do it for the nicer class hierarchy
+ // the parent would expect the tag in $value but we're storing the
+ // legend there, so we have to set the type manually
+ parent::__construct($legend, $attributes);
+ $this->type = 'fieldsetopen';
+ }
+
+ /**
+ * The HTML representation of this element
+ *
+ * @return string
+ */
+ public function toHTML() {
+ $html = '<fieldset '.buildAttributes($this->attrs()).'>';
+ $legend = $this->val();
+ if($legend) $html .= DOKU_LF.'<legend>'.hsc($legend).'</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 '<label ' . buildAttributes($this->attrs()) . '>' . hsc($this->label) . '</label>';
+ return '<label ' . buildAttributes($this->attrs()) . '>' . hsc($this->val()) . '</label>';
}
}
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 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class TagCloseElement
+ *
+ * Creates an HTML close tag. You have to make sure it has been opened
+ * before or this will produce invalid HTML
+ *
+ * @package dokuwiki\Form
+ */
+class TagCloseElement extends ValueElement {
+
+ /**
+ * @param string $tag
+ * @param array $attributes
+ */
+ public function __construct($tag, $attributes = array()) {
+ parent::__construct('tagclose', $tag, $attributes);
+ }
+
+ /**
+ * The HTML representation of this element
+ *
+ * @return string
+ */
+ public function toHTML() {
+ return '</'.$this->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 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class TagElement
+ *
+ * Creates a self closing HTML tag
+ *
+ * @package dokuwiki\Form
+ */
+class TagElement extends ValueElement {
+
+ /**
+ * @param string $tag
+ * @param array $attributes
+ */
+ public function __construct($tag, $attributes = array()) {
+ parent::__construct('tag', $tag, $attributes);
+ }
+
+ /**
+ * The HTML representation of this element
+ *
+ * @return string
+ */
+ public function toHTML() {
+ return '<'.$this->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 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class TagOpenElement
+ *
+ * Creates an open HTML tag. You have to make sure you close it
+ * again or this will produce invalid HTML
+ *
+ * @package dokuwiki\Form
+ */
+class TagOpenElement extends ValueElement {
+
+ /**
+ * @param string $tag
+ * @param array $attributes
+ */
+ public function __construct($tag, $attributes = array()) {
+ parent::__construct('tagopen', $tag, $attributes);
+ }
+
+ /**
+ * The HTML representation of this element
+ *
+ * @return string
+ */
+ public function toHTML() {
+ return '<'.$this->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 @@
+<?php
+
+namespace dokuwiki\Form;
+
+/**
+ * Class ValueElement
+ *
+ * Just like an Element but it's value is not part of its attributes
+ *
+ * What the value is (tag name, content, etc) is defined by the actual implementations
+ *
+ * @package dokuwiki\Form
+ */
+abstract class ValueElement extends Element {
+
+ /**
+ * @var string holds the element's value
+ */
+ protected $value = '';
+
+ /**
+ * @param string $type
+ * @param array $value
+ * @param array $attributes
+ */
+ public function __construct($type, $value, $attributes = array()) {
+ parent::__construct($type, $attributes);
+ $this->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;
+ }
+
+}