Scripts

HTMLSelect Class

Misschien lijkt het je op het eerste gezicht deze klasse overbodig, echter als je dieper in het OOP verhaal duikt, en objecten gaat maken die dus verschillende HTML elementen genereert kun je hiermee zeker weten de voorbeelden eruit halen. Voorbeeld: Stel je hebt een form klasse met deze klasse kun je een formulier bouwen. Men kan form "questions" toevoegen door bijvoorbeeld $form->addSelect() te doen. De form klasse instantieert intern een select object en voegt hem toe aan zijn tree met "questions". Ik ga ook meerder van deze klasse bouwen alleen moet ik kijken welke ik onder de open source norm zet. Deze klasse is wel open source, en mag je dus gratis gebruiken! voordelen - Fout tolerante HTML genereren - Laat objecten het HTML werk doen - Netter voor je code - Gemakkelijk en snel een select box maken uitbreiding Je zou deze klasse kunnen uitbreiden door aan de magic __Set functie een aantal property's kunnen toevoegen. Ik heb de meest gebruikte toegevoegd. Gebruik Redelijk simpel:

htmlselect-class
<?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();
      }
}
?>

Reacties

0
Nog geen reacties.