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