1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package fwolflib |
4
|
|
|
* @subpackage class |
5
|
|
|
* @copyright Copyright 2009-2010, Fwolf |
6
|
|
|
* @author Fwolf <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
require_once(dirname(__FILE__) . '/fwolflib.php'); |
11
|
|
|
require_once(FWOLFLIB . 'func/string.php'); |
12
|
|
|
require_once(FWOLFLIB . 'func/validate.php'); |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Form operate class |
17
|
|
|
* |
18
|
|
|
* Generate html of form, |
19
|
|
|
* plus operate like validate, data recieve etc. |
20
|
|
|
* |
21
|
|
|
* Reference: |
22
|
|
|
* <http://pear.php.net/package/HTML_QuickForm/docs> |
23
|
|
|
* Form format: |
24
|
|
|
* <http://www.52css.com/article.asp?id=238> |
25
|
|
|
* |
26
|
|
|
* @package fwolflib |
27
|
|
|
* @subpackage class |
28
|
|
|
* @copyright Copyright 2009-2010, Fwolf |
29
|
|
|
* @author Fwolf <[email protected]> |
30
|
|
|
* @since 2009-07-26 |
31
|
|
|
*/ |
32
|
|
|
class Form extends Fwolflib { |
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Configuration |
35
|
|
|
* array( |
36
|
|
|
* action => url |
37
|
|
|
* enctype => useful when upload file |
38
|
|
|
* method => POST or GET |
39
|
|
|
* name => same as id |
40
|
|
|
* ) |
41
|
|
|
* @var array |
42
|
|
|
* @see Reset() |
43
|
|
|
* @see SetCfg() |
44
|
|
|
* @see SetCfgEnctype() |
45
|
|
|
*/ |
46
|
|
|
public $aCfg = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Form element define, raw order |
50
|
|
|
* |
51
|
|
|
* First value of attrib is DEFAULT value. |
52
|
|
|
* array( |
53
|
|
|
* name => array( |
54
|
|
|
* name, |
55
|
|
|
* type, |
56
|
|
|
* label, |
57
|
|
|
* value, |
58
|
|
|
* attrib = array( |
59
|
|
|
* ), |
60
|
|
|
* ) |
61
|
|
|
* ) |
62
|
|
|
* @var array |
63
|
|
|
* @see $aElementAttribDefault |
64
|
|
|
*/ |
65
|
|
|
protected $aElement = array(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Default value of element attrib, use if not defined |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
public $aElementAttribDefault = array( |
72
|
|
|
// For textarea only |
73
|
|
|
'cols' => null, |
74
|
|
|
// Additional html define ? |
75
|
|
|
'html-add' => '', |
76
|
|
|
// Will following element stay in same row ? |
77
|
|
|
'keep_div' => false, |
78
|
|
|
// Label is before input or after it ? |
79
|
|
|
'label-pos' => 'before', |
80
|
|
|
// For select, multiple means multi-select, value is size |
81
|
|
|
'multiple' => null, |
82
|
|
|
// Selection or value list, usually be array |
83
|
|
|
'option' => null, |
84
|
|
|
// For date input(My97DatePicker) only |
85
|
|
|
'param' => '', |
86
|
|
|
// For textarea only |
87
|
|
|
'rows' => null, |
88
|
|
|
// Spacer between mutli item, eg: radio |
89
|
|
|
'spacer' => '', |
90
|
|
|
// Only image has src attrib |
91
|
|
|
'src' => '', |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Flag control <div> generate when doing element |
96
|
|
|
* 0 not setuped |
97
|
|
|
* 1 cur element will not end <div> |
98
|
|
|
* can be recursive. |
99
|
|
|
* @var boolean |
100
|
|
|
*/ |
101
|
|
|
protected $iFlagKeepDiv = false; |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* contruct |
106
|
|
|
*/ |
107
|
|
|
public function __construct() { |
108
|
|
|
$this->Reset(); |
109
|
|
|
} // end of func __construct |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Add an element define |
114
|
|
|
* @param string $type |
115
|
|
|
* @param string $name Must not be empty or duplicate |
116
|
|
|
* @param string $label |
117
|
|
|
* @param array $attrib Additional html attributes. |
118
|
|
|
* @see $aElement |
119
|
|
|
*/ |
120
|
|
|
public function AddElement($type, $name, $label = '', $attrib = array()) { |
121
|
|
|
$this->aElement[$name] = array( |
122
|
|
|
'name' => $name, |
123
|
|
|
'type' => $type, |
124
|
|
|
'label' => $label, |
125
|
|
|
'attrib' => $attrib, |
126
|
|
|
); |
127
|
|
|
if ('file' == $type) |
128
|
|
|
$this->SetCfgEnctype(1); |
129
|
|
|
} // end of func AddElement |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add element attribe define |
134
|
|
|
* @param string $name |
135
|
|
|
* @param mixed $key |
136
|
|
|
* @param mixed $val |
137
|
|
|
* @see $aElement |
138
|
|
|
*/ |
139
|
|
|
public function AddElementAttrib($name, $key, $val = null) { |
140
|
|
|
if (isset($this->aElement[$name])) { |
141
|
|
|
if (is_array($key)) { |
142
|
|
|
foreach ($key as $k => $v) |
143
|
|
|
$this->aElement[$name]['attrib'][$k] = $v; |
144
|
|
|
} |
145
|
|
|
else |
146
|
|
|
$this->aElement[$name]['attrib'][$key] = $val; |
147
|
|
|
} |
148
|
|
|
} // end of func AddElementAttrib |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add element value attrib |
153
|
|
|
* |
154
|
|
|
* If $name is an array, it's a name/value array, |
155
|
|
|
* or only assign $v to single element $name. |
156
|
|
|
* @param mixed $name |
157
|
|
|
* @param mixed $v |
158
|
|
|
*/ |
159
|
|
|
public function AddElementValue($name, $v = null) { |
160
|
|
|
if (is_array($name)) { |
161
|
|
|
foreach ($name as $key => $val) |
162
|
|
|
$this->AddElementValue($key, $val); |
163
|
|
|
} |
164
|
|
|
else { |
165
|
|
|
if (!empty($v) && isset($this->aElement[$name])) |
166
|
|
|
$this->aElement[$name]['value'] = $v; |
167
|
|
|
} |
168
|
|
|
} // end of func AddElementValue |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Example of Generate task edit form |
173
|
|
|
* @param object &$form |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function EgGenFormTaskEdit(&$form) { |
177
|
|
|
if (empty($form)) |
178
|
|
|
$form = $this->oForm; |
|
|
|
|
179
|
|
|
$form->Reset(); |
180
|
|
|
|
181
|
|
|
$form->SetCfg(array( |
182
|
|
|
'name' => 'frm_task_edit', |
183
|
|
|
)); |
184
|
|
|
|
185
|
|
|
$form->AddElement('text', 'title', '任务名称' |
186
|
|
|
, array('html-add' => 'style="color: red;"')); |
187
|
|
|
|
188
|
|
|
$form->AddElement('fieldset', 'fs_1', '第一组'); |
189
|
|
|
$form->AddElement('checkbox', 'cb_1', '选项1' |
190
|
|
|
, array('checked' => true, 'option' => 1)); |
191
|
|
|
$form->AddElement('checkbox', 'cb_2', '选项2' |
192
|
|
|
, array('checked' => false, 'label_align' => 'after')); |
193
|
|
|
$form->AddElementAttrib('cb_2', 'option', 2); |
194
|
|
|
$form->AddElement('file', 'fu_1', '上传文件'); |
195
|
|
|
$form->AddElement('fieldset_end', 'fse_1', '第一组'); |
196
|
|
|
|
197
|
|
|
$form->AddElement('hidden', 'hid_1', 'Hide'); |
198
|
|
|
$form->AddElementValue('hid_1', "I'm hidden."); |
199
|
|
|
$form->AddElement('image', 'img_1', '图片?' |
200
|
|
|
, array('src' => 'http://www.acronymfinder.com/~/st/i/deli.gif')); |
201
|
|
|
$form->AddElement('password', 'pwd_1', '密码' |
202
|
|
|
, array('html-add' => 'style="color: red;"')); |
203
|
|
|
$form->AddElement('radio', 'rad_1', '只能选一个:'); |
204
|
|
|
$form->AddElementAttrib('rad_1', array( |
205
|
|
|
'spacer' => '<br />', |
206
|
|
|
'label_align' => 'after', |
207
|
|
|
)); |
208
|
|
|
$form->AddElementAttrib('rad_1', 'option', array( |
209
|
|
|
array('label' => '选择1', 'option' => '1'), |
210
|
|
|
array('label' => '选择2', 'option' => '2'), |
211
|
|
|
)); |
212
|
|
|
$form->AddElement('select', 'sel_1', '选一个'); |
213
|
|
|
$form->AddElementAttrib('sel_1', 'option', array( |
214
|
|
|
array('label' => '选择11', 'option' => '11'), |
215
|
|
|
array('label' => '选择22', 'option' => '22'), |
216
|
|
|
array('label' => '选择33', 'option' => '33'), |
217
|
|
|
array('label' => '选择44', 'option' => '44'), |
218
|
|
|
)); |
219
|
|
|
$form->AddElementAttrib('sel_1', 'multiple', 8); |
220
|
|
|
$form->AddElementValue('sel_1', array(22, 44)); |
221
|
|
|
$form->AddElement('select', 'sel_2', '再选一个'); |
222
|
|
|
$form->AddElementAttrib('sel_2', 'option', array( |
223
|
|
|
array('label' => '选择11', 'option' => '11'), |
224
|
|
|
array('label' => '选择22', 'option' => '22'), |
225
|
|
|
array('label' => '选择33', 'option' => '33'), |
226
|
|
|
array('label' => '选择44', 'option' => '44'), |
227
|
|
|
)); |
228
|
|
|
$form->AddElementValue('sel_2', 22); |
229
|
|
|
$form->AddElement('textarea', 'tx_2', '详细说明' |
230
|
|
|
, array('rows' => 4, 'cols' => 50)); |
231
|
|
|
$form->AddElement('date_my97', 'date_1', '时间' |
232
|
|
|
, array('param' => 'lang:\'zh-cn\'')); |
233
|
|
|
|
234
|
|
|
$form->AddElement('html', 'html_1', '<strong> or </strong>'); |
235
|
|
|
$form->AddElement('xsubmit', 'frm_save', '保存'); |
236
|
|
|
$form->AddElementAttrib('frm_save', 'keep_div', true); |
237
|
|
|
$form->AddElement('htmlraw', 'html_2', '<strong> OR </strong>'); |
238
|
|
|
$form->AddElement('xreset', 'frm_reset', '重置'); |
239
|
|
|
|
240
|
|
|
// return $this->oForm->GetHtml(); |
241
|
|
|
return $form; |
242
|
|
|
} // end of func EgGenFormTaskEdit |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Generate a form state, to validate this is not a |
247
|
|
|
* fake form pretend by a hacker. |
248
|
|
|
* Just like .NET's _viewstate |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
protected function GenFormState() { |
252
|
|
|
$s = session_id() . $this->aCfg['name']; |
253
|
|
|
$s = sha1($s); |
254
|
|
|
$s = str_replace(array('a', 'e', 'i', 'o', 'u'), '', $s); |
255
|
|
|
return $s; |
256
|
|
|
} // end of func GenFormState |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get html of an element |
261
|
|
|
* @param array $v |
|
|
|
|
262
|
|
|
* @return string |
263
|
|
|
* @see AddElement() |
264
|
|
|
*/ |
265
|
|
|
public function GetElement($elt) { |
266
|
|
|
// Apply element's default value |
267
|
|
|
foreach ($this->aElementAttribDefault as $k => $v) { |
268
|
|
|
if (!isset($elt['attrib'][$k]) && !empty($v)) |
269
|
|
|
$elt['attrib'][$k] = $v; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$s_html = ''; |
273
|
|
|
|
274
|
|
|
if (isset($elt['attrib']['label_align']) |
275
|
|
|
&& ('after' == $elt['attrib']['label_align'])) |
276
|
|
|
$s_div = 'fl_frm_elt_lr'; |
277
|
|
|
else |
278
|
|
|
$s_div = 'fl_frm_elt_ll'; |
279
|
|
|
|
280
|
|
|
if (false == $this->iFlagKeepDiv) |
|
|
|
|
281
|
|
|
$s_html .= '<div class="' . $s_div . '" id="fl_frm_elt_' |
282
|
|
|
. $elt['name'] . '">' . "\n"; |
283
|
|
|
|
284
|
|
|
// :TODO: autocomplete |
285
|
|
|
switch ($elt['type']) { |
286
|
|
|
case 'button': |
287
|
|
|
case 'reset': |
288
|
|
|
case 'submit': |
289
|
|
|
$s_html .= $this->GetElementButton($elt); |
290
|
|
|
break; |
291
|
|
|
case 'xbutton': |
292
|
|
|
case 'xreset': |
293
|
|
|
case 'xsubmit': |
294
|
|
|
$s_html .= $this->GetElementButtonX($elt); |
295
|
|
|
break; |
296
|
|
|
case 'checkbox': |
297
|
|
|
$s_html .= $this->GetElementCheckbox($elt); |
298
|
|
|
break; |
299
|
|
|
case 'datetime': |
300
|
|
|
case 'date_my97': |
301
|
|
|
$s_html .= $this->GetElementDateMy97($elt); |
302
|
|
|
break; |
303
|
|
|
case 'fieldset': |
304
|
|
|
return $this->GetElementFieldset($elt); |
305
|
|
|
break; |
|
|
|
|
306
|
|
|
case 'fieldset_end': |
307
|
|
|
return $this->GetElementFieldsetEnd($elt); |
308
|
|
|
break; |
|
|
|
|
309
|
|
|
case 'hidden': |
310
|
|
|
// Do not need outer div, so use return directly. |
311
|
|
|
return $this->GetElementHidden($elt); |
312
|
|
|
break; |
|
|
|
|
313
|
|
|
case 'html': |
314
|
|
|
$s_html .= $elt['label'] . "\n"; |
315
|
|
|
break; |
316
|
|
|
case 'htmlraw': |
317
|
|
|
return $elt['label'] . "\n"; |
318
|
|
|
break; |
|
|
|
|
319
|
|
|
case 'image': |
320
|
|
|
$s_html .= $this->GetElementImage($elt); |
321
|
|
|
break; |
322
|
|
|
case 'radio': |
323
|
|
|
$s_html .= $this->GetElementRadio($elt); |
324
|
|
|
break; |
325
|
|
|
case 'select': |
326
|
|
|
$s_html .= $this->GetElementSelect($elt); |
327
|
|
|
break; |
328
|
|
|
case 'file': |
329
|
|
|
case 'password': |
330
|
|
|
case 'text': |
331
|
|
|
$s_html .= $this->GetElementText($elt); |
332
|
|
|
break; |
333
|
|
|
case 'textarea': |
334
|
|
|
$s_html .= $this->GetElementTextarea($elt); |
335
|
|
|
break; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
if (isset($elt['attrib']['keep_div']) |
339
|
|
|
&& (true == $elt['attrib']['keep_div'])) |
340
|
|
|
$this->iFlagKeepDiv = true; |
341
|
|
|
else |
342
|
|
|
$this->iFlagKeepDiv = false; |
343
|
|
|
if (false == $this->iFlagKeepDiv) |
|
|
|
|
344
|
|
|
$s_html .= '</div>' . "\n\n"; |
345
|
|
|
|
346
|
|
|
return $s_html; |
347
|
|
|
} // end of func GetElement |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get html of element input/submit button |
352
|
|
|
* @param array $elt |
353
|
|
|
* @return string |
354
|
|
|
* @see AddElement() |
355
|
|
|
*/ |
356
|
|
|
protected function GetElementButton($elt) { |
357
|
|
|
$s_html = $this->GetHtmlInput($elt); |
358
|
|
|
// Label set as value |
359
|
|
|
$s_html = str_replace('/>', 'value="' . $elt['label'] . '" />' |
360
|
|
|
, $s_html); |
361
|
|
|
return $s_html; |
362
|
|
|
} // end of func GetElementButton |
363
|
|
|
|
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Get html of element checkbox |
367
|
|
|
* @param array $elt |
368
|
|
|
* @return string |
369
|
|
|
* @see AddElement() |
370
|
|
|
*/ |
371
|
|
|
protected function GetElementCheckbox($elt) { |
372
|
|
|
$s_label = $this->GetHtmlLabel($elt); |
373
|
|
|
$s_input = $this->GetHtmlInput($elt); |
374
|
|
|
|
375
|
|
|
// Attrib option as value |
376
|
|
View Code Duplication |
if (isset($elt['attrib']['option'])) |
|
|
|
|
377
|
|
|
$s_input = str_replace('/>' |
378
|
|
|
, 'value="' . $elt['attrib']['option'] . '" />' |
379
|
|
|
, $s_input); |
380
|
|
|
|
381
|
|
|
// Checked ? |
382
|
|
|
if (!empty($elt['attrib']['checked'])) |
383
|
|
|
$s_input = str_replace('/>' |
384
|
|
|
, 'checked="checked" />' |
385
|
|
|
, $s_input); |
386
|
|
|
|
387
|
|
View Code Duplication |
if (isset($elt['attrib']['label_align']) |
|
|
|
|
388
|
|
|
&& ('after' == $elt['attrib']['label_align'])) |
389
|
|
|
$s_html = $s_input . $s_label; |
390
|
|
|
else |
391
|
|
|
$s_html = $s_label . $s_input; |
392
|
|
|
|
393
|
|
|
return $s_html; |
394
|
|
|
} // end of func GetElementCheckbox |
395
|
|
|
|
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Get html of element date, using My97DatePicker |
399
|
|
|
* |
400
|
|
|
* Must include WdatePicker.js in tpl manually: |
401
|
|
|
* <script type="text/javascript" src="/js/DatePicker/WdatePicker.js"></script> |
402
|
|
|
* @param array $elt |
403
|
|
|
* @return string |
404
|
|
|
* @see AddElement() |
405
|
|
|
*/ |
406
|
|
|
protected function GetElementDateMy97($elt) { |
407
|
|
|
$s_html = $this->GetElementText($elt); |
408
|
|
|
// Make type for input right(input) |
409
|
|
|
$s_html = str_replace('<input type="date_my97"' |
410
|
|
|
, '<input type="input"', $s_html); |
411
|
|
|
|
412
|
|
|
// Value is already set in GetElementHidden() |
413
|
|
|
/* |
414
|
|
|
if (isset($elt['value'])) |
415
|
|
|
$s_html = str_replace('/>' |
416
|
|
|
, ' value="' . $elt['value'] . '" />' |
417
|
|
|
, $s_html); |
418
|
|
|
*/ |
419
|
|
|
|
420
|
|
|
// Add My97DatePicker part |
421
|
|
|
if (isset($elt['attrib']['param'])) |
422
|
|
|
$s_param = $elt['attrib']['param']; |
423
|
|
|
else |
424
|
|
|
$s_param = $this->aElementAttribDefault['param']; |
425
|
|
|
$s_html = str_replace('/>' |
426
|
|
|
, 'class="Wdate" onfocus="WdatePicker({' . $s_param . '})" />' |
427
|
|
|
, $s_html); |
428
|
|
|
|
429
|
|
|
return $s_html; |
430
|
|
|
} // end of func GetElementDateMy97 |
431
|
|
|
|
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Get html of element fieldset(begin) |
435
|
|
|
* |
436
|
|
|
* @param array $elt |
437
|
|
|
* @return string |
438
|
|
|
* @see AddElement() |
439
|
|
|
*/ |
440
|
|
|
protected function GetElementFieldset($elt) { |
441
|
|
|
$s_html = $this->GetHtmlInput($elt); |
442
|
|
|
$s_html = str_replace('<input type="fieldset"' |
443
|
|
|
, '<fieldset', $s_html); |
444
|
|
|
|
445
|
|
|
// Label as legend |
446
|
|
View Code Duplication |
if (isset($elt['label'])) |
|
|
|
|
447
|
|
|
$s_html = str_replace('/>' |
448
|
|
|
, '>' . "\n " . '<legend>' . $elt['label'] |
449
|
|
|
. '</legend>' . "\n" |
450
|
|
|
, $s_html); |
451
|
|
|
else |
452
|
|
|
$s_html = str_replace('/>', ">\n", $s_html); |
453
|
|
|
|
454
|
|
|
return $s_html; |
455
|
|
|
} // end of func GetElementFieldset |
456
|
|
|
|
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Get html of element fieldset(end) |
460
|
|
|
* @param array $elt |
461
|
|
|
* @return string |
462
|
|
|
* @see AddElement() |
463
|
|
|
*/ |
464
|
|
|
protected function GetElementFieldsetEnd($elt) { |
|
|
|
|
465
|
|
|
return "</fieldset>\n\n"; |
466
|
|
|
} // end of func GetElementFieldsetEnd |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Get html of element hidden |
471
|
|
|
* @param array $elt |
472
|
|
|
* @return string |
473
|
|
|
* @see AddElement() |
474
|
|
|
*/ |
475
|
|
|
protected function GetElementHidden($elt) { |
476
|
|
|
$s_html = $this->GetHtmlInput($elt); |
477
|
|
View Code Duplication |
if (isset($elt['value'])) |
|
|
|
|
478
|
|
|
$s_html = str_replace('/>' |
479
|
|
|
, ' value="' . $elt['value'] . '" />' |
480
|
|
|
, $s_html); |
481
|
|
|
return $s_html; |
482
|
|
|
} // end of func GetElementHidden |
483
|
|
|
|
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Get html of element image |
487
|
|
|
* @param array $elt |
488
|
|
|
* @return string |
489
|
|
|
* @see AddElement() |
490
|
|
|
*/ |
491
|
|
|
protected function GetElementImage($elt) { |
492
|
|
|
// No label |
493
|
|
|
$s_html = $this->GetElementHidden($elt); |
494
|
|
|
|
495
|
|
View Code Duplication |
if (isset($elt['attrib']['src'])) |
|
|
|
|
496
|
|
|
$s_html = str_replace('/>' |
497
|
|
|
, 'src="' . $elt['attrib']['src'] . '" />' |
498
|
|
|
, $s_html); |
499
|
|
|
|
500
|
|
|
// Label is alt text |
501
|
|
View Code Duplication |
if (isset($elt['label'])) |
|
|
|
|
502
|
|
|
$s_html = str_replace('/>' |
503
|
|
|
, 'alt="' . $elt['label'] . '" />' |
504
|
|
|
, $s_html); |
505
|
|
|
|
506
|
|
|
return $s_html; |
507
|
|
|
} // end of func GetElementImage |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Get html of element radio |
512
|
|
|
* @param array $elt |
513
|
|
|
* @return string |
514
|
|
|
* @see AddElement() |
515
|
|
|
*/ |
516
|
|
|
protected function GetElementRadio($elt) { |
517
|
|
|
$s_html = ''; |
518
|
|
|
$t = $elt; |
519
|
|
|
$s_spacer = (isset($elt['attrib']['spacer'])) |
520
|
|
|
? $elt['attrib']['spacer'] : ''; |
521
|
|
|
$i_id = 1; |
522
|
|
|
// Option is an array like array('label' => , 'option' =>) |
523
|
|
|
foreach ($elt['attrib']['option'] as $v) { |
524
|
|
|
// Use input go get label and input html. |
525
|
|
|
$t['label'] = $v['label']; |
526
|
|
|
$t['value'] = $v['option']; |
527
|
|
|
$s_t = $this->GetElementText($t) . $s_spacer; |
528
|
|
|
|
529
|
|
|
// Id can't be same, so rename them |
530
|
|
|
$s_t = str_replace('for="' . $elt['name'] . '"' |
531
|
|
|
, 'for="' . $elt['name'] . '-' . $i_id . '"', $s_t); |
532
|
|
|
$s_t = str_replace('id="' . $elt['name'] . '"' |
533
|
|
|
, 'id="' . $elt['name'] . '-' . $i_id . '"', $s_t); |
534
|
|
|
|
535
|
|
|
$i_id ++; |
536
|
|
|
$s_html .= $s_t; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
return $s_html; |
540
|
|
|
} // end of func GetElementRadio |
541
|
|
|
|
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Get html of element select |
545
|
|
|
* @param array $elt |
546
|
|
|
* @return string |
547
|
|
|
* @see AddElement() |
548
|
|
|
*/ |
549
|
|
|
protected function GetElementSelect($elt) { |
550
|
|
|
// Div, label, and input html |
551
|
|
|
$s_html = $this->GetElementText($elt); |
552
|
|
|
// Input -> select |
553
|
|
|
$s_html = str_replace('<input', '<select', $s_html); |
554
|
|
|
if (empty($elt['attrib']['multiple'])) { |
555
|
|
|
$s_html = str_replace('/>', '>', $s_html); |
556
|
|
|
} |
557
|
|
View Code Duplication |
else { |
|
|
|
|
558
|
|
|
$s_html = str_replace('/>', 'multiple="multiple" size="' |
559
|
|
|
. $elt['attrib']['multiple'] . '">', $s_html); |
560
|
|
|
// Set name to array format |
561
|
|
|
$s_html = str_replace('<select type="select" name="' |
562
|
|
|
. $elt['name'] . '"' |
563
|
|
|
, '<select type="select" name="' |
564
|
|
|
. $elt['name'] . '[]"' |
565
|
|
|
, $s_html); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
// Options |
569
|
|
|
$s_option = ''; |
|
|
|
|
570
|
|
|
// Option is an array like array('label' => , 'option' =>) |
571
|
|
|
foreach ($elt['attrib']['option'] as $v) { |
572
|
|
|
// <option value="volvo">Volvo</option> |
573
|
|
|
$s_t = 'value="' . $v['option'] |
574
|
|
|
. '">' . $v['label'] . '</option>' . "\n"; |
575
|
|
|
|
576
|
|
|
// Selected ? |
577
|
|
|
// Value can be array if multiple is set |
578
|
|
|
// Array - in_array, and = for string |
579
|
|
|
$b_selected = false; |
580
|
|
|
if (isset($elt['value'])) { |
581
|
|
|
if (is_array($elt['value']) |
582
|
|
|
&& in_array($v['option'], $elt['value'])) |
583
|
|
|
$b_selected = true; |
584
|
|
|
elseif ($elt['value'] == $v['option']) |
585
|
|
|
$b_selected = true; |
586
|
|
|
} |
587
|
|
|
if ($b_selected) |
588
|
|
|
$s_t = '<option selected="selected" ' . $s_t; |
589
|
|
|
else |
590
|
|
|
$s_t = '<option ' . $s_t; |
591
|
|
|
|
592
|
|
|
$s_html .= $s_t; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
$s_html .= "</select>\n"; |
596
|
|
|
return $s_html; |
597
|
|
|
} // end of func GetElementSelect |
598
|
|
|
|
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Get html of element common input/text |
602
|
|
|
* @param array $elt |
603
|
|
|
* @return string |
604
|
|
|
* @see AddElement() |
605
|
|
|
*/ |
606
|
|
|
protected function GetElementText($elt) { |
607
|
|
|
$s_label = $this->GetHtmlLabel($elt); |
608
|
|
|
// Plus str without label |
609
|
|
|
$s_input = $this->GetElementHidden($elt); |
610
|
|
|
|
611
|
|
View Code Duplication |
if (isset($elt['attrib']['label_align']) |
|
|
|
|
612
|
|
|
&& ('after' == $elt['attrib']['label_align'])) |
613
|
|
|
$s_html = $s_input . $s_label; |
614
|
|
|
else |
615
|
|
|
$s_html = $s_label . $s_input; |
616
|
|
|
|
617
|
|
|
return $s_html; |
618
|
|
|
} // end of func GetElementText |
619
|
|
|
|
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Get html of element textarea |
623
|
|
|
* @param array $elt |
624
|
|
|
* @return string |
625
|
|
|
* @see AddElement() |
626
|
|
|
*/ |
627
|
|
|
protected function GetElementTextarea($elt) { |
628
|
|
|
$s_row_col = ''; |
629
|
|
View Code Duplication |
if (isset($elt['attrib']['rows'])) |
|
|
|
|
630
|
|
|
$s_row_col .= 'rows="' . $elt['attrib']['rows'] . '" '; |
631
|
|
View Code Duplication |
if (isset($elt['attrib']['cols'])) |
|
|
|
|
632
|
|
|
$s_row_col .= 'cols="' . $elt['attrib']['cols'] . '" '; |
633
|
|
|
|
634
|
|
|
// Div, label, and input html |
635
|
|
|
$s_html = $this->GetElementText($elt); |
636
|
|
|
// Input -> select |
637
|
|
|
$s_html = str_replace('<input', '<textarea ' . $s_row_col, $s_html); |
638
|
|
|
$s_html = str_replace('/>', '>', $s_html); |
639
|
|
|
|
640
|
|
|
// Textarea value |
641
|
|
|
if (isset($elt['value'])) |
642
|
|
|
$s_html .= HtmlEncode($elt['value']); |
|
|
|
|
643
|
|
|
|
644
|
|
|
$s_html .= "</textarea>\n"; |
645
|
|
|
return $s_html; |
646
|
|
|
} // end of func GetElementTextarea |
647
|
|
|
|
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Get html of element button, not the input type=button one |
651
|
|
|
* @param array $elt |
652
|
|
|
* @return string |
653
|
|
|
* @see AddElement() |
654
|
|
|
*/ |
655
|
|
|
protected function GetElementButtonX($elt) { |
656
|
|
|
// Remove leading 'x' |
657
|
|
|
$elt['type'] = substr($elt['type'], 1); |
658
|
|
|
|
659
|
|
|
$s_html = $this->GetHtmlInput($elt); |
660
|
|
|
$s_html = str_replace('<input', '<button', $s_html); |
661
|
|
|
// Can have value, alough useless |
662
|
|
View Code Duplication |
if (isset($elt['value'])) |
|
|
|
|
663
|
|
|
$s_html = str_replace('/>', 'value="' . $elt['value'] . '" />' |
664
|
|
|
, $s_html); |
665
|
|
|
// Label set bteween tags |
666
|
|
View Code Duplication |
if (isset($elt['label'])) |
|
|
|
|
667
|
|
|
$s_html = str_replace('/>', '>' . $elt['label'] . '</button>' |
668
|
|
|
, $s_html); |
669
|
|
|
return $s_html; |
670
|
|
|
} // end of func GetElementButtonX |
671
|
|
|
|
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Get form html |
675
|
|
|
* @return string |
676
|
|
|
*/ |
677
|
|
|
public function GetHtml() { |
678
|
|
|
$s_html = ''; |
679
|
|
|
// Form style, for typeset only |
680
|
|
|
// ll = label left, lr = label right |
681
|
|
|
$s_html .= ' |
682
|
|
|
<style type="text/css" media="screen, print"> |
683
|
|
|
<!-- |
684
|
|
|
#' . $this->aCfg['name'] . ' .fl_frm_elt_ll { |
685
|
|
|
clear: left; |
686
|
|
|
padding-top: 0.2em; |
687
|
|
|
} |
688
|
|
|
#' . $this->aCfg['name'] . ' .fl_frm_elt_ll label { |
689
|
|
|
float: left; |
690
|
|
|
text-align: right; |
691
|
|
|
margin-right: 0.3em; |
692
|
|
|
padding-top: 0.2em; |
693
|
|
|
} |
694
|
|
|
#' . $this->aCfg['name'] . ' .fl_frm_elt_lr { |
695
|
|
|
/*clear: right;*/ |
696
|
|
|
padding-top: 0.2em; |
697
|
|
|
} |
698
|
|
|
#' . $this->aCfg['name'] . ' .fl_frm_elt_lr label { |
699
|
|
|
/*float: right;*/ |
700
|
|
|
text-align: left; |
701
|
|
|
margin-left: 0.3em; |
702
|
|
|
padding-top: 0.2em; |
703
|
|
|
} |
704
|
|
|
--> |
705
|
|
|
</style> |
706
|
|
|
'; |
707
|
|
|
|
708
|
|
|
// Form head |
709
|
|
|
$s_html .= '<form '; |
710
|
|
|
foreach ($this->aCfg as $k => $v) { |
711
|
|
|
if (!empty($v)) |
712
|
|
|
$s_html .= $k . '="' . $v . '" '; |
713
|
|
|
} |
714
|
|
|
if (!empty($this->aCfg['name'])) |
715
|
|
|
$s_html .= 'id="' . $this->aCfg['name'] . '" '; |
716
|
|
|
$s_html .= ' class="fl_frm"' . " >\n"; |
717
|
|
|
|
718
|
|
|
// Form body |
719
|
|
|
foreach ($this->aElement as $v) { |
720
|
|
|
$s_html .= $this->GetElement($v); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
// Form state, to validate form is posted, and security. |
724
|
|
|
// Hidden element, not need id property. |
725
|
|
|
$s_html .= '<input type="hidden" name="' |
726
|
|
|
. $this->aCfg['name'] . '_fs"' |
727
|
|
|
. ' value="' . $this->GenFormState() . '"/>' |
728
|
|
|
. "\n"; |
729
|
|
|
|
730
|
|
|
// Form footer |
731
|
|
|
$s_html .= "</form>\n"; |
732
|
|
|
return $s_html; |
733
|
|
|
} // end of func GetHtml |
734
|
|
|
|
735
|
|
|
|
736
|
|
|
/** |
737
|
|
|
* Get html of element's label part |
738
|
|
|
* @param array $elt |
739
|
|
|
* @return string |
740
|
|
|
* @see GetElement() |
741
|
|
|
*/ |
742
|
|
|
protected function GetHtmlLabel($elt) { |
743
|
|
|
$s_label = ''; |
744
|
|
|
if (!empty($elt['label'])) { |
745
|
|
|
$s_label .= '<label for="' . $elt['name'] . '">'; |
746
|
|
|
$s_label .= $elt['label'] . '</label>' . "\n"; |
747
|
|
|
} |
748
|
|
|
return $s_label; |
749
|
|
|
} // end of func GetHtmlLabel |
750
|
|
|
|
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* Get html of element's input part |
754
|
|
|
* @param array $elt |
755
|
|
|
* @return string |
756
|
|
|
* @see AddElement() |
757
|
|
|
*/ |
758
|
|
|
protected function GetHtmlInput($elt) { |
759
|
|
|
$s_input = ''; |
760
|
|
|
$s_input .= '<input '; |
761
|
|
|
$s_input .= 'type="' . $elt['type'] . '" '; |
762
|
|
|
$s_input .= 'name="' . $elt['name'] . '" '; |
763
|
|
|
$s_input .= 'id="' . $elt['name'] . '" '; |
764
|
|
|
if (isset($elt['attrib']['html-add']) |
765
|
|
|
&& (true == $elt['attrib']['html-add'])) |
766
|
|
|
$s_input .= $elt['attrib']['html-add']; |
767
|
|
|
$s_input .= '/>' . "\n"; |
768
|
|
|
|
769
|
|
|
return $s_input; |
770
|
|
|
} // end of func GetHtmlInput |
771
|
|
|
|
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* Check if it can get $_POST data from this form |
775
|
|
|
* Also include a little security check. |
776
|
|
|
* |
777
|
|
|
* If form state generated diff with posted, |
778
|
|
|
* probably form is not postd from original page, |
779
|
|
|
* so we assume it is an attack, treat as not a valid POST. |
780
|
|
|
* @return boolean |
781
|
|
|
* @see GenFormState() |
782
|
|
|
*/ |
783
|
|
|
public function IsPost() { |
784
|
|
|
$s = $this->aCfg['name'] . '_fs'; |
785
|
|
|
if (isset($_POST[$s]) |
786
|
|
|
&& ($_POST[$s] == $this->GenFormState())) |
787
|
|
|
return true; |
788
|
|
|
else |
789
|
|
|
return false; |
790
|
|
|
} // end of func IsPost |
791
|
|
|
|
792
|
|
|
|
793
|
|
|
/** |
794
|
|
|
* Reset all data to default, prepare to create a new form |
795
|
|
|
* |
796
|
|
|
* @param boolean $b_init Re-do init. |
797
|
|
|
*/ |
798
|
|
|
public function Reset ($b_init = false) { |
799
|
|
|
$this->aCfg = array( |
800
|
|
|
'action' => '', |
801
|
|
|
'enctype' => '', |
802
|
|
|
'method' => 'POST', |
803
|
|
|
'name' => 'fl_frm', |
804
|
|
|
); |
805
|
|
|
$this->aElement = array(); |
806
|
|
|
} // end of func Reset |
807
|
|
|
|
808
|
|
|
|
809
|
|
|
/** |
810
|
|
|
* Set configuration |
811
|
|
|
* @param array|string $c Config array or name/value pair. |
812
|
|
|
* @param string $v Config value |
813
|
|
|
* @see $aCfg |
814
|
|
|
*/ |
815
|
|
View Code Duplication |
public function SetCfg($c, $v = '') { |
|
|
|
|
816
|
|
|
if (is_array($c)) { |
817
|
|
|
if (!empty($c)) |
818
|
|
|
foreach ($c as $idx => $val) |
819
|
|
|
$this->SetCfg($idx, $val); |
820
|
|
|
} |
821
|
|
|
else |
822
|
|
|
$this->aCfg[$c] = $v; |
823
|
|
|
} // end of func SetCfg |
824
|
|
|
|
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* Set configuration enctype |
828
|
|
|
* @param int $type 0:application/x-www-form-urlencoded |
829
|
|
|
* 1:multipart/form-data |
830
|
|
|
* other value will empty the setting |
831
|
|
|
*/ |
832
|
|
|
public function SetCfgEnctype($type = 0) { |
833
|
|
|
if (0 == $type) |
834
|
|
|
$this->aCfg['enctype'] = 'application/x-www-form-urlencoded'; |
835
|
|
|
else if (1 == $type) |
836
|
|
|
$this->aCfg['enctype'] = 'multipart/form-data'; |
837
|
|
|
else |
838
|
|
|
$this->aCfg['enctype'] = ''; |
839
|
|
|
} // end of func SetCfgEnctype |
840
|
|
|
} // end of class Form |
841
|
|
|
?> |
|
|
|
|
842
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.