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