* Add enclair_db option to UserDB.pm. Allows logging of enclair password
[interchange.git] / code / UserTag / table_organize.tag
1 # Copyright 2002-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: table_organize.tag,v 1.11 2007-11-05 20:15:27 docelic Exp $
9
10 UserTag table-organize Order         cols
11 UserTag table-organize attrAlias     columns cols
12 UserTag table-organize Interpolate
13 UserTag table-organize addAttr
14 UserTag table-organize hasEndTag
15 UserTag table-organize Version       $Revision: 1.11 $
16 UserTag table-organize Routine <<EOR
17 sub {
18         my ($cols, $opt, $body) = @_;
19         $cols = int($cols) || 2;
20         $body =~ s/(.*?)(<td)\b/$2/is
21                 or return;
22         my $out = $1;
23         $body =~ s:(</td>)(?!.*</td>)(.*):$1:is;
24         my $postamble = $2;
25
26         my @cells;
27         if($opt->{cells} and ref($opt->{cells}) eq 'ARRAY') {
28                 @cells = @{$opt->{cells}};
29         }
30         elsif($opt->{embed}) {
31                 if($opt->{embed} eq 'lc') {
32                         push @cells, $1 while $body =~ s:(<td\b.*?</td>)::s;
33                 }
34                 else {
35                         push @cells, $1 while $body =~ s:(<TD\b.*?</TD>)::s;
36                 }
37         }
38         else {
39                 push @cells, $1 while $body =~ s:(<td\b.*?</td>)::is;
40         }
41
42         while ($opt->{min_rows} and ($opt->{min_rows} * ($cols - 1)) > scalar(@cells) ) {
43                 $cols--;
44                 last if $cols == 1;
45         }
46
47         if(int($opt->{limit}) and $opt->{limit} < scalar(@cells) ) {
48                 splice(@cells, $opt->{limit});
49         }
50
51         for(qw/ table/) {
52                 $opt->{$_} = defined $opt->{$_} ? " $opt->{$_}" : '';
53         }
54
55         my @td;
56
57         if(! $opt->{td}) {
58                 @td = '' x $cols;
59         }
60         elsif (ref $opt->{td} ) {
61                 @td = @{$opt->{td}};
62                 push @td, '' while scalar(@td) < $cols;
63         }
64         else {
65                 @td = (" $opt->{td}") x $cols;
66         }
67
68         my %attr;
69         for(qw/caption tr pre post/) {
70                 if( ! $opt->{$_} ) {
71                         #do nothing
72                 }
73                 elsif (ref $opt->{$_}) {
74                         $attr{$_} = $opt->{$_};
75                 }
76                 else {
77                         $attr{$_} = [$opt->{$_}];
78                 }
79         }
80
81         my $pretty = $opt->{pretty};
82
83         #$opt->{td} =~ s/^(\S)/ $1/;
84         #$opt->{tr} =~ s/^(\S)/ $1/;
85
86         my @rest;
87         my $rows;
88
89         my $rmod;
90         my $tmod = 0;
91         my $total_mod;
92
93         $opt->{filler} = '&nbsp;' if ! defined $opt->{filler};
94
95         my $td_beg;
96         my $td_end;
97         if($opt->{font}) {
98                 $td_beg = qq{<FONT $opt->{font}>};
99                 $td_end = qq{</FONT>};
100         }
101
102         if($rows = int($opt->{rows}) ) {
103                 $total_mod = $rows * $cols;
104                 @rest = splice(@cells, $total_mod)
105                         if $total_mod < @cells;
106                 $opt->{table} = ' ' if ! $opt->{table};
107         }
108
109         my $joiner = $opt->{joiner} || ($pretty ? "\n\t\t" : "");
110         while(@cells) {
111                 if ($opt->{columnize}) {
112                         my $cell_count = scalar @cells;
113                         my $row_count_ceil = POSIX::ceil($cell_count / $cols);
114                         my $row_count_floor = int($cell_count / $cols);
115                         my $remainder = $cell_count % $cols;
116                         my @tmp = splice(@cells, 0);
117                         my $index;
118                         for (my $r = 0; $r < $row_count_ceil; $r++) {
119                                 for (my $c = 0; $c < $cols; $c++) {
120                                         if ($c >= $remainder + 1) {
121                                                 $index = $r + $row_count_floor * $c + $remainder;
122                                         }
123                                         else {
124                                                 $index = $r + $row_count_ceil * $c;
125                                         }
126                                         push @cells, $tmp[$index];
127                                         last if $r + 1 == $row_count_ceil and $c + 1 == $remainder;
128                                 }
129                         }
130                 }
131
132                 while (scalar(@cells) % $cols) {
133                         push @cells, "<td>$opt->{filler}</td>";
134                 }
135
136                 #$out .= "<!-- starting table tmod=$tmod -->";
137                 if($opt->{table}) {
138                         $out .= "<table$opt->{table}>";
139                         $out .= "\n" if $pretty;
140                         if($opt->{caption}) {
141                                 my $idx = $tmod % scalar(@{$attr{caption}});
142                                 #$out .= "<!-- caption index $idx -->";
143                                 $out .= "\n" if $pretty;
144                                 $out .= "<caption>" . $attr{caption}[$idx] . "</caption>";
145                                 $out .= "\n" if $pretty;
146                         }
147                 }
148                 $rmod = 0;
149                 while(@cells) {
150                         $out .= "\t" if $pretty;
151                         $out .= "<tr";
152                         if($opt->{tr}) {
153                                 my $idx = $rmod % scalar(@{$attr{tr}});
154                                 $out .= " " . $attr{tr}[$idx];
155                         }
156                         $out .= ">";
157                         $out .= "\n\t\t" if $pretty;
158                         my @op =  splice (@cells, 0, $cols);
159                         if($opt->{td}) {
160                                 for ( my $i = 0; $i < $cols; $i++) {
161                                         $op[$i] =~ s/(<td)/$1 $td[$i]/i;
162                                 }
163                         }
164                         @op = map { s/>/>$td_beg/; $_ }                  @op    if $td_beg;
165                         @op = map { s/(<[^<]+)$/$td_end$1/; $_ } @op    if $td_end;
166
167                         $out .= join($joiner, @op);
168                         $out .= "\n\t" if $pretty;
169                         $out .= "</tr>";
170                         $out .= "\n" if $pretty;
171                         $rmod++;
172                 }
173                 if($opt->{table}) {
174                         $out .= "</table>";
175                         $out .= "\n" if $pretty;
176                 }
177                 if(@rest) {
178                         my $num = $total_mod < scalar(@rest) ? $total_mod : scalar(@rest);
179                         @cells = splice(@rest, 0, $num);
180                 }
181                 $tmod++;
182         }
183         return $out . $postamble;
184 }
185 EOR