Bug 6649 Allow help screens editing
[koha.git] / cataloguing / merge.pl
blobc97ca7a367a3ac9b1ec38bafd9e93a06952ed7d7
1 #!/usr/bin/perl
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
11 # version.
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.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Serials;
30 my $input = new CGI;
31 my @biblionumber = $input->param('biblionumber');
32 my $merge = $input->param('merge');
34 my @errors;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "cataloguing/merge.tmpl",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => { editcatalogue => 'edit_catalogue' },
46 #------------------------
47 # Merging
48 #------------------------
49 if ($merge) {
51 my $dbh = C4::Context->dbh;
52 my $sth;
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);
63 my @notmoveditems;
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);
86 if ($subcount > 0) {
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);
95 # Moving serials
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);
107 # Errors
108 my @errors_loop = map{{error => $_}}@errors;
110 # Parameters
111 $template->param(
112 errors => \@errors_loop,
113 result => 1,
114 biblio1 => $input->param('biblio1')
118 #-------------------------
119 # Show records to merge
120 #-------------------------
121 } else {
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) {
131 $template->param(
132 choosereference => 1,
133 biblio1 => $biblionumber[0],
134 biblio2 => $biblionumber[1],
135 title1 => $data1->{'title'},
136 title2 => $data2->{'title'}
138 } else {
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]);
147 my $framework;
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.";
150 } else {
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);
163 # Errors
164 my @errors_loop = map{{error => $_}}@errors;
166 # Parameters
167 $template->param(
168 errors => \@errors_loop,
169 biblio1 => $mergereference,
170 biblio2 => $notreference,
171 mergereference => $mergereference,
172 record1 => @record1,
173 record2 => @record2,
174 framework => $framework
178 output_html_with_http_headers $input, $cookie, $template->output;
179 exit;
181 =head1 FUNCTIONS
183 =cut
185 # ------------------------
186 # Functions
187 # ------------------------
188 sub _createMarcHash {
189 my $record = shift;
190 my $tagslib = shift;
191 my @array;
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) {
199 push @array, {
200 field => [
202 tag => $fieldtag,
203 key => createKey(),
204 value => $field->data(),
209 } else {
210 my @subfields = $field->subfields();
211 my @subfield_array;
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') {
224 push @array, {
225 field => [
227 tag => $fieldtag,
228 key => createKey(),
229 indicator1 => $field->indicator(1),
230 indicator2 => $field->indicator(2),
231 subfield => [@subfield_array],
239 return [@array];
243 =head2 CreateKey
245 Create a random value to set it into the input name
247 =cut
249 sub createKey(){
250 return int(rand(1000000));