|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use CGI qw ( -utf8 ); |
| 21 |
use Encode; |
| 22 |
use List::MoreUtils qw/uniq/; |
| 23 |
use MARC::Record; |
| 24 |
|
| 25 |
use Koha::Biblios; |
| 26 |
use Koha::Items; |
| 27 |
use Koha::Database; |
| 28 |
|
| 29 |
use C4::Auth; |
| 30 |
use C4::Output; |
| 31 |
use C4::Biblio; |
| 32 |
use C4::Context; |
| 33 |
use C4::Items; |
| 34 |
|
| 35 |
my $cgi = new CGI; |
| 36 |
|
| 37 |
my ($template, $loggedinuser, $cookie) = get_template_and_user({ |
| 38 |
template_name => 'cataloguing/linkelements.tt', |
| 39 |
query => $cgi, |
| 40 |
type => 'intranet', |
| 41 |
authnotrequired => 0, |
| 42 |
flagsrequired => { editcatalogue => 'edit_catalogue' }, |
| 43 |
}); |
| 44 |
|
| 45 |
my $biblionumber = $cgi->param('biblionumber'); |
| 46 |
my $sessionId = $cgi->cookie('CGISESSID'); |
| 47 |
my $session = C4::Auth::get_session($sessionId); |
| 48 |
my $schema = Koha::Database->new()->schema(); |
| 49 |
my $items_bundle_rs = $schema->resultset('ItemsBundle'); |
| 50 |
my $items_bundle_item_rs = $schema->resultset('ItemsBundleItem'); |
| 51 |
|
| 52 |
if ($cgi->request_method() eq 'POST') { |
| 53 |
my $barcodelist = $cgi->param('barcodelist'); |
| 54 |
my $withdrawn = $cgi->param('withdrawn'); |
| 55 |
my @barcodes = uniq( split(/\s*\n\s*/, $barcodelist) ); |
| 56 |
my @items; |
| 57 |
my @notfound_barcodes; |
| 58 |
my @found_barcodes; |
| 59 |
my @alreadylinked_barcodes; |
| 60 |
foreach my $barcode (@barcodes) { |
| 61 |
my $item = Koha::Items->find( { barcode => $barcode } ); |
| 62 |
if ($item) { |
| 63 |
my $items_bundle_item = $items_bundle_item_rs->find({ itemnumber => $item->itemnumber }); |
| 64 |
if ($items_bundle_item && $items_bundle_item->items_bundle->get_column('biblionumber') ne $biblionumber) { |
| 65 |
# This item is already linked to another biblio |
| 66 |
push @alreadylinked_barcodes, $barcode |
| 67 |
} else { |
| 68 |
push @items, $item; |
| 69 |
push @found_barcodes, $barcode; |
| 70 |
} |
| 71 |
} else { |
| 72 |
push @notfound_barcodes, $barcode; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (@items) { |
| 77 |
my $biblio = Koha::Biblios->find($biblionumber); |
| 78 |
my $record = GetMarcBiblio({ biblionumber => $biblionumber }); |
| 79 |
my $tag = C4::Context->preference('marcflavour') eq 'UNIMARC' ? '462' : '774'; |
| 80 |
foreach my $item (@items) { |
| 81 |
# Remove fields that references the same itemnumber |
| 82 |
# to avoid duplicates |
| 83 |
my @fields = $record->field($tag); |
| 84 |
my @duplicate_fields = grep { |
| 85 |
$_->subfield('9') eq $item->itemnumber; |
| 86 |
} @fields; |
| 87 |
$record->delete_fields(@duplicate_fields); |
| 88 |
|
| 89 |
my $field = MARC::Field->new($tag, '', '', |
| 90 |
't' => $item->biblio->title, |
| 91 |
'0' => $item->biblionumber, |
| 92 |
'9' => $item->itemnumber); |
| 93 |
$record->insert_fields_ordered($field); |
| 94 |
|
| 95 |
my $items_bundle = $items_bundle_rs->find_or_create({ biblionumber => $biblionumber }); |
| 96 |
$items_bundle_item_rs->find_or_create({ |
| 97 |
items_bundle_id => $items_bundle->items_bundle_id, |
| 98 |
itemnumber => $item->itemnumber, |
| 99 |
}); |
| 100 |
|
| 101 |
if (defined $withdrawn and $withdrawn ne '') { |
| 102 |
$item->withdrawn($withdrawn)->store(); |
| 103 |
} |
| 104 |
} |
| 105 |
ModBiblio($record, $biblionumber, $biblio->frameworkcode); |
| 106 |
} |
| 107 |
|
| 108 |
# Store barcodes in the session to display messages after the redirect |
| 109 |
$session->param('linkelements_notfound_barcodes', \@notfound_barcodes); |
| 110 |
$session->param('linkelements_found_barcodes', \@found_barcodes); |
| 111 |
$session->param('linkelements_alreadylinked_barcodes', \@alreadylinked_barcodes); |
| 112 |
$session->flush(); |
| 113 |
|
| 114 |
print $cgi->redirect('/cgi-bin/koha/cataloguing/linkelements.pl?biblionumber=' . $biblionumber); |
| 115 |
exit; |
| 116 |
} |
| 117 |
|
| 118 |
my @notfound_barcodes = map { |
| 119 |
Encode::is_utf8($_) ? $_ : Encode::decode('UTF-8', $_) |
| 120 |
} @{ $session->param('linkelements_notfound_barcodes') // [] }; |
| 121 |
|
| 122 |
my @found_barcodes = map { |
| 123 |
Encode::is_utf8($_) ? $_ : Encode::decode('UTF-8', $_) |
| 124 |
} @{ $session->param('linkelements_found_barcodes') // [] }; |
| 125 |
|
| 126 |
my @alreadylinked_barcodes = map { |
| 127 |
Encode::is_utf8($_) ? $_ : Encode::decode('UTF-8', $_) |
| 128 |
} @{ $session->param('linkelements_alreadylinked_barcodes') // [] }; |
| 129 |
|
| 130 |
$session->clear([ |
| 131 |
'linkelements_notfound_barcodes', |
| 132 |
'linkelements_found_barcodes', |
| 133 |
'linkelements_alreadylinked_barcodes', |
| 134 |
]); |
| 135 |
$session->flush(); |
| 136 |
|
| 137 |
$template->param( |
| 138 |
biblionumber => $biblionumber, |
| 139 |
notfound_barcodes => \@notfound_barcodes, |
| 140 |
found_barcodes => \@found_barcodes, |
| 141 |
alreadylinked_barcodes => \@alreadylinked_barcodes, |
| 142 |
); |
| 143 |
|
| 144 |
output_html_with_http_headers $cgi, $cookie, $template->output; |