Lines 1-137
Link Here
|
1 |
#! /usr/bin/perl |
|
|
2 |
# vim: enc=utf-8 fdm=marker fdn=1 sw=4 |
3 |
use utf8; |
4 |
use strict; |
5 |
use warnings; |
6 |
use Devel::SimpleTrace; |
7 |
use Storable qw< freeze thaw >; |
8 |
use C4::Auth; |
9 |
use C4::Context; |
10 |
use C4::Biblio; |
11 |
use C4::VirtualShelves; |
12 |
use YAML; |
13 |
use CGI; |
14 |
|
15 |
sub debug { print STDERR @_,"\n" } |
16 |
|
17 |
# Global values {{{ |
18 |
my $query = CGI->new; |
19 |
my $sessionID = (checkauth($query))[2]; |
20 |
my $session = C4::Auth::get_session( $sessionID ); |
21 |
|
22 |
my ( $template, $user, $cookie ) = get_template_and_user { |
23 |
qw( |
24 |
template_name virtualshelves/merge.tmpl |
25 |
type intranet |
26 |
authnotrequired 0 |
27 |
) |
28 |
, query => $query |
29 |
, flagsrequired => {} |
30 |
}; |
31 |
# }}} |
32 |
# Functions {{{ |
33 |
|
34 |
sub render { |
35 |
use C4::Output; |
36 |
$template->param( session => $sessionID ); |
37 |
output_html_with_http_headers $query, $cookie, $template->output; |
38 |
} |
39 |
|
40 |
sub maybe_lost { |
41 |
$template->param( maybe_lost => 1 ); |
42 |
render; exit; |
43 |
} |
44 |
|
45 |
sub fields_to_merge { $session->param('fields_to_merge') } |
46 |
|
47 |
# global values ... but only for nextid |
48 |
|
49 |
my $selection_id = 0; |
50 |
sub nextid { $selection_id++ } |
51 |
|
52 |
sub prepare_subfield { { key => $$_[0], value => $$_[1] } } |
53 |
|
54 |
sub prepare_field { |
55 |
my $ready = { |
56 |
field => freeze($_) |
57 |
, from => shift |
58 |
, tag => $_->tag |
59 |
, id => nextid |
60 |
}; |
61 |
|
62 |
if ( $_->is_control_field ) { |
63 |
$$ready{control} = 1; |
64 |
} else { |
65 |
$$ready{subfields} = [ map prepare_subfield, $_->subfields ] |
66 |
}; |
67 |
|
68 |
$ready; |
69 |
} |
70 |
|
71 |
sub fields_by_tag { $$a{tag} cmp $$b{tag} } |
72 |
|
73 |
sub build_records { |
74 |
my $newbiblio = MARC::Record->new; |
75 |
my $items = MARC::Record->new; |
76 |
my ( $selected_fields ) = @_; |
77 |
my $stored_fields = $session->param('fields'); |
78 |
my $kept_biblio = $stored_fields->[0]->{id}; |
79 |
my %biblio_to_delete; |
80 |
|
81 |
for ( @$stored_fields ) { |
82 |
my $from = $$_{from}; |
83 |
$biblio_to_delete{ $from } = 1; |
84 |
|
85 |
if ( $$_{tag} eq '995' ) { |
86 |
if ( $from != $kept_biblio ) { |
87 |
$items->append_fields( thaw $$_{field} ) |
88 |
} |
89 |
} else { |
90 |
if ( exists $$selected_fields{ $$_{id} } ) { |
91 |
$newbiblio->append_fields( thaw $$_{field} ) |
92 |
} |
93 |
} |
94 |
} |
95 |
delete $biblio_to_delete{ $kept_biblio }; |
96 |
( $newbiblio, $kept_biblio, $items, [ values %biblio_to_delete ] ); |
97 |
} |
98 |
|
99 |
sub clear_session { (shift)->clear([qw< fields shelf >]) } |
100 |
|
101 |
# }}} |
102 |
# the controller {{{ |
103 |
|
104 |
if ( my %field_selection = map { $_ => 1 } $query->param('selected_field') ) { |
105 |
my ($record, $number, $items, $delete ) = build_records( \%field_selection ); |
106 |
ModBiblio( $record, $number, GetFrameworkCode( $number )); |
107 |
AddItems( $items, $number ); |
108 |
for ( @$delete ) { |
109 |
if ( my $error = DelBiblio($_) ) { |
110 |
die $error |
111 |
} |
112 |
} |
113 |
} else { |
114 |
my $shelf = $query->param('shelf') or maybe_lost; |
115 |
if ( my @records = C4::VirtualShelves::each_biblionumbers { |
116 |
{ id => $_, record => GetMarcBiblio($_) } |
117 |
} $shelf |
118 |
) { |
119 |
my @fields = sort {fields_by_tag} map { |
120 |
my ( $id , $record ) = @$_{qw< id record >}; |
121 |
map { prepare_field($id) } $record->fields; |
122 |
} @records; |
123 |
my @store = ( fields => \@fields ); |
124 |
|
125 |
for ($session) { |
126 |
clear_session($_); |
127 |
$_->param(@store, shelf => $shelf ); |
128 |
} |
129 |
$template->param(@store); |
130 |
} else { |
131 |
die "GET LOST ?"; |
132 |
} |
133 |
} |
134 |
|
135 |
render; |
136 |
|
137 |
# }}} |
138 |
- |