Better layout. Copyright notes. Mailto links. (2. run)
[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 check($label, $name, $value=null, $submit_on_change=false, $title=false)
259 {
260         global $Ajax;
261
262         default_focus($name);
263         if ($label)
264                 echo $label . "  ";
265
266         if ($value === null)
267                 $value = get_post($name,0);
268
269                 echo "<input"
270                     .($value == 1 ? ' checked':'')
271                     ." type='checkbox' name='$name' value='1'"
272                     .($submit_on_change ? " onclick='JsHttpRequest.request(\"_{$name}_update\", this.form);'" : '')
273                     .($title ? " title='$title'" : '')
274                     ." >\n";
275         $Ajax->addUpdate($name, $name, $value);
276 }
277
278 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
279 {
280         if ($label != null)
281                 echo "<td>$label</td>\n";
282         echo "<td>";
283         check(null, $name, $value, $submit_on_change, $title);
284         echo "</td>";
285 }
286
287 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
288 {
289         echo "<tr>";
290         check_cells($label, $name, $value, $submit_on_change, $title);
291         echo "</tr>\n";
292 }
293
294 //-----------------------------------------------------------------------------------
295
296 function labelheader_cell($label, $params="")
297 {
298         echo "<td class='tableheader' $params>$label</td>\n";
299 }
300
301 function label_cell($label, $params="", $id=null)
302 {
303     global $Ajax;
304
305         if(isset($id))
306         {
307             $params .= " id='$id'";
308             $Ajax->addUpdate($id, $id, $label);
309         }
310         echo "<td $params>$label</td>\n";
311
312         return $label;
313 }
314
315 function email_cell($label, $params="", $id=null)
316 {
317         label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
318 }
319
320 function amount_cell($label, $bold=false, $params="", $id=null)
321 {
322         if ($bold)
323                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
324         else
325                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
326 }
327
328 function percent_cell($label, $bold=false, $id=null)
329 {
330         if ($bold)
331                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
332         else
333                 label_cell(percent_format($label), "nowrap align=right", $id);
334 }
335 // 2008-06-15. Changed
336 function qty_cell($label, $bold=false, $dec=null, $id=null)
337 {
338         if (!isset($dec))
339                 $dec = get_qty_dec();
340         if ($bold)
341                 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
342         else
343                 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
344 }
345
346 function label_cells($label, $value, $params="", $params2="", $id='')
347 {
348         if ($label != null)
349                 echo "<td $params>$label</td>\n";
350         label_cell($value, $params2, $id);
351 }
352
353 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
354 {
355         echo "<tr>";
356         label_cells($label, $value, $params, $params2, $id);
357         if ($leftfill!=0)
358                 echo "<td colspan=$leftfill></td>";
359         echo "</tr>\n";
360 }
361
362 //-----------------------------------------------------------------------------------
363
364 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
365 {
366         global $Ajax;
367
368         default_focus($name);
369         if ($label != null)
370                 label_cell($label, $params);
371         echo "<td>";
372
373         if ($value === null)
374                 $value = get_post($name);
375         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
376             .($title ? " title='$title'" : '')
377             .">";
378
379         if ($post_label != "")
380                 echo " " . $post_label;
381
382         echo "</td>\n";
383         $Ajax->addUpdate($name, $name, $value);
384 }
385
386 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
387 {
388         global $Ajax;
389
390         default_focus($name);
391         if (!isset($_POST[$name]) || $_POST[$name] == "")
392         {
393                 if ($init)
394                         $_POST[$name] = $init;
395                 else
396                         $_POST[$name] = "";
397         }
398         if ($label != null)
399                 label_cell($label, $params);
400
401         if (!isset($max))
402                 $max = $size;
403
404         echo "<td>";
405         $class = $submit_on_change ? 'class="searchbox"' : '';
406         echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
407          .($title ? " title='$title'": '')." >";
408
409         if ($post_label)
410                 echo " " . $post_label;
411
412         echo "</td>\n";
413         $Ajax->addUpdate($name, $name, $_POST[$name]);
414 }
415
416 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
417 {
418         echo "<tr>";
419
420         text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
421
422         echo "</tr>\n";
423 }
424
425 //-----------------------------------------------------------------------------------
426
427 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
428 {
429         echo "<tr>";
430
431         text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
432
433         echo "</tr>\n";
434 }
435
436 //-----------------------------------------------------------------------------------
437 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
438 {
439         text_row("<a href='Mailto:".$_POST[$name]."'>$label</a>", $name, $value, $size, $max, $title, $params, $post_label);
440 }
441
442 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
443 {
444         text_row_ex("<a href='Mailto:".$_POST[$name]."'>$label</a>", $name, $size, $max, $title, $value, $params, $post_label);
445 }
446
447 //-----------------------------------------------------------------------------------
448
449 function date_cells($label, $name, $title = null, $init=null, $inc_days=0, 
450         $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
451 {
452         global $use_date_picker, $path_to_root;
453         if (!isset($_POST[$name]) || $_POST[$name] == "")
454         {
455                 if (!$init)
456                 {
457                         if ($inc_years == 1001)
458                                 $_POST[$name] = null;
459                         else
460                         {
461                                 $dd = Today();
462                                 if ($inc_days != 0)
463                                         $dd = add_days($dd, $inc_days);
464                                 if ($inc_months != 0)
465                                         $dd = add_months($dd, $inc_months);
466                                 if ($inc_years != 0)
467                                         $dd = add_years($dd, $inc_years);
468                                 $_POST[$name] = $dd;
469                         }
470                 }
471                 else
472                         $_POST[$name] = $init;
473         }
474         if ($use_date_picker)
475                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
476                 . "     <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";
477         else
478                 $post_label = "";
479         text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label, $submit_on_change);
480 }
481
482 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0, 
483         $inc_years=0, $params=null, $submit_on_change=false)
484 {
485         echo "<tr>";
486         date_cells($label, $name, $title, $init, $inc_days, $inc_months, 
487                 $inc_years, $params, $submit_on_change);
488         echo "</tr>\n";
489 }
490
491 //-----------------------------------------------------------------------------------
492
493 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
494 {
495         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
496 }
497
498 //-----------------------------------------------------------------------------------
499
500 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
501 {
502         echo "<tr>";
503         ref_cells($label, $name, $title, $init, null, $submit_on_change);
504         echo "</tr>\n";
505 }
506
507 //-----------------------------------------------------------------------------------
508
509 function percent_row($label, $name, $init=null)
510 {
511
512         if (!isset($_POST[$name]) || $_POST[$name]=="")
513         {
514                 $_POST[$name] = $init == null ? '' : $init;
515         }
516
517         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
518 }
519
520 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
521 {
522         global $Ajax;
523
524         if (!isset($dec))
525                 $dec = user_price_dec();
526         if (!isset($_POST[$name]) || $_POST[$name] == "")
527         {
528                 if ($init !== null)
529                         $_POST[$name] = $init;
530                 else
531                         $_POST[$name] = '';
532         }
533         if ($label != null)
534                 label_cell($label, $params);
535
536         if (!isset($max))
537                 $max = $size;
538
539         if ($label != null)
540                 echo "<td>";
541         else
542                 echo "<td align='right'>";
543
544         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
545
546         if ($post_label) {
547                 echo "<span id='_{$name}_label'> $post_label</span>";
548                 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
549         }
550         echo "</td>\n";
551         $Ajax->addUpdate($name, $name, $_POST[$name]);
552         $Ajax->addAssign($name, $name, 'dec', $dec);
553 }
554
555
556 //-----------------------------------------------------------------------------------
557
558 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
559 {
560         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
561 }
562
563 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
564 {
565         echo "<tr>";
566         amount_cells($label, $name, $init, $params, $post_label, $dec);
567         echo "</tr>\n";
568 }
569
570 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
571 {
572         echo "<tr>";
573         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
574         echo "</tr>\n";
575 }
576
577 //-----------------------------------------------------------------------------------
578
579 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
580 {
581         if (!isset($dec))
582                 $dec = user_qty_dec();
583
584         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
585 }
586
587 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
588 {
589         if (!isset($dec))
590                 $dec = user_qty_dec();
591
592         echo "<tr>";
593         amount_cells($label, $name, $init, $params, $post_label, $dec);
594         echo "</tr>\n";
595 }
596
597 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
598 {
599         if (!isset($dec))
600                 $dec = user_qty_dec();
601
602         echo "<tr>";
603         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
604         echo "</tr>\n";
605 }
606
607 //-----------------------------------------------------------------------------------
608
609 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
610 {
611         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
612 }
613
614 //-----------------------------------------------------------------------------------
615
616 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
617 {
618         if (!isset($dec))
619                 $dec = user_qty_dec();
620         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
621 }
622
623 //-----------------------------------------------------------------------------------
624
625 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
626 {
627         global $Ajax;
628
629         default_focus($name);
630         if ($label != null)
631                 echo "<td $params>$label</td>\n";
632         if ($value == null)
633                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
634         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
635         .($title ? " title='$title'" : '')
636         .">$value</textarea></td>\n";
637         $Ajax->addUpdate($name, $name, $value);
638 }
639
640 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
641 {
642         echo "<tr>";
643         textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
644         echo "</tr>\n";
645 }
646
647 //-----------------------------------------------------------------------------------
648 /*
649 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
650 {
651         global $Ajax;
652
653         default_focus($name);
654         echo "<tr><td>$label</td>\n";
655         echo "<td>";
656
657         if ($value == null)
658                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
659         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
660
661         submit($input_name, $input_value);
662
663         echo "</td></tr>\n";
664         $Ajax->addUpdate($name, $name, $value);
665 }
666 */
667 //-----------------------------------------------------------------------------------
668
669
670 ?>