"animals", 2 => "birds");
// $list = array ( 1 => array ( 1 =>"tiger", 2 => "lion"), 2 => array ( 3 => "chiken", 4=>"duc"));
//
// $combo = new DoubleCombo("ComboTest", $class, $list);
//
// $combo->create_js();
// // start
class DoubleCombo {
var $base_list; // array with list data.
var $sub_list;
var $name; // DoubleCombo name ; used to handle multiple DoubleCombo in one page.
var $base_opts; // base select options
var $debug_level = 0; // trigger debug output ; set by $this->debug_level($level);
// -------------------------------- Constructor -------------------------------------------
function DoubleCombo($name, $base_list, $sub_list){
$this->name = $name;
$this->base_list = $base_list;
$this->sub_list = $sub_list;
}
// -------------------------------- public methods -------------------------------------------
function create_js()
{
echo '\n\n";
}
function create_base_select($name, $selected_index = 0, $options = 0){
$this->base_opts["name"] = $name;
$this->base_opts["selected_index"] = $selected_index;
$this->base_opts["options"] = &$options;
}
function create_sub_select($name, $selected_index = 0, $options = 0){
// base select / take care of selected index.
// class_changed javascript callback is linked to (this, sub_select, and a reference to data (combo_data_.DoubleCombo->name())
//
echo "\n" ;
// second select ( sub_select)
echo "" ;
}
// -------------------------------- errors and debug -------------------------------------------
function debug_level($level){
$this->debug_level = $level;
}
function debug($level, $msg)
{
if ($level >= $this->debug_level) {
echo "
DEBUG: DoubleCombo({$this->name}) -> " . $msg . "
" ;
}
}
function error($msg, $line)
{
$this->debug(0, $smg . " line : " . $line );
exit(1);
}
// -------------------------------- Private Methods -------------------------------------------
// -------------------------------- Javascript generation -------------------------------------------
function create_js_arrays(){
// base_list
echo "// DoubleCombo : base list\n";
$this->array_to_js($this->base_list, "combo_base_" . $this->name);
// sub_list
echo "// DoubleCombo : sub list (linked)\n";
$this->array_to_js($this->sub_list, "combo_data_" . $this->name);
}
function array_to_js(&$arr, $varname) {
if(is_array($arr)) {
printf("%s = ", $varname);
$this->array_to_js_recursive(&$arr, $varname, 0);
printf(";\n\n");
}
else
$this->error("array_to_js : invalide argument", __LINE__);
}
// partial.
function array_to_js_recursive(&$arr, $level = 0) {
$space = " ";
$indent = "$space";
for($i=0 ; $i < $level ; $i++)
$ident = $indent."$space";
if($level == 0)
printf ("new Array(\n", $indent);
else
printf ("%snew Array(\n", $indent);
$count = 0;
foreach($arr as $key=>$cell) {
if($count != 0)
printf(",\n");
$count++;
if(is_array($cell)) {
$this->array_to_js_recursive(&$cell, $level+1);
}
else
printf ("%s%s%s, %s",$indent, $space, $key, $this->convertVar($cell));
}
printf ("\n%s)", $indent);
}
function convertVar($var) {
switch (gettype($var)){
case 'boolean':
return $var?'true':'false';
break;
case 'string':
return ('"' . $this->escapeString($var) . '"');
break;
case 'integer':
case 'double':
return $var;
break;
default:
$this->error("convertVar() : unkown type", __LINE__);
break;
}
}
// {{{ escapeString
/**
* Used to terminate escape characters in strings, as javascript doesn't allow them
*
* @param string $str the string to be processed
* @return mixed the processed string
* @access public
*/
function escapeString($str)
{
$js_escape = array(
"\r" => '\r',
"\n" => '\n',
"\t" => '\t',
"'" => "\\'",
'"' => '\"',
'\\' => '\\\\'
);
return strtr($str,$js_escape);
}// }}} escapeString
// -------------------------------- Javascript portability functions and common --------------------------------
function browser_check()
{
// Determines the client browser software... it is not a full check
// but a base check (too bad for IE 4.x and NS 3.x users...) :((
// user agent
$ua = $_SERVER["HTTP_USER_AGENT"];
$browser = "";
if (strstr($_SERVER["HTTP_USER_AGENT"],"MSIE"))
$browser = "ie" ;
else {
$browser = "ns" ;
if (strstr($GLOBALS["HTTP_USER_AGENT"],"Mozilla/5.0"))
$browser = $browser . "5" ;
else
$browser = $browser . "4" ;
}
$this->debug(5, "browser_check() = $ua - $browser");
return $browser;
}
// -------------------------------------------------------------------------------------------------
function js_common()
{
echo <<0;i--)
{
frm.elements[sel].remove(i) ;
}
}
}
END;
} // function js_NS5_funcs()
// -------------------------------------------------------------------------------------------------
function js_IE_funcs()
{
echo <<0;i--)
{
frm.elements(sel).remove(i) ;
}
}
}
END;
} // js_IE_funcs()
// -------------------------------- end class DoubleCombo -------------------------------------------
} // class DoubleCombo
?>