Small template cleanup: display library name and cat description instead of codes
[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 my ($template, $loggedinuser, $cookie)
36     = get_template_and_user
37     (
38         {
39             template_name => "tools/export.tmpl",
40             query => $query,
41             type => "intranet",
42             authnotrequired => 0,
43             flagsrequired => {tools => 'export_catalog'},
44             debug => 1,
45             }
46     );
47
48 if ($op eq "export") {
49     binmode(STDOUT,":utf8");
50         print $query->header(   -type => 'application/octet-stream', 
51                             -charset => 'utf-8',
52                             -attachment=>$filename);
53      
54     my $StartingBiblionumber  = $query->param("StartingBiblionumber");
55     my $EndingBiblionumber    = $query->param("EndingBiblionumber");
56     my $output_format         = $query->param("output_format");
57     my $branch                = $query->param("branch");
58     my $itemtype              = $query->param("itemtype");
59     my $start_callnumber      = $query->param("start_callnumber");
60     my $end_callnumber        = $query->param("end_callnumber");
61     my $dont_export_items     = $query->param("dont_export_item");
62     my $dont_export_fields    = $query->param("dont_export_fields");
63     my @sql_params;
64     my $query = " SELECT DISTINCT biblioitems.biblionumber
65                   FROM biblioitems,items
66                   WHERE biblioitems.biblionumber=items.biblionumber ";
67                   
68     if ( $StartingBiblionumber ) {
69         $query .= " AND biblioitems.biblionumber >= ? ";
70         push @sql_params, $StartingBiblionumber;
71     }
72     
73     if ( $EndingBiblionumber ) {
74         $query .= " AND biblioitems.biblionumber <= ? ";
75         push @sql_params, $EndingBiblionumber;    
76     }
77     
78     if ( $branch ) {
79         $query .= " AND biblioitems.biblionumber = items.biblionumber AND homebranch = ? ";
80         push @sql_params, $branch;
81     }
82     
83     if ( $start_callnumber ) {
84         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber <= ? ";
85         push @sql_params, $start_callnumber;
86     }
87     
88     if ( $end_callnumber ) {
89         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber >= ? ";
90         push @sql_params, $end_callnumber;
91     }
92     
93     if ( $itemtype ) {
94         $query .= " AND biblioitems.itemtype = ?";
95         push @sql_params, $itemtype;
96     }
97
98     my $sth = $dbh->prepare($query);
99     $sth->execute(@sql_params);
100     
101     while (my ($biblionumber) = $sth->fetchrow) {
102         my $record = GetMarcBiblio($biblionumber);
103         if ( $dont_export_items ) {
104             # now, find where the itemnumber is stored & extract only the item
105             my ( $itemnumberfield, $itemnumbersubfield ) =
106                 GetMarcFromKohaField( 'items.itemnumber', '' );
107
108             # and delete it.
109             foreach ($record->field($itemnumberfield)){
110                 $record->delete_field($record->field($itemnumberfield));
111             }
112         }
113         
114         if ( $dont_export_fields ) {
115             my @fields = split " ", $dont_export_fields;
116             foreach ( @fields ) {
117                 /^(\d*)(\w)?$/;
118                 my $field = $1;
119                 my $subfield = $2;
120                 if( $subfield ) {
121                     $record->field($field)->delete_subfields($subfield);
122                 }
123                 else {
124                     $record->delete_field($record->field($field));
125                 }
126             }
127         }
128         if ( $output_format eq "xml" ) {
129             print $record->as_xml_record($marcflavour);
130         }
131         else {
132             print $record->as_usmarc(); 
133         }
134     }
135     exit;
136     
137 } # if export
138
139 else {
140
141     my $itemtypes = GetItemTypes;
142     my @itemtypesloop;
143     foreach my $thisitemtype (sort keys %$itemtypes) {
144         my %row =
145             (
146                 value => $thisitemtype,
147                 description => $itemtypes->{$thisitemtype}->{'description'},
148             );
149        push @itemtypesloop, \%row;
150     }
151     
152     my $branches = GetBranches;
153     my $branch   = GetBranch($query,$branches);
154     my @branchloop;
155     foreach my $thisbranch (keys %$branches) {
156         my $selected = 1 if $thisbranch eq $branch;
157         my %row = (
158             value => $thisbranch,
159             selected => $selected,
160             branchname => $branches->{$thisbranch}->{'branchname'},
161        );
162        push @branchloop, \%row;
163     }
164     
165     $template->param(
166         branchloop   => \@branchloop,
167         itemtypeloop => \@itemtypesloop
168     );
169     
170     output_html_with_http_headers $query, $cookie, $template->output;
171 }