1 |
<?php |
2 |
/** |
3 |
* This file contains the FEListBoxMaster |
4 |
* |
5 |
* $Id: FEListBoxMaster.inc,v 1.3 2004/04/01 02:32:08 sumedh_thakar Exp $ |
6 |
* |
7 |
* @author Sumedh Thakar <sumedh_thakar@yahoo.com> |
8 |
* @package phpHtmlLib |
9 |
* @subpackage FormProcessing |
10 |
* |
11 |
* @copyright LGPL - See LICENCE |
12 |
* |
13 |
*/ |
14 |
|
15 |
/** |
16 |
* This element builds the master datalist that will control one slave datalist |
17 |
* to change the value of the slave datalist when master changes in JS. It will also set |
18 |
* the valid list for the slave depending on the value of the master. |
19 |
* |
20 |
* Please use regular FEListBox element for non-master lists. |
21 |
* |
22 |
* please examples area. |
23 |
* |
24 |
* @author Sumedh Thakar |
25 |
*/ |
26 |
class FEListBoxMaster extends FEListBox { |
27 |
|
28 |
var $mdata = array(); |
29 |
/** |
30 |
* This function sets the array of data |
31 |
* to be used in the data list |
32 |
* |
33 |
* @param array data_list - list of data elements (name=>value) |
34 |
*/ |
35 |
function set_list_data($data) { |
36 |
|
37 |
$this->_mdata = $data; |
38 |
|
39 |
$tmp = array(); |
40 |
foreach($this->_mdata as $master => $rec){ |
41 |
$tmp[$master] = isset($rec["code"])? $rec["code"] : $master; |
42 |
} |
43 |
|
44 |
$this->_data_list = $tmp; |
45 |
} |
46 |
|
47 |
/** |
48 |
* This function will set the |
49 |
* data the parent wants to set for the slave |
50 |
* |
51 |
* @param the slave |
52 |
*/ |
53 |
function set_slave_data(&$slave,$value) { |
54 |
if (isset($value) && isset($this->_mdata[$value]["slave"])) { |
55 |
$slave->set_list_data($this->_mdata[$value]["slave"]); |
56 |
}else{ |
57 |
$slave->set_list_data(array("None"=>"None")); |
58 |
} |
59 |
} |
60 |
|
61 |
/** |
62 |
* This method is used for adding any javascript |
63 |
* that is used by this element. This will automatically |
64 |
* get called and added to the page by the FormProcessor |
65 |
* |
66 |
* @return string - raw js |
67 |
*/ |
68 |
function javascript() { |
69 |
$slaves = $this->get_slave_elements(); |
70 |
$master_name = $this->get_element_name(); |
71 |
$slave = $slaves[0]; |
72 |
$slave_name = $slave->get_element_name(); |
73 |
$gFlagName = $master_name."_".$slave_name."_defined"; |
74 |
|
75 |
|
76 |
global $$gFlagName; |
77 |
|
78 |
if (!isset($$gFlagName)) { |
79 |
$$gFlagName = true; |
80 |
//Create the JS array that will be used to populate the slave when master value changes |
81 |
$js_array = "slave_".$slave_name." = new Array();\n"; |
82 |
foreach($this->_mdata as $master => $rec){ |
83 |
if(isset($rec['slave']) && is_array($rec['slave'])){ |
84 |
$js_array .= "slave_".$slave_name."[\"".$master."\"] = new Array();\n"; |
85 |
foreach($rec['slave'] as $slave => $slave_rec){ |
86 |
$js_array .= "slave_".$slave_name."[\"".$master."\"][\"".$slave_rec."\"] = '".$slave."';\n"; |
87 |
} |
88 |
} |
89 |
} |
90 |
}else{ |
91 |
$js_array = ""; |
92 |
} |
93 |
|
94 |
global $FEListBoxMaster_js; |
95 |
|
96 |
if (!isset($FEListBoxMaster_js)) { |
97 |
|
98 |
$FEListBoxMaster_js = true; |
99 |
|
100 |
$js_str = <<<JSEND |
101 |
function change_slave(master, slave, data) { |
102 |
_name = master; |
103 |
len = document.forms[0].elements[_name].length; |
104 |
for (i=0;i<len;i++) |
105 |
if (document.forms[0].elements[_name].options[i].selected) |
106 |
break; |
107 |
val = document.forms[0].elements[_name].options[i].value; |
108 |
|
109 |
_name = slave; |
110 |
i=0; |
111 |
if (data[val] == null) { |
112 |
nn = new Option("None"); |
113 |
document.forms[0].elements[_name].options[0] = nn; |
114 |
document.forms[0].elements[_name].options[0].value = "None"; |
115 |
i++; |
116 |
} |
117 |
else { |
118 |
for (var n in data[val]) { |
119 |
nn = new Option(data[val][n]); |
120 |
document.forms[0].elements[_name].options[i] = nn; |
121 |
document.forms[0].elements[_name].options[i].value = n; |
122 |
i++; |
123 |
} |
124 |
} |
125 |
|
126 |
len = document.forms[0].elements[_name].length; |
127 |
for (j=i;j<len;j++) |
128 |
document.forms[0].elements[_name].options[i] = null; |
129 |
} |
130 |
|
131 |
JSEND; |
132 |
|
133 |
}else{ |
134 |
$js_str = ""; |
135 |
} |
136 |
return $js_array.$js_str; |
137 |
} |
138 |
/** |
139 |
* this function retuns the javaScript code for |
140 |
* an onChange event |
141 |
* |
142 |
* @return string - javascript code |
143 |
*/ |
144 |
function onChange() { |
145 |
$master_name = $this->get_element_name(); |
146 |
$slaves = $this->get_slave_elements(); |
147 |
$slave = $slaves[0]; |
148 |
$slave_name = $slave->get_element_name(); |
149 |
|
150 |
return "change_slave('".$master_name."','".$slave_name."',slave_".$slave_name.")"; |
151 |
} |
152 |
|
153 |
|
154 |
} |
155 |
?> |