Separated checkbox generation for indirect display use.
[fa-stable.git] / includes / ui / ui_input.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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 function submit($name, $value, $echo=true, $title=false, $async=false, $icon=false)
108 {
109         global $path_to_root;
110         
111         default_focus($name);
112         $submit_str = "<button class=\""
113             .($async ? 'ajaxsubmit' : 'inputsubmit')
114                 ."\" type=\"submit\""
115                 .($async === null ? (fallback_mode() ? ' aspect="fallback"' : " style='display:none;'" ): 
116                         ($async === 'process' ? 'aspect="process"' : '') )
117             ." name=\"$name\"  id=\"$name\" value=\"$value\""
118             .($title ? " title='$title'" : '')
119             ."><span>$value</span>"
120                 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon'>" : '')
121                 ."</button>\n";
122         if ($echo)
123                 echo $submit_str;
124         else
125                 return $submit_str;
126 }
127
128 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
129 {
130         echo "<center>";
131         submit($name, $value, $echo, $title, $async, $icon);
132         echo "</center>";
133 }
134
135 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
136 {
137         echo "<center>";
138         submit($name, $value, true, $title, $async, $icon);
139         echo "&nbsp;";
140 }
141
142 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
143 {
144         echo "&nbsp;";
145         submit($name, $value, true, $title, $async, $icon);
146         echo "</center>";
147 }
148
149 function submit_add_or_update($add=true, $title=false, $async=false)
150 {
151         if ($add)
152                 submit('ADD_ITEM', _("Add new"), true, $title, $async);
153         else {
154                 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
155                 submit('RESET', _("Cancel"), true, $title, $async);
156         }
157 }
158
159 function submit_add_or_update_center($add=true, $title=false, $async=false)
160 {
161         echo "<center>";
162         submit_add_or_update($add, $title, $async);
163         echo "</center>";
164 }
165
166 /*
167 function submit_add_or_update_row($add=true)
168 {
169         echo "<tr><td colspan=99 align=center>";
170         submit_add_or_update($add);
171         echo "</td></tr>\n";
172 }
173 */
174 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
175 {
176         echo "<tr>";
177         if ($right)
178                 echo "<td>&nbsp;</td>\n";
179         echo "<td $extra>";
180         submit_add_or_update($add, $title, $async);
181         echo "</td></tr>\n";
182 }
183
184 function submit_cells($name, $value, $extra="", $title=false, $async=false)
185 {
186         echo "<td $extra>";
187         submit($name, $value, true, $title, $async);
188         echo "</td>\n";
189 }
190
191 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
192 {
193         echo "<tr>";
194         if ($right)
195                 echo "<td>&nbsp;</td>\n";
196         submit_cells($name, $value, $extra, $title, $async);
197         echo "</tr>\n";
198 }
199
200 function submit_return($name, $value, $title=false, $async=false)
201 {
202         if (count($_SESSION['Context'])) {
203                 submit($name, $value, true, $title, $async);
204         }
205 }
206 //-----------------------------------------------------------------------------------
207
208 function set_icon($icon, $title=false)
209 {
210         global $path_to_root;
211         return "<img src='$path_to_root/themes/".user_theme()."/images/$icon' width='14' height='14' border='0'".($title ? " title='$title'" : "")." />\n";     
212 }
213
214 function button($name, $value, $title=false, $icon=false)
215 {
216         // php silently changes dots,spaces,'[' and characters 128-159
217         // to underscore in POST names, to maintain compatibility with register_globals
218         if (user_graphic_links() && $icon)
219         {
220                 if ($value == _("Delete")) // Helper during implementation
221                         $icon = ICON_DELETE;
222                 return "<button type='submit' class='editbutton' name='".
223                         htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
224                         "' value=''" . ($title ? " title='$title'":" title='$value'")." />".set_icon($icon)."\n";
225         }
226         else
227                 return "<input type='submit' class='editbutton' name='"
228                         .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
229                         ."' value='$value'"
230                         .($title ? " title='$title'":'')." />\n";
231 }
232
233 function button_cell($name, $value, $title=false, $icon=false)
234 {
235         echo "<td>";
236         echo button($name, $value, $title, $icon);
237         echo "</td>";
238 }
239
240 function delete_button_cell($name, $value, $title=false)
241 {
242         button_cell($name, $value, $title, ICON_DELETE);
243 }
244
245 function edit_button_cell($name, $value, $title=false)
246 {
247         button_cell($name, $value, $title, ICON_EDIT);
248 }
249 //-----------------------------------------------------------------------------------
250
251 function check_value($name)
252 {
253         if (!isset($_POST[$name]))
254                 return 0;
255         return 1;
256 }
257
258 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
259 {
260         global $Ajax;
261
262         $str = '';      
263         default_focus($name);
264         if ($label)
265                 $str .= $label . "  ";
266         if ($submit_on_change !== false) {
267                 if ($submit_on_change === true)
268                         $submit_on_change = 
269                                 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
270         }
271         if ($value === null)
272                 $value = get_post($name,0);
273
274         $str .= "<input"
275             .($value == 1 ? ' checked':'')
276             ." type='checkbox' name='$name' value='1'"
277             .($submit_on_change ? " onclick='$submit_on_change'" : '')
278             .($title ? " title='$title'" : '')
279             ." >\n";
280
281         $Ajax->addUpdate($name, $name, $value);
282         return $str;
283 }
284
285 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
286 {
287         echo checkbox($label, $name, $value, $submit_on_change, $title);
288 }
289
290 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
291 {
292         if ($label != null)
293                 echo "<td>$label</td>\n";
294         echo "<td>";
295         echo check(null, $name, $value, $submit_on_change, $title);
296         echo "</td>";
297 }
298
299 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
300 {
301         echo "<tr>";
302         echo check_cells($label, $name, $value, $submit_on_change, $title);
303         echo "</tr>\n";
304 }
305
306 //-----------------------------------------------------------------------------------
307
308 function labelheader_cell($label, $params="")
309 {
310         echo "<td class='tableheader' $params>$label</td>\n";
311 }
312
313 function label_cell($label, $params="", $id=null)
314 {
315     global $Ajax;
316
317         if(isset($id))
318         {
319             $params .= " id='$id'";
320             $Ajax->addUpdate($id, $id, $label);
321         }
322         echo "<td $params>$label</td>\n";
323
324         return $label;
325 }
326
327 function email_cell($label, $params="", $id=null)
328 {
329         label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
330 }
331
332 function amount_cell($label, $bold=false, $params="", $id=null)
333 {
334         if ($bold)
335                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
336         else
337                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
338 }
339
340 function percent_cell($label, $bold=false, $id=null)
341 {
342         if ($bold)
343                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
344         else
345                 label_cell(percent_format($label), "nowrap align=right", $id);
346 }
347 // 2008-06-15. Changed
348 function qty_cell($label, $bold=false, $dec=null, $id=null)
349 {
350         if (!isset($dec))
351                 $dec = get_qty_dec();
352         if ($bold)
353                 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
354         else
355                 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
356 }
357
358 function label_cells($label, $value, $params="", $params2="", $id='')
359 {
360         if ($label != null)
361                 echo "<td $params>$label</td>\n";
362         label_cell($value, $params2, $id);
363 }
364
365 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
366 {
367         echo "<tr>";
368         label_cells($label, $value, $params, $params2, $id);
369         if ($leftfill!=0)
370                 echo "<td colspan=$leftfill></td>";
371         echo "</tr>\n";
372 }
373
374 //-----------------------------------------------------------------------------------
375
376 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
377 {
378         global $Ajax;
379
380         default_focus($name);
381         if ($label != null)
382                 label_cell($label, $params);
383         echo "<td>";
384
385         if ($value === null)
386                 $value = get_post($name);
387         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
388             .($title ? " title='$title'" : '')
389             .">";
390
391         if ($post_label != "")
392                 echo " " . $post_label;
393
394         echo "</td>\n";
395         $Ajax->addUpdate($name, $name, $value);
396 }
397
398 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
399 {
400         global $Ajax;
401
402         default_focus($name);
403         if (!isset($_POST[$name]) || $_POST[$name] == "")
404         {
405                 if ($init)
406                         $_POST[$name] = $init;
407                 else
408                         $_POST[$name] = "";
409         }
410         if ($label != null)
411                 label_cell($label, $params);
412
413         if (!isset($max))
414                 $max = $size;
415
416         echo "<td>";
417         $class = $submit_on_change ? 'class="searchbox"' : '';
418         echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
419          .($title ? " title='$title'": '')." >";
420
421         if ($post_label)
422                 echo " " . $post_label;
423
424         echo "</td>\n";
425         $Ajax->addUpdate($name, $name, $_POST[$name]);
426 }
427
428 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
429 {
430         echo "<tr>";
431
432         text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
433
434         echo "</tr>\n";
435 }
436
437 //-----------------------------------------------------------------------------------
438
439 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
440 {
441         echo "<tr>";
442
443         text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
444
445         echo "</tr>\n";
446 }
447
448 //-----------------------------------------------------------------------------------
449 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
450 {
451         text_row("<a href='Mailto:".$_POST[$name]."'>$label</a>", $name, $value, $size, $max, $title, $params, $post_label);
452 }
453
454 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
455 {
456         text_row_ex("<a href='Mailto:".$_POST[$name]."'>$label</a>", $name, $size, $max, $title, $value, $params, $post_label);
457 }
458
459 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
460 {
461         text_row("<a href='".$_POST[$name]."' target='_blank'>$label</a>", $name, $value, $size, $max, $title, $params, $post_label);
462 }
463
464 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
465 {
466         text_row_ex("<a href='".$_POST[$name]."' target='_blank'>$label</a>", $name, $size, $max, $title, $value, $params, $post_label);
467 }
468
469 //-----------------------------------------------------------------------------------
470
471 function date_cells($label, $name, $title = null, $init=null, $inc_days=0, 
472         $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
473 {
474         global $use_date_picker, $path_to_root;
475         if (!isset($_POST[$name]) || $_POST[$name] == "")
476         {
477                 if (!$init)
478                 {
479                         if ($inc_years == 1001)
480                                 $_POST[$name] = null;
481                         else
482                         {
483                                 $dd = Today();
484                                 if ($inc_days != 0)
485                                         $dd = add_days($dd, $inc_days);
486                                 if ($inc_months != 0)
487                                         $dd = add_months($dd, $inc_months);
488                                 if ($inc_years != 0)
489                                         $dd = add_years($dd, $inc_years);
490                                 $_POST[$name] = $dd;
491                         }
492                 }
493                 else
494                         $_POST[$name] = $init;
495         }
496         if ($use_date_picker)
497                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
498                 . "     <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";
499         else
500                 $post_label = "";
501         text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label, $submit_on_change);
502 }
503
504 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0, 
505         $inc_years=0, $params=null, $submit_on_change=false)
506 {
507         echo "<tr>";
508         date_cells($label, $name, $title, $init, $inc_days, $inc_months, 
509                 $inc_years, $params, $submit_on_change);
510         echo "</tr>\n";
511 }
512
513 //-----------------------------------------------------------------------------------
514
515 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
516 {
517         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
518 }
519
520 //-----------------------------------------------------------------------------------
521
522 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
523 {
524         echo "<tr>";
525         ref_cells($label, $name, $title, $init, null, $submit_on_change);
526         echo "</tr>\n";
527 }
528
529 //-----------------------------------------------------------------------------------
530
531 function percent_row($label, $name, $init=null)
532 {
533
534         if (!isset($_POST[$name]) || $_POST[$name]=="")
535         {
536                 $_POST[$name] = $init == null ? '' : $init;
537         }
538
539         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
540 }
541
542 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
543 {
544         global $Ajax;
545
546         if (!isset($dec))
547                 $dec = user_price_dec();
548         if (!isset($_POST[$name]) || $_POST[$name] == "")
549         {
550                 if ($init !== null)
551                         $_POST[$name] = $init;
552                 else
553                         $_POST[$name] = '';
554         }
555         if ($label != null)
556                 label_cell($label, $params);
557
558         if (!isset($max))
559                 $max = $size;
560
561         if ($label != null)
562                 echo "<td>";
563         else
564                 echo "<td align='right'>";
565
566         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
567
568         if ($post_label) {
569                 echo "<span id='_{$name}_label'> $post_label</span>";
570                 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
571         }
572         echo "</td>\n";
573         $Ajax->addUpdate($name, $name, $_POST[$name]);
574         $Ajax->addAssign($name, $name, 'dec', $dec);
575 }
576
577
578 //-----------------------------------------------------------------------------------
579
580 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
581 {
582         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
583 }
584
585 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
586 {
587         echo "<tr>";
588         amount_cells($label, $name, $init, $params, $post_label, $dec);
589         echo "</tr>\n";
590 }
591
592 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
593 {
594         echo "<tr>";
595         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
596         echo "</tr>\n";
597 }
598
599 //-----------------------------------------------------------------------------------
600
601 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
602 {
603         if (!isset($dec))
604                 $dec = user_qty_dec();
605
606         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
607 }
608
609 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
610 {
611         if (!isset($dec))
612                 $dec = user_qty_dec();
613
614         echo "<tr>";
615         amount_cells($label, $name, $init, $params, $post_label, $dec);
616         echo "</tr>\n";
617 }
618
619 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
620 {
621         if (!isset($dec))
622                 $dec = user_qty_dec();
623
624         echo "<tr>";
625         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
626         echo "</tr>\n";
627 }
628
629 //-----------------------------------------------------------------------------------
630
631 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
632 {
633         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
634 }
635
636 //-----------------------------------------------------------------------------------
637
638 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
639 {
640         if (!isset($dec))
641                 $dec = user_qty_dec();
642         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
643 }
644
645 //-----------------------------------------------------------------------------------
646
647 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
648 {
649         global $Ajax;
650
651         default_focus($name);
652         if ($label != null)
653                 echo "<td $params>$label</td>\n";
654         if ($value == null)
655                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
656         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
657         .($title ? " title='$title'" : '')
658         .">$value</textarea></td>\n";
659         $Ajax->addUpdate($name, $name, $value);
660 }
661
662 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
663 {
664         echo "<tr>";
665         textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
666         echo "</tr>\n";
667 }
668
669 //-----------------------------------------------------------------------------------
670 /*
671 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
672 {
673         global $Ajax;
674
675         default_focus($name);
676         echo "<tr><td>$label</td>\n";
677         echo "<td>";
678
679         if ($value == null)
680                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
681         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
682
683         submit($input_name, $input_value);
684
685         echo "</td></tr>\n";
686         $Ajax->addUpdate($name, $name, $value);
687 }
688 */
689 //-----------------------------------------------------------------------------------
690
691
692 ?>