Handle hostnames with upper-case letters
[webmin.git] / ui-lib.pl
1 use vars qw($theme_no_table $ui_radio_selector_donejs $module_name 
2             $ui_multi_select_donejs);
3
4 =head1 ui-lib.pl
5
6 Common functions for generating HTML for Webmin user interface elements.
7 Some example code :
8
9  use WebminCore;
10  init_config();
11  ui_print_header(undef, 'My Module', '');
12
13  print ui_form_start('save.cgi');
14  print ui_table_start('My form', undef, 2);
15
16  print ui_table_row('Enter your name',
17         ui_textbox('name', undef, 40));
18
19  print ui_table_end();
20  print ui_form_end([ [ undef, 'Save' ] ]);
21
22  ui_print_footer('/', 'Webmin index');
23
24 =cut
25
26 ####################### table generation functions
27
28 =head2 ui_table_start(heading, [tabletags], [cols], [&default-tds], [right-heading])
29
30 Returns HTML for the start of a form block into which labelled inputs can
31 be placed. By default this is implemented as a table with another table inside
32 it, but themes may override this with their own layout.
33
34 The parameters are :
35
36 =item heading - Text to show at the top of the form.
37
38 =item tabletags - HTML attributes to put in the outer <table>, typically something like width=100%.
39
40 =item cols - Desired number of columns for labels and fields. Defaults to 4, but can be 2 for forms with lots of wide inputs.
41
42 =item default-tds - An optional array reference of HTML attributes for the <td> tags in each row of the table.
43
44 =item right-heading - HTML to appear in the heading, aligned to the right.
45
46 =cut
47 sub ui_table_start
48 {
49 return &theme_ui_table_start(@_) if (defined(&theme_ui_table_start));
50 my ($heading, $tabletags, $cols, $tds, $rightheading) = @_;
51 if (defined($main::ui_table_cols)) {
52         # Push on stack, for nested call
53         push(@main::ui_table_cols_stack, $main::ui_table_cols);
54         push(@main::ui_table_pos_stack, $main::ui_table_pos);
55         push(@main::ui_table_default_tds_stack, $main::ui_table_default_tds);
56         }
57 my $colspan = 1;
58 my $rv;
59 $rv .= "<table class='ui_table' border $tabletags>\n";
60 if (defined($heading) || defined($rightheading)) {
61         $rv .= "<tr $tb class='ui_table_head'>";
62         if (defined($heading)) {
63                 $rv .= "<td><b>$heading</b></td>"
64                 }
65         if (defined($rightheading)) {
66                 $rv .= "<td align=right>$rightheading</td>";
67                 $colspan++;
68                 }
69         $rv .= "</tr>\n";
70         }
71 $rv .= "<tr $cb class='ui_table_body'> <td colspan=$colspan>".
72        "<table width=100%>\n";
73 $main::ui_table_cols = $cols || 4;
74 $main::ui_table_pos = 0;
75 $main::ui_table_default_tds = $tds;
76 return $rv;
77 }
78
79 =head2 ui_table_end
80
81 Returns HTML for the end of a block started by ui_table_start.
82
83 =cut
84 sub ui_table_end
85 {
86 return &theme_ui_table_end(@_) if (defined(&theme_ui_table_end));
87 my $rv;
88 if ($main::ui_table_cols == 4 && $main::ui_table_pos) {
89         # Add an empty block to balance the table
90         $rv .= &ui_table_row(" ", " ");
91         }
92 if (@main::ui_table_cols_stack) {
93         $main::ui_table_cols = pop(@main::ui_table_cols_stack);
94         $main::ui_table_pos = pop(@main::ui_table_pos_stack);
95         $main::ui_table_default_tds = pop(@main::ui_table_default_tds_stack);
96         }
97 else {
98         $main::ui_table_cols = undef;
99         $main::ui_table_pos = undef;
100         $main::ui_table_default_tds = undef;
101         }
102 $rv .= "</table></td></tr></table>\n";
103 return $rv;
104 }
105
106 =head2 ui_table_row(label, value, [cols], [&td-tags])
107
108 Returns HTML for a row in a table started by ui_table_start, with a 1-column
109 label and 1+ column value. The parameters are :
110
111 =item label - Label for the input field. If this is undef, no label is displayed.
112
113 =item value - HTML for the input part of the row.
114
115 =item cols - Number of columns the value should take up, defaulting to 1.
116
117 =item td-tags - Array reference of HTML attributes for the <td> tags in this row.
118
119 =cut
120 sub ui_table_row
121 {
122 return &theme_ui_table_row(@_) if (defined(&theme_ui_table_row));
123 my ($label, $value, $cols, $tds) = @_;
124 $cols ||= 1;
125 $tds ||= $main::ui_table_default_tds;
126 my $rv;
127 if ($main::ui_table_pos+$cols+1 > $main::ui_table_cols &&
128     $main::ui_table_pos != 0) {
129         # If the requested number of cols won't fit in the number
130         # remaining, start a new row
131         $rv .= "</tr>\n";
132         $main::ui_table_pos = 0;
133         }
134 $rv .= "<tr class='ui_table_row'>\n"
135         if ($main::ui_table_pos%$main::ui_table_cols == 0);
136 $rv .= "<td valign=top $tds->[0] class='ui_label'><b>$label</b></td>\n"
137         if (defined($label));
138 $rv .= "<td valign=top colspan=$cols $tds->[1] class='ui_value'>$value</td>\n";
139 $main::ui_table_pos += $cols+(defined($label) ? 1 : 0);
140 if ($main::ui_table_pos%$main::ui_table_cols == 0) {
141         $rv .= "</tr>\n";
142         $main::ui_table_pos = 0;
143         }
144 return $rv;
145 }
146
147 =head2 ui_table_hr
148
149 Returns HTML for a row in a block started by ui_table_row, with a horizontal
150 line inside it to separate sections.
151
152 =cut
153 sub ui_table_hr
154 {
155 return &theme_ui_table_hr(@_) if (defined(&theme_ui_table_hr));
156 my $rv;
157 if ($ui_table_pos) {
158         $rv .= "</tr>\n";
159         $ui_table_pos = 0;
160         }
161 $rv .= "<tr class='ui_table_hr'> ".
162        "<td colspan=$main::ui_table_cols><hr></td> </tr>\n";
163 return $rv;
164 }
165
166 =head2 ui_table_span(text)
167
168 Outputs a table row that spans the whole table, and contains the given text.
169
170 =cut
171 sub ui_table_span
172 {
173 my ($text) = @_;
174 return &theme_ui_table_hr(@_) if (defined(&theme_ui_table_hr));
175 my $rv;
176 if ($ui_table_pos) {
177         $rv .= "</tr>\n";
178         $ui_table_pos = 0;
179         }
180 $rv .= "<tr class='ui_table_span'> ".
181        "<td colspan=$main::ui_table_cols>$text</td> </tr>\n";
182 return $rv;
183 }
184
185 =head2 ui_columns_start(&headings, [width-percent], [noborder], [&tdtags], [heading])
186
187 Returns HTML for the start of a multi-column table, with the given headings.
188 The parameters are :
189
190 =item headings - An array reference of headers for the table's columns.
191
192 =item width-percent - Desired width as a percentage, or undef to let the browser decide.
193
194 =item noborder - Set to 1 if the table should not have a border.
195
196 =item tdtags - An optional reference to an array of HTML attributes for the table's <td> tags.
197
198 =item heading - An optional heading to put above the table.
199
200 =cut
201 sub ui_columns_start
202 {
203 return &theme_ui_columns_start(@_) if (defined(&theme_ui_columns_start));
204 my ($heads, $width, $noborder, $tdtags, $title) = @_;
205 my $rv;
206 $rv .= "<table".($noborder ? "" : " border").
207                 (defined($width) ? " width=$width%" : "")." class='ui_columns'>\n";
208 if ($title) {
209         $rv .= "<tr $tb class='ui_columns_heading'>".
210                "<td colspan=".scalar(@$heads)."><b>$title</b></td></tr>\n";
211         }
212 $rv .= "<tr $tb class='ui_columns_heads'>\n";
213 my $i;
214 for($i=0; $i<@$heads; $i++) {
215         $rv .= "<td ".$tdtags->[$i]."><b>".
216                ($heads->[$i] eq "" ? "<br>" : $heads->[$i])."</b></td>\n";
217         }
218 $rv .= "</tr>\n";
219 return $rv;
220 }
221
222 =head2 ui_columns_row(&columns, &tdtags)
223
224 Returns HTML for a row in a multi-column table. The parameters are :
225
226 =item columns - Reference to an array containing the HTML to show in the columns for this row.
227
228 =item tdtags - An optional array reference containing HTML attributes for the row's <td> tags.
229
230 =cut
231 sub ui_columns_row
232 {
233 return &theme_ui_columns_row(@_) if (defined(&theme_ui_columns_row));
234 my ($cols, $tdtags) = @_;
235 my $rv;
236 $rv .= "<tr $cb class='ui_columns_row'>\n";
237 my $i;
238 for($i=0; $i<@$cols; $i++) {
239         $rv .= "<td ".$tdtags->[$i].">".
240                ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i])."</td>\n";
241         }
242 $rv .= "</tr>\n";
243 return $rv;
244 }
245
246 =head2 ui_columns_header(&columns, &tdtags)
247
248 Returns HTML for a row in a multi-column table, styled as a header. Parameters
249 are the same as ui_columns_row.
250
251 =cut
252 sub ui_columns_header
253 {
254 return &theme_ui_columns_header(@_) if (defined(&theme_ui_columns_header));
255 my ($cols, $tdtags) = @_;
256 my $rv;
257 $rv .= "<tr $tb class='ui_columns_header'>\n";
258 my $i;
259 for($i=0; $i<@$cols; $i++) {
260         $rv .= "<td ".$tdtags->[$i]."><b>".
261                ($cols->[$i] eq "" ? "<br>" : $cols->[$i])."</b></td>\n";
262         }
263 $rv .= "</tr>\n";
264 return $rv;
265 }
266
267 =head2 ui_checked_columns_row(&columns, &tdtags, checkname, checkvalue, [checked?], [disabled], [tags])
268
269 Returns HTML for a row in a multi-column table, in which the first column 
270 contains a checkbox. The parameters are :
271
272 =item columns - Reference to an array containing the HTML to show in the columns for this row.
273
274 =item tdtags - An optional array reference containing HTML attributes for the row's <td> tags.
275
276 =item checkname - Name for the checkbox input. Should be the same for all rows.
277
278 =item checkvalue - Value for this checkbox input.
279
280 =item checked - Set to 1 if it should be checked by default.
281
282 =item disabled - Set to 1 if the checkbox should be disabled and thus un-clickable.
283
284 =item tags - Extra HTML tags to include in the radio button.
285
286 =cut
287 sub ui_checked_columns_row
288 {
289 return &theme_ui_checked_columns_row(@_) if (defined(&theme_ui_checked_columns_row));
290 my ($cols, $tdtags, $checkname, $checkvalue, $checked, $disabled, $tags) = @_;
291 my $rv;
292 $rv .= "<tr $cb class='ui_checked_columns'>\n";
293 $rv .= "<td class='ui_checked_checkbox' ".$tdtags->[0].">".
294        &ui_checkbox($checkname, $checkvalue, undef, $checked, $tags, $disabled).
295        "</td>\n";
296 my $i;
297 for($i=0; $i<@$cols; $i++) {
298         $rv .= "<td ".$tdtags->[$i+1].">";
299         if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
300                 $rv .= "<label for=\"".
301                         &quote_escape("${checkname}_${checkvalue}")."\">";
302                 }
303         $rv .= ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i]);
304         if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
305                 $rv .= "</label>";
306                 }
307         $rv .= "</td>\n";
308         }
309 $rv .= "</tr>\n";
310 return $rv;
311 }
312
313 =head2 ui_radio_columns_row(&columns, &tdtags, checkname, checkvalue, [checked], [disabled], [tags])
314
315 Returns HTML for a row in a multi-column table, in which the first
316 column is a radio button. The parameters are :
317
318 =item columns - Reference to an array containing the HTML to show in the columns for this row.
319
320 =item tdtags - An optional array reference containing HTML attributes for the row's <td> tags.
321
322 =item checkname - Name for the radio button input. Should be the same for all rows.
323
324 =item checkvalue - Value for this radio button option.
325
326 =item checked - Set to 1 if it should be checked by default.
327
328 =item disabled - Set to 1 if the radio button should be disabled and thus un-clickable.
329
330 =item tags - Extra HTML tags to include in the radio button.
331
332 =cut
333 sub ui_radio_columns_row
334 {
335 return &theme_ui_radio_columns_row(@_) if (defined(&theme_ui_radio_columns_row));
336 my ($cols, $tdtags, $checkname, $checkvalue, $checked, $dis, $tags) = @_;
337 my $rv;
338 $rv .= "<tr $cb class='ui_radio_columns'>\n";
339 $rv .= "<td class='ui_radio_radio' ".$tdtags->[0].">".
340     &ui_oneradio($checkname, $checkvalue, "", $checked, undef, $dis)."</td>\n";
341 my $i;
342 for($i=0; $i<@$cols; $i++) {
343         $rv .= "<td ".$tdtags->[$i+1].">";
344         if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
345                 $rv .= "<label for=\"".
346                         &quote_escape("${checkname}_${checkvalue}")."\">";
347                 }
348         $rv .= ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i]);
349         if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
350                 $rv .= "</label>";
351                 }
352         $rv .= "</td>\n";
353         }
354 $rv .= "</tr>\n";
355 return $rv;
356 }
357
358 =head2 ui_columns_end
359
360 Returns HTML to end a table started by ui_columns_start.
361
362 =cut
363 sub ui_columns_end
364 {
365 return &theme_ui_columns_end(@_) if (defined(&theme_ui_columns_end));
366 return "</table>\n";
367 }
368
369 =head2 ui_columns_table(&headings, width-percent, &data, &types, no-sort, title, empty-msg)
370
371 Returns HTML for a complete table, typically generated internally by
372 ui_columns_start, ui_columns_row and ui_columns_end. The parameters are :
373
374 =item headings - An array ref of heading HTML.
375
376 =item width-percent - Preferred total width
377
378 =item data - A 2x2 array ref of table contents. Each can either be a simple string, or a hash ref like :
379
380   { 'type' => 'group', 'desc' => 'Some section title' }
381   { 'type' => 'string', 'value' => 'Foo', 'colums' => 3,
382     'nowrap' => 1 }
383   { 'type' => 'checkbox', 'name' => 'd', 'value' => 'foo',
384     'label' => 'Yes', 'checked' => 1, 'disabled' => 1 }
385   { 'type' => 'radio', 'name' => 'd', 'value' => 'foo', ... }
386
387 =item types - An array ref of data types, such as 'string', 'number', 'bytes' or 'date'
388
389 =item no-sort - Set to 1 to disable sorting by theme.
390
391 =item title - Text to appear above the table.
392
393 =item empty-msg - Message to display if no data.
394
395 =cut
396 sub ui_columns_table
397 {
398 return &theme_ui_columns_table(@_) if (defined(&theme_ui_columns_table));
399 my ($heads, $width, $data, $types, $nosort, $title, $emptymsg) = @_;
400 my $rv;
401
402 # Just show empty message if no data
403 if ($emptymsg && !@$data) {
404         $rv .= &ui_subheading($title) if ($title);
405         $rv .= "<span class='ui_emptymsg'><b>$emptymsg</b></span><p>\n";
406         return $rv;
407         }
408
409 # Are there any checkboxes in each column? If so, make those columns narrow
410 my @tds = map { "valign=top" } @$heads;
411 my $maxwidth = 0;
412 foreach my $r (@$data) {
413         my $cc = 0;
414         foreach my $c (@$r) {
415                 if (ref($c) &&
416                     ($c->{'type'} eq 'checkbox' || $c->{'type'} eq 'radio')) {
417                         $tds[$cc] .= " width=5" if ($tds[$cc] !~ /width=/);
418                         }
419                 $cc++;
420                 }
421         $maxwidth = $cc if ($cc > $maxwidth);
422         }
423 $rv .= &ui_columns_start($heads, $width, 0, \@tds, $title);
424
425 # Add the data rows
426 foreach my $r (@$data) {
427         my $c0;
428         if (ref($r->[0]) && ($r->[0]->{'type'} eq 'checkbox' ||
429                              $r->[0]->{'type'} eq 'radio')) {
430                 # First column is special
431                 $c0 = $r->[0];
432                 $r = [ @$r[1..(@$r-1)] ];
433                 }
434         # Turn data into HTML
435         my @rtds = @tds;
436         my @cols;
437         my $cn = 0;
438         $cn++ if ($c0);
439         foreach my $c (@$r) {
440                 if (!ref($c)) {
441                         # Plain old string
442                         push(@cols, $c);
443                         }
444                 elsif ($c->{'type'} eq 'checkbox') {
445                         # Checkbox in non-first column
446                         push(@cols, &ui_checkbox($c->{'name'}, $c->{'value'},
447                                                  $c->{'label'}, $c->{'checked'},
448                                                  $c->{'tags'},
449                                                  $c->{'disabled'}));
450                         }
451                 elsif ($c->{'type'} eq 'radio') {
452                         # Radio button in non-first column
453                         push(@cols, &ui_oneradio($c->{'name'}, $c->{'value'},
454                                                  $c->{'label'}, $c->{'checked'},
455                                                  $c->{'tags'},
456                                                  $c->{'disabled'}));
457                         }
458                 elsif ($c->{'type'} eq 'group') {
459                         # Header row that spans whole table
460                         $rv .= &ui_columns_header([ $c->{'desc'} ],
461                                                   [ "colspan=$width" ]);
462                         next;
463                         }
464                 elsif ($c->{'type'} eq 'string') {
465                         # A string, which might be special
466                         push(@cols, $c->{'value'});
467                         if ($c->{'columns'} > 1) {
468                                 splice(@rtds, $cn, $c->{'columns'},
469                                        "colspan=".$c->{'columns'});
470                                 }
471                         if ($c->{'nowrap'}) {
472                                 $rtds[$cn] .= " nowrap";
473                                 }
474                         }
475                 $cn++;
476                 }
477         # Add the row
478         if (!$c0) {
479                 $rv .= &ui_columns_row(\@cols, \@rtds);
480                 }
481         elsif ($c0->{'type'} eq 'checkbox') {
482                 $rv .= &ui_checked_columns_row(\@cols, \@rtds, $c0->{'name'},
483                                                $c0->{'value'}, $c0->{'checked'},
484                                                $c0->{'disabled'},
485                                                $c0->{'tags'});
486                 }
487         elsif ($c0->{'type'} eq 'radio') {
488                 $rv .= &ui_radio_columns_row(\@cols, \@rtds, $c0->{'name'},
489                                              $c0->{'value'}, $c0->{'checked'},
490                                              $c0->{'disabled'},
491                                              $c0->{'tags'});
492                 }
493         }
494
495 $rv .= &ui_columns_end();
496 return $rv;
497 }
498
499 =head2 ui_form_columns_table(cgi, &buttons, select-all, &otherlinks, &hiddens, &headings, width-percent, &data, &types, no-sort, title, empty-msg, form-no)
500
501 Similar to ui_columns_table, but wrapped in a form. Parameters are :
502
503 =item cgi - URL to submit the form to.
504
505 =item buttons - An array ref of buttons at the end of the form, similar to that taken by ui_form_end.
506
507 =item select-all - If set to 1, include select all / invert links.
508
509 =item otherslinks - An array ref of other links to put at the top of the table, each of which is a 3-element hash ref of url, text and alignment (left or right).
510
511 =item hiddens - An array ref of hidden fields, each of which is a 2-element array ref containing the name and value.
512
513 All other parameters are the same as ui_columns_table.
514
515 =cut
516 sub ui_form_columns_table
517 {
518 return &theme_ui_form_columns_table(@_)
519         if (defined(&theme_ui_form_columns_table));
520 my ($cgi, $buttons, $selectall, $others, $hiddens,
521        $heads, $width, $data, $types, $nosort, $title, $emptymsg, $formno) = @_;
522 my $rv;
523
524 # Build links
525 my @leftlinks = map { "<a href='$_->[0]'>$_->[1]</a>" }
526                        grep { $_->[2] ne 'right' } @$others;
527 my @rightlinks = map { "<a href='$_->[0]'>$_->[1]</a>" }
528                        grep { $_->[2] eq 'right' } @$others;
529 my $links;
530
531 # Add select links
532 if (@$data) {
533         if ($selectall) {
534                 my $cbname;
535                 foreach my $r (@$data) {
536                         foreach my $c (@$r) {
537                                 if (ref($c) && $c->{'type'} eq 'checkbox') {
538                                         $cbname = $c->{'name'};
539                                         last;
540                                         }
541                                 }
542                         }
543                 if ($cbname) {
544                         unshift(@leftlinks, &select_all_link($cbname, $formno),
545                                     &select_invert_link($cbname, $formno));
546                         }
547                 }
548         }
549
550 # Turn to HTML
551 if (@rightlinks) {
552         $links = &ui_grid_table([ &ui_links_row(\@leftlinks),
553                                   &ui_links_row(\@rightlinks) ], 2, 100,
554                                 [ undef, "align=right" ]);
555         }
556 elsif (@leftlinks) {
557         $links = &ui_links_row(\@leftlinks);
558         }
559
560 # Start the form, if we need one
561 if (@$data) {
562         $rv .= &ui_form_start($cgi, "post");
563         foreach my $h (@$hiddens) {
564                 $rv .= &ui_hidden(@$h);
565                 }
566         $rv .= $links;
567         }
568
569 # Add the table
570 $rv .= &ui_columns_table($heads, $width, $data, $types, $nosort, $title,
571                          $emptymsg);
572
573 # Add form end
574 $rv .= $links;
575 if (@$data) {
576         $rv .= &ui_form_end($buttons);
577         }
578
579 return $rv;
580 }
581
582 ####################### form generation functions
583
584 =head2 ui_form_start(script, method, [target], [tags])
585
586 Returns HTML for the start of a a form that submits to some script. The
587 parameters are :
588
589 =item script - CGI script to submit to, like save.cgi.
590
591 =item method - HTTP method, which must be one of 'get', 'post' or 'form-data'. If form-data is used, the target CGI must call ReadParseMime to parse parameters.
592
593 =item target - Optional target window or frame for the form.
594
595 =item tags - Additional HTML attributes for the form tag.
596
597 =cut
598 sub ui_form_start
599 {
600 return &theme_ui_form_start(@_) if (defined(&theme_ui_form_start));
601 my ($script, $method, $target, $tags) = @_;
602 my $rv;
603 $rv .= "<form class='ui_form' action='".&html_escape($script)."' ".
604         ($method eq "post" ? "method=post" :
605          $method eq "form-data" ?
606                 "method=post enctype=multipart/form-data" :
607                 "method=get").
608         ($target ? " target=$target" : "").
609         " ".$tags.
610        ">\n";
611 return $rv;
612 }
613
614 =head2 ui_form_end([&buttons], [width])
615
616 Returns HTML for the end of a form, optionally with a row of submit buttons.
617 These are specified by the buttons parameter, which is an array reference
618 of array refs, with the following elements :
619
620 =item HTML value for the submit input for the button, or undef for none.
621
622 =item Text to appear on the button.
623
624 =item HTML or other inputs to appear after the button.
625
626 =item Set to 1 if the button should be disabled.
627
628 =item Additional HTML attributes to appear inside the button's input tag.
629
630 =cut
631 sub ui_form_end
632 {
633 return &theme_ui_form_end(@_) if (defined(&theme_ui_form_end));
634 my ($buttons, $width) = @_;
635 my $rv;
636 if ($buttons && @$buttons) {
637         $rv .= "<table class='ui_form_end_buttons' ".($width ? " width=$width" : "")."><tr>\n";
638         my $b;
639         foreach $b (@$buttons) {
640                 if (ref($b)) {
641                         $rv .= "<td".(!$width ? "" :
642                                       $b eq $buttons->[0] ? " align=left" :
643                                       $b eq $buttons->[@$buttons-1] ?
644                                         " align=right" : " align=center").">".
645                                &ui_submit($b->[1], $b->[0], $b->[3], $b->[4]).
646                                ($b->[2] ? " ".$b->[2] : "")."</td>\n";
647                         }
648                 elsif ($b) {
649                         $rv .= "<td>$b</td>\n";
650                         }
651                 else {
652                         $rv .= "<td>&nbsp;&nbsp;</td>\n";
653                         }
654                 }
655         $rv .= "</tr></table>\n";
656         }
657 $rv .= "</form>\n";
658 return $rv;
659 }
660
661 =head2 ui_textbox(name, value, size, [disabled?], [maxlength], [tags])
662
663 Returns HTML for a text input box. The parameters are :
664
665 =item name - Name for this input.
666
667 =item value - Initial contents for the text box.
668
669 =item size - Desired width in characters.
670
671 =item disabled - Set to 1 if this text box should be disabled by default.
672
673 =item maxlength - Maximum length of the string the user is allowed to input.
674
675 =item tags - Additional HTML attributes for the <input> tag.
676
677 =cut
678 sub ui_textbox
679 {
680 return &theme_ui_textbox(@_) if (defined(&theme_ui_textbox));
681 my ($name, $value, $size, $dis, $max, $tags) = @_;
682 $size = &ui_max_text_width($size);
683 return "<input class='ui_textbox' name=\"".&quote_escape($name)."\" ".
684        "value=\"".&quote_escape($value)."\" ".
685        "size=$size ".($dis ? "disabled=true" : "").
686        ($max ? " maxlength=$max" : "").
687        " ".$tags.
688        ">";
689 }
690
691 =head2 ui_filebox(name, value, size, [disabled?], [maxlength], [tags], [dir-only])
692
693 Returns HTML for a text box for choosing a file. Parameters are the same
694 as ui_textbox, except for the extra dir-only option which limits the chooser
695 to directories.
696
697 =cut
698 sub ui_filebox
699 {
700 return &theme_ui_filebox(@_) if (defined(&theme_ui_filebox));
701 my ($name, $value, $size, $dis, $max, $tags, $dironly) = @_;
702 return &ui_textbox($name, $value, $size, $dis, $max, $tags)."&nbsp;".
703        &file_chooser_button($name, $dironly);
704 }
705
706 =head2 ui_bytesbox(name, bytes, [size], [disabled?])
707
708 Returns HTML for entering a number of bytes, but with friendly kB/MB/GB
709 options. May truncate values to 2 decimal points! The parameters are :
710
711 =item name - Name for this input.
712
713 =item bytes - Initial number of bytes to show.
714
715 =item size - Desired width of the text box part.
716
717 =item disabled - Set to 1 if this text box should be disabled by default.
718
719 =item tags - Additional HTML attributes for the <input> tag.
720
721 =item defaultunits - Units mode selected by default
722
723 =cut
724 sub ui_bytesbox
725 {
726 my ($name, $bytes, $size, $dis, $tags, $defaultunits) = @_;
727 my $units = 1;
728 if ($bytes eq '' && $defaultunits) {
729         $units = $defaultunits;
730         }
731 elsif ($bytes >= 10*1024*1024*1024*1024) {
732         $units = 1024*1024*1024*1024;
733         }
734 elsif ($bytes >= 10*1024*1024*1024) {
735         $units = 1024*1024*1024;
736         }
737 elsif ($bytes >= 10*1024*1024) {
738         $units = 1024*1024;
739         }
740 elsif ($bytes >= 10*1024) {
741         $units = 1024;
742         }
743 else {
744         $units = 1;
745         }
746 if ($bytes ne "") {
747         $bytes = sprintf("%.2f", ($bytes*1.0)/$units);
748         $bytes =~ s/\.00$//;
749         }
750 $size = &ui_max_text_width($size || 8);
751 return &ui_textbox($name, $bytes, $size, $dis, undef, $tags)." ".
752        &ui_select($name."_units", $units,
753                  [ [ 1, "bytes" ],
754                    [ 1024, "kB" ],
755                    [ 1024*1024, "MB" ],
756                    [ 1024*1024*1024, "GB" ],
757                    [ 1024*1024*1024*1024, "TB" ] ], undef, undef, undef, $dis);
758 }
759
760 =head2 ui_upload(name, size, [disabled?], [tags])
761
762 Returns HTML for a file upload input, for use in a form with the form-data
763 method. The parameters are :
764
765 =item name - Name for this input.
766
767 =item size - Desired width in characters.
768
769 =item disabled - Set to 1 if this text box should be disabled by default.
770
771 =item tags - Additional HTML attributes for the <input> tag.
772
773 =cut
774 sub ui_upload
775 {
776 return &theme_ui_upload(@_) if (defined(&theme_ui_upload));
777 my ($name, $size, $dis, $tags) = @_;
778 $size = &ui_max_text_width($size);
779 return "<input class='ui_upload' type=file name=\"".&quote_escape($name)."\" ".
780        "size=$size ".
781        ($dis ? "disabled=true" : "").
782        ($tags ? " ".$tags : "").">";
783 }
784
785 =head2 ui_password(name, value, size, [disabled?], [maxlength], [tags])
786
787 Returns HTML for a password text input. Parameters are the same as ui_textbox,
788 and behaviour is identical except that the user's input is not visible.
789
790 =cut
791 sub ui_password
792 {
793 return &theme_ui_password(@_) if (defined(&theme_ui_password));
794 my ($name, $value, $size, $dis, $max, $tags) = @_;
795 $size = &ui_max_text_width($size);
796 return "<input class='ui_password' ".
797        "type=password name=\"".&quote_escape($name)."\" ".
798        "value=\"".&quote_escape($value)."\" ".
799        "size=$size ".($dis ? "disabled=true" : "").
800        ($max ? " maxlength=$max" : "").
801        " ".$tags.
802        ">";
803 }
804
805 =head2 ui_hidden(name, value)
806
807 Returns HTML for a hidden field with the given name and value.
808
809 =cut
810 sub ui_hidden
811 {
812 return &theme_ui_hidden(@_) if (defined(&theme_ui_hidden));
813 my ($name, $value) = @_;
814 return "<input class='ui_hidden' type=hidden ".
815        "name=\"".&quote_escape($name)."\" ".
816        "value=\"".&quote_escape($value)."\">\n";
817 }
818
819 =head2 ui_select(name, value|&values, &options, [size], [multiple], [add-if-missing], [disabled?], [javascript])
820
821 Returns HTML for a drop-down menu or multiple selection list. The parameters
822 are :
823
824 =item name - Name for this input.
825
826 =item value - Either a single initial value, or an array reference of values if this is a multi-select list.
827
828 =item options - An array reference of possible options. Each element can either be a scalar, or a two-element array ref containing a submitted value and displayed text.
829
830 =item size - Desired vertical size in rows, which defaults to 1. For multi-select lists, this must be set to something larger.
831
832 =item multiple - Set to 1 for a multi-select list, 0 for single.
833
834 =item add-if-missing - If set to 1, any value that is not in the list of options will be automatically added (and selected).
835
836 =item disabled - Set to 1 to disable this input.
837
838 =item javascript - Additional HTML attributes for the <select> input.
839
840 =cut
841 sub ui_select
842 {
843 return &theme_ui_select(@_) if (defined(&theme_ui_select));
844 my ($name, $value, $opts, $size, $multiple, $missing, $dis, $js) = @_;
845 my $rv;
846 $rv .= "<select class='ui_select' name=\"".&quote_escape($name)."\"".
847        ($size ? " size=$size" : "").
848        ($multiple ? " multiple" : "").
849        ($dis ? " disabled=true" : "")." ".$js.">\n";
850 my ($o, %opt, $s);
851 my %sel = ref($value) ? ( map { $_, 1 } @$value ) : ( $value, 1 );
852 foreach $o (@$opts) {
853         $o = [ $o ] if (!ref($o));
854         $rv .= "<option value=\"".&quote_escape($o->[0])."\"".
855                ($sel{$o->[0]} ? " selected" : "")." ".$o->[2].">".
856                ($o->[1] || $o->[0])."\n";
857         $opt{$o->[0]}++;
858         }
859 foreach $s (keys %sel) {
860         if (!$opt{$s} && $missing) {
861                 $rv .= "<option value=\"".&quote_escape($s)."\"".
862                        "selected>".($s eq "" ? "&nbsp;" : $s)."\n";
863                 }
864         }
865 $rv .= "</select>\n";
866 return $rv;
867 }
868
869 =head2 ui_multi_select(name, &values, &options, size, [add-if-missing], [disabled?], [options-title, values-title], [width])
870
871 Returns HTML for selecting many of many from a list. By default, this is
872 implemented using two <select> lists and Javascript buttons to move elements
873 between them. The resulting input value is \n separated.
874
875 Parameters are :
876
877 =item name - HTML name for this input.
878
879 =item values - An array reference of two-element array refs, containing the submitted values and descriptions of items that are selected by default.
880
881 =item options - An array reference of two-element array refs, containing the submitted values and descriptions of items that the user can select from.
882
883 =item size - Vertical size in rows.
884
885 =item add-if-missing - If set to 1, any entries that are in values but not in options will be added automatically.
886
887 =item disabled - Set to 1 to disable this input by default.
888
889 =item options-title - Optional text to appear above the list of options.
890
891 =item values-title - Optional text to appear above the list of selected values.
892
893 =item width - Optional width of the two lists in pixels.
894
895 =cut
896 sub ui_multi_select
897 {
898 return &theme_ui_multi_select(@_) if (defined(&theme_ui_multi_select));
899 my ($name, $values, $opts, $size, $missing, $dis,
900        $opts_title, $vals_title, $width) = @_;
901 my $rv;
902 my %already = map { $_->[0], $_ } @$values;
903 my $leftover = [ grep { !$already{$_->[0]} } @$opts ];
904 if ($missing) {
905         my %optsalready = map { $_->[0], $_ } @$opts;
906         push(@$opts, grep { !$optsalready{$_->[0]} } @$values);
907         }
908 if (!defined($width)) {
909         $width = "200";
910         }
911 my $wstyle = $width ? "style='width:$width'" : "";
912
913 if (!$main::ui_multi_select_donejs++) {
914         $rv .= &ui_multi_select_javascript();
915         }
916 $rv .= "<table cellpadding=0 cellspacing=0 class='ui_multi_select'>";
917 if (defined($opts_title)) {
918         $rv .= "<tr class='ui_multi_select_heads'> ".
919                "<td><b>$opts_title</b></td> ".
920                "<td></td> <td><b>$vals_title</b></td> </tr>";
921         }
922 $rv .= "<tr class='ui_multi_select_row'>";
923 $rv .= "<td>".&ui_select($name."_opts", [ ], $leftover,
924                          $size, 1, 0, $dis, $wstyle)."</td>\n";
925 $rv .= "<td>".&ui_button("->", $name."_add", $dis,
926                  "onClick='multi_select_move(\"$name\", form, 1)'")."<br>".
927               &ui_button("<-", $name."_remove", $dis,
928                  "onClick='multi_select_move(\"$name\", form, 0)'")."</td>\n";
929 $rv .= "<td>".&ui_select($name."_vals", [ ], $values,
930                          $size, 1, 0, $dis, $wstyle)."</td>\n";
931 $rv .= "</tr></table>\n";
932 $rv .= &ui_hidden($name, join("\n", map { $_->[0] } @$values));
933 return $rv;
934 }
935
936 =head2 ui_multi_select_javascript
937
938 Returns <script> section for left/right select boxes. For internal use only.
939
940 =cut
941 sub ui_multi_select_javascript
942 {
943 return &theme_ui_multiselect_javascript()
944         if (defined(&theme_ui_multiselect_javascript));
945 return <<EOF;
946 <script>
947 // Move an element from the options list to the values list, or vice-versa
948 function multi_select_move(name, f, dir)
949 {
950 var opts = f.elements[name+"_opts"];
951 var vals = f.elements[name+"_vals"];
952 var opts_idx = opts.selectedIndex;
953 var vals_idx = vals.selectedIndex;
954 if (dir == 1 && opts_idx >= 0) {
955         // Moving from options to selected list
956         for(var i=0; i<opts.options.length; i++) {
957                 var o = opts.options[i];
958                 if (o.selected) {
959                         vals.options[vals.options.length] =
960                                 new Option(o.text, o.value);
961                         opts.remove(i);
962                         i--;
963                         }
964                 }
965         }
966 else if (dir == 0 && vals_idx >= 0) {
967         // Moving the other way
968         for(var i=0; i<vals.options.length; i++) {
969                 var o = vals.options[i];
970                 if (o.selected) {
971                         opts.options[opts.options.length] =
972                                 new Option(o.text, o.value);
973                         vals.remove(i);
974                         i--;
975                         }
976                 }
977         }
978 // Fill in hidden field
979 var hid = f.elements[name];
980 if (hid) {
981         var hv = new Array();
982         for(var i=0; i<vals.options.length; i++) {
983                 hv.push(vals.options[i].value);
984                 }
985         hid.value = hv.join("\\n");
986         }
987 }
988 </script>
989 EOF
990 }
991
992 =head2 ui_radio(name, value, &options, [disabled?])
993
994 Returns HTML for a series of radio buttons, of which one can be selected. The
995 parameters are :
996
997 =item name - HTML name for the radio buttons.
998
999 =item value - Value of the button that is selected by default.
1000
1001 =item options - Array ref of radio button options, each of which is an array ref containing the submitted value and description for each button.
1002
1003 =item disabled - Set to 1 to disable all radio buttons by default.
1004
1005 =cut
1006 sub ui_radio
1007 {
1008 return &theme_ui_radio(@_) if (defined(&theme_ui_radio));
1009 my ($name, $value, $opts, $dis) = @_;
1010 my $rv;
1011 my $o;
1012 foreach $o (@$opts) {
1013         my $id = &quote_escape($name."_".$o->[0]);
1014         my $label = $o->[1] || $o->[0];
1015         my $after;
1016         if ($label =~ /^(.*?)((<a\s+href|<input|<select|<textarea)[\000-\377]*)$/i) {
1017                 $label = $1;
1018                 $after = $2;
1019                 }
1020         $rv .= "<input class='ui_radio' type=radio ".
1021                "name=\"".&quote_escape($name)."\" ".
1022                "value=\"".&quote_escape($o->[0])."\"".
1023                ($o->[0] eq $value ? " checked" : "").
1024                ($dis ? " disabled=true" : "").
1025                " id=\"$id\"".
1026                " $o->[2]> <label for=\"$id\">".
1027                $label."</label>".$after."\n";
1028         }
1029 return $rv;
1030 }
1031
1032 =head2 ui_yesno_radio(name, value, [yes], [no], [disabled?])
1033
1034 Like ui_radio, but always displays just two inputs (yes and no). The parameters
1035 are :
1036
1037 =item name - HTML name of the inputs.
1038
1039 =item value - Option selected by default, typically 1 or 0.
1040
1041 =item yes - The value for the yes option, defaulting to 1.
1042
1043 =item no - The value for the no option, defaulting to 0.
1044
1045 =item disabled - Set to 1 to disable all radio buttons by default.
1046
1047 =cut
1048 sub ui_yesno_radio
1049 {
1050 my ($name, $value, $yes, $no, $dis) = @_;
1051 return &theme_ui_yesno_radio(@_) if (defined(&theme_ui_yesno_radio));
1052 $yes = 1 if (!defined($yes));
1053 $no = 0 if (!defined($no));
1054 $value = int($value);
1055 return &ui_radio($name, $value, [ [ $yes, $text{'yes'} ],
1056                                   [ $no, $text{'no'} ] ], $dis);
1057 }
1058
1059 =head2 ui_checkbox(name, value, label, selected?, [tags], [disabled?])
1060
1061 Returns HTML for a single checkbox. Parameters are :
1062
1063 =item name - HTML name of the checkbox.
1064
1065 =item value - Value that will be submitted if it is checked.
1066
1067 =item label - Text to appear next to the checkbox.
1068
1069 =item selected - Set to 1 for it to be checked by default.
1070
1071 =item tags - Additional HTML attributes for the <input> tag.
1072
1073 =item disabled - Set to 1 to disable the checkbox by default.
1074
1075 =cut
1076 sub ui_checkbox
1077 {
1078 return &theme_ui_checkbox(@_) if (defined(&theme_ui_checkbox));
1079 my ($name, $value, $label, $sel, $tags, $dis) = @_;
1080 my $after;
1081 if ($label =~ /^([^<]*)(<[\000-\377]*)$/) {
1082         $label = $1;
1083         $after = $2;
1084         }
1085 return "<input class='ui_checkbox' type=checkbox ".
1086        "name=\"".&quote_escape($name)."\" ".
1087        "value=\"".&quote_escape($value)."\" ".
1088        ($sel ? " checked" : "").($dis ? " disabled=true" : "").
1089        " id=\"".&quote_escape("${name}_${value}")."\"".
1090        " $tags> ".
1091        ($label eq "" ? $after :
1092          "<label for=\"".&quote_escape("${name}_${value}").
1093          "\">$label</label>$after")."\n";
1094 }
1095
1096 =head2 ui_oneradio(name, value, label, selected?, [tags], [disabled?])
1097
1098 Returns HTML for a single radio button. The parameters are :
1099
1100 =item name - HTML name of the radio button.
1101
1102 =item value - Value that will be submitted if it is selected.
1103
1104 =item label - Text to appear next to the button.
1105
1106 =item selected - Set to 1 for it to be selected by default.
1107
1108 =item tags - Additional HTML attributes for the <input> tag.
1109
1110 =item disabled - Set to 1 to disable the radio button by default.
1111
1112 =cut
1113 sub ui_oneradio
1114 {
1115 return &theme_ui_oneradio(@_) if (defined(&theme_ui_oneradio));
1116 my ($name, $value, $label, $sel, $tags, $dis) = @_;
1117 my $id = &quote_escape("${name}_${value}");
1118 my $after;
1119 if ($label =~ /^([^<]*)(<[\000-\377]*)$/) {
1120         $label = $1;
1121         $after = $2;
1122         }
1123 return "<input class='ui_radio' type=radio name=\"".&quote_escape($name)."\" ".
1124        "value=\"".&quote_escape($value)."\" ".
1125        ($sel ? " checked" : "").($dis ? " disabled=true" : "").
1126        " id=\"$id\"".
1127        " $tags> <label for=\"$id\">$label</label>$after\n";
1128 }
1129
1130 =head2 ui_textarea(name, value, rows, cols, [wrap], [disabled?], [tags])
1131
1132 Returns HTML for a multi-line text input. The function parameters are :
1133
1134 =item name - Name for this HTML <textarea>.
1135
1136 =item value - Default value. Multiple lines must be separated by \n.
1137
1138 =item rows - Number of rows, in lines.
1139
1140 =item cols - Number of columns, in characters.
1141
1142 =item wrap - Wrapping mode. Can be one of soft, hard or off.
1143
1144 =item disabled - Set to 1 to disable this text area by default.
1145
1146 =item tags - Additional HTML attributes for the <textarea> tag.
1147
1148 =cut
1149 sub ui_textarea
1150 {
1151 return &theme_ui_textarea(@_) if (defined(&theme_ui_textarea));
1152 my ($name, $value, $rows, $cols, $wrap, $dis, $tags) = @_;
1153 $cols = &ui_max_text_width($cols, 1);
1154 return "<textarea class='ui_textarea' name=\"".&quote_escape($name)."\" ".
1155        "rows=$rows cols=$cols".($wrap ? " wrap=$wrap" : "").
1156        ($dis ? " disabled=true" : "").
1157        ($tags ? " $tags" : "").">".
1158        &html_escape($value).
1159        "</textarea>";
1160 }
1161
1162 =head2 ui_user_textbox(name, value, [form], [disabled?], [tags])
1163
1164 Returns HTML for an input for selecting a Unix user. Parameters are the
1165 same as ui_textbox.
1166
1167 =cut
1168 sub ui_user_textbox
1169 {
1170 return &theme_ui_user_textbox(@_) if (defined(&theme_ui_user_textbox));
1171 return &ui_textbox($_[0], $_[1], 13, $_[3], undef, $_[4])." ".
1172        &user_chooser_button($_[0], 0, $_[2]);
1173 }
1174
1175 =head2 ui_group_textbox(name, value, [form], [disabled?], [tags])
1176
1177 Returns HTML for an input for selecting a Unix group. Parameters are the
1178 same as ui_textbox.
1179
1180 =cut
1181 sub ui_group_textbox
1182 {
1183 return &theme_ui_group_textbox(@_) if (defined(&theme_ui_group_textbox));
1184 return &ui_textbox($_[0], $_[1], 13, $_[3], undef, $_[4])." ".
1185        &group_chooser_button($_[0], 0, $_[2]);
1186 }
1187
1188 =head2 ui_opt_textbox(name, value, size, option1, [option2], [disabled?], [&extra-fields], [max])
1189
1190 Returns HTML for a text field that is optional, implemented by default as
1191 a field with radio buttons next to it. The parameters are :
1192
1193 =item name - HTML name for the text box. The radio buttons will have the same name, but with _def appended.
1194
1195 =item value - Initial value, or undef if you want the default radio button selected initially.
1196
1197 =item size - Width of the text box in characters.
1198
1199 =item option1 - Text for the radio button for selecting that no input is being given, such as 'Default'.
1200
1201 =item option2 - Text for the radio button for selecting that you will provide input.
1202
1203 =item disabled - Set to 1 to disable this input by default.
1204
1205 =item extra-fields - An optional array ref of field names that should be disabled by Javascript when this field is disabled.
1206
1207 =item max - Optional maximum allowed input length, in characters.
1208
1209 =item tags - Additional HTML attributes for the text box
1210
1211 =cut
1212 sub ui_opt_textbox
1213 {
1214 return &theme_ui_opt_textbox(@_) if (defined(&theme_ui_opt_textbox));
1215 my ($name, $value, $size, $opt1, $opt2, $dis, $extra, $max, $tags) = @_;
1216 my $dis1 = &js_disable_inputs([ $name, @$extra ], [ ]);
1217 my $dis2 = &js_disable_inputs([ ], [ $name, @$extra ]);
1218 my $rv;
1219 $size = &ui_max_text_width($size);
1220 $rv .= &ui_radio($name."_def", $value eq '' ? 1 : 0,
1221                  [ [ 1, $opt1, "onClick='$dis1'" ],
1222                    [ 0, $opt2 || " ", "onClick='$dis2'" ] ], $dis)."\n";
1223 $rv .= "<input class='ui_opt_textbox' name=\"".&quote_escape($name)."\" ".
1224        "size=$size value=\"".&quote_escape($value)."\" ".
1225        ($value eq "" || $dis ? "disabled=true" : "").
1226        ($max ? " maxlength=$max" : "").
1227        " ".$tags.
1228        ">\n";
1229 return $rv;
1230 }
1231
1232 =head2 ui_submit(label, [name], [disabled?], [tags])
1233
1234 Returns HTML for a form submit button. Parameters are :
1235
1236 =item label - Text to appear on the button.
1237
1238 =item name - Optional HTML name for the button. Useful if the CGI it submits to needs to know which of several buttons was clicked.
1239
1240 =item disabled - Set to 1 if this button should be disabled by default.
1241
1242 =item tags - Additional HTML attributes for the <input> tag.
1243
1244 =cut
1245 sub ui_submit
1246 {
1247 return &theme_ui_submit(@_) if (defined(&theme_ui_submit));
1248 my ($label, $name, $dis, $tags) = @_;
1249 return "<input class='ui_submit' type=submit".
1250        ($name ne '' ? " name=\"".&quote_escape($name)."\"" : "").
1251        " value=\"".&quote_escape($label)."\"".
1252        ($dis ? " disabled=true" : "").
1253        ($tags ? " ".$tags : "").">\n";
1254                         
1255 }
1256
1257 =head2 ui_reset(label, [disabled?])
1258
1259 Returns HTML for a form reset button, which clears all fields when clicked.
1260 Parameters are :
1261
1262 =item label - Text to appear on the button.
1263
1264 =item disabled - Set to 1 if this button should be disabled by default.
1265
1266 =cut
1267 sub ui_reset
1268 {
1269 return &theme_ui_reset(@_) if (defined(&theme_ui_reset));
1270 my ($label, $dis) = @_;
1271 return "<input type=reset value=\"".&quote_escape($label)."\"".
1272        ($dis ? " disabled=true" : "").">\n";
1273                         
1274 }
1275
1276 =head2 ui_button(label, [name], [disabled?], [tags])
1277
1278 Returns HTML for a form button, which doesn't do anything when clicked unless
1279 you add some Javascript to it. The parameters are :
1280
1281 =item label - Text to appear on the button.
1282
1283 =item name - HTML name for this input.
1284
1285 =item disabled - Set to 1 if this button should be disabled by default.
1286
1287 =item tags - Additional HTML attributes for the <input> tag, typically Javascript inside an onClick attribute.
1288
1289 =cut
1290 sub ui_button
1291 {
1292 return &theme_ui_button(@_) if (defined(&theme_ui_button));
1293 my ($label, $name, $dis, $tags) = @_;
1294 return "<input type=button".
1295        ($name ne '' ? " name=\"".&quote_escape($name)."\"" : "").
1296        " value=\"".&quote_escape($label)."\"".
1297        ($dis ? " disabled=true" : "").
1298        ($tags ? " ".$tags : "").">\n";
1299 }
1300
1301 =head2 ui_date_input(day, month, year, day-name, month-name, year-name, [disabled?])
1302
1303 Returns HTML for a date-selection field, with day, month and year inputs.
1304 The parameters are :
1305
1306 =item day - Initial day of the month.
1307
1308 =item month - Initial month of the year, indexed from 1.
1309
1310 =item year - Initial year, four-digit.
1311
1312 =item day-name - Name of the day input field.
1313
1314 =item month-name - Name of the month select field.
1315
1316 =item year-name - Name of the year input field.
1317
1318 =item disabled - Set to 1 to disable all fields by default.
1319
1320 =cut
1321 sub ui_date_input
1322 {
1323 my ($day, $month, $year, $dayname, $monthname, $yearname, $dis) = @_;
1324 my $rv;
1325 $rv .= "<span class='ui_data'>";
1326 $rv .= &ui_textbox($dayname, $day, 3, $dis);
1327 $rv .= "/";
1328 $rv .= &ui_select($monthname, $month,
1329                   [ map { [ $_, $text{"smonth_$_"} ] } (1 .. 12) ],
1330                   1, 0, 0, $dis);
1331 $rv .= "/";
1332 $rv .= &ui_textbox($yearname, $year, 5, $dis);
1333 $rv .= "</span>";
1334 return $rv;
1335 }
1336
1337 =head2 ui_buttons_start
1338
1339 Returns HTML for the start of a block of action buttoms with descriptions, as
1340 generated by ui_buttons_row. Some example code :
1341
1342   print ui_buttons_start();
1343   print ui_buttons_row('start.cgi', 'Start server',
1344                        'Click this button to start the server process');
1345   print ui_buttons_row('stop.cgi', 'Stop server',
1346                        'Click this button to stop the server process');
1347   print ui_buttons_end();
1348
1349 =cut
1350 sub ui_buttons_start
1351 {
1352 return &theme_ui_buttons_start(@_) if (defined(&theme_ui_buttons_start));
1353 return "<table width=100% class='ui_buttons_table'>\n";
1354 }
1355
1356 =head2 ui_buttons_end
1357
1358 Returns HTML for the end of a block started by ui_buttons_start.
1359
1360 =cut
1361 sub ui_buttons_end
1362 {
1363 return &theme_ui_buttons_end(@_) if (defined(&theme_ui_buttons_end));
1364 return "</table>\n";
1365 }
1366
1367 =head2 ui_buttons_row(script, button-label, description, [hiddens], [after-submit], [before-submit]) 
1368
1369 Returns HTML for a button with a description next to it, and perhaps other
1370 inputs. The parameters are :
1371
1372 =item script - CGI script that this button submits to, like start.cgi.
1373
1374 =item button-label - Text to appear on the button.
1375
1376 =item description - Text to appear next to the button, describing in more detail what it does.
1377
1378 =item hiddens - HTML for hidden fields to include in the form this function generates.
1379
1380 =item after-submit - HTML for text or inputs to appear after the submit button.
1381
1382 =item before-submit - HTML for text or inputs to appear before the submit button.
1383
1384 =cut
1385 sub ui_buttons_row
1386 {
1387 return &theme_ui_buttons_row(@_) if (defined(&theme_ui_buttons_row));
1388 my ($script, $label, $desc, $hiddens, $after, $before) = @_;
1389 return "<form action=$script class='ui_buttons_form'>\n".
1390        $hiddens.
1391        "<tr class='ui_buttons_row'> ".
1392        "<td nowrap width=20% valign=top class=ui_buttons_label>".
1393        ($before ? $before." " : "").
1394        &ui_submit($label).($after ? " ".$after : "")."</td>\n".
1395        "<td valign=top width=80% valign=top class=ui_buttons_value>".
1396        $desc."</td> </tr>\n".
1397        "</form>\n";
1398 }
1399
1400 =head2 ui_buttons_hr([title])
1401
1402 Returns HTML for a separator row, for use inside a ui_buttons_start block.
1403
1404 =cut
1405 sub ui_buttons_hr
1406 {
1407 my ($title) = @_;
1408 return &theme_ui_buttons_hr(@_) if (defined(&theme_ui_buttons_hr));
1409 if ($title) {
1410         return "<tr class='ui_buttons_hr'> <td colspan=2><table cellpadding=0 cellspacing=0 width=100%><tr> <td width=50%><hr></td> <td nowrap>$title</td> <td width=50%><hr></td> </tr></table></td> </tr>\n";
1411         }
1412 else {
1413         return "<tr class='ui_buttons_hr'> <td colspan=2><hr></td> </tr>\n";
1414         }
1415 }
1416
1417 ####################### header and footer functions
1418
1419 =head2 ui_post_header([subtext])
1420
1421 Returns HTML to appear directly after a standard header() call. This is never
1422 called directly - instead, ui_print_header calls it. But it can be overridden
1423 by themes.
1424
1425 =cut
1426 sub ui_post_header
1427 {
1428 return &theme_ui_post_header(@_) if (defined(&theme_ui_post_header));
1429 my ($text) = @_;
1430 my $rv;
1431 $rv .= "<center class='ui_post_header'><font size=+1>$text</font></center>\n" if (defined($text));
1432 if (!$tconfig{'nohr'} && !$tconfig{'notophr'}) {
1433         $rv .= "<hr id='post_header_hr'>\n";
1434         }
1435 return $rv;
1436 }
1437
1438 =head2 ui_pre_footer
1439
1440 Returns HTML to appear directly before a standard footer() call. This is never
1441 called directly - instead, ui_print_footer calls it. But it can be overridden
1442 by themes.
1443
1444 =cut
1445 sub ui_pre_footer
1446 {
1447 return &theme_ui_pre_footer(@_) if (defined(&theme_ui_pre_footer));
1448 my $rv;
1449 if (!$tconfig{'nohr'} && !$tconfig{'nobottomhr'}) {
1450         $rv .= "<hr id='pre_footer_hr'>\n";
1451         }
1452 return $rv;
1453 }
1454
1455 =head2 ui_print_header(subtext, image, [help], [config], [nomodule], [nowebmin], [rightside], [head-stuff], [body-stuff], [below])
1456
1457 Print HTML for a header with the post-header line. The args are the same
1458 as those passed to header(), defined in web-lib-funcs.pl, with the addition
1459 of the subtext parameter :
1460
1461 =item subtext - Text to display below the title
1462
1463 =item title - The text to show at the top of the page
1464
1465 =item image - An image to show instead of the title text. This is typically left blank.
1466
1467 =item help - If set, this is the name of a help page that will be linked to in the title.
1468
1469 =item config - If set to 1, the title will contain a link to the module's config page.
1470
1471 =item nomodule - If set to 1, there will be no link in the title section to the module's index.
1472
1473 =item nowebmin - If set to 1, there will be no link in the title section to the Webmin index.
1474
1475 =item rightside - HTML to be shown on the right-hand side of the title. Can contain multiple lines, separated by <br>. Typically this is used for links to stop, start or restart servers.
1476
1477 =item head-stuff - HTML to be included in the <head> section of the page.
1478
1479 =item body-stuff - HTML attributes to be include in the <body> tag.
1480
1481 =item below - HTML to be displayed below the title. Typically this is used for application or server version information.
1482
1483
1484
1485 =cut
1486 sub ui_print_header
1487 {
1488 &load_theme_library();
1489 return &theme_ui_print_header(@_) if (defined(&theme_ui_print_header));
1490 my ($text, @args) = @_;
1491 &header(@args);
1492 print &ui_post_header($text);
1493 }
1494
1495 =head2 ui_print_unbuffered_header(subtext, args...)
1496
1497 Like ui_print_header, but ensures that output for this page is not buffered
1498 or contained in a table. This should be called by scripts that are producing
1499 output while performing some long-running process.
1500
1501 =cut
1502 sub ui_print_unbuffered_header
1503 {
1504 &load_theme_library();
1505 return &theme_ui_print_unbuffered_header(@_) if (defined(&theme_ui_print_unbuffered_header));
1506 $| = 1;
1507 $theme_no_table = 1;
1508 &ui_print_header(@_);
1509 }
1510
1511 =head2 ui_print_footer(args...)
1512
1513 Print HTML for a footer with the pre-footer line. Args are the same as those
1514 passed to footer().
1515
1516 =cut
1517 sub ui_print_footer
1518 {
1519 return &theme_ui_print_footer(@_) if (defined(&theme_ui_print_footer));
1520 my @args = @_;
1521 print &ui_pre_footer();
1522 &footer(@args);
1523 }
1524
1525 =head2 ui_config_link(text, &subs)
1526
1527 Returns HTML for a module config link. The first non-null sub will be
1528 replaced with the appropriate URL for the module's config page.
1529
1530 =cut
1531 sub ui_config_link
1532 {
1533 return &theme_ui_config_link(@_) if (defined(&theme_ui_config_link));
1534 my ($text, $subs) = @_;
1535 my @subs = map { $_ || "../config.cgi?$module_name" }
1536                   ($subs ? @$subs : ( undef ));
1537 return "<p>".&text($text, @subs)."<p>\n";
1538 }
1539
1540 =head2 ui_print_endpage(text)
1541
1542 Prints HTML for an error message followed by a page footer with a link to
1543 /, then exits. Good for main page error messages.
1544
1545 =cut
1546 sub ui_print_endpage
1547 {
1548 return &theme_ui_print_endpage(@_) if (defined(&theme_ui_print_endpage));
1549 my ($text) = @_;
1550 print $text,"<p class='ui_footer'>\n";
1551 print "</p>\n";
1552 &ui_print_footer("/", $text{'index'});
1553 exit;
1554 }
1555
1556 =head2 ui_subheading(text, ...)
1557
1558 Returns HTML for a section heading whose message is the given text strings.
1559
1560 =cut
1561 sub ui_subheading
1562 {
1563 return &theme_ui_subheading(@_) if (defined(&theme_ui_subheading));
1564 return "<h3 class='ui_subheading'>".join("", @_)."</h3>\n";
1565 }
1566
1567 =head2 ui_links_row(&links)
1568
1569 Returns HTML for a row of links, like select all / invert selection / add..
1570 Each element of the links array ref should be an HTML fragment like :
1571
1572   <a href='user_form.cgi'>Create new user</a>
1573
1574 =cut
1575 sub ui_links_row
1576 {
1577 return &theme_ui_links_row(@_) if (defined(&theme_ui_links_row));
1578 my ($links) = @_;
1579 return @$links ? join("\n|\n", @$links)."<br>\n"
1580                : "";
1581 }
1582
1583 ########################### collapsible section / tab functions
1584
1585 =head2 ui_hidden_javascript
1586
1587 Returns <script> and <style> sections for hiding functions and CSS. For
1588 internal use only.
1589
1590 =cut
1591 sub ui_hidden_javascript
1592 {
1593 return &theme_ui_hidden_javascript(@_)
1594         if (defined(&theme_ui_hidden_javascript));
1595 my $rv;
1596 my $imgdir = "$gconfig{'webprefix'}/images";
1597 my ($jscb, $jstb) = ($cb, $tb);
1598 $jscb =~ s/'/\\'/g;
1599 $jstb =~ s/'/\\'/g;
1600
1601 return <<EOF;
1602 <style>
1603 .opener_shown {display:inline}
1604 .opener_hidden {display:none}
1605 </style>
1606 <script>
1607 // Open or close a hidden section
1608 function hidden_opener(divid, openerid)
1609 {
1610 var divobj = document.getElementById(divid);
1611 var openerobj = document.getElementById(openerid);
1612 if (divobj.className == 'opener_shown') {
1613   divobj.className = 'opener_hidden';
1614   openerobj.innerHTML = '<img border=0 src=$imgdir/closed.gif>';
1615   }
1616 else {
1617   divobj.className = 'opener_shown';
1618   openerobj.innerHTML = '<img border=0 src=$imgdir/open.gif>';
1619   }
1620 }
1621
1622 // Show a tab
1623 function select_tab(name, tabname, form)
1624 {
1625 var tabnames = document[name+'_tabnames'];
1626 var tabtitles = document[name+'_tabtitles'];
1627 for(var i=0; i<tabnames.length; i++) {
1628   var tabobj = document.getElementById('tab_'+tabnames[i]);
1629   var divobj = document.getElementById('div_'+tabnames[i]);
1630   var title = tabtitles[i];
1631   if (tabnames[i] == tabname) {
1632     // Selected table
1633     tabobj.innerHTML = '<table cellpadding=0 cellspacing=0><tr>'+
1634                        '<td valign=top $jscb>'+
1635                        '<img src=$imgdir/lc2.gif alt=""></td>'+
1636                        '<td $jscb nowrap>'+
1637                        '&nbsp;<b>'+title+'</b>&nbsp;</td>'+
1638                        '<td valign=top $jscb>'+
1639                        '<img src=$imgdir/rc2.gif alt=""></td>'+
1640                        '</tr></table>';
1641     divobj.className = 'opener_shown';
1642     }
1643   else {
1644     // Non-selected tab
1645     tabobj.innerHTML = '<table cellpadding=0 cellspacing=0><tr>'+
1646                        '<td valign=top $jstb>'+
1647                        '<img src=$imgdir/lc1.gif alt=""></td>'+
1648                        '<td $jstb nowrap>'+
1649                        '&nbsp;<a href=\\'\\' onClick=\\'return select_tab("'+
1650                        name+'", "'+tabnames[i]+'")\\'>'+title+'</a>&nbsp;</td>'+
1651                        '<td valign=top $jstb>'+
1652                        '<img src=$imgdir/rc1.gif alt=""></td>'+
1653                        '</tr></table>';
1654     divobj.className = 'opener_hidden';
1655     }
1656   }
1657 if (document.forms[0] && document.forms[0][name]) {
1658   document.forms[0][name].value = tabname;
1659   }
1660 return false;
1661 }
1662 </script>
1663 EOF
1664 }
1665
1666 =head2 ui_hidden_start(title, name, status, thisurl)
1667
1668 Returns HTML for the start of a collapsible hidden section, such as for
1669 advanced options. When clicked on, the section header will expand to display
1670 whatever is between this function and ui_hidden_end. The parameters are :
1671
1672 =item title - Text for the start of this hidden section.
1673
1674 =item name - A unique name for this section.
1675
1676 =item status - 1 if it should be initially open, 0 if not.
1677
1678 =item thisurl - URL of the current page. This is used by themes on devices that don't support Javascript to implement the opening and closing.
1679
1680 =cut
1681 sub ui_hidden_start
1682 {
1683 return &theme_ui_hidden_start(@_) if (defined(&theme_ui_hidden_start));
1684 my ($title, $name, $status, $url) = @_;
1685 my $rv;
1686 if (!$main::ui_hidden_start_donejs++) {
1687         $rv .= &ui_hidden_javascript();
1688         }
1689 my $divid = "hiddendiv_$name";
1690 my $openerid = "hiddenopener_$name";
1691 my $defimg = $status ? "open.gif" : "closed.gif";
1692 my $defclass = $status ? 'opener_shown' : 'opener_hidden';
1693 $rv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\" id='$openerid'><img border=0 src='$gconfig{'webprefix'}/images/$defimg' alt='*'></a>\n";
1694 $rv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\">$title</a><br>\n";
1695 $rv .= "<div class='$defclass' id='$divid'>\n";
1696 return $rv;
1697 }
1698
1699 =head2 ui_hidden_end(name)
1700
1701 Returns HTML for the end of a hidden section, started by ui_hidden_start.
1702
1703 =cut
1704 sub ui_hidden_end
1705 {
1706 return &theme_ui_hidden_end(@_) if (defined(&theme_ui_hidden_end));
1707 my ($name) = @_;
1708 return "</div>\n";
1709 }
1710
1711 =head2 ui_hidden_table_row_start(title, name, status, thisurl)
1712
1713 Similar to ui_hidden_start, but for use within a table started with
1714 ui_table_start. I recommend against using this where possible, as it can
1715 be difficult for some themes to implement.
1716
1717 =cut
1718 sub ui_hidden_table_row_start
1719 {
1720 return &theme_ui_hidden_table_row_start(@_)
1721         if (defined(&theme_ui_hidden_table_row_start));
1722 my ($title, $name, $status, $url) = @_;
1723 my ($rv, $rrv);
1724 if (!$main::ui_hidden_start_donejs++) {
1725         $rv .= &ui_hidden_javascript();
1726         }
1727 my $divid = "hiddendiv_$name";
1728 my $openerid = "hiddenopener_$name";
1729 my $defimg = $status ? "open.gif" : "closed.gif";
1730 my $defclass = $status ? 'opener_shown' : 'opener_hidden';
1731 $rrv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\" id='$openerid'><img border=0 src='$gconfig{'webprefix'}/images/$defimg'></a>\n";
1732 $rrv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\">$title</a><br>\n";
1733 $rv .= &ui_table_row(undef, $rrv, $main::ui_table_cols);
1734 $rv .= "</table>\n";
1735 $rv .= "<div class='$defclass' id='$divid'>\n";
1736 $rv .= "<table width=100%>\n";
1737 return $rv;
1738 }
1739
1740 =head2 ui_hidden_table_row_end(name)
1741
1742 Returns HTML to end a block started by ui_hidden_table_start.
1743
1744 =cut
1745 sub ui_hidden_table_row_end
1746 {
1747 return &theme_ui_hidden_table_row_end(@_)
1748         if (defined(&theme_ui_hidden_table_row_end));
1749 my ($name) = @_;
1750 return "</table></div><table width=100%>\n";
1751 }
1752
1753 =head2 ui_hidden_table_start(heading, [tabletags], [cols], name, status, [&default-tds], [rightheading])
1754
1755 Returns HTML for the start of a form block into which labelled inputs can
1756 be placed, which is collapsible by clicking on the header. Basically the same
1757 as ui_table_start, and must contain HTML generated by ui_table_row.
1758
1759 The parameters are :
1760
1761 =item heading - Text to show at the top of the form.
1762
1763 =item tabletags - HTML attributes to put in the outer <table>, typically something like width=100%.
1764
1765 =item cols - Desired number of columns for labels and fields. Defaults to 4, but can be 2 for forms with lots of wide inputs.
1766
1767 =item name - A unique name for this table.
1768
1769 =item status - Set to 1 if initially open, 0 if initially closed.
1770
1771 =item default-tds - An optional array reference of HTML attributes for the <td> tags in each row of the table.
1772
1773 =item right-heading - HTML to appear in the heading, aligned to the right.
1774
1775 =cut
1776 sub ui_hidden_table_start
1777 {
1778 return &theme_ui_hidden_table_start(@_)
1779         if (defined(&theme_ui_hidden_table_start));
1780 my ($heading, $tabletags, $cols, $name, $status, $tds, $rightheading) = @_;
1781 my $rv;
1782 if (!$main::ui_hidden_start_donejs++) {
1783         $rv .= &ui_hidden_javascript();
1784         }
1785 my $divid = "hiddendiv_$name";
1786 my $openerid = "hiddenopener_$name";
1787 my $defimg = $status ? "open.gif" : "closed.gif";
1788 my $defclass = $status ? 'opener_shown' : 'opener_hidden';
1789 my $text = defined($tconfig{'cs_text'}) ? $tconfig{'cs_text'} : 
1790               defined($gconfig{'cs_text'}) ? $gconfig{'cs_text'} : "000000";
1791 $rv .= "<table class='ui_table' border $tabletags>\n";
1792 my $colspan = 1;
1793 if (defined($heading) || defined($rightheading)) {
1794         $rv .= "<tr $tb> <td>";
1795         if (defined($heading)) {
1796                 $rv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\" id='$openerid'><img border=0 src='$gconfig{'webprefix'}/images/$defimg'></a> <a href=\"javascript:hidden_opener('$divid', '$openerid')\"><b><font color=#$text>$heading</font></b></a></td>";
1797                 }
1798         if (defined($rightheading)) {
1799                 $rv .= "<td align=right>$rightheading</td>";
1800                 $colspan++;
1801                 }
1802         $rv .= "</td> </tr>\n";
1803         }
1804 $rv .= "<tr $cb> <td colspan=$colspan><div class='$defclass' id='$divid'><table width=100%>\n";
1805 $main::ui_table_cols = $cols || 4;
1806 $main::ui_table_pos = 0;
1807 $main::ui_table_default_tds = $tds;
1808 return $rv;
1809 }
1810
1811 =head2 ui_hidden_table_end(name)
1812
1813 Returns HTML for the end of a form block with hiding, as started by
1814 ui_hidden_table_start.
1815
1816 =cut
1817 sub ui_hidden_table_end
1818 {
1819 my ($name) = @_;
1820 return &theme_ui_hidden_table_end(@_) if (defined(&theme_ui_hidden_table_end));
1821 return "</table></div></td></tr></table>\n";
1822 }
1823
1824 =head2 ui_tabs_start(&tabs, name, selected, show-border)
1825
1826 Returns a row of tabs from which one can be selected, displaying HTML
1827 associated with that tab. The parameters are :
1828
1829 =item tabs - An array reference of array refs, each of which contains the value and user-visible text for a tab.
1830
1831 =item name - Name of the HTML field into which the selected tab will be placed.
1832
1833 =item selected - Value for the tab selected by default.
1834
1835 =item show-border - Set to 1 if there should be a border around the contents of the tabs.
1836
1837 Example code :
1838
1839   @tabs = ( [ 'list', 'List services' ],
1840             [ 'install', 'Install new service' ] );
1841   print ui_tabs_start(\@tabs, 'mode', 'list');
1842
1843   print ui_tabs_start_tab('mode', 'list');
1844   generate_service_list();
1845   print ui_tabs_end_tab('mode', 'list');
1846
1847   print ui_tabs_start_tab('mode', 'install');
1848   generate_install_form();
1849   print ui_tabs_end_tab('mode', 'install);
1850
1851   print ui_tabs_end();
1852
1853 =cut
1854 sub ui_tabs_start
1855 {
1856 return &theme_ui_tabs_start(@_) if (defined(&theme_ui_tabs_start));
1857 my ($tabs, $name, $sel, $border) = @_;
1858 my $rv;
1859 if (!$main::ui_hidden_start_donejs++) {
1860         $rv .= &ui_hidden_javascript();
1861         }
1862
1863 # Build list of tab titles and names
1864 my $tabnames = "[".join(",", map { "\"".&quote_escape($_->[0])."\"" } @$tabs)."]";
1865 my $tabtitles = "[".join(",", map { "\"".&quote_escape($_->[1])."\"" } @$tabs)."]";
1866 $rv .= "<script>\n";
1867 $rv .= "document.${name}_tabnames = $tabnames;\n";
1868 $rv .= "document.${name}_tabtitles = $tabtitles;\n";
1869 $rv .= "</script>\n";
1870
1871 # Output the tabs
1872 my $imgdir = "$gconfig{'webprefix'}/images";
1873 $rv .= &ui_hidden($name, $sel)."\n";
1874 $rv .= "<table border=0 cellpadding=0 cellspacing=0 class='ui_tabs'>\n";
1875 $rv .= "<tr><td bgcolor=#ffffff colspan=".(scalar(@$tabs)*2+1).">";
1876 if ($ENV{'HTTP_USER_AGENT'} !~ /msie/i) {
1877         # For some reason, the 1-pixel space above the tabs appears huge on IE!
1878         $rv .= "<img src=$imgdir/1x1.gif>";
1879         }
1880 $rv .= "</td></tr>\n";
1881 $rv .= "<tr>\n";
1882 $rv .= "<td bgcolor=#ffffff width=1><img src=$imgdir/1x1.gif></td>\n";
1883 foreach my $t (@$tabs) {
1884         if ($t ne $$tabs[0]) {
1885                 # Spacer
1886                 $rv .= "<td width=2 bgcolor=#ffffff class='ui_tab_spacer'>".
1887                        "<img src=$imgdir/1x1.gif></td>\n";
1888                 }
1889         my $tabid = "tab_".$t->[0];
1890         $rv .= "<td id=${tabid} class='ui_tab'>";
1891         $rv .= "<table cellpadding=0 cellspacing=0 border=0><tr>";
1892         if ($t->[0] eq $sel) {
1893                 # Selected tab
1894                 $rv .= "<td valign=top $cb class='selectedTabLeft'>".
1895                        "<img src=$imgdir/lc2.gif alt=\"\"></td>";
1896                 $rv .= "<td $cb nowrap class='selectedTabMiddle'>".
1897                        "&nbsp;<b>$t->[1]</b>&nbsp;</td>";
1898                 $rv .= "<td valign=top $cb class='selectedTabRight'>".
1899                        "<img src=$imgdir/rc2.gif alt=\"\"></td>";
1900                 }
1901         else {
1902                 # Other tab (which has a link)
1903                 $rv .= "<td valign=top $tb>".
1904                        "<img src=$imgdir/lc1.gif alt=\"\"></td>";
1905                 $rv .= "<td $tb nowrap>".
1906                        "&nbsp;<a href='$t->[2]' ".
1907                        "onClick='return select_tab(\"$name\", \"$t->[0]\")'>".
1908                        "$t->[1]</a>&nbsp;</td>";
1909                 $rv .= "<td valign=top $tb>".
1910                        "<img src=$imgdir/rc1.gif ".
1911                        "alt=\"\"></td>";
1912                 $rv .= "</td>\n";
1913                 }
1914         $rv .= "</tr></table>";
1915         $rv .= "</td>\n";
1916         }
1917 $rv .= "<td bgcolor=#ffffff width=1><img src=$imgdir/1x1.gif></td>\n";
1918 $rv .= "</table>\n";
1919
1920 if ($border) {
1921         # All tabs are within a grey box
1922         $rv .= "<table width=100% cellpadding=0 cellspacing=0 border=0 ".
1923                "class='ui_tabs_box'>\n";
1924         $rv .= "<tr> <td bgcolor=#ffffff rowspan=3 width=1><img src=$imgdir/1x1.gif></td>\n";
1925         $rv .= "<td $cb colspan=3 height=2><img src=$imgdir/1x1.gif></td> </tr>\n";
1926         $rv .= "<tr> <td $cb width=2><img src=$imgdir/1x1.gif></td>\n";
1927         $rv .= "<td valign=top>";
1928         }
1929 $main::ui_tabs_selected = $sel;
1930 return $rv;
1931 }
1932
1933 =head2 ui_tabs_end(show-border)
1934
1935 Returns HTML to end a block started by ui_tabs_start. The show-border parameter
1936 must match the parameter with the same name in the start function.
1937
1938 =cut
1939 sub ui_tabs_end
1940 {
1941 return &theme_ui_tabs_end(@_) if (defined(&theme_ui_tabs_end));
1942 my ($border) = @_;
1943 my $rv;
1944 my $imgdir = "$gconfig{'webprefix'}/images";
1945 if ($border) {
1946         $rv .= "</td>\n";
1947         $rv .= "<td $cb width=2><img src=$imgdir/1x1.gif></td>\n";
1948         $rv .= "</tr>\n";
1949         $rv .= "<tr> <td $cb colspan=3 height=2><img src=$imgdir/1x1.gif></td> </tr>\n";
1950         $rv .= "</table>\n";
1951         }
1952 return $rv;
1953 }
1954
1955 =head2 ui_tabs_start_tab(name, tab)
1956
1957 Must be called before outputting the HTML for the named tab, and returns HTML
1958 for the required <div> block. 
1959
1960 =cut
1961 sub ui_tabs_start_tab
1962 {
1963 return &theme_ui_tabs_start_tab(@_) if (defined(&theme_ui_tabs_start_tab));
1964 my ($name, $tab) = @_;
1965 my $defclass = $tab eq $main::ui_tabs_selected ?
1966                         'opener_shown' : 'opener_hidden';
1967 my $rv = "<div id='div_$tab' class='$defclass ui_tabs_start'>\n";
1968 return $rv;
1969 }
1970
1971 =head2 ui_tabs_start_tabletab(name, tab)
1972
1973 Behaves like ui_tabs_start_tab, but for use within a ui_table_start block. 
1974 I recommend against using this where possible, as it is difficult for themes
1975 to implement.
1976
1977 =cut
1978 sub ui_tabs_start_tabletab
1979 {
1980 return &theme_ui_tabs_start_tabletab(@_)
1981         if (defined(&theme_ui_tabs_start_tabletab));
1982 my $div = &ui_tabs_start_tab(@_);
1983 return "</table>\n".$div."<table width=100%>\n";
1984 }
1985
1986 =head2 ui_tabs_end_tab
1987
1988 Returns HTML for the end of a block started by ui_tabs_start_tab.
1989
1990 =cut
1991 sub ui_tabs_end_tab
1992 {
1993 return &theme_ui_tabs_end_tab(@_) if (defined(&theme_ui_tabs_end_tab));
1994 return "</div>\n";
1995 }
1996
1997 =head2 ui_tabs_end_tabletab
1998
1999 Returns HTML for the end of a block started by ui_tabs_start_tabletab.
2000
2001 =cut
2002 sub ui_tabs_end_tabletab
2003 {
2004 return &theme_ui_tabs_end_tabletab(@_)
2005         if (defined(&theme_ui_tabs_end_tabletab));
2006 return "</table></div><table width=100%>\n";
2007 }
2008
2009 =head2 ui_max_text_width(width, [text-area?])
2010
2011 Returns a new width for a text field, based on theme settings. For internal
2012 use only.
2013
2014 =cut
2015 sub ui_max_text_width
2016 {
2017 my ($w, $ta) = @_;
2018 my $max = $ta ? $tconfig{'maxareawidth'} : $tconfig{'maxboxwidth'};
2019 return $max && $w > $max ? $max : $w;
2020 }
2021
2022 ####################### radio hidden functions
2023
2024 =head2 ui_radio_selector(&opts, name, selected)
2025
2026 Returns HTML for a set of radio buttons, each of which shows a different
2027 block of HTML when selected. The parameters are :
2028
2029 =item opts - An array ref to arrays containing [ value, label, html ]
2030
2031 =item name - HTML name for the radio buttons
2032
2033 =item selected - Value for the initially selected button.
2034
2035 =cut
2036 sub ui_radio_selector
2037 {
2038 return &theme_ui_radio_selector(@_) if (defined(&theme_ui_radio_selector));
2039 my ($opts, $name, $sel) = @_;
2040 my $rv;
2041 if (!$main::ui_radio_selector_donejs++) {
2042         $rv .= &ui_radio_selector_javascript();
2043         }
2044 my $optnames =
2045         "[".join(",", map { "\"".&html_escape($_->[0])."\"" } @$opts)."]";
2046 foreach my $o (@$opts) {
2047         $rv .= &ui_oneradio($name, $o->[0], $o->[1], $sel eq $o->[0],
2048             "onClick='selector_show(\"$name\", \"$o->[0]\", $optnames)'");
2049         }
2050 $rv .= "<br>\n";
2051 foreach my $o (@$opts) {
2052         my $cls = $o->[0] eq $sel ? "selector_shown" : "selector_hidden";
2053         $rv .= "<div id=sel_${name}_$o->[0] class=$cls>".$o->[2]."</div>\n";
2054         }
2055 return $rv;
2056 }
2057
2058 sub ui_radio_selector_javascript
2059 {
2060 return <<EOF;
2061 <style>
2062 .selector_shown {display:inline}
2063 .selector_hidden {display:none}
2064 </style>
2065 <script>
2066 function selector_show(name, value, values)
2067 {
2068 for(var i=0; i<values.length; i++) {
2069         var divobj = document.getElementById('sel_'+name+'_'+values[i]);
2070         divobj.className = value == values[i] ? 'selector_shown'
2071                                               : 'selector_hidden';
2072         }
2073 }
2074 </script>
2075 EOF
2076 }
2077
2078 ####################### grid layout functions
2079
2080 =head2 ui_grid_table(&elements, columns, [width-percent], [&tds], [tabletags], [title])
2081
2082 Given a list of HTML elements, formats them into a table with the given
2083 number of columns. However, themes are free to override this to use fewer
2084 columns where space is limited. Parameters are :
2085
2086 =item elements - An array reference of table elements, each of which can be any HTML you like.
2087
2088 =item columns - Desired number of columns in the grid.
2089
2090 =item width-percent - Optional desired width as a percentage.
2091
2092 =item tds - Array ref of HTML attributes for <td> tags in the tables.
2093
2094 =item tabletags - HTML attributes for the <table> tag.
2095
2096 =item title - Optional title to add to the top of the grid.
2097
2098 =cut
2099 sub ui_grid_table
2100 {
2101 return &theme_ui_grid_table(@_) if (defined(&theme_ui_grid_table));
2102 my ($elements, $cols, $width, $tds, $tabletags, $title) = @_;
2103 return "" if (!@$elements);
2104 my $rv = "<table class='ui_grid_table'".
2105             ($width ? " width=$width%" : "").
2106             ($tabletags ? " ".$tabletags : "").
2107             ">\n";
2108 my $i;
2109 for($i=0; $i<@$elements; $i++) {
2110         $rv .= "<tr class='ui_grid_row'>" if ($i%$cols == 0);
2111         $rv .= "<td ".$tds->[$i%$cols]." valign=top class='ui_grid_cell'>".
2112                $elements->[$i]."</td>\n";
2113         $rv .= "</tr>" if ($i%$cols == $cols-1);
2114         }
2115 if ($i%$cols) {
2116         while($i%$cols) {
2117                 $rv .= "<td ".$tds->[$i%$cols]." class='ui_grid_cell'>".
2118                        "<br></td>\n";
2119                 $i++;
2120                 }
2121         $rv .= "</tr>\n";
2122         }
2123 $rv .= "</table>\n";
2124 if (defined($title)) {
2125         $rv = "<table class=ui_table border ".
2126               ($width ? " width=$width%" : "").">\n".
2127               ($title ? "<tr $tb> <td><b>$title</b></td> </tr>\n" : "").
2128               "<tr $cb> <td>$rv</td> </tr>\n".
2129               "</table>";
2130         }
2131 return $rv;
2132 }
2133
2134 =head2 ui_radio_table(name, selected, &rows, [no-bold])
2135
2136 Returns HTML for a table of radio buttons, each of which has a label and
2137 some associated inputs to the right. The parameters are :
2138
2139 =item name - Unique name for this table, which is also the radio buttons' name.
2140
2141 =item selected - Value for the initially selected radio button.
2142
2143 =item rows - Array ref of array refs, one per button. The elements of each are the value for this option, a label, and option additional HTML to appear next to it.
2144
2145 =item no-bold - When set to 1, labels in the table will not be bolded
2146
2147 =cut
2148 sub ui_radio_table
2149 {
2150 return &theme_ui_radio_table(@_) if (defined(&theme_ui_radio_table));
2151 my ($name, $sel, $rows, $nobold) = @_;
2152 return "" if (!@$rows);
2153 my $rv = "<table class='ui_radio_table'>\n";
2154 foreach my $r (@$rows) {
2155         $rv .= "<tr>\n";
2156         $rv .= "<td valign=top".(defined($r->[2]) ? "" : " colspan=2").">".
2157                ($nobold ? "" : "<b>").
2158                &ui_oneradio($name, $r->[0], $r->[1], $r->[0] eq $sel, $r->[3]).
2159                ($nobold ? "" : "</b>").
2160                "</td>\n";
2161         if (defined($r->[2])) {
2162                 $rv .= "<td valign=top>".$r->[2]."</td>\n";
2163                 }
2164         $rv .= "</tr>\n";
2165         }
2166 $rv .= "</table>\n";
2167 return $rv;
2168 }
2169
2170 =head2 ui_up_down_arrows(uplink, downlink, up-show, down-show)
2171
2172 Returns HTML for moving some objects in a table up or down. The parameters are :
2173
2174 =item uplink - URL for the up-arrow link.
2175
2176 =item downlink - URL for the down-arrow link.
2177
2178 =item up-show - Set to 1 if the up-arrow should be shown, 0 if not.
2179
2180 =item down-show - Set to 1 if the down-arrow should be shown, 0 if not.
2181
2182 =cut
2183 sub ui_up_down_arrows
2184 {
2185 return &theme_ui_up_down_arrows(@_) if (defined(&theme_ui_up_down_arrows));
2186 my ($uplink, $downlink, $upshow, $downshow) = @_;
2187 my $mover;
2188 my $imgdir = "$gconfig{'webprefix'}/images";
2189 if ($downshow) {
2190         $mover .= "<a href=\"$downlink\">".
2191                   "<img src=$imgdir/movedown.gif border=0></a>";
2192         }
2193 else {
2194         $mover .= "<img src=$imgdir/movegap.gif>";
2195         }
2196 if ($upshow) {
2197         $mover .= "<a href=\"$uplink\">".
2198                   "<img src=$imgdir/moveup.gif border=0></a>";
2199         }
2200 else {
2201         $mover .= "<img src=$imgdir/movegap.gif>";
2202         }
2203 return $mover;
2204 }
2205
2206 =head2 ui_hr
2207
2208 Returns a horizontal row tag, typically just an <hr>
2209
2210 =cut
2211 sub ui_hr
2212 {
2213 return &theme_ui_hr() if (defined(&theme_ui_hr));
2214 return "<hr>\n";
2215 }
2216
2217 =head2 ui_nav_link(direction, url, disabled)
2218
2219 Returns an arrow icon linking to the provided url.
2220
2221 =cut
2222 sub ui_nav_link
2223 {
2224 return &theme_ui_nav_link(@_) if (defined(&theme_ui_nav_link));
2225 my ($direction, $url, $disabled) = @_;
2226 my $alt = $direction eq "left" ? '<-' : '->';
2227 if ($disabled) {
2228         return "<img alt=\"$alt\" align=\"middle\""
2229              . "src=\"$gconfig{'webprefix'}/images/$direction-grey.gif\">\n";
2230         }
2231 else {
2232         return "<a href=\"$url\"><img alt=\"$alt\" align=\"middle\""
2233              . "src=\"$gconfig{'webprefix'}/images/$direction.gif\"></a>\n";
2234         }
2235 }
2236
2237 =head2 ui_confirmation_form(cgi, message, &hiddens, [&buttons], [otherinputs], [extra-warning])
2238
2239 Returns HTML for a form asking for confirmation before performing some
2240 action, such as deleting a user. The parameters are :
2241
2242 =item cgi - Script to which the confirmation form submits, like delete.cgi.
2243
2244 =item message - Warning message for the user to see.
2245
2246 =item hiddens - Array ref of two-element array refs, containing hidden form field names and values.
2247
2248 =item buttons - Array ref of two-element array refs, containing form button names and labels.
2249
2250 =item otherinputs - HTML for extra inputs to include in ther form.
2251
2252 =item extra-warning - An additional separate warning message to show.
2253
2254 =cut
2255 sub ui_confirmation_form
2256 {
2257 my ($cgi, $message, $hiddens, $buttons, $others, $warning) = @_;
2258 my $rv;
2259 $rv .= "<center class=ui_confirmation>\n";
2260 $rv .= &ui_form_start($cgi, "post");
2261 foreach my $h (@$hiddens) {
2262         $rv .= &ui_hidden(@$h);
2263         }
2264 $rv .= "<b>$message</b><p>\n";
2265 if ($warning) {
2266         $rv .= "<b><font color=#ff0000>$warning</font></b><p>\n";
2267         }
2268 if ($others) {
2269         $rv .= $others."<p>\n";
2270         }
2271 $rv .= &ui_form_end($buttons);
2272 $rv .= "</center>\n";
2273 return $rv;
2274 }
2275
2276 ####################### javascript functions
2277
2278 =head2 js_disable_inputs(&disable-inputs, &enable-inputs, [tag])
2279
2280 Returns Javascript to disable some form elements and enable others. Mainly
2281 for internal use.
2282
2283 =cut
2284 sub js_disable_inputs
2285 {
2286 my $rv;
2287 my $f;
2288 foreach $f (@{$_[0]}) {
2289         $rv .= "e = form.elements[\"$f\"]; e.disabled = true; ";
2290         $rv .= "for(i=0; i<e.length; i++) { e[i].disabled = true; } ";
2291         }
2292 foreach $f (@{$_[1]}) {
2293         $rv .= "e = form.elements[\"$f\"]; e.disabled = false; ";
2294         $rv .= "for(i=0; i<e.length; i++) { e[i].disabled = false; } ";
2295         }
2296 foreach $f (@{$_[1]}) {
2297         if ($f =~ /^(.*)_def$/ && &indexof($1, @{$_[1]}) >= 0) {
2298                 # When enabling both a _def field and its associated text field,
2299                 # disable the text if the _def is set to 1
2300                 my $tf = $1;
2301                 $rv .= "e = form.elements[\"$f\"]; for(i=0; i<e.length; i++) { if (e[i].checked && e[i].value == \"1\") { form.elements[\"$tf\"].disabled = true } } ";
2302                 }
2303         }
2304 return $_[2] ? "$_[2]='$rv'" : $rv;
2305 }
2306
2307 =head2 ui_page_flipper(message, [inputs, cgi], left-link, right-link, [far-left-link], [far-right-link], [below])
2308
2309 Returns HTML for moving left and right in some large list, such as an inbox
2310 or database table. If only 5 parameters are given, no far links are included.
2311 If any link is undef, that array will be greyed out. The parameters are :
2312
2313 =item message - Text or display between arrows.
2314
2315 =item inputs - Additional HTML inputs to show after message.
2316
2317 =item cgi - Optional CGI for form wrapping arrows to submit to.
2318
2319 =item left-link - Link for left-facing arrow.
2320
2321 =item right-link - Link for right-facing arrow.
2322
2323 =item far-left-link - Link for far left-facing arrow, optional.
2324
2325 =item far-right-link - Link for far right-facing arrow, optional.
2326
2327 =item below - HTML to display below the arrows.
2328
2329 =cut
2330 sub ui_page_flipper
2331 {
2332 return &theme_ui_page_flipper(@_) if (defined(&theme_ui_page_flipper));
2333 my ($msg, $inputs, $cgi, $left, $right, $farleft, $farright, $below) = @_;
2334 my $rv = "<center>";
2335 $rv .= &ui_form_start($cgi) if ($cgi);
2336
2337 # Far left link, if needed
2338 if (@_ > 5) {
2339         if ($farleft) {
2340                 $rv .= "<a href='$farleft'>".
2341                        "<img src=$gconfig{'webprefix'}/images/first.gif ".
2342                        "border=0 align=middle></a>\n";
2343                 }
2344         else {
2345                 $rv .= "<img src=$gconfig{'webprefix'}/images/first-grey.gif ".
2346                        "border=0 align=middle></a>\n";
2347                 }
2348         }
2349
2350 # Left link
2351 if ($left) {
2352         $rv .= "<a href='$left'>".
2353                "<img src=$gconfig{'webprefix'}/images/left.gif ".
2354                "border=0 align=middle></a>\n";
2355         }
2356 else {
2357         $rv .= "<img src=$gconfig{'webprefix'}/images/left-grey.gif ".
2358                "border=0 align=middle></a>\n";
2359         }
2360
2361 # Message and inputs
2362 $rv .= $msg;
2363 $rv .= " ".$inputs if ($inputs);
2364
2365 # Right link
2366 if ($right) {
2367         $rv .= "<a href='$right'>".
2368                "<img src=$gconfig{'webprefix'}/images/right.gif ".
2369                "border=0 align=middle></a>\n";
2370         }
2371 else {
2372         $rv .= "<img src=$gconfig{'webprefix'}/images/right-grey.gif ".
2373                "border=0 align=middle></a>\n";
2374         }
2375
2376 # Far right link, if needed
2377 if (@_ > 5) {
2378         if ($farright) {
2379                 $rv .= "<a href='$farright'>".
2380                        "<img src=$gconfig{'webprefix'}/images/last.gif ".
2381                        "border=0 align=middle></a>\n";
2382                 }
2383         else {
2384                 $rv .= "<img src=$gconfig{'webprefix'}/images/last-grey.gif ".
2385                        "border=0 align=middle></a>\n";
2386                 }
2387         }
2388
2389 $rv .= "<br>".$below if ($below);
2390 $rv .= &ui_form_end() if ($cgi);
2391 $rv .= "</center>\n";
2392 return $rv;
2393 }
2394
2395 =head2 js_checkbox_disable(name, &checked-disable, &checked-enable, [tag])
2396
2397 For internal use only.
2398
2399 =cut
2400 sub js_checkbox_disable
2401 {
2402 my $rv;
2403 my $f;
2404 foreach $f (@{$_[1]}) {
2405         $rv .= "form.elements[\"$f\"].disabled = $_[0].checked; ";
2406         }
2407 foreach $f (@{$_[2]}) {
2408         $rv .= "form.elements[\"$f\"].disabled = !$_[0].checked; ";
2409         }
2410 return $_[3] ? "$_[3]='$rv'" : $rv;
2411 }
2412
2413 =head2 js_redirect(url, [window-object])
2414
2415 Returns HTML to trigger a redirect to some URL.
2416
2417 =cut
2418 sub js_redirect
2419 {
2420 my ($url, $window) = @_;
2421 if (defined(&theme_js_redirect)) {
2422         return &theme_js_redirect(@_);
2423         }
2424 $window ||= "window";
2425 if ($url =~ /^\//) {
2426         # If the URL is like /foo , add webprefix
2427         $url = $gconfig{'webprefix'}.$url;
2428         }
2429 return "<script>${window}.location = '".&quote_escape($url)."';</script>\n";
2430 }
2431
2432 1;
2433