Automatic first field focus on page start, preserved focus sequence
[fa-stable.git] / includes / ui / ui_input.inc
1 <?php
2
3 //------------------------------------------------------------------------------
4 //    Seek for _POST variable with $prefix.
5 //    If var is found returns variable name with prefix stripped,
6 //    and null or -1 otherwise.
7 //
8 function find_submit($prefix, $numeric=true) {
9
10     foreach($_POST as $postkey=>$postval ) {
11         if (strpos($postkey, $prefix) === 0) {
12                 $id = substr($postkey, strlen($prefix));
13                 return $numeric ? (int)$id : $id;
14         }
15     }
16     return $numeric ? -1 : null;
17 }
18
19 //------------------------------------------------------------------------------
20 //
21 //      Read numeric value from user formatted input
22 //
23 function input_num($postname=null) {
24         if (!isset($_POST[$postname])) 
25           return null;
26
27     return user_numeric($_POST[$postname]);
28 }
29
30 //---------------------------------------------------------------------------------
31
32 function hidden($name, $value)
33 {
34         echo "<input type=\"hidden\" name=\"$name\" value=\"$value\">\n";
35 }
36
37 //---------------------------------------------------------------------------------
38
39 function submit($name, $value, $echo=true)
40 {
41         default_focus($name);
42         $submit_str = "<input type=\"submit\" class=\"inputsubmit\" name=\"$name\" value=\"$value\">\n";
43         if ($echo)
44                 echo $submit_str;
45         else
46                 return $submit_str;
47 }
48
49 function submit_center($name, $value, $echo=true)
50 {
51         echo "<center>";
52         submit($name, $value, $echo);
53         echo "</center>";
54 }
55
56 function submit_center_first($name, $value)
57 {
58         echo "<center>";
59         submit($name, $value);
60         echo "&nbsp;";
61 }
62
63 function submit_center_last($name, $value)
64 {
65         echo "&nbsp;";
66         submit($name, $value);
67         echo "</center>";
68 }
69
70 function submit_add_or_update($add=true)
71 {
72         if ($add)
73                 submit('ADD_ITEM', _("Save"));
74         else
75                 submit('UPDATE_ITEM', _("Update"));
76 }
77
78 function submit_add_or_update_center($add=true)
79 {
80         echo "<center>";
81         submit_add_or_update($add);
82         echo "</center>";
83 }
84
85 /*
86 function submit_add_or_update_row($add=true)
87 {
88         echo "<tr><td colspan=99 align=center>";
89         submit_add_or_update($add);
90         echo "</td></tr>\n";
91 }
92 */
93 function submit_add_or_update_row($add=true, $right=true, $extra="")
94 {
95         echo "<tr>";
96         if ($right)
97                 echo "<td>&nbsp;</td>\n";
98         echo "<td $extra>";
99         submit_add_or_update($add);
100         echo "</td></tr>\n";
101 }
102
103 function submit_cells($name, $value, $extra="")
104 {
105         echo "<td $extra>";
106         submit($name, $value);
107         echo "</td>\n";
108 }
109
110 function submit_row($name, $value, $right=true, $extra="")
111 {
112         echo "<tr>";
113         if ($right)
114                 echo "<td>&nbsp;</td>\n";
115         submit_cells($name, $value, $extra);
116         echo "</tr>\n";
117 }
118 //---------------------------------------------------------------------------------
119
120 function button($name, $value, $onclick)
121 {
122   default_focus($name);
123   echo "<input type=\"button\" class=\"inputsubmit\" name=\"$name\" value=\"$value\" onclick=\"$onclick\" />\n";
124 }
125
126 function button_cell($name, $value, $onclick)
127 {
128         echo "<td>";
129         button($name, $value, $onclick);
130         echo "</td>\n";
131 }
132
133 //-----------------------------------------------------------------------------------
134
135 function check_value($name)
136 {
137         if (!isset($_POST[$name]))
138                 return 0;
139         return 1;
140 }
141
142 function check($label, $name, $value, $submit_on_change=false)
143 {
144         default_focus($name);
145         if ($label)
146                 echo $label . "  ";
147
148         if ($value == null)
149                 $value = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
150         if ($value == 1)
151                 echo "<input checked type='checkbox' name='$name' value='1' ";
152         else
153                 echo "<input type='checkbox' name='$name' value='1'";
154         if ($submit_on_change)
155                 echo " onclick='this.form.submit();' ";
156         echo " >\n";
157 }
158
159 function check_cells($label, $name, $value, $submit_on_change=false)
160 {
161         if ($label != null)
162                 echo "<td>$label</td>\n";
163         echo "<td>";
164         check(null, $name, $value, $submit_on_change);
165         echo "</td>";
166 }
167
168 function check_row($label, $name, $value, $submit_on_change=false)
169 {
170         echo "<tr>";
171         check_cells($label, $name, $value, $submit_on_change);
172         echo "</tr>\n";
173 }
174
175 //-----------------------------------------------------------------------------------
176
177 function labelheader_cell($label, $params="")
178 {
179         echo "<td class='tableheader' $params>$label</td>\n";
180 }
181
182 function label_cell($label, $params="")
183 {
184         echo "<td $params>$label</td>\n";
185 }
186
187 function amount_cell($label, $bold=false)
188 {
189         if ($bold)
190                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right");
191         else
192                 label_cell(price_format($label), "nowrap align=right");
193 }
194
195 function percent_cell($label, $bold=false)
196 {
197         if ($bold)
198                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right");
199         else
200                 label_cell(percent_format($label), "nowrap align=right");
201 }
202
203 function qty_cell($label, $bold=false, $dec=null)
204 {
205         if ($bold)
206                 label_cell("<b>".qty_format($label, $dec)."</b>", "nowrap align=right");
207         else
208                 label_cell(qty_format($label, $dec), "nowrap align=right");
209 }
210
211 function label_cells($label, $value, $params="", $params2="")
212 {
213         if ($label != null)
214                 echo "<td $params>$label</td>\n";
215         echo "<td $params2>$value</td>\n";
216 }
217
218 function label_row($label, $value, $params="", $params2="", $leftfill=0)
219 {
220         echo "<tr>";
221         label_cells($label, $value, $params, $params2);
222         if($leftfill!=0)
223           echo "<td colspan=$leftfill></td>";
224         echo "</tr>\n";
225 }
226
227 //-----------------------------------------------------------------------------------
228
229 function text_cells($label, $name, $value, $size="", $max="", $params="", $post_label="", $disabled="")
230 {
231         default_focus($name);
232         if ($label != null)
233                 label_cell($label, $params);
234         echo "<td>";
235
236         if ($value == null)
237                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
238         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">";
239
240         if ($post_label != "")
241                 echo " " . $post_label;
242
243         echo "</td>\n";
244 }
245
246 function text_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null)
247 {
248         default_focus($name);
249         if (!isset($_POST[$name]) || $_POST[$name] == "")
250         {
251                 if ($init)
252                         $_POST[$name] = $init;
253                 else
254                         $_POST[$name] = "";
255         }
256         if ($label != null)
257                 label_cell($label, $params);
258
259         if (!isset($max))
260                 $max = $size;
261
262         echo "<td>";
263
264         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\">";
265
266         if ($post_label)
267                 echo " " . $post_label;
268
269         echo "</td>\n";
270 }
271
272 function text_row($label, $name, $value, $size, $max, $params="", $post_label="")
273 {
274         echo "<tr>";
275
276         text_cells($label, $name, $value, $size, $max, $params, $post_label);
277
278         echo "</tr>\n";
279 }
280
281 //-----------------------------------------------------------------------------------
282
283 function text_row_ex($label, $name, $size, $max=null, $value=null, $params=null, $post_label=null)
284 {
285         echo "<tr>";
286
287         text_cells_ex($label, $name, $size, $max, $value, $params, $post_label);
288
289         echo "</tr>\n";
290 }
291
292 //-----------------------------------------------------------------------------------
293
294 function date_cells($label, $name, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
295 {
296         global $use_date_picker, $path_to_root;
297         if (!isset($_POST[$name]) || $_POST[$name] == "")
298         {
299                 if (!$init)
300                 {
301                         if ($inc_years == 1001)
302                                 $_POST[$name] = null;
303                         else
304                         {
305                                 $dd = Today();
306                                 if ($inc_days != 0)
307                                         $dd = add_days($dd, $inc_days);
308                                 if ($inc_months != 0)
309                                         $dd = add_months($dd, $inc_months);
310                                 if ($inc_years != 0)
311                                         $dd = add_years($dd, $inc_years);
312                                 $_POST[$name] = $dd;
313                         }
314                 }
315                 else
316                         $_POST[$name] = $init;
317         }
318         if ($use_date_picker)
319                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.forms[0].$name);\">"
320                 . "     <img src='$path_to_root/themes/default/images/cal.gif' width='16' height='16' border='0' alt='"._('Click Here to Pick up the date')."'></a>\n";
321         else
322                 $post_label = "";
323         text_cells_ex($label, $name, 9, 12, $_POST[$name], $params, $post_label);
324 }
325
326 function date_row($label, $name, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
327 {
328         echo "<tr>";
329         date_cells($label, $name, $init, $inc_days, $inc_months, $inc_years, $params);
330         echo "</tr>\n";
331 }
332
333 //-----------------------------------------------------------------------------------
334
335 function ref_cells($label, $name, $init=null, $params=null)
336 {
337         text_cells_ex($label, $name, 16, 18, $init, $params);
338 }
339
340 //-----------------------------------------------------------------------------------
341
342 function ref_row($label, $name, $init=null)
343 {
344         echo "<tr>";
345         ref_cells($label, $name, $init);
346         echo "</tr>\n";
347 }
348
349 //-----------------------------------------------------------------------------------
350
351 function percent_row($label, $name, $init=null)
352 {
353
354         if (!isset($_POST[$name]) || $_POST[$name]=="")
355         {
356                         $_POST[$name] = $init== null ? '' : $init;
357         }
358
359         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
360 }
361
362 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
363 {
364         if (!isset($dec))
365           $dec = user_price_dec();
366         if (!isset($_POST[$name]) || $_POST[$name] == "")
367         {
368                 if ($init)
369                         $_POST[$name] = $init;
370                 else
371                         $_POST[$name] = '';
372         }
373         if ($label != null)
374                 label_cell($label, $params);
375
376         if (!isset($max))
377                 $max = $size;
378
379         echo "<td>";
380
381         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
382
383         if ($post_label)
384                 echo " " . $post_label;
385
386         echo "</td>\n";
387 }
388
389
390 //-----------------------------------------------------------------------------------
391
392 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
393 {
394         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
395 }
396
397 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
398 {
399         echo "<tr>";
400         amount_cells($label, $name, $init, $params, $post_label, $dec);
401         echo "</tr>\n";
402 }
403
404 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
405 {
406         echo "<tr>";
407         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
408         echo "</tr>\n";
409 }
410
411 //-----------------------------------------------------------------------------------
412
413 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
414 {
415         if(!isset($dec))
416           $dec = user_qty_dec();
417
418         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
419 }
420
421 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
422 {
423         if(!isset($dec))
424           $dec = user_qty_dec();
425
426         echo "<tr>";
427         amount_cells($label, $name, $init, $params, $post_label, $dec);
428         echo "</tr>\n";
429 }
430
431 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
432 {
433         if(!isset($dec))
434           $dec = user_qty_dec();
435
436         echo "<tr>";
437         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
438         echo "</tr>\n";
439 }
440
441 //-----------------------------------------------------------------------------------
442
443 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
444 {
445         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
446 }
447
448 //-----------------------------------------------------------------------------------
449
450 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
451 {
452   if (!isset($dec))
453           $dec = user_qty_dec();
454         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
455 }
456
457 //-----------------------------------------------------------------------------------
458
459 function textarea_cells($label, $name, $value, $cols, $rows, $params="")
460 {
461         default_focus($name);
462         if ($label != null)
463                 echo "<td $params>$label</td>\n";
464         if ($value == null)
465                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
466         echo "<td><textarea name='$name' cols='$cols' rows='$rows'>$value</textarea></td>\n";
467 }
468
469 function textarea_row($label, $name, $value, $cols, $rows, $params="")
470 {
471         echo "<tr>";
472         textarea_cells($label, $name, $value, $cols, $rows, $params);
473         echo "</tr>\n";
474 }
475
476 //-----------------------------------------------------------------------------------
477
478 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
479 {
480         default_focus($name);
481         echo "<tr><td>$label</td>\n";
482         echo "<td>";
483
484         if ($value == null)
485                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
486         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
487
488         submit($input_name, $input_value);
489
490         echo "</td></tr>\n";
491 }
492
493 //-----------------------------------------------------------------------------------
494
495
496 ?>