1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 # <sales@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;
61 map { $self->CurrentUser->loc($_), $_ } qw( Status Queue ); # loc_qw
63 foreach my $type ( qw(Owner Creator LastUpdatedBy Requestor Cc AdminCc Watcher) ) { # loc_qw
65 qw( Name EmailAddress RealName NickName Organization Lang City Country Timezone ) # loc_qw
69 $self->CurrentUser->loc($type) . ' '
70 . $self->CurrentUser->loc($field), $type . '.' . $field;
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
78 $self->CurrentUser->loc($field)
79 . $self->CurrentUser->loc($frequency),
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;
93 my $CustomFields = RT::CustomFields->new( $self->CurrentUser );
94 foreach my $id (keys %$queues) {
95 my $queue = RT::Queue->new( $self->CurrentUser );
97 $CustomFields->LimitToQueue($queue->Id) if $queue->Id;
99 $CustomFields->LimitToGlobal;
100 while ( my $CustomField = $CustomFields->Next ) {
101 push @fields, $self->CurrentUser->loc(
102 "Custom field '[_1]'",
105 "CF.{" . $CustomField->id . "}";
114 if ( $field =~ /^(?:CF|CustomField)\.\{(.*)\}$/ ) {
116 return $self->CurrentUser->loc( "Custom field '[_1]'", $cf ) if $cf =~ /\D/;
117 my $obj = RT::CustomField->new( $self->CurrentUser );
119 return $self->CurrentUser->loc( "Custom field '[_1]'", $obj->Name );
121 return $self->CurrentUser->loc($field);
126 my %args = (Query => undef, GroupBy => undef, @_);
128 $self->FromSQL( $args{'Query'} );
129 my @group_by = ref( $args{'GroupBy'} )? @{ $args{'GroupBy'} } : ($args{'GroupBy'});
130 $self->GroupBy( map { {FIELD => $_} } @group_by );
132 # UseSQLForACLChecks may add late joins
133 my $joined = ($self->_isJoined || RT->Config->Get('UseSQLForACLChecks')) ? 1 : 0;
136 push @res, $self->Column( FUNCTION => ($joined? 'DISTINCT COUNT' : 'COUNT'), FIELD => 'id' );
137 push @res, map $self->Column( FIELD => $_ ), @group_by;
143 my @args = ref $_[0]? @_ : { @_ };
145 @{ $self->{'_group_by_field'} ||= [] } = map $_->{'FIELD'}, @args;
146 $_ = { $self->_FieldToFunction( %$_ ) } foreach @args;
148 $self->SUPER::GroupBy( @args );
155 if ( $args{'FIELD'} && !$args{'FUNCTION'} ) {
156 %args = $self->_FieldToFunction( %args );
159 return $self->SUPER::Column( %args );
164 Subclass _DoSearch from our parent so we can go through and add in empty
165 columns if it makes sense
171 $self->SUPER::_DoSearch( @_ );
172 if ( $self->{'must_redo_search'} ) {
174 "_DoSearch is not so successful as it still needs redo search, won't call AddEmptyRows"
182 =head2 _FieldToFunction FIELD
184 Returns a tuple of the field or a database function to allow grouping on that
189 sub _FieldToFunction {
193 my $field = $args{'FIELD'};
195 if ($field =~ /^(.*)(Hourly|Daily|Monthly|Annually)$/) {
196 my ($field, $grouping) = ($1, $2);
197 my $alias = $args{'ALIAS'} || 'main';
199 my $func = "$alias.$field";
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')
206 if ( lc $tz eq 'utc' ) {
209 elsif ( $db_type eq 'Pg' ) {
210 $func = "timezone('UTC', $func)";
211 $func = "timezone(". $self->_Handle->dbh->quote($tz) .", $func)";
213 elsif ( $db_type eq 'mysql' ) {
214 $func = "CONVERT_TZ($func, 'UTC', "
215 . $self->_Handle->dbh->quote($tz)
219 $RT::Logger->warning(
220 "ChartsTimezonesInDB config option"
221 ." is not supported on $db_type."
226 # Pg 8.3 requires explicit casting
227 $func .= '::text' if $db_type eq 'Pg';
229 if ( $grouping eq 'Hourly' ) {
230 $func = "SUBSTR($func,1,13)";
232 if ( $grouping eq 'Daily' ) {
233 $func = "SUBSTR($func,1,10)";
235 elsif ( $grouping eq 'Monthly' ) {
236 $func = "SUBSTR($func,1,7)";
238 elsif ( $grouping eq 'Annually' ) {
239 $func = "SUBSTR($func,1,4)";
241 $args{'FUNCTION'} = $func;
242 } elsif ( $field =~ /^(?:CF|CustomField)\.\{(.*)\}$/ ) { #XXX: use CFDecipher method
244 my $cf = RT::CustomField->new( $self->CurrentUser );
247 $RT::Logger->error("Couldn't load CustomField #$cf_name");
249 my ($ticket_cf_alias, $cf_alias) = $self->_CustomFieldJoin($cf->id, $cf->id, $cf_name);
250 @args{qw(ALIAS FIELD)} = ($ticket_cf_alias, 'Content');
252 } elsif ( $field =~ /^(?:(Owner|Creator|LastUpdatedBy))(?:\.(.*))?$/ ) {
254 my $column = $2 || 'Name';
255 my $u_alias = $self->{"_sql_report_${type}_users_${column}"}
263 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
264 } elsif ( $field =~ /^(?:Watcher|(Requestor|Cc|AdminCc))(?:\.(.*))?$/ ) {
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;
273 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
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.
284 push @{$self->{'items'}}, $record;
292 # Gotta skip over RT::Tickets->Next, since it does all sorts of crazy magic we
296 $self->RT::SearchBuilder::Next(@_);
302 return RT::Report::Tickets::Entry->new(RT->SystemUser); # $self->CurrentUser);
308 If we're grouping on a criterion we know how to add zero-value rows
315 if ( @{ $self->{'_group_by_field'} || [] } == 1 && $self->{'_group_by_field'}[0] eq 'Status' ) {
316 my %has = map { $_->__Value('Status') => 1 } @{ $self->ItemsArrayRef || [] };
318 foreach my $status ( grep !$has{$_}, RT::Queue->new($self->CurrentUser)->StatusArray ) {
320 my $record = $self->NewItem;
321 $record->LoadFromHash( {
325 $self->AddRecord($record);
330 RT::Base->_ImportOverlays();