Location: PHPKode > projects > GuanxiCRM Business Solution > include/mSelectBox.class.inc
<?php
/* $Id: mSelectBox.class.inc,v 1.16 2002/10/16 17:12:13 kfish Exp $ */

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.16 $", __FILE__);

/**
* class to build SELECT-Boxes
* for PHP 4.0 and later
*
* @param $varname (name of the field in the formular)
* @access public
* @author Marc Logemann <hide@address.com>
* @author Michael Ochtrop <hide@address.com>
* @author Dirk Hartmann <hide@address.com>
* @version 1.1 2000-10-18
*
*/
class mSelectBox{
    var $valueARR;
    var $contentARR;
    var $SB_multiple = false;
    var $ctype;
    var $htype;
    var $hvalue;

    /**
     * class-constructor
     *
     * @access public
     * @param [FieldName]
     * @param [SelectedField]
     *
     */
    function mSelectBox($varname){
	$this->varname = $varname;
	if(func_num_args()>1) $this->SelectedValue = func_get_arg(1);
    }


    /**
     * to change the name of the element
     *
     * @access public
     * @param [FieldName]
     *
     */	
    function setVarname($varname){
	$this->varname = $varname;
    }


    /**
     * methods to change the design of the SelectBox
     *
     * @access public
     *
     */
    function setMultiple(){
	$this->SB_multiple = true;
    }

    /**
     * @param [Height]
     */
    function setHeight($height){
	$this->SB_height = $height;
    }

    /**
     * assigns a handler to the selectlist
     *
     * for example:
     *		setHandler("onChange","submitit()")
     *		will result in
     *		<select name="name" onChange="submitit")
     *
     * @access public
     * @param handlertype
     * @param handlervalue
     */
    function setHandler($htype, $hvalue){
	$this->htype = $htype;
	$this->hvalue = $hvalue;
    }

    /**
     * build the ValueArray of the keys from the ContentArray
     *
     * @access private
     *
     */
    function convertArrays(){
	$this->valueARR = array_keys($this->contentARR);
    }


    /**
     * methods to sort the SelectBox-Arrays
     *
     * @access public
     *
     */
    function sortbyValue(){
	ksort($this->contentARR);
	$this->convertArrays();
    }
    function sortbyContent(){
	asort($this->contentARR);
	$this->convertArrays();
    }


    /**
     * add an Option to the SelectBox-Arrays
     *  If the value exists in the array only
     *  the content will be changed, not the
     *  position.
     *
     * @access public
     * @param [value]
     * @param [option]
     * @param [position] pre / after
     *
     */
    function addOption($value,$option,$pos="after"){
	if(isset($this->contentARR[$value])){
	    $this->contentARR[$value] = $option;
	}
	else{
	    if($pos=="pre") $new_contentARR[$value] = $option;

            if (isset($this->contentARR)) {
		$contentARR = $this->contentARR;
	    } 
			
	    // if content is in the array, means, if someone called addOption oder
	    // addContent before
	    if(isset ($contentARR) && count($contentARR)>0) $valueARR = array_keys($contentARR);
 
            if (isset ($valueARR)) {
		for($a=0;$a<count($valueARR);$a++){
		    $new_contentARR[$valueARR[$a]] = $contentARR[$valueARR[$a]];
		}
	    }
	    if($pos=="after") $new_contentARR[$value] = $option;

	    $this->contentARR = $new_contentARR;
	    $this->convertArrays();
	}
    }


    /**
     * set the kind of the content
     *
     *	arr	->	array
     *	num	->	nummeric
     *	chr	->	character
     *	sql	->	sql-statement
     *	
     *
     * @access public
     *
     */
    function setContentType($type){
	$this->ctype = $type;
    }


    /**
     * set the value-fields of SQL-Statement
     *  if you want to take only one field from DB as value
     *  then its okay if you give these one Parameter,
     *  but if you take more then one, add a seperator on
     *  the first position
     * 
     * @access public
     *
     */
    function setSQLvalue($a){
	$num = func_num_args();
	if($num>1){
	    $this->sqlvaldev = $a;
	    for($a=1;$a<$num;$a++){
		$this->sqlval[$a-1] = func_get_arg($a);
	    }
	}
	else{
	    $this->sqlvaldev = "";
	    $this->sqlval[0] = $a;
	}
    }

    /**
     * set the content-fields of SQL-Statement
     *  the same like setSQLvalues();
     *
     * @access public
     *
     */
    function setSQLcontent($a){
	$num = func_num_args();
	if($num>1){
	    $this->sqloptdev = $a;
	    for($a=1;$a<$num;$a++)
		{
		    $this->sqlopt[$a-1] = func_get_arg($a);
		}
	}
	else{
	    $this->sqloptdev = "";
	    $this->sqlopt[0] = $a;
	}
    }


