Bug 6874: License Text Update
[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         if($id) {
104             my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
105             $OPACBaseURL =~ s#/$##;
106             my $return = "$OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=$id";
107             $template->param(
108                 success => 1,
109                 return => $return,
110                 uploaded_file => $uploaded_file,
111             );
112         } else {
113             $template->param(error => 1);
114         }
115     } elsif ($delete || ($id && ($dangling==0 || $dangling==1))) {
116         # If there's already a file uploaded for this field,
117         # We handle its deletion
118         if ($delete) {
119             if(C4::UploadedFiles::DelUploadedFile($id)==0) {;
120                 $template->param(error => 1);
121             } else {
122                 $template->param(success => 1);
123             }
124         }
125     } else {
126         my $upload_path = C4::Context->config('upload_path');
127         if ($upload_path) {
128             my $filefield = CGI::filefield(
129                 -name => 'uploaded_file',
130                 -size => 50,
131             );
132
133             my $dirs_tree = [ {
134                 name => '/',
135                 value => '/',
136                 dirs => finddirs($upload_path)
137             } ];
138
139             $template->param(
140                 dirs_tree => $dirs_tree,
141                 filefield => $filefield
142             );
143         } else {
144             $template->param( error_upload_path_not_configured => 1 );
145         }
146
147         if (!$uploaded_file && !$dir && $from_popup) {
148             $template->param(error_nothing_selected => 1);
149         }
150         elsif (!$uploaded_file && $dir) {
151             $template->param(error_no_file_selected => 1);
152         }
153         if ($uploaded_file and not $dir) {
154             $template->param(error_no_dir_selected => 1);
155         }
156
157     }
158
159     $template->param(
160         index => $index,
161         id => $id,
162     );
163
164     output_html_with_http_headers $input, $cookie, $template->output;
165 }
166
167 # Build a hierarchy of directories
168 sub finddirs {
169     my $base = shift;
170     my $upload_path = C4::Context->config('upload_path');
171     my $found = 0;
172     my @dirs;
173     my @files = glob("$base/*");
174     foreach (@files) {
175         if (-d $_ and -w $_) {
176             my $lastdirname = basename($_);
177             my $dirname =  $_;
178             $dirname =~ s/^$upload_path//g;
179             push @dirs, {
180                 value => $dirname,
181                 name => $lastdirname,
182                 dirs => finddirs($_)
183             };
184             $found = 1;
185         };
186     }
187     return \@dirs;
188 }
189
190 1;
191
192
193 __END__
194
195 =head1 upload.pl
196
197 This plugin allow to upload files on the server and reference it in a marc
198 field.
199
200 Two system preference are used:
201
202 =over 4
203
204 =item * upload_path: the real absolute path where files will be stored
205
206 =item * OPACBaseURL: for building URLs to be stored in MARC
207
208 =back