improve customer field access in RT queries, #16490
[freeside.git] / rt / share / html / Search / Elements / BuildFormatString
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 %#                                          <sales@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 <%ARGS>
49 $Format => RT->Config->Get('DefaultSearchResultFormat')
50
51 %cfqueues => ()
52
53 $Face => undef
54 $Size => undef
55 $Link => undef
56 $Title => undef
57
58 $AddCol => undef
59 $RemoveCol => undef
60 $ColUp => undef
61 $ColDown => undef
62
63 $SelectDisplayColumns => undef
64 $CurrentDisplayColumns => undef
65 </%ARGS>
66 <%init>
67 # This can't be in a <once> block, because otherwise we return the
68 # same \@fields every request, and keep tacking more CustomFields onto
69 # it -- and it grows per request.
70
71 # All the things we can display in the format string by default
72 # referenced by their ColumnMap keys
73 my @fields = (
74   qw(
75     id QueueName Subject
76   ),
77     
78     $m->comp('/Elements/CustomerFields', 'Names'), #freeside
79
80   qw(
81     Status ExtendedStatus UpdateStatus
82     Type
83
84     OwnerName Requestors Cc AdminCc CreatedBy LastUpdatedBy
85
86     Priority InitialPriority FinalPriority
87
88     TimeWorked TimeLeft TimeEstimated
89
90     Starts      StartsRelative
91     Started     StartedRelative
92     Created     CreatedRelative
93     LastUpdated LastUpdatedRelative
94     Told        ToldRelative
95     Due         DueRelative
96     Resolved    ResolvedRelative
97
98     RefersTo    ReferredToBy
99     DependsOn   DependedOnBy
100     MemberOf    Members
101     Parents     Children
102
103     Bookmark
104
105     NEWLINE
106   )
107 ); # loc_qw
108
109 $m->callback( CallbackOnce => 1, CallbackName => 'SetFieldsOnce', Fields => \@fields );
110
111 my $CustomFields = RT::CustomFields->new( $session{'CurrentUser'});
112 foreach my $id (keys %cfqueues) {
113     # Gotta load up the $queue object, since queues get stored by name now. my $id
114     my $queue = RT::Queue->new($session{'CurrentUser'});
115     $queue->Load($id);
116     unless ($queue->id) {
117         # XXX TODO: This ancient code dates from a former developer
118         # we have no idea what it means or why cfqueues are so encoded.
119         $id =~ s/^.'*(.*).'*$/$1/;
120         $queue->Load($id);
121     }
122     $CustomFields->LimitToQueue($queue->Id);
123 }
124 $CustomFields->LimitToGlobal;
125
126 while ( my $CustomField = $CustomFields->Next ) {
127     push @fields, "CustomField.{" . $CustomField->Name . "}";
128 }
129
130 $m->callback( Fields => \@fields, ARGSRef => \%ARGS );
131
132 my ( @seen);
133
134 $Format ||= RT->Config->Get('DefaultSearchResultFormat');
135 my @format = split( /,\s*/, $Format );
136 foreach my $field (@format) {
137     my %column = ();
138     $field =~ s/'(.*)'/$1/;
139     my ( $prefix, $suffix );
140     if ( $field =~ m/(.*)__(.*)__(.*)/ ) {
141         $prefix = $1;
142         $suffix = $3;
143         $field  = $2;
144     }
145     $field = "<blank>" if !$field;
146     $column{Prefix} = $prefix;
147     $column{Suffix} = $suffix;
148     $field =~ s/\s*(.*)\s*/$1/;
149     $column{Column} = $field;
150     push @seen, \%column;
151 }
152
153 if ( $RemoveCol ) {
154     # we do this regex match to avoid a non-numeric warning
155     my ($index) = $CurrentDisplayColumns =~ /^(\d+)/;
156     my $column = $seen[$index];
157     if ( defined($index) ) {
158         delete $seen[$index];
159         my @temp = @seen;
160         @seen = ();
161         foreach my $element (@temp) {
162             next unless $element;
163             push @seen, $element;
164         }
165     }
166 }
167 elsif ( $AddCol ) {
168     if ( defined $SelectDisplayColumns ) {
169         my $selected = $SelectDisplayColumns;
170         my @columns;
171         if (ref($selected) eq 'ARRAY') {
172             @columns = @$selected;
173         } else {
174             push @columns, $selected;
175         }
176         foreach my $col (@columns) {
177             my %column = ();
178             $column{Column} = $col;
179
180             if ( $Face eq "Bold" ) {
181                 $column{Prefix} .= "<b>";
182                 $column{Suffix} .= "</b>";
183             }
184             if ( $Face eq "Italic" ) {
185                 $column{Prefix} .= "<i>";
186                 $column{Suffix} .= "</i>";
187             }
188             if ($Size) {
189                 $column{Prefix} .= "<" . $m->interp->apply_escapes( $Size,  'h' ) . ">";
190                 $column{Suffix} .= "</" . $m->interp->apply_escapes( $Size, 'h' ) . ">";
191             }
192             if ( $Link eq "Display" ) {
193                 $column{Prefix} .= q{<a HREF="__WebPath__/Ticket/Display.html?id=__id__">};
194                 $column{Suffix} .= "</a>";
195             }
196             elsif ( $Link eq "Take" ) {
197                 $column{Prefix} .= q{<a HREF="__WebPath__/Ticket/Display.html?Action=Take&id=__id__">};
198                 $column{Suffix} .= "</a>";
199             }
200             elsif ( $Link eq "Respond" ) {
201                 $column{Prefix} .= q{<a HREF="__WebPath__/Ticket/Update.html?Action=Respond&id=__id__">};
202                 $column{Suffix} .= "</a>";
203             }
204             elsif ( $Link eq "Comment" ) {
205                 $column{Prefix} .= q{<a HREF="__WebPath__/Ticket/Update.html?Action=Comment&id=__id__">};
206                 $column{Suffix} .= "</a>";
207             }
208             elsif ( $Link eq "Resolve" ) {
209                 $column{Prefix} .= q{<a HREF="__WebPath__/Ticket/Update.html?Action=Comment&DefaultStatus=resolved&id=__id__">};
210                 $column{Suffix} .= "</a>";
211             }
212
213             if ($Title) {
214                 $column{Suffix} .= "/TITLE:" . $m->interp->apply_escapes( $Title, 'h' );
215             }
216             push @seen, \%column;
217         }
218     }
219 }
220 elsif ( $ColUp ) {
221     my $index = $CurrentDisplayColumns;
222     if ( defined $index && ( $index - 1 ) >= 0 ) {
223         my $column = $seen[$index];
224         $seen[$index]       = $seen[ $index - 1 ];
225         $seen[ $index - 1 ] = $column;
226         $CurrentDisplayColumns     = $index - 1;
227     }
228 }
229 elsif ( $ColDown ) {
230     my $index = $CurrentDisplayColumns;
231     if ( defined $index && ( $index + 1 ) < scalar @seen ) {
232         my $column = $seen[$index];
233         $seen[$index]       = $seen[ $index + 1 ];
234         $seen[ $index + 1 ] = $column;
235         $CurrentDisplayColumns     = $index + 1;
236     }
237 }
238
239
240 my @format_string;
241 foreach my $field (@seen) {
242     next unless $field;
243     my $row = "'";
244     $row .= $field->{'Prefix'} if defined $field->{'Prefix'};
245     $row .= "__" . ($field->{'Column'} =~ m/\(/ ? $field->{'Column'} # func, don't escape
246                     : $m->interp->apply_escapes( $field->{'Column'}, 'h' )) . "__"
247       unless ( $field->{'Column'} eq "<blank>" );
248     $row .= $field->{'Suffix'} if defined $field->{'Suffix'};
249     $row .= "'";
250     push( @format_string, $row );
251 }
252
253 $Format = join(",\n", @format_string);
254
255
256 return($Format, \@fields, \@seen);
257
258 </%init>