Bug 14660: Fix stocknumberAV.pl cataloguing plugin
[koha.git] / cataloguing / value_builder / upload.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 6874/See also 13437)
4
5 # This file is part of Koha.
6 #
7 # Copyright (C) 2011-2012 BibLibre
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use CGI qw/-utf8/;
24
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28 use C4::UploadedFiles;
29
30 my $builder = sub {
31     my ( $params ) = @_;
32     my $function_name = $params->{id};
33     my $res           = "
34     <script type=\"text/javascript\">
35         function Click$function_name(event) {
36             var index = event.data.id;
37             var id = document.getElementById(index).value;
38             var IsFileUploadUrl=0;
39             if (id.match(/opac-retrieve-file/)) {
40                 IsFileUploadUrl=1;
41             }
42             if(id.match(/id=([0-9a-f]+)/)){
43                 id = RegExp.\$1;
44             }
45             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');
46             newin.focus();
47         }
48     </script>
49 ";
50     return $res;
51 };
52
53 my $launcher = sub {
54     my ( $params ) = @_;
55     my $input = $params->{cgi};
56     my $index = $input->param('index');
57     my $id = $input->param('id');
58     my $delete = $input->param('delete');
59     my $uploaded_file = $input->param('uploaded_file');
60     my $from_popup = $input->param('from_popup');
61     my $isfileuploadurl = $input->param('IsFileUploadUrl');
62     my $dangling = C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl);
63     my $template_name;
64     if ($delete || ($id && ($dangling==0 || $dangling==1))) {
65         $template_name = "upload_delete_file.tt";
66     }
67     else {
68         $template_name = "upload.tt";
69     }
70
71     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72         {   template_name   => "cataloguing/value_builder/$template_name",
73             query           => $input,
74             type            => "intranet",
75             authnotrequired => 0,
76             flagsrequired   => { editcatalogue => '*' },
77             debug           => 1,
78         }
79     );
80
81     if ($dangling==2) {
82         $template->param( dangling => 1 );
83     }
84
85     # Dealing with the uploaded file
86     my $dir = $input->param('uploadcategory');
87     if ($uploaded_file and $dir) {
88         my $fh = $input->upload('uploaded_file');
89
90         $id = C4::UploadedFiles::UploadFile($uploaded_file, $dir, $fh->handle);
91         my $OPACBaseURL = C4::Context->preference('OPACBaseURL') // '';
92         $OPACBaseURL =~ s#/$##;
93         if (!$OPACBaseURL) {
94             $template->param(MissingURL => 1);
95         }
96         if($id && $OPACBaseURL) {
97             my $return = "$OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=$id";
98             $template->param(
99                 success => 1,
100                 return => $return,
101                 uploaded_file => $uploaded_file,
102             );
103         } else {
104             $template->param(error => 1);
105         }
106     } elsif ($delete || ($id && ($dangling==0 || $dangling==1))) {
107         # If there's already a file uploaded for this field,
108         # We handle its deletion
109         if ($delete) {
110             if(C4::UploadedFiles::DelUploadedFile($id)==0) {;
111                 $template->param(error => 1);
112             } else {
113                 $template->param(success => 1);
114             }
115         }
116     } else {
117         my $upload_path = C4::Context->config('upload_path');
118         if ($upload_path) {
119             my $filefield = CGI::filefield(
120                 -name => 'uploaded_file',
121                 -size => 50,
122             );
123             $template->param(
124                 filefield => $filefield,
125                 uploadcategories => C4::UploadedFiles::getCategories(),
126             );
127         } else {
128             $template->param( error_upload_path_not_configured => 1 );
129         }
130
131         if (!$uploaded_file && !$dir && $from_popup) {
132             $template->param(error_nothing_selected => 1);
133         }
134         elsif (!$uploaded_file && $dir) {
135             $template->param(error_no_file_selected => 1);
136         }
137         if ($uploaded_file and not $dir) {
138             $template->param(error_no_dir_selected => 1);
139         }
140
141     }
142
143     $template->param(
144         index => $index,
145         id => $id,
146     );
147
148     output_html_with_http_headers $input, $cookie, $template->output;
149 };
150
151 return { builder => $builder, launcher => $launcher };
152
153 1;
154
155 __END__
156
157 =head1 upload.pl
158
159 This plugin allows to upload files on the server and reference it in a marc
160 field.
161
162 It uses config variable upload_path and pref OPACBaseURL.
163
164 =cut