-package C4::Search; #assumes C4/Search
-
+package C4::Search;
+#require '/u/acli/lib/cvs.pl'; # XXX
# Copyright 2000-2002 Katipo Communications
#
use strict;
require Exporter;
use DBI;
-use C4::Database;
+use C4::Context;
use C4::Reserves2;
-use Set::Scalar;
+ # FIXME - C4::Search uses C4::Reserves2, which uses C4::Search.
+ # So Perl complains that all of the functions here get redefined.
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
&itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
&borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
&getboracctrecord &ItemType &itemissues &subject &subtitle
-&addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs
-&findguarantor &getwebsites &getwebbiblioitems &catalogsearch &itemcount2);
+&addauthor &bibitems &barcodes &findguarantees &allissues
+&findguarantor &getwebsites &getwebbiblioitems &catalogsearch &itemcount2
+&isbnsearch &breedingsearch);
# make all your functions, whether exported or not;
=item findguarantees
#'
sub findguarantees{
my ($bornum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="select cardnumber,borrowernumber from borrowers where
guarantor='$bornum'";
my $sth=$dbh->prepare($query);
$sth->execute;
+
my @dat;
- my $i=0;
- while (my $data=$sth->fetchrow_hashref){
- $dat[$i]=$data;
- $i++;
+ while (my $data = $sth->fetchrow_hashref)
+ {
+ push @dat, $data;
}
$sth->finish;
- $dbh->disconnect;
- return($i,\@dat);
+ return (scalar(@dat), \@dat);
}
=item findguarantor
#'
sub findguarantor{
my ($bornum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="select guarantor from borrowers where
borrowernumber='$bornum'";
my $sth=$dbh->prepare($query);
$sth->execute;
$data=$sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return($data);
}
-=item systemprefs
-
- %prefs = &systemprefs();
-
-Returns a hash giving the system preferences. This is basically just a
-dump of the C<systempreferences> database table.
-
-=cut
-#'
-sub systemprefs {
- my %systemprefs;
- my $dbh=C4Connect;
- my $sth=$dbh->prepare("select variable,value from systempreferences");
- $sth->execute;
- while (my ($variable,$value)=$sth->fetchrow) {
- $systemprefs{$variable}=$value;
- }
- $sth->finish;
- $dbh->disconnect;
- return(%systemprefs);
-}
-
=item NewBorrowerNumber
$num = &NewBorrowerNumber();
=cut
#'
+# FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
+# Pick one and stick with it. Preferably use the other one. This function
+# doesn't belong in C4::Search.
sub NewBorrowerNumber {
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
$sth->execute;
my $data=$sth->fetchrow_hashref;
$sth->finish;
$data->{'max(borrowernumber)'}++;
- $dbh->disconnect;
return($data->{'max(borrowernumber)'});
}
=cut
#'
sub catalogsearch {
- my ($env,$type,$search,$num,$offset)=@_;
- my $dbh = C4Connect();
-# foreach my $key (%$search){
-# $search->{$key}=$dbh->quote($search->{$key});
-# }
- my ($count,@results);
-# print STDERR "Doing a search \n";
- # FIXME - Use "elsif" to avoid this sort of deep nesting
- if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
- print STDERR "Doing a precise search\n";
- ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
-
- } else {
- if ($search->{'subject'} ne ''){
- ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
- } else {
- if ($search->{'keyword'} ne ''){
- ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
- } else {
- ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
-
- }
- }
- }
- if ($env->{itemcount} eq '1') {
- foreach my $data (@results){
- my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
- my $subject2=$data->{'subject'};
- $subject2=~ s/ /%20/g;
- $data->{'itemcount'}=$counts->{'total'};
- my $totalitemcounts=0;
- foreach my $key (keys %$counts){
- if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
- #$data->{'location'}.="$key $counts->{$key} ";
- $totalitemcounts+=$counts->{$key};
- $data->{'locationhash'}->{$key}=$counts->{$key};
- }
- }
- my $locationtext='';
- my $notavailabletext='';
- foreach (sort keys %{$data->{'locationhash'}}) {
- if ($_ eq 'notavailable') {
- $notavailabletext="Not available";
- my $c=$data->{'locationhash'}->{$_};
- if ($totalitemcounts>1) {
- $notavailabletext.=" ($c)";
- }
- } else {
- $locationtext.="$_";
- my $c=$data->{'locationhash'}->{$_};
- if ($totalitemcounts>1) {
- $locationtext.=" ($c), ";
- }
- }
- }
- if ($notavailabletext) {
- $locationtext.=$notavailabletext;
- } else {
- $locationtext=~s/, $//;
- }
- $data->{'location'}=$locationtext;
- $data->{'subject2'}=$subject2;
- }
- }
- return ($count,@results);
+ my ($env,$type,$search,$num,$offset)=@_;
+ my $dbh = C4::Context->dbh;
+ # foreach my $key (%$search){
+ # $search->{$key}=$dbh->quote($search->{$key});
+ # }
+ my ($count,@results);
+ # print STDERR "Doing a search \n";
+ # FIXME - Use "elsif" to avoid this sort of deep nesting
+ if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
+ print STDERR "Doing a precise search\n";
+ ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
+ } else {
+ if ($search->{'subject'} ne ''){
+ ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
+ } else {
+ if ($search->{'keyword'} ne ''){
+ ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
+ } else {
+ ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
+
+ }
+ }
+ }
+ if ($env->{itemcount} eq '1') {
+ foreach my $data (@results){
+ my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
+ my $subject2=$data->{'subject'};
+ $subject2=~ s/ /%20/g;
+ $data->{'itemcount'}=$counts->{'total'};
+ my $totalitemcounts=0;
+ foreach my $key (keys %$counts){
+ if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
+ #$data->{'location'}.="$key $counts->{$key} ";
+ $totalitemcounts+=$counts->{$key};
+ $data->{'locationhash'}->{$key}=$counts->{$key};
+ }
+ }
+ my $locationtext='';
+ my $notavailabletext='';
+ foreach (sort keys %{$data->{'locationhash'}}) {
+ if ($_ eq 'notavailable') {
+ $notavailabletext="Not available";
+ my $c=$data->{'locationhash'}->{$_};
+ if ($totalitemcounts>1) {
+ $notavailabletext.=" ($c)";
+ }
+ } else {
+ $locationtext.="$_";
+ my $c=$data->{'locationhash'}->{$_};
+ if ($totalitemcounts>1) {
+ $locationtext.=" ($c), ";
+ }
+ }
+ }
+ if ($notavailabletext) {
+ $locationtext.=$notavailabletext;
+ } else {
+ $locationtext=~s/, $//;
+ }
+ $data->{'location'}=$locationtext;
+ $data->{'subject2'}=$subject2;
+ }
+ }
+ return ($count,@results);
}
=item KeywordSearch
=cut
#'
-
-# FIXME - Everything that's being done here with Set::Scalar can be
-# done with hashes: to add a result to the set, just use:
-# $results{$foo} = 1;
-# To get the list of results, just use
-# @biblionumbers = sort keys %results;
-# This would remove the dependency on Set::Scalar, which means one
-# fewer package that people have to install.
-
sub KeywordSearch {
my ($env,$type,$search,$num,$offset)=@_;
- my $dbh = &C4Connect;
+ my $dbh = C4::Context->dbh;
$search->{'keyword'}=~ s/ +$//;
$search->{'keyword'}=~ s/'/\\'/;
my @key=split(' ',$search->{'keyword'});
+ # FIXME - Naive users might enter comma-separated
+ # words, e.g., "training, animal". Ought to cope with
+ # this.
my $count=@key;
my $i=1;
- my @results;
+ my %biblionumbers; # Set of biblionumbers returned by the
+ # various searches.
+
+ # FIXME - Ought to filter the stopwords out of the list of keywords.
+ # @key = map { !defined($stopwords{$_}) } @key;
+
+ # FIXME - The way this code is currently set up, it looks for all of
+ # the keywords first in (title, notes, seriestitle), then in the
+ # subtitle, then in the subject. Thus, if you look for keywords
+ # "science fiction", this search won't find a book with
+ # title = "How to write fiction"
+ # subtitle = "A science-based approach"
+ # Is this the desired effect? If not, then the first SQL query
+ # should look in the biblio, subtitle, and subject tables all at
+ # once. The way the first query is built can accomodate this easily.
# Look for keywords in table 'biblio'.
- # FIXME - The next 15 lines can be rewritten in somewhat Lisp-ish
- # fashion as follows:
- #
- # my $query = "select biblionumber from biblio where (" .
- # join(") or (",
- # map {
- # my $field = $_;
- # join (" and ",
- # map {
- # "($field like '$_\%' or $field like '\% $_\%')"
- # }
- # @key)
- # }
- # qw( title biblio.notes seriestitle )) .
- # ")";
- # FIXME - Also,
- # field like 'string%' or field like '% string%'
- # can be rewritten (in MySQL, at least) as
- # field regexp '(^| )string';
- # However, this isn't portable. Though PostgreSQL allows you to use "~"
- # instead of "regexp".
- my $query="Select biblionumber from biblio
- where ((title like '$key[0]%' or title like '% $key[0]%')";
- while ($i < $count){
- $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
- $i++;
- }
- $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
- }
- $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
+ # Build an SQL query that finds each of the keywords in any of the
+ # title, biblio.notes, or seriestitle. To do this, we'll build up an
+ # array of clauses, one for each keyword.
+ my $query; # The SQL query
+ my @clauses = (); # The search clauses
+
+ $query = <<EOT; # Beginning of the query
+ SELECT biblionumber
+ FROM biblio
+ WHERE
+EOT
+ foreach my $keyword (@key)
+ {
+ my @subclauses = (); # Subclauses, one for each field we're
+ # searching on
+
+ # For each field we're searching on, create a subclause that'll
+ # match the current keyword in the current field.
+ foreach my $field (qw(title notes seriestitle))
+ {
+ push @subclauses,
+ "$field LIKE '\Q$keyword\E%' OR $field LIKE '% \Q$keyword\E%'";
+ }
+ # (Yes, this could have been done as
+ # @subclauses = map {...} qw(field1 field2 ...)
+ # )but I think this way is more readable.
+
+ # Construct the current clause by joining the subclauses.
+ push @clauses, "(" . join(")\n\tOR (", @subclauses) . ")";
}
- $query.=" )";
-# print $query;
+ # Now join all of the clauses together and append to the query.
+ $query .= "(" . join(")\nAND (", @clauses) . ")";
+
+ # FIXME - Perhaps use $sth->bind_columns() ? Documented as the most
+ # efficient way to fetch data.
my $sth=$dbh->prepare($query);
$sth->execute;
- $i=0;
- while (my @res=$sth->fetchrow_array){
- $results[$i]=$res[0];
- $i++;
+ while (my @res = $sth->fetchrow_array) {
+ for (@res)
+ {
+ $biblionumbers{$_} = 1; # Add these results to the set
+ }
}
$sth->finish;
- my $set1=Set::Scalar->new(@results);
# Now look for keywords in the 'bibliosubtitle' table.
- $query="Select biblionumber from bibliosubtitle where
- ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
- for ($i=1;$i<$count;$i++){
- $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
+
+ # Again, we build a list of clauses from the keywords.
+ @clauses = ();
+ $query = "SELECT biblionumber FROM bibliosubtitle WHERE ";
+ foreach my $keyword (@key)
+ {
+ push @clauses,
+ "subtitle LIKE '\Q$keyword\E%' OR subtitle like '% \Q$keyword\E%'";
}
- $query.=" )";
-# print $query;
+ $query .= "(" . join(") AND (", @clauses) . ")";
+
$sth=$dbh->prepare($query);
$sth->execute;
- $i=0;
- while (my @res=$sth->fetchrow_array){
- $results[$i]=$res[0];
- $i++;
+ while (my @res = $sth->fetchrow_array) {
+ for (@res)
+ {
+ $biblionumbers{$_} = 1; # Add these results to the set
+ }
}
$sth->finish;
- my $set2=Set::Scalar->new(@results);
- if ($i > 0){
- $set1=$set1+$set2;
- }
# Look for the keywords in the notes for individual items
# ('biblioitems.notes')
- $query ="Select biblionumber from biblioitems where
- ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
+
+ # Again, we build a list of clauses from the keywords.
+ @clauses = ();
+ $query = "SELECT biblionumber FROM biblioitems WHERE ";
+ foreach my $keyword (@key)
+ {
+ push @clauses,
+ "notes LIKE '\Q$keyword\E%' OR notes like '% \Q$keyword\E%'";
}
- $query.=" )";
-# print $query;
+ $query .= "(" . join(") AND (", @clauses) . ")";
+
$sth=$dbh->prepare($query);
$sth->execute;
- $i=0;
- while (my @res=$sth->fetchrow_array){
- $results[$i]=$res[0];
- $i++;
+ while (my @res = $sth->fetchrow_array) {
+ for (@res)
+ {
+ $biblionumbers{$_} = 1; # Add these results to the set
+ }
}
$sth->finish;
- my $set3=Set::Scalar->new(@results);
- if ($i > 0){
- $set1=$set1+$set3;
- }
# Look for keywords in the 'bibliosubject' table.
+
+ # FIXME - The other queries look for words in the desired field that
+ # begin with the individual keywords the user entered. This one
+ # searches for the literal string the user entered. Is this the
+ # desired effect?
+ # Note in particular that spaces are retained: if the user typed
+ # science fiction
+ # (with two spaces), this won't find the subject "science fiction"
+ # (one space). Likewise, a search for "%" will return absolutely
+ # everything.
+ # If this isn't the desired effect, see the previous searches for
+ # how to do it.
+
$sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
like '%$search->{'keyword'}%' group by biblionumber");
$sth->execute;
- $i=0;
- while (my @res=$sth->fetchrow_array){
- $results[$i]=$res[0];
- $i++;
+
+ while (my @res = $sth->fetchrow_array) {
+ for (@res)
+ {
+ $biblionumbers{$_} = 1; # Add these results to the set
+ }
}
$sth->finish;
- my $set4=Set::Scalar->new(@results);
- if ($i > 0){
- $set1=$set1+$set4;
- }
+
my $i2=0;
my $i3=0;
my $i4=0;
my @res2;
- my @res = $set1->members;
+ my @res = keys %biblionumbers;
$count=@res;
-# print $set1;
+
$i=0;
# print "count $count";
if ($search->{'class'} ne ''){
} else {
# $search->{'class'} was not specified
+
+ # FIXME - This is bogus: it makes a separate query for each
+ # biblioitem, and returns results in apparently random order. It'd
+ # be much better to combine all of the previous queries into one big
+ # one (building it up a little at a time, of course), and have that
+ # big query select all of the desired fields, instead of just
+ # 'biblionumber'.
+
while ($i2 < $num && $i2 < $count){
my $query="select * from biblio,biblioitems where
biblio.biblionumber='$res[$i2+$offset]' and
biblio.biblionumber=biblioitems.biblionumber ";
- if ($search->{'class'} ne ''){ # FIXME - Ignored: we already know
- # that $search->{'class'} eq '';
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= "and ( itemtype='$temp[0]'";
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype='$temp[$i]'";
- }
- $query.=")";
- }
if ($search->{'dewey'} ne ''){
$query.= "and (dewey like '$search->{'dewey'}%') ";
}
}
}
- $dbh->disconnect;
#$count=$i;
return($count,@res2);
sub KeywordSearch2 {
my ($env,$type,$search,$num,$offset)=@_;
- my $dbh = &C4Connect;
+ my $dbh = C4::Context->dbh;
$search->{'keyword'}=~ s/ +$//;
$search->{'keyword'}=~ s/'/\\'/;
my @key=split(' ',$search->{'keyword'});
biblio.biblionumber=bibliosubtitle.biblionumber and
(((title like '$key[0]%' or title like '% $key[0]%')";
while ($i < $count){
- $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
+ $query .= " and (title like '$key[$i]%' or title like '% $key[$i]%')";
$i++;
}
$query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
or seriestitle like '% new zealand')"
}
- $query=$query."))";
+ $query .= "))";
if ($search->{'class'} ne ''){
my @temp=split(/\|/,$search->{'class'});
my $count=@temp;
$i2++;
}
$sth->finish;
- $dbh->disconnect;
# $i--;
# $i++;
return($i,@res2);
=head2 Subject search
-If C<$type> is set to C<subject>, the following search criteria may be
-used:
+If C<$type> is set to C<subject>, the following search criterion may
+be used:
=over 4
=cut
#'
sub CatSearch {
- my ($env,$type,$search,$num,$offset)=@_;
- my $dbh = &C4Connect;
- my $query = '';
- my @results;
- # FIXME - Why not just
- # $search->{'title'} = quotemeta($search->{'title'})
- # to escape all questionable characters, not just single-quotes?
- $search->{'title'}=~ s/'/\\'/g;
- $search->{'author'}=~ s/'/\\'/g;
- $search->{'illustrator'}=~ s/'/\\'/g;
- my $title = lc($search->{'title'});
-
- if ($type eq 'loose') {
- if ($search->{'author'} ne ''){
- my @key=split(' ',$search->{'author'});
- my $count=@key;
- my $i=1;
- $query="select *,biblio.author,biblio.biblionumber from
- biblio
- left join additionalauthors
- on additionalauthors.biblionumber =biblio.biblionumber
- where
- ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
- additionalauthors.author like '$key[0]%' or additionalauthors.author
- like '% $key[0]%'
- )";
- while ($i < $count){
- $query=$query." and (
- biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
- additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
- )";
- $i++;
- }
- $query=$query.")";
- if ($search->{'title'} ne ''){
- my @key=split(' ',$search->{'title'});
- my $count=@key;
- my $i=0;
- $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
- while ($i<$count){
- $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
- $i++;
- }
-# $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')";
-# for ($i=1;$i<$count;$i++){
-# $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";
-# }
- $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
- }
- $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
- }
- $query=$query."))";
- #$query=$query. " and (title like '%$search->{'title'}%'
- #or seriestitle like '%$search->{'title'}%')";
- }
- if ($search->{'abstract'} ne ''){
- $query.= " and (abstract like '%$search->{'abstract'}%')";
- }
- if ($search->{'date-before'} ne ''){
- $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
- }
-
- $query.=" group by biblio.biblionumber";
- } else {
- if ($search->{'title'} ne '') {
- if ($search->{'ttype'} eq 'exact'){
- $query="select * from biblio
- where
- (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
- or biblio.unititle like '$search->{'title'} |%' or
- biblio.unititle like '%| $search->{'title'} |%' or
- biblio.unititle like '%| $search->{'title'}') or
- (biblio.seriestitle = '$search->{'title'}' or
- biblio.seriestitle like '$search->{'title'} |%' or
- biblio.seriestitle like '%| $search->{'title'} |%' or
- biblio.seriestitle like '%| $search->{'title'}')
- )";
- } else {
- my @key=split(' ',$search->{'title'});
- my $count=@key;
- my $i=1;
- $query="select * from biblio
- left join bibliosubtitle on
- biblio.biblionumber=bibliosubtitle.biblionumber
- where
- (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
- while ($i<$count){
- $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
- $i++;
- }
- $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
- }
- $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
- }
- $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
- for ($i=1;$i<$count;$i++){
- $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
- }
- $query=$query."))";
- }
- if ($search->{'abstract'} ne ''){
- $query.= " and (abstract like '%$search->{'abstract'}%')";
- }
- if ($search->{'date-before'} ne ''){
- $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
- }
- } elsif ($search->{'class'} ne ''){
- $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= " and ( itemtype='$temp[0]'";
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype='$temp[$i]'";
- }
- $query.=")";
- if ($search->{'illustrator'} ne ''){
- $query.=" and illus like '%".$search->{'illustrator'}."%' ";
- }
- if ($search->{'dewey'} ne ''){
- $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
- }
- } elsif ($search->{'dewey'} ne ''){
- $query="select * from biblioitems,biblio
- where biblio.biblionumber=biblioitems.biblionumber
- and biblioitems.dewey like '$search->{'dewey'}%'";
- } elsif ($search->{'illustrator'} ne '') {
- $query="select * from biblioitems,biblio
- where biblio.biblionumber=biblioitems.biblionumber
- and biblioitems.illus like '%".$search->{'illustrator'}."%'";
- } elsif ($search->{'publisher'} ne ''){
- $query.= "Select * from biblio,biblioitems where biblio.biblionumber
- =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
- } elsif ($search->{'abstract'} ne ''){
- $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
-
- } elsif ($search->{'date-before'} ne ''){
- $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
- }
- $query .=" group by biblio.biblionumber";
- }
- }
- if ($type eq 'subject'){
- my @key=split(' ',$search->{'subject'});
- my $count=@key;
- my $i=1;
- $query="select distinct(subject) from bibliosubject where( subject like
- '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
- while ($i<$count){
- $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
- or subject like '% $key[$i]'
- or subject like '%($key[$i])%')";
- $i++;
- }
-
- # FIXME - Wouldn't it be better to fix the database so that if a
- # book has a subject "NZ", then it also gets added the subject
- # "New Zealand"?
- # This can also be generalized by adding a table of subject
- # synonyms to the database: just declare "NZ" to be a synonym for
- # "New Zealand", "SF" a synonym for both "Science fiction" and
- # "Fantastic fiction", etc.
-
- # FIXME - This can be rewritten as
- # if (lc($search->{"subject"}) eq "nz") {
- if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){
- $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
- or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
- } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
- $query=~ s/ nz/ NEW ZEALAND/ig;
- $query=~ s/nz /NEW ZEALAND /ig;
- $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
- }
- }
- if ($type eq 'precise'){
-
- if ($search->{'item'} ne ''){
- $query="select * from items,biblio ";
- my $search2=uc $search->{'item'};
- $query=$query." where
- items.biblionumber=biblio.biblionumber
- and barcode='$search2'";
- }
- if ($search->{'isbn'} ne ''){
- my $search2=uc $search->{'isbn'};
- my $query1 = "select * from biblioitems where isbn='$search2'";
- my $sth1=$dbh->prepare($query1);
-# print STDERR "$query1\n";
- $sth1->execute;
- my $i2=0;
- while (my $data=$sth1->fetchrow_hashref) {
- $query="select * from biblioitems,biblio where
- biblio.biblionumber = $data->{'biblionumber'}
- and biblioitems.biblionumber = biblio.biblionumber";
- my $sth=$dbh->prepare($query);
- $sth->execute;
- my $data=$sth->fetchrow_hashref;
- my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
- # FIXME - The following assumes that the Dewey code is a
- # floating-point number. It isn't: it's a string.
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass");
- $data->{'dewey'}=$dewey;
- $results[$i2]=$data;
-# $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
- $i2++;
- $sth->finish;
+ my ($env,$type,$search,$num,$offset)=@_;
+ warn "type = $type";
+ my $dbh = C4::Context->dbh;
+ my $query = '';
+ my @results;
+ # FIXME - Why not just
+ # $search->{'title'} = quotemeta($search->{'title'})
+ # to escape all questionable characters, not just single-quotes?
+ $search->{'title'}=~ s/'/\\'/g;
+ $search->{'author'}=~ s/'/\\'/g;
+ $search->{'illustrator'}=~ s/'/\\'/g;
+ my $title = lc($search->{'title'});
+ if ($type eq 'loose') {
+ if ($search->{'author'} ne ''){
+ my @key=split(' ',$search->{'author'});
+ my $count=@key;
+ my $i=1;
+ $query="select *,biblio.author,biblio.biblionumber from
+ biblio
+ left join additionalauthors
+ on additionalauthors.biblionumber =biblio.biblionumber
+ where
+ ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
+ additionalauthors.author like '$key[0]%' or additionalauthors.author
+ like '% $key[0]%'
+ )";
+ while ($i < $count){
+ $query .= " and (
+ biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
+ additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
+ )";
+ $i++;
+ }
+ $query .= ")";
+ if ($search->{'title'} ne ''){
+ my @key=split(' ',$search->{'title'});
+ my $count=@key;
+ my $i=0;
+ $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
+ while ($i<$count){
+ $query .= " and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
+ $i++;
+ }
+ $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
+ for ($i=1;$i<$count;$i++){
+ $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
+ }
+ $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
+ for ($i=1;$i<$count;$i++){
+ $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
+ }
+ $query .= "))";
+ #$query=$query. " and (title like '%$search->{'title'}%'
+ #or seriestitle like '%$search->{'title'}%')";
+ }
+ if ($search->{'abstract'} ne ''){
+ $query.= " and (abstract like '%$search->{'abstract'}%')";
+ }
+ if ($search->{'date-before'} ne ''){
+ $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
+ }
+ $query.=" group by biblio.biblionumber";
+ } else {
+ if ($search->{'title'} ne '') {
+ if ($search->{'ttype'} eq 'exact'){
+ $query="select * from biblio
+ where
+ (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
+ or biblio.unititle like '$search->{'title'} |%' or
+ biblio.unititle like '%| $search->{'title'} |%' or
+ biblio.unititle like '%| $search->{'title'}') or
+ (biblio.seriestitle = '$search->{'title'}' or
+ biblio.seriestitle like '$search->{'title'} |%' or
+ biblio.seriestitle like '%| $search->{'title'} |%' or
+ biblio.seriestitle like '%| $search->{'title'}')
+ )";
+ } else {
+ my @key=split(' ',$search->{'title'});
+ my $count=@key;
+ my $i=1;
+ $query="select biblio.biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp,subtitle from biblio
+ left join bibliosubtitle on
+ biblio.biblionumber=bibliosubtitle.biblionumber
+ where
+ (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
+ while ($i<$count){
+ $query .= " and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
+ $i++;
+ }
+ $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
+ for ($i=1;$i<$count;$i++){
+ $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
+ }
+ $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
+ for ($i=1;$i<$count;$i++){
+ $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
+ }
+ $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
+ for ($i=1;$i<$count;$i++){
+ $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
+ }
+ $query .= "))";
+ }
+ if ($search->{'abstract'} ne ''){
+ $query.= " and (abstract like '%$search->{'abstract'}%')";
+ }
+ if ($search->{'date-before'} ne ''){
+ $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
+ }
+ } elsif ($search->{'class'} ne ''){
+ $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
+ my @temp=split(/\|/,$search->{'class'});
+ my $count=@temp;
+ $query.= " and ( itemtype='$temp[0]'";
+ for (my $i=1;$i<$count;$i++){
+ $query.=" or itemtype='$temp[$i]'";
+ }
+ $query.=")";
+ if ($search->{'illustrator'} ne ''){
+ $query.=" and illus like '%".$search->{'illustrator'}."%' ";
+ }
+ if ($search->{'dewey'} ne ''){
+ $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
+ }
+ } elsif ($search->{'dewey'} ne ''){
+ $query="select * from biblioitems,biblio
+ where biblio.biblionumber=biblioitems.biblionumber
+ and biblioitems.dewey like '$search->{'dewey'}%'";
+ } elsif ($search->{'illustrator'} ne '') {
+ $query="select * from biblioitems,biblio
+ where biblio.biblionumber=biblioitems.biblionumber
+ and biblioitems.illus like '%".$search->{'illustrator'}."%'";
+ } elsif ($search->{'publisher'} ne ''){
+ $query.= "Select * from biblio,biblioitems where biblio.biblionumber
+ =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
+ } elsif ($search->{'abstract'} ne ''){
+ $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
+ } elsif ($search->{'date-before'} ne ''){
+ $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
+ }
+ $query .=" group by biblio.biblionumber";
+ }
}
- $sth1->finish;
- }
- }
-#print $query;
-if ($type ne 'precise' && $type ne 'subject'){
- if ($search->{'author'} ne ''){
- $query=$query." order by biblio.author,title";
- } else {
- $query=$query." order by title";
- }
-} else {
- if ($type eq 'subject'){
- $query=$query." order by subject";
- }
-}
-#print STDERR "$query\n";
-my $sth=$dbh->prepare($query);
-$sth->execute;
-my $count=1;
-my $i=0;
-my $limit= $num+$offset;
-while (my $data=$sth->fetchrow_hashref){
- my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
- if ($search->{'class'} ne ''){
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= " and ( itemtype='$temp[0]'";
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype='$temp[$i]'";
- }
- $query.=")";
- }
- if ($search->{'dewey'} ne ''){
- $query.=" and dewey='$search->{'dewey'}' ";
- }
- if ($search->{'illustrator'} ne ''){
- $query.=" and illus like '%".$search->{'illustrator'}."%' ";
- }
- if ($search->{'publisher'} ne ''){
- $query.= " and (publishercode like '%$search->{'publisher'}%')";
- }
-
- my $sti=$dbh->prepare($query);
- $sti->execute;
- my $dewey;
- my $subclass;
- my $true=0;
- my $publishercode;
- my $bibitemdata;
- if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
- $true=1;
- $dewey=$bibitemdata->{'dewey'};
- $subclass=$bibitemdata->{'subclass'};
- $publishercode=$bibitemdata->{'publishercode'};
- }
- print STDERR "$dewey $subclass $publishercode\n";
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass");
- $data->{'dewey'}=$dewey;
- $data->{'publishercode'}=$publishercode;
- $sti->finish;
- if ($true == 1){
- if ($count > $offset && $count <= $limit){
- $results[$i]=$data;
- $i++;
- }
- $count++;
- }
-}
-$sth->finish;
-#if ($type ne 'precise'){
- $count--;
-#}
-#$count--;
-return($count,@results);
+ if ($type eq 'subject'){
+ # FIXME - Subject search is badly broken. The query defined by
+ # $query returns a single item (the subject), but later code
+ # expects a ref-to-hash with all sorts of stuff in it.
+ # Also, the count of items (biblios?) with the given subject is
+ # wrong.
+
+ my @key=split(' ',$search->{'subject'});
+ my $count=@key;
+ my $i=1;
+ $query="select distinct(subject) from bibliosubject where( subject like
+ '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
+ while ($i<$count){
+ $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
+ or subject like '% $key[$i]'
+ or subject like '%($key[$i])%')";
+ $i++;
+ }
+
+ # FIXME - Wouldn't it be better to fix the database so that if a
+ # book has a subject "NZ", then it also gets added the subject
+ # "New Zealand"?
+ # This can also be generalized by adding a table of subject
+ # synonyms to the database: just declare "NZ" to be a synonym for
+ # "New Zealand", "SF" a synonym for both "Science fiction" and
+ # "Fantastic fiction", etc.
+
+ # FIXME - This can be rewritten as
+ # if (lc($search->{"subject"}) eq "nz") {
+ if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){
+ $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
+ or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
+ } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
+ $query=~ s/ nz/ NEW ZEALAND/ig;
+ $query=~ s/nz /NEW ZEALAND /ig;
+ $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
+ }
+ }
+ if ($type eq 'precise'){
+ if ($search->{'itemnumber'} ne ''){
+ $query="select * from items,biblio ";
+ my $search2=uc $search->{'itemnumber'};
+ $query=$query." where
+ items.biblionumber=biblio.biblionumber
+ and barcode='$search2'";
+ # FIXME - .= <<EOT;
+ }
+ if ($search->{'isbn'} ne ''){
+ my $search2=uc $search->{'isbn'};
+ my $query1 = "select * from biblioitems where isbn='$search2'";
+ my $sth1=$dbh->prepare($query1);
+ # print STDERR "$query1\n";
+ $sth1->execute;
+ my $i2=0;
+ while (my $data=$sth1->fetchrow_hashref) {
+ $query="select * from biblioitems,biblio where
+ biblio.biblionumber = $data->{'biblionumber'}
+ and biblioitems.biblionumber = biblio.biblionumber";
+ my $sth=$dbh->prepare($query);
+ $sth->execute;
+ # FIXME - There's already a $data in this scope.
+ my $data=$sth->fetchrow_hashref;
+ my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
+ # FIXME - The following assumes that the Dewey code is a
+ # floating-point number. It isn't: it's a string.
+ $dewey=~s/\.*0*$//;
+ ($dewey == 0) && ($dewey='');
+ ($dewey) && ($dewey.=" $subclass");
+ $data->{'dewey'}=$dewey;
+ $results[$i2]=$data;
+ # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
+ $i2++;
+ $sth->finish;
+ }
+ $sth1->finish;
+ }
+ }
+ if ($type ne 'precise' && $type ne 'subject'){
+ if ($search->{'author'} ne ''){
+ $query .= " order by biblio.author,title";
+ } else {
+ $query .= " order by title";
+ }
+ } else {
+ if ($type eq 'subject'){
+ $query .= " order by subject";
+ }
+ }
+ my $sth=$dbh->prepare($query);
+ $sth->execute;
+ my $count=1;
+ my $i=0;
+ my $limit= $num+$offset;
+ while (my $data=$sth->fetchrow_hashref){
+ my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
+ if ($search->{'class'} ne ''){
+ my @temp=split(/\|/,$search->{'class'});
+ my $count=@temp;
+ $query.= " and ( itemtype='$temp[0]'";
+ for (my $i=1;$i<$count;$i++){
+ $query.=" or itemtype='$temp[$i]'";
+ }
+ $query.=")";
+ }
+ if ($search->{'dewey'} ne ''){
+ $query.=" and dewey='$search->{'dewey'}' ";
+ }
+ if ($search->{'illustrator'} ne ''){
+ $query.=" and illus like '%".$search->{'illustrator'}."%' ";
+ }
+ if ($search->{'publisher'} ne ''){
+ $query.= " and (publishercode like '%$search->{'publisher'}%')";
+ }
+ warn $query;
+ my $sti=$dbh->prepare($query);
+ $sti->execute;
+ my $dewey;
+ my $subclass;
+ my $true=0;
+ my $publishercode;
+ my $bibitemdata;
+ if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
+ $true=1;
+ $dewey=$bibitemdata->{'dewey'};
+ $subclass=$bibitemdata->{'subclass'};
+ $publishercode=$bibitemdata->{'publishercode'};
+ }
+ # print STDERR "$dewey $subclass $publishercode\n";
+ # FIXME - The Dewey code is a string, not a number.
+ $dewey=~s/\.*0*$//;
+ ($dewey == 0) && ($dewey='');
+ ($dewey) && ($dewey.=" $subclass");
+ $data->{'dewey'}=$dewey;
+ $data->{'publishercode'}=$publishercode;
+ $sti->finish;
+ if ($true == 1){
+ if ($count > $offset && $count <= $limit){
+ $results[$i]=$data;
+ $i++;
+ }
+ $count++;
+ }
+ }
+ $sth->finish;
+ $count--;
+ return($count,@results);
}
sub updatesearchstats{
#'
sub subsearch {
my ($env,$subject)=@_;
- my $dbh=C4Connect();
+ my $dbh = C4::Context->dbh;
$subject=$dbh->quote($subject);
my $query="Select * from biblio,bibliosubject where
biblio.biblionumber=bibliosubject.biblionumber and
$i++;
}
$sth->finish;
- $dbh->disconnect;
return(@results);
}
If this is set, it is set to C<One Order>.
+=back
+
=cut
#'
sub ItemInfo {
my ($env,$biblionumber,$type) = @_;
- my $dbh = &C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "SELECT * FROM items, biblio, biblioitems, itemtypes
WHERE items.biblionumber = ?
AND biblioitems.biblioitemnumber = items.biblioitemnumber
$dewey='';
}
$dewey=~ s/\.$//;
- $class = $class.$dewey;
+ $class .= $dewey;
if ($dewey ne ''){
- $class = $class.$data->{'subclass'};
+ $class .= $data->{'subclass'};
}
# $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
# FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
}
$sth2->finish;
- $dbh->disconnect;
return(@results);
}
sub GetItems {
my ($env,$biblionumber)=@_;
#debug_msg($env,"GetItems");
- my $dbh = &C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
#debug_msg($env,$query);
my $sth=$dbh->prepare($query);
my $dewey = $data->{'dewey'};
$dewey =~ s/0+$//;
my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
- $line = $line."\t$data->{'classification'}\t$dewey";
- $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
- $line = $line."\t$data->{'volume'}\t$data->{number}";
+ $line .= "\t$data->{'classification'}\t$dewey";
+ $line .= "\t$data->{'subclass'}\t$data->{isbn}";
+ $line .= "\t$data->{'volume'}\t$data->{number}";
my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
$isth->execute;
while (my $idata = $isth->fetchrow_hashref) {
my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
if ($idata->{'notforloan'} == 1) {
- $iline = $iline."NFL ";
+ $iline .= "NFL ";
}
if ($idata->{'itemlost'} == 1) {
- $iline = $iline."LOST ";
+ $iline .= "LOST ";
}
- $line = $line."\t$iline";
+ $line .= "\t$iline";
}
$isth->finish;
$results[$i] = $line;
$i++;
}
$sth->finish;
- $dbh->disconnect;
return(@results);
}
#'
sub itemdata {
my ($barcode)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select * from items,biblioitems where barcode='$barcode'
and items.biblioitemnumber=biblioitems.biblioitemnumber";
# print $query;
$sth->execute;
my $data=$sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return($data);
}
#'
sub bibdata {
my ($bibnum, $type) = @_;
- my $dbh = C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "Select *, biblio.notes
from biblio, biblioitems
left join bibliosubtitle on
} # while
$sth->finish;
- $dbh->disconnect;
return($data);
} # sub bibdata
#'
sub bibitemdata {
my ($bibitem) = @_;
- my $dbh = C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
where biblio.biblionumber = biblioitems.biblionumber
and biblioitemnumber = $bibitem
$data = $sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return($data);
} # sub bibitemdata
#'
sub subject {
my ($bibnum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select * from bibliosubject where biblionumber=$bibnum";
my $sth=$dbh->prepare($query);
$sth->execute;
$i++;
}
$sth->finish;
- $dbh->disconnect;
return($i,\@results);
}
#'
sub addauthor {
my ($bibnum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select * from additionalauthors where biblionumber=$bibnum";
my $sth=$dbh->prepare($query);
$sth->execute;
$i++;
}
$sth->finish;
- $dbh->disconnect;
return($i,\@results);
}
#'
sub subtitle {
my ($bibnum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
my $sth=$dbh->prepare($query);
$sth->execute;
$i++;
}
$sth->finish;
- $dbh->disconnect;
return($i,\@results);
}
#'
sub itemissues {
my ($bibitem, $biblio)=@_;
- my $dbh = C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "Select * from items where
items.biblioitemnumber = '$bibitem'";
# FIXME - If this function die()s, the script will abort, and the
# Find the last 3 people who borrowed this item.
$query2 = "select * from issues, borrowers
-where itemnumber = '$data->{'itemnumber'}'
-and issues.borrowernumber = borrowers.borrowernumber
-and returndate is not NULL
-order by returndate desc,timestamp desc";
- $sth2 = $dbh->prepare($query2)
- || die $dbh->errstr;
- $sth2->execute
- || die $sth2->errstr;
-
- for (my $i2 = 0; $i2 < 2; $i2++) {
+ where itemnumber = ?
+ and issues.borrowernumber = borrowers.borrowernumber
+ and returndate is not NULL
+ order by returndate desc,timestamp desc";
+warn "$query2";
+ $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
+ $sth2->execute($data->{'itemnumber'}) || die $sth2->errstr;
+ for (my $i2 = 0; $i2 < 2; $i2++) { # FIXME : error if there is less than 3 pple borrowing this item
if (my $data2 = $sth2->fetchrow_hashref) {
$data->{"timestamp$i2"} = $data2->{'timestamp'};
$data->{"card$i2"} = $data2->{'cardnumber'};
}
$sth->finish;
- $dbh->disconnect;
return(@results);
}
#'
sub itemnodata {
my ($env,$dbh,$itemnumber) = @_;
- $dbh=C4Connect;
+ $dbh = C4::Context->dbh;
my $query="Select * from biblio,items,biblioitems
where items.itemnumber = '$itemnumber'
and biblio.biblionumber = items.biblionumber
$sth->execute;
my $data=$sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return($data);
}
#called by member.pl
sub BornameSearch {
my ($env,$searchstring,$type)=@_;
- my $dbh = &C4Connect;
+ my $dbh = C4::Context->dbh;
$searchstring=~ s/\'/\\\'/g;
my @data=split(' ',$searchstring);
my $count=@data;
$query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"
or firstname like \"$data[$i]%\" or firstname like \"% $data[$i]%\"
or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
+ # FIXME - .= <<EOT;
}
$query=$query.") or cardnumber = \"$searchstring\"
order by surname,firstname";
+ # FIXME - .= <<EOT;
# print $query,"\n";
my $sth=$dbh->prepare($query);
$sth->execute;
}
# $sth->execute;
$sth->finish;
- $dbh->disconnect;
return ($cnt,\@results);
}
sub borrdata {
my ($cardnumber,$bornum)=@_;
$cardnumber = uc $cardnumber;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query;
if ($bornum eq ''){
$query="Select * from borrowers where cardnumber='$cardnumber'";
$sth->execute;
my $data=$sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return($data);
}
#'
sub borrissues {
my ($bornum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query;
$query="Select * from issues,biblio,items where borrowernumber='$bornum' and
items.itemnumber=issues.itemnumber and
my $sth=$dbh->prepare($query);
$sth->execute;
my @result;
- my $i=0;
- while (my $data=$sth->fetchrow_hashref){
- $result[$i]=$data;;
- $i++;
+ while (my $data = $sth->fetchrow_hashref) {
+ push @result, $data;
}
$sth->finish;
- $dbh->disconnect;
- return($i,\@result);
+ return(scalar(@result), \@result);
}
=item allissues
#'
sub allissues {
my ($bornum,$order,$limit)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query;
$query="Select * from issues,biblio,items,biblioitems
where borrowernumber='$bornum' and
$i++;
}
$sth->finish;
- $dbh->disconnect;
return($i,\@result);
}
#'
sub borrdata2 {
my ($env,$bornum)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select count(*) from issues where borrowernumber='$bornum' and
returndate is NULL";
# print $query;
$sth->execute;
my $data3=$sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
}
#'
sub getboracctrecord {
my ($env,$params) = @_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my @acctlines;
my $numlines=0;
my $query= "Select * from accountlines where
-borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
+borrowernumber=? order by date desc,timestamp desc";
my $sth=$dbh->prepare($query);
# print $query;
- $sth->execute;
+ $sth->execute($params->{'borrowernumber'});
my $total=0;
while (my $data=$sth->fetchrow_hashref){
# if ($data->{'itemnumber'} ne ''){
# }
$acctlines[$numlines] = $data;
$numlines++;
- $total = $total+ $data->{'amountoutstanding'};
+ $total += $data->{'amountoutstanding'};
}
$sth->finish;
- $dbh->disconnect;
return ($numlines,\@acctlines,$total);
}
=cut
#'
+
+# FIXME - There's also a &C4::Biblio::itemcount.
+# Since they're all exported, acqui/acquire.pl doesn't compile with -w.
sub itemcount {
my ($env,$bibnum,$type)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select * from items where
biblionumber=$bibnum ";
if ($type ne 'intra'){
# $count+=$ocount;
$sth2->finish;
$sth->finish;
- $dbh->disconnect;
return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
}
#'
sub itemcount2 {
my ($env,$bibnum,$type)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="Select * from items,branches where
biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
if ($type ne 'intra'){
# sort for this?
if (my $data2=$sth2->fetchrow_hashref){
$counts{'not available'}++;
+ #my $x = "Not available, data2=" . (defined $data2? CGI::escapeHTML(cvs($data2)): "undef");
+ #$counts{$x}++; #XXX
} else {
$counts{$data->{'branchname'}}++;
}
}
$sth2->finish;
$sth->finish;
- $dbh->disconnect;
return (\%counts);
}
# rates, printers, stopwords, and perhaps others.
sub ItemType {
my ($type)=@_;
- my $dbh=C4Connect;
+ my $dbh = C4::Context->dbh;
my $query="select description from itemtypes where itemtype='$type'";
my $sth=$dbh->prepare($query);
$sth->execute;
my $dat=$sth->fetchrow_hashref;
$sth->finish;
- $dbh->disconnect;
return ($dat->{'description'});
}
#'
sub bibitems {
my ($bibnum) = @_;
- my $dbh = C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "SELECT biblioitems.*,
itemtypes.*,
MIN(items.itemlost) as itemlost,
$count++;
} # while
$sth->finish;
- $dbh->disconnect;
return($count, @results);
} # sub bibitems
sub barcodes{
#called from request.pl
my ($biblioitemnumber)=@_;
- my $dbh=C4Connect;
- my $query="SELECT barcode, itemlost FROM items
+ my $dbh = C4::Context->dbh;
+ my $query="SELECT barcode, itemlost, holdingbranch FROM items
WHERE biblioitemnumber = ?
AND (wthdrawn <> 1 OR wthdrawn IS NULL)";
my $sth=$dbh->prepare($query);
$i++;
}
$sth->finish;
- $dbh->disconnect;
return(@barcodes);
}
#'
sub getwebsites {
my ($biblionumber) = @_;
- my $dbh = C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "Select * from websites where biblionumber = $biblionumber";
my $sth = $dbh->prepare($query);
my $count = 0;
} # while
$sth->finish;
- $dbh->disconnect;
return($count, @results);
} # sub getwebsites
#'
sub getwebbiblioitems {
my ($biblionumber) = @_;
- my $dbh = C4Connect;
+ my $dbh = C4::Context->dbh;
my $query = "Select * from biblioitems where biblionumber = $biblionumber
and itemtype = 'WEB'";
my $sth = $dbh->prepare($query);
} # while
$sth->finish;
- $dbh->disconnect;
return($count, @results);
} # sub getwebbiblioitems
+=item breedingsearch
+
+ ($count, @results) = &breedingsearch($title);
+
+C<$count> is the number of items in C<@results>. C<@results> is an
+array of references-to-hash; the keys are the items from the
+C<marc_breeding> table of the Koha database.
+
+=cut
+
+sub breedingsearch {
+ my ($title,$isbn) = @_;
+ my $dbh = C4::Context->dbh;
+ my $count = 0;
+ my $query;
+ my $sth;
+ my @results;
+
+ $query = "Select id,file,isbn,title,author from marc_breeding where ";
+ if ($title) {
+ $query .= "title like \"$title%\"";
+ }
+ if ($title && $isbn) {
+ $query .= " and ";
+ }
+ if ($isbn) {
+ $query .= "isbn like \"$isbn%\"";
+ }
+ $sth = $dbh->prepare($query);
+ $sth->execute;
+ while (my $data = $sth->fetchrow_hashref) {
+ $results[$count] = $data;
+ $count++;
+ } # while
+
+ $sth->finish;
+ return($count, @results);
+} # sub breedingsearch
+
+=item isbnsearch
+
+ ($count, @results) = &isbnsearch($isbn,$title);
+
+Given an isbn and/or a title, returns the biblios having it.
+Used in acqui.simple, isbnsearch.pl only
+
+C<$count> is the number of items in C<@results>. C<@results> is an
+array of references-to-hash; the keys are the items from the
+C<biblioitems> table of the Koha database.
+
+=cut
+
+sub isbnsearch {
+ my ($isbn,$title) = @_;
+ my $dbh = C4::Context->dbh;
+ my $count = 0;
+ my $query;
+ my $sth;
+ my @results;
+
+ $query = "Select distinct biblio.* from biblio, biblioitems where
+ biblio.biblionumber = biblioitems.biblionumber";
+ if ($isbn) {
+ $query .= " and isbn=".$dbh->quote($isbn);
+ }
+ if ($title) {
+ $query .= " and title like ".$dbh->quote($title."%");
+ }
+ warn $query;
+ $sth = $dbh->prepare($query);
+
+ $sth->execute;
+ while (my $data = $sth->fetchrow_hashref) {
+ $results[$count] = $data;
+ $count++;
+ } # while
+
+ $sth->finish;
+ return($count, @results);
+} # sub isbnsearch
+
END { } # module clean-up code here (global destructor)
1;
Koha Developement team <info@koha.org>
-=head1 SEE ALSO
-
-L<perl>.
-
=cut