<?php
/*
  $Id: class.select.php,v 1.0 2006/07/07 22:10:38 hpdl Exp $

  Author: Kees Schepers  
  More Info: http://www.phphulp.nl/php/scripts/4/692
  Released under the GNU General Public License
*/
interface htmlElementInterface {
      public function getHTML();
      public function getDomObject();
}
/**
* Class to create a select box and to set some property's
**/
class select implements htmlElementInterface {
      private $aAttributes;
      private $objDomDocument;
      private $aData;
      private $objSelect;
      public function __Construct($p_aData) {            
            /**
            * Check and build the data array
            **/
            $this->aData = array();
            $this->setData($p_aData);
            
            /**
            * Start the DOM document for creating HTML
            **/
            $this->objDomDocument = new DOMDocument('1.0');
      }      
      public function __Set($key,$value) {
            switch($key) {
                  case 'id' :
                        $this->aAttributes['id'] = $value;
                        break;
                  case 'name' :
                        $this->aAttributes['name'] = $value;
                        break;
                  case 'onchange' :
                        $this->aAttributes['onchange'] = $value;
                        break;
                  case 'class' :
                        $this->aAttributes['class'] = $value;
                        break;
                  case 'style' :
                        $this->aAttributes['style'] = $value;
                        break;
                  default :
                        throw new exception('select::__Set() -> Trying to set an option which not exists! ('.$key.')');
                        break;                        
            }
      }
      /**
      * function which builds the correct data array which will be internaly used.
      * @param array $p_aData array with elements for the select list
      **/
      private function setData($p_aData) {
            if( is_array($p_aData) ) {
                  foreach($p_aData AS $element) {
                        if( is_array( $element ) ) {
                              if( count( $element ) > 1 )  {
                                    /**
                                    * Array has a value and a text node. The first index is the value second the                                     
                                    * text node. If more keys given, ignore them.
                                    **/
                                    $this->aData[] = array('value' => $element[0],'text' => $element[1]);
                              } else {
                                    throw new exception('select::setData() -> Wrong data given as parameter! (expects an array with at least more then one index)');
                              }
                        } else {
                              /**
                              * Text and value are the same
                              **/
                              $this->aData[] = array('value' => $element, 'text' => $element);
                        }
                  }
            } else {
                  throw new exception('select::setData() -> Data given as parameter should be an array!');
            }
      }
      /**
      * function which creates the select element, and calls the attribute(s)-creator-function
      **/
      private function setSelectElement() {
            $this->objSelect = $this->objDomDocument->createElement('select');
            $this->objSelect = $this->objDomDocument->appendChild($this->objSelect);
            $this->setAttributes();
      }
      /**
      * function which adds attributes to the select element.
      **/
      private function setAttributes() {
            if( is_array($this->aAttributes) ) {
                  foreach($this->aAttributes AS $attributeName=>$attributeValue) {
                        $this->objSelect->setAttribute($attributeName,$attributeValue);
                  }
            }
      }
      /**
      * This function loops the array and creates the select box options
      **/
      private function setSelectOptions() {
            if( is_array($this->aData) ) {
                  /**
                  * Loop all array options
                  **/
                  foreach($this->aData AS $aElement) {
                        $objOption = $this->objDomDocument->createElement('option');
                        $objOption = $this->objSelect->appendChild($objOption);
                        $objOption->setAttribute('value',$aElement['value']);
      
                        $objOptionText = $this->objDomDocument->createTextNode($aElement['text']);
                        $objOptionText = $objOption->appendChild($objOptionText);                        
                  }
            } else {
                  throw new exception('select::getSelectedOption() -> No data given in object!');
            }
      }
      /**
      * Calls all his members to actually create his select box
      **/
      private function createSelectBox() {
            $this->setSelectElement();
            $this->setSelectOptions();
      }
      /**
      * Get the whole object with a tree construction to add this object to others
      **/
      public function getDomObject() {
            $this->createSelectBox();
            return $this->objDomDocument;
      }
      /**
      * Function which builds the select box and gives you back the HTML
      **/
      public function getHTML() {
            $this->createSelectBox();
            return $this->objDomDocument->saveHTML();
      }
}
?>