fixing export tool to export iso2709 correctly
[koha.git] / tools / export.pl
1 #!/usr/bin/perl
2
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;
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
28
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");
34
35 if ($op eq "export") {
36         print $query->header(   -type => 'application/octet-stream', 
37                                                         -attachment=>$filename);
38     
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 ";
52                   
53     if ( $StartingBiblionumber ) {
54         $query .= " AND biblioitems.biblionumber >= ? ";
55         push @sql_params, $StartingBiblionumber;
56     }
57     
58     if ( $EndingBiblionumber ) {
59         $query .= " AND biblioitems.biblionumber <= ? ";
60         push @sql_params, $EndingBiblionumber;    
61     }
62     
63     if ( $branch ) {
64         $query .= " AND biblioitems.biblionumber = items.biblionumber AND homebranch = ? ";
65         push @sql_params, $branch;
66     }
67     
68     if ( $start_callnumber ) {
69         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber <= ? ";
70         push @sql_params, $start_callnumber;
71     }
72     
73     if ( $end_callnumber ) {
74         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber >= ? ";
75         push @sql_params, $end_callnumber;
76     }
77     
78     if ( $itemtype ) {
79         $query .= " AND biblioitems.itemtype = ?";
80         push @sql_params, $itemtype;
81     }
82
83     my $sth = $dbh->prepare($query);
84     $sth->execute(@sql_params);
85     
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', '' );
92
93             # and delete it.
94             foreach ($record->field($itemnumberfield)){
95                 $record->delete_field($record->field($itemnumberfield));
96             }
97         }
98         
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);
107                 }
108                 else {
109                     $record->delete_field($record->field($field));
110                 }
111             }
112         }
113         if ( $output_format eq "xml" ) {
114             print $record->as_xml_record($marcflavour);
115         }
116         else {
117             print $record->as_usmarc(); 
118         }
119     }
120     exit;
121     
122 } # if export
123
124 else {
125
126     my $itemtypes = GetItemTypes;
127     my @itemtypesloop;
128     foreach my $thisitemtype (sort keys %$itemtypes) {
129         my %row =
130             (
131                 value => $thisitemtype,
132                 description => $itemtypes->{$thisitemtype}->{'description'},
133             );
134        push @itemtypesloop, \%row;
135     }
136     
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'},
146        );
147        push @branchloop, \%row;
148     }
149     
150     my ($template, $loggedinuser, $cookie)
151     = get_template_and_user
152     (
153         {
154             template_name => "tools/export.tmpl",
155             query => $query,
156             type => "intranet",
157             authnotrequired => 0,
158             flagsrequired => {tools => 1},
159             debug => 1,
160          }
161     );
162     
163     $template->param(
164         branchloop   => \@branchloop,
165         itemtypeloop => \@itemtypesloop
166     );
167     
168     output_html_with_http_headers $query, $cookie, $template->output;
169 }