Fixed right alignement of amount cells.
[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 // Helper function for simple db table editor pages
38 //
39 function simple_page_mode($numeric_id = true)
40 {
41         global $Ajax, $Mode, $selected_id;
42
43         $default = $numeric_id ? -1 : '';
44         $selected_id = get_post('selected_id', $default);
45         foreach (array('ADD_ITEM', 'UPDATE_ITEM', 'RESET') as $m) {
46                 if (isset($_POST[$m])) {
47                         $Ajax->activate('_page_body');
48                         if ($m == 'RESET') 
49                                 $selected_id = $default;
50                         $Mode = $m; return;
51                 }
52         }
53         foreach (array('Edit', 'Delete') as $m) {
54                 foreach ($_POST as $p => $pvar) {
55                         if (strpos($p, $m) === 0) {
56 //                              $selected_id = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
57                                 unset($_POST['_focus']); // focus on first form entry
58                                 $selected_id = quoted_printable_decode(substr($p, strlen($m)));
59                                 $Ajax->activate('_page_body');
60                                 $Mode = $m;
61                                 return;
62                         }
63                 }
64         }
65         $Mode = '';
66 }
67
68 //------------------------------------------------------------------------------
69 //
70 //      Read numeric value from user formatted input
71 //
72 function input_num($postname=null, $dflt=null)
73 {
74         if (!isset($_POST[$postname]))
75                 return $dflt;
76
77     return user_numeric($_POST[$postname]);
78 }
79
80 //---------------------------------------------------------------------------------
81
82 function hidden($name, $value=null, $echo=true)
83 {
84         global $Ajax;
85         
86         if ($value === null) 
87                 $value = get_post($name);
88         
89         $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
90         $Ajax->addUpdate($name, $name, $value);
91         if ($echo)
92                 echo $ret."\n";
93         else
94                 return $ret;
95 }
96
97 //---------------------------------------------------------------------------------
98 //      Submit button.
99 //      $async parameter can have 3 values:
100 //              null - fallback button not visible in js enabled mode
101 //              false - normal submit via form action
102 //              true  - when js is on submition via ajax call
103 //              'process' - ditto with processing indicator in msgbox
104 //
105 function submit($name, $value, $echo=true, $title=false, $async=false)
106 {
107
108         default_focus($name);
109         $submit_str = "<input type=\"submit\" class=\""
110             .($async ? 'ajaxsubmit' : 'inputsubmit')
111                 ."\""
112                 .($async === null ? (in_ajax() ? " style='display:none;'" : ' aspect="fallback"' ): 
113                         ($async === 'process' ? 'aspect="process"' : '') )
114             ." name=\"$name\"  id=\"$name\" value=\"$value\""
115             .($title ? " title='$title'" : '')
116             .">\n";
117         if ($echo)
118                 echo $submit_str;
119         else
120                 return $submit_str;
121 }
122
123 function submit_center($name, $value, $echo=true, $title=false, $async=false)
124 {
125         echo "<center>";
126         submit($name, $value, $echo, $title, $async);
127         echo "</center>";
128 }
129
130 function submit_center_first($name, $value, $title=false, $async=false)
131 {
132         echo "<center>";
133         submit($name, $value, true, $title, $async);
134         echo "&nbsp;";
135 }
136
137 function submit_center_last($name, $value, $title=false, $async=false)
138 {
139         echo "&nbsp;";
140         submit($name, $value, true, $title, $async);
141         echo "</center>";
142 }
143
144 function submit_add_or_update($add=true, $title=false, $async=false)
145 {
146         if ($add)
147                 submit('ADD_ITEM', _("Add new"), true, $title, $async);
148         else {
149                 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
150                 submit('RESET', _("Cancel"), true, $title, $async);
151         }
152 }
153
154 function submit_add_or_update_center($add=true, $title=false, $async=false)
155 {
156         echo "<center>";
157         submit_add_or_update($add, $title, $async);
158         echo "</center>";
159 }
160
161 /*
162 function submit_add_or_update_row($add=true)
163 {
164         echo "<tr><td colspan=99 align=center>";
165         submit_add_or_update($add);
166         echo "</td></tr>\n";
167 }
168 */
169 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
170 {
171         echo "<tr>";
172         if ($right)
173                 echo "<td>&nbsp;</td>\n";
174         echo "<td $extra>";
175         submit_add_or_update($add, $title, $async);
176         echo "</td></tr>\n";
177 }
178
179 function submit_cells($name, $value, $extra="", $title=false, $async=false)
180 {
181         echo "<td $extra>";
182         submit($name, $value, true, $title, $async);
183         echo "</td>\n";
184 }
185
186 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
187 {
188         echo "<tr>";
189         if ($right)
190                 echo "<td>&nbsp;</td>\n";
191         submit_cells($name, $value, $extra, $title, $async);
192         echo "</tr>\n";
193 }
194
195 function submit_return($name, $value, $title=false, $async=false)
196 {
197         if (count($_SESSION['Context'])) {
198                 submit($name, $value, true, $title, $async);
199         }
200 }
201 //---------------------------------------------------------------------------------
202
203 function button($name, $value, $onclick, $title=false)
204 {
205         default_focus($name);
206         echo "<input type=\"button\" class=\"inputsubmit\" name=\"$name\" value=\"$value\" onclick=\"$onclick\""
207             .($title ? " title='$title'" : '')
208             ." />\n";
209 }
210
211 function button_cell($name, $value, $onclick, $title=false)
212 {
213         echo "<td>";
214         button($name, $value, $onclick, $title);
215         echo "</td>\n";
216 }
217
218 //-----------------------------------------------------------------------------------
219
220 function check_value($name)
221 {
222         if (!isset($_POST[$name]))
223                 return 0;
224         return 1;
225 }
226
227 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
228 {
229         global $Ajax;
230
231         default_focus($name);
232         if ($label)
233                 echo $label . "  ";
234
235         if ($value === null)
236                 $value = get_post($name,0);
237
238                 echo "<input"
239                     .($value == 1 ? ' checked':'')
240                     ." type='checkbox' name='$name' value='1'"
241                     .($submit_on_change ? " onclick='JsHttpRequest.request(\"_{$name}_update\", this.form);'" : '')
242                     .($title ? " title='$title'" : '')
243                     ." >\n";
244         $Ajax->addUpdate($name, $name, $value);
245 }
246
247 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
248 {
249         if ($label != null)
250                 echo "<td>$label</td>\n";
251         echo "<td>";
252         check(null, $name, $value, $submit_on_change, $title);
253         echo "</td>";
254 }
255
256 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
257 {
258         echo "<tr>";
259         check_cells($label, $name, $value, $submit_on_change, $title);
260         echo "</tr>\n";
261 }
262
263 //-----------------------------------------------------------------------------------
264
265 function labelheader_cell($label, $params="")
266 {
267         echo "<td class='tableheader' $params>$label</td>\n";
268 }
269
270 function label_cell($label, $params="", $id=null)
271 {
272     global $Ajax;
273
274         if(isset($id))
275         {
276             $params .= " id='$id'";
277             $Ajax->addUpdate($id, $id, $label);
278         }
279         echo "<td $params>$label</td>\n";
280
281         return $label;
282 }
283
284 function amount_cell($label, $bold=false, $params="", $id=null)
285 {
286         if ($bold)
287                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
288         else
289                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
290 }
291
292 function percent_cell($label, $bold=false, $id=null)
293 {
294         if ($bold)
295                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
296         else
297                 label_cell(percent_format($label), "nowrap align=right", $id);
298 }
299 // 2008-06-15. Changed
300 function qty_cell($label, $bold=false, $dec=null, $id=null)
301 {
302         if ($dec == null)
303                 $dec = get_qty_dec();
304         if ($bold)
305                 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
306         else
307                 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
308 }
309
310 function label_cells($label, $value, $params="", $params2="")
311 {
312         if ($label != null)
313                 echo "<td $params>$label</td>\n";
314         echo "<td $params2>$value</td>\n";
315 }
316
317 function label_row($label, $value, $params="", $params2="", $leftfill=0)
318 {
319         echo "<tr>";
320         label_cells($label, $value, $params, $params2);
321         if ($leftfill!=0)
322                 echo "<td colspan=$leftfill></td>";
323         echo "</tr>\n";
324 }
325
326 //-----------------------------------------------------------------------------------
327
328 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
329 {
330         global $Ajax;
331
332         default_focus($name);
333         if ($label != null)
334                 label_cell($label, $params);
335         echo "<td>";
336
337         if ($value === null)
338                 $value = get_post($name);
339         echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
340             .($title ? " title='$title'" : '')
341             .">";
342
343         if ($post_label != "")
344                 echo " " . $post_label;
345
346         echo "</td>\n";
347         $Ajax->addUpdate($name, $name, $value);
348 }
349
350 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
351 {
352         global $Ajax;
353
354         default_focus($name);
355         if (!isset($_POST[$name]) || $_POST[$name] == "")
356         {
357                 if ($init)
358                         $_POST[$name] = $init;
359                 else
360                         $_POST[$name] = "";
361         }
362         if ($label != null)
363                 label_cell($label, $params);
364
365         if (!isset($max))
366                 $max = $size;
367
368         echo "<td>";
369         $class = $submit_on_change ? 'class="searchbox"' : '';
370         echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
371          .($title ? " title='$title'": '')." >";
372
373         if ($post_label)
374                 echo " " . $post_label;
375
376         echo "</td>\n";
377         $Ajax->addUpdate($name, $name, $_POST[$name]);
378 }
379
380 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
381 {
382         echo "<tr>";
383
384         text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
385
386         echo "</tr>\n";
387 }
388
389 //-----------------------------------------------------------------------------------
390
391 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
392 {
393         echo "<tr>";
394
395         text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
396
397         echo "</tr>\n";
398 }
399
400 //-----------------------------------------------------------------------------------
401
402 function date_cells($label, $name, $title = null, $init=null, $inc_days=0, 
403         $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
404 {
405         global $use_date_picker, $path_to_root;
406         if (!isset($_POST[$name]) || $_POST[$name] == "")
407         {
408                 if (!$init)
409                 {
410                         if ($inc_years == 1001)
411                                 $_POST[$name] = null;
412                         else
413                         {
414                                 $dd = Today();
415                                 if ($inc_days != 0)
416                                         $dd = add_days($dd, $inc_days);
417                                 if ($inc_months != 0)
418                                         $dd = add_months($dd, $inc_months);
419                                 if ($inc_years != 0)
420                                         $dd = add_years($dd, $inc_years);
421                                 $_POST[$name] = $dd;
422                         }
423                 }
424                 else
425                         $_POST[$name] = $init;
426         }
427         if ($use_date_picker)
428                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
429                 . "     <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";
430         else
431                 $post_label = "";
432         text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label, $submit_on_change);
433 }
434
435 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0, 
436         $inc_years=0, $params=null, $submit_on_change=false)
437 {
438         echo "<tr>";
439         date_cells($label, $name, $title, $init, $inc_days, $inc_months, 
440                 $inc_years, $params, $submit_on_change);
441         echo "</tr>\n";
442 }
443
444 //-----------------------------------------------------------------------------------
445
446 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
447 {
448         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
449 }
450
451 //-----------------------------------------------------------------------------------
452
453 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
454 {
455         echo "<tr>";
456         ref_cells($label, $name, $title, $init, null, $submit_on_change);
457         echo "</tr>\n";
458 }
459
460 //-----------------------------------------------------------------------------------
461
462 function percent_row($label, $name, $init=null)
463 {
464
465         if (!isset($_POST[$name]) || $_POST[$name]=="")
466         {
467                 $_POST[$name] = $init == null ? '' : $init;
468         }
469
470         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
471 }
472
473 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
474 {
475         global $Ajax;
476
477         if (!isset($dec))
478                 $dec = user_price_dec();
479         if (!isset($_POST[$name]) || $_POST[$name] == "")
480         {
481                 if ($init !== null)
482                         $_POST[$name] = $init;
483                 else
484                         $_POST[$name] = '';
485         }
486         if ($label != null)
487                 label_cell($label, $params);
488
489         if (!isset($max))
490                 $max = $size;
491
492         echo "<td align='right'>";
493
494         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
495
496         if ($post_label)
497                 echo " " . $post_label;
498
499         echo "</td>\n";
500         $Ajax->addUpdate($name, $name, $_POST[$name]);
501         $Ajax->addAssign($name, $name, 'dec', $dec);
502 }
503
504
505 //-----------------------------------------------------------------------------------
506
507 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
508 {
509         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
510 }
511
512 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
513 {
514         echo "<tr>";
515         amount_cells($label, $name, $init, $params, $post_label, $dec);
516         echo "</tr>\n";
517 }
518
519 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
520 {
521         echo "<tr>";
522         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
523         echo "</tr>\n";
524 }
525
526 //-----------------------------------------------------------------------------------
527
528 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
529 {
530         if (!isset($dec))
531                 $dec = user_qty_dec();
532
533         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
534 }
535
536 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
537 {
538         if (!isset($dec))
539                 $dec = user_qty_dec();
540
541         echo "<tr>";
542         amount_cells($label, $name, $init, $params, $post_label, $dec);
543         echo "</tr>\n";
544 }
545
546 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
547 {
548         if (!isset($dec))
549                 $dec = user_qty_dec();
550
551         echo "<tr>";
552         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
553         echo "</tr>\n";
554 }
555
556 //-----------------------------------------------------------------------------------
557
558 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
559 {
560         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
561 }
562
563 //-----------------------------------------------------------------------------------
564
565 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
566 {
567         if (!isset($dec))
568                 $dec = user_qty_dec();
569         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
570 }
571
572 //-----------------------------------------------------------------------------------
573
574 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
575 {
576         global $Ajax;
577
578         default_focus($name);
579         if ($label != null)
580                 echo "<td $params>$label</td>\n";
581         if ($value == null)
582                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
583         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
584         .($title ? " title='$title'" : '')
585         .">$value</textarea></td>\n";
586         $Ajax->addUpdate($name, $name, $value);
587 }
588
589 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
590 {
591         echo "<tr>";
592         textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
593         echo "</tr>\n";
594 }
595
596 //-----------------------------------------------------------------------------------
597 /*
598 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
599 {
600         global $Ajax;
601
602         default_focus($name);
603         echo "<tr><td>$label</td>\n";
604         echo "<td>";
605
606         if ($value == null)
607                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
608         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
609
610         submit($input_name, $input_value);
611
612         echo "</td></tr>\n";
613         $Ajax->addUpdate($name, $name, $value);
614 }
615 */
616 //-----------------------------------------------------------------------------------
617
618
619 ?>