summaryrefslogtreecommitdiff
path: root/inc/Form
diff options
context:
space:
mode:
Diffstat (limited to 'inc/Form')
-rw-r--r--inc/Form/ButtonElement.php34
-rw-r--r--inc/Form/Form.php63
-rw-r--r--inc/Form/InputElement.php9
-rw-r--r--inc/Form/LabelElement.php (renamed from inc/Form/Label.php)6
4 files changed, 103 insertions, 9 deletions
diff --git a/inc/Form/ButtonElement.php b/inc/Form/ButtonElement.php
new file mode 100644
index 000000000..77c30ed4f
--- /dev/null
+++ b/inc/Form/ButtonElement.php
@@ -0,0 +1,34 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class ButtonElement
+ *
+ * Represents a simple button
+ *
+ * @package dokuwiki\Form
+ */
+class ButtonElement extends Element {
+
+ /** @var string HTML content */
+ protected $content = '';
+
+ /**
+ * @param string $name
+ * @param string $content HTML content of the button. You have to escape it yourself.
+ */
+ function __construct($name, $content = '') {
+ parent::__construct('button', array('name' => $name, 'value' => 1));
+ $this->content = $content;
+ }
+
+ /**
+ * The HTML representation of this element
+ *
+ * @return string
+ */
+ public function toHTML() {
+ return '<button ' . buildAttributes($this->attrs()) . '>'.$this->content.'</button>';
+ }
+
+}
diff --git a/inc/Form/Form.php b/inc/Form/Form.php
index 625557fa1..7eaa53041 100644
--- a/inc/Form/Form.php
+++ b/inc/Form/Form.php
@@ -140,7 +140,7 @@ class Form extends Element {
* @return Element
*/
public function addElement(Element $element, $pos = -1) {
- if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
+ if(is_a($element, '\dokuwiki\Form\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
if($pos < 0) {
$this->elements[] = $element;
} else {
@@ -156,7 +156,7 @@ class Form extends Element {
* @param $pos 0-based position of the element to replace
*/
public function replaceElement(Element $element, $pos) {
- if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
+ if(is_a($element, '\dokuwiki\Form\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
array_splice($this->elements, $pos, 1, array($element));
}
@@ -234,6 +234,65 @@ class Form extends Element {
}
/**
+ * Adds a simple button, escapes the content for you
+ *
+ * @param string $name
+ * @param string $content
+ * @param int $pos
+ * @return Element
+ */
+ public function addButton($name, $content, $pos = -1) {
+ return $this->addElement(new ButtonElement($name, hsc($content)), $pos);
+ }
+
+ /**
+ * Adds a simple button, allows HTML for content
+ *
+ * @param string $name
+ * @param string $html
+ * @param int $pos
+ * @return Element
+ */
+ public function addButtonHTML($name, $html, $pos = -1) {
+ return $this->addElement(new ButtonElement($name, $html), $pos);
+ }
+
+ /**
+ * Adds a label referencing another input element, escapes the label for you
+ *
+ * @param $label
+ * @param string $for
+ * @param int $pos
+ * @return Element
+ */
+ public function addLabel($label, $for='', $pos = -1) {
+ return $this->addLabelHTML(hsc($label), $for, $pos);
+ }
+
+ /**
+ * Adds a label referencing another input element, allows HTML for content
+ *
+ * @param string $content
+ * @param string|Element $for
+ * @param int $pos
+ * @return Element
+ */
+ public function addLabelHTML($content, $for='', $pos = -1) {
+ $element = new LabelElement(hsc($content));
+
+ if(is_a($for, '\dokuwiki\Form\Element')) {
+ /** @var Element $for */
+ $for = $for->id();
+ }
+ $for = (string) $for;
+ if($for !== '') {
+ $element->attr('for', $for);
+ }
+
+ return $this->addElement($element, $pos);
+ }
+
+ /**
* Add fixed HTML to the form
*
* @param $html
diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php
index 5908f7d11..694dd0848 100644
--- a/inc/Form/InputElement.php
+++ b/inc/Form/InputElement.php
@@ -12,7 +12,7 @@ namespace dokuwiki\Form;
*/
class InputElement extends Element {
/**
- * @var Label
+ * @var LabelElement
*/
protected $label = null;
@@ -24,18 +24,19 @@ class InputElement extends Element {
/**
* @param string $type The type of this element
* @param string $name The name of this form element
- * @param string $label The label text for this element
+ * @param string $label The label text for this element (will be autoescaped)
*/
public function __construct($type, $name, $label = '') {
parent::__construct($type, array('name' => $name));
$this->attr('name', $name);
- if($label) $this->label = new Label($label);
+ $this->attr('type', $type);
+ if($label) $this->label = new LabelElement($label);
}
/**
* Returns the label element if there's one set
*
- * @return Label|null
+ * @return LabelElement|null
*/
public function getLabel() {
return $this->label;
diff --git a/inc/Form/Label.php b/inc/Form/LabelElement.php
index 8dcd7cd5f..9c8d54277 100644
--- a/inc/Form/Label.php
+++ b/inc/Form/LabelElement.php
@@ -5,12 +5,12 @@ namespace dokuwiki\Form;
* Class Label
* @package dokuwiki\Form
*/
-class Label extends ValueElement {
+class LabelElement extends ValueElement {
/**
* Creates a new Label
*
- * @param string $label
+ * @param string $label This is is raw HTML and will not be escaped
*/
public function __construct($label) {
parent::__construct('label', $label);
@@ -22,6 +22,6 @@ class Label extends ValueElement {
* @return string
*/
public function toHTML() {
- return '<label ' . buildAttributes($this->attrs()) . '>' . hsc($this->val()) . '</label>';
+ return '<label ' . buildAttributes($this->attrs()) . '>' . $this->val() . '</label>';
}
}