add functions for easily adding images
[php-utility-classes.git] / include / cbsm / util / document.php-class
index 11ebf973f8738a9b662f0fc8d268eb5827674332..58637850153d708b5015f58f128f0df484be1dec 100755 (executable)
@@ -46,6 +46,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
   //     returns the new child
   //
+  // function appendImage($src, [$alt_text])
+  //   appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // function appendFormDiv($action, $method, $name)
   //   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
   // function appendFormDiv($action, $method, $name)
   //   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
@@ -70,6 +74,18 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
   //     returns the new child
   //
+  // function appendTextArea($name, $columns, $rows, [$id], [$value])
+  //   appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
+  //     returns the new child
+  //
+  // function appendElementSelect($name, [$id], [$options], [$default])
+  //   appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
+  //     returns the new child
+  //
+  // function appendElementOption($key, $desc, [$selected])
+  //   appends an ExtendedDocument::createElementOption() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // function appendLabel($for_id, $value)
   //   appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
   //     returns the new child
   // function appendLabel($for_id, $value)
   //   appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
   //     returns the new child
@@ -90,9 +106,16 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
   //     NO return value!
   //
   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
   //     NO return value!
   //
+  // function appendCOMElement($module, $attributes)
+  //   appends an ExtendedDocument::createCOMElement() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // function createElementLink($target, [$value])
   //   returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
   //
   // function createElementLink($target, [$value])
   //   returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
   //
+  // function createElementImage($src, [$alt_text])
+  //   returns an ExtendedElement that is an HTML <img> with the given (src) and alt attributes (set to '' by default)
+  //
   // function createElementForm($action, $method, $name)
   //   returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
   //   with the given action, method, and name
   // function createElementForm($action, $method, $name)
   //   returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
   //   with the given action, method, and name
