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