This commit was generated by cvs2svn to compensate for changes in r4407,
[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     my $queuestr;
125     if ($CustomField->QueueObj && $CustomField->QueueObj->Id != 0) {
126         $queuestr = $CustomField->QueueObj->Name . ".";
127     }
128     push @fields, "CustomField." . $queuestr . "{" . $CustomField->Name . "}";
129 }
130
131 my ( @seen);
132
133 my @format = split( /,\s*/, $Format );
134 foreach my $field (@format) {
135     my %column = ();
136     $field =~ s/'(.*)'/$1/;
137     my ( $prefix, $suffix );
138     if ( $field =~ m/(.*)__(.*)__(.*)/ ) {
139         $prefix = $1;
140         $suffix = $3;
141         $field  = $2;
142     }
143     $field = "<blank>" if !$field;
144     $column{Prefix} = $prefix;
145     $column{Suffix} = $suffix;
146     $field =~ s/\s*(.*)\s*/$1/;
147     $column{Column} = $field;
148     push @seen, \%column;
149 }
150
151 if ( $RemoveCol ) {
152     my $index  = $CurrentDisplayColumns;
153     my $column = $seen[$index];
154     if ($index) {
155         delete $seen[$index];
156         my @temp = @seen;
157         @seen = ();
158         foreach my $element (@temp) {
159             next unless $element;
160             push @seen, $element;
161         }
162     }
163 }
164 elsif ( $AddCol ) {
165     if ( defined $SelectDisplayColumns ) {
166         my $selected = $SelectDisplayColumns;
167         my @columns;
168         if (ref($selected) eq 'ARRAY') {
169             @columns = @$selected;
170         } else {
171             push @columns, $selected;
172         }
173         foreach my $col (@columns) {
174             my %column = ();
175             $column{Column} = $col;
176
177     if ( $Face eq "Bold" ) {
178         $column{Prefix} .= "<B>";
179         $column{Suffix} .= "</B>";
180     }
181     if ( $Face eq "Italic" ) {
182         $column{Prefix} .= "<I>";
183         $column{Suffix} .= "</I>";
184     }
185     if ($Size) {
186         $column{Prefix} .= "<" . $m->interp->apply_escapes( $Size,  'h' ) . ">";
187         $column{Suffix} .= "</" . $m->interp->apply_escapes( $Size, 'h' ) . ">";
188     }
189     if ( $Link eq "Display" ) {
190         $column{Prefix} .=
191           "<A HREF=\"" . $RT::WebPath . "/Ticket/Display.html?id=__id__\">";
192         $column{Suffix} .= "</a>";
193     }
194     elsif ( $Link eq "Take" ) {
195         $column{Prefix} .= "<A HREF=\"" . $RT::WebPath
196           . "/Ticket/Display.html?Action=Take&id=__id__\">";
197         $column{Suffix} .= "</a>";
198     }
199
200     if ($Title) {
201         $column{Suffix} .= "/TITLE:" . $m->interp->apply_escapes( $Title, 'h' );
202     }
203     push @seen, \%column;
204 }
205 }
206 }
207 elsif ( $ColUp ) {
208     my $index = $CurrentDisplayColumns;
209     if ( defined $index && ( $index - 1 ) >= 0 ) {
210         my $column = $seen[$index];
211         $seen[$index]       = $seen[ $index - 1 ];
212         $seen[ $index - 1 ] = $column;
213         $CurrentDisplayColumns     = $index - 1;
214     }
215 }
216 elsif ( $ColDown ) {
217     my $index = $CurrentDisplayColumns;
218     if ( defined $index && ( $index + 1 ) < scalar @seen ) {
219         my $column = $seen[$index];
220         $seen[$index]       = $seen[ $index + 1 ];
221         $seen[ $index + 1 ] = $column;
222         $CurrentDisplayColumns     = $index + 1;
223     }
224 }
225
226
227 my @format_string;
228 foreach my $field (@seen) {
229     next unless $field;
230     my $row = "'" . $field->{Prefix};
231     $row .= "__" . $m->interp->apply_escapes( $field->{Column}, 'h' ) . "__"
232       unless ( $field->{Column} eq "<blank>" );
233     $row .= $field->{Suffix} . "'";
234     push( @format_string, $row );
235 }
236
237 $Format = join(",\n", @format_string);
238
239
240 return($Format, \@fields, \@seen);
241
242 </%init>
243