@@ -115,16 +138,33 @@ class ExtendedDocument extends DOMDocument {
   // function createElementInputSubmit($value)
   //   returns an ExtendedElement that is an HTML <input> of type 'submit' with the given name and value
   //
   // function createElementInputSubmit($value)
   //   returns an ExtendedElement that is an HTML <input> of type 'submit' with the given name and value
   //
+  // function createElementTextArea($name, $columns, $rows, [$id], [$value])
+  //   returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
+  //   and optionally id and value
+  //
+  // function createElementSelect($name, [$id], [$options], [$default])
+  //   returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
+  //   array of options (key => description) and key of the by-default selected entry
+  //
+  // function createElementOption($key, $desc, [$selected])
+  //   returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
+  //   and optionally bool that tells if the entry is selected
+  //
   // function createElementLabel($for_id, $value)
   //   returns an ExtendedElement that is an HTML <label> with the given 'for' and value
   //
   // function createElementJS($jsdata)
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
   // function createElementLabel($for_id, $value)
   //   returns an ExtendedElement that is an HTML <label> with the given 'for' and value
   //
   // function createElementJS($jsdata)
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
+  //
+  // function createCOMElement($module, $attributes)
+  //   returns an ExtendedElement that is in COM_NS namespace, with the given module as name and the
+  //     given name=>value array as attributes
 
   function __construct($version = '1.0', $encoding = 'utf-8') {
     // make sure the default DOMDocument constructor runs
     parent::__construct($version, $encoding);
     $this->registerNodeClass('DOMElement', 'ExtendedElement');
 
   function __construct($version = '1.0', $encoding = 'utf-8') {
     // make sure the default DOMDocument constructor runs
     parent::__construct($version, $encoding);
     $this->registerNodeClass('DOMElement', 'ExtendedElement');
+    $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
   }
 
   function appendElement($name, $value = '') {
   }
 
   function appendElement($name, $value = '') {
@@ -139,6 +179,9 @@ class ExtendedDocument extends DOMDocument {
   function appendLink($target, $value = '') {
     return $this->appendChild($this->createElementLink($target, $value));
   }
   function appendLink($target, $value = '') {
     return $this->appendChild($this->createElementLink($target, $value));
   }
+  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));
     return $formelem->appendElement('div');
   function appendFormDiv($action, $method, $name) {
     $formelem = $this->appendChild($this->createElementForm($action, $method, $name));
     return $formelem->appendElement('div');
@@ -158,6 +201,15 @@ class ExtendedDocument extends DOMDocument {
   function appendInputSubmit($value) {
     return $this->appendChild($this->createElementInputSubmit($value));
   }
   function appendInputSubmit($value) {
     return $this->appendChild($this->createElementInputSubmit($value));
   }
+  function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
+    return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
+  }
+  function appendElementSelect($name, $id = null, $options = array(), $default = null) {
+    return $this->appendChild($this->createElementSelect($name, $id, $options, $default));
+  }
+  function appendElementOption($key, $desc, $selected = false) {
+    return $this->appendChild($this->createElementOption($key, $desc, $selected));
+  }
   function appendLabel($for_id, $value) {
     return $this->appendChild($this->createElementLabel($for_id, $value));
   }
   function appendLabel($for_id, $value) {
     return $this->appendChild($this->createElementLabel($for_id, $value));
   }
@@ -167,6 +219,9 @@ class ExtendedDocument extends DOMDocument {
   function appendJSElement($jsdata) {
     $this->appendChild($this->createElementJS($jsdata));
   }
   function appendJSElement($jsdata) {
     $this->appendChild($this->createElementJS($jsdata));
   }
+  function appendCOMElement($module, $attributes) {
+    $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  }
 
   function appendHTMLMarkup($htmldata, $parentNode = null) {
     if (is_null($parentNode)) { $parentNode =& $this; }
 
   function appendHTMLMarkup($htmldata, $parentNode = null) {
     if (is_null($parentNode)) { $parentNode =& $this; }
@@ -188,7 +243,14 @@ class ExtendedDocument extends DOMDocument {
 
   function createElementLink($target, $value = '') {
     $link = $this->createElement('a', $value);
 
   function createElementLink($target, $value = '') {
     $link = $this->createElement('a', $value);
-    $link->setAttribute('href', $target);
+    $link->setAttribute('href', $target); // XXX: take care of & etc. in links
+    return $link;
+  }
+
+  function createElementImage($src, $alt_text = '') {
+    $link = $this->createElement('img');
+    $link->setAttribute('src', $src);
+    $link->setAttribute('alt', $alt_text);
     return $link;
   }
 
     return $link;
   }
 
@@ -246,6 +308,32 @@ class ExtendedDocument extends DOMDocument {
     return $submitbtn;
   }
 
     return $submitbtn;
   }
 
+  function createElementTextArea($name, $columns, $rows, $id = null, $value = null) {
+    $txtarea = $this->createElement('textarea', $value);
+    $txtarea->setAttribute('name', $name);
+    $txtarea->setAttribute('cols', $columns);
+    $txtarea->setAttribute('rows', $rows);
+    if (!is_null($id)) { $txtarea->setAttribute('id', $id); }
+    return $txtarea;
+  }
+
+  function createElementSelect($name, $id = null, $options = array(), $default = null) {
+    $select = $this->createElement('select');
+    $select->setAttribute('name', $name);
+    if (!is_null($id)) { $select->setAttribute('id', $id); }
+    foreach ($options as $key => $desc) {
+      $select->appendElementOption($key, $desc, ($key == $default));
+    }
+    return $select;
+  }
+
+  function createElementOption($key, $desc, $selected = false) {
+    $option = $this->createElement('option', $desc);
+    $option->setAttribute('value', $key);
+    if ($selected) { $option->setAttribute('selected', ''); }
+    return $option;
+  }
+
   function createElementLabel($for_id, $value) {
     $label = $this->createElement('label', $value);
     $label->setAttribute('for', $for_id);
   function createElementLabel($for_id, $value) {
     $label = $this->createElement('label', $value);
     $label->setAttribute('for', $for_id);
@@ -258,6 +346,16 @@ class ExtendedDocument extends DOMDocument {
     $jselem->appendChild($this->createCDATASection($jsdata));
     return $jselem;
   }
     $jselem->appendChild($this->createCDATASection($jsdata));
     return $jselem;
   }
+
+  function createCOMElement($module, $attributes) {
+    $com_elem = $this->createElementNS(COM_NS, $module);
+    if (is_array($attributes) && count($attributes)) {
+      foreach ($attributes as $name=>$value) {
+        $com_elem->setAttribute($name, $value);
+      }
+    }
+    return $com_elem;
+  }
 }
 
 class ExtendedElement extends DOMElement {
 }
 
 class ExtendedElement extends DOMElement {
@@ -277,6 +375,10 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
   //     returns the new child
   //
+  // function appendImage($src, [$alt_text])
+  //   appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // function appendFormDiv($action, $method, $name)
   //   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
   // function appendFormDiv($action, $method, $name)
   //   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
@@ -301,6 +403,18 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
   //     returns the new child
   //
+  // function appendTextArea($name, $columns, $rows, [$id], [$value])
+  //   appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
+  //     returns the new child
+  //
+  // function appendElementSelect($name, [$id], [$options], [$default])
+  //   appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
+  //     returns the new child
+  //
+  // function appendElementOption($key, $desc, [$selected])
+  //   appends an ExtendedDocument::createElementOption() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // function appendLabel($for_id, $value)
   //   appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
   //     returns the new child
   // function appendLabel($for_id, $value)
   //   appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
   //     returns the new child
@@ -320,6 +434,10 @@ class ExtendedElement extends DOMElement {
   // function appendJSElement($jsdata)
   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
   //     NO return value!
   // function appendJSElement($jsdata)
   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
   //     NO return value!
+  //
+  // function appendCOMElement($module, $attributes)
+  //   appends an ExtendedDocument::createCOMElement() as a child of this element (see there for params)
+  //     returns the new child
 
   function appendElement($name, $value = '') {
     return $this->appendChild($this->ownerDocument->createElement($name, $value));
 
   function appendElement($name, $value = '') {
     return $this->appendChild($this->ownerDocument->createElement($name, $value));
@@ -332,6 +450,9 @@ class ExtendedElement extends DOMElement {
   function appendLink($target, $value = '') {
     return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
   }
   function appendLink($target, $value = '') {
     return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
   }
+  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));
     return $formelem->appendElement('div');
   function appendFormDiv($action, $method, $name) {
     $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
     return $formelem->appendElement('div');
@@ -351,6 +472,15 @@ class ExtendedElement extends DOMElement {
   function appendInputSubmit($value) {
     return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
   }
   function appendInputSubmit($value) {
     return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
   }
+  function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
+  }
+  function appendElementSelect($name, $id = null, $options = array(), $default = null) {
+    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
+  }
+  function appendElementOption($key, $desc, $selected = false) {
+    return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
+  }
   function appendLabel($for_id, $value) {
     return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
   }
   function appendLabel($for_id, $value) {
     return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
   }
@@ -366,5 +496,151 @@ class ExtendedElement extends DOMElement {
   function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
   function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
+  function appendCOMElement($module, $attributes) {
+    $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  }
+}
+
+class ExtendedDocumentFragment extends DOMDocumentFragment {
+  // ExtendedDocumentFragment PHP class
+  // this extends the general PHP DOM Document Fragment class to simplify some usual constructs
+  //
+  // function appendElement($name, [$value])
+  //   appends a DOMDocument::createElement() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendElementXML($name, $xmldata)
+  //   appends a DOMDocument::createElement() with the given name as a child of this fragment,
+  //   with an ExtendedDocument::createXMLFragment() of the given XML data inside
+  //     returns the new child
+  //
+  // function appendLink($target, [$value])
+  //   appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendImage($src, [$alt_text])
+  //   appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
+  //     returns the new child
+  //
+  // function appendFormDiv($action, $method, $name)
+  //   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
+  //
+  // function appendInputHidden($name, $value)
+  //   appends an ExtendedDocument::createElementInputHidden() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendInputText($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputText() 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
+  //
+  // function appendInputCheckbox($name, $id, $value, $checked)
+  //   appends an ExtendedDocument::createElementInputCheckbox() 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
+  //
+  // function appendTextArea($name, $columns, $rows, [$id], [$value])
+  //   appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendElementSelect($name, [$id], [$options], [$default])
+  //   appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendElementOption($key, $desc, [$selected])
+  //   appends an ExtendedDocument::createElementOption() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendLabel($for_id, $value)
+  //   appends an ExtendedDocument::createElementLabel() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendText($text)
+  //   appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
+  // function appendHTMLMarkup($htmldata)
+  //   appends a representation of the HTML data as children of this fragment
+  //     NO return value!
+  //
+  // function appendXMLMarkup($xmldata)
+  //   appends a representation of the XML data as children of this fragment
+  //     NO return value!
+  //
+  // function appendJSElement($jsdata)
+  //   appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
+  //     NO return value!
+  //
+  // function appendCOMElement($module, $attributes)
+  //   appends an ExtendedDocument::createCOMElement() as a child of this fragment (see there for params)
+  //     returns the new child
+
+  function appendElement($name, $value = '') {
+    return $this->appendChild($this->ownerDocument->createElement($name, $value));
+  }
+  function appendElementXML($name, $xmldata) {
+    $aelem = $this->appendChild($this->ownerDocument->createElement($name));
+    $aelem->appendXMLMarkup($xmldata);
+    return $aelem;
+  }
+  function appendLink($target, $value = '') {
+    return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
+  }
+  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));
+    return $formelem->appendElement('div');
+  }
+  function appendInputHidden($name, $value) {
+    return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
+  }
+  function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputText($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 appendInputSubmit($value) {
+    return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
+  }
+  function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
+  }
+  function appendElementSelect($name, $id = null, $options = array(), $default = null) {
+    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
+  }
+  function appendElementOption($key, $desc, $selected = false) {
+    return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
+  }
+  function appendLabel($for_id, $value) {
+    return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
+  }
+  function appendText($text) {
+    return $this->appendChild($this->ownerDocument->createTextNode($text));
+  }
+  function appendHTMLMarkup($htmldata) {
+    $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
+  }
+  function appendXMLMarkup($xmldata) {
+    $this->ownerDocument->appendXMLMarkup($xmldata, $this);
+  }
+  function appendJSElement($jsdata) {
+    $this->appendChild($this->ownerDocument->createElementJS($jsdata));
+  }
+  function appendCOMElement($module, $attributes) {
+    $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  }
 }
 ?>
 }
 ?>