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