4 # This file is part of Koha.
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
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.
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
23 use C4::Output; # contains gettemplate
24 use C4::Biblio; # GetMarcBiblio GetXmlBiblio
26 use C4::Koha; # GetItemTypes
27 use C4::Branch; # GetBranches
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");
35 my ($template, $loggedinuser, $cookie)
36 = get_template_and_user
39 template_name => "tools/export.tmpl",
43 flagsrequired => {tools => 'export_catalog'},
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");
55 if ($op eq "export") {
56 binmode(STDOUT,":utf8");
57 print $query->header( -type => 'application/octet-stream',
59 -attachment=>$filename);
61 my $StartingBiblionumber = $query->param("StartingBiblionumber");
62 my $EndingBiblionumber = $query->param("EndingBiblionumber");
63 my $output_format = $query->param("output_format");
64 my $itemtype = $query->param("itemtype");
65 my $start_callnumber = $query->param("start_callnumber");
66 my $end_callnumber = $query->param("end_callnumber");
67 my $start_accession = ($query->param("start_accession")) ? C4::Dates->new($query->param("start_accession")) : '' ;
68 my $end_accession = ($query->param("end_accession")) ? C4::Dates->new($query->param("end_accession")) : '' ;
69 my $dont_export_items = $query->param("dont_export_item");
70 my $strip_nonlocal_items = $query->param("strip_nonlocal_items");
71 my $dont_export_fields = $query->param("dont_export_fields");
75 $branch || $start_callnumber || $end_callnumber ||
76 $start_accession || $end_accession ||
77 ($itemtype && C4::Context->preference('item-level_itypes'));
78 my $query = $items_filter ?
79 "SELECT DISTINCT biblioitems.biblionumber
80 FROM biblioitems JOIN items
81 USING (biblionumber) WHERE 1"
83 "SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ";
85 if ( $StartingBiblionumber ) {
86 $query .= " AND biblioitems.biblionumber >= ? ";
87 push @sql_params, $StartingBiblionumber;
90 if ( $EndingBiblionumber ) {
91 $query .= " AND biblioitems.biblionumber <= ? ";
92 push @sql_params, $EndingBiblionumber;
96 $query .= " AND biblioitems.biblionumber = items.biblionumber AND homebranch = ? ";
97 push @sql_params, $branch;
100 if ( $start_callnumber ) {
101 $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber <= ? ";
102 push @sql_params, $start_callnumber;
105 if ( $end_callnumber ) {
106 $query .= " AND biblioitems.biblionumber = items.biblionumber AND itemcallnumber >= ? ";
107 push @sql_params, $end_callnumber;
109 if ( $start_accession ) {
110 $query .= " AND biblioitems.biblionumber = items.biblionumber AND dateaccessioned >= ? ";
111 push @sql_params,$start_accession->output('iso');
114 if ( $end_accession ) {
115 $query .= " AND biblioitems.biblionumber = items.biblionumber AND dateaccessioned <= ? ";
116 push @sql_params, $end_accession->output('iso');
120 $query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?";
121 push @sql_params, $itemtype;
123 warn "$query, @sql_params";
124 my $sth = $dbh->prepare($query);
125 $sth->execute(@sql_params);
127 while (my ($biblionumber) = $sth->fetchrow) {
128 my $record = GetMarcBiblio($biblionumber);
129 next if not defined $record;
130 if ( $dont_export_items || $strip_nonlocal_items || $limit_ind_branch) {
131 my ( $homebranchfield, $homebranchsubfield ) =
132 GetMarcFromKohaField( 'items.homebranch', '' );
133 for my $itemfield ($record->field($homebranchfield)){
134 # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
135 $branch = C4::Context->userenv->{'branch'} unless $branch;
136 $record->delete_field($itemfield) if($dont_export_items || ($itemfield->subfield($homebranchsubfield) ne $branch) ) ;
140 if ( $dont_export_fields ) {
141 my @fields = split " ", $dont_export_fields;
142 foreach ( @fields ) {
146 # skip if this record doesn't have this field
147 next if not defined $record->field($field);
149 $record->field($field)->delete_subfields($subfield);
152 $record->delete_field($record->field($field));
156 if ( $output_format eq "xml" ) {
157 print $record->as_xml_record($marcflavour);
160 print $record->as_usmarc();
169 my $itemtypes = GetItemTypes;
171 foreach my $thisitemtype (sort keys %$itemtypes) {
174 value => $thisitemtype,
175 description => $itemtypes->{$thisitemtype}->{'description'},
177 push @itemtypesloop, \%row;
180 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
181 my $selected = 1 if $thisbranch eq $branch;
183 value => $thisbranch,
184 selected => $selected,
185 branchname => $branches->{$thisbranch}->{'branchname'},
187 push @branchloop, \%row;
191 branchloop => \@branchloop,
192 itemtypeloop => \@itemtypesloop,
193 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
196 output_html_with_http_headers $query, $cookie, $template->output;