Dateimanagement: Controller zum Laden und Generierung der Dateien
[kivitendo-erp.git] / SL / Helper / File.pm
1 package SL::Helper::File;
2
3 use strict;
4
5 use Exporter 'import';
6 our @EXPORT_OK = qw(store_pdf append_general_pdf_attachments);
7 our %EXPORT_TAGS = (all => \@EXPORT_OK,);
8 use SL::File;
9
10 sub store_pdf {
11   my ($self, $form) = @_;
12   return unless $::instance_conf->get_doc_storage;
13   my $type = $form->{type};
14   $type = $form->{formname} if $form->{formname} && !$form->{type};
15   my $id = $form->{id};
16   $id = $form->{attachment_id} if $form->{attachment_id} && !$form->{id};
17   return if !$id || !$type;
18   my $prefix = $form->get_number_prefix_for_type();
19   SL::File->save(
20     object_id   => $id,
21     object_type => $type,
22     mime_type   => 'application/pdf',
23     source      => 'created',
24     file_type   => 'document',
25     file_name   => $form->{attachment_filename},
26     file_path   => $form->{tmpfile},
27     file_number => $form->{"${prefix}number"},
28   );
29 }
30
31 # This method also needed by $form to append all general pdf attachments
32 #
33 sub append_general_pdf_attachments {
34   my ($self, %params) = @_;
35   return 0 unless $::instance_conf->get_doc_storage;
36   return 0 if !$params{filepath} || !$params{type};
37
38   my @files = SL::File->get_all(
39     object_id   => 0,
40     object_type => $params{type},
41     mime_type   => 'application/pdf'
42   );
43   return 0 if $#files < 0;
44
45   my @pdf_file_names = ($params{filepath});
46   foreach my $file (@files) {
47     my $path = $file->get_file;
48     push @pdf_file_names, $path if $path;
49   }
50
51   #TODO immer noch das alte Problem:
52   #je nachdem von woher der Aufruf kommt ist man in ./users oder .
53   my $savedir = POSIX::getcwd();
54   chdir("$self->{cwd}");
55   $self->merge_pdfs(
56     file_names => \@pdf_file_names,
57     out_path   => $params{filepath}
58   );
59   chdir("$savedir");
60
61   return 0;
62 }
63
64 1;
65
66 __END__
67
68 =encoding utf-8
69
70 =head1 NAME
71
72 SL::Helper::File - Helper for $::Form to store generated PDF-Documents
73
74
75 =head1 SYNOPSIS
76
77 # This Helper is used by SL::Form to store new generated PDF-Files and append general attachments to this documents.
78 #
79 # in SL::Form.pm:
80
81  $self->store_pdf($self);
82
83  $self->append_general_pdf_attachments($self) if ( $ext_for_format eq 'pdf' );
84
85 =head1 DESCRIPTION
86
87 The files with file_type "generated" are stored.
88
89 See also L<SL::File>.
90
91 =head1 METHODS
92
93
94 =head2 C<store_pdf>
95
96 Copy generated PDF-File to File destination.
97 This method is need from SL::Form after LaTeX-PDF Generation
98
99 =over 4
100
101 =item C<form.id>
102
103 ID of ERP-Document
104
105 =item C<form.type>
106
107 type of ERP-document
108
109 =item C<form.formname>
110
111 if no type is set this is used as type
112
113 =item C<form.attachment_id>
114
115 if no id is set this is used as id
116
117 =item C<form.tmpfile>
118
119 The path of the generated PDF-file
120
121 =item C<form.attachment_filename>
122
123 The generated filename which is used as new filename (without timestamp)
124
125 =back
126
127 =head2 C<append_general_pdf_attachments PARAMS>
128
129 This method also needed by SL::Form to append all general pdf attachments
130
131 needed C<PARAMS>:
132
133 =over 4
134
135 =item C<type>
136
137 type of ERP-document
138
139 =item C<outname>
140
141 Name of file to which the general attachments must be added
142
143 =back
144
145 =head1 AUTHOR
146
147 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
148
149
150 =cut
151