forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.pm
660 lines (603 loc) · 15.9 KB
/
Table.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package WebminUI::Table;
use WebminUI::JavascriptButton;
use WebminCore;
=head2 new WebminUI::Table(&headings, [width], [name], [heading])
Create a multi-column table, with support for sorting, paging and so on
=cut
sub new
{
if (defined(&WebminUI::Theme::Table::new) &&
caller() !~ /WebminUI::Theme::Table/) {
return new WebminUI::Theme::Table(@_[1..$#_]);
}
my ($self, $headings, $width, $name, $heading) = @_;
$self = { 'sorter' => [ map { \&default_sorter } @$headings ] };
bless($self);
$self->set_headings($headings);
$self->set_name($name) if (defined($name));
$self->set_width($width) if (defined($width));
$self->set_heading($heading) if (defined($heading));
$self->set_all_sortable(1);
$self->set_paging(1);
return $self;
}
=head2 add_row(&fields)
Adds a row to the table. Each element in the row can be either an input of some
kind, or a piece of text.
=cut
sub add_row
{
my ($self, $fields) = @_;
push(@{$self->{'rows'}}, $fields);
}
=head2 html()
Returns the HTML for this table. The actual ordering may depend upon sort headers
clicked by the user. The rows to display may be limited by the page size.
=cut
sub html
{
my ($self) = @_;
my @srows = @{$self->{'rows'}};
my $thisurl = $self->{'form'}->{'page'}->get_myurl();
my $name = $self->get_name();
my $rv;
# Add the heading
if ($self->get_heading()) {
$rv .= &ui_subheading($self->get_heading())."\n";
}
my $sm = $self->get_searchmax();
if (defined($sm) && @srows > $sm) {
# Too many rows to show .. add a search form. This will need to close
# the parent form, and then re-open it after the search form, as nested
# forms aren't allowed!
if ($self->get_searchmsg()) {
$rv .= $self->get_searchmsg()."<br>\n";
}
my $form = new WebminUI::Form($thisurl, "get");
$form->set_input($self->{'form'}->{'in'});
my $section = new WebminUI::Section(undef, 2);
$form->add_section($section);
my $col = new WebminUI::Select("ui_searchcol_".$name, undef);
my $i = 0;
foreach my $h (@{$self->get_headings()}) {
if ($self->{'sortable'}->[$i]) {
$col->add_option($i, $h);
}
$i++;
}
$section->add_input($text{'ui_searchcol'}, $col);
my $for = new WebminUI::Textbox("ui_searchfor_".$name, undef, 30);
$section->add_input($text{'ui_searchfor'}, $for);
$rv .= $section->html();
my $url = $self->make_url(undef, undef, undef, undef, 1);
my $jsb = new WebminUI::JavascriptButton($text{'ui_searchok'},
"window.location = '$url'+'&'+'ui_searchfor_${name}'+'='+escape(form.ui_searchfor_${name}.value)+'&'+'ui_searchcol_${name}'+'='+escape(form.ui_searchcol_${name}.selectedIndex)");
$rv .= $jsb->html();
$rv .= "<br>\n";
# Limit records to current search
if (defined($col->get_value())) {
my $sf = $for->get_value();
@srows = grep { $_->[$col->get_value()] =~ /\Q$sf\E/i } @srows;
}
else {
@srows = ( );
}
}
# Prepare the selector
my $selc = $self->{'selectcolumn'};
my $seli = $self->{'selectinput'};
my %selmap;
if (defined($selc)) {
my $i = 0;
foreach my $r (@srows) {
$selmap{$r,$selc} = $seli->one_html($i);
$i++;
}
}
# Sort the rows
my ($sortcol, $sortdir) = $self->get_sortcolumn();
if (defined($sortcol)) {
my $func = $self->{'sorter'}->[$sortcol];
@srows = sort { my $so = &$func($a->[$sortcol], $b->[$sortcol], $sortcol);
$sortdir ? -$so : $so } @srows;
}
# Build the td attributes
my @tds = map { "valign=top" } @{$self->{'headings'}};
if ($self->{'widths'}) {
my $i = 0;
foreach my $w (@{$self->{'widths'}}) {
$tds[$i++] .= " width=$w";
}
}
if ($self->{'aligns'}) {
my $i = 0;
foreach my $a (@{$self->{'aligns'}}) {
$tds[$i++] .= " align=$a";
}
}
# Find the page we want
my $page = $self->get_pagepos();
my ($start, $end, $origsize);
if ($self->get_paging() && $self->get_pagesize()) {
# Restrict view to rows within some page
$start = $self->get_pagesize()*$page;
$end = $self->get_pagesize()*($page+1) - 1;
if ($start >= @srows) {
# Gone off end!
$start = 0;
$end = $self->get_pagesize()-1;
}
if ($end >= @srows) {
# End is too far
$end = @srows-1;
}
$origsize = scalar(@srows);
@srows = @srows[$start..$end];
}
# Generate the headings, with sorters
$thisurl .= $thisurl =~ /\?/ ? "&" : "?";
my @sheadings;
my $i = 0;
foreach my $h (@{$self->get_headings()}) {
if ($self->{'sortable'}->[$i]) {
# Column can be sorted!
my $hh = "<table cellpadding=0 cellspacing=0 width=100%><tr>";
$hh .= "<td><b>$h</b></td> <td align=right>";
if (!defined($sortcol) || $sortcol != $i) {
# Not sorting on this column .. show grey button
my $url = $self->make_url($i, 0, undef, undef);
$hh .= "<a href='$url'>".
"<img src=$gconfig{'webprefix'}/images/nosort.gif border=0></a>";
}
else {
# Sorting .. show button to switch mode
my $notsort = !$sortdir;
my $url = $self->make_url($i, $sortdir ? 0 : 1, undef, undef);
$hh .= "<a href='$url'>".
"<img src=$gconfig{'webprefix'}/images/sort.gif border=0></a>";
}
$hh .= "</td></tr></table>";
push(@sheadings, $hh);
}
else {
push(@sheadings, $h);
}
$i++;
}
# Get any errors for inputs
my @errs = map { $_->get_errors() } $self->list_inputs();
if (@errs) {
foreach my $e (@errs) {
$rv .= "<font color=#ff0000>$e</font><br>\n";
}
}
# Build links for top and bottom
my $links;
if (ref($seli) =~ /Checkboxes/) {
# Add select all/none links
my $formno = $self->{'form'}->get_formno();
$links .= &select_all_link($seli->get_name(), $formno,
$text{'ui_selall'})."\n";
$links .= &select_invert_link($seli->get_name(), $formno,
$text{'ui_selinv'})."\n";
$links .= " \n";
}
foreach my $l (@{$self->{'links'}}) {
$links .= "<a href='$l->[0]'>$l->[1]</a>\n";
}
$links .= "<br>" if ($links);
# Build list of inputs for bottom
my $inputs;
foreach my $i (@{$self->{'inputs'}}) {
$inputs .= $i->html()."\n";
}
$inputs .= "<br>" if ($inputs);
# Create the pager
if ($self->get_paging() && $origsize) {
my $lastpage = int(($origsize-1)/$self->get_pagesize());
$rv .= "<center>";
if ($page != 0) {
# Add start and left arrows
my $surl = $self->make_url(undef, undef, undef, 0);
$rv .= "<a href='$surl'><img src=$gconfig{'webprefix'}/images/first.gif border=0 align=middle></a>\n";
my $lurl = $self->make_url(undef, undef, undef, $page-1);
$rv .= "<a href='$lurl'><img src=$gconfig{'webprefix'}/images/left.gif border=0 align=middle></a>\n";
}
else {
# Start and left are disabled
$rv .= "<img src=$gconfig{'webprefix'}/images/first-grey.gif border=0 align=middle>\n";
$rv .= "<img src=$gconfig{'webprefix'}/images/left-grey.gif border=0 align=middle>\n";
}
$rv .= &text('ui_paging', $start+1, $end+1, $origsize);
if ($end < $origsize-1) {
# Add right and end arrows
my $rurl = $self->make_url(undef, undef, undef, $page+1);
$rv .= "<a href='$rurl'><img src=$gconfig{'webprefix'}/images/right.gif border=0 align=middle></a>\n";
my $eurl = $self->make_url(undef, undef, undef, $lastpage);
$rv .= "<a href='$eurl'><img src=$gconfig{'webprefix'}/images/last.gif border=0 align=middle></a>\n";
}
else {
# Right and end are disabled
$rv .= "<img src=$gconfig{'webprefix'}/images/right-grey.gif border=0 align=middle>\n";
$rv .= "<img src=$gconfig{'webprefix'}/images/last-grey.gif border=0 align=middle>\n";
}
$rv .= "</center>\n";
}
# Create actual table
if (@srows) {
$rv .= $links;
$rv .= &ui_columns_start(\@sheadings, $self->{'width'}, 0, \@tds);
foreach my $r (@srows) {
my @row;
for(my $i=0; $i<@$r || $i<@sheadings; $i++) {
if (ref($r->[$i]) eq "ARRAY") {
my $j = $r->[$i]->[0] &&
$r->[$i]->[0]->isa("WebminUI::TableAction")
? " | " : " ";
$row[$i] = $selmap{$r,$i}.
join($j, map { ref($_) ? $_->html() : $_ }
@{$r->[$i]});
}
elsif (ref($r->[$i])) {
$row[$i] = $selmap{$r,$i}.$r->[$i]->html();
}
else {
$row[$i] = $selmap{$r,$i}.$r->[$i];
}
}
$rv .= &ui_columns_row(\@row, \@tds);
}
$rv .= &ui_columns_end();
}
elsif ($self->{'emptymsg'}) {
$rv .= $self->{'emptymsg'}."<p>\n";
}
$rv .= $links;
$rv .= $inputs;
return $rv;
}
=head2 set_form(form)
Called by the WebminUI::Form object when this table is added to it
=cut
sub set_form
{
my ($self, $form) = @_;
$self->{'form'} = $form;
foreach my $i ($self->list_inputs()) {
$i->set_form($form);
}
}
=head2 set_sorter(function, [column])
Sets a function used for sorting fields. Will be called with two field values to
compare, and a column number.
=cut
sub set_sorter
{
my ($self, $func, $col) = @_;
if (defined($col)) {
$self->{'sorter'}->[$col] = $func;
}
else {
$self->{'sorter'} = [ map { $func } @{$self->{'headings'}} ];
}
}
=head2 default_sorter(value1, value2, col)
=cut
sub default_sorter
{
my ($value1, $value2, $col) = @_;
if (ref($value1) && $value1->isa("WebminUI::TableAction")) {
$value1 = $value1->get_value();
$value2 = $value2->get_value();
}
return lc($value1) cmp lc($value2);
}
=head2 numeric_sorter(value1, value2, col)
=cut
sub numeric_sorter
{
my ($value1, $value2, $col) = @_;
return $value1 <=> $value2;
}
=head2 set_sortable(column, sortable?)
Tells the table if some column should allow sorting or not. By default, all are.
=cut
sub set_sortable
{
my ($self, $col, $sortable) = @_;
$self->{'sortable'}->[$col] = $sortable;
}
=head2 set_all_sortable(sortable?)
Enabled or disables sorting for all columns
=cut
sub set_all_sortable
{
my ($self, $sortable) = @_;
$self->{'sortable'} = [ map { $sortable } @{$self->{'headings'}} ];
}
=head2 get_sortcolumn()
Returns the column to sort on and the order (1 for descending), or undef for none
=cut
sub get_sortcolumn
{
my ($self) = @_;
my $in = $self->{'form'} ? $self->{'form'}->{'in'} : undef;
my $name = $self->get_name();
if ($in && defined($in->{"ui_sortcolumn_".$name})) {
return ( $in->{"ui_sortcolumn_".$name},
$in->{"ui_sortdir_".$name} );
}
else {
return ( $self->{'sortcolumn'}, $self->{'sortdir'} );
}
}
=head2 set_sortcolumn(num, descending?)
Sets the default column on which sorting will be done, unless overridden by
the user.
=cut
sub set_sortcolumn
{
my ($self, $col, $desc) = @_;
$self->{'sortcolumn'} = $col;
$self->{'sortdir'} = $desc;
}
=head2 get_paging()
Returns 1 if page-by-page display should be used
=cut
sub get_paging
{
my ($self) = @_;
my $in = $self->{'form'} ? $self->{'form'}->{'in'} : undef;
my $name = $self->get_name();
if ($in && defined($in->{"ui_paging_".$name})) {
return ( $in->{"ui_paging_".$name} );
}
else {
return ( $self->{'paging'} );
}
}
=head2 set_paging(paging?)
Turns page-by-page display of the table on or off
=cut
sub set_paging
{
my ($self, $paging) = @_;
$self->{'paging'} = $paging;
}
sub set_name
{
my ($self, $name) = @_;
$self->{'name'} = $name;
}
=head2 get_name()
Returns the name for indentifying this table in HTML
=cut
sub get_name
{
my ($self) = @_;
if (defined($self->{'name'})) {
return $self->{'name'};
}
elsif ($self->{'form'}) {
my $secs = $self->{'form'}->{'sections'};
for(my $i=0; $i<@$secs; $i++) {
return "table".$i if ($secs->[$i] eq $self);
}
}
return "table";
}
sub set_headings
{
my ($self, $headings) = @_;
$self->{'headings'} = $headings;
}
sub get_headings
{
my ($self) = @_;
return $self->{'headings'};
}
=head2 set_selector(column, input)
Takes a WebminUI::Checkboxes or WebminUI::Radios object, and uses it to add checkboxes
in the specified column.
=cut
sub set_selector
{
my ($self, $col, $input) = @_;
$self->{'selectcolumn'} = $col;
$self->{'selectinput'} = $input;
$input->set_form($form);
}
=head2 get_selector()
Returns the UI element used for selecting rows
=cut
sub get_selector
{
my ($self) = @_;
return wantarray ? ( $self->{'selectinput'}, $self->{'selectcolumn'} )
: $self->{'selectinput'};
}
=head2 set_widths(&widths)
Given an array reference of widths (like 50 or 20%), uses them for the columns
in the table.
=cut
sub set_widths
{
my ($self, $widths) = @_;
$self->{'widths'} = $widths;
}
=head2 set_width([number|number%])
Sets the width of this entire table. Can be called with 100%, 500 or undef to use
the minimum possible width.
=cut
sub set_width
{
my ($self, $width) = @_;
$self->{'width'} = $width;
}
=head2 set_aligns(&aligns)
Given an array reference of horizontal alignments (like left or right), uses them
for the columns in the table.
=cut
sub set_aligns
{
my ($self, $aligns) = @_;
$self->{'aligns'} = $aligns;
}
=head2 validate()
Validates all inputs, and returns a list of error messages
=cut
sub validate
{
my ($self) = @_;
my $seli = $self->{'selectinput'};
my @errs;
if ($seli) {
push(@errs, map { [ $seli->get_name(), $_ ] } $seli->validate());
}
foreach my $i ($self->list_inputs()) {
foreach my $e ($i->validate()) {
push(@errs, [ $i->get_name(), $e ]);
}
}
return @errs;
}
=head2 get_value(input-name)
Returns the value of the input with the given name.
=cut
sub get_value
{
my ($self, $name) = @_;
if ($self->{'selectinput'} && $self->{'selectinput'}->get_name() eq $name) {
return $self->{'selectinput'}->get_value();
}
foreach my $i ($self->list_inputs()) {
if ($i->get_name() eq $name) {
return $i->get_value();
}
}
return undef;
}
=head2 list_inputs()
Returns all inputs in all form sections
=cut
sub list_inputs
{
my ($self) = @_;
my @rv = @{$self->{'inputs'}};
push(@rv, $self->{'selectinput'}) if ($self->{'selectinput'});
return @rv;
}
=head2 set_searchmax(num, [message])
Sets the maximum number of table rows to display before showing a search form
=cut
sub set_searchmax
{
my ($self, $searchmax, $searchmsg) = @_;
$self->{'searchmax'} = $searchmax;
$self->{'searchmsg'} = $searchmsg;
}
sub get_searchmax
{
my ($self) = @_;
return $self->{'searchmax'};
}
sub get_searchmsg
{
my ($self) = @_;
return $self->{'searchmsg'};
}
=head2 add_link(link, message)
Adds a link to the table, for example for adding a new entry
=cut
sub add_link
{
my ($self, $link, $msg) = @_;
push(@{$self->{'links'}}, [ $link, $msg ]);
}
=head2 add_input(input)
Adds some input to be displayed at the bottom of the table
=cut
sub add_input
{
my ($self, $input) = @_;
push(@{$self->{'inputs'}}, $input);
$input->set_form($self->{'form'});
}
=head2 set_emptymsg(message)
Sets the message to display when the table is empty
=cut
sub set_emptymsg
{
my ($self, $emptymsg) = @_;
$self->{'emptymsg'} = $emptymsg;
}
=head2 set_heading(text)
Sets the heading text to appear above the table
=cut
sub set_heading
{
my ($self, $heading) = @_;
$self->{'heading'} = $heading;
}
sub get_heading
{
my ($self) = @_;
return $self->{'heading'};
}
=head2 set_pagesize(pagesize)
Sets the size of a page. Set to 0 to turn off completely.
=cut
sub set_pagesize
{
my ($self, $pagesize) = @_;
$self->{'pagesize'} = $pagesize;
}
=head2 get_pagesize()
Returns the size of a page, or 0 if paging is turned off totally
=cut
sub get_pagesize
{
my ($self) = @_;
return $self->{'pagesize'};
}
sub get_pagepos
{
my ($self) = @_;
my $in = $self->{'form'} ? $self->{'form'}->{'in'} : undef;
my $name = $self->get_name();
if ($in && defined($in->{"ui_pagepos_".$name})) {
return ( $in->{"ui_pagepos_".$name} );
}
else {
return ( $self->{'pagepos'} );
}
}
=head2 make_url(sortcol, sortdir, paging, page, [no-searchargs], [no-pagearg])
Returns a link to this table's page, with the defaults for the various state
fields overriden by the parameters (where defined)
=cut
sub make_url
{
my ($self, $newsortcol, $newsortdir, $newpaging, $newpagepos,
$nosearch, $nopage) = @_;
my ($sortcol, $sortdir) = $self->get_sortcolumn();
$sortcol = $newsortcol if (defined($newsortcol));
$sortdir = $newsortdir if (defined($newsortdir));
my $paging = $self->get_paging();
$paging = $newpaging if (defined($newpaging));
my $pagepos = $self->get_pagepos();
$pagepos = $newpagepos if (defined($newpagepos));
my $thisurl = $self->{'form'}->{'page'}->get_myurl();
my $name = $self->get_name();
$thisurl .= $thisurl =~ /\?/ ? "&" : "?";
my $in = $self->{'form'}->{'in'};
return "${thisurl}ui_sortcolumn_${name}=$sortcol".
"&ui_sortdir_${name}=$sortdir".
"&ui_paging_${name}=$paging".
($nopage ? "" : "&ui_pagepos_${name}=$pagepos").
($nosearch ? "" : "&ui_searchfor_${name}=".
&urlize($in->{"ui_searchfor_${name}"}).
"&ui_searchcol_${name}=".
&urlize($in->{"ui_searchcol_${name}"}));
}
1;