1 package Webmin::LinkTable;
5 =head2 new Webmin::LinkTable(heading, [columns], [width], [name])
6 Creates a new table that just displays links, like in the Users and Groups module
10 if (defined(&Webmin::Theme::LinkTable::new) &&
11 caller() !~ /Webmin::Theme::LinkTable/) {
12 return new Webmin::Theme::LinkTable(@_[1..$#_]);
14 my ($self, $heading, $columns, $width, $name) = @_;
15 $self = { 'sorter' => \&Webmin::Table::default_sorter,
19 $self->set_heading($heading);
20 $self->set_name($name) if (defined($name));
21 $self->set_width($width) if (defined($width));
22 $self->set_columns($columns) if (defined($columns));
26 =head2 add_entry(name, link)
27 Adds one item to appear in the table
31 my ($self, $name, $link) = @_;
32 push(@{$self->{'entries'}}, [ $name, $link ]);
36 Returns the HTML for this table.
42 # Prepare the selector
43 my @srows = @{$self->{'entries'}};
45 if (defined($self->{'selectinput'})) {
47 foreach my $r (@srows) {
48 $selmap{$r} = $self->{'selectinput'}->one_html($i);
54 my $sortdir = $self->get_sortdir();
55 if (defined($sortdir)) {
56 my $func = $self->{'sorter'};
57 @srows = sort { my $so = &$func($a->[0], $b->[0]);
58 $sortdir ? -$so : $so } @srows;
63 my $thisurl = $self->{'form'}->{'page'}->get_myurl();
64 $thisurl .= $thisurl =~ /\?/ ? "&" : "?";
65 my $name = $self->get_name();
66 if ($self->get_sortable()) {
67 $head = "<table cellpadding=0 cellspacing=0 width=100%><tr>";
68 $head .= "<td><b>".$self->get_heading()."</b></td> <td align=right>";
69 if (!defined($sortdir)) {
70 # Not sorting .. show grey button
71 $head .= "<a href='${thisurl}ui_sortdir_${name}=0'>".
72 "<img src=/images/nosort.gif border=0></a>";
75 # Sorting .. show button to switch mode
76 my $notsort = !$sortdir;
77 $head .= "<a href='${thisurl}ui_sortdir_${name}=$notsort'>".
78 "<img src=/images/sort.gif border=0></a>";
80 $head .= "</td></tr></table>";
83 $head = $self->get_heading();
88 if ($self->{'selectinput'}) {
89 # Get any errors for inputs
90 my @errs = $self->{'form'}->field_errors(
91 $self->{'selectinput'}->get_name());
93 foreach my $e (@errs) {
94 $rv .= "<font color=#ff0000>$e</font><br>\n";
99 # Create the actual table
100 $rv .= &ui_table_start($head,
101 defined($self->{'width'}) ? "width=$self->{'width'}"
103 $rv .= "<td colspan=2><table width=100%>";
105 my $cols = $self->get_columns();
107 foreach my $r (@srows) {
108 $rv .= "<tr>\n" if ($i%$cols == 0);
109 $rv .= "<td width=$pc%>".$selmap{$r}."<a href='$r->[1]'>".
110 &html_escape($r->[0])."</a></td>\n";
111 $rv .= "<tr>\n" if ($i%$cols == $cols-1);
116 while($i++%$cols != $cols-1) {
117 $rv .= "<td width=$pc%></td>\n";
121 $rv .= "</table></td>";
122 $rv .= &ui_table_end();
126 =head2 set_heading(text)
127 Sets the heading text to appear above the table
131 my ($self, $heading) = @_;
132 $self->{'heading'} = $heading;
138 return $self->{'heading'};
143 my ($self, $name) = @_;
144 $self->{'name'} = $name;
148 Returns the name for indentifying this table in HTML
153 if (defined($self->{'name'})) {
154 return $self->{'name'};
156 elsif ($self->{'form'}) {
157 my $secs = $self->{'form'}->{'sections'};
158 for(my $i=0; $i<@$secs; $i++) {
159 return "table".$i if ($secs->[$i] eq $self);
165 =head2 set_sorter(function)
166 Sets a function used for sorting fields. Will be called with two values to compare
170 my ($self, $func) = @_;
171 $self->{'sorter'} = $func;
174 =head2 default_sorter(value1, value2)
178 my ($value1, $value2, $col) = @_;
179 return lc($value1) cmp lc($value2);
182 =head2 set_sortable(sortable?)
183 Tells the table if sorting is allowed or not. By default, it is.
187 my ($self, $sortable) = @_;
188 $self->{'sortable'} = $sortable;
194 return $self->{'sortable'};
198 Returns the order to sort in (1 for descending)
203 my $in = $self->{'form'} ? $self->{'form'}->{'in'} : undef;
204 my $name = $self->get_name();
205 if ($in && defined($in->{"ui_sortdir_".$name})) {
206 return ( $in->{"ui_sortdir_".$name} );
209 return ( $self->{'sortdir'} );
213 =head2 set_sortdir(descending?)
214 Sets the default sort direction, unless overridden by the user.
218 my ($self, $desc) = @_;
219 $self->{'sortdir'} = $desc;
222 =head2 set_width([number|number%])
223 Sets the width of this entire table. Can be called with 100%, 500 or undef to use
224 the minimum possible width.
228 my ($self, $width) = @_;
229 $self->{'width'} = $width;
232 =head2 set_columns(cols)
233 Sets the number of columns to display
237 my ($self, $columns) = @_;
238 $self->{'columns'} = $columns;
244 return $self->{'columns'};
247 =head2 set_form(form)
248 Called by the Webmin::Form object when this table is added to it
252 my ($self, $form) = @_;
253 $self->{'form'} = $form;
254 if ($self->{'selectinput'}) {
255 $self->{'selectinput'}->set_form($form);
259 =head2 set_selector(input)
260 Takes a Webmin::Checkboxes or Webmin::Radios object, and uses it to add checkboxes
265 my ($self, $input) = @_;
266 $self->{'selectinput'} = $input;
267 $input->set_form($form);
270 =head2 get_selector()
271 Returns the UI element used for selecting rows
276 return $self->{'selectinput'};
280 Validates the selector input
285 my $seli = $self->{'selectinput'};
287 return map { [ $seli->get_name(), $_ ] } $seli->validate();
292 =head2 get_value(input-name)
293 Returns the value of the input with the given name.
297 my ($self, $name) = @_;
298 if ($self->{'selectinput'} && $self->{'selectinput'}->get_name() eq $name) {
299 return $self->{'selectinput'}->get_value();
305 Returns all inputs in all form sections
310 return $self->{'selectinput'} ? ( $self->{'selectinput'} ) : ( );