2 # Common functions for generating HTML for Webmin user interface elements
4 ####################### table generation functions
6 # ui_table_start(heading, [tabletags], [cols], [&default-tds])
7 # A table with a heading and table inside
10 return &theme_ui_table_start(@_) if (defined(&theme_ui_table_start));
11 local ($heading, $tabletags, $cols, $tds) = @_;
12 if (defined($main::ui_table_cols)) {
13 # Push on stack, for nested call
14 push(@main::ui_table_cols_stack, $main::ui_table_cols);
15 push(@main::ui_table_pos_stack, $main::ui_table_pos);
16 push(@main::ui_table_default_tds_stack, $main::ui_table_default_tds);
19 $rv .= "<table class='ui_table' border $tabletags>\n";
20 $rv .= "<tr $tb> <td><b>$heading</b></td> </tr>\n" if (defined($heading));
21 $rv .= "<tr $cb> <td><table width=100%>\n";
22 $main::ui_table_cols = $cols || 4;
23 $main::ui_table_pos = 0;
24 $main::ui_table_default_tds = $tds;
29 # The end of a table started by ui_table_start
32 return &theme_ui_table_end(@_) if (defined(&theme_ui_table_end));
34 if ($main::ui_table_cols == 4 && $main::ui_table_pos) {
35 # Add an empty block to balance the table
36 $rv .= &ui_table_row(" ", " ");
38 if (@main::ui_table_cols_stack) {
39 $main::ui_table_cols = pop(@main::ui_table_cols_stack);
40 $main::ui_table_pos = pop(@main::ui_table_pos_stack);
41 $main::ui_table_default_tds = pop(@main::ui_table_default_tds_stack);
44 $main::ui_table_cols = undef;
45 $main::ui_table_pos = undef;
46 $main::ui_table_default_tds = undef;
48 $rv .= "</table></td></tr></table>\n";
52 # ui_columns_start(&headings, [width-percent], [noborder], [&tdtags], [heading])
53 # Returns HTML for a multi-column table, with the given headings
56 return &theme_ui_columns_start(@_) if (defined(&theme_ui_columns_start));
57 local ($heads, $width, $noborder, $tdtags, $title) = @_;
59 $rv .= "<table".($noborder ? "" : " border").
60 (defined($width) ? " width=$width%" : "")." class='ui_columns'>\n";
62 $rv .= "<tr $tb><td colspan=".scalar(@$heads).
63 " class='ui_columns_heading'><b>$title</b></td></tr>\n";
65 $rv .= "<tr $tb class='ui_columns_heads'>\n";
67 for($i=0; $i<@$heads; $i++) {
68 $rv .= "<td ".$tdtags->[$i]."><b>".
69 ($heads->[$i] eq "" ? "<br>" : $heads->[$i])."</b></td>\n";
75 # ui_columns_row(&columns, &tdtags)
76 # Returns HTML for a row in a multi-column table
79 return &theme_ui_columns_row(@_) if (defined(&theme_ui_columns_row));
80 local ($cols, $tdtags) = @_;
82 $rv .= "<tr $cb class='ui_columns_row'>\n";
84 for($i=0; $i<@$cols; $i++) {
85 $rv .= "<td ".$tdtags->[$i].">".
86 ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i])."</td>\n";
92 # ui_columns_header(&columns, &tdtags)
93 # Returns HTML for a row in a multi-column table, with a header background
96 return &theme_ui_columns_header(@_) if (defined(&theme_ui_columns_header));
97 local ($cols, $tdtags) = @_;
99 $rv .= "<tr $tb class='ui_columns_header'>\n";
101 for($i=0; $i<@$cols; $i++) {
102 $rv .= "<td ".$tdtags->[$i]."><b>".
103 ($cols->[$i] eq "" ? "<br>" : $cols->[$i])."</b></td>\n";
109 # ui_checked_columns_row(&columns, &tdtags, checkname, checkvalue, [checked?],
111 # Returns HTML for a row in a multi-column table, in which the first
112 # column is a checkbox
113 sub ui_checked_columns_row
115 return &theme_ui_checked_columns_row(@_) if (defined(&theme_ui_checked_columns_row));
116 local ($cols, $tdtags, $checkname, $checkvalue, $checked, $disabled) = @_;
118 $rv .= "<tr $cb class='ui_checked_columns'>\n";
119 $rv .= "<td class='ui_checked_checkbox' ".$tdtags->[0].">".
120 &ui_checkbox($checkname, $checkvalue, undef, $checked, undef, $disabled).
123 for($i=0; $i<@$cols; $i++) {
124 $rv .= "<td ".$tdtags->[$i+1].">";
125 if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
126 $rv .= "<label for=\"".
127 "e_escape("${checkname}_${checkvalue}")."\">";
129 $rv .= ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i]);
130 if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
139 # ui_radio_columns_row(&columns, &tdtags, checkname, checkvalue, [checked],
141 # Returns HTML for a row in a multi-column table, in which the first
142 # column is a radio button
143 sub ui_radio_columns_row
145 return &theme_ui_radio_columns_row(@_) if (defined(&theme_ui_radio_columns_row));
146 local ($cols, $tdtags, $checkname, $checkvalue, $checked, $dis) = @_;
148 $rv .= "<tr $cb class='ui_radio_columns'>\n";
149 $rv .= "<td class='ui_radio_radio' ".$tdtags->[0].">".
150 &ui_oneradio($checkname, $checkvalue, "", $checked, undef, $dis)."</td>\n";
152 for($i=0; $i<@$cols; $i++) {
153 $rv .= "<td ".$tdtags->[$i+1].">";
154 if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
155 $rv .= "<label for=\"".
156 "e_escape("${checkname}_${checkvalue}")."\">";
158 $rv .= ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i]);
159 if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
169 # Returns HTML to end a table started by ui_columns_start
172 return &theme_ui_columns_end(@_) if (defined(&theme_ui_columns_end));
176 # ui_columns_table(&headings, width-percent, &data, &types, no-sort, title,
178 # Returns HTML for a complete table.
179 # headings - An array ref of heading HTML
180 # width-percent - Preferred total width
181 # data - A 2x2 array ref of table contents. Each can either be a simple string,
182 # or a hash ref like :
183 # { 'type' => 'group', 'desc' => 'Some section title' }
184 # { 'type' => 'string', 'value' => 'Foo', 'colums' => 3 }
185 # { 'type' => 'checkbox', 'name' => 'd', 'value' => 'foo',
186 # 'label' => 'Yes', 'checked' => 1, 'disabled' => 1 }
187 # { 'type' => 'radio', 'name' => 'd', 'value' => 'foo', ... }
188 # types - An array ref of data types, such as 'string', 'number', 'bytes'
190 # no-sort - Set to 1 to disable sorting by theme
191 # title - Text to appear above the table
192 # empty-msg - Message to display if no data
195 return &theme_ui_columns_table(@_) if (defined(&theme_ui_columns_table));
196 local ($heads, $width, $data, $types, $nosort, $title, $emptymsg) = @_;
199 # Just show empty message if no data
200 if ($emptymsg && !@$data) {
201 $rv .= &ui_subheading($title) if ($title);
202 $rv .= "<b>$emptymsg</b><p>\n";
206 # Are there any checkboxes in each column? If so, make those columns narrow
209 foreach my $r (@$data) {
211 foreach my $c (@$r) {
213 ($c->{'type'} eq 'checkbox' || $c->{'type'} eq 'radio')) {
214 $tds[$cc] = "width=5";
218 $maxwidth = $cc if ($cc > $maxwidth);
220 $rv .= &ui_columns_start($heads, $width, $noborder, \@tds, $title);
223 foreach my $r (@$data) {
225 if (ref($r->[0]) && ($r->[0]->{'type'} eq 'checkbox' ||
226 $r->[0]->{'type'} eq 'radio')) {
227 # First column is special
229 $r = [ @$r[1..(@$r-1)] ];
231 # Turn data into HTML
234 foreach my $c (@$r) {
239 elsif ($c->{'type'} eq 'checkbox') {
240 # Checkbox in non-first column
241 push(@cols, &ui_checkbox($c->{'name'}, $c->{'value'},
242 $c->{'label'}, $c->{'checked'},
243 undef, $c->{'disabled'}));
245 elsif ($c->{'type'} eq 'radio') {
246 # Radio button in non-first column
247 push(@cols, &ui_oneradio($c->{'name'}, $c->{'value'},
248 $c->{'label'}, $c->{'checked'},
249 undef, $c->{'disabled'}));
251 elsif ($c->{'type'} eq 'group') {
252 # Header row that spans whole table
253 $rv .= &ui_columns_header([ $c->{'desc'} ],
254 [ "colspan=$width" ]);
257 elsif ($c->{'type'} eq 'string') {
258 # A string, which might be special
259 push(@cols, $c->{'value'});
260 if ($c->{'columns'} > 1) {
261 splice(@rtds, 0, $c->{'columns'},
262 "colspan=".$c->{'columns'});
268 $rv .= &ui_columns_row(\@cols, \@rtds);
270 elsif ($c0->{'type'} eq 'checkbox') {
271 $rv .= &ui_checked_columns_row(\@cols, \@rtds, $c0->{'name'},
272 $c0->{'value'}, $c0->{'checked'},
275 elsif ($c0->{'type'} eq 'radio') {
276 $rv .= &ui_radio_columns_row(\@cols, \@rtds, $c0->{'name'},
277 $c0->{'value'}, $c0->{'checked'},
282 $rv .= &ui_columns_end();
286 # ui_form_columns_table(cgi, &buttons, select-all, &otherlinks,
287 # &headings, width-percent, &data, &types, no-sort, title,
289 # Similar to ui_columns_table, but wrapped in a form. Args are :
290 # cgi - URL to submit the form to
291 # buttons - An array ref of buttons at the end of the form, similar to
292 # that taken by ui_form_end
293 # select-all - If set to 1, include select all / invert links
294 # otherslinks - An array ref of other links to put at the top of the table,
295 # each of which is a 2-element hash ref of url and text
296 # All other parameters are the same as ui_columns_table
297 sub ui_form_columns_table
299 return &theme_ui_form_columns_table(@_)
300 if (defined(&theme_ui_form_columns_table));
301 local ($cgi, $buttons, $selectall, $others,
302 $heads, $width, $data, $types, $nosort, $title, $emptymsg) = @_;
304 local @links = map { "<a href='$_->[0]'>$_->[1]</a>" } @$others;
306 # Start the form, if we need one
308 $rv .= &ui_form_start($cgi, "post");
311 foreach my $r (@$data) {
312 foreach my $c (@$r) {
313 if (ref($c) && $c->{'type'} eq 'checkbox') {
314 $cbname = $c->{'name'};
320 unshift(@links, &select_all_link("d"),
321 &select_invert_link("d"));
324 $rv .= &ui_links_row(\@links);
328 $rv .= &ui_columns_table($heads, $width, $data, $types, $nosort, $title,
332 $rv .= &ui_links_row(\@links);
334 $rv .= &ui_form_end($buttons);
340 ####################### form generation functions
342 # ui_form_start(script, method, [target], [tags])
343 # Returns HTML for a form that submits to some script
346 return &theme_ui_form_start(@_) if (defined(&theme_ui_form_start));
347 local ($script, $method, $target, $tags) = @_;
349 $rv .= "<form class='ui_form' action='".&html_escape($script)."' ".
350 ($method eq "post" ? "method=post" :
351 $method eq "form-data" ?
352 "method=post enctype=multipart/form-data" :
354 ($target ? " target=$target" : "").
360 # ui_form_end([&buttons], [width])
361 # Returns HTML for the end of a form, optionally with a row of submit buttons
364 return &theme_ui_form_end(@_) if (defined(&theme_ui_form_end));
365 local ($buttons, $width) = @_;
367 if ($buttons && @$buttons) {
368 $rv .= "<table class='ui_form_end_buttons' ".($width ? " width=$width" : "")."><tr>\n";
370 foreach $b (@$buttons) {
372 $rv .= "<td".(!$width ? "" :
373 $b eq $buttons->[0] ? " align=left" :
374 $b eq $buttons->[@$buttons-1] ?
375 " align=right" : " align=center").">".
376 &ui_submit($b->[1], $b->[0], $b->[3], $b->[4]).
377 ($b->[2] ? " ".$b->[2] : "")."</td>\n";
380 $rv .= "<td>$b</td>\n";
383 $rv .= "<td> </td>\n";
386 $rv .= "</tr></table>\n";
392 # ui_textbox(name, value, size, [disabled?], [maxlength], [tags])
393 # Returns HTML for a text input
396 return &theme_ui_textbox(@_) if (defined(&theme_ui_textbox));
397 local ($name, $value, $size, $dis, $max, $tags) = @_;
398 $size = &ui_max_text_width($size);
399 return "<input name=\""."e_escape($name)."\" ".
400 "value=\""."e_escape($value)."\" ".
401 "size=$size ".($dis ? "disabled=true" : "").
402 ($max ? " maxlength=$max" : "").
407 # ui_bytesbox(name, bytes, [size], [disabled?])
408 # Returns HTML for entering a number of bytes, but with friendly kB/MB/GB
409 # options. May truncate values to 2 decimal points!
412 local ($name, $bytes, $size, $dis) = @_;
414 if ($bytes >= 10*1024*1024*1024) {
415 $units = 1024*1024*1024;
417 elsif ($bytes >= 10*1024*1024) {
420 elsif ($bytes >= 10*1024) {
427 $bytes = sprintf("%.2f", ($bytes*1.0)/$units);
430 $size = &ui_max_text_width($size || 8);
431 return &ui_textbox($name, $bytes, $size, $dis)." ".
432 &ui_select($name."_units", $units,
433 [ [ 1, "bytes" ], [ 1024, "kB" ], [ 1024*1024, "MB" ],
434 [ 1024*1024*1024, "GB" ] ], undef, undef, undef, $dis);
437 # ui_upload(name, size, [disabled?], [tags])
438 # Returns HTML for a file upload input
441 return &theme_ui_upload(@_) if (defined(&theme_ui_upload));
442 local ($name, $size, $dis, $tags) = @_;
443 $size = &ui_max_text_width($size);
444 return "<input type=file name=\""."e_escape($name)."\" ".
446 ($dis ? "disabled=true" : "").
447 ($tags ? " ".$tags : "").">";
450 # ui_password(name, value, size, [disabled?], [maxlength])
451 # Returns HTML for a password text input
454 return &theme_ui_password(@_) if (defined(&theme_ui_password));
455 local ($name, $value, $size, $dis, $max) = @_;
456 $size = &ui_max_text_width($size);
457 return "<input type=password name=\""."e_escape($name)."\" ".
458 "value=\""."e_escape($value)."\" ".
459 "size=$size ".($dis ? "disabled=true" : "").
460 ($max ? " maxlength=$max" : "").
464 # ui_hidden(name, value)
465 # Returns HTML for a hidden field
468 return &theme_ui_hidden(@_) if (defined(&theme_ui_hidden));
469 local ($name, $value) = @_;
470 return "<input type=hidden name=\""."e_escape($name)."\" ".
471 "value=\""."e_escape($value)."\">\n";
474 # ui_select(name, value|&values, &options, [size], [multiple],
475 # [add-if-missing], [disabled?], [javascript])
476 # Returns HTML for a drop-down menu or multiple selection list
479 return &theme_ui_select(@_) if (defined(&theme_ui_select));
480 local ($name, $value, $opts, $size, $multiple, $missing, $dis, $js) = @_;
482 $rv .= "<select name=\""."e_escape($name)."\"".
483 ($size ? " size=$size" : "").
484 ($multiple ? " multiple" : "").
485 ($dis ? " disabled=true" : "")." ".$js.">\n";
486 local ($o, %opt, $s);
487 local %sel = ref($value) ? ( map { $_, 1 } @$value ) : ( $value, 1 );
488 foreach $o (@$opts) {
489 $o = [ $o ] if (!ref($o));
490 $rv .= "<option value=\""."e_escape($o->[0])."\"".
491 ($sel{$o->[0]} ? " selected" : "").">".
492 ($o->[1] || $o->[0])."\n";
495 foreach $s (keys %sel) {
496 if (!$opt{$s} && $missing) {
497 $rv .= "<option value=\""."e_escape($s)."\"".
498 "selected>".($s eq "" ? " " : $s)."\n";
501 $rv .= "</select>\n";
505 # ui_radio(name, value, &options, [disabled?])
506 # Returns HTML for a series of radio buttons
509 return &theme_ui_radio(@_) if (defined(&theme_ui_radio));
510 local ($name, $value, $opts, $dis) = @_;
513 foreach $o (@$opts) {
514 local $id = "e_escape($name."_".$o->[0]);
515 local $label = $o->[1] || $o->[0];
517 if ($label =~ /^(.*?)((<a\s+href|<input|<select|<textarea)[\000-\377]*)$/i) {
521 $rv .= "<input type=radio name=\""."e_escape($name)."\" ".
522 "value=\""."e_escape($o->[0])."\"".
523 ($o->[0] eq $value ? " checked" : "").
524 ($dis ? " disabled=true" : "").
526 " $o->[2]> <label for=\"$id\">".
527 $label."</label>".$after."\n";
532 # ui_yesno_radio(name, value, [yes], [no], [disabled?])
533 # Like ui_yesno, but always displays just two inputs (yes and no)
536 local ($name, $value, $yes, $no, $dis) = @_;
537 return &theme_ui_yesno_radio(@_) if (defined(&theme_ui_yesno_radio));
538 $yes = 1 if (!defined($yes));
539 $no = 0 if (!defined($no));
540 $value = int($value);
541 return &ui_radio($name, $value, [ [ $yes, $text{'yes'} ],
542 [ $no, $text{'no'} ] ], $dis);
545 # ui_checkbox(name, value, label, selected?, [tags], [disabled?])
546 # Returns HTML for a single checkbox
549 return &theme_ui_checkbox(@_) if (defined(&theme_ui_checkbox));
550 local ($name, $value, $label, $sel, $tags, $dis) = @_;
552 if ($label =~ /^([^<]*)(<[\000-\377]*)$/) {
556 return "<input type=checkbox name=\""."e_escape($name)."\" ".
557 "value=\""."e_escape($value)."\" ".
558 ($sel ? " checked" : "").($dis ? " disabled=true" : "").
559 " id=\""."e_escape("${name}_${value}")."\"".
561 ($label eq "" ? $after :
562 "<label for=\""."e_escape("${name}_${value}").
563 "\">$label</label>$after")."\n";
566 # ui_oneradio(name, value, label, selected?, [tags], [disabled?])
567 # Returns HTML for a single radio button
570 return &theme_ui_oneradio(@_) if (defined(&theme_ui_oneradio));
571 local ($name, $value, $label, $sel, $tags, $dis) = @_;
572 local $id = "e_escape("${name}_${value}");
574 if ($label =~ /^([^<]*)(<[\000-\377]*)$/) {
578 return "<input type=radio name=\""."e_escape($name)."\" ".
579 "value=\""."e_escape($value)."\" ".
580 ($sel ? " checked" : "").($dis ? " disabled=true" : "").
582 " $tags> <label for=\"$id\">$label</label>$after\n";
585 # ui_textarea(name, value, rows, cols, [wrap], [disabled?], [tags])
586 # Returns HTML for a multi-line text input
589 return &theme_ui_textarea(@_) if (defined(&theme_ui_textarea));
590 local ($name, $value, $rows, $cols, $wrap, $dis, $tags) = @_;
591 $cols = &ui_max_text_width($cols, 1);
592 return "<textarea name=\""."e_escape($name)."\" ".
593 "rows=$rows cols=$cols".($wrap ? " wrap=$wrap" : "").
594 ($dis ? " disabled=true" : "").
595 ($tags ? " $tags" : "").">".
596 &html_escape($value).
600 # ui_user_textbox(name, value, [form], [disabled?], [tags])
601 # Returns HTML for a Unix user input
604 return &theme_ui_user_textbox(@_) if (defined(&theme_ui_user_textbox));
605 return &ui_textbox($_[0], $_[1], 13, $_[3], undef, $_[4])." ".
606 &user_chooser_button($_[0], 0, $_[2]);
609 # ui_group_textbox(name, value, [form], [disabled?], [tags])
610 # Returns HTML for a Unix group input
613 return &theme_ui_group_textbox(@_) if (defined(&theme_ui_group_textbox));
614 return &ui_textbox($_[0], $_[1], 13, $_[3], undef, $_[4])." ".
615 &group_chooser_button($_[0], 0, $_[2]);
618 # ui_opt_textbox(name, value, size, option1, [option2], [disabled?],
619 # [&extra-fields], [max])
620 # Returns HTML for a text field that is optional
623 return &theme_ui_opt_textbox(@_) if (defined(&theme_ui_opt_textbox));
624 local ($name, $value, $size, $opt1, $opt2, $dis, $extra, $max) = @_;
625 local $dis1 = &js_disable_inputs([ $name, @$extra ], [ ]);
626 local $dis2 = &js_disable_inputs([ ], [ $name, @$extra ]);
628 $size = &ui_max_text_width($size);
629 $rv .= &ui_radio($name."_def", $value eq '' ? 1 : 0,
630 [ [ 1, $opt1, "onClick='$dis1'" ],
631 [ 0, $opt2 || " ", "onClick='$dis2'" ] ], $dis)."\n";
632 $rv .= "<input name=\""."e_escape($name)."\" ".
633 "size=$size value=\""."e_escape($value)."\" ".
634 ($value eq "" || $dis ? "disabled=true" : "").
635 ($max ? " maxlength=$max" : "").">\n";
639 # ui_submit(label, [name], [disabled?], [tags])
640 # Returns HTML for a form submit button
643 return &theme_ui_submit(@_) if (defined(&theme_ui_submit));
644 local ($label, $name, $dis, $tags) = @_;
645 return "<input type=submit".
646 ($name ne '' ? " name=\""."e_escape($name)."\"" : "").
647 " value=\""."e_escape($label)."\"".
648 ($dis ? " disabled=true" : "").
649 ($tags ? " ".$tags : "").">\n";
653 # ui_reset(label, [disabled?])
654 # Returns HTML for a form reset button
657 return &theme_ui_reset(@_) if (defined(&theme_ui_reset));
658 local ($label, $dis) = @_;
659 return "<input type=reset value=\""."e_escape($label)."\"".
660 ($dis ? " disabled=true" : "").">\n";
664 # ui_button(label, [name], [disabled?], [tags])
665 # Returns HTML for a form button
668 return &theme_ui_button(@_) if (defined(&theme_ui_button));
669 local ($label, $name, $dis, $tags) = @_;
670 return "<input type=button".
671 ($name ne '' ? " name=\""."e_escape($name)."\"" : "").
672 " value=\""."e_escape($label)."\"".
673 ($dis ? " disabled=true" : "").
674 ($tags ? " ".$tags : "").">\n";
677 # ui_date_input(day, month, year, day-name, month-name, year-name, [disabled?])
678 # Returns HTML for a date-selection field
681 local ($day, $month, $year, $dayname, $monthname, $yearname, $dis) = @_;
683 $rv .= &ui_textbox($dayname, $day, 3, $dis);
685 $rv .= &ui_select($monthname, $month,
686 [ map { [ $_, $text{"smonth_$_"} ] } (1 .. 12) ],
689 $rv .= &ui_textbox($yearname, $year, 5, $dis);
693 # ui_table_row(label, value, [cols], [&td-tags])
694 # Returns HTML for a row in a table started by ui_table_start, with a 1-column
695 # label and 1+ column value.
698 return &theme_ui_table_row(@_) if (defined(&theme_ui_table_row));
699 local ($label, $value, $cols, $tds) = @_;
701 $tds ||= $main::ui_table_default_tds;
703 if ($main::ui_table_pos+$cols+1 > $main::ui_table_cols &&
704 $main::ui_table_pos != 0) {
705 # If the requested number of cols won't fit in the number
706 # remaining, start a new row
708 $main::ui_table_pos = 0;
710 $rv .= "<tr>\n" if ($main::ui_table_pos%$main::ui_table_cols == 0);
711 $rv .= "<td valign=top $tds->[0]><b>$label</b></td>\n" if (defined($label));
712 $rv .= "<td valign=top colspan=$cols $tds->[1]>$value</td>\n";
713 $main::ui_table_pos += $cols+(defined($label) ? 1 : 0);
714 if ($main::ui_table_pos%$main::ui_table_cols == 0) {
716 $main::ui_table_pos = 0;
724 return &theme_ui_table_hr(@_) if (defined(&theme_ui_table_hr));
730 $rv .= "<tr> <td colspan=$main::ui_table_cols><hr></td> </tr>\n";
734 # ui_table_span(text)
735 # Outputs a table row that spans the whole table, and contains the given text
739 return &theme_ui_table_hr(@_) if (defined(&theme_ui_table_hr));
745 $rv .= "<tr> <td colspan=$main::ui_table_cols>$text</td> </tr>\n";
752 return &theme_ui_buttons_start(@_) if (defined(&theme_ui_buttons_start));
753 return "<table width=100%>\n";
759 return &theme_ui_buttons_end(@_) if (defined(&theme_ui_buttons_end));
763 # ui_buttons_row(script, button-label, description, [hiddens], [after-submit],
767 return &theme_ui_buttons_row(@_) if (defined(&theme_ui_buttons_row));
768 local ($script, $label, $desc, $hiddens, $after, $before) = @_;
769 return "<form action=$script>\n".
771 "<tr> <td nowrap width=20% valign=top>".($before ? $before." " : "").
772 &ui_submit($label).($after ? " ".$after : "")."</td>\n".
773 "<td valign=top width=80% valign=top>$desc</td> </tr>\n".
777 # ui_buttons_hr([title])
781 return &theme_ui_buttons_hr(@_) if (defined(&theme_ui_buttons_hr));
783 return "<tr> <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";
786 return "<tr> <td colspan=2><hr></td> </tr>\n";
790 ####################### header and footer functions
792 # ui_post_header([subtext])
793 # Returns HTML to appear directly after a standard header() call
796 return &theme_ui_post_header(@_) if (defined(&theme_ui_post_header));
799 $rv .= "<center class='ui_post_header'><font size=+1>$text</font></center>\n" if (defined($text));
800 if (!$tconfig{'nohr'} && !$tconfig{'notophr'}) {
801 $rv .= "<hr id='post_header_hr'>\n";
807 # Returns HTML to appear directly before a standard footer() call
810 return &theme_ui_pre_footer(@_) if (defined(&theme_ui_pre_footer));
812 if (!$tconfig{'nohr'} && !$tconfig{'nobottomhr'}) {
813 $rv .= "<hr id='pre_footer_hr'>\n";
818 # ui_print_header(subtext, args...)
819 # Print HTML for a header with the post-header line. The args are the same
820 # as those passed to header()
823 &load_theme_library();
824 return &theme_ui_print_header(@_) if (defined(&theme_ui_print_header));
825 local ($text, @args) = @_;
827 print &ui_post_header($text);
830 # ui_print_unbuffered_header(subtext, args...)
831 # Like ui_print_header, but ensures that output for this page is not buffered
832 # or contained in a table.
833 sub ui_print_unbuffered_header
835 &load_theme_library();
836 return &theme_ui_print_unbuffered_header(@_) if (defined(&theme_ui_print_unbuffered_header));
839 &ui_print_header(@_);
842 # ui_print_footer(args...)
843 # Print HTML for a footer with the pre-footer line. Args are the same as those
847 return &theme_ui_print_footer(@_) if (defined(&theme_ui_print_footer));
849 print &ui_pre_footer();
853 # ui_config_link(text, &subs)
854 # Returns HTML for a module config link. The first non-null sub will be
855 # replaced with the appropriate URL.
858 return &theme_ui_config_link(@_) if (defined(&theme_ui_config_link));
859 local ($text, $subs) = @_;
860 local @subs = map { $_ || "../config.cgi?$module_name" }
861 ($subs ? @$subs : ( undef ));
862 return "<p>".&text($text, @subs)."<p>\n";
865 # ui_print_endpage(text)
866 # Prints HTML for an error message followed by a page footer with a link to
867 # /, then exits. Good for main page error messages.
870 return &theme_ui_print_endpage(@_) if (defined(&theme_ui_print_endpage));
872 print $text,"<p class='ui_footer'>\n";
874 &ui_print_footer("/", $text{'index'});
878 # ui_subheading(text, ...)
879 # Returns HTML for a section heading
882 return &theme_ui_subheading(@_) if (defined(&theme_ui_subheading));
883 return "<h3 class='ui_subheading'>".join("", @_)."</h3>\n";
886 # ui_links_row(&links)
887 # Returns HTML for a row of links, like select all / invert selection / add..
890 return &theme_ui_links_row(@_) if (defined(&theme_ui_links_row));
892 return @$links ? join("\n|\n", @$links)."<br>\n"
896 ########################### collapsible section / tab functions
898 # ui_hidden_javascript()
899 # Returns <script> and <style> sections for hiding functions and CSS
900 sub ui_hidden_javascript
902 return &theme_ui_hidden_javascript(@_)
903 if (defined(&theme_ui_hidden_javascript));
905 local $imgdir = "$gconfig{'webprefix'}/images";
906 local ($jscb, $jstb) = ($cb, $tb);
912 .opener_shown {display:inline}
913 .opener_hidden {display:none}
916 // Open or close a hidden section
917 function hidden_opener(divid, openerid)
919 var divobj = document.getElementById(divid);
920 var openerobj = document.getElementById(openerid);
921 if (divobj.className == 'opener_shown') {
922 divobj.className = 'opener_hidden';
923 openerobj.innerHTML = '<img border=0 src=$imgdir/closed.gif>';
926 divobj.className = 'opener_shown';
927 openerobj.innerHTML = '<img border=0 src=$imgdir/open.gif>';
932 function select_tab(name, tabname, form)
934 var tabnames = document[name+'_tabnames'];
935 var tabtitles = document[name+'_tabtitles'];
936 for(var i=0; i<tabnames.length; i++) {
937 var tabobj = document.getElementById('tab_'+tabnames[i]);
938 var divobj = document.getElementById('div_'+tabnames[i]);
939 var title = tabtitles[i];
940 if (tabnames[i] == tabname) {
942 tabobj.innerHTML = '<table cellpadding=0 cellspacing=0><tr>'+
943 '<td valign=top $jscb>'+
944 '<img src=$imgdir/lc2.gif alt=""></td>'+
946 ' <b>'+title+'</b> </td>'+
947 '<td valign=top $jscb>'+
948 '<img src=$imgdir/rc2.gif alt=""></td>'+
950 divobj.className = 'opener_shown';
954 tabobj.innerHTML = '<table cellpadding=0 cellspacing=0><tr>'+
955 '<td valign=top $jstb>'+
956 '<img src=$imgdir/lc1.gif alt=""></td>'+
958 ' <a href=\\'\\' onClick=\\'return select_tab("'+
959 name+'", "'+tabnames[i]+'")\\'>'+title+'</a> </td>'+
960 '<td valign=top $jstb>'+
961 '<img src=$imgdir/rc1.gif alt=""></td>'+
963 divobj.className = 'opener_hidden';
966 if (document.forms[0] && document.forms[0][name]) {
967 document.forms[0][name].value = tabname;
975 # ui_hidden_start(title, name, status, thisurl)
976 # Returns HTML for the start of a collapsible hidden section, such as for
980 return &theme_ui_hidden_start(@_) if (defined(&theme_ui_hidden_start));
981 local ($title, $name, $status, $url) = @_;
983 if (!$main::ui_hidden_start_donejs++) {
984 $rv .= &ui_hidden_javascript();
986 local $divid = "hiddendiv_$name";
987 local $openerid = "hiddenopener_$name";
988 local $defimg = $status ? "open.gif" : "closed.gif";
989 local $defclass = $status ? 'opener_shown' : 'opener_hidden';
990 $rv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\" id='$openerid'><img border=0 src='$gconfig{'webprefix'}/images/$defimg' alt='*'></a>\n";
991 $rv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\">$title</a><br>\n";
992 $rv .= "<div class='$defclass' id='$divid'>\n";
996 # ui_hidden_end(name)
997 # Returns HTML for the end of a hidden section
1000 return &theme_ui_hidden_end(@_) if (defined(&theme_ui_hidden_end));
1005 # ui_hidden_table_row_start(title, name, status, thisurl)
1006 # Similar to ui_hidden_start, but for use within a table started with
1008 sub ui_hidden_table_row_start
1010 return &theme_ui_hidden_table_row_start(@_)
1011 if (defined(&theme_ui_hidden_table_row_start));
1012 local ($title, $name, $status, $url) = @_;
1014 if (!$main::ui_hidden_start_donejs++) {
1015 $rv .= &ui_hidden_javascript();
1017 local $divid = "hiddendiv_$name";
1018 local $openerid = "hiddenopener_$name";
1019 local $defimg = $status ? "open.gif" : "closed.gif";
1020 local $defclass = $status ? 'opener_shown' : 'opener_hidden';
1021 $rrv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\" id='$openerid'><img border=0 src='$gconfig{'webprefix'}/images/$defimg'></a>\n";
1022 $rrv .= "<a href=\"javascript:hidden_opener('$divid', '$openerid')\">$title</a><br>\n";
1023 $rv .= &ui_table_row(undef, $rrv, $main::ui_table_cols);
1024 $rv .= "</table>\n";
1025 $rv .= "<div class='$defclass' id='$divid'>\n";
1026 $rv .= "<table width=100%>\n";
1030 # ui_hidden_table_row_end(name)
1031 sub ui_hidden_table_row_end
1033 return &theme_ui_hidden_table_row_end(@_)
1034 if (defined(&theme_ui_hidden_table_row_end));
1036 return "</table></div><table width=100%>\n";
1039 # ui_hidden_table_start(heading, [tabletags], [cols], name, status,
1041 # A table with a heading and table inside, and which is collapsible
1042 sub ui_hidden_table_start
1044 return &theme_ui_hidden_table_start(@_)
1045 if (defined(&theme_ui_hidden_table_start));
1046 local ($heading, $tabletags, $cols, $name, $status, $tds) = @_;
1048 if (!$main::ui_hidden_start_donejs++) {
1049 $rv .= &ui_hidden_javascript();
1051 local $divid = "hiddendiv_$name";
1052 local $openerid = "hiddenopener_$name";
1053 local $defimg = $status ? "open.gif" : "closed.gif";
1054 local $defclass = $status ? 'opener_shown' : 'opener_hidden';
1055 local $text = defined($tconfig{'cs_text'}) ? $tconfig{'cs_text'} :
1056 defined($gconfig{'cs_text'}) ? $gconfig{'cs_text'} : "000000";
1057 $rv .= "<table class='ui_table' border $tabletags class='ui_table'>\n";
1058 $rv .= "<tr $tb> <td><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> </tr>\n" if (defined($heading));
1059 $rv .= "<tr $cb> <td><div class='$defclass' id='$divid'><table width=100%>\n";
1060 $main::ui_table_cols = $cols || 4;
1061 $main::ui_table_pos = 0;
1062 $main::ui_table_default_tds = $tds;
1066 # ui_hidden_table_end(name)
1067 # Returns HTML for the end of table with hiding, as started by
1068 # ui_hidden_table_start
1069 sub ui_hidden_table_end
1072 return &theme_ui_hidden_table_end(@_) if (defined(&theme_ui_hidden_table_end));
1073 return "</table></div></td></tr></table>\n";
1076 # ui_tabs_start(&tabs, name, selected, show-border)
1077 # Render a row of tabs from which one can be selected. Each tab is an array
1078 # ref containing a name, title and link.
1081 return &theme_ui_tabs_start(@_) if (defined(&theme_ui_tabs_start));
1082 local ($tabs, $name, $sel, $border) = @_;
1084 if (!$main::ui_hidden_start_donejs++) {
1085 $rv .= &ui_hidden_javascript();
1088 # Build list of tab titles and names
1089 local $tabnames = "[".join(",", map { "\"".&html_escape($_->[0])."\"" } @$tabs)."]";
1090 local $tabtitles = "[".join(",", map { "\"".&html_escape($_->[1])."\"" } @$tabs)."]";
1091 $rv .= "<script>\n";
1092 $rv .= "document.${name}_tabnames = $tabnames;\n";
1093 $rv .= "document.${name}_tabtitles = $tabtitles;\n";
1094 $rv .= "</script>\n";
1097 local $imgdir = "$gconfig{'webprefix'}/images";
1098 $rv .= &ui_hidden($name, $sel)."\n";
1099 $rv .= "<table border=0 cellpadding=0 cellspacing=0>\n";
1100 $rv .= "<tr><td bgcolor=#ffffff colspan=".(scalar(@$tabs)*2+1).">";
1101 if ($ENV{'HTTP_USER_AGENT'} !~ /msie/i) {
1102 # For some reason, the 1-pixel space above the tabs appears huge on IE!
1103 $rv .= "<img src=$imgdir/1x1.gif>";
1105 $rv .= "</td></tr>\n";
1107 $rv .= "<td bgcolor=#ffffff width=1><img src=$imgdir/1x1.gif></td>\n";
1108 foreach my $t (@$tabs) {
1109 if ($t ne $tabs[0]) {
1111 $rv .= "<td width=2 bgcolor=#ffffff>".
1112 "<img src=$imgdir/1x1.gif></td>\n";
1114 local $tabid = "tab_".$t->[0];
1115 $rv .= "<td id=${tabid}>";
1116 $rv .= "<table cellpadding=0 cellspacing=0 border=0><tr>";
1117 if ($t->[0] eq $sel) {
1119 $rv .= "<td valign=top $cb>".
1120 "<img src=$imgdir/lc2.gif alt=\"\"></td>";
1121 $rv .= "<td $cb nowrap>".
1122 " <b>$t->[1]</b> </td>";
1123 $rv .= "<td valign=top $cb>".
1124 "<img src=$imgdir/rc2.gif alt=\"\"></td>";
1127 # Other tab (which has a link)
1128 $rv .= "<td valign=top $tb>".
1129 "<img src=$imgdir/lc1.gif alt=\"\"></td>";
1130 $rv .= "<td $tb nowrap>".
1131 " <a href='$t->[2]' ".
1132 "onClick='return select_tab(\"$name\", \"$t->[0]\")'>".
1133 "$t->[1]</a> </td>";
1134 $rv .= "<td valign=top $tb>".
1135 "<img src=$imgdir/rc1.gif ".
1139 $rv .= "</tr></table>";
1142 $rv .= "<td bgcolor=#ffffff width=1><img src=$imgdir/1x1.gif></td>\n";
1143 $rv .= "</table>\n";
1146 # All tabs are within a grey box
1147 $rv .= "<table width=100% cellpadding=0 cellspacing=0 border=0>\n";
1148 $rv .= "<tr> <td bgcolor=#ffffff rowspan=3 width=1><img src=$imgdir/1x1.gif></td>\n";
1149 $rv .= "<td $cb colspan=3 height=2><img src=$imgdir/1x1.gif></td> </tr>\n";
1150 $rv .= "<tr> <td $cb width=2><img src=$imgdir/1x1.gif></td>\n";
1151 $rv .= "<td valign=top>";
1153 $main::ui_tabs_selected = $sel;
1157 # ui_tabs_end(border)
1160 return &theme_ui_tabs_end(@_) if (defined(&theme_ui_tabs_end));
1161 local ($border) = @_;
1163 local $imgdir = "$gconfig{'webprefix'}/images";
1166 $rv .= "<td $cb width=2><img src=$imgdir/1x1.gif></td>\n";
1168 $rv .= "<tr> <td $cb colspan=3 height=2><img src=$imgdir/1x1.gif></td> </tr>\n";
1169 $rv .= "</table>\n";
1174 # ui_tabs_start_tab(name, tab)
1175 # Must be called before outputting the HTML for the named tab
1176 sub ui_tabs_start_tab
1178 return &theme_ui_tabs_start_tab(@_) if (defined(&theme_ui_tabs_start_tab));
1179 local ($name, $tab) = @_;
1180 local $defclass = $tab eq $main::ui_tabs_selected ?
1181 'opener_shown' : 'opener_hidden';
1182 local $rv = "<div id='div_$tab' class='$defclass'>\n";
1186 # ui_tabs_start_tabletab(name, tab)
1187 # Behaves like ui_tabs_start_tab, but for use within a ui_table_start block
1188 sub ui_tabs_start_tabletab
1190 return &theme_ui_tabs_start_tabletab(@_)
1191 if (defined(&theme_ui_tabs_start_tabletab));
1192 local $div = &ui_tabs_start_tab(@_);
1193 return "</table>\n".$div."<table width=100%>\n";
1198 return &theme_ui_tabs_end_tab(@_) if (defined(&theme_ui_tabs_end_tab));
1202 sub ui_tabs_end_tabletab
1204 return &theme_ui_tabs_end_tabletab(@_)
1205 if (defined(&theme_ui_tabs_end_tabletab));
1206 return "</table></div><table width=100%>\n";
1209 # ui_max_text_width(width, [text-area?])
1210 # Returns a new width for a text field, based on theme settings
1211 sub ui_max_text_width
1213 local ($w, $ta) = @_;
1214 local $max = $ta ? $tconfig{'maxareawidth'} : $tconfig{'maxboxwidth'};
1215 return $max && $w > $max ? $max : $w;
1218 ####################### radio hidden functions
1220 # ui_radio_selector(&opts, name, selected)
1221 # Returns HTML for a set of radio buttons, each of which shows a different
1222 # block of HTML when selected. &opts is an array ref to arrays containing
1223 # [ value, label, html ]
1224 sub ui_radio_selector
1226 return &theme_ui_radio_selector(@_) if (defined(&theme_ui_radio_selector));
1227 local ($opts, $name, $sel) = @_;
1229 if (!$main::ui_radio_selector_donejs++) {
1230 $rv .= &ui_radio_selector_javascript();
1233 "[".join(",", map { "\"".&html_escape($_->[0])."\"" } @$opts)."]";
1234 foreach my $o (@$opts) {
1235 $rv .= &ui_oneradio($name, $o->[0], $o->[1], $sel eq $o->[0],
1236 "onClick='selector_show(\"$name\", \"$o->[0]\", $optnames)'");
1239 foreach my $o (@$opts) {
1240 local $cls = $o->[0] eq $sel ? "selector_shown" : "selector_hidden";
1241 $rv .= "<div id=sel_${name}_$o->[0] class=$cls>".$o->[2]."</div>\n";
1246 sub ui_radio_selector_javascript
1250 .selector_shown {display:inline}
1251 .selector_hidden {display:none}
1254 function selector_show(name, value, values)
1256 for(var i=0; i<values.length; i++) {
1257 var divobj = document.getElementById('sel_'+name+'_'+values[i]);
1258 divobj.className = value == values[i] ? 'selector_shown'
1259 : 'selector_hidden';
1266 ####################### grid layout functions
1268 # ui_grid_table(&elements, columns, [width-percent], [tds], [tabletags],
1270 # Given a list of HTML elements, formats them into a table with the given
1271 # number of columns. However, themes are free to override this to use fewer
1272 # columns where space is limited.
1275 return &theme_ui_grid_table(@_) if (defined(&theme_ui_grid_table));
1276 local ($elements, $cols, $width, $tds, $tabletags, $title) = @_;
1277 return "" if (!@$elements);
1278 local $rv = "<table".
1279 ($width ? " width=$width%" : "").
1280 ($tabletags ? " ".$tabletags : "").
1283 for($i=0; $i<@$elements; $i++) {
1284 $rv .= "<tr>" if ($i%$cols == 0);
1285 $rv .= "<td ".$tds->[$i%$cols]." valign=top>".$elements->[$i]."</td>\n";
1286 $rv .= "</tr>" if ($i%$cols == $cols-1);
1290 $rv .= "<td ".$tds->[$i%$cols]."><br></td>\n";
1295 $rv .= "</table>\n";
1296 if (defined($title)) {
1297 $rv = "<table class=ui_table border ".
1298 ($width ? " width=$width%" : "").">\n".
1299 ($title ? "<tr $tb> <td><b>$title</b></td> </tr>\n" : "").
1300 "<tr $cb> <td>$rv</td> </tr>\n".
1306 # ui_radio_table(name, selected, &rows)
1307 # Returns HTML for a table of radio buttons, each of which has a label and
1308 # some associated inputs to the right.
1311 return &theme_ui_radio_table(@_) if (defined(&theme_ui_radio_table));
1312 local ($name, $sel, $rows) = @_;
1313 return "" if (!@$rows);
1314 local $rv = "<table>\n";
1315 foreach my $r (@$rows) {
1317 $rv .= "<td valign=top><b>".&ui_oneradio($name, $r->[0], $r->[1],
1318 $r->[0] eq $sel)."</b></td>\n";
1319 $rv .= "<td valign=top>".$r->[2]."</td>\n";
1322 $rv .= "</table>\n";
1326 # ui_up_down_arrows(uplink, downlink, up-show, down-show)
1327 # Returns HTML for moving some objects in a table up or down
1328 sub ui_up_down_arrows
1330 return &theme_ui_up_down_arrows(@_) if (defined(&theme_ui_up_down_arrows));
1331 local ($uplink, $downlink, $upshow, $downshow) = @_;
1333 local $imgdir = "$gconfig{'webprefix'}/images";
1335 $mover .= "<a href=\"$downlink\">".
1336 "<img src=$imgdir/movedown.gif border=0></a>";
1339 $mover .= "<img src=$imgdir/movegap.gif>";
1342 $mover .= "<a href=\"$uplink\">".
1343 "<img src=$imgdir/moveup.gif border=0></a>";
1346 $mover .= "<img src=$imgdir/movegap.gif>";
1352 # Returns a horizontal row tag
1355 return &theme_ui_hr() if (defined(&theme_ui_hr));
1359 ####################### javascript functions
1361 # js_disable_input(&disable-inputs, &enable-inputs, [tag])
1362 # Returns Javascript to disable some form elements and enable others
1363 sub js_disable_inputs
1367 foreach $f (@{$_[0]}) {
1368 $rv .= "e = form.elements[\"$f\"]; e.disabled = true; ";
1369 $rv .= "for(i=0; i<e.length; i++) { e[i].disabled = true; } ";
1371 foreach $f (@{$_[1]}) {
1372 $rv .= "e = form.elements[\"$f\"]; e.disabled = false; ";
1373 $rv .= "for(i=0; i<e.length; i++) { e[i].disabled = false; } ";
1375 foreach $f (@{$_[1]}) {
1376 if ($f =~ /^(.*)_def$/ && &indexof($1, @{$_[1]}) >= 0) {
1377 # When enabling both a _def field and its associated text field,
1378 # disable the text if the _def is set to 1
1380 $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 } } ";
1383 return $_[2] ? "$_[2]='$rv'" : $rv;
1386 # js_checkbox_disable(name, &checked-disable, &checked-enable, [tag])
1387 sub js_checkbox_disable
1391 foreach $f (@{$_[1]}) {
1392 $rv .= "form.elements[\"$f\"].disabled = $_[0].checked; ";
1394 foreach $f (@{$_[2]}) {
1395 $rv .= "form.elements[\"$f\"].disabled = !$_[0].checked; ";
1397 return $_[3] ? "$_[3]='$rv'" : $rv;