break down search.html into components, RT#5108
[freeside.git] / httemplate / search / elements / search-xls.html
1 <% $data %>
2 <%init>
3
4 my %args = @_;
5 my $type   = $args{'type'};
6 my $header = $args{'header'};
7 my $rows   = $args{'rows'};
8 my %opt    = %{ $args{'opt'} };    
9
10 #http_header('Content-Type' => 'application/excel' ); #eww
11 #http_header('Content-Type' => 'application/msexcel' ); #alas
12 #http_header('Content-Type' => 'application/x-msexcel' ); #?
13
14 #http://support.microsoft.com/kb/199841
15 http_header('Content-Type' => 'application/vnd.ms-excel' );
16
17 #http://support.microsoft.com/kb/812935
18 #http://support.microsoft.com/kb/323308
19 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
20
21 my $data = '';
22 my $XLS = new IO::Scalar \$data;
23 my $workbook = Spreadsheet::WriteExcel->new($XLS)
24   or die "Error opening .xls file: $!";
25
26 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
27
28 my($r,$c) = (0,0);
29
30 $worksheet->write($r, $c++, $_) foreach @$header;
31
32 foreach my $row ( @$rows ) {
33   $r++;
34   $c = 0;
35
36   if ( $opt{'fields'} ) {
37
38     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
39     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
40
41     foreach my $field ( @{$opt{'fields'}} ) {
42       #my $align = $aligns ? shift @$aligns : '';
43       #$align = " ALIGN=$align" if $align;
44       #my $a = '';
45       #if ( $links ) {
46       #  my $link = shift @$links;
47       #  $link = &{$link}($row) if ref($link) eq 'CODE';
48       #  if ( $link ) {
49       #    my( $url, $method ) = @{$link};
50       #    if ( ref($method) eq 'CODE' ) {
51       #      $a = $url. &{$method}($row);
52       #    } else {
53       #      $a = $url. $row->$method();
54       #    }
55       #    $a = qq(<A HREF="$a">);
56       #  }
57       #}
58       if ( ref($field) eq 'CODE' ) {
59         foreach my $value ( &{$field}($row) ) {
60           if ( ref($value) eq 'ARRAY' ) { 
61             $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
62           } else {
63             $worksheet->write($r, $c++, $value );
64           }
65         }
66       } else {
67         $worksheet->write($r, $c++, $row->$field() );
68       }
69     }
70
71   } else {
72     $worksheet->write($r, $c++, $_) foreach @$row;
73   }
74
75 }
76
77 $workbook->close();# or die "Error creating .xls file: $!";
78
79 http_header('Content-Length' => length($data) );
80  
81 </%init>