Bug 19040: Refactor GetMarcBiblio parameters
[koha.git] / cataloguing / linkitem.pl
1 #!/usr/bin/perl
2
3 # Link an item belonging to an analytical record, the item barcode needs to be provided
4 #
5 # Copyright 2009 BibLibre, 2010 Nucsoft OSS Labs
6 #
7 # This file is part of Koha.
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 strict;
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Context;
29 use C4::Koha;
30
31
32 my $query = CGI->new;
33
34 my $biblionumber = $query->param('biblionumber');
35 my $barcode      = $query->param('barcode');
36
37 my ($template, $loggedinuser, $cookie) = get_template_and_user(
38     {
39         template_name   => "cataloguing/linkitem.tt",
40         query           => $query,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { editcatalogue => 'edit_catalogue' },
44         debug           => 1,
45     }
46 );
47
48 my $biblio = GetMarcBiblio({ biblionumber => $biblionumber });
49 my $marcflavour = C4::Context->preference("marcflavour");
50 $marcflavour ||="MARC21";
51 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
52     $template->param(bibliotitle => $biblio->subfield('245','a'));
53 } elsif ($marcflavour eq 'UNIMARC') {
54     $template->param(bibliotitle => $biblio->subfield('200','a'));
55 }
56
57 $template->param(biblionumber => $biblionumber);
58
59 if ( $barcode && $biblionumber ) {
60
61     my $item = Koha::Items->find( { barcode => $barcode } );
62
63     if ($item) {
64         my $field = PrepHostMarcField( $item->biblio->biblionumber, $item->itemnumber, $marcflavour );
65         $biblio->append_fields($field);
66
67         my $modresult = ModBiblio( $biblio, $biblionumber, '' );
68         if ($modresult) {
69             $template->param( success => 1 );
70         }
71         else {
72             $template->param(
73                 error            => 1,
74                 errornomodbiblio => 1
75             );
76         }
77     }
78     else {
79         $template->param(
80             error                 => 1,
81             errornohostitemnumber => 1,
82         );
83     }
84
85     $template->param(
86         barcode        => $barcode,
87         hostitemnumber => $item->itemnumber,
88     );
89
90 }
91 else {
92     $template->param( missingparameter => 1 );
93     if ( !$barcode )      { $template->param( missingbarcode      => 1 ); }
94     if ( !$biblionumber ) { $template->param( missingbiblionumber => 1 ); }
95 }
96
97
98 output_html_with_http_headers $query, $cookie, $template->output;