4 # Copyright 2009 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
31 my @biblionumber = $input->param('biblionumber');
32 my $merge = $input->param('merge');
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
38 template_name
=> "cataloguing/merge.tmpl",
42 flagsrequired
=> { editcatalogue
=> 'edit_catalogue' },
46 #------------------------
48 #------------------------
51 my $dbh = C4
::Context
->dbh;
54 # Creating a new record from the html code
55 my $record = TransformHtmlToMarc
( $input );
56 my $tobiblio = $input->param('biblio1');
57 my $frombiblio = $input->param('biblio2');
59 # Rewriting the leader
60 $record->leader(GetMarcBiblio
($tobiblio)->leader());
62 my $frameworkcode = &GetFrameworkCode
($tobiblio);
65 # Modifying the reference record
66 ModBiblio
($record, $tobiblio, $frameworkcode);
68 # Moving items from the other record to the reference record
69 my $itemnumbers = get_itemnumbers_of
($frombiblio);
70 foreach my $itloop ($itemnumbers->{$frombiblio}) {
71 foreach my $itemnumber (@
$itloop) {
72 my $res = MoveItemFromBiblio
($itemnumber, $frombiblio, $tobiblio);
73 if (not defined $res) {
74 push @notmoveditems, $itemnumber;
78 # If some items could not be moved :
79 if (scalar(@notmoveditems) > 0) {
80 my $itemlist = join(' ',@notmoveditems);
81 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
84 # Moving subscriptions from the other record to the reference record
85 my $subcount = CountSubscriptionFromBiblionumber
($frombiblio);
87 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
88 $sth->execute($tobiblio, $frombiblio);
90 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
91 $sth->execute($tobiblio, $frombiblio);
96 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
97 $sth->execute($tobiblio, $frombiblio);
99 # TODO : Moving reserves
101 # Deleting the other record
102 if (scalar(@errors) == 0) {
103 my $error = DelBiblio
($frombiblio);
104 push @errors, $error if ($error);
108 my @errors_loop = map{{error
=> $_}}@errors;
112 errors
=> \
@errors_loop,
114 biblio1
=> $input->param('biblio1')
118 #-------------------------
119 # Show records to merge
120 #-------------------------
123 my $mergereference = $input->param('mergereference');
124 my $biblionumber = $input->param('biblionumber');
126 my $data1 = GetBiblioData
($biblionumber[0]);
127 my $data2 = GetBiblioData
($biblionumber[1]);
129 # Ask the user to choose which record will be the kept
130 if (not $mergereference) {
132 choosereference
=> 1,
133 biblio1
=> $biblionumber[0],
134 biblio2
=> $biblionumber[1],
135 title1
=> $data1->{'title'},
136 title2
=> $data2->{'title'}
140 if (scalar(@biblionumber) != 2) {
141 push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
144 # Checks if both records use the same framework
145 my $frameworkcode1 = &GetFrameworkCode
($biblionumber[0]);
146 my $frameworkcode2 = &GetFrameworkCode
($biblionumber[1]);
148 if ($frameworkcode1 ne $frameworkcode2) {
149 push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
151 $framework = $frameworkcode1;
154 # Getting MARC Structure
155 my $tagslib = GetMarcStructure
(1, $framework);
157 my $notreference = ($biblionumber[0] == $mergereference) ?
$biblionumber[1] : $biblionumber[0];
159 # Creating a loop for display
160 my @record1 = _createMarcHash
(GetMarcBiblio
($mergereference), $tagslib);
161 my @record2 = _createMarcHash
(GetMarcBiblio
($notreference), $tagslib);
164 my @errors_loop = map{{error
=> $_}}@errors;
168 errors
=> \
@errors_loop,
169 biblio1
=> $mergereference,
170 biblio2
=> $notreference,
171 mergereference
=> $mergereference,
174 framework
=> $framework
178 output_html_with_http_headers
$input, $cookie, $template->output;
185 # ------------------------
187 # ------------------------
188 sub _createMarcHash
{
192 my @fields = $record->fields();
195 foreach my $field (@fields) {
196 my $fieldtag = $field->tag();
197 if ($fieldtag < 10) {
198 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
204 value
=> $field->data(),
210 my @subfields = $field->subfields();
212 foreach my $subfield (@subfields) {
213 if ($tagslib->{$fieldtag}->{@
$subfield[0]}->{'tab'} >= 0) {
214 push @subfield_array, {
215 subtag
=> @
$subfield[0],
216 subkey
=> createKey
(),
217 value
=> @
$subfield[1],
223 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
229 indicator1
=> $field->indicator(1),
230 indicator2
=> $field->indicator(2),
231 subfield
=> [@subfield_array],
245 Create a random value to set it into the input name
250 return int(rand(1000000));