starting to work...
[freeside.git] / rt / lib / RT / Report / Tickets.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2012 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 = 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 Told)) {
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     if ( $self->{'must_redo_search'} ) {
170         $RT::Logger->crit(
171 "_DoSearch is not so successful as it still needs redo search, won't call AddEmptyRows"
172         );
173     }
174     else {
175         $self->AddEmptyRows;
176     }
177 }
178
179 =head2 _FieldToFunction FIELD
180
181 Returns a tuple of the field or a database function to allow grouping on that 
182 field.
183
184 =cut
185
186 sub _FieldToFunction {
187     my $self = shift;
188     my %args = (@_);
189
190     my $field = $args{'FIELD'};
191
192     if ($field =~ /^(.*)(Hourly|Daily|Monthly|Annually)$/) {
193         my ($field, $grouping) = ($1, $2);
194         my $alias = $args{'ALIAS'} || 'main';
195
196         my $func = "$alias.$field";
197
198         my $db_type = RT->Config->Get('DatabaseType');
199         if ( RT->Config->Get('ChartsTimezonesInDB') ) {
200             my $tz = $self->CurrentUser->UserObj->Timezone
201                 || RT->Config->Get('Timezone')
202                 || 'UTC';
203             if ( lc $tz eq 'utc' ) {
204                 # do nothing
205             }
206             elsif ( $db_type eq 'Pg' ) {
207                 $func = "timezone('UTC', $func)";
208                 $func = "timezone(". $self->_Handle->dbh->quote($tz) .", $func)";
209             }
210             elsif ( $db_type eq 'mysql' ) {
211                 $func = "CONVERT_TZ($func, 'UTC', "
212                     . $self->_Handle->dbh->quote($tz)
213                     .")";
214             }
215             else {
216                 $RT::Logger->warning(
217                     "ChartsTimezonesInDB config option"
218                     ." is not supported on $db_type."
219                 );
220             }
221         }
222
223         # Pg 8.3 requires explicit casting
224         $func .= '::text' if $db_type eq 'Pg';
225
226         if ( $grouping eq 'Hourly' ) {
227             $func = "SUBSTR($func,1,13)";
228         }
229         if ( $grouping eq 'Daily' ) {
230             $func = "SUBSTR($func,1,10)";
231         }
232         elsif ( $grouping eq 'Monthly' ) {
233             $func = "SUBSTR($func,1,7)";
234         }
235         elsif ( $grouping eq 'Annually' ) {
236             $func = "SUBSTR($func,1,4)";
237         }
238         $args{'FUNCTION'} = $func;
239     } elsif ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) { #XXX: use CFDecipher method
240         my $cf_name = $1;
241         my $cf = RT::CustomField->new( $self->CurrentUser );
242         $cf->Load($cf_name);
243         unless ( $cf->id ) {
244             $RT::Logger->error("Couldn't load CustomField #$cf_name");
245         } else {
246             my ($ticket_cf_alias, $cf_alias) = $self->_CustomFieldJoin($cf->id, $cf->id, $cf_name);
247             @args{qw(ALIAS FIELD)} = ($ticket_cf_alias, 'Content');
248         }
249     } elsif ( $field =~ /^(?:(Owner|Creator|LastUpdatedBy))(?:\.(.*))?$/ ) {
250         my $type = $1 || '';
251         my $column = $2 || 'Name';
252         my $u_alias = $self->{"_sql_report_${type}_users_${column}"}
253             ||= $self->Join(
254                 TYPE   => 'LEFT',
255                 ALIAS1 => 'main',
256                 FIELD1 => $type,
257                 TABLE2 => 'Users',
258                 FIELD2 => 'id',
259             );
260         @args{qw(ALIAS FIELD)} = ($u_alias, $column);
261     } elsif ( $field =~ /^(?:Watcher|(Requestor|Cc|AdminCc))(?:\.(.*))?$/ ) {
262         my $type = $1 || '';
263         my $column = $2 || 'Name';
264         my $u_alias = $self->{"_sql_report_watcher_users_alias_$type"};
265         unless ( $u_alias ) {
266             my ($g_alias, $gm_alias);
267             ($g_alias, $gm_alias, $u_alias) = $self->_WatcherJoin( $type );
268             $self->{"_sql_report_watcher_users_alias_$type"} = $u_alias;
269         }
270         @args{qw(ALIAS FIELD)} = ($u_alias, $column);
271     }
272     return %args;
273 }
274
275
276 # Override the AddRecord from DBI::SearchBuilder::Unique. id isn't id here
277 # wedon't want to disambiguate all the items with a count of 1.
278 sub AddRecord {
279     my $self = shift;
280     my $record = shift;
281     push @{$self->{'items'}}, $record;
282     $self->{'rows'}++;
283 }
284
285 1;
286
287
288
289 # Gotta skip over RT::Tickets->Next, since it does all sorts of crazy magic we 
290 # don't want.
291 sub Next {
292     my $self = shift;
293     $self->RT::SearchBuilder::Next(@_);
294
295 }
296
297 sub NewItem {
298     my $self = shift;
299     return RT::Report::Tickets::Entry->new(RT->SystemUser); # $self->CurrentUser);
300 }
301
302
303 =head2 AddEmptyRows
304
305 If we're grouping on a criterion we know how to add zero-value rows
306 for, do that.
307
308 =cut
309
310 sub AddEmptyRows {
311     my $self = shift;
312     if ( @{ $self->{'_group_by_field'} || [] } == 1 && $self->{'_group_by_field'}[0] eq 'Status' ) {
313         my %has = map { $_->__Value('Status') => 1 } @{ $self->ItemsArrayRef || [] };
314
315         foreach my $status ( grep !$has{$_}, RT::Queue->new($self->CurrentUser)->StatusArray ) {
316
317             my $record = $self->NewItem;
318             $record->LoadFromHash( {
319                 id     => 0,
320                 status => $status
321             } );
322             $self->AddRecord($record);
323         }
324     }
325 }
326
327 RT::Base->_ImportOverlays();
328
329 1;