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