3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
23 use MARC::File::USMARC;
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
33 C4::Breeding : script to add a biblio in marc_breeding table.
36 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random);
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.
49 This module doesn't do anything.
54 @EXPORT = qw(&ImportBreeding &BreedingSearch);
57 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random) = @_;
58 my @marcarray = split /\x1D/, $marcrecords;
59 my $dbh = C4::Context->dbh;
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");
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
76 my $alreadyinfarm = 0;
77 my $notmarcrecord = 0;
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) {
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
96 if ($oldbiblio->{isbn}) {
97 push @kohafields,"isbn";
98 push @values,$oldbiblio->{isbn};
101 ($count,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
103 push @kohafields,"issn";
104 push @values,$oldbiblio->{issn};
108 ($count,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
111 if ($count>0 && !$z3950random) {
114 # search in breeding farm
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;
123 if ($breedingid && $overwrite_biblio eq 0) {
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);
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;
140 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
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.
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.
157 my ($title,$isbn,$z3950random) = @_;
158 my $dbh = C4::Context->dbh;
164 $query = "Select id,file,isbn,title,author,classification,subclass from marc_breeding where ";
166 $query .= "z3950random = ?";
167 @bind=($z3950random);
171 $query .= "title like ?";
172 push(@bind,"$title%");
174 if ($title && $isbn) {
178 $query .= "isbn like ?";
179 push(@bind,"$isbn%");
182 $sth = $dbh->prepare($query);
183 $sth->execute(@bind);
184 while (my $data = $sth->fetchrow_hashref) {
185 $results[$count] = $data;
190 return($count, @results);
191 } # sub breedingsearch
194 END { } # module clean-up code here (global destructor)