Revision e0f5e577
Von Jan Büren vor fast 5 Jahren hinzugefügt
- ID e0f5e57709ed22bdc85f9189a470e80ca346c625
- Vorgänger 857b8879
SL/Controller/DownloadZip.pm | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use parent qw(SL::Controller::Base); |
6 | 6 |
|
7 |
use List::Util qw(first max); |
|
8 |
|
|
9 | 7 |
use utf8; |
10 |
use Encode qw(decode encode); |
|
11 |
use Archive::Zip; |
|
8 |
use IO::Compress::Zip qw(zip $ZipError); |
|
12 | 9 |
use SL::File; |
13 | 10 |
use SL::SessionFile::Random; |
14 | 11 |
|
... | ... | |
35 | 32 |
#$Archive::Zip::UNICODE = 1; |
36 | 33 |
|
37 | 34 |
my $object_id = $::form->{object_id}; |
38 |
my $object_type = $::form->{object_type}; |
|
39 |
my $element_type = $::form->{element_type}; |
|
40 | 35 |
my $sfile = SL::SessionFile::Random->new(mode => "w"); |
41 |
my $zip = Archive::Zip->new(); |
|
42 |
#TODO Check client encoding !! |
|
43 |
#my $name_encoding = 'cp850'; |
|
44 |
my $name_encoding = 'UTF-8'; |
|
45 |
|
|
46 |
if ( $object_id |
|
47 |
&& ($object_type =~ m{^(?:sales_order|purchase_order|sales_quotation|request_quotation)$}) |
|
48 |
&& ($element_type eq 'part')) { |
|
49 |
my $orderitems = SL::DB::Manager::OrderItem->get_all(query => ['order.id' => $object_id ], |
|
50 |
with_objects => [ 'order', 'part' ], |
|
51 |
sort_by => 'part.partnumber ASC'); |
|
52 |
foreach my $item ( @{$orderitems} ) { |
|
53 |
my @files = SL::File->get_all(object_id => $item->parts_id, |
|
54 |
object_type => $element_type, |
|
55 |
); |
|
56 |
next unless @files; |
|
57 |
|
|
58 |
$zip->addDirectory($item->part->partnumber); |
|
59 |
$zip->addFile($_->get_file, Encode::encode($name_encoding, $item->part->partnumber . '/' . $_->db_file->file_name)) for @files; |
|
36 |
my (@files, %name_subs); |
|
37 |
|
|
38 |
die "Need a saved object!" unless $object_id; |
|
39 |
die "Works only for Quotations or Orders" |
|
40 |
unless $::form->{object_type} =~ m{^(?:sales_order|purchase_order|sales_quotation|request_quotation)$}; |
|
41 |
|
|
42 |
my $orderitems = SL::DB::Manager::OrderItem->get_all(query => ['order.id' => $object_id ], |
|
43 |
with_objects => [ 'order', 'part' ], |
|
44 |
sort_by => 'part.partnumber ASC'); |
|
45 |
foreach my $item ( @{$orderitems} ) { |
|
46 |
my @files_cur = SL::File->get_all(object_id => $item->parts_id, |
|
47 |
object_type => 'part' # this is a mandatory param! get_all can only fetch one type |
|
48 |
); |
|
49 |
next unless @files_cur; |
|
50 |
|
|
51 |
foreach (@files_cur) { |
|
52 |
push @files, $_->get_file; |
|
53 |
$name_subs{$_->get_file} = $item->part->partnumber . '/' . $_->db_file->file_name; |
|
60 | 54 |
} |
61 | 55 |
} |
62 |
unless ( $zip->writeToFileNamed($sfile->file_name) == Archive::Zip::AZ_OK ) { |
|
63 |
die 'zipfile write error'; |
|
64 |
} |
|
65 |
$sfile->fh->close; |
|
56 |
zip \@files => $sfile->file_name, FilterName => sub { s/.*/$name_subs{$_}/; } |
|
57 |
or die "zip failed: $ZipError\n"; |
|
66 | 58 |
|
67 | 59 |
return $self->send_file( |
68 | 60 |
$sfile->file_name, |
... | ... | |
87 | 79 |
|
88 | 80 |
Some customer want all attached files for the parts of an sales order or sales delivery order in one zip to download. |
89 | 81 |
This is a special method for one customer, so it is moved into an extra controller. |
90 |
The $Archive::Zip::UNICODE = 1; doesnt work ok |
|
91 |
So today the filenames in cp850/DOS format for legacy windows. |
|
92 |
To ues it for Linux Clients an additinal effort must be done, |
|
93 |
for ex. a link to the same file with an utf-8 name. |
|
82 |
|
|
94 | 83 |
|
95 | 84 |
There is also a special javascript method necessary which calles this controller method. |
96 | 85 |
THis method must be inserted into the customer branch: |
... | ... | |
124 | 113 |
|
125 | 114 |
Is this method needed in the master branch ? |
126 | 115 |
|
116 |
No. But now we've got it. |
|
117 |
The pod is not quite accurate, sales delivery order is not a valid record_type |
|
118 |
for this Controller. |
|
119 |
|
|
127 | 120 |
=head1 AUTHOR |
128 | 121 |
|
129 | 122 |
Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt> |
Auch abrufbar als: Unified diff
bugfix-422: DownloadZip.pm auf IO::Compress Zip umgestellt
Zusatz: Etwas (m.M.n.) lesbarer gemacht