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