import rt 3.8.8
[freeside.git] / rt / share / html / Elements / CollectionList
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %# 
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %# 
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 <%INIT>
49 if (!$Collection && $Class eq 'RT::Tickets') {
50     $Collection = RT::Tickets->new( $session{'CurrentUser'} );
51     $Collection->FromSQL($Query);
52 }
53
54 my $TotalFound = $Collection->CountAll();
55 return '' if !$TotalFound && !$ShowEmpty;
56
57 # XXX: ->{'order_by'} is hacky, but there is no way to check if
58 # collection is ordered or not
59 if ( @OrderBy && ($AllowSorting || !$Collection->{'order_by'}) ) {
60     if ( $OrderBy[0] =~ /\|/ ) {
61         @OrderBy = split /\|/, $OrderBy[0];
62         @Order = split /\|/,$Order[0];
63     }
64     $Collection->OrderByCols(
65         map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
66         ( 0 .. $#OrderBy )
67     );
68 }
69
70 $Collection->RowsPerPage( $Rows ) if $Rows;
71 $Page = 1 unless $Page && $Page > 0;    # workaround problems with $Page = '' or undef
72 $Collection->GotoPage( $Page - 1 ); # SB uses page 0 as the first page
73
74 # DisplayFormat lets us use a "temporary" format for display, while 
75 # still using our original format for next/prev page links.
76 # bulk update uses this feature to add checkboxes
77
78 $DisplayFormat ||= $Format;
79
80 # Scrub the html of the format string to remove any potential nasties.
81 $Format = $m->comp('/Elements/ScrubHTML', Content => $Format);
82 $DisplayFormat = $m->comp('/Elements/ScrubHTML', Content => $DisplayFormat);
83
84 my @Format = $m->comp('/Elements/CollectionAsTable/ParseFormat', Format => $DisplayFormat);
85
86 # Find the maximum number of items in any row, so we can pad the table.
87 my ($maxitems, $item) = (0, 0);
88 foreach my $col (@Format) {
89     $item++;
90     if ( $col->{title} && ($col->{title} eq 'NEWLINE') ) {
91         $item = 0;
92     }
93     else {
94         $maxitems = $item if $item > $maxitems;
95     }
96 }
97
98 $Class ||= ref $Collection;
99 $Class =~ s/s$//;
100 $Class =~ s/:/_/g;
101
102 $m->out('<table border="0" cellspacing="0" cellpadding="1"' . 
103         ' width="100%" class="' . 
104         ($Collection->isa('RT::Tickets') ? 'ticket-list' : 'collection') . '">');
105
106 if ( $ShowHeader ) {
107   $m->comp('/Elements/CollectionAsTable/Header',
108            %ARGS,
109            Class        => $Class,
110            Format       => \@Format,
111            FormatString => $Format,
112            Order        => \@Order,
113            OrderBy      => \@OrderBy,
114            Rows         => $Rows,
115            Page         => $Page,
116            AllowSorting => $AllowSorting, 
117            BaseURL      => $BaseURL,
118            GenericQueryArgs => $GenericQueryArgs,
119            maxitems     => $maxitems,
120            );
121 }
122
123 my ($i, $column_map) = (0, {});
124 while ( my $record = $Collection->Next ) {
125     # Every ten rows, flush the buffer and put something on the page.
126     $m->flush_buffer unless ++$i % 10;
127
128     my $warning = 0;
129     my $Classes = '';
130
131     $m->callback(
132         CallbackName => 'EachRow',
133         Record       => $record,
134         Warning      => \$warning,
135         Classes      => \$Classes,
136         Format       => \@Format,
137     );
138
139     $m->comp('/Elements/CollectionAsTable/Row',
140         i         => $i,
141         Format    => \@Format,
142         record    => $record,
143         maxitems  => $maxitems,
144         ColumnMap => $column_map,
145         Class     => $Class,
146         Warning   => $warning,
147         Classes   => $Classes,
148     );
149 }
150
151 $m->out('</table>');
152 if ( $Rows && $ShowNavigation && $TotalFound > $Rows ) {
153   my $oddRows = ($TotalFound && $TotalFound % $Rows == 0 )? 0 : 1;
154   my $pages = int( $TotalFound / $Rows ) + $oddRows;
155   $pages = 1 if $pages < 1;
156
157
158   my %query_args = map { $_ => $ARGS{$_} } @PassArguments;
159   $m->comp(
160         '/Elements/CollectionListPaging',
161         BaseURL     => $BaseURL,
162         Rows        => $Rows,
163         TotalFound  => $TotalFound,
164         CurrentPage => $Page,
165         Pages       => $pages,
166         URLParams   => \%query_args
167     );
168
169 }
170
171 </%INIT>
172 <%ARGS>
173 $Class         => ''
174 $Collection    => undef
175 $Format        => undef
176 $DisplayFormat => undef
177 @Order         => ()
178 @OrderBy       => ()
179 $GenericQueryArgs => undef
180 $Rows          => undef
181 $Page          => 1
182 $Title         => loc('Ticket Search')
183 $BaseURL       => RT->Config->Get('WebPath') . $m->request_comp->path .'?'
184 @PassArguments => qw( Query Format Rows Page Order OrderBy)
185
186 $AllowSorting   => 0
187 $ShowNavigation => 1
188 $ShowHeader     => 1
189 $ShowEmpty      => 0
190 $Query => 0
191 </%ARGS>