Adding tools directory template and scripts
[koha.git] / tools / export.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('Content-Type: text/marc');
19         my $start_bib = $query->param("start_bib");
20         my $end_bib = $query->param("end_bib");
21         my $format = $query->param("format");
22         my $branch = $query->param("branch");
23         my $start_callnumber = $query->param("start_callnumber");
24         my $end_callnumber = $query->param("end_callnumber");
25         my $limit = $query->param("limit");
26         my $strsth;
27         $strsth="select bibid from marc_biblio ";
28         if ($start_bib && $end_bib) {
29                 $strsth.=" where biblionumber>=$start_bib and biblionumber<=$end_bib ";
30         }elsif ($format) {
31                 if ($strsth=~/ where/){
32                         $strsth=~s/ where (.*)/,biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\' and $1/;
33                 }else {
34                         $strsth.=",biblioitems where biblioitems.biblionumber=marc_biblio.biblionumber and biblioitems.itemtype=\'$format\'";
35                 }
36         } elsif ($branch) {
37                 if ($strsth=~/ where/){
38                         $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\' and $1/;
39                 }else {
40                         $strsth.=",items where items.biblionumber=marc_biblio.biblionumber and items.homebranch=\'$branch\'";
41                 }
42         } elsif ($start_callnumber && $end_callnumber) {
43                 $start_callnumber=~s/\*/\%/g;
44                 $start_callnumber=~s/\?/\_/g;
45                 $end_callnumber=~s/\*/\%/g;
46                 $end_callnumber=~s/\?/\_/g;
47                 if ($strsth=~/,items/){
48                         $strsth.=" and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\'";
49                 } else {
50                         if ($strsth=~/ where/){
51                                 $strsth=~s/ where (.*)/,items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' and $1/;
52                         }else {
53                                 $strsth=~",items where items.biblionumber=marc_biblio.biblionumber and items.itemcallnumber between \'$start_callnumber\' and \'$end_callnumber\' ";
54                         }
55                 }
56         }
57         $strsth.=" order by marc_biblio.biblionumber ";
58         $strsth.= "LIMIT 0,$limit " if ($limit);
59         warn "requĂȘte marc.pl : ".$strsth;
60         my $req=$dbh->prepare($strsth);
61         $req->execute;
62         while (my ($bibid) = $req->fetchrow) {
63                 my $record = MARCgetbiblio($dbh,$bibid);
64
65                 print $record->as_usmarc();
66         }
67 } else {
68         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
69         $sth->execute;
70         my  @itemtype;
71         my %itemtypes;
72         push @itemtype, "";
73         $itemtypes{''} = "";
74         while (my ($value,$lib) = $sth->fetchrow_array) {
75                         push @itemtype, $value;
76                         $itemtypes{$value}=$lib;
77         }
78         
79         my $CGIitemtype=CGI::scrolling_list( -name     => 'format',
80                                                         -values   => \@itemtype,
81                                                         -default  => '',
82                                                         -labels   => \%itemtypes,
83                                                         -size     => 1,
84                                                         -multiple => 0 );
85         $sth->finish;
86         
87         my $branches = getallbranches;
88         my @branchloop;
89         foreach my $thisbranch (keys %$branches) {
90 #                       my $selected = 1 if $thisbranch eq $branch;
91                         my %row =(value => $thisbranch,
92 #                                                                       selected => $selected,
93                                                                         branchname => $branches->{$thisbranch}->{'branchname'},
94                                                         );
95                         push @branchloop, \%row;
96         }
97         
98         my ($template, $loggedinuser, $cookie)
99         = get_template_and_user({template_name => "tools/export.tmpl",
100                                         query => $query,
101                                         type => "intranet",
102                                         authnotrequired => 0,
103                                         flagsrequired => {parameters => 1, management => 1, tools => 1},
104                                         debug => 1,
105                                         });
106         $template->param(branchloop=>\@branchloop,CGIitemtype=>$CGIitemtype);
107         output_html_with_http_headers $query, $cookie, $template->output;
108 }
109