A Purchasing Price of 12.34 with a conversion factor of 100,0000 caused a false inter...
[fa-stable.git] / includes / ui / ui_input.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function get_post($name, $dflt='')
14 {
15         return ((!isset($_POST[$name]) || $_POST[$name] === '') ? $dflt : $_POST[$name]);
16 }
17 //
18 // Sets local POST value and adds Value to ajax posting if needed
19 //
20 /*function set_post($name, $value, $ajax_trigger=true) {
21     global $Ajax;
22
23     $_POST[$name] = $value;
24     if ($ajax_trigger) $Ajax->activate($name);
25 }
26 */
27 //------------------------------------------------------------------------------
28 //    Seek for _POST variable with $prefix.
29 //    If var is found returns variable name with prefix stripped,
30 //    and null or -1 otherwise.
31 //
32 function find_submit($prefix, $numeric=true)
33 {
34
35     foreach($_POST as $postkey=>$postval )
36     {
37                 if (strpos($postkey, $prefix) === 0)
38                 {
39                         $id = substr($postkey, strlen($prefix));
40                         return $numeric ? (int)$id : $id;
41                 }
42     }
43     return $numeric ? -1 : null;
44 }
45 //------------------------------------------------------------------------------
46 //
47 // Helper function for simple db table editor pages
48 //
49 function simple_page_mode($numeric_id = true)
50 {
51         global $Ajax, $Mode, $selected_id;
52
53         $default = $numeric_id ? -1 : '';
54         $selected_id = get_post('selected_id', $default);
55         foreach (array('ADD_ITEM', 'UPDATE_ITEM', 'RESET') as $m) {
56                 if (isset($_POST[$m])) {
57                         $Ajax->activate('_page_body');
58                         if ($m == 'RESET') 
59                                 $selected_id = $default;
60                         $Mode = $m; return;
61                 }
62         }
63         foreach (array('Edit', 'Delete') as $m) {
64                 foreach ($_POST as $p => $pvar) {
65                         if (strpos($p, $m) === 0) {
66 //                              $selected_id = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
67                                 unset($_POST['_focus']); // focus on first form entry
68                                 $selected_id = quoted_printable_decode(substr($p, strlen($m)));
69                                 $Ajax->activate('_page_body');
70                                 $Mode = $m;
71                                 return;
72                         }
73                 }
74         }
75         $Mode = '';
76 }
77
78 //------------------------------------------------------------------------------
79 //
80 //      Read numeric value from user formatted input
81 //
82 function input_num($postname=null, $dflt=null)
83 {
84         if (!isset($_POST[$postname]) || $_POST[$postname] == "")
85                 return $dflt;
86
87     return user_numeric($_POST[$postname]);
88 }
89
90 //---------------------------------------------------------------------------------
91
92 function hidden($name, $value=null, $echo=true)
93 {
94         global $Ajax;
95         
96         if ($value === null) 
97                 $value = get_post($name);
98         
99         $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
100         $Ajax->addUpdate($name, $name, $value);
101         if ($echo)
102                 echo $ret."\n";
103         else
104                 return $ret;
105 }
106 /*
107         Various types of submit button selected by $type:
108         null    - button visible only in fallback mode
109         false   - normal submit button
110         true    - ajax driven submit
111         'process' - ajax button with long timeout 
112 */
113 function submit($name, $value, $echo=true, $title=false, $type=false, $icon=false)
114 {
115         global $path_to_root;
116
117         default_focus($name);
118         $submit_str = "<button class=\""
119             .($type ? 'ajaxsubmit' : 'inputsubmit')
120                 ."\" type=\"submit\""
121                 .($type === null ? (fallback_mode() ? ' aspect="fallback"' : " style='display:none;'" ): 
122                         ($type === 'process' ? 'aspect="process"' : '') )
123             ." name=\"$name\"  id=\"$name\" value=\"$value\""
124             .($title ? " title='$title'" : '')
125             ."><span>$value</span>"
126                 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon'>" : '')
127                 ."</button>\n";
128         if ($echo)
129                 echo $submit_str;
130         else
131                 return $submit_str;
132 }
133
134 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
135 {
136         echo "<center>";
137         submit($name, $value, $echo, $title, $async, $icon);
138         echo "</center>";
139 }
140
141 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
142 {
143         echo "<center>";
144         submit($name, $value, true, $title, $async, $icon);
145         echo "&nbsp;";
146 }
147
148 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
149 {
150         echo "&nbsp;";
151         submit($name, $value, true, $title, $async, $icon);
152         echo "</center>";
153 }
154
155 function submit_add_or_update($add=true, $title=false, $async=false)
156 {
157         if ($add)
158                 submit('ADD_ITEM', _("Add new"), true, $title, $async);
159         else {
160                 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
161                 submit('RESET', _("Cancel"), true, $title, $async);
162         }
163 }
164
165 function submit_add_or_update_center($add=true, $title=false, $async=false)
166 {
167         echo "<center>";
168         submit_add_or_update($add, $title, $async);
169         echo "</center>";
170 }
171
172 /*
173 function submit_add_or_update_row($add=true)
174 {
175         echo "<tr><td colspan=99 align=center>";
176         submit_add_or_update($add);
177         echo "</td></tr>\n";
178 }
179 */
180 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
181 {
182         echo "<tr>";
183         if ($right)
184                 echo "<td>&nbsp;</td>\n";
185         echo "<td $extra>";
186         submit_add_or_update($add, $title, $async);
187         echo "</td></tr>\n";
188 }
189
190 function submit_cells($name, $value, $extra="", $title=false, $async=false)
191 {
192         echo "<td $extra>";
193         submit($name, $value, true, $title, $async);
194         echo "</td>\n";
195 }
196
197 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
198 {
199         echo "<tr>";
200         if ($right)
201                 echo "<td>&nbsp;</td>\n";
202         submit_cells($name, $value, $extra, $title, $async);
203         echo "</tr>\n";
204 }
205
206 function submit_return($name, $value, $title=false, $async=false)
207 {
208         if (count($_SESSION['Context'])) {
209                 submit($name, $value, true, $title, $async);
210         }
211 }
212
213 function submit_js_confirm($name, $msg) {
214         add_js_source(
215                 "_validate.$name=function(){ return confirm('$msg');};");
216 };
217 //-----------------------------------------------------------------------------------
218
219 function set_icon($icon, $title=false)
220 {
221         global $path_to_root;
222         return "<img src='$path_to_root/themes/".user_theme()."/images/$icon' width='14' height='14' border='0'".($title ? " title='$title'" : "")." />\n";     
223 }
224
225 function button($name, $value, $title=false, $icon=false)
226 {
227         // php silently changes dots,spaces,'[' and characters 128-159
228         // to underscore in POST names, to maintain compatibility with register_globals
229         if (user_graphic_links() && $icon)
230         {
231                 if ($value == _("Delete")) // Helper during implementation
232                         $icon = ICON_DELETE;
233                 return "<button type='submit' class='editbutton' name='".
234                         htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
235                         "' value='1'" . ($title ? " title='$title'":" title='$value'")." />".set_icon($icon)."\n";
236         }
237         else
238                 return "<input type='submit' class='editbutton' name='"
239                         .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
240                         ."' value='$value'"
241                         .($title ? " title='$title'":'')." />\n";
242 }
243
244 function button_cell($name, $value, $title=false, $icon=false)
245 {
246         echo "<td>";
247         echo button($name, $value, $title, $icon);
248         echo "</td>";
249 }
250
251 function delete_button_cell($name, $value, $title=false)
252 {
253         button_cell($name, $value, $title, ICON_DELETE);
254 }
255
256 function edit_button_cell($name, $value, $title=false)
257 {
258         button_cell($name, $value, $title, ICON_EDIT);
259 }
260 //-----------------------------------------------------------------------------------
261
262 function check_value($name)
263 {
264         if (!isset($_POST[$name]))
265                 return 0;
266         return 1;
267 }
268
269 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
270 {
271         global $Ajax;
272
273         $str = '';      
274         default_focus($name);
275         if ($label)
276                 $str .= $label . "  ";
277         if ($submit_on_change !== false) {
278                 if ($submit_on_change === true)
279                         $submit_on_change = 
280                                 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
281         }
282         if ($value === null)
283                 $value = get_post($name,0);
284
285         $str .= "<input"
286             .($value == 1 ? ' checked':'')
287             ." type='checkbox' name='$name' value='1'"
288             .($submit_on_change ? " onclick='$submit_on_change'" : '')
289             .($title ? " title='$title'" : '')
290             ." >\n";
291
292         $Ajax->addUpdate($name, $name, $value);
293         return $str;
294 }
295
296 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
297 {
298         echo checkbox($label, $name, $value, $submit_on_change, $title);
299 }
300
301 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
302 {
303         if ($label != null)
304                 echo "<td>$label</td>\n";
305         echo "<td>";
306         echo check(null, $name, $value, $submit_on_change, $title);
307         echo "</td>";
308 }
309
310 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
311 {
312         echo "<tr>";
313         echo check_cells($label, $name, $value, $submit_on_change, $title);
314         echo "</tr>\n";
315 }
316
317 //-----------------------------------------------------------------------------------
318
319 function labelheader_cell($label, $params="")
320 {
321         echo "<td class='tableheader' $params>$label</td>\n";
322 }
323
324 function label_cell($label, $params="", $id=null)
325 {
326     global $Ajax;
327
328         if(isset($id))
329         {
330             $params .= " id='$id'";
331             $Ajax->addUpdate($id, $id, $label);
332         }
333         echo "<td $params>$label</td>\n";
334
335         return $label;
336 }
337
338 function email_cell($label, $params="", $id=null)
339 {
340         label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
341 }
342
343 function amount_decimal_cell($label, $params="", $id=null)
344 {
345         $dec = 0;
346         label_cell(price_decimal_format($label, $dec), "nowrap align=right ".$params, $id);
347 }
348
349 function amount_cell($label, $bold=false, $params="", $id=null)
350 {
351         if ($bold)
352                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
353         else
354                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
355 }
356
357 function percent_cell($label, $bold=false, $id=null)
358 {
359         if ($bold)
360                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
361         else
362                 label_cell(percent_format($label), "nowrap align=right", $id);
363 }
364 // 2008-06-15. Changed
365 function qty_cell($label, $bold=false, $dec=null, $id=null)
366 {
367         if (!isset($dec))
368                 $dec = get_qty_dec();
369         if ($bold)
370                 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
371         else
372                 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
373 }
374
375 function label_cells($label, $value, $params="", $params2="", $id='')
376 {
377         if ($label != null)
378                 echo "<td $params>$label</td>\n";
379         label_cell($value, $params2, $id);
380 }
381
382 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
383 {
384         echo "<tr>";
385         label_cells($label, $value, $params, $params2, $id);
386         if ($leftfill!=0)
387                 echo "<td colspan=$leftfill></td>";
388         echo "</tr>\n";
389 }
390
391 //-----------------------------------------------------------------------------------
392
393 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
394 {
395         global $Ajax;
396
397         default_focus($name);
398         if ($label != null)
399                 label_cell($label, $params);
400         echo "<td>";
401
402         if ($value === null)
403                 $value = get_post($name);
404         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
405             .($title ? " title='$title'" : '')
406             .">";
407
408         if ($post_label != "")
409                 echo " " . $post_label;
410
411         echo "</td>\n";
412         $Ajax->addUpdate($name, $name, $value);
413 }
414
415 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
416 {
417         global $Ajax;
418
419         default_focus($name);
420         if (!isset($_POST[$name]) || $_POST[$name] == "")
421         {
422                 if ($init)
423                         $_POST[$name] = $init;
424                 else
425                         $_POST[$name] = "";
426         }
427         if ($label != null)
428                 label_cell($label, $params);
429
430         if (!isset($max))
431                 $max = $size;
432
433         echo "<td>";
434         $class = $submit_on_change ? 'class="searchbox"' : '';
435         echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
436          .($title ? " title='$title'": '')." >";
437
438         if ($post_label)
439                 echo " " . $post_label;
440
441         echo "</td>\n";
442         $Ajax->addUpdate($name, $name, $_POST[$name]);
443 }
444
445 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
446 {
447         echo "<tr>";
448
449         text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
450
451         echo "</tr>\n";
452 }
453
454 //-----------------------------------------------------------------------------------
455
456 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
457 {
458         echo "<tr>";
459
460         text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
461
462         echo "</tr>\n";
463 }
464
465 //-----------------------------------------------------------------------------------
466 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
467 {
468         if (get_post($name)) 
469                 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
470         text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
471 }
472
473 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
474 {
475         if (get_post($name)) 
476                 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
477         text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
478 }
479
480 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
481 {
482         $val = get_post($name);
483         if ($val) {
484                 if (strpos($val,'http://')===false)
485                         $val = 'http://'.$val;
486                 $label = "<a href='$val' target='_blank'>$label</a>";
487         }
488         text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
489 }
490
491 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
492 {
493         $val = get_post($name);
494         if ($val) {
495                 if (strpos($val,'http://')===false)
496                         $val = 'http://'.$val;
497                 $label = "<a href='$val' target='_blank'>$label</a>";
498         }
499         text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
500 }
501
502 //-----------------------------------------------------------------------------------
503
504 function date_cells($label, $name, $title = null, $init=null, $inc_days=0, 
505         $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
506 {
507         global $use_date_picker, $path_to_root;
508         if (!isset($_POST[$name]) || $_POST[$name] == "")
509         {
510                 if (!$init)
511                 {
512                         if ($inc_years == 1001)
513                                 $_POST[$name] = null;
514                         else
515                         {
516                                 $dd = Today();
517                                 if ($inc_days != 0)
518                                         $dd = add_days($dd, $inc_days);
519                                 if ($inc_months != 0)
520                                         $dd = add_months($dd, $inc_months);
521                                 if ($inc_years != 0)
522                                         $dd = add_years($dd, $inc_years);
523                                 $_POST[$name] = $dd;
524                         }
525                 }
526                 else
527                         $_POST[$name] = $init;
528         }
529         if ($use_date_picker)
530                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
531                 . "     <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";
532         else
533                 $post_label = "";
534         text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label, $submit_on_change);
535 }
536
537 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0, 
538         $inc_years=0, $params=null, $submit_on_change=false)
539 {
540         echo "<tr>";
541         date_cells($label, $name, $title, $init, $inc_days, $inc_months, 
542                 $inc_years, $params, $submit_on_change);
543         echo "</tr>\n";
544 }
545
546 //-----------------------------------------------------------------------------------
547
548 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
549 {
550         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
551 }
552
553 //-----------------------------------------------------------------------------------
554
555 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
556 {
557         echo "<tr>";
558         ref_cells($label, $name, $title, $init, null, $submit_on_change);
559         echo "</tr>\n";
560 }
561
562 //-----------------------------------------------------------------------------------
563
564 function percent_row($label, $name, $init=null)
565 {
566
567         if (!isset($_POST[$name]) || $_POST[$name]=="")
568         {
569                 $_POST[$name] = $init == null ? '' : $init;
570         }
571
572         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
573 }
574
575 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
576 {
577         global $Ajax;
578
579         if (!isset($dec))
580                 $dec = user_price_dec();
581         if (!isset($_POST[$name]) || $_POST[$name] == "")
582         {
583                 if ($init !== null)
584                         $_POST[$name] = $init;
585                 else
586                         $_POST[$name] = '';
587         }
588         if ($label != null)
589                 label_cell($label, $params);
590
591         if (!isset($max))
592                 $max = $size;
593
594         if ($label != null)
595                 echo "<td>";
596         else
597                 echo "<td align='right'>";
598
599         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
600
601         if ($post_label) {
602                 echo "<span id='_{$name}_label'> $post_label</span>";
603                 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
604         }
605         echo "</td>\n";
606         $Ajax->addUpdate($name, $name, $_POST[$name]);
607         $Ajax->addAssign($name, $name, 'dec', $dec);
608 }
609
610
611 //-----------------------------------------------------------------------------------
612
613 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
614 {
615         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
616 }
617
618 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
619 {
620         echo "<tr>";
621         amount_cells($label, $name, $init, $params, $post_label, $dec);
622         echo "</tr>\n";
623 }
624
625 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
626 {
627         echo "<tr>";
628         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
629         echo "</tr>\n";
630 }
631
632 //-----------------------------------------------------------------------------------
633
634 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
635 {
636         if (!isset($dec))
637                 $dec = user_qty_dec();
638
639         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
640 }
641
642 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
643 {
644         if (!isset($dec))
645                 $dec = user_qty_dec();
646
647         echo "<tr>";
648         amount_cells($label, $name, $init, $params, $post_label, $dec);
649         echo "</tr>\n";
650 }
651
652 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
653 {
654         if (!isset($dec))
655                 $dec = user_qty_dec();
656
657         echo "<tr>";
658         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
659         echo "</tr>\n";
660 }
661
662 //-----------------------------------------------------------------------------------
663
664 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
665 {
666         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
667 }
668
669 //-----------------------------------------------------------------------------------
670
671 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
672 {
673         if (!isset($dec))
674                 $dec = user_qty_dec();
675         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
676 }
677
678 //-----------------------------------------------------------------------------------
679
680 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
681 {
682         global $Ajax;
683
684         default_focus($name);
685         if ($label != null)
686                 echo "<td $params>$label</td>\n";
687         if ($value == null)
688                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
689         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
690         .($title ? " title='$title'" : '')
691         .">$value</textarea></td>\n";
692         $Ajax->addUpdate($name, $name, $value);
693 }
694
695 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
696 {
697         echo "<tr>";
698         textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
699         echo "</tr>\n";
700 }
701
702 //-----------------------------------------------------------------------------------
703 /*
704 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
705 {
706         global $Ajax;
707
708         default_focus($name);
709         echo "<tr><td>$label</td>\n";
710         echo "<td>";
711
712         if ($value == null)
713                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
714         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
715
716         submit($input_name, $input_value);
717
718         echo "</td></tr>\n";
719         $Ajax->addUpdate($name, $name, $value);
720 }
721 */
722 //-----------------------------------------------------------------------------------
723
724
725 ?>