Dateimanagement: überflüssigen Code in SL::Controller::DownloadZip entfernt
[kivitendo-erp.git] / SL / Controller / DownloadZip.pm
1 package SL::Controller::DownloadZip;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::Util qw(first max);
8
9 use utf8;
10 use Encode qw(decode encode);
11 use Archive::Zip;
12 use SL::File;
13 use SL::SessionFile::Random;
14
15 sub action_download_orderitems_files {
16   my ($self) = @_;
17
18   #
19   # special case for customer which want to have not all
20   # in kivitendo.conf some regex may be defined:
21   # For no values just let it commented out
22   # PA = Produktionsauftrag, L = Lieferschein, ML = Materialliste
23   # If you want several options, please separate the letter with '|'. Example: '^(PA|L).*'
24   #set_sales_documenttype_for_delivered_quantity = '^(LS).*'
25   #set_purchase_documenttype_for_delivered_quantity = '^(EL).*'
26   #
27   # enbale this perl code:
28   #  my $doctype = $::lx_office_conf{system}->{"set_documenttype_for_part_zip_download"};
29   #  if ( $doctype ) {
30   #    # eliminate first and last char (are quotes)
31   #    $doctype =~ s/^.//;
32   #    $doctype =~ s/.$//;
33   #  }
34
35   #$Archive::Zip::UNICODE = 1;
36
37   my $object_id    = $::form->{object_id};
38   my $object_type  = $::form->{object_type};
39   my $element_type = $::form->{element_type};
40   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   # today only sales_order implementation !
47   if ( $object_id && $object_type eq 'sales_order' && $element_type eq 'part' ) {
48     my $orderitems = SL::DB::Manager::OrderItem->get_all(query => ['order.id' => $object_id ],
49                                                          with_objects => [ 'order', 'part' ],
50                                                          sort_by => 'part.partnumber ASC');
51     foreach my $item ( @{$orderitems} ) {
52       my @files = SL::File->get_all(object_id   => $item->parts_id,
53                                     object_type => $element_type,
54                                   );
55       my @wanted_files;
56       ## also for filtering if needed:
57       # if ( $doctype ) {
58       #   @wanted_files = grep { $_->{file_name} =~ /$doctype/ } @files;
59       # } else {
60       @wanted_files = @files;
61       # }
62       if ( scalar (@wanted_files) > 0 ) {
63         $zip->addDirectory($item->part->partnumber);
64         $zip->addFile($_->get_file,
65                       Encode::encode($name_encoding,$item->part->partnumber.'/'.$_->{file_name})
66                       ) for @wanted_files;
67       }
68     }
69   }
70   unless ( $zip->writeToFileNamed($sfile->file_name) == Archive::Zip::AZ_OK ) {
71     die 'zipfile write error';
72   }
73   $sfile->fh->close;
74
75   return $self->send_file(
76     $sfile->file_name,
77     type => 'application/zip',
78     name => $::form->{zipname}.'.zip',
79   );
80 }
81
82 1;
83
84 __END__
85
86 =pod
87
88 =encoding utf-8
89
90 =head1 NAME
91
92 SL::Controller::DownloadZip - controller for download all files from parts of an order in one zip file
93
94 =head2  C<action_download_zip FORMPARAMS>
95
96 Some customer want all attached files for the parts of an sales order or sales delivery order in one zip to download.
97 This is a special method for one customer, so it is moved into an extra controller.
98 The $Archive::Zip::UNICODE = 1; doesnt work ok
99 So today the filenames in cp850/DOS format for legacy windows.
100 To ues it for Linux Clients an additinal effort must be done,
101 for ex. a link to the same file with an utf-8 name.
102
103 There is also a special javascript method necessary which calles this controller method.
104 THis method must be inserted into the customer branch:
105
106 =begin text
107
108   ns.downloadOrderitemsAtt = function(type,id) {
109     var rowcount  = $('input[name=rowcount]').val() - 1;
110     var data = {
111         action:     'FileManagement/download_zip',
112         type:       type,
113         object_id:  id,
114         rowcount:   rowcount
115     };
116     if ( rowcount == 0 ) {
117         kivi.display_flash('error', kivi.t8('No articles have been added yet.'));
118         return false;
119     }
120     for (var i = 1; i <= rowcount; i++) {
121         data['parts_id_'+i] =  $('#id_' + i).val();
122     };
123     $.download("controller.pl", data);
124     return false;
125   }
126
127 =end text
128
129 See also L<SL::Controller::FileManagement>
130
131 =head1 DISCUSSION
132
133 Is this method needed in the master branch ?
134
135 =head1 AUTHOR
136
137 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
138
139 =cut