Handle hostnames with upper-case letters
[webmin.git] / Webmin / Section.pm
1 package Webmin::Section;
2 use WebminCore;
3
4 =head2 new Webmin::Section(header, [columns], [title], [width])
5 Create a new form section, which has a header and contains some inputs
6 =cut
7 sub new
8 {
9 if (defined(&Webmin::Theme::Section::new) &&
10     caller() !~ /Webmin::Theme::Section/) {
11         return new Webmin::Theme::Section(@_[1..$#_]);
12         }
13 my ($self, $header, $columns, $title, $width) = @_;
14 $self = { 'columns' => 4 };
15 bless($self);
16 $self->set_header($header);
17 $self->set_columns($columns) if (defined($columns));
18 $self->set_title($title) if (defined($title));
19 $self->set_width($width) if (defined($width));
20 return $self;
21 }
22
23 =head2 html()
24 Returns the HTML for this form section
25 =cut
26 sub html
27 {
28 my ($self) = @_;
29 my $rv;
30 $rv .= &ui_table_start($self->{'header'},
31                      $self->{'width'} ? "width=$self->{'width'}" : undef,
32                      $self->{'columns'});
33 foreach my $i (@{$self->{'inputs'}}) {
34         if (is_input($i->[1])) {
35                 my $errs;
36                 my @errs = $self->{'form'}->field_errors($i->[1]->get_name());
37                 if (@errs) {
38                         foreach my $e (@errs) {
39                                 $errs .= "<br><font color=#ff0000>$e</font>\n";
40                                 }
41                         }
42                 $rv .= &ui_table_row($i->[0], $i->[1]->html().$errs,
43                                            $i->[2]);
44                 }
45         else {
46                 $rv .= &ui_table_row($i->[0],
47                         ref($i->[1]) ? $i->[1]->html() : $i->[1], $i->[2]);
48                 }
49         }
50 $rv .= &ui_table_end();
51 return $rv;
52 }
53
54 =head2 add_input(label, input, [columns])
55 Adds some Webmin::Input object to this form section
56 =cut
57 sub add_input
58 {
59 my ($self, $label, $input, $cols) = @_;
60 push(@{$self->{'inputs'}}, [ $label, $input, $cols ]);
61 $input->set_form($self->{'form'});
62 }
63
64 =head2 add_row(label, text, [columns])
65 Adds a non-editable row to this form section
66 =cut
67 sub add_row
68 {
69 my ($self, $label, $text, $cols) = @_;
70 push(@{$self->{'inputs'}}, [ $label, $text, $cols ]);
71 }
72
73 =head2 add_separator()
74 Adds some kind of separator at this point in the section
75 =cut
76 sub add_separator
77 {
78 my ($self) = @_;
79 push(@{$self->{'inputs'}}, [ undef, "<hr>", $self->{'columns'} ]);
80 }
81
82 sub set_header
83 {
84 my ($self, $header) = @_;
85 $self->{'header'} = $header;
86 }
87
88 sub set_columns
89 {
90 my ($self, $columns) = @_;
91 $self->{'columns'} = $columns;
92 }
93
94 sub set_title
95 {
96 my ($self, $title) = @_;
97 $self->{'title'} = $title;
98 }
99
100 =head2 set_width([number|number%])
101 Sets the width of this section. Can be called with 100%, 500, or undef to use
102 the minimum possible width.
103 =cut
104 sub set_width
105 {
106 my ($self, $width) = @_;
107 $self->{'width'} = $width;
108 }
109
110 =head2 validate()
111 Validates all form inputs, based on the given CGI input hash. Returns a list
112 of errors, each of which is field name, error message and field label.
113 =cut
114 sub validate
115 {
116 my ($self) = @_;
117 my @errs;
118 foreach my $i (@{$self->{'inputs'}}) {
119         if (is_input($i->[1])) {
120                 foreach my $e ($i->[1]->validate()) {
121                         push(@errs, [ $i->[1]->get_name(), $e, $i->[0] ]);
122                         }
123                 }
124         }
125 return @errs;
126 }
127
128 =head2 get_value(input-name)
129 Returns the value of the input with the given name.
130 =cut
131 sub get_value
132 {
133 my ($self, $name) = @_;
134 foreach my $i (@{$self->{'inputs'}}) {
135         if (is_input($i->[1]) && $i->[1]->get_name() eq $name) {
136                 return $i->[1]->get_value();
137                 }
138         }
139 return undef;
140 }
141
142 =head2 set_form(form)
143 Called by the Webmin::Form object when this section is added to it
144 =cut
145 sub set_form
146 {
147 my ($self, $form) = @_;
148 $self->{'form'} = $form;
149 foreach my $i (@{$self->{'inputs'}}) {
150         if (is_input($i->[1])) {
151                 $i->[1]->set_form($form);
152                 }
153         }
154 }
155
156 sub list_inputs
157 {
158 my ($self) = @_;
159 return map { $_->[1] } grep { is_input($_->[1]) } @{$self->{'inputs'}};
160 }
161
162 =head2 is_input(object)
163 =cut
164 sub is_input
165 {
166 my ($object) = @_;
167 return ref($object) && ref($object) =~ /::/ &&
168        $object->isa("Webmin::Input");
169 }
170
171 1;
172