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