synch'ing 2.2 and head
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Biblio;
22 use MARC::File::USMARC;
23 require Exporter;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 # set the version for version checking
28 $VERSION = 0.01;
29
30 =head1 NAME
31
32 C4::Breeding : script to add a biblio in marc_breeding table.
33
34 =head1 SYNOPSIS
35
36         use C4::Scan;
37         &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random);
38
39         C<$marcrecord> => the MARC::Record
40         C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
41                                                                 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
42                                                                 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN possible in the breeding
43         C<$encoding> => USMARC
44                                                 or UNIMARC. used for char_decoding.
45                                                 If not present, the parameter marcflavour is used instead
46         C<$z3950random> => the random value created during a z3950 search result.
47
48 =head1 DESCRIPTION
49
50 This module doesn't do anything.
51
52 =cut
53
54 @ISA = qw(Exporter);
55 @EXPORT = qw(&ImportBreeding &BreedingSearch);
56
57 sub  ImportBreeding {
58         my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random) = @_;
59         my @marcarray = split /\x1D/, $marcrecords;
60         my $dbh = C4::Context->dbh;
61         my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
62         my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
63         my $searchbreeding = $dbh->prepare("select id from marc_breeding
64 where isbn=? and title=?");
65         my $insertsql = $dbh->prepare("insert into marc_breeding (file,isbn,title,author,marc,encoding,z3950random) values(?,?,?,?,?,?,?)");
66         my $replacesql = $dbh->prepare("update marc_breeding set file=?,isbn=?,title=?,author=?,marc=?,encoding=?,z3950random=? where id=?");
67         $encoding = C4::Context->preference("marcflavour") unless $encoding;
68         # fields used for import results
69         my $imported=0;
70         my $alreadyindb = 0;
71         my $alreadyinfarm = 0;
72         my $notmarcrecord = 0;
73         for (my $i=0;$i<=$#marcarray;$i++) {
74                 my $marcrecord = MARC::File::USMARC::decode($marcarray[$i]."\x1D");
75                 my @warnings = $marcrecord->warnings();
76                 if (scalar($marcrecord->fields()) == 0) {
77                         $notmarcrecord++;
78                 } else {
79                         my $oldbiblio = MARCmarc2koha($dbh,$marcrecord,'');
80                         $oldbiblio->{title} = char_decode($oldbiblio->{title},$encoding);
81                         $oldbiblio->{author} = char_decode($oldbiblio->{author},$encoding);
82                         # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, overwrite or ignore depending on user choice
83                         # drop every "special" char : spaces, - ...
84                         $oldbiblio->{isbn} =~ s/ |-|\.//g,
85                         $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,10);
86                         $oldbiblio->{issn} =~ s/ |-|\.//g,
87                         $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
88                         # search if biblio exists
89                         my $biblioitemnumber;
90                         if ($oldbiblio->{isbn}) {
91                                 $searchisbn->execute($oldbiblio->{isbn});
92                                 ($biblioitemnumber) = $searchisbn->fetchrow;
93                         } else {
94                                 $searchissn->execute($oldbiblio->{issn});
95                                 ($biblioitemnumber) = $searchissn->fetchrow;
96                         }
97                         if ($biblioitemnumber) {
98                                 $alreadyindb++;
99                         } else {
100                                 # search in breeding farm
101                                 my $breedingid;
102                                 if ($oldbiblio->{isbn}) {
103                                         $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
104                                         ($breedingid) = $searchbreeding->fetchrow;
105                                 } elsif ($oldbiblio->{issn}){
106                                         $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
107                                         ($breedingid) = $searchbreeding->fetchrow;
108                                 }
109                                 if ($breedingid && $overwrite_biblio eq 0) {
110                                         $alreadyinfarm++;
111                                 } else {
112                                         my $recoded;
113                                         $recoded = $marcrecord->as_usmarc();
114                                         if ($breedingid && $overwrite_biblio eq 1) {
115                                                 $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random,$breedingid);
116                                         } else {
117                                                 $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random);
118                                         }
119                                         $imported++;
120                                 }
121                         }
122                 }
123         }
124         return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported);
125 }
126
127
128 =item BreedingSearch
129
130   ($count, @results) = &BreedingSearch($title,$isbn,$random);
131 C<$title> contains the title,
132 C<$isbn> contains isbn or issn,
133 C<$random> contains the random seed from a z3950 search.
134
135 C<$count> is the number of items in C<@results>. C<@results> is an
136 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
137
138 =cut
139
140 sub BreedingSearch {
141         my ($title,$isbn,$z3950random) = @_;
142         my $dbh   = C4::Context->dbh;
143         my $count = 0;
144         my ($query,@bind);
145         my $sth;
146         my @results;
147
148         $query = "Select id,file,isbn,title,author from marc_breeding where ";
149         if ($z3950random) {
150                 $query .= "z3950random = ?";
151                 @bind=($z3950random);
152         } else {
153             @bind=();
154                 if ($title) {
155                         $query .= "title like ?";
156                         push(@bind,"$title%");
157                 }
158                 if ($title && $isbn) {
159                         $query .= " and ";
160                 }
161                 if ($isbn) {
162                         $query .= "isbn like ?";
163                         push(@bind,"$isbn%");
164                 }
165         }
166         $sth   = $dbh->prepare($query);
167         $sth->execute(@bind);
168         while (my $data = $sth->fetchrow_hashref) {
169                         $results[$count] = $data;
170                         $count++;
171         } # while
172
173         $sth->finish;
174         return($count, @results);
175 } # sub breedingsearch
176
177
178 END { }       # module clean-up code here (global destructor)