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