functions that were in C4::Interface::CGI::Output are now in C4::Output.
[koha.git] / export / marc.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 use C4::Branch; # GetBranches
23 use strict;
24 require Exporter;
25
26 use C4::Auth;
27 use C4::Output;  # contains gettemplate
28 use C4::Biblio;
29 use CGI;
30 use C4::Koha;
31
32 my $query = new CGI;
33 my $op=$query->param("op");
34 my $dbh=C4::Context->dbh;
35
36 if ($op eq "export") {
37     print $query->header(    -type => 'application/octet-stream',
38                 -attachment=>'koha.mrc');
39     my $StartingBiblionumber = $query->param("StartingBiblionumber");
40     my $EndingBiblionumber = $query->param("EndingBiblionumber");
41     my $format = $query->param("format");
42     my $branch = $query->param("branch");
43     my $start_callnumber = $query->param("start_callnumber");
44     my $end_callnumber = $query->param("end_callnumber");
45     my $limit = $query->param("limit");
46     my $strsth;
47     $strsth="select bibid from marc_biblio ";
48     if ($StartingBiblionumber && $EndingBiblionumber) {
49         $strsth.=" where biblionumber>=$StartingBiblionumber and biblionumber<=$EndingBiblionumber ";
50     }elsif ($format) {
51         if ($strsth=~/ where/){
52             $strsth=~s/ where (.*)/,biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\' and $1/;
53         }else {
54             $strsth.=",biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\'";
55         }
56     } elsif ($branch) {
57         if ($strsth=~/ where/){
58             $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\' and $1/;
59         }else {
60             $strsth.=",items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\'";
61         }
62     } elsif ($start_callnumber && $end_callnumber) {
63         $start_callnumber=~s/\*/\%/g;
64         $start_callnumber=~s/\?/\_/g;
65         $end_callnumber=~s/\*/\%/g;
66         $end_callnumber=~s/\?/\_/g;
67         if ($strsth=~/,items/){
68             $strsth.=" and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\'";
69         } else {
70             if ($strsth=~/ where/){
71                 $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' and $1/;
72             }else {
73                 $strsth=~",items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' ";
74             }
75         }
76     }
77     $strsth.=" order by marc_biblio.biblionumber ";
78     $strsth.= "LIMIT 0,$limit " if ($limit);
79     warn "requete marc.pl : ".$strsth;
80     my $req=$dbh->prepare($strsth);
81     $req->execute;
82     while (my ($bibid) = $req->fetchrow) {
83         my $record = GetMarcBiblio($bibid);
84
85         print $record->as_usmarc();
86     }
87 } else {
88     my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
89     $sth->execute;
90     my  @itemtype;
91     my %itemtypes;
92     push @itemtype, "";
93     $itemtypes{''} = "";
94     while (my ($value,$lib) = $sth->fetchrow_array) {
95             push @itemtype, $value;
96             $itemtypes{$value}=$lib;
97     }
98     
99     my $CGIitemtype=CGI::scrolling_list( -name     => 'format',
100                             -values   => \@itemtype,
101                             -default  => '',
102                             -labels   => \%itemtypes,
103                             -size     => 1,
104                              -tabindex=>'',
105                             -multiple => 0 );
106     $sth->finish;
107     
108     my $branches = GetBranches;
109     my @branchloop;
110     foreach my $thisbranch (keys %$branches) {
111 #             my $selected = 1 if $thisbranch eq $branch;
112             my %row =(value => $thisbranch,
113 #                                     selected => $selected,
114                                     branchname => $branches->{$thisbranch}->{'branchname'},
115                             );
116             push @branchloop, \%row;
117     }
118     
119     my ($template, $loggedinuser, $cookie)
120     = get_template_and_user({template_name => "export/marc.tmpl",
121                     query => $query,
122                     type => "intranet",
123                     authnotrequired => 0,
124                     flagsrequired => {tools => 1},
125                     debug => 1,
126                     });
127     $template->param(branchloop=>\@branchloop,
128             CGIitemtype=>$CGIitemtype,
129             intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
130         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
131         IntranetNav => C4::Context->preference("IntranetNav"),
132             );
133     output_html_with_http_headers $query, $cookie, $template->output;
134 }
135