Support for calling external pages during document entry (e.g. for adding/viewing...
[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, $inc_months=0, $inc_years=0, $params=null)
403 {
404         global $use_date_picker, $path_to_root;
405         if (!isset($_POST[$name]) || $_POST[$name] == "")
406         {
407                 if (!$init)
408                 {
409                         if ($inc_years == 1001)
410                                 $_POST[$name] = null;
411                         else
412                         {
413                                 $dd = Today();
414                                 if ($inc_days != 0)
415                                         $dd = add_days($dd, $inc_days);
416                                 if ($inc_months != 0)
417                                         $dd = add_months($dd, $inc_months);
418                                 if ($inc_years != 0)
419                                         $dd = add_years($dd, $inc_years);
420                                 $_POST[$name] = $dd;
421                         }
422                 }
423                 else
424                         $_POST[$name] = $init;
425         }
426         if ($use_date_picker)
427                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
428                 . "     <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";
429         else
430                 $post_label = "";
431         text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label);
432 }
433
434 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0, $inc_years=0, $params=null)
435 {
436         echo "<tr>";
437         date_cells($label, $name, $title, $init, $inc_days, $inc_months, $inc_years, $params);
438         echo "</tr>\n";
439 }
440
441 //-----------------------------------------------------------------------------------
442
443 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
444 {
445         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
446 }
447
448 //-----------------------------------------------------------------------------------
449
450 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
451 {
452         echo "<tr>";
453         ref_cells($label, $name, $title, $init, null, $submit_on_change);
454         echo "</tr>\n";
455 }
456
457 //-----------------------------------------------------------------------------------
458
459 function percent_row($label, $name, $init=null)
460 {
461
462         if (!isset($_POST[$name]) || $_POST[$name]=="")
463         {
464                 $_POST[$name] = $init == null ? '' : $init;
465         }
466
467         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
468 }
469
470 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
471 {
472         global $Ajax;
473
474         if (!isset($dec))
475                 $dec = user_price_dec();
476         if (!isset($_POST[$name]) || $_POST[$name] == "")
477         {
478                 if ($init !== null)
479                         $_POST[$name] = $init;
480                 else
481                         $_POST[$name] = '';
482         }
483         if ($label != null)
484                 label_cell($label, $params);
485
486         if (!isset($max))
487                 $max = $size;
488
489         echo "<td>";
490
491         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
492
493         if ($post_label)
494                 echo " " . $post_label;
495
496         echo "</td>\n";
497         $Ajax->addUpdate($name, $name, $_POST[$name]);
498         $Ajax->addAssign($name, $name, 'dec', $dec);
499 }
500
501
502 //-----------------------------------------------------------------------------------
503
504 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
505 {
506         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
507 }
508
509 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
510 {
511         echo "<tr>";
512         amount_cells($label, $name, $init, $params, $post_label, $dec);
513         echo "</tr>\n";
514 }
515
516 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
517 {
518         echo "<tr>";
519         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
520         echo "</tr>\n";
521 }
522
523 //-----------------------------------------------------------------------------------
524
525 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
526 {
527         if (!isset($dec))
528                 $dec = user_qty_dec();
529
530         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
531 }
532
533 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
534 {
535         if (!isset($dec))
536                 $dec = user_qty_dec();
537
538         echo "<tr>";
539         amount_cells($label, $name, $init, $params, $post_label, $dec);
540         echo "</tr>\n";
541 }
542
543 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
544 {
545         if (!isset($dec))
546                 $dec = user_qty_dec();
547
548         echo "<tr>";
549         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
550         echo "</tr>\n";
551 }
552
553 //-----------------------------------------------------------------------------------
554
555 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
556 {
557         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
558 }
559
560 //-----------------------------------------------------------------------------------
561
562 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
563 {
564         if (!isset($dec))
565                 $dec = user_qty_dec();
566         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
567 }
568
569 //-----------------------------------------------------------------------------------
570
571 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
572 {
573         global $Ajax;
574
575         default_focus($name);
576         if ($label != null)
577                 echo "<td $params>$label</td>\n";
578         if ($value == null)
579                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
580         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
581         .($title ? " title='$title'" : '')
582         .">$value</textarea></td>\n";
583         $Ajax->addUpdate($name, $name, $value);
584 }
585
586 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
587 {
588         echo "<tr>";
589         textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
590         echo "</tr>\n";
591 }
592
593 //-----------------------------------------------------------------------------------
594 /*
595 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
596 {
597         global $Ajax;
598
599         default_focus($name);
600         echo "<tr><td>$label</td>\n";
601         echo "<td>";
602
603         if ($value == null)
604                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
605         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
606
607         submit($input_name, $input_value);
608
609         echo "</td></tr>\n";
610         $Ajax->addUpdate($name, $name, $value);
611 }
612 */
613 //-----------------------------------------------------------------------------------
614
615
616 ?>