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
71 push @fields, map {$_, $_} qw(
72 DueDaily DueMonthly DueAnnually
73 ResolvedDaily ResolvedMonthly ResolvedAnnually
74 CreatedDaily CreatedMonthly CreatedAnnually
75 LastUpdatedDaily LastUpdatedMonthly LastUpdatedAnnually
76 StartedDaily StartedMonthly StartedAnnually
77 StartsDaily StartsMonthly StartsAnnually
80 my $queues = $args{'Queues'};
81 if ( !$queues && $args{'Query'} ) {
82 require RT::Interface::Web::QueryBuilder::Tree;
83 my $tree = RT::Interface::Web::QueryBuilder::Tree->new('AND');
84 $tree->ParseSQL( Query => $args{'Query'}, CurrentUser => $self->CurrentUser );
85 $queues = $tree->GetReferencedQueues;
89 my $CustomFields = RT::CustomFields->new( $self->CurrentUser );
90 foreach my $id (keys %$queues) {
91 my $queue = RT::Queue->new( $self->CurrentUser );
94 # XXX TODO: This ancient code dates from a former developer
95 # we have no idea what it means or why cfqueues are so encoded.
96 $id =~ s/^.'*(.*).'*$/$1/;
99 $CustomFields->LimitToQueue($queue->Id);
101 $CustomFields->LimitToGlobal;
102 while ( my $CustomField = $CustomFields->Next ) {
103 push @fields, "Custom field '". $CustomField->Name ."'", "CF.{". $CustomField->id ."}";
112 if ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) {
114 return $self->CurrentUser->loc( "Custom field '[_1]'", $cf ) if $cf =~ /\D/;
115 my $obj = RT::CustomField->new( $self->CurrentUser );
117 return $self->CurrentUser->loc( "Custom field '[_1]'", $obj->Name );
119 return $self->CurrentUser->loc($field);
124 my %args = (Query => undef, GroupBy => undef, @_);
126 $self->FromSQL( $args{'Query'} );
127 my @group_by = ref( $args{'GroupBy'} )? @{ $args{'GroupBy'} } : ($args{'GroupBy'});
128 $self->GroupBy( map { {FIELD => $_} } @group_by );
130 # UseSQLForACLChecks may add late joins
131 my $joined = ($self->_isJoined || RT->Config->Get('UseSQLForACLChecks')) ? 1 : 0;
134 push @res, $self->Column( FUNCTION => ($joined? 'DISTINCT COUNT' : 'COUNT'), FIELD => 'id' );
135 push @res, map $self->Column( FIELD => $_ ), @group_by;
141 my @args = ref $_[0]? @_ : { @_ };
143 @{ $self->{'_group_by_field'} ||= [] } = map $_->{'FIELD'}, @args;
144 $_ = { $self->_FieldToFunction( %$_ ) } foreach @args;
146 $self->SUPER::GroupBy( @args );
153 if ( $args{'FIELD'} && !$args{'FUNCTION'} ) {
154 %args = $self->_FieldToFunction( %args );
157 return $self->SUPER::Column( %args );
162 Subclass _DoSearch from our parent so we can go through and add in empty
163 columns if it makes sense
169 $self->SUPER::_DoSearch( @_ );
173 =head2 _FieldToFunction FIELD
175 Returns a tuple of the field or a database function to allow grouping on that
180 sub _FieldToFunction {
184 my $field = $args{'FIELD'};
186 if ($field =~ /^(.*)(Daily|Monthly|Annually)$/) {
187 my ($field, $grouping) = ($1, $2);
188 my $alias = $args{'ALIAS'} || 'main';
189 # Pg 8.3 requires explicit casting
190 $field .= '::text' if RT->Config->Get('DatabaseType') eq 'Pg';
191 if ( $grouping =~ /Daily/ ) {
192 $args{'FUNCTION'} = "SUBSTR($alias.$field,1,10)";
194 elsif ( $grouping =~ /Monthly/ ) {
195 $args{'FUNCTION'} = "SUBSTR($alias.$field,1,7)";
197 elsif ( $grouping =~ /Annually/ ) {
198 $args{'FUNCTION'} = "SUBSTR($alias.$field,1,4)";
200 } elsif ( $field =~ /^(?:CF|CustomField)\.{(.*)}$/ ) { #XXX: use CFDecipher method
202 my $cf = RT::CustomField->new( $self->CurrentUser );
205 $RT::Logger->error("Couldn't load CustomField #$cf_name");
207 my ($ticket_cf_alias, $cf_alias) = $self->_CustomFieldJoin($cf->id, $cf->id, $cf_name);
208 @args{qw(ALIAS FIELD)} = ($ticket_cf_alias, 'Content');
210 } elsif ( $field =~ /^(?:(Owner|Creator|LastUpdatedBy))(?:\.(.*))?$/ ) {
212 my $column = $2 || 'Name';
213 my $u_alias = $self->{"_sql_report_${type}_users_${column}"}
221 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
222 } elsif ( $field =~ /^(?:Watcher|(Requestor|Cc|AdminCc))(?:\.(.*))?$/ ) {
224 my $column = $2 || 'Name';
225 my $u_alias = $self->{"_sql_report_watcher_users_alias_$type"};
226 unless ( $u_alias ) {
227 my ($g_alias, $gm_alias);
228 ($g_alias, $gm_alias, $u_alias) = $self->_WatcherJoin( $type );
229 $self->{"_sql_report_watcher_users_alias_$type"} = $u_alias;
231 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
237 # Override the AddRecord from DBI::SearchBuilder::Unique. id isn't id here
238 # wedon't want to disambiguate all the items with a count of 1.
242 push @{$self->{'items'}}, $record;
250 # Gotta skip over RT::Tickets->Next, since it does all sorts of crazy magic we
254 $self->RT::SearchBuilder::Next(@_);
260 return RT::Report::Tickets::Entry->new($RT::SystemUser); # $self->CurrentUser);
266 If we're grouping on a criterion we know how to add zero-value rows
273 if ( @{ $self->{'_group_by_field'} || [] } == 1 && $self->{'_group_by_field'}[0] eq 'Status' ) {
274 my %has = map { $_->__Value('Status') => 1 } @{ $self->ItemsArrayRef || [] };
276 foreach my $status ( grep !$has{$_}, RT::Queue->new($self->CurrentUser)->StatusArray ) {
278 my $record = $self->NewItem;
279 $record->LoadFromHash( {
283 $self->AddRecord($record);
288 eval "require RT::Report::Tickets_Vendor";
289 if ($@ && $@ !~ qr{^Can't locate RT/Report/Tickets_Vendor.pm}) {
293 eval "require RT::Report::Tickets_Local";
294 if ($@ && $@ !~ qr{^Can't locate RT/Report/Tickets_Local.pm}) {