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