add support for file input
[php-utility-classes.git] / include / cbsm / util / document.php-class
index 257d52bcbcda23848532fbba679ba071a6d78e27..c267623a5f26831a85b21a281399d066cb576c42 100755 (executable)
@@ -50,7 +50,11 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
   //     returns the new child
   //
-  // function appendFormDiv($action, $method, $name)
+  // function appendForm($action, $method, $name, [$id])
+  //   appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
+  //     returns the new child
+  //
+  // function appendFormDiv($action, $method, $name, [$id])
   //   appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
   //     returns an HTML <div> that is a child of the new child
   //
@@ -62,6 +66,14 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementInputText() as a child of this document (see there for params)
   //     returns the new child
   //
+  // function appendInputNumber($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputNumber() as a child of this document (see there for params)
+  //     returns the new child
+  //
+  // function appendInputPassword($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputPassword() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // function appendInputRadio($name, $id, $value, $checked)
   //   appends an ExtendedDocument::createElementInputRadio() as a child of this document (see there for params)
   //     returns the new child
@@ -70,6 +82,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementInputCheckbox() as a child of this document (see there for params)
   //     returns the new child
   //
+  // function appendInputFile($name, $id, $accept)
+  //   appends an ExtendedDocument::createElementInputFile() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // function appendInputSubmit($value)
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
   //     returns the new child
@@ -135,6 +151,14 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
   //   and optionally id and value
   //
+  // function createElementInputNumber($name, $maxlength, $size, [$id], [$value])
+  //   returns an ExtendedElement that is an HTML <input> of type 'number' with the given name, maxlength, size,
+  //   and optionally id and value
+  //
+  // function createElementInputPassword($name, $maxlength, $size, [$id], [$value])
+  //   returns an ExtendedElement that is an HTML <input> of type 'password' with the given name, maxlength, size,
+  //   and optionally id and value
+  //
   // function createElementInputRadio($name, $id, $value, $checked)
   //   returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
   //   checked state
@@ -143,6 +167,9 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
   //   checked state
   //
+  // function createElementInputFile($name, $id, $accept)
+  //   returns an ExtendedElement that is an HTML <input> of type 'file' with the given name, id and accept
+  //
   // function createElementInputSubmit($value)
   //   returns an ExtendedElement that is an HTML <input> of type 'submit' with the given value as label
   //
@@ -193,8 +220,11 @@ class ExtendedDocument extends DOMDocument {
   function appendImage($src, $alt_text = '') {
     return $this->appendChild($this->createElementImage($src, $alt_text));
   }
-  function appendFormDiv($action, $method, $name) {
-    $formelem = $this->appendChild($this->createElementForm($action, $method, $name));
+  function appendForm($action, $method, $name, $id = null) {
+    return $this->appendChild($this->createElementForm($action, $method, $name, $id));
+  }
+  function appendFormDiv($action, $method, $name, $id = null) {
+    $formelem = $this->appendChild($this->createElementForm($action, $method, $name, $id));
     return $formelem->appendElement('div');
   }
   function appendInputHidden($name, $value) {
@@ -203,12 +233,21 @@ class ExtendedDocument extends DOMDocument {
   function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
     return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
   }
+  function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->createElementInputNumber($name, $maxlength, $size, $id, $value));
+  }
+  function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->createElementInputPassword($name, $maxlength, $size, $id, $value));
+  }
   function appendInputRadio($name, $id, $value, $checked) {
     return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
   }
   function appendInputCheckbox($name, $id, $value, $checked) {
     return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
   }
+  function appendInputFile($name, $id, $accept) {
+    return $this->appendChild($this->createElementInputFile($name, $id, $accept));
+  }
   function appendInputSubmit($value) {
     return $this->appendChild($this->createElementInputSubmit($value));
   }
@@ -258,6 +297,13 @@ class ExtendedDocument extends DOMDocument {
     }
   }
 
