Independent-branchify MARC export. Add option to remove all holdings except your...
[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         my $limit_ind_branch=(C4::Context->preference('IndependantBranches') &&
49               C4::Context->userenv &&
50               C4::Context->userenv->{flags} !=1  &&
51               C4::Context->userenv->{branch}?1:0);
52         my $branches = GetBranches($limit_ind_branch);    
53     my $branch                = $query->param("branch");
54         if ( C4::Context->preference("IndependantBranches") ) {
55         $branch = C4::Context->userenv->{'branch'};
56         }
57
58 if ($op eq "export") {
59     binmode(STDOUT,":utf8");
60         print $query->header(   -type => 'application/octet-stream', 
61                             -charset => 'utf-8',
62                             -attachment=>$filename);
63      
64     my $StartingBiblionumber  = $query->param("StartingBiblionumber");
65     my $EndingBiblionumber    = $query->param("EndingBiblionumber");
66     my $output_format         = $query->param("output_format");
67     my $itemtype              = $query->param("itemtype");
68     my $start_callnumber      = $query->param("start_callnumber");
69     my $end_callnumber        = $query->param("end_callnumber");
70     my $start_accession      = ($query->param("start_accession")) ? C4::Dates->new($query->param("start_accession")) : '' ;
71     my $end_accession        = ($query->param("end_accession")) ? C4::Dates->new($query->param("end_accession")) : '' ;
72     my $dont_export_items     = $query->param("dont_export_item");
73     my $strip_nonlocal_items   = $query->param("strip_nonlocal_items");
74     my $dont_export_fields    = $query->param("dont_export_fields");
75     my @sql_params;
76     my $query = " SELECT DISTINCT biblioitems.biblionumber
77                   FROM biblioitems,items
78                   WHERE biblioitems.biblionumber=items.biblionumber ";
79                   
80     if ( $StartingBiblionumber ) {
81         $query .= " AND biblioitems.biblionumber >= ? ";
82         push @sql_params, $StartingBiblionumber;
83     }
84     
85     if ( $EndingBiblionumber ) {
86         $query .= " AND biblioitems.biblionumber <= ? ";
87         push @sql_params, $EndingBiblionumber;    
88     }
89     
90     if ( $branch ) {
91         $query .= " AND biblioitems.biblionumber = items.biblionumber AND homebranch = ? ";
92         push @sql_params, $branch;
93     }
94     
95     if ( $start_callnumber ) {
96         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber <= ? ";
97         push @sql_params, $start_callnumber;
98     }
99     
100     if ( $end_callnumber ) {
101         $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber >= ? ";
102         push @sql_params, $end_callnumber;
103     }
104     if ( $start_accession ) {
105         $query .= " AND biblioitems.biblionumber = items.biblionumber AND dateaccessioned >= ? ";
106         push @sql_params,$start_accession->output('iso');
107     }
108     
109     if ( $end_accession ) {
110         $query .= " AND biblioitems.biblionumber = items.biblionumber AND dateaccessioned <= ? ";
111         push @sql_params, $end_accession->output('iso');
112     }
113     
114     if ( $itemtype ) {
115         $query .= " AND biblioitems.itemtype = ?";
116         push @sql_params, $itemtype;
117     }
118     my $sth = $dbh->prepare($query);
119     $sth->execute(@sql_params);
120     
121     while (my ($biblionumber) = $sth->fetchrow) {
122         my $record = GetMarcBiblio($biblionumber);
123         my ( $homebranchfield, $homebranchsubfield ) =  GetMarcFromKohaField( 'items.homebranch', '' );
124         if ( $dont_export_items || $strip_nonlocal_items || $limit_ind_branch) {
125                         for my $itemfield ($record->field($homebranchfield)){
126                                 # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
127                                 $branch = C4::Context->userenv->{'branch'} unless $branch;
128                 $record->delete_field($itemfield) if($dont_export_items || ($itemfield->subfield($homebranchsubfield) ne $branch) ) ;
129             }
130         }
131         
132         if ( $dont_export_fields ) {
133             my @fields = split " ", $dont_export_fields;
134             foreach ( @fields ) {
135                 /^(\d*)(\w)?$/;
136                 my $field = $1;
137                 my $subfield = $2;
138                 if( $subfield ) {
139                     $record->field($field)->delete_subfields($subfield);
140                 }
141                 else {
142                     $record->delete_field($record->field($field));
143                 }
144             }
145         }
146         if ( $output_format eq "xml" ) {
147             print $record->as_xml_record($marcflavour);
148         }
149         else {
150             print $record->as_usmarc(); 
151         }
152     }
153     exit;
154     
155 } # if export
156
157 else {
158
159     my $itemtypes = GetItemTypes;
160     my @itemtypesloop;
161     foreach my $thisitemtype (sort keys %$itemtypes) {
162         my %row =
163             (
164                 value => $thisitemtype,
165                 description => $itemtypes->{$thisitemtype}->{'description'},
166             );
167        push @itemtypesloop, \%row;
168     }
169     my @branchloop;
170         for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
171         my $selected = 1 if $thisbranch eq $branch;
172         my %row = (
173             value => $thisbranch,
174             selected => $selected,
175             branchname => $branches->{$thisbranch}->{'branchname'},
176        );
177        push @branchloop, \%row;
178     }
179     
180     $template->param(
181         branchloop   => \@branchloop,
182         itemtypeloop => \@itemtypesloop,
183                 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
184     );
185     
186     output_html_with_http_headers $query, $cookie, $template->output;
187 }