Authorities now use same structure as biblios
[koha.git] / export / marc.pl
1 #!/usr/bin/perl
2 use HTML::Template;
3 use strict;
4 require Exporter;
5 use C4::Database;
6 use C4::Auth;
7 use C4::Interface::CGI::Output;
8 use C4::Output;  # contains gettemplate
9 use C4::Biblio;
10 use CGI;
11 use C4::Koha;
12
13 my $query = new CGI;
14 my $op=$query->param("op");
15 my $dbh=C4::Context->dbh;
16
17 if ($op eq "export") {
18         print $query->header(   -type => 'application/octet-stream',
19                                 -attachment=>'koha.mrc');
20         my $start_bib = $query->param("start_bib");
21         my $end_bib = $query->param("end_bib");
22         my $format = $query->param("format");
23         my $branch = $query->param("branch");
24         my $start_callnumber = $query->param("start_callnumber");
25         my $end_callnumber = $query->param("end_callnumber");
26         my $limit = $query->param("limit");
27         my $strsth;
28         $strsth="select bibid from marc_biblio ";
29         if ($start_bib && $end_bib) {
30                 $strsth.=" where biblionumber>=$start_bib and biblionumber<=$end_bib ";
31         }elsif ($format) {
32                 if ($strsth=~/ where/){
33                         $strsth=~s/ where (.*)/,biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\' and $1/;
34                 }else {
35                         $strsth.=",biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\'";
36                 }
37         } elsif ($branch) {
38                 if ($strsth=~/ where/){
39                         $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\' and $1/;
40                 }else {
41                         $strsth.=",items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\'";
42                 }
43         } elsif ($start_callnumber && $end_callnumber) {
44                 $start_callnumber=~s/\*/\%/g;
45                 $start_callnumber=~s/\?/\_/g;
46                 $end_callnumber=~s/\*/\%/g;
47                 $end_callnumber=~s/\?/\_/g;
48                 if ($strsth=~/,items/){
49                         $strsth.=" and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\'";
50                 } else {
51                         if ($strsth=~/ where/){
52                                 $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' and $1/;
53                         }else {
54                                 $strsth=~",items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' ";
55                         }
56                 }
57         }
58         $strsth.=" order by marc_biblio.biblionumber ";
59         $strsth.= "LIMIT 0,$limit " if ($limit);
60         warn "requĂȘte marc.pl : ".$strsth;
61         my $req=$dbh->prepare($strsth);
62         $req->execute;
63         while (my ($bibid) = $req->fetchrow) {
64                 my $record = MARCgetbiblio($dbh,$bibid);
65
66                 print $record->as_usmarc();
67         }
68 } else {
69         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
70         $sth->execute;
71         my  @itemtype;
72         my %itemtypes;
73         push @itemtype, "";
74         $itemtypes{''} = "";
75         while (my ($value,$lib) = $sth->fetchrow_array) {
76                         push @itemtype, $value;
77                         $itemtypes{$value}=$lib;
78         }
79         
80         my $CGIitemtype=CGI::scrolling_list( -name     => 'format',
81                                                         -values   => \@itemtype,
82                                                         -default  => '',
83                                                         -labels   => \%itemtypes,
84                                                         -size     => 1,
85                                                         -tabindex=>'',
86                                                         -multiple => 0 );
87         $sth->finish;
88         
89         my $branches = getallbranches;
90         my @branchloop;
91         foreach my $thisbranch (keys %$branches) {
92 #                       my $selected = 1 if $thisbranch eq $branch;
93                         my %row =(value => $thisbranch,
94 #                                                                       selected => $selected,
95                                                                         branchname => $branches->{$thisbranch}->{'branchname'},
96                                                         );
97                         push @branchloop, \%row;
98         }
99         
100         my ($template, $loggedinuser, $cookie)
101         = get_template_and_user({template_name => "export/marc.tmpl",
102                                         query => $query,
103                                         type => "intranet",
104                                         authnotrequired => 0,
105                                         flagsrequired => {parameters => 1, management => 1, tools => 1},
106                                         debug => 1,
107                                         });
108         $template->param(branchloop=>\@branchloop,
109                         CGIitemtype=>$CGIitemtype,
110                         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
111                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
112                 IntranetNav => C4::Context->preference("IntranetNav"),
113                         );
114         output_html_with_http_headers $query, $cookie, $template->output;
115 }
116