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