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