Lines 29-38
use C4::Serials;
Link Here
|
29 |
use C4::Koha; |
29 |
use C4::Koha; |
30 |
use C4::Reserves qw/MergeHolds/; |
30 |
use C4::Reserves qw/MergeHolds/; |
31 |
use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/; |
31 |
use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/; |
|
|
32 |
use C4::ImportBatch; |
32 |
use Koha::MetadataRecord; |
33 |
use Koha::MetadataRecord; |
33 |
|
34 |
|
34 |
my $input = new CGI; |
35 |
my $input = new CGI; |
35 |
my @biblionumbers = $input->multi_param('biblionumber'); |
36 |
my @record_paths = $input->multi_param('record'); |
36 |
my $merge = $input->param('merge'); |
37 |
my $merge = $input->param('merge'); |
37 |
|
38 |
|
38 |
my @errors; |
39 |
my @errors; |
Lines 47-63
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
Link Here
|
47 |
} |
48 |
} |
48 |
); |
49 |
); |
49 |
|
50 |
|
|
|
51 |
my $sources = { |
52 |
catalog => { |
53 |
delete => \&_delete_catalog, |
54 |
fetch => \&_fetch_catalog, |
55 |
get_link => \&_get_link_catalog, |
56 |
get_preview_link => \&_get_preview_link_catalog, |
57 |
merge_other => \&_merge_other_catalog, |
58 |
update => \&_update_catalog, |
59 |
}, |
60 |
batch => { |
61 |
fetch => \&_fetch_import_batch, |
62 |
get_link => \&_get_preview_link_import_batch, |
63 |
get_preview_link => \&_get_preview_link_import_batch, |
64 |
update => \&_update_import_batch, |
65 |
}, |
66 |
}; |
67 |
|
68 |
sub split_record_path { |
69 |
my ($source, $num) = split /\//, shift, 2; |
70 |
$source =~ s/-.*//; |
71 |
|
72 |
return ( $source, $num ); |
73 |
} |
74 |
|
50 |
#------------------------ |
75 |
#------------------------ |
51 |
# Merging |
76 |
# Merging |
52 |
#------------------------ |
77 |
#------------------------ |
53 |
if ($merge) { |
78 |
if ($merge) { |
54 |
|
79 |
|
55 |
my $dbh = C4::Context->dbh; |
|
|
56 |
|
57 |
# Creating a new record from the html code |
80 |
# Creating a new record from the html code |
58 |
my $record = TransformHtmlToMarc( $input, 1 ); |
81 |
my $record = TransformHtmlToMarc( $input ); |
59 |
my $ref_biblionumber = $input->param('ref_biblionumber'); |
82 |
my $ref_record_path = $input->param('ref_record'); |
60 |
@biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers; |
83 |
|
|
|
84 |
my ( $ref_record_source, $ref_record_num ) = split_record_path $ref_record_path; |
85 |
my @other_record_paths = map +[ split_record_path $_ ], grep { $_ ne $ref_record_path } @record_paths; |
61 |
|
86 |
|
62 |
# prepare report |
87 |
# prepare report |
63 |
my @report_records; |
88 |
my @report_records; |
Lines 75-120
if ($merge) {
Link Here
|
75 |
} |
100 |
} |
76 |
|
101 |
|
77 |
# Rewriting the leader |
102 |
# Rewriting the leader |
78 |
$record->leader(GetMarcBiblio($ref_biblionumber)->leader()); |
103 |
my $orig_ref_record = $sources->{$ref_record_source}->{fetch}($ref_record_num); |
|
|
104 |
$record->leader( $orig_ref_record->leader() ); |
79 |
|
105 |
|
80 |
my $frameworkcode = $input->param('frameworkcode'); |
106 |
my $frameworkcode = $input->param('frameworkcode'); |
81 |
my @notmoveditems; |
107 |
my @notmoveditems; |
82 |
|
108 |
|
83 |
# Modifying the reference record |
109 |
# Modifying the reference record |
84 |
ModBiblio($record, $ref_biblionumber, $frameworkcode); |
110 |
$sources->{$ref_record_source}->{update}( $record, $ref_record_num, $frameworkcode ); |
85 |
|
111 |
|
86 |
# Moving items from the other record to the reference record |
112 |
# Are all the records from the same source? |
87 |
foreach my $biblionumber (@biblionumbers) { |
113 |
my $all_records_same_source = 1; |
88 |
my $itemnumbers = get_itemnumbers_of($biblionumber); |
114 |
|
89 |
foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) { |
115 |
# Merging in information from other records |
90 |
my $res = MoveItemFromBiblio($itemnumber, $biblionumber, $ref_biblionumber); |
116 |
foreach my $record_path (@other_record_paths) { |
91 |
if (not defined $res) { |
117 |
my ( $record_source, $record_num ) = @$record_path; |
92 |
push @notmoveditems, $itemnumber; |
118 |
|
|
|
119 |
if ( $record_source eq $ref_record_source ) { |
120 |
# We only want to merge in information from the other records if they're the same kind. |
121 |
# |
122 |
# We can't merge serials from an import bib to a catalog bib, for instance. |
123 |
my @record_errors = $sources->{$record_source}->{merge_other}( $record_num, $ref_record_num ); |
124 |
|
125 |
if ( @record_errors ) { |
126 |
push @errors, @record_errors; |
127 |
} else { |
128 |
push @errors, $sources->{$record_source}->{delete}( $record_num ); |
129 |
} |
130 |
} else { |
131 |
$all_records_same_source = 0; |
93 |
} |
132 |
} |
94 |
} |
133 |
} |
95 |
} |
|
|
96 |
# If some items could not be moved : |
97 |
if (scalar(@notmoveditems) > 0) { |
98 |
my $itemlist = join(' ',@notmoveditems); |
99 |
push @errors, { code => "CANNOT_MOVE", value => $itemlist }; |
100 |
} |
101 |
|
102 |
my $sth_subscription = $dbh->prepare(" |
103 |
UPDATE subscription SET biblionumber = ? WHERE biblionumber = ? |
104 |
"); |
105 |
my $sth_subscriptionhistory = $dbh->prepare(" |
106 |
UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ? |
107 |
"); |
108 |
my $sth_serial = $dbh->prepare(" |
109 |
UPDATE serial SET biblionumber = ? WHERE biblionumber = ? |
110 |
"); |
111 |
|
134 |
|
112 |
my $report_header = {}; |
135 |
my $report_header = {}; |
113 |
foreach my $biblionumber ($ref_biblionumber, @biblionumbers) { |
136 |
foreach my $record_path ( [ $ref_record_source, $ref_record_num ], @other_record_paths ) { |
|
|
137 |
my ( $record_source, $record_num ) = @$record_path; |
138 |
|
114 |
# build report |
139 |
# build report |
115 |
my $marcrecord = GetMarcBiblio($biblionumber); |
140 |
my $marcrecord = $sources->{$record_source}->{fetch}($record_num); |
116 |
my %report_record = ( |
141 |
my %report_record = ( |
117 |
biblionumber => $biblionumber, |
142 |
record_num => $record_num, |
118 |
fields => {}, |
143 |
fields => {}, |
119 |
); |
144 |
); |
120 |
foreach my $field (@report_fields) { |
145 |
foreach my $field (@report_fields) { |
Lines 143-195
if ($merge) {
Link Here
|
143 |
push @report_records, \%report_record; |
168 |
push @report_records, \%report_record; |
144 |
} |
169 |
} |
145 |
|
170 |
|
146 |
foreach my $biblionumber (@biblionumbers) { |
|
|
147 |
# Moving subscriptions from the other record to the reference record |
148 |
my $subcount = CountSubscriptionFromBiblionumber($biblionumber); |
149 |
if ($subcount > 0) { |
150 |
$sth_subscription->execute($ref_biblionumber, $biblionumber); |
151 |
$sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber); |
152 |
} |
153 |
|
154 |
# Moving serials |
155 |
$sth_serial->execute($ref_biblionumber, $biblionumber); |
156 |
|
157 |
# Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio) |
158 |
my @allorders = GetOrdersByBiblionumber($biblionumber); |
159 |
my @tobiblioitem = GetBiblioItemByBiblioNumber ($ref_biblionumber); |
160 |
my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber }; |
161 |
foreach my $myorder (@allorders) { |
162 |
$myorder->{'biblionumber'} = $ref_biblionumber; |
163 |
ModOrder ($myorder); |
164 |
# TODO : add error control (in ModOrder?) |
165 |
} |
166 |
|
167 |
# Deleting the other records |
168 |
if (scalar(@errors) == 0) { |
169 |
# Move holds |
170 |
MergeHolds($dbh, $ref_biblionumber, $biblionumber); |
171 |
my $error = DelBiblio($biblionumber); |
172 |
push @errors, $error if ($error); |
173 |
} |
174 |
} |
175 |
|
176 |
# Parameters |
171 |
# Parameters |
177 |
$template->param( |
172 |
$template->param( |
|
|
173 |
all_records_same_source => $all_records_same_source, |
178 |
result => 1, |
174 |
result => 1, |
179 |
report_records => \@report_records, |
175 |
report_records => \@report_records, |
180 |
report_header => $report_header, |
176 |
report_header => $report_header, |
181 |
ref_biblionumber => scalar $input->param('ref_biblionumber') |
177 |
ref_record_path => scalar $input->param('ref_record'), |
|
|
178 |
ref_record_id => "$ref_record_source-$ref_record_num", |
179 |
ref_record_link => $sources->{$ref_record_source}->{get_link}($ref_record_num), |
182 |
); |
180 |
); |
183 |
|
181 |
|
184 |
#------------------------- |
182 |
#------------------------- |
185 |
# Show records to merge |
183 |
# Show records to merge |
186 |
#------------------------- |
184 |
#------------------------- |
187 |
} else { |
185 |
} else { |
188 |
my $ref_biblionumber = $input->param('ref_biblionumber'); |
186 |
my $ref_record_path = $input->param('ref_record'); |
|
|
187 |
|
188 |
if ($ref_record_path) { |
189 |
my ( $ref_record_source, $ref_record_num ) = split_record_path $ref_record_path; |
189 |
|
190 |
|
190 |
if ($ref_biblionumber) { |
|
|
191 |
my $framework = $input->param('frameworkcode'); |
191 |
my $framework = $input->param('frameworkcode'); |
192 |
$framework //= GetFrameworkCode($ref_biblionumber); |
192 |
$framework //= GetFrameworkCode($ref_record_num) if $ref_record_source eq 'catalog'; |
|
|
193 |
$framework //= ''; |
193 |
|
194 |
|
194 |
# Getting MARC Structure |
195 |
# Getting MARC Structure |
195 |
my $tagslib = GetMarcStructure(1, $framework); |
196 |
my $tagslib = GetMarcStructure(1, $framework); |
Lines 198-216
if ($merge) {
Link Here
|
198 |
|
199 |
|
199 |
# Creating a loop for display |
200 |
# Creating a loop for display |
200 |
my @records; |
201 |
my @records; |
201 |
foreach my $biblionumber (@biblionumbers) { |
202 |
foreach my $record_path ( $input->param('record') ) { |
202 |
my $marcrecord = GetMarcBiblio($biblionumber); |
203 |
my ( $record_source, $record_num ) = split_record_path $record_path; |
203 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
204 |
my $marcrecord = $sources->{$record_source}->{fetch}($record_num); |
|
|
205 |
# FIXME: We assume the default framework for import records. |
206 |
my $frameworkcode = $record_source eq 'catalog' ? GetFrameworkCode($record_num) : ''; |
204 |
my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour}); |
207 |
my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour}); |
205 |
my $record = { |
208 |
my $record = { |
206 |
recordid => $biblionumber, |
209 |
record_path => $record_path, |
|
|
210 |
recordid => "$record_source-$record_num", |
211 |
record_source => $record_source, |
212 |
record_num => $record_num, |
207 |
record => $marcrecord, |
213 |
record => $marcrecord, |
208 |
frameworkcode => $frameworkcode, |
214 |
frameworkcode => $frameworkcode, |
209 |
display => $recordObj->createMergeHash($tagslib), |
215 |
display => $recordObj->createMergeHash($tagslib), |
210 |
}; |
216 |
}; |
211 |
if ($ref_biblionumber and $ref_biblionumber == $biblionumber) { |
217 |
if ($ref_record_path and $ref_record_path eq $record_path) { |
212 |
$record->{reference} = 1; |
218 |
$record->{reference} = 1; |
213 |
$template->param(ref_record => $record); |
219 |
$template->param(ref_record_path => $record); |
214 |
unshift @records, $record; |
220 |
unshift @records, $record; |
215 |
} else { |
221 |
} else { |
216 |
push @records, $record; |
222 |
push @records, $record; |
Lines 221-227
if ($merge) {
Link Here
|
221 |
|
227 |
|
222 |
# Parameters |
228 |
# Parameters |
223 |
$template->param( |
229 |
$template->param( |
224 |
ref_biblionumber => $ref_biblionumber, |
230 |
ref_record_path => $ref_record_path, |
|
|
231 |
ref_recordid => "$ref_record_source-$ref_record_num", |
225 |
records => \@records, |
232 |
records => \@records, |
226 |
ref_record => $records[0], |
233 |
ref_record => $records[0], |
227 |
framework => $framework, |
234 |
framework => $framework, |
Lines 230-240
if ($merge) {
Link Here
|
230 |
); |
237 |
); |
231 |
} else { |
238 |
} else { |
232 |
my @records; |
239 |
my @records; |
233 |
foreach my $biblionumber (@biblionumbers) { |
240 |
foreach my $record_path ( $input->multi_param('record') ) { |
234 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
241 |
my ( $record_source, $record_num ) = split_record_path $record_path; |
|
|
242 |
|
243 |
my $marcrecord = $sources->{$record_source}->{fetch}($record_num); |
244 |
# FIXME: We assume the default framework for import records. |
245 |
my $frameworkcode = $record_source eq 'catalog' ? GetFrameworkCode($record_num) : ''; |
235 |
my $record = { |
246 |
my $record = { |
236 |
biblionumber => $biblionumber, |
247 |
record_path => $record_path, |
237 |
data => GetBiblioData($biblionumber), |
248 |
recordid => "$record_path-$record_num", |
|
|
249 |
record_source => $record_source, |
250 |
record_num => $record_num, |
251 |
preview_link => $sources->{$record_source}->{get_preview_link}($record_num), |
252 |
data => TransformMarcToKoha( $marcrecord, $frameworkcode ), |
238 |
frameworkcode => $frameworkcode, |
253 |
frameworkcode => $frameworkcode, |
239 |
}; |
254 |
}; |
240 |
push @records, $record; |
255 |
push @records, $record; |
Lines 266-269
if (@errors) {
Link Here
|
266 |
} |
281 |
} |
267 |
|
282 |
|
268 |
output_html_with_http_headers $input, $cookie, $template->output; |
283 |
output_html_with_http_headers $input, $cookie, $template->output; |
269 |
exit; |
284 |
|
|
|
285 |
sub _delete_catalog { |
286 |
my ( $biblionumber ) = @_; |
287 |
return DelBiblio($biblionumber); |
288 |
} |
289 |
|
290 |
sub _fetch_catalog { |
291 |
my ( $biblionumber ) = @_; |
292 |
|
293 |
return GetMarcBiblio( $biblionumber ); |
294 |
} |
295 |
|
296 |
sub _get_link_catalog { |
297 |
my ( $biblionumber ) = @_; |
298 |
|
299 |
return "/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber"; |
300 |
} |
301 |
|
302 |
sub _get_preview_link_catalog { |
303 |
my ( $biblionumber ) = @_; |
304 |
|
305 |
return "/cgi-bin/koha/catalogue/showmarc.pl?id=$biblionumber"; |
306 |
} |
307 |
|
308 |
sub _merge_other_catalog { |
309 |
my ( $biblionumber, $ref_biblionumber ) = @_; |
310 |
|
311 |
my $dbh = C4::Context->dbh; |
312 |
my @errors; |
313 |
|
314 |
my @notmoveditems; |
315 |
my $itemnumbers = get_itemnumbers_of($biblionumber); |
316 |
foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) { |
317 |
if ( !MoveItemFromBiblio( $itemnumber, $biblionumber, $ref_biblionumber ) ) { |
318 |
push @notmoveditems, $itemnumber; |
319 |
} |
320 |
} |
321 |
|
322 |
# If some items could not be moved : |
323 |
if (scalar(@notmoveditems) > 0) { |
324 |
my $itemlist = join(' ',@notmoveditems); |
325 |
push @errors, { code => "CANNOT_MOVE", value => $itemlist }; |
326 |
} |
327 |
|
328 |
my $sth_subscription = $dbh->prepare(" |
329 |
UPDATE subscription SET biblionumber = ? WHERE biblionumber = ? |
330 |
"); |
331 |
my $sth_subscriptionhistory = $dbh->prepare(" |
332 |
UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ? |
333 |
"); |
334 |
my $sth_serial = $dbh->prepare(" |
335 |
UPDATE serial SET biblionumber = ? WHERE biblionumber = ? |
336 |
"); |
337 |
|
338 |
# Moving subscriptions from the other record to the reference record |
339 |
my $subcount = CountSubscriptionFromBiblionumber($biblionumber); |
340 |
if ($subcount > 0) { |
341 |
$sth_subscription->execute($ref_biblionumber, $biblionumber); |
342 |
$sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber); |
343 |
} |
344 |
|
345 |
# Moving serials |
346 |
$sth_serial->execute($ref_biblionumber, $biblionumber); |
347 |
|
348 |
# Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio) |
349 |
foreach my $myorder( GetOrdersByBiblionumber($biblionumber) ) { |
350 |
$myorder->{'biblionumber'} = $ref_biblionumber; |
351 |
ModOrder ($myorder); |
352 |
# TODO : add error control (in ModOrder?) |
353 |
} |
354 |
|
355 |
if (scalar(@errors) == 0) { |
356 |
MergeHolds($dbh, $ref_biblionumber, $biblionumber); |
357 |
} |
358 |
|
359 |
return @errors; |
360 |
} |
361 |
|
362 |
sub _update_catalog { |
363 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
364 |
ModBiblio( $record, $biblionumber, $frameworkcode ); |
365 |
} |
366 |
|
367 |
sub _fetch_import_batch { |
368 |
my ( $import_recordid ) = @_; |
369 |
return C4::ImportBatch::GetRecordFromImportBiblio( $import_recordid ); |
370 |
} |
371 |
|
372 |
sub _get_preview_link_import_batch { |
373 |
my ( $import_recordid ) = @_; |
374 |
|
375 |
return "/cgi-bin/koha/catalogue/showmarc.pl?importid=$import_recordid"; |
376 |
} |
377 |
|
378 |
sub _update_import_batch { |
379 |
my ( $record, $import_recordid ) = @_; |
380 |
ModBiblioInBatch( $import_recordid, $record ); |
381 |
} |