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