Shelves wrap up - bugfix 1405, opac checkbox js fixed
[koha.git] / tools / export.pl
blob1fb1dae67a4e8981ef57595ad3374dc1721f8442
1 #!/usr/bin/perl
4 # This file is part of Koha.
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.
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.
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
20 use strict;
21 require Exporter;
22 use C4::Auth;
23 use C4::Output; # contains gettemplate
24 use C4::Biblio; # GetMarcBiblio GetXmlBiblio
25 use CGI;
26 use C4::Koha; # GetItemTypes
27 use C4::Branch; # GetBranches
29 my $query = new CGI;
30 my $op=$query->param("op");
31 my $filename=$query->param("filename");
32 my $dbh=C4::Context->dbh;
33 my $marcflavour = C4::Context->preference("marcflavour");
35 if ($op eq "export") {
36 print $query->header( -type => 'application/octet-stream',
37 -attachment=>$filename);
39 my $StartingBiblionumber = $query->param("StartingBiblionumber");
40 my $EndingBiblionumber = $query->param("EndingBiblionumber");
41 my $output_format = $query->param("output_format");
42 my $branch = $query->param("branch");
43 my $itemtype = $query->param("itemtype");
44 my $start_callnumber = $query->param("start_callnumber");
45 my $end_callnumber = $query->param("end_callnumber");
46 my $dont_export_items = $query->param("dont_export_item");
47 my $dont_export_fields = $query->param("dont_export_fields");
48 my @sql_params;
49 my $query = " SELECT DISTINCT biblioitems.biblionumber
50 FROM biblioitems,items
51 WHERE biblioitems.biblionumber=items.biblionumber ";
53 if ( $StartingBiblionumber ) {
54 $query .= " AND biblioitems.biblionumber >= ? ";
55 push @sql_params, $StartingBiblionumber;
58 if ( $EndingBiblionumber ) {
59 $query .= " AND biblioitems.biblionumber <= ? ";
60 push @sql_params, $EndingBiblionumber;
63 if ( $branch ) {
64 $query .= " AND biblioitems.biblionumber = items.biblionumber AND homebranch = ? ";
65 push @sql_params, $branch;
68 if ( $start_callnumber ) {
69 $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber <= ? ";
70 push @sql_params, $start_callnumber;
73 if ( $end_callnumber ) {
74 $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber >= ? ";
75 push @sql_params, $end_callnumber;
78 if ( $itemtype ) {
79 $query .= " AND biblioitems.itemtype = ?";
80 push @sql_params, $itemtype;
83 my $sth = $dbh->prepare($query);
84 $sth->execute(@sql_params);
86 while (my ($biblionumber) = $sth->fetchrow) {
87 my $record = GetMarcBiblio($biblionumber);
88 if ( $dont_export_items ) {
89 # now, find where the itemnumber is stored & extract only the item
90 my ( $itemnumberfield, $itemnumbersubfield ) =
91 GetMarcFromKohaField( 'items.itemnumber', '' );
93 # and delete it.
94 foreach ($record->field($itemnumberfield)){
95 $record->delete_field($record->field($itemnumberfield));
99 if ( $dont_export_fields ) {
100 my @fields = split " ", $dont_export_fields;
101 foreach ( @fields ) {
102 /^(\d*)(\w)?$/;
103 my $field = $1;
104 my $subfield = $2;
105 if( $subfield ) {
106 $record->field($field)->delete_subfields($subfield);
108 else {
109 $record->delete_field($record->field($field));
113 if ( $output_format eq "xml" ) {
114 print $record->as_xml_record($marcflavour);
116 else {
117 print $record->as_usmarc();
120 exit;
122 } # if export
124 else {
126 my $itemtypes = GetItemTypes;
127 my @itemtypesloop;
128 foreach my $thisitemtype (sort keys %$itemtypes) {
129 my %row =
131 value => $thisitemtype,
132 description => $itemtypes->{$thisitemtype}->{'description'},
134 push @itemtypesloop, \%row;
137 my $branches = GetBranches;
138 my $branch = GetBranch($query,$branches);
139 my @branchloop;
140 foreach my $thisbranch (keys %$branches) {
141 my $selected = 1 if $thisbranch eq $branch;
142 my %row = (
143 value => $thisbranch,
144 selected => $selected,
145 branchname => $branches->{$thisbranch}->{'branchname'},
147 push @branchloop, \%row;
150 my ($template, $loggedinuser, $cookie)
151 = get_template_and_user
154 template_name => "tools/export.tmpl",
155 query => $query,
156 type => "intranet",
157 authnotrequired => 0,
158 flagsrequired => {tools => 1},
159 debug => 1,
163 $template->param(
164 branchloop => \@branchloop,
165 itemtypeloop => \@itemtypesloop
168 output_html_with_http_headers $query, $cookie, $template->output;