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
32 my @biblionumber = $input->param('biblionumber');
33 my $merge = $input->param('merge');
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
39 template_name
=> "cataloguing/merge.tmpl",
43 flagsrequired
=> { editcatalogue
=> 'edit_catalogue' },
47 #------------------------
49 #------------------------
52 my @params = $input->param();
53 my $dbh = C4
::Context
->dbh;
56 # Creating a new record from the html code
57 my $record = TransformHtmlToMarc
( \
@params , $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);
73 foreach my $itloop ($itemnumbers->{$frombiblio}) {
74 foreach my $itemnumber (@
$itloop) {
75 my $res = MoveItemFromBiblio
($itemnumber, $frombiblio, $tobiblio);
76 if (not defined $res) {
77 push @notmoveditems, $itemnumber;
81 # If some items could not be moved :
82 if (scalar(@notmoveditems) > 0) {
83 my $itemlist = join(' ',@notmoveditems);
84 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
87 # Moving subscriptions from the other record to the reference record
88 my $subcount = CountSubscriptionFromBiblionumber
($frombiblio);
90 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
91 $sth->execute($tobiblio, $frombiblio);
93 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
94 $sth->execute($tobiblio, $frombiblio);
99 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
100 $sth->execute($tobiblio, $frombiblio);
102 # TODO : Moving reserves
104 # Deleting the other record
105 if (scalar(@errors) == 0) {
106 my $error = DelBiblio
($frombiblio);
107 push @errors, $error if ($error);
111 my @errors_loop = map{{error
=> $_}}@errors;
115 errors
=> \
@errors_loop,
117 biblio1
=> $input->param('biblio1')
121 #-------------------------
122 # Show records to merge
123 #-------------------------
126 my $mergereference = $input->param('mergereference');
127 my $biblionumber = $input->param('biblionumber');
129 my $data1 = GetBiblioData
($biblionumber[0]);
130 my $data2 = GetBiblioData
($biblionumber[1]);
132 # Ask the user to choose which record will be the kept
133 if (not $mergereference) {
135 choosereference
=> 1,
136 biblio1
=> $biblionumber[0],
137 biblio2
=> $biblionumber[1],
138 title1
=> $data1->{'title'},
139 title2
=> $data2->{'title'}
143 if (scalar(@biblionumber) != 2) {
144 push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
147 # Checks if both records use the same framework
148 my $frameworkcode1 = &GetFrameworkCode
($biblionumber[0]);
149 my $frameworkcode2 = &GetFrameworkCode
($biblionumber[1]);
151 if ($frameworkcode1 ne $frameworkcode2) {
152 push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
154 $framework = $frameworkcode1;
157 # Getting MARC Structure
158 my $tagslib = GetMarcStructure
(1, $framework);
160 my $notreference = ($biblionumber[0] == $mergereference) ?
$biblionumber[1] : $biblionumber[0];
162 # Creating a loop for display
163 my @record1 = _createMarcHash
(GetMarcBiblio
($mergereference), $tagslib);
164 my @record2 = _createMarcHash
(GetMarcBiblio
($notreference), $tagslib);
167 my @errors_loop = map{{error
=> $_}}@errors;
171 errors
=> \
@errors_loop,
172 biblio1
=> $mergereference,
173 biblio2
=> $notreference,
174 mergereference
=> $mergereference,
177 framework
=> $framework
181 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));