Bug 5327 added unit tests for C4/Scheduler.pm
[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 use warnings;
22 use C4::Auth;
23 use C4::Output;
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} % 2 !=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     
77     my $items_filter =
78         $branch || $start_callnumber || $end_callnumber ||  
79         $start_accession || $end_accession || 
80         ($itemtype && C4::Context->preference('item-level_itypes'));
81     my $query = $items_filter ?
82         "SELECT DISTINCT biblioitems.biblionumber
83          FROM biblioitems JOIN items
84          USING (biblionumber) WHERE 1"
85         :
86         "SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ";
87                   
88     if ( $StartingBiblionumber ) {
89         $query .= " AND biblioitems.biblionumber >= ? ";
90         push @sql_params, $StartingBiblionumber;
91     }
92     
93     if ( $EndingBiblionumber ) {
94         $query .= " AND biblioitems.biblionumber <= ? ";
95         push @sql_params, $EndingBiblionumber;    
96     }
97
98     if ($branch) {
99         $query .= " AND homebranch = ? ";
100         push @sql_params, $branch;
101     }
102
103     if ($start_callnumber) {
104         $query .= " AND itemcallnumber <= ? ";
105         push @sql_params, $start_callnumber;
106     }
107
108     if ($end_callnumber) {
109         $query .= " AND itemcallnumber >= ? ";
110         push @sql_params, $end_callnumber;
111     }
112     if ($start_accession) {
113         $query .= " AND dateaccessioned >= ? ";
114         push @sql_params, $start_accession->output('iso');
115     }
116
117     if ($end_accession) {
118         $query .= " AND dateaccessioned <= ? ";
119         push @sql_params, $end_accession->output('iso');
120     }
121     
122     if ( $itemtype ) {
123         $query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?";
124         push @sql_params, $itemtype;
125     }
126     my $sth = $dbh->prepare($query);
127     $sth->execute(@sql_params);
128     
129     while (my ($biblionumber) = $sth->fetchrow) {
130         my $record = eval{ GetMarcBiblio($biblionumber); };
131         # FIXME: decide how to handle records GetMarcBiblio can't parse or retrieve
132         if ($@) {
133             next;
134         }
135         next if not defined $record;
136         C4::Biblio::EmbedItemsInMarcBiblio($record, $biblionumber) unless $dont_export_items;
137         if ($strip_nonlocal_items || $limit_ind_branch) {
138             my ( $homebranchfield, $homebranchsubfield ) =
139                 GetMarcFromKohaField( 'items.homebranch', '' );
140                         for my $itemfield ($record->field($homebranchfield)){
141                                 # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
142                                 $branch = C4::Context->userenv->{'branch'} unless $branch;
143                 $record->delete_field($itemfield) if($itemfield->subfield($homebranchsubfield) ne $branch) ;
144             }
145         }
146         
147         if ( $dont_export_fields ) {
148             my @fields = split " ", $dont_export_fields;
149             foreach ( @fields ) {
150                 /^(\d*)(\w)?$/;
151                 my $field = $1;
152                 my $subfield = $2;
153                 # skip if this record doesn't have this field
154                 next if not defined $record->field($field);
155                 if( $subfield ) {
156                     $record->field($field)->delete_subfields($subfield);
157                 }
158                 else {
159                     $record->delete_field($record->field($field));
160                 }
161             }
162         }
163         if ( $output_format eq "xml" ) {
164             print $record->as_xml_record($marcflavour);
165         }
166         else {
167             print $record->as_usmarc(); 
168         }
169     }
170     exit;
171     
172 } # if export
173
174 else {
175
176     my $itemtypes = GetItemTypes;
177     my @itemtypesloop;
178     foreach my $thisitemtype (sort keys %$itemtypes) {
179         my %row =
180             (
181                 value => $thisitemtype,
182                 description => $itemtypes->{$thisitemtype}->{'description'},
183             );
184        push @itemtypesloop, \%row;
185     }
186     my @branchloop;
187     for my $thisbranch (
188         sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
189         keys %{$branches}
190       ) {
191         push @branchloop,
192           { value      => $thisbranch,
193             selected   => $thisbranch eq $branch,
194             branchname => $branches->{$thisbranch}->{'branchname'},
195           };
196     }
197
198     $template->param(
199         branchloop   => \@branchloop,
200         itemtypeloop => \@itemtypesloop,
201                 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
202     );
203     
204     output_html_with_http_headers $query, $cookie, $template->output;
205 }