import rt 3.4.6
[freeside.git] / rt / html / Search / Elements / BuildFormatString
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
35 %# 
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
44 %# 
45 %# END BPS TAGGED BLOCK }}}
46 <%args>
47 $Format => undef
48 %cfqueues => undef
49 $Face => undef
50 $Size => undef
51 $Link => undef
52 $Title => undef
53 $AddCol => undef
54 $RemoveCol => undef
55 $ColUp => undef
56 $ColDown => undef
57 $SelectDisplayColumns => undef
58 $CurrentDisplayColumns => undef
59 </%args>
60 <%init>
61
62 unless ($Format) {         
63     $Format = $RT::DefaultSearchResultFormat;
64 }   
65    
66
67 # All the things we can display in the format string by default
68 my @fields = qw(
69   id
70   Status
71   ExtendedStatus
72   Subject
73   QueueName
74   OwnerName
75   Priority
76   InitialPriority
77   FinalPriority
78   Type
79   TimeWorked
80   TimeLeft
81   TimeEstimated
82   CreatedBy
83   LastUpdatedBy
84   Requestors
85   Cc
86   AdminCc
87   Starts
88   StartsRelative
89   Started
90   StartedRelative
91   Created
92   CreatedRelative
93   LastUpdated
94   LastUpdatedRelative
95   Told
96   ToldRelative
97   Due
98   DueRelative
99   Resolved
100   ResolvedRelative
101   RefersTo
102   ReferredToBy
103   DependsOn
104   DependedOnBy
105   MemberOf
106   Members
107   Parents
108   Children
109   NEWLINE
110 );
111
112 my $CustomFields = RT::CustomFields->new( $session{'CurrentUser'});
113 foreach my $id (keys %cfqueues) {
114     # What does this _do_? What are the keys to cfqueues
115     $id =~ s/^.'*(.*).'*$/$1/;
116     # Gotta load up the $queue object, since queues get stored by name now.
117     my $queue = RT::Queue->new($session{'CurrentUser'});
118     $queue->Load($id);
119     $CustomFields->LimitToQueue($queue->Id);
120 }
121 $CustomFields->LimitToGlobal;
122
123 while ( my $CustomField = $CustomFields->Next ) {
124     push @fields, "CustomField.{" . $CustomField->Name . "}";
125 }
126
127 my ( @seen);
128
129 my @format = split( /,\s*/, $Format );
130 foreach my $field (@format) {
131     my %column = ();
132     $field =~ s/'(.*)'/$1/;
133     my ( $prefix, $suffix );
134     if ( $field =~ m/(.*)__(.*)__(.*)/ ) {
135         $prefix = $1;
136         $suffix = $3;
137         $field  = $2;
138     }
139     $field = "<blank>" if !$field;
140     $column{Prefix} = $prefix;
141     $column{Suffix} = $suffix;
142     $field =~ s/\s*(.*)\s*/$1/;
143     $column{Column} = $field;
144     push @seen, \%column;
145 }
146
147 if ( $RemoveCol ) {
148     my $index  = $CurrentDisplayColumns;
149     my $column = $seen[$index];
150     if ($index) {
151         delete $seen[$index];
152         my @temp = @seen;
153         @seen = ();
154         foreach my $element (@temp) {
155             next unless $element;
156             push @seen, $element;
157         }
158     }
159 }
160 elsif ( $AddCol ) {
161     if ( defined $SelectDisplayColumns ) {
162         my $selected = $SelectDisplayColumns;
163         my @columns;
164         if (ref($selected) eq 'ARRAY') {
165             @columns = @$selected;
166         } else {
167             push @columns, $selected;
168         }
169         foreach my $col (@columns) {
170             my %column = ();
171             $column{Column} = $col;
172
173     if ( $Face eq "Bold" ) {
174         $column{Prefix} .= "<B>";
175         $column{Suffix} .= "</B>";
176     }
177     if ( $Face eq "Italic" ) {
178         $column{Prefix} .= "<I>";
179         $column{Suffix} .= "</I>";
180     }
181     if ($Size) {
182         $column{Prefix} .= "<" . $m->interp->apply_escapes( $Size,  'h' ) . ">";
183         $column{Suffix} .= "</" . $m->interp->apply_escapes( $Size, 'h' ) . ">";
184     }
185     if ( $Link eq "Display" ) {
186         $column{Prefix} .=
187           "<A HREF=\"" . $RT::WebPath . "/Ticket/Display.html?id=__id__\">";
188         $column{Suffix} .= "</a>";
189     }
190     elsif ( $Link eq "Take" ) {
191         $column{Prefix} .= "<A HREF=\"" . $RT::WebPath
192           . "/Ticket/Display.html?Action=Take&id=__id__\">";
193         $column{Suffix} .= "</a>";
194     }
195
196     if ($Title) {
197         $column{Suffix} .= "/TITLE:" . $m->interp->apply_escapes( $Title, 'h' );
198     }
199     push @seen, \%column;
200 }
201 }
202 }
203 elsif ( $ColUp ) {
204     my $index = $CurrentDisplayColumns;
205     if ( defined $index && ( $index - 1 ) >= 0 ) {
206         my $column = $seen[$index];
207         $seen[$index]       = $seen[ $index - 1 ];
208         $seen[ $index - 1 ] = $column;
209         $CurrentDisplayColumns     = $index - 1;
210     }
211 }
212 elsif ( $ColDown ) {
213     my $index = $CurrentDisplayColumns;
214     if ( defined $index && ( $index + 1 ) < scalar @seen ) {
215         my $column = $seen[$index];
216         $seen[$index]       = $seen[ $index + 1 ];
217         $seen[ $index + 1 ] = $column;
218         $CurrentDisplayColumns     = $index + 1;
219     }
220 }
221
222
223 my @format_string;
224 foreach my $field (@seen) {
225     next unless $field;
226     my $row = "'" . $field->{Prefix};
227     $row .= "__" . $m->interp->apply_escapes( $field->{Column}, 'h' ) . "__"
228       unless ( $field->{Column} eq "<blank>" );
229     $row .= $field->{Suffix} . "'";
230     push( @format_string, $row );
231 }
232
233 $Format = join(",\n", @format_string);
234
235
236 return($Format, \@fields, \@seen);
237
238 </%init>
239