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