summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2015-05-08 19:12:21 +0200
committerAndreas Gohr <andi@splitbrain.org>2015-05-08 19:12:21 +0200
commitde19515f04567db78bd41d5bff68a88bfb8c2a22 (patch)
treee478b1cdf6a186c162f6b62b8dce5addd2f7c17d
parent6d0ceaf93ca31dfb83fd4325ef2eecd9cef733c0 (diff)
downloadrpg-de19515f04567db78bd41d5bff68a88bfb8c2a22.tar.gz
rpg-de19515f04567db78bd41d5bff68a88bfb8c2a22.tar.bz2
started with the compatibility layer
-rw-r--r--inc/Form/Element.php10
-rw-r--r--inc/Form/Form.php27
-rw-r--r--inc/Form/HTMLElement.php48
-rw-r--r--inc/Form/InputElement.php34
-rw-r--r--inc/Form/LegacyForm.php156
-rw-r--r--inc/Form/TextareaElement.php1
6 files changed, 260 insertions, 16 deletions
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 @@
<?php
namespace dokuwiki\Form;
-
/**
* Class Element
*
@@ -31,6 +30,15 @@ abstract class Element {
}
/**
+ * Type of this element
+ *
+ * @return string
+ */
+ public function getType() {
+ return $this->type;
+ }
+
+ /**
* Gets or sets an attribute
*
* When no $value is given, the current content of the attribute is returned.
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 = '<form ' . buildAttributes($this->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 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class HTMLElement
+ *
+ * Holds arbitrary HTML that is added as is to the Form
+ *
+ * @package dokuwiki\Form
+ */
+class HTMLElement extends Element {
+
+ /**
+ * @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;
+ }
+
+ /**
+ * 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 '<label ' . buildAttributes($this->label->attrs()) . '>' . DOKU_LF .
- '<span>' . hsc($this->label->label) . '</span>' . DOKU_LF .
- $this->mainElementHTML() . DOKU_LF .
- '</label>';
+ if($this->label) {
+ return '<label ' . buildAttributes($this->label->attrs()) . '>' . DOKU_LF .
+ '<span>' . hsc($this->label->label) . '</span>' . DOKU_LF .
+ $this->mainElementHTML() . DOKU_LF .
+ '</label>';
+ } 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 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class LegacyForm
+ *
+ * Provides a compatibility layer to the old Doku_Form API
+ *
+ * This can be used to work with the modern API on forms provided by old events for
+ * example. When you start new forms, just use Form\Form
+ *
+ * @package dokuwiki\Form
+ */
+class LegacyForm extends Form {
+
+ /**
+ * Creates a new modern form from an old legacy Doku_Form
+ *
+ * @param \Doku_Form $oldform
+ */
+ public function __construct(\Doku_Form $oldform) {
+ parent::__construct($oldform->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');
}
/**