Skip to content
Newer
Older
100755 220 lines (198 sloc) 8.14 KB
a0b06fb @ctfliblime Prepare for perlbrew usage by changing all script magic to "#!/usr/bi…
ctfliblime authored
1 #!/usr/bin/env perl
5b41a8e New XML API
tgarip1957 authored
2
fc1342f rel_3_0 moved to HEAD
tipaul authored
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
18
19
20 use strict;
e805deb @ccatalfo bug 1907 Partial fix: prevent bad records from truncating output.
ccatalfo authored
21 use warnings;
833e6bf Adding tools directory template and scripts
hdl authored
22 use C4::Auth;
fc1342f rel_3_0 moved to HEAD
tipaul authored
23 use C4::Output; # contains gettemplate
24 use C4::Biblio; # GetMarcBiblio GetXmlBiblio
ac6e99b @pjones9 PT 8642761: include item fields for MARC exporter tool
pjones9 authored
25 use C4::Items;
833e6bf Adding tools directory template and scripts
hdl authored
26 use CGI;
fc1342f rel_3_0 moved to HEAD
tipaul authored
27 use C4::Koha; # GetItemTypes
28 use C4::Branch; # GetBranches
29
833e6bf Adding tools directory template and scripts
hdl authored
30 my $query = new CGI;
0a6c834 @ccatalfo Bug 1907 partial fix: prevent warnings on undefined params.
ccatalfo authored
31 my $op=$query->param("op") || '';
6cdf083 new virtual shelves changes, keyed by biblionumber
Joshua Ferraro authored
32 my $filename=$query->param("filename");
fc1342f rel_3_0 moved to HEAD
tipaul authored
33 my $dbh=C4::Context->dbh;
34 my $marcflavour = C4::Context->preference("marcflavour");
35
4f283bc @PaulPoulain BUGFIX : encoding problem & security problem
PaulPoulain authored
36 my ($template, $loggedinuser, $cookie)
37 = get_template_and_user
38 (
39 {
40 template_name => "tools/export.tmpl",
41 query => $query,
42 type => "intranet",
43 authnotrequired => 0,
d853be9 @PaulPoulain BUGFIX : encoding problem & security problem
PaulPoulain authored
44 flagsrequired => {tools => 'export_catalog'},
4f283bc @PaulPoulain BUGFIX : encoding problem & security problem
PaulPoulain authored
45 debug => 1,
46 }
47 );
48
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
49 my $limit_ind_branch=(C4::Context->preference('IndependantBranches') &&
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
50 C4::Context->userenv &&
6924e7d @PaulPoulain fix for 2997 : superlibrarian's ability to edit item/patron home bran…
PaulPoulain authored
51 C4::Context->userenv->{flags} % 2 !=1 &&
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
52 C4::Context->userenv->{branch}?1:0);
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
53 my $branches = GetBranches($limit_ind_branch);
54 my $branch = $query->param("branch") || '';
55 if ( C4::Context->preference("IndependantBranches") ) {
56 $branch = C4::Context->userenv->{'branch'};
57 }
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
58
833e6bf Adding tools directory template and scripts
hdl authored
59 if ($op eq "export") {
4f283bc @PaulPoulain BUGFIX : encoding problem & security problem
PaulPoulain authored
60 binmode(STDOUT,":utf8");
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
61 print $query->header( -type => 'application/octet-stream',
4f283bc @PaulPoulain BUGFIX : encoding problem & security problem
PaulPoulain authored
62 -charset => 'utf-8',
63 -attachment=>$filename);
64
fc1342f rel_3_0 moved to HEAD
tipaul authored
65 my $StartingBiblionumber = $query->param("StartingBiblionumber");
66 my $EndingBiblionumber = $query->param("EndingBiblionumber");
67 my $output_format = $query->param("output_format");
68 my $itemtype = $query->param("itemtype");
69 my $start_callnumber = $query->param("start_callnumber");
70 my $end_callnumber = $query->param("end_callnumber");
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
71 my $start_accession = ($query->param("start_accession")) ? C4::Dates->new($query->param("start_accession")) : '' ;
72 my $end_accession = ($query->param("end_accession")) ? C4::Dates->new($query->param("end_accession")) : '' ;
fc1342f rel_3_0 moved to HEAD
tipaul authored
73 my $dont_export_items = $query->param("dont_export_item");
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
74 my $strip_nonlocal_items = $query->param("strip_nonlocal_items");
fc1342f rel_3_0 moved to HEAD
tipaul authored
75 my $dont_export_fields = $query->param("dont_export_fields");
76 my @sql_params;
550801c @fredericd Fixed bug #2357
fredericd authored
77
78 my $items_filter =
79 $branch || $start_callnumber || $end_callnumber ||
80 $start_accession || $end_accession ||
81 ($itemtype && C4::Context->preference('item-level_itypes'));
82 my $query = $items_filter ?
83 "SELECT DISTINCT biblioitems.biblionumber
84 FROM biblioitems,items
85 WHERE biblioitems.biblionumber=items.biblionumber "
86 :
87 "SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ";
fc1342f rel_3_0 moved to HEAD
tipaul authored
88
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
89 if ( $StartingBiblionumber ) {
90 if ($EndingBiblionumber) {
91 $query .= ' AND biblioitems.biblionumber BETWEEN ? AND ? ';
92 push @sql_params, $StartingBiblionumber, $EndingBiblionumber;
93 }
94 else {
95 $query .= ' AND biblioitems.biblionumber >= ? ';
96 push @sql_params, $StartingBiblionumber;
97 }
98 }
99 elsif ( $EndingBiblionumber ) {
fc1342f rel_3_0 moved to HEAD
tipaul authored
100 $query .= " AND biblioitems.biblionumber <= ? ";
101 push @sql_params, $EndingBiblionumber;
102 }
103
104 if ( $branch ) {
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
105 $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.homebranch = ? ";
fc1342f rel_3_0 moved to HEAD
tipaul authored
106 push @sql_params, $branch;
107 }
108
109 if ( $start_callnumber ) {
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
110 if ($end_callnumber) {
111 $query .= ' AND biblioitems.biblionumber = items.biblionumber
112 AND items.itemcallnumber BETWEEN ? AND ? ';
113 push @sql_params, $start_callnumber, $end_callnumber;
114 }
115 else {
116 $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.itemcallnumber <= ? ";
117 push @sql_params, $start_callnumber;
118 }
119 }
120 elsif ( $end_callnumber ) {
121 $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.itemcallnumber >= ? ";
fc1342f rel_3_0 moved to HEAD
tipaul authored
122 push @sql_params, $end_callnumber;
123 }
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
124 if ( $start_accession ) {
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
125 $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.dateaccessioned >= ? ";
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
126 push @sql_params,$start_accession->output('iso');
127 }
128
129 if ( $end_accession ) {
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
130 $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.dateaccessioned <= ? ";
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
131 push @sql_params, $end_accession->output('iso');
132 }
fc1342f rel_3_0 moved to HEAD
tipaul authored
133
134 if ( $itemtype ) {
e613ef5 Apply item-level_itypes syspref to MARC export.
Ryan Higgins authored
135 $query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?";
fc1342f rel_3_0 moved to HEAD
tipaul authored
136 push @sql_params, $itemtype;
137 }
138 my $sth = $dbh->prepare($query);
139 $sth->execute(@sql_params);
140
141 while (my ($biblionumber) = $sth->fetchrow) {
ac6e99b @pjones9 PT 8642761: include item fields for MARC exporter tool
pjones9 authored
142 my $record = eval{ ($dont_export_items)
143 ? C4::Biblio::GetMarcBiblio($biblionumber)
144 : C4::Items::GetMarcWithItems($biblionumber);
145 };
146 # FIXME: decide how to handle unparseable or irretrievable records
0a6c834 @ccatalfo Bug 1907 partial fix: prevent warnings on undefined params.
ccatalfo authored
147 if ($@) {
ac6e99b @pjones9 PT 8642761: include item fields for MARC exporter tool
pjones9 authored
148 warn "Error processing biblio $biblionumber";
0a6c834 @ccatalfo Bug 1907 partial fix: prevent warnings on undefined params.
ccatalfo authored
149 next;
150 }
e805deb @ccatalfo bug 1907 Partial fix: prevent bad records from truncating output.
ccatalfo authored
151 next if not defined $record;
ac6e99b @pjones9 PT 8642761: include item fields for MARC exporter tool
pjones9 authored
152 if ( $strip_nonlocal_items || $limit_ind_branch) {
550801c @fredericd Fixed bug #2357
fredericd authored
153 my ( $homebranchfield, $homebranchsubfield ) =
154 GetMarcFromKohaField( 'items.homebranch', '' );
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
155 for my $itemfield ($record->field($homebranchfield)){
156 # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
157 $branch = C4::Context->userenv->{'branch'} unless $branch;
ac6e99b @pjones9 PT 8642761: include item fields for MARC exporter tool
pjones9 authored
158 $record->delete_field($itemfield) if ($itemfield->subfield($homebranchsubfield) ne $branch);
fc1342f rel_3_0 moved to HEAD
tipaul authored
159 }
160 }
161
162 if ( $dont_export_fields ) {
163 my @fields = split " ", $dont_export_fields;
164 foreach ( @fields ) {
165 /^(\d*)(\w)?$/;
166 my $field = $1;
167 my $subfield = $2;
e805deb @ccatalfo bug 1907 Partial fix: prevent bad records from truncating output.
ccatalfo authored
168 # skip if this record doesn't have this field
169 next if not defined $record->field($field);
fc1342f rel_3_0 moved to HEAD
tipaul authored
170 if( $subfield ) {
171 $record->field($field)->delete_subfields($subfield);
172 }
173 else {
174 $record->delete_field($record->field($field));
175 }
176 }
177 }
178 if ( $output_format eq "xml" ) {
179 print $record->as_xml_record($marcflavour);
180 }
181 else {
c55cca8 fixing export tool to export iso2709 correctly
Paul POULAIN authored
182 print $record->as_usmarc();
fc1342f rel_3_0 moved to HEAD
tipaul authored
183 }
184 }
185 exit;
186
187 } # if export
188
189 else {
190
191 my $itemtypes = GetItemTypes;
192 my @itemtypesloop;
193 foreach my $thisitemtype (sort keys %$itemtypes) {
194 my %row =
195 (
196 value => $thisitemtype,
197 description => $itemtypes->{$thisitemtype}->{'description'},
198 );
199 push @itemtypesloop, \%row;
200 }
201 my @branchloop;
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
202 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
fc1342f rel_3_0 moved to HEAD
tipaul authored
203 my $selected = 1 if $thisbranch eq $branch;
204 my %row = (
205 value => $thisbranch,
206 selected => $selected,
207 branchname => $branches->{$thisbranch}->{'branchname'},
208 );
209 push @branchloop, \%row;
210 }
211
212 $template->param(
213 branchloop => \@branchloop,
34b4e77 Independent-branchify MARC export. Add option to remove all holdings …
Ryan Higgins authored
214 itemtypeloop => \@itemtypesloop,
7221398 @hgq [fixes #13461471] batch export bibliographic records by callnumber range
hgq authored
215 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
fc1342f rel_3_0 moved to HEAD
tipaul authored
216 );
217
218 output_html_with_http_headers $query, $cookie, $template->output;
219 }
Something went wrong with that request. Please try again.