+  function createElement($name, $value = '') {
+    // Adding the $value in DOMDocument's createElement does NOT escape it, so override it and use appendText to support that.
+    $aelem = parent::createElement($name);
+    $aelem->appendText($value);
+    return $aelem;
+  }
+
   function createElementLink($target, $value = '') {
     $link = $this->createElement('a', $value);
     $link->setAttribute('href', $target); // XXX: take care of & etc. in links
@@ -265,17 +311,18 @@ class ExtendedDocument extends DOMDocument {
   }
 
   function createElementImage($src, $alt_text = '') {
-    $link = $this->createElement('img');
-    $link->setAttribute('src', $src);
-    $link->setAttribute('alt', $alt_text);
-    return $link;
+    $img = $this->createElement('img');
+    $img->setAttribute('src', $src);
+    $img->setAttribute('alt', $alt_text);
+    return $img;
   }
 
-  function createElementForm($action, $method, $name) {
+  function createElementForm($action, $method, $name, $id = null) {
     $formelem = $this->createElement('form');
     $formelem->setAttribute('action', $action);
     $formelem->setAttribute('method', $method);
     $formelem->setAttribute('name', $name);
+    $formelem->setAttribute('id', $id);
     return $formelem;
   }
 
@@ -298,11 +345,33 @@ class ExtendedDocument extends DOMDocument {
     return $txfield;
   }
 
+  function createElementInputNumber($name, $maxlength, $size, $id = null, $value = null) {
+    $txfield = $this->createElement('input');
+    $txfield->setAttribute('type', 'number');
+    if (!is_null($id)) { $txfield->setAttribute('id', $id); }
+    $txfield->setAttribute('name', $name);
+    $txfield->setAttribute('maxlength', $maxlength);
+    $txfield->setAttribute('size', $size);
+    if (!is_null($value)) { $txfield->setAttribute('value', $value); }
+    return $txfield;
+  }
+
+  function createElementInputPassword($name, $maxlength, $size, $id = null, $value = null) {
+    $pwfield = $this->createElement('input');
+    $pwfield->setAttribute('type', 'password');
+    if (!is_null($id)) { $pwfield->setAttribute('id', $id); }
+    $pwfield->setAttribute('name', $name);
+    $pwfield->setAttribute('maxlength', $maxlength);
+    $pwfield->setAttribute('size', $size);
+    if (!is_null($value)) { $pwfield->setAttribute('value', $value); }
+    return $pwfield;
+  }
+
   function createElementInputRadio($name, $id, $value, $checked) {
     $radio = $this->createElement('input');
     $radio->setAttribute('type', 'radio');
     $radio->setAttribute('name', $name);
-    $radio->setAttribute('id', $id);
+    if (!is_null($id)) { $radio->setAttribute('id', $id); }
     $radio->setAttribute('value', $value);
     if ($checked) { $radio->setAttribute('checked', ''); }
     return $radio;
@@ -312,12 +381,21 @@ class ExtendedDocument extends DOMDocument {
     $cbox = $this->createElement('input');
     $cbox->setAttribute('type', 'checkbox');
     $cbox->setAttribute('name', $name);
-    $cbox->setAttribute('id', $id);
+    if (!is_null($id)) { $cbox->setAttribute('id', $id); }
     $cbox->setAttribute('value', $value);
     if ($checked) { $cbox->setAttribute('checked', ''); }
     return $cbox;
   }
 
+  function createElementInputFile($name, $id, $accept) {
+    $fileinput = $this->createElement('input');
+    $fileinput->setAttribute('type', 'file');
+    $fileinput->setAttribute('name', $name);
+    if (!is_null($id)) { $fileinput->setAttribute('id', $id); }
+    $fileinput->setAttribute('accept', $accept);
+    return $fileinput;
+  }
+
   function createElementInputSubmit($value) {
     $submitbtn = $this->createElement('input');
     $submitbtn->setAttribute('type', 'submit');
@@ -404,7 +482,11 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
   //     returns the new child
   //
-  // function appendFormDiv($action, $method, $name)
+  // function appendForm($action, $method, $name, [$id])
+  //   appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
+  //     returns the new child
+  //
+  // function appendFormDiv($action, $method, $name, [$id])
   //   appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
   //     returns an HTML <div> that is a child of the new child
   //
@@ -416,6 +498,14 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
   //     returns the new child
   //
+  // function appendInputNumber($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputNumber() as a child of this element (see there for params)
+  //     returns the new child
+  //
+  // function appendInputPassword($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputPassword() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // function appendInputRadio($name, $id, $value, $checked)
   //   appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
   //     returns the new child
@@ -424,6 +514,10 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
   //     returns the new child
   //
+  // function appendInputFile($name, $id, $accept)
+  //   appends an ExtendedDocument::createElementInputFile() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // function appendInputSubmit($value)
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
   //     returns the new child
@@ -486,8 +580,11 @@ class ExtendedElement extends DOMElement {
   function appendImage($src, $alt_text = '') {
     return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
   }
-  function appendFormDiv($action, $method, $name) {
-    $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
+  function appendForm($action, $method, $name, $id = null) {
+    return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
+  }
+  function appendFormDiv($action, $method, $name, $id = null) {
+    $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
     return $formelem->appendElement('div');
   }
   function appendInputHidden($name, $value) {
@@ -496,12 +593,21 @@ class ExtendedElement extends DOMElement {
   function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
   }
+  function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
+  }
+  function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
+  }
   function appendInputRadio($name, $id, $value, $checked) {
     return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
   }
   function appendInputCheckbox($name, $id, $value, $checked) {
     return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
   }
+  function appendInputFile($name, $id, $accept) {
+    return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
+  }
   function appendInputSubmit($value) {
     return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
   }
@@ -561,7 +667,11 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
   //     returns the new child
   //
-  // function appendFormDiv($action, $method, $name)
+  // function appendForm($action, $method, $name, [$id])
+  //   appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendFormDiv($action, $method, $name, [$id])
   //   appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
   //     returns an HTML <div> that is a child of the new child
   //
@@ -573,6 +683,14 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementInputText() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // function appendInputNumber($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputNumber() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendInputPassword($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputPassword() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
   // function appendInputRadio($name, $id, $value, $checked)
   //   appends an ExtendedDocument::createElementInputRadio() as a child of this fragment (see there for params)
   //     returns the new child
@@ -581,6 +699,10 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementInputCheckbox() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // function appendInputFile($name, $id, $accept)
+  //   appends an ExtendedDocument::createElementInputFile() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
   // function appendInputSubmit($value)
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this fragment (see there for params)
   //     returns the new child
@@ -643,8 +765,11 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   function appendImage($src, $alt_text = '') {
     return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
   }
-  function appendFormDiv($action, $method, $name) {
-    $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
+  function appendForm($action, $method, $name, $id = null) {
+    return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
+  }
+  function appendFormDiv($action, $method, $name, $id = null) {
+    $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
     return $formelem->appendElement('div');
   }
   function appendInputHidden($name, $value) {
@@ -653,12 +778,21 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
   }
+  function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
+  }
+  function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
+  }
   function appendInputRadio($name, $id, $value, $checked) {
     return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
   }
   function appendInputCheckbox($name, $id, $value, $checked) {
     return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
   }
+  function appendInputFile($name, $id, $accept) {
+    return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
+  }
   function appendInputSubmit($value) {
     return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
   }