|
Lines 19-24
Link Here
|
| 19 |
|
19 |
|
| 20 |
use strict; |
20 |
use strict; |
| 21 |
use warnings; |
21 |
use warnings; |
|
|
22 |
use List::MoreUtils qw(uniq); |
| 22 |
use C4::Auth; |
23 |
use C4::Auth; |
| 23 |
use C4::Output; |
24 |
use C4::Output; |
| 24 |
use C4::Biblio; # GetMarcBiblio GetXmlBiblio |
25 |
use C4::Biblio; # GetMarcBiblio GetXmlBiblio |
|
Lines 26-37
use C4::AuthoritiesMarc; # GetAuthority
Link Here
|
| 26 |
use CGI; |
27 |
use CGI; |
| 27 |
use C4::Koha; # GetItemTypes |
28 |
use C4::Koha; # GetItemTypes |
| 28 |
use C4::Branch; # GetBranches |
29 |
use C4::Branch; # GetBranches |
|
|
30 |
use C4::Record; |
| 31 |
use C4::Csv; |
| 29 |
|
32 |
|
| 30 |
my $query = new CGI; |
33 |
my $query = new CGI; |
| 31 |
my $op=$query->param("op") || ''; |
34 |
my $op=$query->param("op") || ''; |
| 32 |
my $filename=$query->param("filename"); |
35 |
my $filename=$query->param("filename") || 'koha.mrc'; |
| 33 |
my $dbh=C4::Context->dbh; |
36 |
my $dbh=C4::Context->dbh; |
| 34 |
my $marcflavour = C4::Context->preference("marcflavour"); |
37 |
my $marcflavour = C4::Context->preference("marcflavour"); |
|
|
38 |
my $format = $query->param("format") || 'iso2709'; |
| 35 |
|
39 |
|
| 36 |
my ($template, $loggedinuser, $cookie) |
40 |
my ($template, $loggedinuser, $cookie) |
| 37 |
= get_template_and_user |
41 |
= get_template_and_user |
|
Lines 58-227
my ($template, $loggedinuser, $cookie)
Link Here
|
| 58 |
} |
62 |
} |
| 59 |
|
63 |
|
| 60 |
if ($op eq "export") { |
64 |
if ($op eq "export") { |
| 61 |
binmode STDOUT, ':encoding(UTF-8)'; |
65 |
if ( $format eq "iso2709" ) { |
| 62 |
print $query->header( -type => 'application/octet-stream', |
66 |
binmode STDOUT, ':encoding(UTF-8)'; |
| 63 |
-charset => 'utf-8', |
67 |
print $query->header( |
| 64 |
-attachment=>$filename); |
68 |
-type => 'application/octet-stream', |
| 65 |
|
69 |
-charset => 'utf-8', |
| 66 |
my $record_type = $query->param("record_type"); |
70 |
-attachment=>$filename |
| 67 |
my $output_format = $query->param("output_format"); |
71 |
); |
| 68 |
my $dont_export_fields = $query->param("dont_export_fields"); |
|
|
| 69 |
my @sql_params; |
| 70 |
my $sql_query; |
| 71 |
|
72 |
|
| 72 |
my $StartingBiblionumber = $query->param("StartingBiblionumber"); |
73 |
my $record_type = $query->param("record_type"); |
| 73 |
my $EndingBiblionumber = $query->param("EndingBiblionumber"); |
74 |
my $output_format = $query->param("output_format") || 'marc'; |
| 74 |
my $itemtype = $query->param("itemtype"); |
75 |
my $dont_export_fields = $query->param("dont_export_fields"); |
| 75 |
my $start_callnumber = $query->param("start_callnumber"); |
76 |
my @biblionumbers = $query->param("biblionumbers"); |
| 76 |
my $end_callnumber = $query->param("end_callnumber"); |
77 |
my @itemnumbers = $query->param("itemnumbers"); |
| 77 |
my $start_accession = |
78 |
my @sql_params; |
| 78 |
( $query->param("start_accession") ) |
79 |
my $sql_query; |
| 79 |
? C4::Dates->new( $query->param("start_accession") ) |
80 |
my @recordids; |
| 80 |
: ''; |
|
|
| 81 |
my $end_accession = |
| 82 |
( $query->param("end_accession") ) |
| 83 |
? C4::Dates->new( $query->param("end_accession") ) |
| 84 |
: ''; |
| 85 |
my $dont_export_items = $query->param("dont_export_item"); |
| 86 |
my $strip_nonlocal_items = $query->param("strip_nonlocal_items"); |
| 87 |
|
81 |
|
| 88 |
my $starting_authid = $query->param('starting_authid'); |
82 |
my $StartingBiblionumber = $query->param("StartingBiblionumber"); |
| 89 |
my $ending_authid = $query->param('ending_authid'); |
83 |
my $EndingBiblionumber = $query->param("EndingBiblionumber"); |
| 90 |
my $authtype = $query->param('authtype'); |
84 |
my $itemtype = $query->param("itemtype"); |
|
|
85 |
my $start_callnumber = $query->param("start_callnumber"); |
| 86 |
my $end_callnumber = $query->param("end_callnumber"); |
| 87 |
my $start_accession = |
| 88 |
( $query->param("start_accession") ) |
| 89 |
? C4::Dates->new( $query->param("start_accession") ) |
| 90 |
: ''; |
| 91 |
my $end_accession = |
| 92 |
( $query->param("end_accession") ) |
| 93 |
? C4::Dates->new( $query->param("end_accession") ) |
| 94 |
: ''; |
| 95 |
my $dont_export_items = $query->param("dont_export_item"); |
| 96 |
my $strip_nonlocal_items = $query->param("strip_nonlocal_items"); |
| 91 |
|
97 |
|
| 92 |
if ( $record_type eq 'bibs' ) { |
98 |
my $starting_authid = $query->param('starting_authid'); |
| 93 |
my $items_filter = |
99 |
my $ending_authid = $query->param('ending_authid'); |
| 94 |
$branch || $start_callnumber || $end_callnumber || |
100 |
my $authtype = $query->param('authtype'); |
| 95 |
$start_accession || $end_accession || |
|
|
| 96 |
($itemtype && C4::Context->preference('item-level_itypes')); |
| 97 |
$sql_query = $items_filter ? |
| 98 |
"SELECT DISTINCT biblioitems.biblionumber |
| 99 |
FROM biblioitems JOIN items |
| 100 |
USING (biblionumber) WHERE 1" |
| 101 |
: |
| 102 |
"SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 "; |
| 103 |
|
101 |
|
| 104 |
if ( $StartingBiblionumber ) { |
102 |
if ( $record_type eq 'bibs' and not @biblionumbers ) { |
| 105 |
$sql_query .= " AND biblioitems.biblionumber >= ? "; |
103 |
my $items_filter = |
| 106 |
push @sql_params, $StartingBiblionumber; |
104 |
$branch |
| 107 |
} |
105 |
|| $start_callnumber |
|
|
106 |
|| $end_callnumber |
| 107 |
|| $start_accession |
| 108 |
|| $end_accession |
| 109 |
|| ($itemtype && C4::Context->preference('item-level_itypes')); |
| 110 |
$sql_query = $items_filter ? |
| 111 |
"SELECT DISTINCT biblioitems.biblionumber |
| 112 |
FROM biblioitems JOIN items |
| 113 |
USING (biblionumber) WHERE 1" |
| 114 |
: |
| 115 |
"SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 "; |
| 108 |
|
116 |
|
| 109 |
if ( $EndingBiblionumber ) { |
117 |
if ( $StartingBiblionumber ) { |
| 110 |
$sql_query .= " AND biblioitems.biblionumber <= ? "; |
118 |
$sql_query .= " AND biblioitems.biblionumber >= ? "; |
| 111 |
push @sql_params, $EndingBiblionumber; |
119 |
push @sql_params, $StartingBiblionumber; |
| 112 |
} |
120 |
} |
| 113 |
|
121 |
|
| 114 |
if ($branch) { |
122 |
if ( $EndingBiblionumber ) { |
| 115 |
$sql_query .= " AND homebranch = ? "; |
123 |
$sql_query .= " AND biblioitems.biblionumber <= ? "; |
| 116 |
push @sql_params, $branch; |
124 |
push @sql_params, $EndingBiblionumber; |
| 117 |
} |
125 |
} |
| 118 |
|
126 |
|
| 119 |
if ($start_callnumber) { |
127 |
if ($branch) { |
| 120 |
$sql_query .= " AND itemcallnumber <= ? "; |
128 |
$sql_query .= " AND homebranch = ? "; |
| 121 |
push @sql_params, $start_callnumber; |
129 |
push @sql_params, $branch; |
| 122 |
} |
130 |
} |
| 123 |
|
131 |
|
| 124 |
if ($end_callnumber) { |
132 |
if ($start_callnumber) { |
| 125 |
$sql_query .= " AND itemcallnumber >= ? "; |
133 |
$sql_query .= " AND itemcallnumber <= ? "; |
| 126 |
push @sql_params, $end_callnumber; |
134 |
push @sql_params, $start_callnumber; |
| 127 |
} |
135 |
} |
| 128 |
if ($start_accession) { |
|
|
| 129 |
$sql_query .= " AND dateaccessioned >= ? "; |
| 130 |
push @sql_params, $start_accession->output('iso'); |
| 131 |
} |
| 132 |
|
136 |
|
| 133 |
if ($end_accession) { |
137 |
if ($end_callnumber) { |
| 134 |
$sql_query .= " AND dateaccessioned <= ? "; |
138 |
$sql_query .= " AND itemcallnumber >= ? "; |
| 135 |
push @sql_params, $end_accession->output('iso'); |
139 |
push @sql_params, $end_callnumber; |
| 136 |
} |
140 |
} |
|
|
141 |
if ($start_accession) { |
| 142 |
$sql_query .= " AND dateaccessioned >= ? "; |
| 143 |
push @sql_params, $start_accession->output('iso'); |
| 144 |
} |
| 137 |
|
145 |
|
| 138 |
if ( $itemtype ) { |
146 |
if ($end_accession) { |
| 139 |
$sql_query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?"; |
147 |
$sql_query .= " AND dateaccessioned <= ? "; |
| 140 |
push @sql_params, $itemtype; |
148 |
push @sql_params, $end_accession->output('iso'); |
| 141 |
} |
149 |
} |
| 142 |
} |
|
|
| 143 |
elsif ( $record_type eq 'auths' ) { |
| 144 |
$sql_query = |
| 145 |
"SELECT DISTINCT auth_header.authid FROM auth_header WHERE 1"; |
| 146 |
|
150 |
|
| 147 |
if ($starting_authid) { |
151 |
if ( $itemtype ) { |
| 148 |
$sql_query .= " AND auth_header.authid >= ? "; |
152 |
$sql_query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?"; |
| 149 |
push @sql_params, $starting_authid; |
153 |
push @sql_params, $itemtype; |
|
|
154 |
} |
| 150 |
} |
155 |
} |
|
|
156 |
elsif ( $record_type eq 'auths' ) { |
| 157 |
$sql_query = |
| 158 |
"SELECT DISTINCT auth_header.authid FROM auth_header WHERE 1"; |
| 151 |
|
159 |
|
| 152 |
if ($ending_authid) { |
160 |
if ($starting_authid) { |
| 153 |
$sql_query .= " AND auth_header.authid <= ? "; |
161 |
$sql_query .= " AND auth_header.authid >= ? "; |
| 154 |
push @sql_params, $ending_authid; |
162 |
push @sql_params, $starting_authid; |
| 155 |
} |
163 |
} |
| 156 |
|
164 |
|
| 157 |
if ($authtype) { |
165 |
if ($ending_authid) { |
| 158 |
$sql_query .= " AND auth_header.authtypecode = ? "; |
166 |
$sql_query .= " AND auth_header.authid <= ? "; |
| 159 |
push @sql_params, $authtype; |
167 |
push @sql_params, $ending_authid; |
| 160 |
} |
168 |
} |
| 161 |
} |
|
|
| 162 |
|
169 |
|
| 163 |
my $sth = $dbh->prepare($sql_query); |
170 |
if ($authtype) { |
| 164 |
$sth->execute(@sql_params); |
171 |
$sql_query .= " AND auth_header.authtypecode = ? "; |
|
|
172 |
push @sql_params, $authtype; |
| 173 |
} |
| 174 |
} elsif ( @biblionumbers ) { |
| 175 |
push @recordids, (@biblionumbers); |
| 176 |
} |
| 165 |
|
177 |
|
| 166 |
while ( my ($recordid) = $sth->fetchrow ) { |
178 |
unless ( @biblionumbers ) { |
| 167 |
my $record; |
179 |
my $sth = $dbh->prepare($sql_query); |
| 168 |
if ( $record_type eq 'bibs' ) { |
180 |
$sth->execute(@sql_params); |
| 169 |
$record = eval { GetMarcBiblio($recordid); }; |
181 |
push @recordids, map { map { $$_[0] } $_ } @{$sth->fetchall_arrayref}; |
|
|
182 |
} |
| 170 |
|
183 |
|
| 171 |
# FIXME: decide how to handle records GetMarcBiblio can't parse or retrieve |
184 |
for my $recordid ( uniq @recordids ) { |
| 172 |
if ($@) { |
185 |
my $record; |
| 173 |
next; |
186 |
if ( $record_type eq 'bibs' ) { |
| 174 |
} |
187 |
$record = eval { GetMarcBiblio($recordid); }; |
| 175 |
next if not defined $record; |
188 |
# FIXME: decide how to handle records GetMarcBiblio can't parse or retrieve |
| 176 |
C4::Biblio::EmbedItemsInMarcBiblio( $record, $recordid ) |
189 |
next if $@; |
| 177 |
unless $dont_export_items; |
190 |
next if not defined $record; |
| 178 |
if ( $strip_nonlocal_items || $limit_ind_branch ) { |
191 |
C4::Biblio::EmbedItemsInMarcBiblio( $record, $recordid, \@itemnumbers ) |
| 179 |
my ( $homebranchfield, $homebranchsubfield ) = |
192 |
unless $dont_export_items; |
| 180 |
GetMarcFromKohaField( 'items.homebranch', '' ); |
193 |
if ( $strip_nonlocal_items || $limit_ind_branch || $dont_export_items ) { |
| 181 |
for my $itemfield ( $record->field($homebranchfield) ) { |
194 |
my ( $homebranchfield, $homebranchsubfield ) = |
|
|
195 |
GetMarcFromKohaField( 'items.homebranch', '' ); |
| 196 |
for my $itemfield ( $record->field($homebranchfield) ) { |
| 182 |
|
197 |
|
| 183 |
# if stripping nonlocal items, use loggedinuser's branch if they didn't select one |
198 |
# if stripping nonlocal items, use loggedinuser's branch if they didn't select one |
| 184 |
$branch = C4::Context->userenv->{'branch'} unless $branch; |
199 |
$branch = C4::Context->userenv->{'branch'} unless $branch; |
| 185 |
$record->delete_field($itemfield) |
200 |
$record->delete_field($itemfield) |
| 186 |
if ( |
201 |
if ( $dont_export_items || $itemfield->subfield($homebranchsubfield) ne $branch ); |
| 187 |
$itemfield->subfield($homebranchsubfield) ne $branch ); |
202 |
} |
| 188 |
} |
203 |
} |
| 189 |
} |
204 |
} |
| 190 |
} |
205 |
elsif ( $record_type eq 'auths' ) { |
| 191 |
elsif ( $record_type eq 'auths' ) { |
206 |
$record = C4::AuthoritiesMarc::GetAuthority($recordid); |
| 192 |
$record = C4::AuthoritiesMarc::GetAuthority($recordid); |
207 |
next if not defined $record; |
| 193 |
next if not defined $record; |
208 |
} |
| 194 |
} |
|
|
| 195 |
|
209 |
|
| 196 |
if ( $dont_export_fields ) { |
210 |
if ( $dont_export_fields ) { |
| 197 |
my @fields = split " ", $dont_export_fields; |
211 |
my @fields = split " ", $dont_export_fields; |
| 198 |
foreach ( @fields ) { |
212 |
foreach ( @fields ) { |
| 199 |
/^(\d*)(\w)?$/; |
213 |
/^(\d*)(\w)?$/; |
| 200 |
my $field = $1; |
214 |
my $field = $1; |
| 201 |
my $subfield = $2; |
215 |
my $subfield = $2; |
| 202 |
# skip if this record doesn't have this field |
216 |
# skip if this record doesn't have this field |
| 203 |
next if not defined $record->field($field); |
217 |
next if not defined $record->field($field); |
| 204 |
if( $subfield ) { |
218 |
if( $subfield ) { |
| 205 |
$record->field($field)->delete_subfields($subfield); |
219 |
$record->field($field)->delete_subfields($subfield); |
|
|
220 |
} |
| 221 |
else { |
| 222 |
$record->delete_field($record->field($field)); |
| 223 |
} |
| 206 |
} |
224 |
} |
| 207 |
else { |
225 |
} |
| 208 |
$record->delete_field($record->field($field)); |
226 |
if ( $output_format eq "xml" ) { |
|
|
227 |
if ($marcflavour eq 'UNIMARC' && $record_type eq 'auths') { |
| 228 |
print $record->as_xml_record('UNIMARCAUTH'); |
| 229 |
} else { |
| 230 |
print $record->as_xml_record($marcflavour); |
| 209 |
} |
231 |
} |
| 210 |
} |
232 |
} |
| 211 |
} |
233 |
else { |
| 212 |
if ( $output_format eq "xml" ) { |
234 |
print $record->as_usmarc(); |
| 213 |
if ($marcflavour eq 'UNIMARC' && $record_type eq 'auths') { |
|
|
| 214 |
print $record->as_xml_record('UNIMARCAUTH'); |
| 215 |
} else { |
| 216 |
print $record->as_xml_record($marcflavour); |
| 217 |
} |
235 |
} |
| 218 |
} |
236 |
} |
| 219 |
else { |
237 |
exit; |
| 220 |
print $record->as_usmarc(); |
238 |
} |
| 221 |
} |
239 |
elsif ( $format eq "csv" ) { |
|
|
240 |
my @biblionumbers = uniq $query->param("biblionumbers"); |
| 241 |
my @itemnumbers = $query->param("itemnumbers"); |
| 242 |
my $output = marc2csv( |
| 243 |
\@biblionumbers, |
| 244 |
GetCsvProfileId(C4::Context->preference('CsvProfileForExport')), |
| 245 |
\@itemnumbers, |
| 246 |
); |
| 247 |
print $query->header( |
| 248 |
-type => 'application/octet-stream', |
| 249 |
-'Content-Transfer-Encoding' => 'binary', |
| 250 |
-attachment => "export.csv" |
| 251 |
); |
| 252 |
print $output; |
| 253 |
exit; |
| 222 |
} |
254 |
} |
| 223 |
exit; |
|
|
| 224 |
|
| 225 |
} # if export |
255 |
} # if export |
| 226 |
|
256 |
|
| 227 |
else { |
257 |
else { |
|
Lines 264-269
else {
Link Here
|
| 264 |
itemtypeloop => \@itemtypesloop, |
294 |
itemtypeloop => \@itemtypesloop, |
| 265 |
DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), |
295 |
DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), |
| 266 |
authtypeloop => \@authtypesloop, |
296 |
authtypeloop => \@authtypesloop, |
|
|
297 |
dont_export_fields => C4::Context->preference("DontExportFields"), |
| 267 |
); |
298 |
); |
| 268 |
|
299 |
|
| 269 |
output_html_with_http_headers $query, $cookie, $template->output; |
300 |
output_html_with_http_headers $query, $cookie, $template->output; |
| 270 |
- |
|
|