1 |
jonen |
1.1 |
<?php |
2 |
|
|
/** |
3 |
|
|
* This file contains the FEListBoxMaster |
4 |
|
|
* |
5 |
jonen |
1.2 |
* $Id: FEListBoxMaster.inc,v 1.5 2004/06/04 20:08:58 hemna Exp $ |
6 |
jonen |
1.1 |
* |
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 |
jonen |
1.2 |
function set_slave_data(&$slave, $value) { |
54 |
|
|
|
55 |
|
|
$flipped_array = array_flip($this->_data_list); |
56 |
|
|
if (isset($flipped_array[$value])) $value = $flipped_array[$value]; |
57 |
|
|
|
58 |
jonen |
1.1 |
if (isset($value) && isset($this->_mdata[$value]["slave"])) { |
59 |
|
|
$slave->set_list_data($this->_mdata[$value]["slave"]); |
60 |
|
|
}else{ |
61 |
|
|
$slave->set_list_data(array("None"=>"None")); |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* This method is used for adding any javascript |
67 |
|
|
* that is used by this element. This will automatically |
68 |
|
|
* get called and added to the page by the FormProcessor |
69 |
|
|
* |
70 |
|
|
* @return string - raw js |
71 |
|
|
*/ |
72 |
|
|
function javascript() { |
73 |
|
|
$slaves = $this->get_slave_elements(); |
74 |
|
|
$master_name = $this->get_element_name(); |
75 |
|
|
$slave = $slaves[0]; |
76 |
|
|
$slave_name = $slave->get_element_name(); |
77 |
|
|
$gFlagName = $master_name."_".$slave_name."_defined"; |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
global $$gFlagName; |
81 |
|
|
|
82 |
|
|
if (!isset($$gFlagName)) { |
83 |
|
|
$$gFlagName = true; |
84 |
|
|
//Create the JS array that will be used to populate the slave when master value changes |
85 |
|
|
$js_array = "slave_".$slave_name." = new Array();\n"; |
86 |
|
|
foreach($this->_mdata as $master => $rec){ |
87 |
jonen |
1.2 |
|
88 |
|
|
if(isset($rec['slave']) && is_array($rec['slave'])){ |
89 |
|
|
$js_array .= "slave_".$slave_name."[\"".$rec['code']."\"] = new Array();\n"; |
90 |
jonen |
1.1 |
foreach($rec['slave'] as $slave => $slave_rec){ |
91 |
jonen |
1.2 |
$js_array .= "slave_".$slave_name."[\"".$rec['code']."\"][\"".$slave_rec."\"] = '".$slave."';\n"; |
92 |
jonen |
1.1 |
} |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
}else{ |
96 |
|
|
$js_array = ""; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
global $FEListBoxMaster_js; |
100 |
|
|
|
101 |
|
|
if (!isset($FEListBoxMaster_js)) { |
102 |
|
|
|
103 |
|
|
$FEListBoxMaster_js = true; |
104 |
|
|
|
105 |
|
|
$js_str = <<<JSEND |
106 |
|
|
function change_slave(master, slave, data) { |
107 |
|
|
_name = master; |
108 |
|
|
len = document.forms[0].elements[_name].length; |
109 |
|
|
for (i=0;i<len;i++) |
110 |
|
|
if (document.forms[0].elements[_name].options[i].selected) |
111 |
|
|
break; |
112 |
|
|
val = document.forms[0].elements[_name].options[i].value; |
113 |
|
|
|
114 |
|
|
_name = slave; |
115 |
|
|
i=0; |
116 |
|
|
if (data[val] == null) { |
117 |
|
|
nn = new Option("None"); |
118 |
|
|
document.forms[0].elements[_name].options[0] = nn; |
119 |
|
|
document.forms[0].elements[_name].options[0].value = "None"; |
120 |
|
|
i++; |
121 |
|
|
} |
122 |
|
|
else { |
123 |
|
|
for (var n in data[val]) { |
124 |
|
|
nn = new Option(data[val][n]); |
125 |
|
|
document.forms[0].elements[_name].options[i] = nn; |
126 |
|
|
document.forms[0].elements[_name].options[i].value = n; |
127 |
|
|
i++; |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
len = document.forms[0].elements[_name].length; |
132 |
|
|
for (j=i;j<len;j++) |
133 |
|
|
document.forms[0].elements[_name].options[i] = null; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
JSEND; |
137 |
|
|
|
138 |
|
|
}else{ |
139 |
|
|
$js_str = ""; |
140 |
|
|
} |
141 |
|
|
return $js_array.$js_str; |
142 |
|
|
} |
143 |
|
|
/** |
144 |
|
|
* this function retuns the javaScript code for |
145 |
|
|
* an onChange event |
146 |
|
|
* |
147 |
|
|
* @return string - javascript code |
148 |
|
|
*/ |
149 |
|
|
function onChange() { |
150 |
|
|
$master_name = $this->get_element_name(); |
151 |
|
|
$slaves = $this->get_slave_elements(); |
152 |
|
|
$slave = $slaves[0]; |
153 |
|
|
$slave_name = $slave->get_element_name(); |
154 |
|
|
|
155 |
|
|
return "change_slave('".$master_name."','".$slave_name."',slave_".$slave_name.")"; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
|
|
} |
160 |
|
|
?> |