This commit was generated by cvs2svn to compensate for changes in r9232,
[freeside.git] / rt / lib / RT / Report / Tickets.pm
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
49 package RT::Report::Tickets;
50
51 use base qw/RT::Tickets/;
52 use RT::Report::Tickets::Entry;
53
54 use strict;
55 use warnings;
56
57 sub Groupings {
58     my $self = shift;
59     my %args = (@_);
60     my @fields = map {$_, $_} qw(
61         Status
62         Queue
63     );
64
65     foreach my $type ( qw(Owner Creator LastUpdatedBy Requestor Cc AdminCc Watcher) ) {
66         push @fields, $type.' '.$_, $type.'.'.$_ foreach qw(
67             Name EmailAddress RealName NickName Organization Lang City Country Timezone
68         );
69     }
70
71
72     for my $field (qw(Due Resolved Created LastUpdated Started Starts)) {
73         for my $frequency (qw(Hourly Daily Monthly Annually)) {
74             my $item = $field.$frequency;
75             push @fields,  $item,  $item;
76         }
77     }
78
79     my $queues = $args{'Queues'};
80     if ( !$queues && $args{'Query'} ) {
81         require RT::Interface::Web::QueryBuilder::Tree;
82         my $tree = RT::Interface::Web::QueryBuilder::Tree->new('AND');
83         $tree->ParseSQL( Query => $args{'Query'}, CurrentUser => $self->CurrentUser );
84         $queues = $tree->GetReferencedQueues;
85     }
86
87     if ( $queues ) {
88         my $CustomFields = RT::CustomFields->new( $self->CurrentUser );
89         foreach my $id (keys %$queues) {
90             my $queue = RT::Queue->new( $self->CurrentUser );
91             $queue->Load($id);
92             unless ($queue->id) {
93                 # XXX TODO: This ancient code dates from a former developer
94                 # we have no idea what it means or why cfqueues are so encoded.
95                 $id =~ s/^.'*(.*).'*$/$1/;
96                 $queue->Load($id);
97             }
98             $CustomFields->LimitToQueue($queue->Id);
99         }
100         $CustomFields->LimitToGlobal;
101         while ( my $CustomField = $CustomFields->Next ) {
102             push @fields, "Custom field '". $CustomField->Name ."'", "CF.{". $CustomField->id ."}";
103         }
104     }
105     return @fields;
106 }
107
108 sub Label {
109     my $self = shift;
110     my $field = shift;
111     if ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) {
112         my $cf = $1;
113         return $self->CurrentUser->loc( "Custom field '[_1]'", $cf ) if $cf =~ /\D/;
114         my $obj = RT::CustomField->new( $self->CurrentUser );
115         $obj->Load( $cf );
116         return $self->CurrentUser->loc( "Custom field '[_1]'", $obj->Name );
117     }
118     return $self->CurrentUser->loc($field);
119 }
120
121 sub SetupGroupings {
122     my $self = shift;
123     my %args = (Query => undef, GroupBy => undef, @_);
124
125     $self->FromSQL( $args{'Query'} );
126     my @group_by = ref( $args{'GroupBy'} )? @{ $args{'GroupBy'} } : ($args{'GroupBy'});
127     $self->GroupBy( map { {FIELD => $_} } @group_by );
128
129     # UseSQLForACLChecks may add late joins
130     my $joined = ($self->_isJoined || RT->Config->Get('UseSQLForACLChecks')) ? 1 : 0;
131
132     my @res;
133     push @res, $self->Column( FUNCTION => ($joined? 'DISTINCT COUNT' : 'COUNT'), FIELD => 'id' );
134     push @res, map $self->Column( FIELD => $_ ), @group_by;
135     return @res;
136 }
137
138 sub GroupBy {
139     my $self = shift;
140     my @args = ref $_[0]? @_ : { @_ };
141
142     @{ $self->{'_group_by_field'} ||= [] } = map $_->{'FIELD'}, @args;
143     $_ = { $self->_FieldToFunction( %$_ ) } foreach @args;
144
145     $self->SUPER::GroupBy( @args );
146 }
147
148 sub Column {
149     my $self = shift;
150     my %args = (@_);
151
152     if ( $args{'FIELD'} && !$args{'FUNCTION'} ) {
153         %args = $self->_FieldToFunction( %args );
154     }
155
156     return $self->SUPER::Column( %args );
157 }
158
159 =head2 _DoSearch
160
161 Subclass _DoSearch from our parent so we can go through and add in empty 
162 columns if it makes sense 
163
164 =cut
165
166 sub _DoSearch {
167     my $self = shift;
168     $self->SUPER::_DoSearch( @_ );
169     $self->AddEmptyRows;
170 }
171
172 =head2 _FieldToFunction FIELD
173
174 Returns a tuple of the field or a database function to allow grouping on that 
175 field.
176
177 =cut
178
179 sub _FieldToFunction {
180     my $self = shift;
181     my %args = (@_);
182
183     my $field = $args{'FIELD'};
184
185     if ($field =~ /^(.*)(Hourly|Daily|Monthly|Annually)$/) {
186         my ($field, $grouping) = ($1, $2);
187         my $alias = $args{'ALIAS'} || 'main';
188
189         my $func = "$alias.$field";
190
191         my $db_type = RT->Config->Get('DatabaseType');
192         if ( RT->Config->Get('ChartsTimezonesInDB') ) {
193             my $tz = $self->CurrentUser->UserObj->Timezone
194                 || RT->Config->Get('Timezone')
195                 || 'UTC';
196             if ( lc $tz eq 'utc' ) {
197                 # do nothing
198             }
199             elsif ( $db_type eq 'Pg' ) {
200                 $func = "timezone('UTC', $func)";
201                 $func = "timezone(". $self->_Handle->dbh->quote($tz) .", $func)";
202             }
203             elsif ( $db_type eq 'mysql' ) {
204                 $func = "CONVERT_TZ($func, 'UTC', "
205                     . $self->_Handle->dbh->quote($tz)
206                     .")";
207             }
208             else {
209                 $RT::Logger->warning(
210                     "ChartsTimezonesInDB config option"
211                     ." is not supported on $db_type."
212                 );
213             }
214         }
215
216         # Pg 8.3 requires explicit casting
217         $func .= '::text' if $db_type eq 'Pg';
218
219         if ( $grouping eq 'Hourly' ) {
220             $func = "SUBSTR($func,1,13)";
221         }
222         if ( $grouping eq 'Daily' ) {
223             $func = "SUBSTR($func,1,10)";
224         }
225         elsif ( $grouping eq 'Monthly' ) {
226             $func = "SUBSTR($func,1,7)";
227         }
228         elsif ( $grouping eq 'Annually' ) {
229             $func = "SUBSTR($func,1,4)";
230         }
231         $args{'FUNCTION'} = $func;
232     } elsif ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) { #XXX: use CFDecipher method
233         my $cf_name = $1;
234         my $cf = RT::CustomField->new( $self->CurrentUser );
235         $cf->Load($cf_name);
236         unless ( $cf->id ) {
237             $RT::Logger->error("Couldn't load CustomField #$cf_name");
238         } else {
239             my ($ticket_cf_alias, $cf_alias) = $self->_CustomFieldJoin($cf->id, $cf->id, $cf_name);
240             @args{qw(ALIAS FIELD)} = ($ticket_cf_alias, 'Content');
241         }
242     } elsif ( $field =~ /^(?:(Owner|Creator|LastUpdatedBy))(?:\.(.*))?$/ ) {
243         my $type = $1 || '';
244         my $column = $2 || 'Name';
245         my $u_alias = $self->{"_sql_report_${type}_users_${column}"}
246             ||= $self->Join(
247                 TYPE   => 'LEFT',
248                 ALIAS1 => 'main',
249                 FIELD1 => $type,
250                 TABLE2 => 'Users',
251                 FIELD2 => 'id',
252             );
253         @args{qw(ALIAS FIELD)} = ($u_alias, $column);
254     } elsif ( $field =~ /^(?:Watcher|(Requestor|Cc|AdminCc))(?:\.(.*))?$/ ) {
255         my $type = $1 || '';
256         my $column = $2 || 'Name';
257         my $u_alias = $self->{"_sql_report_watcher_users_alias_$type"};
258         unless ( $u_alias ) {
259             my ($g_alias, $gm_alias);
260             ($g_alias, $gm_alias, $u_alias) = $self->_WatcherJoin( $type );
261             $self->{"_sql_report_watcher_users_alias_$type"} = $u_alias;
262         }
263         @args{qw(ALIAS FIELD)} = ($u_alias, $column);
264     }
265     return %args;
266 }
267
268
269 # Override the AddRecord from DBI::SearchBuilder::Unique. id isn't id here
270 # wedon't want to disambiguate all the items with a count of 1.
271 sub AddRecord {
272     my $self = shift;
273     my $record = shift;
274     push @{$self->{'items'}}, $record;
275     $self->{'rows'}++;
276 }
277
278 1;
279
280
281
282 # Gotta skip over RT::Tickets->Next, since it does all sorts of crazy magic we 
283 # don't want.
284 sub Next {
285     my $self = shift;
286     $self->RT::SearchBuilder::Next(@_);
287
288 }
289
290 sub NewItem {
291     my $self = shift;
292     return RT::Report::Tickets::Entry->new($RT::SystemUser); # $self->CurrentUser);
293 }
294
295
296 =head2 AddEmptyRows
297
298 If we're grouping on a criterion we know how to add zero-value rows
299 for, do that.
300
301 =cut
302
303 sub AddEmptyRows {
304     my $self = shift;
305     if ( @{ $self->{'_group_by_field'} || [] } == 1 && $self->{'_group_by_field'}[0] eq 'Status' ) {
306         my %has = map { $_->__Value('Status') => 1 } @{ $self->ItemsArrayRef || [] };
307
308         foreach my $status ( grep !$has{$_}, RT::Queue->new($self->CurrentUser)->StatusArray ) {
309
310             my $record = $self->NewItem;
311             $record->LoadFromHash( {
312                 id     => 0,
313                 status => $status
314             } );
315             $self->AddRecord($record);
316         }
317     }
318 }
319
320 eval "require RT::Report::Tickets_Vendor";
321 if ($@ && $@ !~ qr{^Can't locate RT/Report/Tickets_Vendor.pm}) {
322     die $@;
323 };
324
325 eval "require RT::Report::Tickets_Local";
326 if ($@ && $@ !~ qr{^Can't locate RT/Report/Tickets_Local.pm}) {
327     die $@;
328 };
329
330 1;