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