Bug 6874: Move uploadPath syspref to koha-conf.xml
[koha.git] / cataloguing / value_builder / upload.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011-2012 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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             if(id.match(/id=([0-9a-f]+)/)){
50                 id = RegExp.\$1;
51             }
52             window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=upload.pl&index=\"+index+\"&id=\"+id, 'upload', 'width=600,height=400,toolbar=false,scrollbars=no');
53
54         }
55     </script>
56 ";
57
58     return ( $function_name, $res );
59 }
60
61 sub plugin {
62     my ($input) = @_;
63     my $index = $input->param('index');
64     my $id = $input->param('id');
65     my $delete = $input->param('delete');
66     my $uploaded_file = $input->param('uploaded_file');
67
68     my $template_name = ($id || $delete)
69                     ? "upload_delete_file.tt"
70                     : "upload.tt";
71
72     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73         {   template_name   => "cataloguing/value_builder/$template_name",
74             query           => $input,
75             type            => "intranet",
76             authnotrequired => 0,
77             flagsrequired   => { editcatalogue => '*' },
78             debug           => 1,
79         }
80     );
81
82     # Dealing with the uploaded file
83     if ($uploaded_file) {
84         my $fh = $input->upload('uploaded_file');
85         my $dir = $input->param('dir');
86
87         $id = C4::UploadedFiles::UploadFile($uploaded_file, $dir, $fh->handle);
88         if($id) {
89             my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
90             $OPACBaseURL =~ s#/$##;
91             my $return = "$OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=$id";
92             $template->param(
93                 success => 1,
94                 return => $return,
95                 uploaded_file => $uploaded_file,
96             );
97         } else {
98             $template->param(error => 1);
99         }
100     } elsif ($delete || $id) {
101         # If there's already a file uploaded for this field,
102         # We handle its deletion
103         if ($delete) {
104             if(C4::UploadedFiles::DelUploadedFile($id)) {;
105                 $template->param(success => 1);
106             } else {
107                 $template->param(error => 1);
108             }
109         }
110     } else {
111         my $upload_path = C4::Context->config('uploadPath');
112         if ($upload_path) {
113             my $filefield = CGI::filefield(
114                 -name => 'uploaded_file',
115                 -size => 50,
116             );
117
118             my $dirs_tree = [ {
119                 name => '/',
120                 value => '/',
121                 dirs => finddirs($upload_path)
122             } ];
123
124             $template->param(
125                 dirs_tree => $dirs_tree,
126                 filefield => $filefield
127             );
128         } else {
129             $template->param( error_upload_path_not_configured => 1 );
130         }
131     }
132
133     $template->param(
134         index => $index,
135         id => $id
136     );
137
138     output_html_with_http_headers $input, $cookie, $template->output;
139 }
140
141 # Build a hierarchy of directories
142 sub finddirs {
143     my $base = shift;
144     my $upload_path = C4::Context->config('uploadPath');
145     my $found = 0;
146     my @dirs;
147     my @files = glob("$base/*");
148     foreach (@files) {
149         if (-d $_ and -w $_) {
150             my $lastdirname = basename($_);
151             my $dirname =  $_;
152             $dirname =~ s/^$upload_path//g;
153             push @dirs, {
154                 value => $dirname,
155                 name => $lastdirname,
156                 dirs => finddirs($_)
157             };
158             $found = 1;
159         };
160     }
161     return \@dirs;
162 }
163
164 1;
165
166
167 __END__
168
169 =head1 upload.pl
170
171 This plugin allow to upload files on the server and reference it in a marc
172 field.
173
174 Two system preference are used:
175
176 =over 4
177
178 =item * uploadPath: the real absolute path where files will be stored
179
180 =item * OPACBaseURL: for building URLs to be stored in MARC
181
182 =back