Location: PHPKode > scripts > DropDown.inc > dropdown-inc/FormElements.inc
<?php

include("Template.inc");


/* A generic drop down. */
class DropDown extends Template {

  //describe the drop down HTML
  var $selectOpen;
  var $selectTemplate;
  var $selectClose;

  //additional option that they might want to set
  var $leadingOptions;
  var $mainOptions;
  var $endingOptions;

  //some constants for nice interface
  var $AT_FRONT;
  var $AT_BACK;


  function DropDown($name, $size = 1, $class = "", $otherOptions = ""){

    //initialize the member vars
    $this->AT_FRONT = 1;
    $this->AT_BACK  = 2;
    $this->setDelimiter("#");

    $this->selectOpen      = "\n<!-- Generated automatically by DropDown class -->\n";
    $this->selectOpen     .= "<select name=\"$name\" size=\"$size\" class=\"$class\" $otherOptions>\n";
    $this->selectTemplate  = "   <option value=\"#f1#\">#f2#</option>\n";
    $this->selectClose     = "</select>\n";

    $this->Template( $this->selectTemplate, false );

  }//constructor



  /* Get the contents without printing them */
  function toString(){

    $returnString = "";

    if( !empty($this->leadingOptions) )
      $returnString .= $this->convertHash($this->leadingOptions);

    $returnString .= $this->mainOptions;

    if( !empty($this->endingOptions) )
      $returnString .= $this->convertHash($this->endingOptions);

    return ($this->selectOpen . $returnString . $this->selectClose);

  }//method toString



  /* Print out contents as they stands now */
  function printContents(){
    print "" . $this->toString();
  }//method printContents



  /* Take the input SQLCall object and templatize the output */
  function addFromQuery($query){
    $this->mainOptions .= $this->processQuery( $query );
  }//method fromQuery



  /* Take the input hash object and templatize it's contents */
  function addFromHash($hash){

    if( !empty($hash) )
      $this->mainOptions .= $this->processHash( $hash );

  }//method fromHash



  /* add an option to the list of options */
  function addOption($value, $displayName, $position){
 
    if( $position == $this->AT_FRONT )
      $this->leadingOptions[$value] = $displayName;
    else if ( $position == $this->AT_BACK )
      $this->endingOptions[$value] = $displayName;

  }//method addOption



  /* Utility method - convert the input hashes to a structure that is usable */
  function convertHash($hash){

    $returnString = "";

    //the hash should be field1 = 1st option, field2 = 2nd option
    while( list($name, $value) = each($hash) ){
      $fields["f1"] = $name;
      $fields["f2"] = $value;
      $returnString .= $this->processHash( $fields );
    }//while

    return $returnString;

  }//method convertHash


}//class DropDown


?>
Return current item: DropDown.inc