*** empty log message ***
[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 # $Id$
20
21 use strict;
22 require Exporter;
23 use C4::Auth;
24 use C4::Interface::CGI::Output;
25 use C4::Output;  # contains gettemplate
26 use C4::Biblio;  # GetMarcBiblio GetXmlBiblio
27 use CGI;
28 use C4::Koha;    # GetItemTypes
29 use C4::Branch;  # GetBranches
30
31 my $query = new CGI;
32 my $op=$query->param("op");
33 my $dbh=C4::Context->dbh;
34 my $marcflavour = C4::Context->preference("marcflavour");
35
36 if ($op eq "export") {
37
38     print $query->header('Content-Type: text/marc');
39     
40     my $StartingBiblionumber  = $query->param("StartingBiblionumber");
41     my $EndingBiblionumber    = $query->param("EndingBiblionumber");
42     my $output_format         = $query->param("output_format");
43     my $branch                = $query->param("branch");
44     my $itemtype              = $query->param("itemtype");
45     my $start_callnumber      = $query->param("start_callnumber");
46     my $end_callnumber        = $query->param("end_callnumber");
47     my $dont_export_items     = $query->param("dont_export_item");
48     my $dont_export_fields    = $query->param("dont_export_fields");
49     my @sql_params;
50     my $query = " SELECT DISTINCT biblioitems.biblionumber
51                   FROM biblioitems,items
52                   WHERE biblioitems.biblionumber=items.biblionumber ";
53                   
54     if ( $StartingBiblionumber ) {
55         $query .= " AND biblioitems.biblionumber >= ? ";
56         push @sql_params, $StartingBiblionumber;
57     }
58     
59     if ( $EndingBiblionumber ) {
60         $query .= " AND biblioitems.biblionumber <= ? ";
61         push @sql_params, $EndingBiblionumber;    
62     }
63     
64     if ( $branch ) {
65         $query .= " AND biblioitems.biblionumber = items.biblionumber AND homebranch = ? ";
66         push @sql_params, $branch;
67     }
68     
69     if ( $start_callnumber ) {
70         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber <= ? ";
71         push @sql_params, $start_callnumber;
72     }
73     
74     if ( $end_callnumber ) {
75         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber >= ? ";
76         push @sql_params, $end_callnumber;
77     }
78     
79     if ( $itemtype ) {
80         $query .= " AND biblioitems.itemtype = ?";
81         push @sql_params, $itemtype;
82     }
83
84     my $sth = $dbh->prepare($query);
85     $sth->execute(@sql_params);
86     
87     while (my ($biblionumber) = $sth->fetchrow) {
88         my $record = GetMarcBiblio($biblionumber);
89         if ( $dont_export_items ) {
90             # now, find where the itemnumber is stored & extract only the item
91             my ( $itemnumberfield, $itemnumbersubfield ) =
92                 MARCfind_marc_from_kohafield( $dbh, 'items.itemnumber', '' );
93
94             # and delete it.
95             foreach ($record->field($itemnumberfield)){
96                 $record->delete_field($record->field($itemnumberfield));
97             }
98         }
99         
100         if ( $dont_export_fields ) {
101             my @fields = split " ", $dont_export_fields;
102             foreach ( @fields ) {
103                 /^(\d*)(\w)?$/;
104                 my $field = $1;
105                 my $subfield = $2;
106                 if( $subfield ) {
107                     $record->field($field)->delete_subfields($subfield);
108                 }
109                 else {
110                     $record->delete_field($record->field($field));
111                 }
112             }
113         }
114         if ( $output_format eq "xml" ) {
115             print $record->as_xml_record($marcflavour);
116         }
117         else {
118             print $record->as_formatted; 
119         }
120     }
121     exit;
122     
123 } # if export
124
125 else {
126
127     my $itemtypes = GetItemTypes;
128     my @itemtypesloop;
129     foreach my $thisitemtype (sort keys %$itemtypes) {
130         my %row =
131             (
132                 value => $thisitemtype,
133                 description => $itemtypes->{$thisitemtype}->{'description'},
134             );
135        push @itemtypesloop, \%row;
136     }
137     
138     my $branches = GetBranches;
139     my $branch   = GetBranch($query,$branches);
140     my @branchloop;
141     foreach my $thisbranch (keys %$branches) {
142         my $selected = 1 if $thisbranch eq $branch;
143         my %row = (
144             value => $thisbranch,
145             selected => $selected,
146             branchname => $branches->{$thisbranch}->{'branchname'},
147        );
148        push @branchloop, \%row;
149     }
150     
151     my ($template, $loggedinuser, $cookie)
152     = get_template_and_user
153     (
154         {
155             template_name => "tools/export.tmpl",
156             query => $query,
157             type => "intranet",
158             authnotrequired => 0,
159             flagsrequired => {tools => 1},
160             debug => 1,
161          }
162     );
163     
164     $template->param(
165         branchloop   => \@branchloop,
166         itemtypeloop => \@itemtypesloop
167     );
168     
169     output_html_with_http_headers $query, $cookie, $template->output;
170 }