Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 219 lines (198 sloc) 8.334 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#!/usr/bin/env perl

#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA


use strict;
use warnings;
use C4::Auth;
use C4::Output; # contains gettemplate
use C4::Biblio; # GetMarcBiblio GetXmlBiblio
use C4::Items;
use CGI;
use C4::Koha; # GetItemTypes
use C4::Branch; # GetBranches

my $query = new CGI;
my $op=$query->param("op") || '';
my $filename=$query->param("filename");
my $dbh=C4::Context->dbh;
my $marcflavour = C4::Context->preference("marcflavour");

my ($template, $loggedinuser, $cookie)
    = get_template_and_user
    (
        {
            template_name => "tools/export.tmpl",
            query => $query,
            type => "intranet",
            authnotrequired => 0,
            flagsrequired => {tools => 'export_catalog'},
            debug => 1,
            }
    );

        my $limit_ind_branch=(C4::Context->preference('IndependantBranches') &&
              C4::Context->userenv &&
              C4::Context->userenv->{flags} % 2 !=1 &&
              C4::Context->userenv->{branch}?1:0);
        my $branches = GetBranches($limit_ind_branch);
        my $branch = $query->param("branch") || '';
        if ( C4::Context->preference("IndependantBranches") ) {
            $branch = C4::Context->userenv->{'branch'};
        }

if ($op eq "export") {
    binmode(STDOUT,":utf8");
        print $query->header( -type => 'application/octet-stream',
                            -charset => 'utf-8',
                            -attachment=>$filename);
     
    my $StartingBiblionumber = $query->param("StartingBiblionumber");
    my $EndingBiblionumber = $query->param("EndingBiblionumber");
    my $output_format = $query->param("output_format");
    my $itemtype = $query->param("itemtype");
    my $start_callnumber = $query->param("start_callnumber");
    my $end_callnumber = $query->param("end_callnumber");
    my $start_accession = ($query->param("start_accession")) ? C4::Dates->new($query->param("start_accession")) : '' ;
    my $end_accession = ($query->param("end_accession")) ? C4::Dates->new($query->param("end_accession")) : '' ;
    my $dont_export_items = $query->param("dont_export_item");
    my $strip_nonlocal_items = $query->param("strip_nonlocal_items");
    my $dont_export_fields = $query->param("dont_export_fields");
    my @sql_params;
    
    my $items_filter =
        $branch || $start_callnumber || $end_callnumber ||
        $start_accession || $end_accession ||
        ($itemtype && C4::Context->preference('item-level_itypes'));
    my $query = $items_filter ?
        "SELECT DISTINCT biblioitems.biblionumber
FROM biblioitems,items
WHERE biblioitems.biblionumber=items.biblionumber "
        :
        "SELECT biblioitems.biblionumber FROM biblioitems WHERE biblionumber >0 ";
                  
   if ( $StartingBiblionumber ) {
      if ($EndingBiblionumber) {
         $query .= ' AND biblioitems.biblionumber BETWEEN ? AND ? ';
         push @sql_params, $StartingBiblionumber, $EndingBiblionumber;
      }
      else {
         $query .= ' AND biblioitems.biblionumber >= ? ';
         push @sql_params, $StartingBiblionumber;
      }
   }
   elsif ( $EndingBiblionumber ) {
        $query .= " AND biblioitems.biblionumber <= ? ";
        push @sql_params, $EndingBiblionumber;
    }
    
    if ( $branch ) {
        $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.homebranch = ? ";
        push @sql_params, $branch;
    }
    
    if ( $start_callnumber ) {
       if ($end_callnumber) {
          $query .= ' AND biblioitems.biblionumber = items.biblionumber
AND items.itemcallnumber BETWEEN ? AND ? ';
          push @sql_params, $start_callnumber, $end_callnumber;
       }
       else {
        $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.itemcallnumber <= ? ";
         push @sql_params, $start_callnumber;
       }
    }
    elsif ( $end_callnumber ) {
        $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.itemcallnumber >= ? ";
        push @sql_params, $end_callnumber;
    }
    if ( $start_accession ) {
        $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.dateaccessioned >= ? ";
        push @sql_params,$start_accession->output('iso');
    }
    
    if ( $end_accession ) {
        $query .= " AND biblioitems.biblionumber = items.biblionumber AND items.dateaccessioned <= ? ";
        push @sql_params, $end_accession->output('iso');
    }
    
    if ( $itemtype ) {
        $query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?";
        push @sql_params, $itemtype;
    }
    my $sth = $dbh->prepare($query);
    $sth->execute(@sql_params);
    
    while (my ($biblionumber) = $sth->fetchrow) {
        my $record = eval{ ($dont_export_items)
            ? C4::Biblio::GetMarcBiblio($biblionumber)
            : C4::Items::GetMarcWithItems($biblionumber);
        };
        # FIXME: decide how to handle unparseable or irretrievable records
        if ($@) {
            warn "Error processing biblio $biblionumber";
            next;
        }
        next if not defined $record;
        if ( $strip_nonlocal_items || $limit_ind_branch) {
            my ( $homebranchfield, $homebranchsubfield ) =
                GetMarcFromKohaField( 'items.homebranch', '' );
                        for my $itemfield ($record->field($homebranchfield)){
                                # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
                                $branch = C4::Context->userenv->{'branch'} unless $branch;
                $record->delete_field($itemfield) if ($itemfield->subfield($homebranchsubfield) ne $branch);
            }
        }
        
        if ( $dont_export_fields ) {
            my @fields = split " ", $dont_export_fields;
            foreach ( @fields ) {
                /^(\d*)(\w)?$/;
                my $field = $1;
                my $subfield = $2;
                # skip if this record doesn't have this field
                next if not defined $record->field($field);
                if( $subfield ) {
                    $record->field($field)->delete_subfields($subfield);
                }
                else {
                    $record->delete_field($record->field($field));
                }
            }
        }
        if ( $output_format eq "xml" ) {
            print $record->as_xml_record($marcflavour);
        }
        else {
            print $record->as_usmarc();
        }
    }
    exit;
    
} # if export

else {

    my $itemtypes = GetItemTypes;
    my @itemtypesloop;
    foreach my $thisitemtype (sort keys %$itemtypes) {
        my %row =
            (
                value => $thisitemtype,
                description => $itemtypes->{$thisitemtype}->{'description'},
            );
       push @itemtypesloop, \%row;
    }
    my @branchloop;
        for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
        my $selected = 1 if $thisbranch eq $branch;
        my %row = (
            value => $thisbranch,
            selected => $selected,
            branchname => $branches->{$thisbranch}->{'branchname'},
       );
       push @branchloop, \%row;
    }
    
    $template->param(
        branchloop => \@branchloop,
        itemtypeloop => \@itemtypesloop,
                DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
    );
    
    output_html_with_http_headers $query, $cookie, $template->output;
}
Something went wrong with that request. Please try again.