Location: PHPKode > projects > FiForms Framework > FiForms/FiForms-includes/FiForms_iDBSelect.inc.php
<?php
/**
*******************************************************************************

    FiForms -- A collection of PHP classes designed 
    to facilitate rapid development of web-database software

    Copyright (C) 2003-2008  Daniel McFeeters

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    The original author of this library can be contacted at the following 
    address:

    Daniel McFeeters
    182 Baker Rd.
    Faubush, KY 42544-6526
    email:databases [at] fiforms [dot] org
    http://www.fiforms.org/


iDBSelect class
Project Started May 4, 2003
*******************************************************************************
FiForms_iDBSelect.inc.php

iDBSelect Definition File

This file contains the definitions for the iDBSelect input class 

*******************************************************************************
**/

require_once("FiForms_iInput.inc.php");

/* ?><code><?php */

$FIFORM_XML_INPUTS[] = 'f:iDBSelect';

class iDBSelect extends iInput
//creates a data-bound select box
{
    public $rowQuery;     // SQL query to load rows from, or array of values. Not 
                          // necessary if using dynamic loading
    public $radioStyle;   // print all options as radio boxes.
    public $dynamic;      // use a dynamic select box, loading values by javascript
    public $displayField; // Name of field containing value to display. Boosts performance
                          // in sheet view and is necessary when using dynamic select.
    public $multiField;
    public $extendedAtt;  // Use 3rd database column for extentended attributes
    private $allSetUp;
    private $str_Start;
    private $str_End;
    private $str_Option;
    private $str_Selected;
    private $str_OptEnd;
    private $str_ValueStart;
    private $str_ValueEnd;
        
    function iDBSelect()
    {
        $this->depends = array();
        $this->rowReport = "";
        $this->radioStyle = false;
        $this->multiField = true;
        $this->displayField = false;
        $this->allSetUp = false;
        $this->extendedAtt = false;
        $dynamic = false;
        if(func_num_args() >= 3)
        {
            $this->rowQuery=func_get_arg(2);
            if(func_num_args() >= 4)
            {
                $this->radioStyle=func_get_arg(3);
            }    
        }
        $this->iInput(func_get_args());
        $this->ajaxLoad = true;
    }
        
    function setupStrings()
    {
        $this->allSetUp = TRUE;
        if(!$this->rowQuery)
        {
            $this->radioStyle = false;
        }
        if($this->radioStyle == TRUE)
        {
            $this->str_Start = "";
            $this->str_End = "";
            $this->str_Option = "<input type=\"radio\" name=\"".
                        $this->dbField."\" ";
            $this->str_Selected = "checked=\"checked\" ";
            $this->str_OptEnd = "<br />\n";
            $this->str_ValueStart = "value=\"";
            $this->str_ValueEnd = "\" />";
        } // if radio
        else if($this->dynamic == TRUE)
        {
            $this->icons = new iconset();
            $this->str_Start = 
                    "<span class=\"text\"><input type=\"hidden\" name=\"".$this->dbField."\" id=\"".$this->dbField."\" value=\"".$this->value."".
                        "\" ".($this->autoReload === "always" ? "class=\"always\" ": "")."/><input type=\"text\" name=\"".$this->dbField."_display\" id=\"".
                        $this->dbField."_display\" onblur=\"updateDynSelect('".$this->dbField."',true,true);\" class=\"no_border\" autocomplete=\"off\" onkeydown=\"return dynSelectNav(event,'$this->dbField');\" onkeyup=\"return dynSelectChange(event,'$this->dbField');\"".
                        " value=\"".$this->fieldList[$this->displayField]."\" ".$this->otherTags." /><span onclick=\"openDynSelect('$this->dbField',true);\">".
            $this->icons->down."</span></span><script type=\"text/javascript\">
fiformsDynSel[fiformsSelCount] = new Array();
fiformsDynSelObjects[fiformsSelCount] = new dropDown('$this->dbField');\n\n   fiformsSelCount++;\n</script>" .
            "<div id=\"$this->dbField"."_selectionarea\" class=\"selectionarea\" style=\"width: 400px; height: 150px; display: none;\">" .
                            "<table onkeyup=\"return dynSelectNav(event,'".$this->dbField."');\">";
                $this->str_End = "</table></div>".$this->getAJAXScript().$this->getRefreshLink();
            $this->str_Option = "<tr style=\"cursor:default;\" onmousedown=\"clickDynRow(this,'".$this->dbField."');\"><td ";
            $this->str_Selected = "id=\"".$this->dbField."_selected\" class=\"selected\" ";
                $this->str_OptEnd = "</td></tr>\n";
                $this->str_ValueStart = "title=\"";
                $this->str_ValueEnd = "\">";
                $this->allSetUp = false;
        } // if dynamic
        else
        {
            $this->str_Start = 
                    "<select name=\"".$this->dbField."\" id=\"".$this->dbField."\" class=\"text\"".
            $this->otherTags." >";
            $this->str_End = "</select>".$this->getAJAXScript().$this->getRefreshLink();
            $this->str_Option = "<option ";
            $this->str_Selected = "selected=\"selected\" ";
            $this->str_OptEnd = "</option>\n";
            $this->str_ValueStart = "value=\"";
            $this->str_ValueEnd = "\">";
        } // else select

    }  // function setupStrings
    
    function drawInput()
    {
                unset($rowArray);
                if(!$this->allSetUp)
                {
                    $this->setupStrings();
                }
        $output = $this->str_Start;
        $valueFound = FALSE;
        if($this->readOnly && $this->displayField !== false)
        {
            $valueFound = TRUE;
            if(!$this->multiField && $this->displayField == $this->dbField && $this->freshData)
            {
                $outputValue = $this->value;
                //echo "x";
            }
            else
            {

                $outputValue = $this->fieldList[$this->displayField];
                $this->dbField = $this->displayField;
                $this->multiField = false;
                $this->freshData = false;
            }
        } // if readonly and displayfield
        else
        {
            if(is_array($this->rowQuery)) // this is an array, so we'll take the value from here
            {
                $rowArray = $this->rowQuery;
                if(is_array($rowArray[0]))
                {
                    $twodim = TRUE;
                }
                else
                {
                    $twodim = FALSE;
                }  // if two dimensional
            } // if array
            elseif($this->rowQuery)  // rowQuery is an SQL query
            {
                // rowQuery must be an SQL string; fetch from database
                $params = $_GET;
                if(!get_magic_quotes_gpc())
                {
                    foreach($params as $key => $value)
                    {
                        $params[$key] = addslashes($value);
                    }
                }
                $this->rowQuery = fillVars($params,$this->rowQuery);
                $rowResult = @mysql_query($this->rowQuery);
                $i = 0;

                $thisLine = @mysql_fetch_array($rowResult);
                if($thisLine && array_key_exists(1,$thisLine))
                {
                    $twodim = TRUE;
                    if($this->extendedAtt)
                    {
                        $rowArray[$i] = array($thisLine[0],$thisLine[1],$thisLine[2]);
                    }
                    else
                    {
                        $rowArray[$i] = array($thisLine[0],$thisLine[1]);
                    }
                }
                else
                {
                    $twodim = FALSE;
                    $rowArray[$i] = $thisLine[0];
                }
                $i++;

                while($thisLine = @mysql_fetch_array($rowResult))
                {
                    if($twodim)
                    {
                        if($this->extendedAtt)
                        {
                            $rowArray[$i] = array($thisLine[0],$thisLine[1],$thisLine[2]);
                        }
                        else
                        {
                            $rowArray[$i] = array($thisLine[0],$thisLine[1]);
                        }
                    }
                    else
                    {
                        $rowArray[$i] = $thisLine[0];
                    }
                    $i++;
                } // while
            } // if is SQL
            else  // no rowQuery is available
            {
                $twodim = false;
                $rowArray = array();
            }
            if($twodim)
            {
                foreach($rowArray as $rowValue)
                {
                    $output .= $this->str_Option;
                    // Test if this option is the value of the field
                    if(($this->value == "_first" || $rowValue[0] == $this->value) && !$valueFound)
                    {
                        $output .= $this->str_Selected;
                        $valueFound = true;
                        $outputValue = $rowValue[1];
                    }
                    if($this->extendedAtt)
                    {
                        $output .= $rowValue[2];
                    }
                    $output .= $this->str_ValueStart.
                        preg_replace("/\&amp\;\#(\d+)\;/","&#\$1;",
                            htmlentities($rowValue[0])).
                        $this->str_ValueEnd.
                        preg_replace("/\&amp\;\#(\d+)\;/","&#\$1;",
                            htmlentities($rowValue[1])).
                    $this->str_OptEnd;
                } //foreach rowArray
            }  // if two dimensional
            else
            {
                foreach($rowArray as $rowValue)
                {
                    $output .= $this->str_Option;
                    // Test if this option is the value of the field
                    if(($this->value == "_first" || $rowValue == $this->value) && !$valueFound)
                    {
                        $output .= $this->str_Selected;
                        $valueFound = true;
                        $outputValue = $rowValue;
                    }
                    $output .= $this->str_ValueStart.$rowValue.$this->str_ValueEnd.$rowValue.$this->str_OptEnd;
                }  // foreach rowArray
            }  // if one dimensional
        } // if not readonly and displayfield

        // If no value in the last matches field value,
        // insert field value in list and select it.
        if(!$valueFound)
        {
            $output .= $this->str_Option.$this->str_Selected.
                    $this->str_ValueStart.$this->value.$this->str_ValueEnd.
                    $this->value.$this->str_OptEnd;
            $outputValue = $this->value;
        } // if valueFound
        $output .= $this->str_End;
        if($this->readOnly)
        {
            $output = $outputValue."&#160;";
        }
        //print_r($this);die();
        return($output);
    } // function drawInput


    function buildFromXML($input)
    {
        parent::buildFromXML($input);
        if(!$this->rowQuery)
        {
            $option = $input->firstChild;
            $optionArray = array();
            $i = 0;
            while($option)
            {
                if($option->attributes)
                {
                    $value = $option->attributes->getNamedItem('value');
                    if(is_object($value))
                    {
                        $value = $value->nodeValue;
                    }
                }
                $text = $option->nodeValue;
                if(substr($option->nodeName,-6) == "option")
                {
                $optionArray[$i][0] = $value ? $value : $text;
                $optionArray[$i][1] = $text;
                $i++;
                }
                $option = $option->nextSibling;
            }
            $this->rowQuery = $optionArray;
        }  // if iDBSelect
    } // function buildFromXML


} // class iDBSelect

/* ?></code><?php */


?>
Return current item: FiForms Framework