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