Initial import.
[siap.git] / manager / core / form / fields / FieldTree.php
CommitLineData
696f20d5
MS
1<?
2class FieldTree extends Field {
3 var $menu;
4 var $args = array();
5 var $arr_pais = array();
6 /* 0 - idcategoria (campo a ser salvo na tabela que ele se encontra)
7 * 1 - label (label do campo e name do hidden)
8 * 2 - id (campo a ser buscado na tabela)
9 * 3 - idPai (campo que se relaciona com o id)
10 * 4 - nome (campo que será mostrado nos outros combos)
11 * 5 - tabela (tabela)
12 */
13
14 function FieldTree($tmp_params) {
15 parent::Field("items", $tmp_params[0]);
16
17 $this->testRequiredType(array("varchar", "int"));
18
19 $this->label = $tmp_params[1];
20 $this->setInputType("select");
21 $this->args = array($tmp_params[2],$tmp_params[3],$tmp_params[4],$tmp_params[5]);
22 }
23
24 function setInputType($tmp_type) {
25 switch ($tmp_type) {
26 default:
27 case "select":
28 $this->input_type = "select";
29 $this->validation = "CMB";
30
31 break;
32 }
33 }
34
35 //private functions
36 function getInput() {
37 $value = $this->getValue();
38
39 switch ($this->input_type) {
40 default:
41 case "select":
42 /* 0 - id (campo a ser buscado na tabela)
43 * 1 - idPai (campo que se relaciona com o id)
44 * 2 - nome (campo que será mostrado nos outros combos)
45 * 3 - tabela (tabela)
46 */
47 //print_r2($this->$args);
48 $args_line = implode(",",$this->args);
49 $html = "<input type='hidden' id='".$this->name."_id' name='".$this->name."' value='".$value."'>".LF;//hidden que o sistema "verá"
50 $html .= "<input type='hidden' id='".$this->label."_filhos' name='".$this->label."_filhos' value=''>".LF;//hidden com os filhos separados por "!!!"
51 $html .= "<input type='hidden' id='".$this->label."_linha_pais' name='".$this->label."_linha_pais' value='$ARR_PAIS$'>".LF;
52 $html .= "<input type='hidden' id='".$this->label."_args' name='".$this->label."_args' value='".$args_line."'>".LF;//hidden com os argumentos para se utilizar na rotina de atualização
53 $html .= "".LF;
54
55 $html .= "<select class='input' id='".$this->label."_pai' name='".$this->label."_pai' onchange='javascript: ai(this,\"".$this->name."_id\");' ".$this->input_extra.">".LF;
56 $html .= "<option value=''>Selecione</option>".LF;
57 $html .= "<option value=''>------</option>".LF;
58 $passou = false;
59 foreach ($this->elements as $k => $v) {
60 if ($k == $value) {
61 $s = " selected";
62 $passou = true;
63 } else {
64 $s = "";
65 }
66 $html .= "<option value='".$k."'".$s.">".$v."</option>".LF;
67 }
68
69 $html .= "</select>" . LF;
70 $html .= "<BR>".LF;
71 $html .= "<div id='".$this->label."_pai_div' style='padding-top: 5px;'></div>";
72
73 if(!$passou) {
74 $this->loadByChild($value); //descobre os pais do cara que está selecionado e põe no arr_pais da classe
75 $this->arr_pais = array_reverse($this->arr_pais);
76 $this->arr_pais[] = $value;
77 //echo "<BR><BR>valor atual: ".$value."<BR>";
78 //print_r2($this->arr_pais);
79 //die;
80
81 $pais_line = implode(",",$this->arr_pais);
82 //echo $pais_line."<BR>";
83 $html = str_replace("$ARR_PAIS$",$pais_line, $html);
84 $html .= "<script type='text/javascript'>".LF;
85 $html .= "loadFromFilho($$('".$this->label."_pai'));".LF;
86 $html .= "</script>".LF;
87 }else{
88 $html = str_replace("$ARR_PAIS$","", $html);
89 }
90 break;
91 }
92
93 if (($this->menu != "") && (!$this->is_static)) {
94 global $menu;
95
96 $n = "";
97
98 foreach ($menu->_itens as $k => $v) {
99 foreach ($v["subs"] as $k2 => $v2) {
100 if ($v2->class == $this->menu) {
101 $n = $k . "-" . $k2;
102
103 break;
104 }
105 }
106 }
107
108 if ($n != "") {
109 $html .= "<img title='Editar registros' align='top' onclick=\"javascript: { document.location = '?menu=" . $n . "'; }\" src='../img/icons/table_edit.gif' style='cursor: pointer; margin: 1 2 0 3px; _margin: 2 2 0 3px;'>";
110 }
111 }
112
113 $this->element_key = $value;
114 if ($value != "") {
115 if (array_key_exists($value, $this->elements)) {
116 $this->value = $this->elements[$value];
117 }
118 }
119
120 return $html;
121 }
122 function loadByChild($idComparar = "") {
123 global $db;
124
125 //se entrou aqui, significa que não encontrou o valor atual dentro da primeira lista (pai)
126 /* 0 - id (campo a ser buscado na tabela)
127 * 1 - idPai (campo que se relaciona com o id)
128 * 2 - nome (campo que será mostrado nos outros combos)
129 * 3 - tabela (tabela)
130 */
131 $sql = "select distinct ".$this->args[0].",".$this->args[2].",".$this->args[1]." from ".$this->args[3]." where ".$this->args[0]."=".$idComparar;
132 //echo $sql."<BR>";
133 if ($idComparar != "")
134 $rs_pais = $db->execute($sql);
135 else
136 return;
137
138 if ($rs_pais->recordcount > 0){
139 if ($rs_pais->fields($this->args[1]) == $idComparar){
140 return;
141 }else{
142 if($rs_pais->fields($this->args[1]) != "") {
143 $this->arr_pais[] = $rs_pais->fields($this->args[1]);
144 $this->loadByChild($rs_pais->fields($this->args[1]));
145 }else{
146 return;
147 }
148 }
149 }
150 }
151 function getFilter() {
152 global $input;
153
154 $html = "";
155 $sql = "";
156
157 $filter_name = $this->_getFilterName();
158 $filter_value = $this->_getFilterValue();
159 $filter_chk = $this->_getFilterChecked();
160 $old_name = $this->input_name;
161
162 $html = "<tr class='fieldset'>" . LF;
163 $html .= " <td class='label'>" . $this->label . ":</td>" . LF;
164 $html .= " <td class='label' style='width: 30px; text-align: center;'><input type='checkbox' name='" . $filter_name . "-chk' id='" . $filter_name . "-chk' value='1' " . $filter_chk . "></td>" . LF;
165
166 //crete filter input
167 $this->input_name = $filter_name;
168 $this->value = $filter_value;
169 $this->is_required = false;
170 if ($this->input_type == "select") {
171 $this->input_extra = "onChange=\"javascript: { if (this.value != '') { $$('" . $filter_name . "-chk').checked = true; } else { $$('" . $filter_name . "-chk').checked = false; } }\"";
172 }
173
174
175 $html .= " <td class='input'>" . $this->getInput() . "</td>" . LF;
176 $html .= "</tr>" . LF;
177
178 if ($filter_value != "") {
179 $sql = " and " . $old_name . " = '" . $filter_value . "'";
180 }
181
182 return array("html" => $html, "sql" => $sql);
183 }
184
185 function getHtmlList($tmp_extra) {
186 $this->formatValue();
187
188 $this->element_key = $this->value;
189 if (array_key_exists($this->value, $this->elements)) {
190 $this->value = $this->elements[$this->value];
191 } else {
192 //não está na primeira lista de objetos carregados.
193 $this->loadByChild($this->value); //descobre os pais do cara que está selecionado e põe no arr_pais da classe
194 $this->arr_pais = array_reverse($this->arr_pais); //coloca os pais por ordem crescente
195 $this->arr_pais[] = $this->value; //adiciona o último, que é o valor real
196
197 $this->value = $this->getNames($this->value);
198 }
199
200 if ($this->value == "") {
201 if ($this->element_key == "") {
202 $this->value = "<font color='silver'>(vazio)</font>";
203 } else {
204 $this->value = "<font color='silver'>[" . $this->element_key . "]</font>";
205 }
206 }
207
208 $html = "<td " . $tmp_extra . ">" . $this->value . "</td>" . LF;
209
210 return $html;
211 }
212 function getNames($valor_atual) {
213 global $db;
214 /* 0 - id (campo a ser buscado na tabela)
215 * 1 - idPai (campo que se relaciona com o id)
216 * 2 - nome (campo que será mostrado nos outros combos)
217 * 3 - tabela (tabela)
218 */
219 $names = "";
220 for($x=0;$x < sizeof($this->arr_pais);$x++){
221 $sql = "select ".$this->args[2].",".$this->args[0]." from ".$this->args[3]." where ".$this->args[0]." = ".$this->arr_pais[$x];
222 //echo $sql."<BR><BR>";
223 $rs = $db->execute($sql);
224
225 if($rs->recordcount > 0) {
226 $atual = $rs->fields($this->args[2]);
227 if ($rs->fields($this->args[0]) == $valor_atual)
228 $atual = "<b>".$atual."</b>";
229
230 if($names != "")
231 $names .= " > ".$atual;
232 else
233 $names = $atual;
234 }
235 }
236 return $names;
237 }
238}
239?>