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