fixing permissions on mainpage
[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::Interface::CGI::Output;
28 use C4::Output;  # contains gettemplate
29 use C4::Biblio;
30 use CGI;
31 use C4::Koha;
32
33 my $query = new CGI;
34 my $op=$query->param("op");
35 my $dbh=C4::Context->dbh;
36
37 if ($op eq "export") {
38     print $query->header(    -type => 'application/octet-stream',
39                 -attachment=>'koha.mrc');
40     my $StartingBiblionumber = $query->param("StartingBiblionumber");
41     my $EndingBiblionumber = $query->param("EndingBiblionumber");
42     my $format = $query->param("format");
43     my $branch = $query->param("branch");
44     my $start_callnumber = $query->param("start_callnumber");
45     my $end_callnumber = $query->param("end_callnumber");
46     my $limit = $query->param("limit");
47     my $strsth;
48     $strsth="select bibid from marc_biblio ";
49     if ($StartingBiblionumber && $EndingBiblionumber) {
50         $strsth.=" where biblionumber>=$StartingBiblionumber and biblionumber<=$EndingBiblionumber ";
51     }elsif ($format) {
52         if ($strsth=~/ where/){
53             $strsth=~s/ where (.*)/,biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\' and $1/;
54         }else {
55             $strsth.=",biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\'";
56         }
57     } elsif ($branch) {
58         if ($strsth=~/ where/){
59             $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\' and $1/;
60         }else {
61             $strsth.=",items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\'";
62         }
63     } elsif ($start_callnumber && $end_callnumber) {
64         $start_callnumber=~s/\*/\%/g;
65         $start_callnumber=~s/\?/\_/g;
66         $end_callnumber=~s/\*/\%/g;
67         $end_callnumber=~s/\?/\_/g;
68         if ($strsth=~/,items/){
69             $strsth.=" and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\'";
70         } else {
71             if ($strsth=~/ where/){
72                 $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' and $1/;
73             }else {
74                 $strsth=~",items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' ";
75             }
76         }
77     }
78     $strsth.=" order by marc_biblio.biblionumber ";
79     $strsth.= "LIMIT 0,$limit " if ($limit);
80     warn "requete marc.pl : ".$strsth;
81     my $req=$dbh->prepare($strsth);
82     $req->execute;
83     while (my ($bibid) = $req->fetchrow) {
84         my $record = GetMarcBiblio($bibid);
85
86         print $record->as_usmarc();
87     }
88 } else {
89     my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
90     $sth->execute;
91     my  @itemtype;
92     my %itemtypes;
93     push @itemtype, "";
94     $itemtypes{''} = "";
95     while (my ($value,$lib) = $sth->fetchrow_array) {
96             push @itemtype, $value;
97             $itemtypes{$value}=$lib;
98     }
99     
100     my $CGIitemtype=CGI::scrolling_list( -name     => 'format',
101                             -values   => \@itemtype,
102                             -default  => '',
103                             -labels   => \%itemtypes,
104                             -size     => 1,
105                              -tabindex=>'',
106                             -multiple => 0 );
107     $sth->finish;
108     
109     my $branches = GetBranches;
110     my @branchloop;
111     foreach my $thisbranch (keys %$branches) {
112 #             my $selected = 1 if $thisbranch eq $branch;
113             my %row =(value => $thisbranch,
114 #                                     selected => $selected,
115                                     branchname => $branches->{$thisbranch}->{'branchname'},
116                             );
117             push @branchloop, \%row;
118     }
119     
120     my ($template, $loggedinuser, $cookie)
121     = get_template_and_user({template_name => "export/marc.tmpl",
122                     query => $query,
123                     type => "intranet",
124                     authnotrequired => 0,
125                     flagsrequired => {tools => 1},
126                     debug => 1,
127                     });
128     $template->param(branchloop=>\@branchloop,
129             CGIitemtype=>$CGIitemtype,
130             intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
131         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
132         IntranetNav => C4::Context->preference("IntranetNav"),
133             );
134     output_html_with_http_headers $query, $cookie, $template->output;
135 }
136