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