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;
57 use Scalar::Util qw(weaken);
60 Status => 'Enum', #loc_left_pair
62 Queue => 'Queue', #loc_left_pair
64 Owner => 'User', #loc_left_pair
65 Creator => 'User', #loc_left_pair
66 LastUpdatedBy => 'User', #loc_left_pair
68 Requestor => 'Watcher', #loc_left_pair
69 Cc => 'Watcher', #loc_left_pair
70 AdminCc => 'Watcher', #loc_left_pair
71 Watcher => 'Watcher', #loc_left_pair
73 Created => 'Date', #loc_left_pair
74 Starts => 'Date', #loc_left_pair
75 Started => 'Date', #loc_left_pair
76 Resolved => 'Date', #loc_left_pair
77 Due => 'Date', #loc_left_pair
78 Told => 'Date', #loc_left_pair
79 LastUpdated => 'Date', #loc_left_pair
81 CF => 'CustomField', #loc_left_pair
85 our %GROUPINGS_META = (
91 my $queue = RT::Queue->new( $self->CurrentUser );
92 $queue->Load( $args{'VALUE'} );
98 SubFields => [grep RT::User->_Accessible($_, "public"), qw(
99 Name RealName NickName
102 Lang City Country Timezone
104 Function => 'GenerateUserFunction',
107 SubFields => [grep RT::User->_Accessible($_, "public"), qw(
108 Name RealName NickName
111 Lang City Country Timezone
113 Function => 'GenerateWatcherFunction',
120 DayOfWeek Day DayOfMonth DayOfYear
125 Function => 'GenerateDateFunction',
130 my $raw = $args{'VALUE'};
131 return $raw unless defined $raw;
133 if ( $args{'SUBKEY'} eq 'DayOfWeek' ) {
134 return $self->loc($RT::Date::DAYS_OF_WEEK[ int $raw ]);
136 elsif ( $args{'SUBKEY'} eq 'Month' ) {
137 return $self->loc($RT::Date::MONTHS[ int($raw) - 1 ]);
149 my $queues = $args->{'Queues'};
150 if ( !$queues && $args->{'Query'} ) {
151 require RT::Interface::Web::QueryBuilder::Tree;
152 my $tree = RT::Interface::Web::QueryBuilder::Tree->new('AND');
153 $tree->ParseSQL( Query => $args->{'Query'}, CurrentUser => $self->CurrentUser );
154 $queues = $args->{'Queues'} = $tree->GetReferencedQueues;
156 return () unless $queues;
160 my $CustomFields = RT::CustomFields->new( $self->CurrentUser );
161 foreach my $id (keys %$queues) {
162 my $queue = RT::Queue->new( $self->CurrentUser );
164 next unless $queue->id;
166 $CustomFields->LimitToQueue($queue->id);
168 $CustomFields->LimitToGlobal;
169 while ( my $CustomField = $CustomFields->Next ) {
170 push @res, ["Custom field", $CustomField->Name], "CF.{". $CustomField->id ."}";
174 Function => 'GenerateCustomFieldFunction',
179 my ($cf) = ( $args{'SUBKEY'} =~ /^\{(.*)\}$/ );
180 if ( $cf =~ /^\d+$/ ) {
181 my $obj = RT::CustomField->new( $self->CurrentUser );
186 return 'Custom field [_1]', $cf;
194 # loc'able strings below generated with (s/loq/loc/):
195 # perl -MRT=-init -MRT::Report::Tickets -E 'say qq{\# loq("$_->[0]")} while $_ = splice @RT::Report::Tickets::STATISTICS, 0, 2'
197 # loc("Ticket count")
198 # loc("Summary of time worked")
199 # loc("Total time worked")
200 # loc("Average time worked")
201 # loc("Minimum time worked")
202 # loc("Maximum time worked")
203 # loc("Summary of time estimated")
204 # loc("Total time estimated")
205 # loc("Average time estimated")
206 # loc("Minimum time estimated")
207 # loc("Maximum time estimated")
208 # loc("Summary of time left")
209 # loc("Total time left")
210 # loc("Average time left")
211 # loc("Minimum time left")
212 # loc("Maximum time left")
213 # loc("Summary of Created-Started")
214 # loc("Total Created-Started")
215 # loc("Average Created-Started")
216 # loc("Minimum Created-Started")
217 # loc("Maximum Created-Started")
218 # loc("Summary of Created-Resolved")
219 # loc("Total Created-Resolved")
220 # loc("Average Created-Resolved")
221 # loc("Minimum Created-Resolved")
222 # loc("Maximum Created-Resolved")
223 # loc("Summary of Created-LastUpdated")
224 # loc("Total Created-LastUpdated")
225 # loc("Average Created-LastUpdated")
226 # loc("Minimum Created-LastUpdated")
227 # loc("Maximum Created-LastUpdated")
228 # loc("Summary of Starts-Started")
229 # loc("Total Starts-Started")
230 # loc("Average Starts-Started")
231 # loc("Minimum Starts-Started")
232 # loc("Maximum Starts-Started")
233 # loc("Summary of Due-Resolved")
234 # loc("Total Due-Resolved")
235 # loc("Average Due-Resolved")
236 # loc("Minimum Due-Resolved")
237 # loc("Maximum Due-Resolved")
238 # loc("Summary of Started-Resolved")
239 # loc("Total Started-Resolved")
240 # loc("Average Started-Resolved")
241 # loc("Minimum Started-Resolved")
242 # loc("Maximum Started-Resolved")
245 COUNT => ['Ticket count', 'Count', 'id'],
248 foreach my $field (qw(TimeWorked TimeEstimated TimeLeft)) {
249 my $friendly = lc join ' ', split /(?<=[a-z])(?=[A-Z])/, $field;
251 "ALL($field)" => ["Summary of $friendly", 'TimeAll', $field ],
252 "SUM($field)" => ["Total $friendly", 'Time', 'SUM', $field ],
253 "AVG($field)" => ["Average $friendly", 'Time', 'AVG', $field ],
254 "MIN($field)" => ["Minimum $friendly", 'Time', 'MIN', $field ],
255 "MAX($field)" => ["Maximum $friendly", 'Time', 'MAX', $field ],
260 foreach my $pair (qw(
268 my ($from, $to) = split /-/, $pair;
270 "ALL($pair)" => ["Summary of $pair", 'DateTimeIntervalAll', $from, $to ],
271 "SUM($pair)" => ["Total $pair", 'DateTimeInterval', 'SUM', $from, $to ],
272 "AVG($pair)" => ["Average $pair", 'DateTimeInterval', 'AVG', $from, $to ],
273 "MIN($pair)" => ["Minimum $pair", 'DateTimeInterval', 'MIN', $from, $to ],
274 "MAX($pair)" => ["Maximum $pair", 'DateTimeInterval', 'MAX', $from, $to ],
280 our %STATISTICS_META = (
284 my $field = shift || 'id';
286 # UseSQLForACLChecks may add late joins
287 my $joined = ($self->_isJoined || RT->Config->Get('UseSQLForACLChecks')) ? 1 : 0;
289 FUNCTION => ($joined ? 'DISTINCT COUNT' : 'COUNT'),
297 my ($function, $field) = @_;
298 return (FUNCTION => $function, FIELD => $field);
304 my ($function, $field) = @_;
305 return (FUNCTION => "$function(?)*60", FIELD => $field);
307 Display => 'DurationAsString',
310 SubValues => sub { return ('Minimum', 'Average', 'Maximum', 'Total') },
315 Minimum => { FUNCTION => "MIN(?)*60", FIELD => $field },
316 Average => { FUNCTION => "AVG(?)*60", FIELD => $field },
317 Maximum => { FUNCTION => "MAX(?)*60", FIELD => $field },
318 Total => { FUNCTION => "SUM(?)*60", FIELD => $field },
321 Display => 'DurationAsString',
323 DateTimeInterval => {
326 my ($function, $from, $to) = @_;
328 my $interval = $self->_Handle->DateTimeIntervalFunction(
329 From => { FUNCTION => $self->NotSetDateToNullFunction( FIELD => $from ) },
330 To => { FUNCTION => $self->NotSetDateToNullFunction( FIELD => $to ) },
333 return (FUNCTION => "$function($interval)");
335 Display => 'DurationAsString',
337 DateTimeIntervalAll => {
338 SubValues => sub { return ('Minimum', 'Average', 'Maximum', 'Total') },
341 my ($from, $to) = @_;
343 my $interval = $self->_Handle->DateTimeIntervalFunction(
344 From => { FUNCTION => $self->NotSetDateToNullFunction( FIELD => $from ) },
345 To => { FUNCTION => $self->NotSetDateToNullFunction( FIELD => $to ) },
349 Minimum => { FUNCTION => "MIN($interval)" },
350 Average => { FUNCTION => "AVG($interval)" },
351 Maximum => { FUNCTION => "MAX($interval)" },
352 Total => { FUNCTION => "SUM($interval)" },
355 Display => 'DurationAsString',
365 my @tmp = @GROUPINGS;
366 while ( my ($field, $type) = splice @tmp, 0, 2 ) {
367 my $meta = $GROUPINGS_META{ $type } || {};
368 unless ( $meta->{'SubFields'} ) {
369 push @fields, [$field, $field], $field;
371 elsif ( ref( $meta->{'SubFields'} ) eq 'ARRAY' ) {
372 push @fields, map { ([$field, $_], "$field.$_") } @{ $meta->{'SubFields'} };
374 elsif ( my $code = $self->FindImplementationCode( $meta->{'SubFields'} ) ) {
375 push @fields, $code->( $self, \%args );
379 "$type has unsupported SubFields."
380 ." Not an array, a method name or a code reference"
387 sub IsValidGrouping {
390 return 0 unless $args{'GroupBy'};
392 my ($key, $subkey) = split /\./, $args{'GroupBy'}, 2;
394 %GROUPINGS = @GROUPINGS unless keys %GROUPINGS;
395 my $type = $GROUPINGS{$key};
396 return 0 unless $type;
397 return 1 unless $subkey;
399 my $meta = $GROUPINGS_META{ $type } || {};
400 unless ( $meta->{'SubFields'} ) {
403 elsif ( ref( $meta->{'SubFields'} ) eq 'ARRAY' ) {
404 return 1 if grep $_ eq $subkey, @{ $meta->{'SubFields'} };
406 elsif ( my $code = $self->FindImplementationCode( $meta->{'SubFields'}, 'silent' ) ) {
407 return 1 if grep $_ eq "$key.$subkey", $code->( $self, \%args );
414 return map { ref($_)? $_->[0] : $_ } @STATISTICS;
421 my $info = $self->ColumnInfo( $column );
423 $RT::Logger->error("Unknown column '$column'");
424 return $self->CurrentUser->loc('(Incorrect data)');
427 if ( $info->{'META'}{'Label'} ) {
428 my $code = $self->FindImplementationCode( $info->{'META'}{'Label'} );
429 return $self->CurrentUser->loc( $code->( $self, %$info ) )
434 if ( $info->{'TYPE'} eq 'statistic' ) {
435 $res = $info->{'INFO'}[0];
438 $res = join ' ', grep defined && length, @{ $info }{'KEY', 'SUBKEY'};
440 return $self->CurrentUser->loc( $res );
447 return $self->{'column_info'}{$column};
452 return sort { $self->{'column_info'}{$a}{'POSITION'} <=> $self->{'column_info'}{$b}{'POSITION'} }
453 keys %{ $self->{'column_info'} || {} };
465 $self->FromSQL( $args{'Query'} ) if $args{'Query'};
467 %GROUPINGS = @GROUPINGS unless keys %GROUPINGS;
471 my @group_by = grep defined && length,
472 ref( $args{'GroupBy'} )? @{ $args{'GroupBy'} } : ($args{'GroupBy'});
473 @group_by = ('Status') unless @group_by;
475 foreach my $e ( splice @group_by ) {
476 unless ($self->IsValidGrouping( Query => $args{Query}, GroupBy => $e )) {
477 RT->Logger->error("'$e' is not a valid grouping for reports; skipping");
480 my ($key, $subkey) = split /\./, $e, 2;
481 $e = { $self->_FieldToFunction( KEY => $key, SUBKEY => $subkey ) };
482 $e->{'TYPE'} = 'grouping';
483 $e->{'INFO'} = $GROUPINGS{ $key };
484 $e->{'META'} = $GROUPINGS_META{ $e->{'INFO'} };
485 $e->{'POSITION'} = $i++;
488 $self->GroupBy( map { {
489 ALIAS => $_->{'ALIAS'},
490 FIELD => $_->{'FIELD'},
491 FUNCTION => $_->{'FUNCTION'},
494 my %res = (Groups => [], Functions => []);
497 foreach my $group_by ( @group_by ) {
498 $group_by->{'NAME'} = $self->Column( %$group_by );
499 $column_info{ $group_by->{'NAME'} } = $group_by;
500 push @{ $res{'Groups'} }, $group_by->{'NAME'};
503 %STATISTICS = @STATISTICS unless keys %STATISTICS;
505 my @function = grep defined && length,
506 ref( $args{'Function'} )? @{ $args{'Function'} } : ($args{'Function'});
507 push @function, 'COUNT' unless @function;
508 foreach my $e ( @function ) {
512 INFO => $STATISTICS{ $e },
513 META => $STATISTICS_META{ $STATISTICS{ $e }[1] },
516 unless ( $e->{'INFO'} && $e->{'META'} ) {
517 $RT::Logger->error("'". $e->{'KEY'} ."' is not valid statistic for report");
518 $e->{'FUNCTION'} = 'NULL';
519 $e->{'NAME'} = $self->Column( FUNCTION => 'NULL' );
521 elsif ( $e->{'META'}{'Function'} ) {
522 my $code = $self->FindImplementationCode( $e->{'META'}{'Function'} );
524 $e->{'FUNCTION'} = 'NULL';
525 $e->{'NAME'} = $self->Column( FUNCTION => 'NULL' );
527 elsif ( $e->{'META'}{'SubValues'} ) {
528 my %tmp = $code->( $self, @{ $e->{INFO} }[2 .. $#{$e->{INFO}}] );
529 $e->{'NAME'} = 'postfunction'. $self->{'postfunctions'}++;
530 while ( my ($k, $v) = each %tmp ) {
531 $e->{'MAP'}{ $k }{'NAME'} = $self->Column( %$v );
532 @{ $e->{'MAP'}{ $k } }{'FUNCTION', 'ALIAS', 'FIELD'} =
533 @{ $v }{'FUNCTION', 'ALIAS', 'FIELD'};
537 my %tmp = $code->( $self, @{ $e->{INFO} }[2 .. $#{$e->{INFO}}] );
538 $e->{'NAME'} = $self->Column( %tmp );
539 @{ $e }{'FUNCTION', 'ALIAS', 'FIELD'} = @tmp{'FUNCTION', 'ALIAS', 'FIELD'};
542 elsif ( $e->{'META'}{'Calculate'} ) {
543 $e->{'NAME'} = 'postfunction'. $self->{'postfunctions'}++;
545 push @{ $res{'Functions'} }, $e->{'NAME'};
546 $column_info{ $e->{'NAME'} } = $e;
549 $self->{'column_info'} = \%column_info;
556 Subclass _DoSearch from our parent so we can go through and add in empty
557 columns if it makes sense
563 $self->SUPER::_DoSearch( @_ );
564 if ( $self->{'must_redo_search'} ) {
566 "_DoSearch is not so successful as it still needs redo search, won't call AddEmptyRows"
570 $self->PostProcessRecords;
574 =head2 _FieldToFunction FIELD
576 Returns a tuple of the field or a database function to allow grouping on that
581 sub _FieldToFunction {
585 $args{'FIELD'} ||= $args{'KEY'};
587 my $meta = $GROUPINGS_META{ $GROUPINGS{ $args{'KEY'} } };
588 return ('FUNCTION' => 'NULL') unless $meta;
590 return %args unless $meta->{'Function'};
592 my $code = $self->FindImplementationCode( $meta->{'Function'} );
593 return ('FUNCTION' => 'NULL') unless $code;
595 return $code->( $self, %args );
599 # Gotta skip over RT::Tickets->Next, since it does all sorts of crazy magic we
603 $self->RT::SearchBuilder::Next(@_);
609 my $res = RT::Report::Tickets::Entry->new($self->CurrentUser);
610 $res->{'report'} = $self;
611 weaken $res->{'report'};
615 # This is necessary since normally NewItem (above) is used to intuit the
616 # correct class. However, since we're abusing a subclass, it's incorrect.
617 sub _RoleGroupClass { "RT::Ticket" }
618 sub _SingularClass { "RT::Report::Tickets::Entry" }
623 $self->_DoSearch if $self->{'must_redo_search'};
624 return unless $self->{'items'} && @{ $self->{'items'} };
627 grep $_->{'TYPE'} eq 'grouping',
628 map $self->ColumnInfo($_),
630 return unless @groups;
633 my $by_multiple = sub ($$) {
634 for my $f ( @SORT_OPS ) {
635 my $r = $f->($_[0], $_[1]);
639 my @data = map [$_], @{ $self->{'items'} };
641 for ( my $i = 0; $i < @groups; $i++ ) {
642 my $group_by = $groups[$i];
646 # If this is a CF, traverse the values being used for labels.
647 # If they all look like numbers or undef, flag for a numeric sort
649 my $looks_like_number;
650 if ( $group_by->{'KEY'} eq 'CF' ){
651 $looks_like_number = 1;
653 foreach my $item (@data){
654 my $cf_label = $item->[0]->RawValue($group_by->{'NAME'});
656 $looks_like_number = 0
657 unless (not defined $cf_label)
658 or Scalar::Util::looks_like_number( $cf_label );
662 my $order = $looks_like_number ? 'numeric label' : 'label';
663 $order = $group_by->{'META'}{Sort} if exists $group_by->{'META'}{Sort};
665 if ( $order eq 'label' ) {
666 push @SORT_OPS, sub { $_[0][$idx] cmp $_[1][$idx] };
667 $method = 'LabelValue';
669 elsif ( $order eq 'numeric label' ) {
670 my $nv = $self->loc("(no value)");
671 # Sort the (no value) elements first, by comparing for them
672 # first, and falling back to a numeric sort on all other
674 push @SORT_OPS, sub {
675 (($_[0][$idx] ne $nv) <=> ($_[1][$idx] ne $nv))
676 || ( $_[0][$idx] <=> $_[1][$idx] ) };
677 $method = 'LabelValue';
679 elsif ( $order eq 'raw' ) {
680 push @SORT_OPS, sub { $_[0][$idx] cmp $_[1][$idx] };
681 $method = 'RawValue';
683 elsif ( $order eq 'numeric raw' ) {
684 push @SORT_OPS, sub { $_[0][$idx] <=> $_[1][$idx] };
685 $method = 'RawValue';
687 $RT::Logger->error("Unknown sorting function '$order'");
690 $_->[$idx] = $_->[0]->$method( $group_by->{'NAME'} ) for @data;
694 sort $by_multiple @data
698 sub PostProcessRecords {
701 my $info = $self->{'column_info'};
702 foreach my $column ( values %$info ) {
703 next unless $column->{'TYPE'} eq 'statistic';
704 if ( $column->{'META'}{'Calculate'} ) {
705 $self->CalculatePostFunction( $column );
707 elsif ( $column->{'META'}{'SubValues'} ) {
708 $self->MapSubValues( $column );
713 sub CalculatePostFunction {
717 my $code = $self->FindImplementationCode( $info->{'META'}{'Calculate'} );
719 # TODO: fill in undefs
723 my $column = $info->{'NAME'};
725 my $base_query = $self->Query;
726 foreach my $item ( @{ $self->{'items'} } ) {
727 $item->{'values'}{ lc $column } = $code->(
730 ' AND ', map "($_)", grep defined && length, $base_query, $item->Query,
733 $item->{'fetched'}{ lc $column } = 1;
741 my $to = $info->{'NAME'};
742 my $map = $info->{'MAP'};
744 foreach my $item ( @{ $self->{'items'} } ) {
745 my $dst = $item->{'values'}{ lc $to } = { };
746 while (my ($k, $v) = each %{ $map } ) {
747 $dst->{ $k } = delete $item->{'values'}{ lc $v->{'NAME'} };
748 # This mirrors the logic in RT::Record::__Value When that
749 # ceases tp use the UTF-8 flag as a character/byte
750 # distinction from the database, this can as well.
751 utf8::decode( $dst->{ $k } )
752 if defined $dst->{ $k }
753 and not utf8::is_utf8( $dst->{ $k } );
754 delete $item->{'fetched'}{ lc $v->{'NAME'} };
756 $item->{'fetched'}{ lc $to } = 1;
760 sub GenerateDateFunction {
765 if ( RT->Config->Get('ChartsTimezonesInDB') ) {
766 my $to = $self->CurrentUser->UserObj->Timezone
767 || RT->Config->Get('Timezone');
768 $tz = { From => 'UTC', To => $to }
769 if $to && lc $to ne 'utc';
772 $args{'FUNCTION'} = $RT::Handle->DateTimeFunction(
773 Type => $args{'SUBKEY'},
774 Field => $self->NotSetDateToNullFunction,
780 sub GenerateCustomFieldFunction {
784 my ($name) = ( $args{'SUBKEY'} =~ /^\{(.*)\}$/ );
785 my $cf = RT::CustomField->new( $self->CurrentUser );
788 $RT::Logger->error("Couldn't load CustomField #$name");
789 @args{qw(FUNCTION FIELD)} = ('NULL', undef);
791 my ($ticket_cf_alias, $cf_alias) = $self->_CustomFieldJoin($cf->id, $cf);
792 @args{qw(ALIAS FIELD)} = ($ticket_cf_alias, 'Content');
797 sub GenerateUserFunction {
801 my $column = $args{'SUBKEY'} || 'Name';
802 my $u_alias = $self->{"_sql_report_$args{FIELD}_users_$column"}
806 FIELD1 => $args{'FIELD'},
810 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
814 sub GenerateWatcherFunction {
818 my $type = $args{'FIELD'};
819 $type = '' if $type eq 'Watcher';
821 my $column = $args{'SUBKEY'} || 'Name';
823 my $u_alias = $self->{"_sql_report_watcher_users_alias_$type"};
824 unless ( $u_alias ) {
825 my ($g_alias, $gm_alias);
826 ($g_alias, $gm_alias, $u_alias) = $self->_WatcherJoin( Name => $type );
827 $self->{"_sql_report_watcher_users_alias_$type"} = $u_alias;
829 @args{qw(ALIAS FIELD)} = ($u_alias, $column);
834 sub DurationAsString {
837 my $v = $args{'VALUE'};
839 return $self->loc("(no value)") unless defined $v && length $v;
840 return RT::Date->new( $self->CurrentUser )->DurationAsString(
841 $v, Show => 3, Short => 1
845 my $date = RT::Date->new( $self->CurrentUser );
847 foreach my $e ( values %res ) {
848 $e = $date->DurationAsString( $e, Short => 1, Show => 3 )
849 if defined $e && length $e;
850 $e = $self->loc("(no value)") unless defined $e && length $e;
859 my $display = $self->ColumnInfo( $name )->{'META'}{'Display'};
860 return undef unless $display;
861 return $self->FindImplementationCode( $display );
865 sub FindImplementationCode {
872 $RT::Logger->error("Value is not defined. Should be method name or code reference")
876 elsif ( !ref $value ) {
877 $code = $self->can( $value );
879 $RT::Logger->error("No method $value in ". (ref $self || $self) ." class" )
884 elsif ( ref( $value ) eq 'CODE' ) {
888 $RT::Logger->error("$value is not method name or code reference")
899 # current user, handle and column_info
900 delete @clone{'user', 'DBIxHandle', 'column_info'};
901 $clone{'items'} = [ map $_->{'values'}, @{ $clone{'items'} || [] } ];
902 $clone{'column_info'} = {};
903 while ( my ($k, $v) = each %{ $self->{'column_info'} } ) {
904 $clone{'column_info'}{$k} = { %$v };
905 delete $clone{'column_info'}{$k}{'META'};
915 %$self = (%$self, %$data);
918 map { my $r = $self->NewItem; $r->LoadFromHash( $_ ); $r }
919 @{ $self->{'items'} }
921 foreach my $e ( values %{ $self->{column_info} } ) {
922 $e->{'META'} = $e->{'TYPE'} eq 'grouping'
923 ? $GROUPINGS_META{ $e->{'INFO'} }
924 : $STATISTICS_META{ $e->{'INFO'}[1] }
933 my (@head, @body, @footer);
935 @head = ({ cells => []});
936 foreach my $column ( @{ $columns{'Groups'} } ) {
937 push @{ $head[0]{'cells'} }, { type => 'head', value => $self->Label( $column ) };
941 while ( my $entry = $self->Next ) {
942 $body[ $i ] = { even => ($i+1)%2, cells => [] };
945 @footer = ({ even => ++$i%2, cells => []});
948 foreach my $column ( @{ $columns{'Groups'} } ) {
951 while ( my $entry = $self->Next ) {
952 my $value = $entry->LabelValue( $column );
953 if ( !$last || $last->{'value'} ne $value ) {
954 push @{ $body[ $i++ ]{'cells'} }, $last = { type => 'label', value => $value };
955 $last->{even} = $g++ % 2
956 unless $column eq $columns{'Groups'}[-1];
960 $last->{rowspan} = ($last->{rowspan}||1) + 1;
964 push @{ $footer[0]{'cells'} }, {
966 value => $self->loc('Total'),
967 colspan => scalar @{ $columns{'Groups'} },
970 my $pick_color = do {
971 my @colors = RT->Config->Get("ChartColors");
972 sub { $colors[ $_[0] % @colors - 1 ] }
975 my $function_count = 0;
976 foreach my $column ( @{ $columns{'Functions'} } ) {
979 my $info = $self->ColumnInfo( $column );
982 if ( $info->{'META'}{'SubValues'} ) {
983 @subs = $self->FindImplementationCode( $info->{'META'}{'SubValues'} )->(
989 unless ( $info->{'META'}{'NoTotals'} ) {
990 while ( my $entry = $self->Next ) {
991 my $raw = $entry->RawValue( $column ) || {};
992 $raw = { '' => $raw } unless ref $raw;
993 $total{ $_ } += $raw->{ $_ } foreach grep $raw->{$_}, @subs;
995 @subs = grep $total{$_}, @subs
996 unless $info->{'META'}{'NoHideEmpty'};
999 my $label = $self->Label( $column );
1002 while ( my $entry = $self->Next ) {
1003 push @{ $body[ $i++ ]{'cells'} }, {
1006 query => $entry->Query,
1009 push @{ $head[0]{'cells'} }, {
1012 rowspan => scalar @head,
1013 color => $pick_color->(++$function_count),
1015 push @{ $footer[0]{'cells'} }, { type => 'value', value => undef };
1019 if ( @subs > 1 && @head == 1 ) {
1020 $_->{rowspan} = 2 foreach @{ $head[0]{'cells'} };
1024 push @{ $head[0]{'cells'} }, {
1027 rowspan => scalar @head,
1028 color => $pick_color->(++$function_count),
1031 push @{ $head[0]{'cells'} }, { type => 'head', value => $label, colspan => scalar @subs };
1032 push @{ $head[1]{'cells'} }, { type => 'head', value => $_, color => $pick_color->(++$function_count) }
1036 while ( my $entry = $self->Next ) {
1037 my $query = $entry->Query;
1038 my $value = $entry->LabelValue( $column ) || {};
1039 $value = { '' => $value } unless ref $value;
1040 foreach my $e ( @subs ) {
1041 push @{ $body[ $i ]{'cells'} }, {
1043 value => $value->{ $e },
1050 unless ( $info->{'META'}{'NoTotals'} ) {
1051 my $total_code = $self->LabelValueCode( $column );
1052 foreach my $e ( @subs ) {
1053 my $total = $total{ $e };
1054 $total = $total_code->( $self, %$info, VALUE => $total )
1056 push @{ $footer[0]{'cells'} }, { type => 'value', value => $total };
1060 foreach my $e ( @subs ) {
1061 push @{ $footer[0]{'cells'} }, { type => 'value', value => undef };
1066 return thead => \@head, tbody => \@body, tfoot => \@footer;
1069 RT::Base->_ImportOverlays();