MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mmultitextfield.class
Ir para a documentação deste ficheiro.
1<?php
2
28{
29 protected $buttons;
30 protected $fields;
31 protected $showcode;
32 protected $shownav;
33 protected $layout;
34 protected $codevalue;
35 protected $colWidth;
36 public $info;
37 protected $numRows;
39
40 public function __construct($name = '', $value = null, $label = '', $fields = '', $width = 200, $buttons = false, $layout = 'vertical', $hint = '')
41 {
42 parent::__construct($name, $value, $label, null, $hint);
43
44 $this->page->addScript('m_multitext.js');
45
46 $this->fields = $fields;
47 $this->colWidth = $width;
48 $this->buttons = $buttons;
49 $this->layout = $layout;
50 $this->fieldWidth = $width - 20;
51 $this->showcode = false;
52 $this->shownav = false;
53 $this->numRows = 5;
54 $this->formMode = 0;
55 }
56
57 public function getCodeValue()
58 {
59 $r = array( );
60
62
63 if ( is_array($value) )
64 {
65 for ( $i = 0; $i < count($value); $i++ )
66 {
67 $s = substr($value[$i], 1, strlen(trim($value[$i])) - 2);
68 $a = explode('] [', $s);
69
70 if ( is_array($a) )
71 {
72 for ( $j = 0; $j < count($a); $j++ )
73 {
74 if ( is_array($this->fields[$j]) )
75 {
76 $options = $this->fields[$j][3];
77
78 if ( $options )
79 {
80 $r[$i][$j] = array_search($a[$j], $options);
81 }
82 else
83 {
84 $r[$i][$j] = $a[$j];
85 }
86 }
87 else
88 {
89 $r[$i][$j] = $a[$j];
90 }
91 }
92 }
93 }
94 }
95
96 $this->codevalue = $r;
97
98 return $this->codevalue;
99 }
100
101 public function setCodeValue($value)
102 {
103 $this->codevalue = $value;
104 $r = array( );
105
106 if ( is_array($value) )
107 {
108 for ( $i = 0; $i < count($value); $i++ )
109 {
110 $a = $value[$i];
111
112 if ( is_array($a) ) // varios valores => varios fields
113 {
114 for ( $j = 0; $j < count($a); $j++ )
115 {
116 if ( is_array($this->fields[$j]) )
117 {
118 $options = $this->fields[$j][3];
119
120 if ( $options )
121 {
122 $r[$i] .= '[' . $options[$value[$i][$j]] . '] ';
123 }
124 else
125 {
126 $r[$i] .= '[' . $value[$i][$j] . '] ';
127 }
128 }
129 else
130 {
131 $r[$i] .= '[' . $value[$i][$j] . '] ';
132 }
133 }
134 }
135 else // valor unico => apenas um field (na posicao 0)
136 {
137 if ( is_object($this->fields[0]) )
138 {
139 $options = $this->fields[0]->value;
140 }
141 else
142 $options = $this->fields[0][3];
143
144 if ( $options )
145 {
146 $r[$i] .= '[' . $options[$a] . '] ';
147 }
148 else
149 {
150 $r[$i] .= '[' . $a . '] ';
151 }
152 }
153 }
154 }
155
156 $this->value = $r;
157 }
158
159 public function generateInner()
160 {
161 $numFields = count($this->fields);
162 $this->page->onSubmit("_MIOLO_MultiTextField_onSubmit('{$this->form->name}','{$this->name}')");
163
164 // select
165 $labelS = $this->info . "&nbsp;";
166 $content = array( );
167
168 if ( is_array($this->value) )
169 {
170 foreach ( $this->value as $v )
171 {
172 $multiValue = '';
173
174 if ( is_array($v) )
175 {
176 foreach ( $v as $i => $v2 )
177 {
178 if ( is_array($this->fields[$i]) && is_array($this->fields[$i][3]) )
179 {
180 $multiValue .= "[" . $this->fields[$i][3][$v2] . "] ";
181 }
182 else
183 $multiValue .= "[" . $v2 . "] ";
184 }
185 }
186 else
187 {
188 $multiValue = $v;
189 }
190
191 $content[] = new MOption('', $multiValue, $multiValue);
192 }
193 }
194
195 $field = new MMultiSelection("{$this->name}", array( ), $labelS, $content, '', '', $this->numRows);
196
197 $field->_AddStyle('width', "{$this->colWidth}px");
198 $field->addAttribute('onKeyDown', "return _MIOLO_MultiTextField_onKeyDown(this,this.form,'{$this->name}',event,$numFields);");
199 $field->addAttribute('onChange', "_MIOLO_MultiTextField_onSelect(this.form,'{$this->name}', $numFields)");
200 $field->setClass('select m-multitext', false);
201 $field->formMode = 2;
202 $select = $field;
203
204 // fields
205 $n = 1;
206 unset($ref);
207
208 foreach ( $this->fields as $f )
209 {
210 if ( $ref )
211 {
212 $ref .= ',';
213 }
214
215 if ( is_object($f) )
216 {
217 $field = $f;
218 $ref .= $f->name;
219
220 $field->name = "{$this->name}_text{$n}";
221 $field->addAttribute('onKeyDown', "return _MIOLO_MultiTextField_onKeyDown(this,this.form,'{$this->name}',event,$numFields)");
222 $field->form = $this->form;
223 }
224 else
225 {
226 $ref .= $f[0];
227 $labelF = htmlspecialchars($f[1]) . ( $f[2] ? ' - ' . htmlspecialchars($f[2]) : '' );
228
229 // caso tenhamos opções para este campo ($f[3] é o array de opções)
230 // utilizamos um selection caso contrário um simples text field
231 $options = $f[3];
232 if ( $options )
233 {
234 $content = array( );
235
236 if ( $this->showcode )
237 {
238 foreach ( $options as $code => $desc )
239 {
240 $content[] = new MOption('', $code, "$code - $desc");
241 }
242 }
243 else
244 {
245 foreach ( $options as $code => $desc )
246 {
247 $content[] = new MOption('', $desc, $desc);
248 }
249 }
250
251 $field = new MSelection("{$this->name}_options{$n}", '', $labelF, $content);
252 $field->setClass('combo');
253 }
254 else
255 {
256 $field = new MTextField("{$this->name}_text{$n}", '', $labelF);
257 $field->addAttribute('onKeyDown', "return _MIOLO_MultiTextField_onKeyDown(this,this.form,'{$this->name}',event,$numFields)");
258 $field->setClass('textfield');
259 }
260 }
261 $field->_AddStyle('width', "{$this->fieldWidth}px");
262 $field->formMode = 2;
263 $fields[] = $field;
264 $n++;
265 }
266
267 // buttons
268 $disposition = ( $this->layout == 'horizontal' ) ? 'vertical' : 'horizontal';
269 $button[] = new MButton("{$this->name}_add", 'Adicionar', "_MIOLO_MultiTextField_add(this.form, '{$this->name}',$numFields)");
270 $button[] = new MButton("{$this->name}_modify", 'Modificar', "_MIOLO_MultiTextField_modify(this.form,'{$this->name}',$numFields)");
271 $button[] = new MButton("{$this->name}_remove", 'Remover', "_MIOLO_MultiTextField_remove(this.form,'{$this->name}',$numFields)");
272
273 if ( $this->shownav )
274 {
275 $button[] = new MButton("{$this->name}_up", '/\\', "_MIOLO_MultiTextField_moveUp(this.form,'{$this->name}',$numFields)");
276 $button[] = new MButton("{$this->name}_down", '\\/', "_MIOLO_MultiTextField_moveDown(this.form,'{$this->name}',$numFields)");
277 }
278
279 foreach ( $button as $b )
280 {
281 $b->setClass('button');
282 }
283
284 $buttons = new MContainer('', $button, $disposition);
285
286 $commentIn = $this->painter->comment('START OF Field MultiTextField');
287 $commentOut = $this->painter->comment('END OF Field MultiTextField');
288
289 // layout
290
291 $t = array( );
292 $cFields = new MContainer('', $fields, 'vertical');
293 if ( $this->layout == 'vertical' )
294 {
295 $t[] = $select;
296 $t[] = $cFields;
297 $t[] = $buttons;
298 $group = new BaseGroup('', $this->label, $t, 'vertical', 'css');
299 }
300 elseif ( $this->layout == 'vertical2' )
301 {
302 $t[] = $cFields;
303 $t[] = $buttons;
304 $t[] = $select;
305 $group = new BaseGroup('', $this->label, $t, 'vertical', 'css');
306 }
307 elseif ( $this->layout == 'horizontal' )
308 {
309 $t[] = $cFields;
310 $t[] = new MDiv('', array( new Div('', '&nbsp;', 'label'), $buttons ), 'buttonPosH');
311 $t[] = new MDiv('', $select, 'selectPosH');
312 $group = new BaseGroup('', $this->label, $t, 'horizontal', 'css');
313 }
314
315 $div = new MDiv('', $group, 'm-multitext-field');
316 $this->inner = array( $commentIn, $div, $commentOut );
317
318 return $this->inner;
319 }
320}
321?>
__construct($name='', $value=null, $label='', $fields='', $width=200, $buttons=false, $layout='vertical', $hint='')