Bug 6874: Clean up file URL generation
[koha.git] / cataloguing / value_builder / upload.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2011-2012 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw/-utf8/;
22 use File::Basename;
23
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::UploadedFiles;
28
29 sub plugin_parameters {
30     my ( $dbh, $record, $tagslib, $i, $tabloop ) = @_;
31     return "";
32 }
33
34 sub plugin_javascript {
35     my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
36     my $function_name = $field_number;
37     my $res           = "
38     <script type=\"text/javascript\">
39         function Focus$function_name(subfield_managed) {
40             return 1;
41         }
42
43         function Blur$function_name(subfield_managed) {
44             return 1;
45         }
46
47         function Clic$function_name(index) {
48             var id = document.getElementById(index).value;
49             var IsFileUploadUrl=0;
50             if (id.match(/opac-retrieve-file/)) {
51                 IsFileUploadUrl=1;
52             }
53             if(id.match(/id=([0-9a-f]+)/)){
54                 id = RegExp.\$1;
55             }
56             var newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=upload.pl&index=\"+index+\"&id=\"+id+\"&from_popup=0\"+\"&IsFileUploadUrl=\"+IsFileUploadUrl, 'upload', 'width=600,height=400,toolbar=false,scrollbars=no');
57             newin.focus();
58
59         }
60     </script>
61 ";
62
63     return ( $function_name, $res );
64 }
65
66 sub plugin {
67     my ($input) = @_;
68     my $index = $input->param('index');
69     my $id = $input->param('id');
70     my $delete = $input->param('delete');
71     my $uploaded_file = $input->param('uploaded_file');
72     my $from_popup = $input->param('from_popup');
73     my $isfileuploadurl = $input->param('IsFileUploadUrl');
74     my $dangling = C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl);
75     my $template_name;
76     if ($delete || ($id && ($dangling==0 || $dangling==1))) {
77         $template_name = "upload_delete_file.tt";
78     }
79     else {
80         $template_name = "upload.tt";
81     }
82
83     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84         {   template_name   => "cataloguing/value_builder/$template_name",
85             query           => $input,
86             type            => "intranet",
87             authnotrequired => 0,
88             flagsrequired   => { editcatalogue => '*' },
89             debug           => 1,
90         }
91     );
92
93     if ($dangling==2) {
94         $template->param( dangling => 1 );
95     }
96
97     # Dealing with the uploaded file
98     my $dir = $input->param('dir');
99     if ($uploaded_file and $dir) {
100         my $fh = $input->upload('uploaded_file');
101
102         $id = C4::UploadedFiles::UploadFile($uploaded_file, $dir, $fh->handle);
103         my $OPACBaseURL = C4::Context->preference('OPACBaseURL') // '';
104         $OPACBaseURL =~ s#/$##;
105         if (!$OPACBaseURL) {
106             $template->param(MissingURL => 1);
107         }
108         if($id && $OPACBaseURL) {
109             my $return = "$OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=$id";
110             $template->param(
111                 success => 1,
112                 return => $return,
113                 uploaded_file => $uploaded_file,
114             );
115         } else {
116             $template->param(error => 1);
117         }
118     } elsif ($delete || ($id && ($dangling==0 || $dangling==1))) {
119         # If there's already a file uploaded for this field,
120         # We handle its deletion
121         if ($delete) {
122             if(C4::UploadedFiles::DelUploadedFile($id)==0) {;
123                 $template->param(error => 1);
124             } else {
125                 $template->param(success => 1);
126             }
127         }
128     } else {
129         my $upload_path = C4::Context->config('upload_path');
130         if ($upload_path) {
131             my $filefield = CGI::filefield(
132                 -name => 'uploaded_file',
133                 -size => 50,
134             );
135
136             my $dirs_tree = [ {
137                 name => '/',
138                 value => '/',
139                 dirs => finddirs($upload_path)
140             } ];
141
142             $template->param(
143                 dirs_tree => $dirs_tree,
144                 filefield => $filefield
145             );
146         } else {
147             $template->param( error_upload_path_not_configured => 1 );
148         }
149
150         if (!$uploaded_file && !$dir && $from_popup) {
151             $template->param(error_nothing_selected => 1);
152         }
153         elsif (!$uploaded_file && $dir) {
154             $template->param(error_no_file_selected => 1);
155         }
156         if ($uploaded_file and not $dir) {
157             $template->param(error_no_dir_selected => 1);
158         }
159
160     }
161
162     $template->param(
163         index => $index,
164         id => $id,
165     );
166
167     output_html_with_http_headers $input, $cookie, $template->output;
168 }
169
170 # Build a hierarchy of directories
171 sub finddirs {
172     my $base = shift;
173     my $upload_path = C4::Context->config('upload_path');
174     my $found = 0;
175     my @dirs;
176     my @files = glob("$base/*");
177     foreach (@files) {
178         if (-d $_ and -w $_) {
179             my $lastdirname = basename($_);
180             my $dirname =  $_;
181             $dirname =~ s/^$upload_path//g;
182             push @dirs, {
183                 value => $dirname,
184                 name => $lastdirname,
185                 dirs => finddirs($_)
186             };
187             $found = 1;
188         };
189     }
190     return \@dirs;
191 }
192
193 1;
194
195
196 __END__
197
198 =head1 upload.pl
199
200 This plugin allow to upload files on the server and reference it in a marc
201 field.
202
203 Two system preference are used:
204
205 =over 4
206
207 =item * upload_path: the real absolute path where files will be stored
208
209 =item * OPACBaseURL: for building URLs to be stored in MARC
210
211 =back