add call to doc-head-open.inc and doc-head-close.inc
[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                                 if ($oldbiblio->{issn}) {
95                                 $searchissn->execute($oldbiblio->{issn});
96                                 ($biblioitemnumber) = $searchissn->fetchrow;
97                                 }
98                         }
99                         if ($biblioitemnumber) {
100                                 $alreadyindb++;
101                         } else {
102                                 # search in breeding farm
103                                 my $breedingid;
104                                 if ($oldbiblio->{isbn}) {
105                                         $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
106                                         ($breedingid) = $searchbreeding->fetchrow;
107                                 } elsif ($oldbiblio->{issn}){
108                                         $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
109                                         ($breedingid) = $searchbreeding->fetchrow;
110                                 }
111                                 if ($breedingid && $overwrite_biblio eq 0) {
112                                         $alreadyinfarm++;
113                                 } else {
114                                         my $recoded;
115                                         $recoded = $marcrecord->as_usmarc();
116                                         if ($breedingid && $overwrite_biblio eq 1) {
117                                                 $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random,$breedingid);
118                                         } else {
119                                                 $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random);
120                                         }
121                                         $imported++;
122                                 }
123                         }
124                 }
125         }
126         return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported);
127 }
128
129
130 =item BreedingSearch
131
132   ($count, @results) = &BreedingSearch($title,$isbn,$random);
133 C<$title> contains the title,
134 C<$isbn> contains isbn or issn,
135 C<$random> contains the random seed from a z3950 search.
136
137 C<$count> is the number of items in C<@results>. C<@results> is an
138 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
139
140 =cut
141
142 sub BreedingSearch {
143         my ($title,$isbn,$z3950random) = @_;
144         my $dbh   = C4::Context->dbh;
145         my $count = 0;
146         my ($query,@bind);
147         my $sth;
148         my @results;
149
150         $query = "Select id,file,isbn,title,author from marc_breeding where ";
151         if ($z3950random) {
152                 $query .= "z3950random = ?";
153                 @bind=($z3950random);
154         } else {
155             @bind=();
156                 if ($title) {
157                         $query .= "title like ?";
158                         push(@bind,"$title%");
159                 }
160                 if ($title && $isbn) {
161                         $query .= " and ";
162                 }
163                 if ($isbn) {
164                         $query .= "isbn like ?";
165                         push(@bind,"$isbn%");
166                 }
167         }
168         $sth   = $dbh->prepare($query);
169         $sth->execute(@bind);
170         while (my $data = $sth->fetchrow_hashref) {
171                         $results[$count] = $data;
172                         $count++;
173         } # while
174
175         $sth->finish;
176         return($count, @results);
177 } # sub breedingsearch
178
179
180 END { }       # module clean-up code here (global destructor)