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