From 0bae4e42f3b96cce105c86d7e31afb0a666bf12f Mon Sep 17 00:00:00 2001 From: Emmanuel Lacour Date: Mon, 13 Sep 2010 10:49:39 +0200 Subject: [PATCH] Update to code changes of Results.tsv in 3.8.8 --- Changes | 3 + META.yml | 2 +- README | 2 +- html/Search/Results.xls | 134 ++++++++++++++++++++-------------- lib/RT/Extension/SearchResults/XLS.pm | 6 +- 5 files changed, 88 insertions(+), 59 deletions(-) diff --git a/Changes b/Changes index d3d4e99..a058b25 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for RT-Extension-SearchResults-XLS +0.06 Mon, 13 Sep 2010 10:46:46 +0200 + Update to code changes of Results.tsv in 3.8.8 + 0.05 Tue, 10 Feb 2009 11:26:41 +0100 Don't remove "\r" and "\n" in customfields values as this can be handled by XLS format diff --git a/META.yml b/META.yml index 8fd948e..a4b18b9 100644 --- a/META.yml +++ b/META.yml @@ -9,4 +9,4 @@ no_index: - html - inc - t -version: 0.05 +version: 0.06 diff --git a/README b/README index 57361fa..d13bba8 100644 --- a/README +++ b/README @@ -42,7 +42,7 @@ You can also look for information at: COPYRIGHT AND LICENCE -Copyright (C) 2008 Emmanuel Lacour +Copyright (C) 2008-2010 Emmanuel Lacour This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. diff --git a/html/Search/Results.xls b/html/Search/Results.xls index 9951a77..c4600c6 100644 --- a/html/Search/Results.xls +++ b/html/Search/Results.xls @@ -2,7 +2,7 @@ %# %# COPYRIGHT: %# -%# This software is Copyright (c) 1996-2008 Best Practical Solutions, LLC +%# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC %# %# %# (Except where explicitly superseded by other copyright notices) @@ -46,6 +46,7 @@ %# %# END BPS TAGGED BLOCK }}} <%ARGS> +$Query => '' $OrderBy => 'id' $Order => 'ASC' @@ -59,84 +60,109 @@ my $workbook = Spreadsheet::WriteExcel->new($fh) or die $!; my $worksheet = $workbook->add_worksheet(); my $Tickets = RT::Tickets->new( $session{'CurrentUser'} ); -$Tickets->FromSQL( $ARGS{'Query'} ); +$Tickets->FromSQL( $Query ); if ( $OrderBy =~ /\|/ ) { - - # Multiple Sorts - my @OrderBy = split /\|/, $OrderBy; - my @Order = split /\|/, $Order; - $Tickets->OrderByCols( - map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } } - ( 0 .. $#OrderBy ) ); + # Multiple Sorts + my @OrderBy = split /\|/, $OrderBy; + my @Order = split /\|/, $Order; + $Tickets->OrderByCols( + map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } } + ( 0 .. $#OrderBy ) + ); } else { - $Tickets->OrderBy( FIELD => $OrderBy, ORDER => $Order ); + $Tickets->OrderBy( FIELD => $OrderBy, ORDER => $Order ); } -my @rows; -my %known_cfs; - -my @attrs = qw( id QueueObj->Name Subject Status TimeEstimated TimeWorked TimeLeft Priority FinalPriority OwnerObj->Name - Requestors->MemberEmailAddressesAsString Cc->MemberEmailAddressesAsString AdminCc->MemberEmailAddressesAsString - DueObj->ISO ToldObj->ISO CreatedObj->ISO ResolvedObj->ISO LastUpdatedObj->ISO); - -$r->content_type('application/vnd.ms-excel'); -while ( my $Ticket = $Tickets->Next()) { - my $row; - foreach my $attr (@attrs) { - if ($attr =~ /(.*)->ISO$/ and $Ticket->$1->Unix <= 0) { - $row->{$attr} = ""; - } else { - my $method = '$Ticket->'.$attr.'()'; - $method =~ s/->ISO\(\)$/->ISO( Timezone => 'user' )/; - $row->{$attr} = eval $method; - if ($@) {die "Failed to find $attr - ". $@}; - } - } +my %cf_id_to_name; +my %cf_name_to_pos; +{ + my $cfs = RT::SQL::PossibleCustomFields( + Query => $Query, CurrentUser => $session{'CurrentUser'}, + ); + while ( my $cf = $cfs->Next ) { + my $name = $cf->Name; + $cf_id_to_name{ $cf->id } = $name; + next if $cf_name_to_pos{ $name }; - my $cfs = $Ticket->QueueObj->TicketCustomFields(); - while (my $cf = $cfs->Next) { - $known_cfs{$cf->Id} = $cf->Name; - my @content; - my $values = $Ticket->CustomFieldValues($cf->Id); - while (my $value = $values->Next) { - push @content, $value->Content; - } - $row->{'CustomField-'.$cf->Id} = join(', ',@content); + $cf_name_to_pos{ $name } = + (sort { $b <=> $a } values %cf_name_to_pos)[0] + 1; } - push @rows, $row; } -{ +my @attrs = qw( + id QueueObj->Name Subject Status + TimeEstimated TimeWorked TimeLeft + Priority FinalPriority + OwnerObj->Name + Requestors->MemberEmailAddressesAsString + Cc->MemberEmailAddressesAsString + AdminCc->MemberEmailAddressesAsString + DueObj->ISO ToldObj->ISO CreatedObj->ISO + ResolvedObj->ISO LastUpdatedObj->ISO +); + +$r->content_type('application/vnd.ms-excel'); +{ my @header; - my $ws_col = 0; foreach my $attr (@attrs) { my $label = $attr; $label =~ s'Obj-.(?:AsString|Name|ISO)''g; $label =~ s'-\>MemberEmailAddressesAsString''g; - $worksheet->write(0, $ws_col, $label); - $ws_col++; + push @header, $label; } - foreach my $id (sort keys %known_cfs) { - $worksheet->write(0, $ws_col, "CF-".$known_cfs{$id}); + + $_ += @header - 1 foreach values %cf_name_to_pos; + + foreach my $name ( sort { $cf_name_to_pos{$a} <=> $cf_name_to_pos{$b} } keys %cf_name_to_pos ) { + push @header, "CF-". $name; + } + my $ws_col = 0; + foreach my $ws_val ( @header ) { + $worksheet->write(0, $ws_col, $ws_val); $ws_col++; } } +my $i = 0; my $ws_row = 1; -foreach my $row (@rows) { - my $ws_col = 0; +while ( my $Ticket = $Tickets->Next()) { my @row; - foreach my $attr(@attrs) { - $worksheet->write($ws_row, $ws_col, $row->{"$attr"}); - $ws_col++; + foreach my $attr (@attrs) { + my $value; + if ($attr =~ /(.*)->ISO$/ and $Ticket->$1->Unix <= 0) { + $value = ''; + } else { + my $method = '$Ticket->'.$attr.'()'; + $method =~ s/->ISO\(\)$/->ISO( Timezone => 'user' )/; + $value = eval $method; + if ($@) {die "Failed to find $attr - ". $@}; + } + push @row, $value; + } + + my $values = $Ticket->CustomFieldValues; + $values->OrderByCols; # don't sort them + while (my $value = $values->Next) { + my $pos = $cf_name_to_pos{ $cf_id_to_name{ $value->CustomField } }; + next unless $pos; + + $row[$pos] = '' unless defined $row[$pos]; + $row[$pos] .= ', ' if $row[$pos]; + $row[$pos] .= $value->Content; } - foreach my $id (sort keys %known_cfs) { - my $val = $row->{'CustomField-'.$id}; - $worksheet->write($ws_row, $ws_col, $val); + + my $ws_col = 0; + foreach my $ws_val ( @row ) { + $worksheet->write($ws_row, $ws_col, $ws_val); $ws_col++; } $ws_row++; + + unless (++$i%10) { + $i = 0; + $m->flush_buffer; + } } $workbook->close; diff --git a/lib/RT/Extension/SearchResults/XLS.pm b/lib/RT/Extension/SearchResults/XLS.pm index f6f3700..b5d242b 100644 --- a/lib/RT/Extension/SearchResults/XLS.pm +++ b/lib/RT/Extension/SearchResults/XLS.pm @@ -9,11 +9,11 @@ RT::Extension::SearchResults::XLS - Add Excel format export to RT search results =head1 VERSION -Version 0.05 +Version 0.06 =cut -our $VERSION = '0.05'; +our $VERSION = '0.06'; =head1 SYNOPSIS @@ -70,7 +70,7 @@ L =head1 COPYRIGHT & LICENSE -Copyright 2008 Emmanuel Lacour, all rights reserved. +Copyright 2008-2010 Emmanuel Lacour, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -- 2.11.0