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