<?php

/**
 * DBSelectForm.class.php
 * Clark Freifeld 31 Mar 06
 * Generate a select form element from a db query
 */

require_once dirname(__FILE__) . '/db.function.php';
//require_once 'Log.php';

class DBSelectForm
{
    var $sel_head;
    var $sel;
    var $sel_foot;
    var $css;

    function __construct($table, $query, $att = array(), $qparams = array()) {
        //$log = Log::factory('file', '/tmp/dbsel.log', 'CONS');
        if(empty($val_field)) {
            $val_field = "${table}_id";
        }
        $db = getDB();
        $res = $db->getAssoc($query, FALSE, $qparams);
        if(PEAR::isError($res)) {
            //$log->log($res->getMessage());
            return $res;
        }
        $this->opts = $res;
        //$this->insertElt(0, '[Choose...]');


        // loop through the tag attributes and add them to select tag
        // provide some default values for the id and name attributes if they aren't passed in
        $this->att = $att;
        if(isset($this->att['id']) && !isset($this->att['name'])) {
            $this->att['name'] = $this->att['id'];
        }
        if(isset($this->att['name']) && !isset($this->att['id'])) {
            $this->att['id'] = $this->att['name'];
        }
        if(!isset($this->att['id'])) {
            $this->att['id'] = "${table}_id";
        }
        if(!isset($this->att['name'])) {
            $this->att['name'] = "${table}_id";
        }
        $this->sel_head = "<select";
        foreach($this->att as $attkey => $attval) {
            $this->sel_head .= " $attkey=\"$attval\"";
        }
        $this->sel_head .= " >\n";
        $this->sel_foot = "</select>\n";
        $this->selected_val = null;
        $this->frozen = false;
    }

    function setAttr($key, $value) {
        $this->att[$key] = $value;
        $this->sel_head = "<select";
        foreach($this->att as $attkey => $attval) {
            $this->sel_head .= " $attkey=\"$attval\"";
        }
        $this->sel_head .= " >\n";
    }
    function getAttr($key) {
        return $this->att[$key];
    }

    function setFrozen($stat) {
        $this->frozen = $stat;
    }

    function setValue($val) {
        if(!isset($this->selected_val) || $val != $this->selected_val) {
            $this->selected_val = $val;
            $this->sel = '';
        }
    }

    function optValExists($val) {
        return isset($this->opts[$val]);
    }

    function insertElt($val, $label) {
        //$this->opts = array_merge(array($val => $label), $this->opts);
        $this->opts = array($val => $label) + $this->opts;
        $this->sel = '';
    }

    function appendElt($val, $label) {
        //$this->opts = array_merge($this->opts, array($val => $label));
        $this->opts += array($val => $label);
        $this->sel = '';
    }

    function getValue($i = null) {
        if(!is_null($this->selected_val)) {
            return $this->selected_val;
        } else {
            //list($val, $label) = each($this->opts);
            //reset($this->opts);
            foreach($this->opts as $val => $label) {
                return $val;
            }
        }
        //$j = 0;
        //foreach($this->opts as $val => $label) {
        //    if($j == $i) {
        //        return $val;
        //    }
        //}
    }

    function printSelect() {
        print $this->getSelect();
    }

    function setSelectable($arr) {
        $this->selectable = $arr;
    }

    function getSelect() {
        if($this->frozen) {
            ($this->opts[$this->selected_val] == 'Other') ? $colon = ':' : $colon = '';
            return '<span class="frozentext">' . $this->opts[$this->selected_val] . "$colon</span> ";
        }
        if(!$this->sel) {
            foreach($this->opts as $val => $label) {
                $attr = '';
                if(isset($this->selectable) && (!isset($this->selectable[$val]) || !$this->selectable[$val])) {
                    $label = "--- $label ---";
                    $attr = 'disabled="disabled"';
                }
                $selected = '';
                if($val == $this->selected_val) {
                    $selected = ' selected="selected"';
                }
                $this->sel .= sprintf('<option %s class="%s_opt" value="%s"%s>%s</option>' . "\n", $attr, $this->att['id'], $val, $selected, $label);
            }
        }
        return $this->sel_head . $this->sel . $this->sel_foot;
    }
}

?>
