Fixed many minor bugs, new ones as well as debtor_trans related.
[fa-stable.git] / includes / ui / ui_input.inc
1 <?php
2
3 function get_post($name, $dflt='')
4 {
5         return ((!isset($_POST[$name]) || $_POST[$name] === '') ? $dflt : $_POST[$name]);
6 }
7 //
8 // Sets local POST value and adds Value to ajax posting if needed
9 //
10 /*function set_post($name, $value, $ajax_trigger=true) {
11     global $Ajax;
12
13     $_POST[$name] = $value;
14     if ($ajax_trigger) $Ajax->activate($name);
15 }
16 */
17 //------------------------------------------------------------------------------
18 //    Seek for _POST variable with $prefix.
19 //    If var is found returns variable name with prefix stripped,
20 //    and null or -1 otherwise.
21 //
22 function find_submit($prefix, $numeric=true)
23 {
24
25     foreach($_POST as $postkey=>$postval )
26     {
27                 if (strpos($postkey, $prefix) === 0)
28                 {
29                         $id = substr($postkey, strlen($prefix));
30                         return $numeric ? (int)$id : $id;
31                 }
32     }
33     return $numeric ? -1 : null;
34 }
35
36 //------------------------------------------------------------------------------
37 //
38 //      Read numeric value from user formatted input
39 //
40 function input_num($postname=null)
41 {
42         if (!isset($_POST[$postname]))
43                 return null;
44
45     return user_numeric($_POST[$postname]);
46 }
47
48 //---------------------------------------------------------------------------------
49
50 function hidden($name, $value, $echo=true)
51 {
52         global $Ajax;
53         $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
54         $Ajax->addUpdate($name, $name, $value);
55         if ($echo)
56                 echo $ret."\n";
57         else
58                 return $ret;
59 }
60
61 //---------------------------------------------------------------------------------
62
63 function submit($name, $value, $echo=true, $async=false)
64 {
65         default_focus($name);
66         $submit_str = "<input type=\"submit\" class=\""
67             .($async ? 'ajaxsubmit' : 'inputsubmit')
68             ."\" name=\"$name\"  id=\"$name\" value=\"$value\" >\n";
69         if ($echo)
70                 echo $submit_str;
71         else
72                 return $submit_str;
73 }
74
75 function submit_center($name, $value, $echo=true, $async=false)
76 {
77         echo "<center>";
78         submit($name, $value, $echo, $async);
79         echo "</center>";
80 }
81
82 function submit_center_first($name, $value, $async=false)
83 {
84         echo "<center>";
85         submit($name, $value, true, $async);
86         echo "&nbsp;";
87 }
88
89 function submit_center_last($name, $value, $async=false)
90 {
91         echo "&nbsp;";
92         submit($name, $value, true, $async);
93         echo "</center>";
94 }
95
96 function submit_add_or_update($add=true, $async=false)
97 {
98         if ($add)
99                 submit('ADD_ITEM', _("Save"), true, $async);
100         else
101                 submit('UPDATE_ITEM', _("Update"), true, $async);
102 }
103
104 function submit_add_or_update_center($add=true, $async=false)
105 {
106         echo "<center>";
107         submit_add_or_update($add, $async);
108         echo "</center>";
109 }
110
111 /*
112 function submit_add_or_update_row($add=true)
113 {
114         echo "<tr><td colspan=99 align=center>";
115         submit_add_or_update($add);
116         echo "</td></tr>\n";
117 }
118 */
119 function submit_add_or_update_row($add=true, $right=true, $extra="", $async=false)
120 {
121         echo "<tr>";
122         if ($right)
123                 echo "<td>&nbsp;</td>\n";
124         echo "<td $extra>";
125         submit_add_or_update($add, $async);
126         echo "</td></tr>\n";
127 }
128
129 function submit_cells($name, $value, $extra="", $async=false)
130 {
131         echo "<td $extra>";
132         submit($name, $value, true, $async);
133         echo "</td>\n";
134 }
135
136 function submit_row($name, $value, $right=true, $extra="", $async=false)
137 {
138         echo "<tr>";
139         if ($right)
140                 echo "<td>&nbsp;</td>\n";
141         submit_cells($name, $value, $extra);
142         echo "</tr>\n";
143 }
144 //---------------------------------------------------------------------------------
145
146 function button($name, $value, $onclick)
147 {
148         default_focus($name);
149         echo "<input type=\"button\" class=\"inputsubmit\" name=\"$name\" value=\"$value\" onclick=\"$onclick\" />\n";
150 }
151
152 function button_cell($name, $value, $onclick)
153 {
154         echo "<td>";
155         button($name, $value, $onclick);
156         echo "</td>\n";
157 }
158
159 //-----------------------------------------------------------------------------------
160
161 function check_value($name)
162 {
163         if (!isset($_POST[$name]))
164                 return 0;
165         return 1;
166 }
167
168 function check($label, $name, $value, $submit_on_change=false)
169 {
170         global $Ajax;
171
172         default_focus($name);
173         if ($label)
174                 echo $label . "  ";
175
176         if ($value == null)
177                 $value = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
178         if ($value == 1)
179                 echo "<input checked type='checkbox' name='$name' value='1' ";
180         else
181                 echo "<input type='checkbox' name='$name' value='1'";
182         if ($submit_on_change)
183                 echo " onclick='this.form.submit();' ";
184         echo " >\n";
185         $Ajax->addUpdate($name, $name, $value);
186 }
187
188 function check_cells($label, $name, $value, $submit_on_change=false)
189 {
190         if ($label != null)
191                 echo "<td>$label</td>\n";
192         echo "<td>";
193         check(null, $name, $value, $submit_on_change);
194         echo "</td>";
195 }
196
197 function check_row($label, $name, $value, $submit_on_change=false)
198 {
199         echo "<tr>";
200         check_cells($label, $name, $value, $submit_on_change);
201         echo "</tr>\n";
202 }
203
204 //-----------------------------------------------------------------------------------
205
206 function labelheader_cell($label, $params="")
207 {
208         echo "<td class='tableheader' $params>$label</td>\n";
209 }
210
211 function label_cell($label, $params="", $id=null)
212 {
213     global $Ajax;
214
215         if(isset($id))
216         {
217             $params .= " id='$id'";
218             $Ajax->addUpdate($id, $id, $label);
219         }
220         echo "<td $params>$label</td>\n";
221
222         return $label;
223 }
224
225 function amount_cell($label, $bold=false, $params="", $id=null)
226 {
227         if ($bold)
228                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
229         else
230                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
231 }
232
233 function percent_cell($label, $bold=false, $id=null)
234 {
235         if ($bold)
236                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
237         else
238                 label_cell(percent_format($label), "nowrap align=right", $id);
239 }
240
241 function qty_cell($label, $bold=false, $dec=null, $id=null)
242 {
243         if ($bold)
244                 label_cell("<b>".qty_format($label, $dec)."</b>", "nowrap align=right", $id);
245         else
246                 label_cell(qty_format($label, $dec), "nowrap align=right", $id);
247 }
248
249 function label_cells($label, $value, $params="", $params2="")
250 {
251         if ($label != null)
252                 echo "<td $params>$label</td>\n";
253         echo "<td $params2>$value</td>\n";
254 }
255
256 function label_row($label, $value, $params="", $params2="", $leftfill=0)
257 {
258         echo "<tr>";
259         label_cells($label, $value, $params, $params2);
260         if ($leftfill!=0)
261                 echo "<td colspan=$leftfill></td>";
262         echo "</tr>\n";
263 }
264
265 //-----------------------------------------------------------------------------------
266
267 function text_cells($label, $name, $value, $size="", $max="", $params="", $post_label="", $disabled="")
268 {
269         global $Ajax;
270
271         default_focus($name);
272         if ($label != null)
273                 label_cell($label, $params);
274         echo "<td>";
275
276         if ($value == null)
277                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
278         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">";
279
280         if ($post_label != "")
281                 echo " " . $post_label;
282
283         echo "</td>\n";
284         $Ajax->addUpdate($name, $name, $value);
285 }
286
287 function text_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null)
288 {
289         global $Ajax;
290
291         default_focus($name);
292         if (!isset($_POST[$name]) || $_POST[$name] == "")
293         {
294                 if ($init)
295                         $_POST[$name] = $init;
296                 else
297                         $_POST[$name] = "";
298         }
299         if ($label != null)
300                 label_cell($label, $params);
301
302         if (!isset($max))
303                 $max = $size;
304
305         echo "<td>";
306
307         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\">";
308
309         if ($post_label)
310                 echo " " . $post_label;
311
312         echo "</td>\n";
313         $Ajax->addUpdate($name, $name, $_POST[$name]);
314 }
315
316 function text_row($label, $name, $value, $size, $max, $params="", $post_label="")
317 {
318         echo "<tr>";
319
320         text_cells($label, $name, $value, $size, $max, $params, $post_label);
321
322         echo "</tr>\n";
323 }
324
325 //-----------------------------------------------------------------------------------
326
327 function text_row_ex($label, $name, $size, $max=null, $value=null, $params=null, $post_label=null)
328 {
329         echo "<tr>";
330
331         text_cells_ex($label, $name, $size, $max, $value, $params, $post_label);
332
333         echo "</tr>\n";
334 }
335
336 //-----------------------------------------------------------------------------------
337
338 function date_cells($label, $name, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
339 {
340         global $use_date_picker, $path_to_root;
341         if (!isset($_POST[$name]) || $_POST[$name] == "")
342         {
343                 if (!$init)
344                 {
345                         if ($inc_years == 1001)
346                                 $_POST[$name] = null;
347                         else
348                         {
349                                 $dd = Today();
350                                 if ($inc_days != 0)
351                                         $dd = add_days($dd, $inc_days);
352                                 if ($inc_months != 0)
353                                         $dd = add_months($dd, $inc_months);
354                                 if ($inc_years != 0)
355                                         $dd = add_years($dd, $inc_years);
356                                 $_POST[$name] = $dd;
357                         }
358                 }
359                 else
360                         $_POST[$name] = $init;
361         }
362         if ($use_date_picker)
363                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.forms[0].$name);\">"
364                 . "     <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";
365         else
366                 $post_label = "";
367         text_cells_ex($label, $name, 9, 12, $_POST[$name], $params, $post_label);
368 }
369
370 function date_row($label, $name, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
371 {
372         echo "<tr>";
373         date_cells($label, $name, $init, $inc_days, $inc_months, $inc_years, $params);
374         echo "</tr>\n";
375 }
376
377 //-----------------------------------------------------------------------------------
378
379 function ref_cells($label, $name, $init=null, $params=null)
380 {
381         text_cells_ex($label, $name, 16, 18, $init, $params);
382 }
383
384 //-----------------------------------------------------------------------------------
385
386 function ref_row($label, $name, $init=null)
387 {
388         echo "<tr>";
389         ref_cells($label, $name, $init);
390         echo "</tr>\n";
391 }
392
393 //-----------------------------------------------------------------------------------
394
395 function percent_row($label, $name, $init=null)
396 {
397
398         if (!isset($_POST[$name]) || $_POST[$name]=="")
399         {
400                 $_POST[$name] = $init== null ? '' : $init;
401         }
402
403         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
404 }
405
406 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
407 {
408         global $Ajax;
409
410         if (!isset($dec))
411                 $dec = user_price_dec();
412         if (!isset($_POST[$name]) || $_POST[$name] == "")
413         {
414                 if ($init)
415                         $_POST[$name] = $init;
416                 else
417                         $_POST[$name] = '';
418         }
419         if ($label != null)
420                 label_cell($label, $params);
421
422         if (!isset($max))
423                 $max = $size;
424
425         echo "<td>";
426
427         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
428
429         if ($post_label)
430                 echo " " . $post_label;
431
432         echo "</td>\n";
433         $Ajax->addUpdate($name, $name, $_POST[$name]);
434 }
435
436
437 //-----------------------------------------------------------------------------------
438
439 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
440 {
441         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
442 }
443
444 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
445 {
446         echo "<tr>";
447         amount_cells($label, $name, $init, $params, $post_label, $dec);
448         echo "</tr>\n";
449 }
450
451 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
452 {
453         echo "<tr>";
454         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
455         echo "</tr>\n";
456 }
457
458 //-----------------------------------------------------------------------------------
459
460 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
461 {
462         if (!isset($dec))
463                 $dec = user_qty_dec();
464
465         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
466 }
467
468 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
469 {
470         if (!isset($dec))
471                 $dec = user_qty_dec();
472
473         echo "<tr>";
474         amount_cells($label, $name, $init, $params, $post_label, $dec);
475         echo "</tr>\n";
476 }
477
478 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
479 {
480         if (!isset($dec))
481                 $dec = user_qty_dec();
482
483         echo "<tr>";
484         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
485         echo "</tr>\n";
486 }
487
488 //-----------------------------------------------------------------------------------
489
490 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
491 {
492         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
493 }
494
495 //-----------------------------------------------------------------------------------
496
497 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
498 {
499         if (!isset($dec))
500                 $dec = user_qty_dec();
501         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
502 }
503
504 //-----------------------------------------------------------------------------------
505
506 function textarea_cells($label, $name, $value, $cols, $rows, $params="")
507 {
508         global $Ajax;
509
510         default_focus($name);
511         if ($label != null)
512                 echo "<td $params>$label</td>\n";
513         if ($value == null)
514                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
515         echo "<td><textarea name='$name' cols='$cols' rows='$rows'>$value</textarea></td>\n";
516         $Ajax->addUpdate($name, $name, $value);
517 }
518
519 function textarea_row($label, $name, $value, $cols, $rows, $params="")
520 {
521         echo "<tr>";
522         textarea_cells($label, $name, $value, $cols, $rows, $params);
523         echo "</tr>\n";
524 }
525
526 //-----------------------------------------------------------------------------------
527
528 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
529 {
530         global $Ajax;
531
532         default_focus($name);
533         echo "<tr><td>$label</td>\n";
534         echo "<td>";
535
536         if ($value == null)
537                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
538         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
539
540         submit($input_name, $input_value);
541
542         echo "</td></tr>\n";
543         $Ajax->addUpdate($name, $name, $value);
544 }
545
546 //-----------------------------------------------------------------------------------
547
548
549 ?>