support XLSX in other places, #17971
[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 my $override = scalar(@$rows) >= 65536 ? 'XLSX' : '';
11
12 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
13
14 my $filename = $opt{'name'} || PL($opt{'name_singular'});
15 $filename .= $format->{extension};
16
17 #http_header('Content-Type' => 'application/excel' ); #eww
18 #http_header('Content-Type' => 'application/msexcel' ); #alas
19 #http_header('Content-Type' => 'application/x-msexcel' ); #?
20
21 #http://support.microsoft.com/kb/199841
22 http_header('Content-Type' => $format->{mime_type} );
23 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
24  
25 #http://support.microsoft.com/kb/812935
26 #http://support.microsoft.com/kb/323308
27 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
28
29 my $data = '';
30 my $XLS = new IO::Scalar \$data;
31 my $workbook = $format->{class}->new($XLS)
32   or die "Error opening Excel file: $!";
33
34 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
35
36 $worksheet->protect();
37
38 my($r,$c) = (0,0);
39
40 my $header_format = $workbook->add_format(
41   bold     => 1,
42   locked   => 1,
43   bg_color => 55, #22,
44   bottom   => 3,
45 );
46 my $default_format = $workbook->add_format(locked => 0);
47
48 my %money_format;
49 my $money_char = FS::Conf->new->config('money_char') || '$';
50
51 my %date_format;
52 xl_parse_date_init();
53
54 my $writer = sub {
55   # Wrapper for $worksheet->write.
56   # Do any massaging of the value/format here.
57   my ($r, $c, $value, $format) = @_;
58   if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
59     # Currency: strip the symbol, clone the requested format,
60     # and format it for currency
61     $value = $1;
62 #    warn "formatting $value as money\n";
63     if ( !exists($money_format{$format}) ) {
64       $money_format{$format} = $workbook->add_format();
65       $money_format{$format}->copy($format);
66       $money_format{$format}->set_num_format($money_char.'#0.00#');
67     }
68     $format = $money_format{$format};
69   }
70   elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
71     # Date: convert the value to an Excel date number and set 
72     # the format
73     $value = xl_parse_date($value);
74 #    warn "formatting $value as date\n";
75     if ( !exists($date_format{$format}) ) {
76       $date_format{$format} = $workbook->add_format();
77       $date_format{$format}->copy($format);
78       $date_format{$format}->set_num_format('mmm dd yyyy');
79     }
80     $format = $date_format{$format};
81   }
82   else {
83     # String: replace line breaks with newlines
84     $value =~ s/<BR>/\n/gi;
85   }
86   $worksheet->write($r, $c, $value, $format);
87 };
88
89 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
90
91 foreach my $row ( @$rows ) {
92   $r++;
93   $c = 0;
94
95   if ( $opt{'fields'} ) {
96
97     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
98     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
99     #could also translate color, size, style into xls equivalents?
100     my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
101
102     foreach my $field ( @{$opt{'fields'}} ) {
103
104       my $xls_format = $default_format;
105
106       if ( my $format = shift @$formats ) {
107         $format = &{$format}($row) if ref($format) eq 'CODE';
108         $format ||= {};
109         $xls_format = $workbook->add_format(locked=>0, %$format);
110       }
111
112       if ( ref($field) eq 'CODE' ) {
113         foreach my $value ( &{$field}($row) ) {
114           if ( ref($value) eq 'ARRAY' ) { 
115             $writer->($r, $c++, '(N/A)' ); #unimplemented
116           } else {
117             $writer->($r, $c++, $value, $xls_format );
118           }
119         }
120       } else {
121         $writer->( $r, $c++, $row->$field(), $xls_format );
122       }
123     }
124
125   } else {
126     # no need for each row to need a new format
127     #my $xls_format = $workbook->add_format(locked=>0);
128     $writer->( $r, $c++, $_, $default_format ) foreach @$row;
129   }
130
131 }
132
133 $workbook->close();# or die "Error creating .xls file: $!";
134
135 http_header('Content-Length' => length($data) );
136
137 </%init>