Merge remote-tracking branch 'origin/new/bug_5347'
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use C4::Biblio;
24 use C4::Koha;
25 use C4::Charset;
26 use MARC::File::USMARC;
27 use C4::ImportBatch;
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30
31 BEGIN {
32         # set the version for version checking
33         $VERSION = 0.02;
34         require Exporter;
35         @ISA = qw(Exporter);
36         @EXPORT = qw(&ImportBreeding &BreedingSearch);
37 }
38
39 =head1 NAME
40
41 C4::Breeding : module to add biblios to import_records via
42                the breeding/reservoir API.
43
44 =head1 SYNOPSIS
45
46     use C4::Scan;
47     &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
48
49     C<$marcrecord> => the MARC::Record
50     C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
51                                 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
52                                 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN 
53                                 possible in the breeding
54     C<$encoding> => USMARC
55                         or UNIMARC. used for char_decoding.
56                         If not present, the parameter marcflavour is used instead
57     C<$z3950random> => the random value created during a z3950 search result.
58
59 =head1 DESCRIPTION
60
61     ImportBreeding import MARC records in the reservoir (import_records/import_batches tables).
62     the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
63     works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
64
65 =head2 ImportBreeding
66
67         ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
68
69         TODO description
70
71 =cut
72
73 sub ImportBreeding {
74     my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
75     my @marcarray = split /\x1D/, $marcrecords;
76     
77     my $dbh = C4::Context->dbh;
78     
79     my $batch_id = GetZ3950BatchId($filename);
80     my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
81     my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
82     # FIXME -- not sure that this kind of checking is actually needed
83     my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?");
84     
85 #     $encoding = C4::Context->preference("marcflavour") unless $encoding;
86     # fields used for import results
87     my $imported=0;
88     my $alreadyindb = 0;
89     my $alreadyinfarm = 0;
90     my $notmarcrecord = 0;
91     my $breedingid;
92     for (my $i=0;$i<=$#marcarray;$i++) {
93         my ($marcrecord, $charset_result, $charset_errors);
94         ($marcrecord, $charset_result, $charset_errors) = 
95             MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
96         
97 #         warn "$i : $marcarray[$i]";
98         # FIXME - currently this does nothing 
99         my @warnings = $marcrecord->warnings();
100         
101         if (scalar($marcrecord->fields()) == 0) {
102             $notmarcrecord++;
103         } else {
104             my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
105             # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, 
106             # overwrite or ignore depending on user choice
107             # drop every "special" char : spaces, - ...
108             $oldbiblio->{isbn} = C4::Koha::_isbn_cleanup($oldbiblio->{isbn}); # FIXME C4::Koha::_isbn_cleanup should be public
109             # search if biblio exists
110             my $biblioitemnumber;
111             if ($oldbiblio->{isbn}) {
112                 $searchisbn->execute($oldbiblio->{isbn});
113                 ($biblioitemnumber) = $searchisbn->fetchrow;
114             } else {
115                 if ($oldbiblio->{issn}) {
116                     $searchissn->execute($oldbiblio->{issn});
117                         ($biblioitemnumber) = $searchissn->fetchrow;
118                 }
119             }
120             if ($biblioitemnumber && $overwrite_biblio ne 2) {
121                 $alreadyindb++;
122             } else {
123                 # FIXME - in context of batch load,
124                 # rejecting records because already present in the reservoir
125                 # not correct in every case.
126                 # search in breeding farm
127                 if ($oldbiblio->{isbn}) {
128                     $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
129                     ($breedingid) = $searchbreeding->fetchrow;
130                 } elsif ($oldbiblio->{issn}){
131                     $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
132                     ($breedingid) = $searchbreeding->fetchrow;
133                 }
134                 if ($breedingid && $overwrite_biblio eq '0') {
135                     $alreadyinfarm++;
136                 } else {
137                     if ($breedingid && $overwrite_biblio eq '1') {
138                         ModBiblioInBatch($breedingid, $marcrecord);
139                     } else {
140                         my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
141                         $breedingid = $import_id;
142                     }
143                     $imported++;
144                 }
145             }
146         }
147     }
148     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
149 }
150
151
152 =head2 BreedingSearch
153
154 ($count, @results) = &BreedingSearch($title,$isbn,$random);
155 C<$title> contains the title,
156 C<$isbn> contains isbn or issn,
157 C<$random> contains the random seed from a z3950 search.
158
159 C<$count> is the number of items in C<@results>. C<@results> is an
160 array of references-to-hash; the keys are the items from the C<import_records> and
161 C<import_biblios> tables of the Koha database.
162
163 =cut
164
165 sub BreedingSearch {
166     my ($search,$isbn,$z3950random) = @_;
167     my $dbh   = C4::Context->dbh;
168     my $count = 0;
169     my ($query,@bind);
170     my $sth;
171     my @results;
172
173     $query = "SELECT import_record_id, file_name, isbn, title, author
174               FROM  import_biblios 
175               JOIN import_records USING (import_record_id)
176               JOIN import_batches USING (import_batch_id)
177               WHERE ";
178     if ($z3950random) {
179         $query .= "z3950random = ?";
180         @bind=($z3950random);
181     } else {
182         $search =~ s/(\s+)/\%/g;
183         @bind=();
184     if ($search) {
185             $query .= "title like ? OR author like ?";
186             push(@bind,"%$search%", "%$search%");
187         }
188         if ($search && $isbn) {
189             $query .= " and ";
190         }
191         if ($isbn) {
192             $query .= "isbn like ?";
193             push(@bind,"$isbn%");
194         }
195     }
196     $sth   = $dbh->prepare($query);
197     $sth->execute(@bind);
198     while (my $data = $sth->fetchrow_hashref) {
199             $results[$count] = $data;
200             # FIXME - hack to reflect difference in name 
201             # of columns in old marc_breeding and import_records
202             # There needs to be more separation between column names and 
203             # field names used in the templates </soapbox>
204             $data->{'file'} = $data->{'file_name'};
205             $data->{'id'} = $data->{'import_record_id'};
206             $count++;
207     } # while
208
209     $sth->finish;
210     return($count, @results);
211 } # sub breedingsearch
212
213 1;
214 __END__
215