    /**
     * this method fills the Arrays for the SelectBox by detecting the kind of content
     *
     * how to use
     *	in a string followed by Arguments devided by blanks
     *
     *	spez:	arr (only an array) without any Arguments
     *
     *	spez:	"<num1> <num2>"
     *		"0 21"	-> from "00" to "21"
     *		"9 1"	-> from "9" downto "1"
     *
     *	spez:	"<char1> <char2>"
     *		"chr A C"	-> from "A" to "Z"
     *		"chr #90 #65"	-> from "Z" to "A"
     *		"chr #65 C"	-> from "A" to "C"
     *
     *	spez:	"<ValueField> <ContentField> <SQL for DB>"
     *		"id nachname select * from kundenstamm"
     *			-> all names of the table kundenstamm with the id of the entry as the value
     *
     * @access public
     * @param $ctype
     * @param $cont
     *
     */
    function addContent($cont){
	global $conn;
	if(!is_array($cont)){
	    $beg = strtok($cont," ");
	    $end = strtok(" ");
	    $aschar = false;
	}
	switch(strtoupper($this->ctype)){
	case "ARR":
	    $this->contentARR = $cont;
	    break;
	case "CHR":
	    $aschar = true;
	    if($beg[0]=="#"){ $beg = substr($beg,1); }else{ $beg = ord($beg[0]); }
	    if($end[0]=="#"){ $end = substr($end,1); }else{ $end = ord($end[0]); }			
	case "NUM":
	    if($beg<$end){
		$lr = "up";
		$sz = strlen($end);
	    }
	    else{
		$lr = "down";
		$sz = strlen($beg);
	    }
	    $a = $beg;
	    while( ($lr=="up" && $a<=$end) || ($lr=="down" && $a>=$end) ){
		$akt = sprintf("%0".$sz."d",$a);
		if($aschar==true){
		    $akt = chr($akt);
		}
		$this->contentARR[$akt] = $akt;
		if($lr=="up"){
		    $a+= 1;
		}
		else{
		    $a-= 1;
		}
	    }
	    break;
	case "SQL":
	    $sql = $cont;
	    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	    $num = $res->RowCount();
	    if($num>0){
		while($arr = $res->FetchRow()){
		    $val = "";
		    for($a=0;$a<count($this->sqlval);$a++){
			$val.= $arr[$this->sqlval[$a]];
			if($a<count($this->sqlval)-1) $val.= $this->sqlvaldev;
		    }
		    $opt = "";
		    for($a=0;$a<count($this->sqlopt);$a++){
			$opt.= $arr[$this->sqlopt[$a]];
			if($a<count($this->sqlopt)-1) $opt.= $this->sqloptdev;
		    }
		    $this->contentARR[$val] = $opt;
		}
	    }
	    else{
		$this->contentARR[] = "NoEntryInTable";
	    }
	    break;
	default:
	    die("Define a Type before!!!");
	}
	$this->convertArrays();
    }


    /**
     * clear the SelectBox-Arrays and set the new content
     *
     * @access public
     *
     */
    function setContent($cont){
	$this->contentARR = "";
	$this->valueARR = "";
	$this->addContent($cont);
    }


    /**
     * this the Method to get the output the SelectBox
     *
     * @access public
     * @return html-construct of the SelectBox
     *
     */
    function draw(){
	if(func_num_args()>0){
	    $this->SelectedValue = func_get_arg(0);
	}
	$extra = (func_num_args()>1) ? " ".func_get_arg(1): '';

	$strg = "\n<SELECT NAME=\"".$this->varname;
	if($this->SB_multiple==true) $strg.= "[]\" multiple";
	else $strg .= "\"";
	if(isset($this->SB_height)) $strg.= " SIZE=\"".$this->SB_height."\"";
	if(isset($this->htype) and isset($this->hvalue)) $strg .= " ". $this->htype . "=\"" . $this->hvalue. "\"";
	$strg.= $extra.">\n";
	for($a=0;$a<count($this->valueARR);$a++){
	    $strg.= "<OPTION";
	    $strg.= " VALUE=\"".$this->valueARR[$a]."\"";

	    if( isset ($this->SelectedValue) && $this->valueARR[$a] == $this->SelectedValue){
		$strg.= " selected";
	    }
	    $strg.= ">";
	    $strg.= $this->contentARR[$this->valueARR[$a]];
	    $strg.= "</OPTION>\n";
	}
	$strg.= "</SELECT>\n";
	return $strg;
    }



    /**
     * some pre-defined Methods of SelectBoxes
     *
     * @access public
     *
     */
    function getSB_Day($aktDay=0, $emptyfirst=true){
	$this->SB_multiple = false;
	$this->setContentType("num");
	$this->setContent("1 31");
	if($emptyfirst) $this->addOption("","", "pre");
	return $this->draw($aktDay);
    }
    
    function getSB_Month($aktMonth=0, $emptyfirst=true){
	$this->SB_multiple = false;
	$this->setContentType("num");
	$this->setContent("1 12");
	if($emptyfirst) $this->addOption("","", "pre");
	return $this->draw($aktMonth);
    }
	
    // ==============================================
    // optionaler 3. Parameter ist ausgewaehltes Jahr
    // ==============================================
    function getSB_Year($von,$bis, $aktYear=0, $emptyfirst=true){
	$this->SB_multiple = false;
	$this->setContentType("num");
	$this->setContent($von." ".$bis);
	if($emptyfirst) $this->addOption("","", "pre");
	
	return $this->draw($aktYear);
    }
    
    function getSB_Title(){
	global $hashmap;
		
	$this->SB_multiple = false;
	if(func_num_args()>0) $aktTitle = func_get_arg(0);

	// we use keys for value (pre-defined like mr, mrs)
	$arr["mr"] = getLanguageString("mr");
	$arr["mrs"] = getLanguageString("mrs");
	$arr["ms"] = getLanguageString("ms");
	$arr["miss"] = getLanguageString("miss");
	$arr["dr"] = getLanguageString("dr");
	$arr["sir"] = getLanguageString("sir");
	$arr["lady"] = getLanguageString("lady");
	$arr["family"] = getLanguageString("family");

	$this->setContentType("arr");
	$this->setContent($arr);
	return $this->draw($aktTitle);
    }
    
    function getSB_CCard(){
	$this->SB_multiple = false;
	if(func_num_args()>0) $aktCard = func_get_arg(0);

	/*	Schl├╝sselwerte als Zahlen, oder so wie hier?	*/
	$arr["v"] = "VISA";
	$arr["m"] = "MasterCard";

	$this->setContentType("arr");
	$this->setContent($arr);
	return $this->draw($aktCard);
    }
}

?>
Return current item: GuanxiCRM Business Solution