4 # Copyright 2009 BibLibre
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #use warnings; FIXME - Bug 2505
30 use C4
::Reserves qw
/MergeHolds/;
33 my @biblionumber = $input->param('biblionumber');
34 my $merge = $input->param('merge');
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
40 template_name
=> "cataloguing/merge.tmpl",
44 flagsrequired
=> { editcatalogue
=> 'edit_catalogue' },
48 #------------------------
50 #------------------------
53 my $dbh = C4
::Context
->dbh;
56 # Creating a new record from the html code
57 my $record = TransformHtmlToMarc
( $input );
58 my $tobiblio = $input->param('biblio1');
59 my $frombiblio = $input->param('biblio2');
61 # Rewriting the leader
62 $record->leader(GetMarcBiblio
($tobiblio)->leader());
64 my $frameworkcode = &GetFrameworkCode
($tobiblio);
67 # Modifying the reference record
68 ModBiblio
($record, $tobiblio, $frameworkcode);
70 # Moving items from the other record to the reference record
71 my $itemnumbers = get_itemnumbers_of
($frombiblio);
72 foreach my $itloop ($itemnumbers->{$frombiblio}) {
73 foreach my $itemnumber (@
$itloop) {
74 my $res = MoveItemFromBiblio
($itemnumber, $frombiblio, $tobiblio);
75 if (not defined $res) {
76 push @notmoveditems, $itemnumber;
80 # If some items could not be moved :
81 if (scalar(@notmoveditems) > 0) {
82 my $itemlist = join(' ',@notmoveditems);
83 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
86 # Moving subscriptions from the other record to the reference record
87 my $subcount = CountSubscriptionFromBiblionumber
($frombiblio);
89 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
90 $sth->execute($tobiblio, $frombiblio);
92 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
93 $sth->execute($tobiblio, $frombiblio);
98 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
99 $sth->execute($tobiblio, $frombiblio);
101 # TODO : Moving reserves
103 # Deleting the other record
104 if (scalar(@errors) == 0) {
106 MergeHolds
($dbh,$tobiblio,$frombiblio);
107 my $error = DelBiblio
($frombiblio);
108 push @errors, $error if ($error);
112 my @errors_loop = map{{error
=> $_}}@errors;
116 errors
=> \
@errors_loop,
118 biblio1
=> $input->param('biblio1')
122 #-------------------------
123 # Show records to merge
124 #-------------------------
127 my $mergereference = $input->param('mergereference');
128 my $biblionumber = $input->param('biblionumber');
130 my $data1 = GetBiblioData
($biblionumber[0]);
131 my $data2 = GetBiblioData
($biblionumber[1]);
133 # Ask the user to choose which record will be the kept
134 if (not $mergereference) {
136 choosereference
=> 1,
137 biblio1
=> $biblionumber[0],
138 biblio2
=> $biblionumber[1],
139 title1
=> $data1->{'title'},
140 title2
=> $data2->{'title'}
144 if (scalar(@biblionumber) != 2) {
145 push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
148 # Checks if both records use the same framework
149 my $frameworkcode1 = &GetFrameworkCode
($biblionumber[0]);
150 my $frameworkcode2 = &GetFrameworkCode
($biblionumber[1]);
152 if ($frameworkcode1 ne $frameworkcode2) {
153 push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
155 $framework = $frameworkcode1;
158 # Getting MARC Structure
159 my $tagslib = GetMarcStructure
(1, $framework);
161 my $notreference = ($biblionumber[0] == $mergereference) ?
$biblionumber[1] : $biblionumber[0];
163 # Creating a loop for display
164 my @record1 = _createMarcHash
(GetMarcBiblio
($mergereference), $tagslib);
165 my @record2 = _createMarcHash
(GetMarcBiblio
($notreference), $tagslib);
168 my @errors_loop = map{{error
=> $_}}@errors;
172 errors
=> \
@errors_loop,
173 biblio1
=> $mergereference,
174 biblio2
=> $notreference,
175 mergereference
=> $mergereference,
178 framework
=> $framework
182 output_html_with_http_headers
$input, $cookie, $template->output;
189 # ------------------------
191 # ------------------------
192 sub _createMarcHash
{
196 my @fields = $record->fields();
199 foreach my $field (@fields) {
200 my $fieldtag = $field->tag();
201 if ($fieldtag < 10) {
202 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
208 value
=> $field->data(),
214 my @subfields = $field->subfields();
216 foreach my $subfield (@subfields) {
217 if ($tagslib->{$fieldtag}->{@
$subfield[0]}->{'tab'} >= 0) {
218 push @subfield_array, {
219 subtag
=> @
$subfield[0],
220 subkey
=> createKey
(),
221 value
=> @
$subfield[1],
227 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
233 indicator1
=> $field->indicator(1),
234 indicator2
=> $field->indicator(2),
235 subfield
=> [@subfield_array],
249 Create a random value to set it into the input name
254 return int(rand(1000000));