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