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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
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 @params = $input->param();
52 my $dbh = C4::Context->dbh;
55 # Creating a new record from the html code
56 my $record = TransformHtmlToMarc( \@params , $input );
57 my $tobiblio = $input->param('biblio1');
58 my $frombiblio = $input->param('biblio2');
59 my $frameworkcode = &GetFrameworkCode($tobiblio);
62 # Modifying the reference record
63 ModBiblio($record, $tobiblio, $frameworkcode);
65 # Moving items from the other record to the reference record
66 my $itemnumbers = get_itemnumbers_of($frombiblio);
68 foreach my $itloop ($itemnumbers->{$frombiblio}) {
69 foreach my $itemnumber (@$itloop) {
70 my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
71 if (not defined $res) {
72 push @notmoveditems, $itemnumber;
76 # If some items could not be moved :
77 if (scalar(@notmoveditems) > 0) {
78 my $itemlist = join(' ',@notmoveditems);
79 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
82 # Moving subscriptions from the other record to the reference record
83 my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
85 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
86 $sth->execute($tobiblio, $frombiblio);
88 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
89 $sth->execute($tobiblio, $frombiblio);
94 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
95 $sth->execute($tobiblio, $frombiblio);
97 # TODO : Moving reserves
99 # Deleting the other record
100 if (scalar(@errors) == 0) {
101 my $error = DelBiblio($frombiblio);
102 push @errors, $error if ($error);
106 my @errors_loop = map{{error => $_}}@errors;
110 errors => \@errors_loop,
112 biblio1 => $input->param('biblio1')
116 #-------------------------
117 # Show records to merge
118 #-------------------------
121 my $mergereference = $input->param('mergereference');
122 my $biblionumber = $input->param('biblionumber');
124 my $data1 = GetBiblioData($biblionumber[0]);
125 my $data2 = GetBiblioData($biblionumber[1]);
127 # Ask the user to choose which record will be the kept
128 if (not $mergereference) {
130 choosereference => 1,
131 biblio1 => $biblionumber[0],
132 biblio2 => $biblionumber[1],
133 title1 => $data1->{'title'},
134 title2 => $data2->{'title'}
138 if (scalar(@biblionumber) != 2) {
139 push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
142 # Checks if both records use the same framework
143 my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
144 my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
146 if ($frameworkcode1 ne $frameworkcode2) {
147 push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
149 $framework = $frameworkcode1;
152 # Getting MARC Structure
153 my $tagslib = GetMarcStructure(1, $framework);
155 my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
157 # Creating a loop for display
158 my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
159 my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
162 my @errors_loop = map{{error => $_}}@errors;
166 errors => \@errors_loop,
167 biblio1 => $mergereference,
168 biblio2 => $notreference,
169 mergereference => $mergereference,
172 framework => $framework
176 output_html_with_http_headers $input, $cookie, $template->output;
180 # ------------------------
182 # ------------------------
183 sub _createMarcHash {
187 my @fields = $record->fields();
190 foreach my $field (@fields) {
191 my $fieldtag = $field->tag();
192 if ($fieldtag < 10) {
193 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
199 value => $field->data(),
205 my @subfields = $field->subfields();
207 foreach my $subfield (@subfields) {
208 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
209 push @subfield_array, {
210 subtag => @$subfield[0],
211 subkey => createKey(),
212 value => @$subfield[1],
218 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
224 indicator1 => $field->indicator(1),
225 indicator2 => $field->indicator(2),
226 subfield => [@subfield_array],
240 Create a random value to set it into the input name
245 return int(rand(1000000));