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