Bug 766: remove CGI::scrolling_list from request.pl
[koha.git] / cataloguing / merge.pl
blob019b4688368ab4809af8641dc308bece5de17a28
1 #!/usr/bin/perl
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
12 # version.
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.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Output;
26 use C4::Auth;
27 use C4::Items;
28 use C4::Biblio;
29 use C4::Serials;
30 use C4::Koha;
31 use C4::Reserves qw/MergeHolds/;
32 use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
34 my $input = new CGI;
35 my @biblionumber = $input->param('biblionumber');
36 my $merge = $input->param('merge');
38 my @errors;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => "cataloguing/merge.tmpl",
43 query => $input,
44 type => "intranet",
45 authnotrequired => 0,
46 flagsrequired => { editcatalogue => 'edit_catalogue' },
50 #------------------------
51 # Merging
52 #------------------------
53 if ($merge) {
55 my $dbh = C4::Context->dbh;
56 my $sth;
58 # Creating a new record from the html code
59 my $record = TransformHtmlToMarc( $input );
60 my $tobiblio = $input->param('biblio1');
61 my $frombiblio = $input->param('biblio2');
63 # Rewriting the leader
64 $record->leader(GetMarcBiblio($tobiblio)->leader());
66 my $frameworkcode = $input->param('frameworkcode');
67 my @notmoveditems;
69 # Modifying the reference record
70 ModBiblio($record, $tobiblio, $frameworkcode);
72 # Moving items from the other record to the reference record
73 # Also moving orders from the other record to the reference record, only if the order is linked to an item of the other record
74 my $itemnumbers = get_itemnumbers_of($frombiblio);
75 foreach my $itloop ($itemnumbers->{$frombiblio}) {
76 foreach my $itemnumber (@$itloop) {
77 my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
78 if (not defined $res) {
79 push @notmoveditems, $itemnumber;
83 # If some items could not be moved :
84 if (scalar(@notmoveditems) > 0) {
85 my $itemlist = join(' ',@notmoveditems);
86 push @errors, { code => "CANNOT_MOVE", value => $itemlist };
89 # Moving subscriptions from the other record to the reference record
90 my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
91 if ($subcount > 0) {
92 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
93 $sth->execute($tobiblio, $frombiblio);
95 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
96 $sth->execute($tobiblio, $frombiblio);
100 # Moving serials
101 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
102 $sth->execute($tobiblio, $frombiblio);
104 # TODO : Moving reserves
106 # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
107 my @allorders = GetOrdersByBiblionumber($frombiblio);
108 my @tobiblioitem = GetBiblioItemByBiblioNumber ($tobiblio);
109 my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
110 foreach my $myorder (@allorders) {
111 $myorder->{'biblionumber'} = $tobiblio;
112 ModOrder ($myorder);
113 # TODO : add error control (in ModOrder?)
116 # Deleting the other record
117 if (scalar(@errors) == 0) {
118 # Move holds
119 MergeHolds($dbh,$tobiblio,$frombiblio);
120 my $error = DelBiblio($frombiblio);
121 push @errors, $error if ($error);
124 # Parameters
125 $template->param(
126 result => 1,
127 biblio1 => $input->param('biblio1')
130 #-------------------------
131 # Show records to merge
132 #-------------------------
133 } else {
134 my $mergereference = $input->param('mergereference');
135 my $biblionumber = $input->param('biblionumber');
137 if (scalar(@biblionumber) != 2) {
138 push @errors, { code => "WRONG_COUNT", value => scalar(@biblionumber) };
140 else {
141 my $data1 = GetBiblioData($biblionumber[0]);
142 my $record1 = GetMarcBiblio($biblionumber[0]);
144 my $data2 = GetBiblioData($biblionumber[1]);
145 my $record2 = GetMarcBiblio($biblionumber[1]);
147 # Checks if both records use the same framework
148 my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
149 my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
152 my $subtitle1 = GetRecordValue('subtitle', $record1, $frameworkcode1);
153 my $subtitle2 = GetRecordValue('subtitle', $record2, $frameworkcode1);
155 if ($mergereference) {
157 my $framework;
158 if ($frameworkcode1 ne $frameworkcode2) {
159 $framework = $input->param('frameworkcode')
160 or push @errors, "Famework not selected.";
161 } else {
162 $framework = $frameworkcode1;
165 # Getting MARC Structure
166 my $tagslib = GetMarcStructure(1, $framework);
168 my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
170 # Creating a loop for display
171 my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
172 my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
174 # Parameters
175 $template->param(
176 biblio1 => $mergereference,
177 biblio2 => $notreference,
178 mergereference => $mergereference,
179 record1 => @record1,
180 record2 => @record2,
181 framework => $framework,
184 else {
186 # Ask the user to choose which record will be the kept
187 $template->param(
188 choosereference => 1,
189 biblio1 => $biblionumber[0],
190 biblio2 => $biblionumber[1],
191 title1 => $data1->{'title'},
192 subtitle1 => $subtitle1,
193 title2 => $data2->{'title'},
194 subtitle2 => $subtitle2
196 if ($frameworkcode1 ne $frameworkcode2) {
197 my $frameworks = getframeworks;
198 my @frameworkselect;
199 foreach my $thisframeworkcode ( keys %$frameworks ) {
200 my %row = (
201 value => $thisframeworkcode,
202 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
204 if ($frameworkcode1 eq $thisframeworkcode){
205 $row{'selected'} = 1;
207 push @frameworkselect, \%row;
209 $template->param(
210 frameworkselect => \@frameworkselect,
211 frameworkcode1 => $frameworkcode1,
212 frameworkcode2 => $frameworkcode2,
219 if (@errors) {
220 # Errors
221 $template->param( errors => \@errors );
224 output_html_with_http_headers $input, $cookie, $template->output;
225 exit;
227 =head1 FUNCTIONS
229 =cut
231 # ------------------------
232 # Functions
233 # ------------------------
234 sub _createMarcHash {
235 my $record = shift;
236 my $tagslib = shift;
237 my @array;
238 my @fields = $record->fields();
241 foreach my $field (@fields) {
242 my $fieldtag = $field->tag();
243 if ($fieldtag < 10) {
244 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
245 push @array, {
246 field => [
248 tag => $fieldtag,
249 key => createKey(),
250 value => $field->data(),
255 } else {
256 my @subfields = $field->subfields();
257 my @subfield_array;
258 foreach my $subfield (@subfields) {
259 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
260 push @subfield_array, {
261 subtag => @$subfield[0],
262 subkey => createKey(),
263 value => @$subfield[1],
269 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
270 push @array, {
271 field => [
273 tag => $fieldtag,
274 key => createKey(),
275 indicator1 => $field->indicator(1),
276 indicator2 => $field->indicator(2),
277 subfield => [@subfield_array],
285 return [@array];
289 =head2 CreateKey
291 Create a random value to set it into the input name
293 =cut
295 sub createKey {
296 return int(rand(1000000));