import rt 3.6.6
[freeside.git] / rt / html / Search / Elements / BuildFormatString
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2007 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/copyleft/gpl.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 <%args>
49 $Format => undef
50 %cfqueues => undef
51 $Face => undef
52 $Size => undef
53 $Link => undef
54 $Title => undef
55 $AddCol => undef
56 $RemoveCol => undef
57 $ColUp => undef
58 $ColDown => undef
59 $SelectDisplayColumns => undef
60 $CurrentDisplayColumns => undef
61 </%args>
62 <%init>
63
64 unless ($Format) {         
65     $Format = $RT::DefaultSearchResultFormat;
66 }   
67    
68
69 # All the things we can display in the format string by default
70 my @fields = qw(
71   id
72   Status
73   ExtendedStatus
74   Subject
75   QueueName
76   OwnerName
77   Priority
78   InitialPriority
79   FinalPriority
80   Type
81   TimeWorked
82   TimeLeft
83   TimeEstimated
84   CreatedBy
85   LastUpdatedBy
86   Requestors
87   Cc
88   AdminCc
89   Starts
90   StartsRelative
91   Started
92   StartedRelative
93   Created
94   CreatedRelative
95   LastUpdated
96   LastUpdatedRelative
97   Told
98   ToldRelative
99   Due
100   DueRelative
101   Resolved
102   ResolvedRelative
103   RefersTo
104   ReferredToBy
105   DependsOn
106   DependedOnBy
107   MemberOf
108   Members
109   Parents
110   Children
111   NEWLINE
112 );
113
114 my $CustomFields = RT::CustomFields->new( $session{'CurrentUser'});
115 foreach my $id (keys %cfqueues) {
116     # What does this _do_? What are the keys to cfqueues
117     $id =~ s/^.'*(.*).'*$/$1/;
118     # Gotta load up the $queue object, since queues get stored by name now.
119     my $queue = RT::Queue->new($session{'CurrentUser'});
120     $queue->Load($id);
121     $CustomFields->LimitToQueue($queue->Id);
122 }
123 $CustomFields->LimitToGlobal;
124
125 while ( my $CustomField = $CustomFields->Next ) {
126     push @fields, "CustomField.{" . $CustomField->Name . "}";
127 }
128
129 my ( @seen);
130
131 my @format = split( /,\s*/, $Format );
132 foreach my $field (@format) {
133     my %column = ();
134     $field =~ s/'(.*)'/$1/;
135     my ( $prefix, $suffix );
136     if ( $field =~ m/(.*)__(.*)__(.*)/ ) {
137         $prefix = $1;
138         $suffix = $3;
139         $field  = $2;
140     }
141     $field = "<blank>" if !$field;
142     $column{Prefix} = $prefix;
143     $column{Suffix} = $suffix;
144     $field =~ s/\s*(.*)\s*/$1/;
145     $column{Column} = $field;
146     push @seen, \%column;
147 }
148
149 if ( $RemoveCol ) {
150     my $index  = $CurrentDisplayColumns;
151     my $column = $seen[$index];
152     if ($index) {
153         delete $seen[$index];
154         my @temp = @seen;
155         @seen = ();
156         foreach my $element (@temp) {
157             next unless $element;
158             push @seen, $element;
159         }
160     }
161 }
162 elsif ( $AddCol ) {
163     if ( defined $SelectDisplayColumns ) {
164         my $selected = $SelectDisplayColumns;
165         my @columns;
166         if (ref($selected) eq 'ARRAY') {
167             @columns = @$selected;
168         } else {
169             push @columns, $selected;
170         }
171         foreach my $col (@columns) {
172             my %column = ();
173             $column{Column} = $col;
174
175     if ( $Face eq "Bold" ) {
176         $column{Prefix} .= "<b>";
177         $column{Suffix} .= "</b>";
178     }
179     if ( $Face eq "Italic" ) {
180         $column{Prefix} .= "<i>";
181         $column{Suffix} .= "</i>";
182     }
183     if ($Size) {
184         $column{Prefix} .= "<" . $m->interp->apply_escapes( $Size,  'h' ) . ">";
185         $column{Suffix} .= "</" . $m->interp->apply_escapes( $Size, 'h' ) . ">";
186     }
187     if ( $Link eq "Display" ) {
188         $column{Prefix} .=
189           "<a HREF=\"" . $RT::WebPath . "/Ticket/Display.html?id=__id__\">";
190         $column{Suffix} .= "</a>";
191     }
192     elsif ( $Link eq "Take" ) {
193         $column{Prefix} .= "<a HREF=\"" . $RT::WebPath
194           . "/Ticket/Display.html?Action=Take&id=__id__\">";
195         $column{Suffix} .= "</a>";
196     }
197
198     if ($Title) {
199         $column{Suffix} .= "/TITLE:" . $m->interp->apply_escapes( $Title, 'h' );
200     }
201     push @seen, \%column;
202 }
203 }
204 }
205 elsif ( $ColUp ) {
206     my $index = $CurrentDisplayColumns;
207     if ( defined $index && ( $index - 1 ) >= 0 ) {
208         my $column = $seen[$index];
209         $seen[$index]       = $seen[ $index - 1 ];
210         $seen[ $index - 1 ] = $column;
211         $CurrentDisplayColumns     = $index - 1;
212     }
213 }
214 elsif ( $ColDown ) {
215     my $index = $CurrentDisplayColumns;
216     if ( defined $index && ( $index + 1 ) < scalar @seen ) {
217         my $column = $seen[$index];
218         $seen[$index]       = $seen[ $index + 1 ];
219         $seen[ $index + 1 ] = $column;
220         $CurrentDisplayColumns     = $index + 1;
221     }
222 }
223
224
225 my @format_string;
226 foreach my $field (@seen) {
227     next unless $field;
228     my $row = "'";
229     $row .= $field->{Prefix} if $field->{Prefix};
230     $row .= "__" . ($field->{Column} =~ m/\(/ ? $field->{Column} # func, don't escape
231                     : $m->interp->apply_escapes( $field->{Column}, 'h' )) . "__"
232       unless ( $field->{Column} eq "<blank>" );
233     $row .= $field->{Suffix} if $field->{Suffix};
234     $row .= "'";
235     push( @format_string, $row );
236 }
237
238 $Format = join(",\n", @format_string);
239
240
241 return($Format, \@fields, \@seen);
242
243 </%init>
244