1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Report::Tickets;
51 use base qw/RT::Tickets/;
52 use RT::Report::Tickets::Entry;
60 my @fields = map {$_, $_} qw(
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
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;
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;
88 my $CustomFields = RT::CustomFields->new( $self->CurrentUser );
89 foreach my $id (keys %$queues) {
90 my $queue = RT::Queue->new( $self->CurrentUser );
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/;
98 $CustomFields->LimitToQueue($queue->Id);
100 $CustomFields->LimitToGlobal;
101 while ( my $CustomField = $CustomFields->Next ) {
102 push @fields, "Custom field '". $CustomField->Name ."'", "CF.{". $CustomField->id ."}";
111 if ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) {
113 return $self->CurrentUser->loc( "Custom field '[_1]'", $cf ) if $cf =~ /\D/;
114 my $obj = RT::CustomField->new( $self->CurrentUser );
116 return $self->CurrentUser->loc( "Custom field '[_1]'", $obj->Name );
118 return $self->CurrentUser->loc($field);
123 my %args = (Query => undef, GroupBy => undef, @_);
125 $self->FromSQL( $args{'Query'} );
126 my @group_by = ref( $args{'GroupBy'} )? @{ $args{'GroupBy'} } : ($args{'GroupBy'});
127 $self->GroupBy( map { {FIELD => $_} } @group_by );
129 # UseSQLForACLChecks may add late joins
130 my $joined = ($self->_isJoined || RT->Config->Get('UseSQLForACLChecks')) ? 1 : 0;
133 push @res, $self->Column( FUNCTION => ($joined? 'DISTINCT COUNT' : 'COUNT'), FIELD => 'id' );
134 push @res, map $self->Column( FIELD => $_ ), @group_by;
140 my @args = ref $_[0]? @_ : { @_ };
142 @{ $self->{'_group_by_field'} ||= [] } = map $_->{'FIELD'}, @args;
143 $_ = { $self->_FieldToFunction( %$_ ) } foreach @args;
145 $self->SUPER::GroupBy( @args );
152 if ( $args{'FIELD'} && !$args{'FUNCTION'} ) {
153 %args = $self->_FieldToFunction( %args );
156 return $self->SUPER::Column( %args );
161 Subclass _DoSearch from our parent so we can go through and add in empty
162 columns if it makes sense
168 $self->SUPER::_DoSearch( @_ );
172 =head2 _FieldToFunction FIELD
174 Returns a tuple of the field or a database function to allow grouping on that
179 sub _FieldToFunction {
183 my $field = $args{'FIELD'};
185 if ($field =~ /^(.*)(Hourly|Daily|Monthly|Annually)$/) {
186 my ($field, $grouping) = ($1, $2);
187 my $alias = $args{'ALIAS'} || 'main';
189 my $func = "$alias.$field";
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')
196 if ( lc $tz eq 'utc' ) {
199 elsif ( $db_type eq 'Pg' ) {
200 $func = "timezone('UTC', $func)";
201 $func = "timezone(". $self->_Handle->dbh->quote($tz) .", $func)";
203 elsif ( $db_type eq 'mysql' ) {
204 $func = "CONVERT_TZ($func, 'UTC', "
205 . $self->_Handle->dbh->quote($tz)
209 $RT::Logger->warning(
210 "ChartsTimezonesInDB config option"
211 ." is not supported on $db_type."
216 # Pg 8.3 requires explicit casting
217 $func .= '::text' if $db_type eq 'Pg';
219 if ( $grouping eq 'Hourly' ) {
220 $func = "SUBSTR($func,1,13)";
222 if ( $grouping eq 'Daily' ) {
223 $func = "SUBSTR($func,1,10)";
225 elsif ( $grouping eq 'Monthly' ) {
226 $func = "SUBSTR($func,1,7)";
228 elsif ( $grouping eq 'Annually' ) {
229 $func = "SUBSTR($func,1,4)";
231 $args{'FUNCTION'} = $func;
232 } elsif ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) { #XXX: use CFDecipher method
234 my $cf = RT::CustomField->new( $self->CurrentUser );
237 $RT::Logger->error("Couldn't load CustomField #$cf_name");
239 my ($ticket_cf_alias, $cf_alias) = $self->_CustomFieldJoin($cf->id, $cf->id, $cf_name);
240 @args{qw(ALIAS FIELD)} = ($ticket_cf_alias, 'Content');
242 } elsif ( $field =~ /^(?:(Owner|Creator|LastUpdatedBy))(?:\.(.*))?$/ ) {
244 my $column = $2 || 'Name';
245 my $u_alias = $self->{"_sql_report_${type}_users_${column}"}
253 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
254 } elsif ( $field =~ /^(?:Watcher|(Requestor|Cc|AdminCc))(?:\.(.*))?$/ ) {
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;
263 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
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.
274 push @{$self->{'items'}}, $record;
282 # Gotta skip over RT::Tickets->Next, since it does all sorts of crazy magic we
286 $self->RT::SearchBuilder::Next(@_);
292 return RT::Report::Tickets::Entry->new($RT::SystemUser); # $self->CurrentUser);
298 If we're grouping on a criterion we know how to add zero-value rows
305 if ( @{ $self->{'_group_by_field'} || [] } == 1 && $self->{'_group_by_field'}[0] eq 'Status' ) {
306 my %has = map { $_->__Value('Status') => 1 } @{ $self->ItemsArrayRef || [] };
308 foreach my $status ( grep !$has{$_}, RT::Queue->new($self->CurrentUser)->StatusArray ) {
310 my $record = $self->NewItem;
311 $record->LoadFromHash( {
315 $self->AddRecord($record);
320 eval "require RT::Report::Tickets_Vendor";
321 if ($@ && $@ !~ qr{^Can't locate RT/Report/Tickets_Vendor.pm}) {
325 eval "require RT::Report::Tickets_Local";
326 if ($@ && $@ !~ qr{^Can't locate RT/Report/Tickets_Local.pm}) {