Optional processing progressbar for submit buttons
[fa-stable.git] / includes / ui / ui_input.inc
1 <?php
2
3 function get_post($name, $dflt='')
4 {
5         return ((!isset($_POST[$name]) || $_POST[$name] === '') ? $dflt : $_POST[$name]);
6 }
7 //
8 // Sets local POST value and adds Value to ajax posting if needed
9 //
10 /*function set_post($name, $value, $ajax_trigger=true) {
11     global $Ajax;
12
13     $_POST[$name] = $value;
14     if ($ajax_trigger) $Ajax->activate($name);
15 }
16 */
17 //------------------------------------------------------------------------------
18 //    Seek for _POST variable with $prefix.
19 //    If var is found returns variable name with prefix stripped,
20 //    and null or -1 otherwise.
21 //
22 function find_submit($prefix, $numeric=true)
23 {
24
25     foreach($_POST as $postkey=>$postval )
26     {
27                 if (strpos($postkey, $prefix) === 0)
28                 {
29                         $id = substr($postkey, strlen($prefix));
30                         return $numeric ? (int)$id : $id;
31                 }
32     }
33     return $numeric ? -1 : null;
34 }
35 //------------------------------------------------------------------------------
36 //
37 // Helper function for simple db table editor pages
38 //
39 function simple_page_mode($numeric_id = true)
40 {
41         global $Ajax, $Mode, $selected_id;
42
43         $default = $numeric_id ? -1 : '';
44         $selected_id = get_post('selected_id', $default);
45         foreach (array('ADD_ITEM', 'UPDATE_ITEM', 'RESET') as $m) {
46                 if (isset($_POST[$m])) {
47                         $Ajax->activate('_page_body');
48                         if ($m == 'RESET') 
49                                 $selected_id = $default;
50                         $Mode = $m; return;
51                 }
52         }
53         foreach (array('Edit', 'Delete') as $m) {
54                 foreach ($_POST as $p => $pvar) {
55                         if (strpos($p, $m) === 0) {
56                                 $selected_id = substr($p, strlen($m));
57                                 $Ajax->activate('_page_body');
58                                 $Mode = $m;
59                                 return;
60                         }
61                 }
62         }
63         $Mode = '';
64 }
65
66 //------------------------------------------------------------------------------
67 //
68 //      Read numeric value from user formatted input
69 //
70 function input_num($postname=null, $dflt=null)
71 {
72         if (!isset($_POST[$postname]))
73                 return $dflt;
74
75     return user_numeric($_POST[$postname]);
76 }
77
78 //---------------------------------------------------------------------------------
79
80 function hidden($name, $value=null, $echo=true)
81 {
82         global $Ajax;
83         
84         if ($value === null) 
85                 $value = get_post($name);
86         
87         $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
88         $Ajax->addUpdate($name, $name, $value);
89         if ($echo)
90                 echo $ret."\n";
91         else
92                 return $ret;
93 }
94
95 //---------------------------------------------------------------------------------
96 //      Submit button.
97 //      $async parameter can have 3 values:
98 //              null - fallback button not visible in js enabled mode
99 //              false - normal submit via form action
100 //              true  - when js is on submition via ajax call
101 //              'process' - ditto with processing indicator in msgbox
102 //
103 function submit($name, $value, $echo=true, $title=false, $async=false)
104 {
105
106         default_focus($name);
107         $submit_str = "<input type=\"submit\" class=\""
108             .($async ? 'ajaxsubmit' : 'inputsubmit')
109                 ."\""
110                 .($async === null ? (in_ajax() ? " style='display:none;'" : ' aspect="fallback"' ): 
111                         ($async === 'process' ? 'aspect="process"' : '') )
112             ." name=\"$name\"  id=\"$name\" value=\"$value\""
113             .($title ? " title='$title'" : '')
114             .">\n";
115         if ($echo)
116                 echo $submit_str;
117         else
118                 return $submit_str;
119 }
120
121 function submit_center($name, $value, $echo=true, $title=false, $async=false)
122 {
123         echo "<center>";
124         submit($name, $value, $echo, $title, $async);
125         echo "</center>";
126 }
127
128 function submit_center_first($name, $value, $title=false, $async=false)
129 {
130         echo "<center>";
131         submit($name, $value, true, $title, $async);
132         echo "&nbsp;";
133 }
134
135 function submit_center_last($name, $value, $title=false, $async=false)
136 {
137         echo "&nbsp;";
138         submit($name, $value, true, $title, $async);
139         echo "</center>";
140 }
141
142 function submit_add_or_update($add=true, $title=false, $async=false)
143 {
144         if ($add)
145                 submit('ADD_ITEM', _("Add new"), true, $title, $async);
146         else {
147                 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
148                 submit('RESET', _("Cancel"), true, $title, $async);
149         }
150 }
151
152 function submit_add_or_update_center($add=true, $title=false, $async=false)
153 {
154         echo "<center>";
155         submit_add_or_update($add, $title, $async);
156         echo "</center>";
157 }
158
159 /*
160 function submit_add_or_update_row($add=true)
161 {
162         echo "<tr><td colspan=99 align=center>";
163         submit_add_or_update($add);
164         echo "</td></tr>\n";
165 }
166 */
167 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
168 {
169         echo "<tr>";
170         if ($right)
171                 echo "<td>&nbsp;</td>\n";
172         echo "<td $extra>";
173         submit_add_or_update($add, $title, $async);
174         echo "</td></tr>\n";
175 }
176
177 function submit_cells($name, $value, $extra="", $title=false, $async=false)
178 {
179         echo "<td $extra>";
180         submit($name, $value, true, $title, $async);
181         echo "</td>\n";
182 }
183
184 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
185 {
186         echo "<tr>";
187         if ($right)
188                 echo "<td>&nbsp;</td>\n";
189         submit_cells($name, $value, $extra, $title, $async);
190         echo "</tr>\n";
191 }
192 //---------------------------------------------------------------------------------
193
194 function button($name, $value, $onclick, $title=false)
195 {
196         default_focus($name);
197         echo "<input type=\"button\" class=\"inputsubmit\" name=\"$name\" value=\"$value\" onclick=\"$onclick\""
198             .($title ? " title='$title'" : '')
199             ." />\n";
200 }
201
202 function button_cell($name, $value, $onclick, $title=false)
203 {
204         echo "<td>";
205         button($name, $value, $onclick, $title);
206         echo "</td>\n";
207 }
208
209 //-----------------------------------------------------------------------------------
210
211 function check_value($name)
212 {
213         if (!isset($_POST[$name]))
214                 return 0;
215         return 1;
216 }
217
218 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
219 {
220         global $Ajax;
221
222         default_focus($name);
223         if ($label)
224                 echo $label . "  ";
225
226         if ($value === null)
227                 $value = get_post($name,0);
228
229                 echo "<input"
230                     .($value == 1 ? ' checked':'')
231                     ." type='checkbox' name='$name' value='1'"
232                     .($submit_on_change ? " onclick='JsHttpRequest.request(\"_{$name}_update\", this.form);'" : '')
233                     .($title ? " title='$title'" : '')
234                     ." >\n";
235         $Ajax->addUpdate($name, $name, $value);
236 }
237
238 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
239 {
240         if ($label != null)
241                 echo "<td>$label</td>\n";
242         echo "<td>";
243         check(null, $name, $value, $submit_on_change, $title);
244         echo "</td>";
245 }
246
247 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
248 {
249         echo "<tr>";
250         check_cells($label, $name, $value, $submit_on_change, $title);
251         echo "</tr>\n";
252 }
253
254 //-----------------------------------------------------------------------------------
255
256 function labelheader_cell($label, $params="")
257 {
258         echo "<td class='tableheader' $params>$label</td>\n";
259 }
260
261 function label_cell($label, $params="", $id=null)
262 {
263     global $Ajax;
264
265         if(isset($id))
266         {
267             $params .= " id='$id'";
268             $Ajax->addUpdate($id, $id, $label);
269         }
270         echo "<td $params>$label</td>\n";
271
272         return $label;
273 }
274
275 function amount_cell($label, $bold=false, $params="", $id=null)
276 {
277         if ($bold)
278                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
279         else
280                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
281 }
282
283 function percent_cell($label, $bold=false, $id=null)
284 {
285         if ($bold)
286                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
287         else
288                 label_cell(percent_format($label), "nowrap align=right", $id);
289 }
290 // 2008-06-15. Changed
291 function qty_cell($label, $bold=false, $dec=null, $id=null)
292 {
293         if ($dec == null)
294                 $dec = get_qty_dec();
295         if ($bold)
296                 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
297         else
298                 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
299 }
300
301 function label_cells($label, $value, $params="", $params2="")
302 {
303         if ($label != null)
304                 echo "<td $params>$label</td>\n";
305         echo "<td $params2>$value</td>\n";
306 }
307
308 function label_row($label, $value, $params="", $params2="", $leftfill=0)
309 {
310         echo "<tr>";
311         label_cells($label, $value, $params, $params2);
312         if ($leftfill!=0)
313                 echo "<td colspan=$leftfill></td>";
314         echo "</tr>\n";
315 }
316
317 //-----------------------------------------------------------------------------------
318
319 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
320 {
321         global $Ajax;
322
323         default_focus($name);
324         if ($label != null)
325                 label_cell($label, $params);
326         echo "<td>";
327
328         if ($value === null)
329                 $value = get_post($name);
330         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
331             .($title ? " title='$title'" : '')
332             .">";
333
334         if ($post_label != "")
335                 echo " " . $post_label;
336
337         echo "</td>\n";
338         $Ajax->addUpdate($name, $name, $value);
339 }
340
341 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
342 {
343         global $Ajax;
344
345         default_focus($name);
346         if (!isset($_POST[$name]) || $_POST[$name] == "")
347         {
348                 if ($init)
349                         $_POST[$name] = $init;
350                 else
351                         $_POST[$name] = "";
352         }
353         if ($label != null)
354                 label_cell($label, $params);
355
356         if (!isset($max))
357                 $max = $size;
358
359         echo "<td>";
360         $class = $submit_on_change ? 'class="searchbox"' : '';
361         echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
362          .($title ? " title='$title'": '')." >";
363
364         if ($post_label)
365                 echo " " . $post_label;
366
367         echo "</td>\n";
368         $Ajax->addUpdate($name, $name, $_POST[$name]);
369 }
370
371 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
372 {
373         echo "<tr>";
374
375         text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
376
377         echo "</tr>\n";
378 }
379
380 //-----------------------------------------------------------------------------------
381
382 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
383 {
384         echo "<tr>";
385
386         text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
387
388         echo "</tr>\n";
389 }
390
391 //-----------------------------------------------------------------------------------
392
393 function date_cells($label, $name, $title = null, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
394 {
395         global $use_date_picker, $path_to_root;
396         if (!isset($_POST[$name]) || $_POST[$name] == "")
397         {
398                 if (!$init)
399                 {
400                         if ($inc_years == 1001)
401                                 $_POST[$name] = null;
402                         else
403                         {
404                                 $dd = Today();
405                                 if ($inc_days != 0)
406                                         $dd = add_days($dd, $inc_days);
407                                 if ($inc_months != 0)
408                                         $dd = add_months($dd, $inc_months);
409                                 if ($inc_years != 0)
410                                         $dd = add_years($dd, $inc_years);
411                                 $_POST[$name] = $dd;
412                         }
413                 }
414                 else
415                         $_POST[$name] = $init;
416         }
417         if ($use_date_picker)
418                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
419                 . "     <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";
420         else
421                 $post_label = "";
422         text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label);
423 }
424
425 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
426 {
427         echo "<tr>";
428         date_cells($label, $name, $title, $init, $inc_days, $inc_months, $inc_years, $params);
429         echo "</tr>\n";
430 }
431
432 //-----------------------------------------------------------------------------------
433
434 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
435 {
436         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
437 }
438
439 //-----------------------------------------------------------------------------------
440
441 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
442 {
443         echo "<tr>";
444         ref_cells($label, $name, $title, $init, null, $submit_on_change);
445         echo "</tr>\n";
446 }
447
448 //-----------------------------------------------------------------------------------
449
450 function percent_row($label, $name, $init=null)
451 {
452
453         if (!isset($_POST[$name]) || $_POST[$name]=="")
454         {
455                 $_POST[$name] = $init == null ? '' : $init;
456         }
457
458         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
459 }
460
461 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
462 {
463         global $Ajax;
464
465         if (!isset($dec))
466                 $dec = user_price_dec();
467         if (!isset($_POST[$name]) || $_POST[$name] == "")
468         {
469                 if ($init !== null)
470                         $_POST[$name] = $init;
471                 else
472                         $_POST[$name] = '';
473         }
474         if ($label != null)
475                 label_cell($label, $params);
476
477         if (!isset($max))
478                 $max = $size;
479
480         echo "<td>";
481
482         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
483
484         if ($post_label)
485                 echo " " . $post_label;
486
487         echo "</td>\n";
488         $Ajax->addUpdate($name, $name, $_POST[$name]);
489         $Ajax->addAssign($name, $name, 'dec', $dec);
490 }
491
492
493 //-----------------------------------------------------------------------------------
494
495 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
496 {
497         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
498 }
499
500 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
501 {
502         echo "<tr>";
503         amount_cells($label, $name, $init, $params, $post_label, $dec);
504         echo "</tr>\n";
505 }
506
507 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
508 {
509         echo "<tr>";
510         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
511         echo "</tr>\n";
512 }
513
514 //-----------------------------------------------------------------------------------
515
516 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
517 {
518         if (!isset($dec))
519                 $dec = user_qty_dec();
520
521         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
522 }
523
524 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
525 {
526         if (!isset($dec))
527                 $dec = user_qty_dec();
528
529         echo "<tr>";
530         amount_cells($label, $name, $init, $params, $post_label, $dec);
531         echo "</tr>\n";
532 }
533
534 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
535 {
536         if (!isset($dec))
537                 $dec = user_qty_dec();
538
539         echo "<tr>";
540         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
541         echo "</tr>\n";
542 }
543
544 //-----------------------------------------------------------------------------------
545
546 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
547 {
548         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
549 }
550
551 //-----------------------------------------------------------------------------------
552
553 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
554 {
555         if (!isset($dec))
556                 $dec = user_qty_dec();
557         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
558 }
559
560 //-----------------------------------------------------------------------------------
561
562 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
563 {
564         global $Ajax;
565
566         default_focus($name);
567         if ($label != null)
568                 echo "<td $params>$label</td>\n";
569         if ($value == null)
570                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
571         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
572         .($title ? " title='$title'" : '')
573         .">$value</textarea></td>\n";
574         $Ajax->addUpdate($name, $name, $value);
575 }
576
577 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
578 {
579         echo "<tr>";
580         textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
581         echo "</tr>\n";
582 }
583
584 //-----------------------------------------------------------------------------------
585 /*
586 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
587 {
588         global $Ajax;
589
590         default_focus($name);
591         echo "<tr><td>$label</td>\n";
592         echo "<td>";
593
594         if ($value == null)
595                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
596         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
597
598         submit($input_name, $input_value);
599
600         echo "</td></tr>\n";
601         $Ajax->addUpdate($name, $name, $value);
602 }
603 */
604 //-----------------------------------------------------------------------------------
605
606
607 ?>