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
25 # FIXME - C4::Search uses C4::Reserves2, which uses C4::Search.
26 # So Perl complains that all of the functions here get redefined.
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
35 C4::Search - Functions for searching the Koha catalog and other databases
41 my ($count, @results) = catalogsearch($env, $type, $search, $num, $offset);
45 This module provides the searching facilities for the Koha catalog and
48 C<&catalogsearch> is a front end to all the other searches. Depending
49 on what is passed to it, it calls the appropriate search function.
58 @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
59 &itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
60 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
61 &getboracctrecord &ItemType &itemissues &subject &subtitle
62 &addauthor &bibitems &barcodes &findguarantees &allissues
63 &findguarantor &getwebsites &getwebbiblioitems &catalogsearch &itemcount2
64 &isbnsearch &breedingsearch);
65 # make all your functions, whether exported or not;
69 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
70 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
71 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
73 C<&findguarantees> takes a borrower number (e.g., that of a patron
74 with children) and looks up the borrowers who are guaranteed by that
75 borrower (i.e., the patron's children).
77 C<&findguarantees> returns two values: an integer giving the number of
78 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
79 of references to hash, which gives the actual results.
85 my $dbh = C4::Context->dbh;
86 my $query="select cardnumber,borrowernumber from borrowers where
88 my $sth=$dbh->prepare($query);
92 while (my $data = $sth->fetchrow_hashref)
97 return (scalar(@dat), \@dat);
102 $guarantor = &findguarantor($borrower_no);
103 $guarantor_cardno = $guarantor->{"cardnumber"};
104 $guarantor_surname = $guarantor->{"surname"};
107 C<&findguarantor> takes a borrower number (presumably that of a child
108 patron), finds the guarantor for C<$borrower_no> (the child's parent),
109 and returns the record for the guarantor.
111 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
112 from the C<borrowers> database table;
118 my $dbh = C4::Context->dbh;
119 my $query="select guarantor from borrowers where
120 borrowernumber='$bornum'";
121 my $sth=$dbh->prepare($query);
123 my $data=$sth->fetchrow_hashref;
125 $query="Select * from borrowers where
126 borrowernumber='$data->{'guarantor'}'";
127 $sth=$dbh->prepare($query);
129 $data=$sth->fetchrow_hashref;
134 =item NewBorrowerNumber
136 $num = &NewBorrowerNumber();
138 Allocates a new, unused borrower number, and returns it.
142 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
143 # Pick one and stick with it. Preferably use the other one. This function
144 # doesn't belong in C4::Search.
145 sub NewBorrowerNumber {
146 my $dbh = C4::Context->dbh;
147 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
149 my $data=$sth->fetchrow_hashref;
151 $data->{'max(borrowernumber)'}++;
152 return($data->{'max(borrowernumber)'});
157 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
159 This is primarily a front-end to other, more specialized catalog
160 search functions: if C<$search-E<gt>{itemnumber}> or
161 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
162 C<&CatSearch>. If $search->{subject} is given, it runs a subject
163 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
164 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
166 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
167 the items for each result, and adds several keys:
173 The total number of copies of this book.
175 =item C<locationhash>
177 This is a reference-to-hash; the keys are the names of branches where
178 this book may be found, and the values are the number of copies at
183 A descriptive string saying where the book is located, and how many
184 copies there are, if greater than 1.
188 The book's subject, with spaces replaced with C<%20>, presumably for
196 my ($env,$type,$search,$num,$offset)=@_;
197 my $dbh = C4::Context->dbh;
198 # foreach my $key (%$search){
199 # $search->{$key}=$dbh->quote($search->{$key});
201 my ($count,@results);
202 # print STDERR "Doing a search \n";
203 if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
204 print STDERR "Doing a precise search\n";
205 ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
206 } elsif ($search->{'subject'} ne ''){
207 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
208 } elsif ($search->{'keyword'} ne ''){
209 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
211 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
214 if ($env->{itemcount} eq '1') {
215 foreach my $data (@results){
216 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
217 my $subject2=$data->{'subject'};
218 $subject2=~ s/ /%20/g;
219 $data->{'itemcount'}=$counts->{'total'};
220 my $totalitemcounts=0;
221 foreach my $key (keys %$counts){
222 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
223 #$data->{'location'}.="$key $counts->{$key} ";
224 $totalitemcounts+=$counts->{$key};
225 $data->{'locationhash'}->{$key}=$counts->{$key};
229 my $notavailabletext='';
230 foreach (sort keys %{$data->{'locationhash'}}) {
231 if ($_ eq 'notavailable') {
232 $notavailabletext="Not available";
233 my $c=$data->{'locationhash'}->{$_};
234 if ($totalitemcounts>1) {
235 $notavailabletext.=" ($c)";
239 my $c=$data->{'locationhash'}->{$_};
240 if ($totalitemcounts>1) {
241 $locationtext.=" ($c), ";
245 if ($notavailabletext) {
246 $locationtext.=$notavailabletext;
248 $locationtext=~s/, $//;
250 $data->{'location'}=$locationtext;
251 $data->{'subject2'}=$subject2;
254 return ($count,@results);
259 $search = { "keyword" => "One or more keywords",
260 "class" => "VID|CD", # Limit search to fiction and CDs
263 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
265 C<&KeywordSearch> searches the catalog by keyword: given a string
266 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
267 keywords, it looks for books that contain any of those keywords in any
268 of a number of places.
270 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
271 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
274 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
275 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
276 etc.). In this case, the search will be restricted to just those
279 If C<$search-E<gt>{"class"}> is not specified, you may specify
280 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
281 particular Dewey Decimal Classification category. Setting
282 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
283 whereas setting it to "5" will return all books with Dewey code 5I<xx>
284 (Science and Mathematics).
286 C<$env> and C<$type> are ignored.
288 C<$offset> and C<$num> specify the subset of results to return.
289 C<$num> specifies the number of results to return, and C<$offset> is
290 the number of the first result. Thus, setting C<$offset> to 100 and
291 C<$num> to 5 will return results 100 through 104 inclusive.
296 my ($env,$type,$search,$num,$offset)=@_;
297 my $dbh = C4::Context->dbh;
298 $search->{'keyword'}=~ s/ +$//;
299 $search->{'keyword'}=~ s/'/\\'/;
300 my @key=split(' ',$search->{'keyword'});
301 # FIXME - Naive users might enter comma-separated
302 # words, e.g., "training, animal". Ought to cope with
306 my %biblionumbers; # Set of biblionumbers returned by the
309 # FIXME - Ought to filter the stopwords out of the list of keywords.
310 # @key = map { !defined($stopwords{$_}) } @key;
312 # FIXME - The way this code is currently set up, it looks for all of
313 # the keywords first in (title, notes, seriestitle), then in the
314 # subtitle, then in the subject. Thus, if you look for keywords
315 # "science fiction", this search won't find a book with
316 # title = "How to write fiction"
317 # subtitle = "A science-based approach"
318 # Is this the desired effect? If not, then the first SQL query
319 # should look in the biblio, subtitle, and subject tables all at
320 # once. The way the first query is built can accomodate this easily.
322 # Look for keywords in table 'biblio'.
324 # Build an SQL query that finds each of the keywords in any of the
325 # title, biblio.notes, or seriestitle. To do this, we'll build up an
326 # array of clauses, one for each keyword.
327 my $query; # The SQL query
328 my @clauses = (); # The search clauses
330 $query = <<EOT; # Beginning of the query
335 foreach my $keyword (@key)
337 my @subclauses = (); # Subclauses, one for each field we're
340 # For each field we're searching on, create a subclause that'll
341 # match the current keyword in the current field.
342 foreach my $field (qw(title notes seriestitle))
345 "$field LIKE '\Q$keyword\E%' OR $field LIKE '% \Q$keyword\E%'";
347 # (Yes, this could have been done as
348 # @subclauses = map {...} qw(field1 field2 ...)
349 # )but I think this way is more readable.
351 # Construct the current clause by joining the subclauses.
352 push @clauses, "(" . join(")\n\tOR (", @subclauses) . ")";
354 # Now join all of the clauses together and append to the query.
355 $query .= "(" . join(")\nAND (", @clauses) . ")";
357 # FIXME - Perhaps use $sth->bind_columns() ? Documented as the most
358 # efficient way to fetch data.
359 my $sth=$dbh->prepare($query);
361 while (my @res = $sth->fetchrow_array) {
364 $biblionumbers{$_} = 1; # Add these results to the set
369 # Now look for keywords in the 'bibliosubtitle' table.
371 # Again, we build a list of clauses from the keywords.
373 $query = "SELECT biblionumber FROM bibliosubtitle WHERE ";
374 foreach my $keyword (@key)
377 "subtitle LIKE '\Q$keyword\E%' OR subtitle like '% \Q$keyword\E%'";
379 $query .= "(" . join(") AND (", @clauses) . ")";
381 $sth=$dbh->prepare($query);
383 while (my @res = $sth->fetchrow_array) {
386 $biblionumbers{$_} = 1; # Add these results to the set
391 # Look for the keywords in the notes for individual items
392 # ('biblioitems.notes')
394 # Again, we build a list of clauses from the keywords.
396 $query = "SELECT biblionumber FROM biblioitems WHERE ";
397 foreach my $keyword (@key)
400 "notes LIKE '\Q$keyword\E%' OR notes like '% \Q$keyword\E%'";
402 $query .= "(" . join(") AND (", @clauses) . ")";
404 $sth=$dbh->prepare($query);
406 while (my @res = $sth->fetchrow_array) {
409 $biblionumbers{$_} = 1; # Add these results to the set
414 # Look for keywords in the 'bibliosubject' table.
416 # FIXME - The other queries look for words in the desired field that
417 # begin with the individual keywords the user entered. This one
418 # searches for the literal string the user entered. Is this the
420 # Note in particular that spaces are retained: if the user typed
422 # (with two spaces), this won't find the subject "science fiction"
423 # (one space). Likewise, a search for "%" will return absolutely
425 # If this isn't the desired effect, see the previous searches for
428 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
429 like '%$search->{'keyword'}%' group by biblionumber");
432 while (my @res = $sth->fetchrow_array) {
435 $biblionumbers{$_} = 1; # Add these results to the set
445 my @res = keys %biblionumbers;
449 # print "count $count";
450 if ($search->{'class'} ne ''){
452 my $query="select * from biblio,biblioitems where
453 biblio.biblionumber='$res[$i2]' and
454 biblio.biblionumber=biblioitems.biblionumber ";
455 if ($search->{'class'} ne ''){ # FIXME - Redundant
456 my @temp=split(/\|/,$search->{'class'});
458 $query.= "and ( itemtype='$temp[0]'";
459 for (my $i=1;$i<$count;$i++){
460 $query.=" or itemtype='$temp[$i]'";
464 my $sth=$dbh->prepare($query);
467 if (my $data2=$sth->fetchrow_hashref){
468 my $dewey= $data2->{'dewey'};
469 my $subclass=$data2->{'subclass'};
470 # FIXME - This next bit is bogus, because it assumes that the
471 # Dewey code is a floating-point number. It isn't. It's
472 # actually a string that mainly consists of numbers. In
473 # particular, "4" is not a valid Dewey code, although "004"
474 # is ("Data processing; Computer science"). Likewise, zeros
475 # after the decimal are significant ("575" is not the same as
476 # "575.0"; the latter is more specific). And "000" is a
477 # perfectly good Dewey code ("General works; computer
478 # science") and should not be interpreted to mean "this
479 # database entry does not have a Dewey code". That's what
482 ($dewey == 0) && ($dewey='');
483 ($dewey) && ($dewey.=" $subclass") ;
485 my $end=$offset +$num;
490 if ($i4 <=$end && $i4 > $offset){
491 $data2->{'dewey'}=$dewey;
494 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
497 # print "in here $i3<br>";
508 # $search->{'class'} was not specified
510 # FIXME - This is bogus: it makes a separate query for each
511 # biblioitem, and returns results in apparently random order. It'd
512 # be much better to combine all of the previous queries into one big
513 # one (building it up a little at a time, of course), and have that
514 # big query select all of the desired fields, instead of just
517 while ($i2 < $num && $i2 < $count){
518 my $query="select * from biblio,biblioitems where
519 biblio.biblionumber='$res[$i2+$offset]' and
520 biblio.biblionumber=biblioitems.biblionumber ";
522 if ($search->{'dewey'} ne ''){
523 $query.= "and (dewey like '$search->{'dewey'}%') ";
526 my $sth=$dbh->prepare($query);
529 if (my $data2=$sth->fetchrow_hashref){
530 my $dewey= $data2->{'dewey'};
531 my $subclass=$data2->{'subclass'};
533 ($dewey == 0) && ($dewey='');
534 ($dewey) && ($dewey.=" $subclass") ;
536 $data2->{'dewey'}=$dewey;
539 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
548 return($count,@res2);
552 my ($env,$type,$search,$num,$offset)=@_;
553 my $dbh = C4::Context->dbh;
554 $search->{'keyword'}=~ s/ +$//;
555 $search->{'keyword'}=~ s/'/\\'/;
556 my @key=split(' ',$search->{'keyword'});
560 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
561 biblio.biblionumber=biblioitems.biblionumber and
562 biblio.biblionumber=bibliosubtitle.biblionumber and
563 (((title like '$key[0]%' or title like '% $key[0]%')";
565 $query .= " and (title like '$key[$i]%' or title like '% $key[$i]%')";
568 $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
569 for ($i=1;$i<$count;$i++){
570 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
572 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
573 for ($i=1;$i<$count;$i++){
574 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
576 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
577 for ($i=1;$i<$count;$i++){
578 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
580 $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
581 for ($i=1;$i<$count;$i++){
582 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
584 if ($search->{'keyword'} =~ /new zealand/i){
585 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
586 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
587 or author like '% nz %' or author like '% nz')"
589 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
590 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
591 $search->{'keyword'} =~ / nz/i){
592 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
593 or title like '% new zealand' or subtitle like 'new zealand%' or
594 subtitle like '% new zealand %'
595 or subtitle like '% new zealand' or author like 'new zealand%'
596 or author like '% new zealand %' or author like '% new zealand' or
597 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
598 or seriestitle like '% new zealand')"
601 if ($search->{'class'} ne ''){
602 my @temp=split(/\|/,$search->{'class'});
604 $query.= "and ( itemtype='$temp[0]'";
605 for (my $i=1;$i<$count;$i++){
606 $query.=" or itemtype='$temp[$i]'";
610 if ($search->{'dewey'} ne ''){
611 $query.= "and (dewey like '$search->{'dewey'}%') ";
613 $query.="group by biblio.biblionumber";
614 #$query.=" order by author,title";
616 my $sth=$dbh->prepare($query);
619 while (my $data=$sth->fetchrow_hashref){
620 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
623 # my ($dewey, $subclass) = $sti->fetchrow;
624 my $dewey=$data->{'dewey'};
625 my $subclass=$data->{'subclass'};
627 ($dewey == 0) && ($dewey='');
628 ($dewey) && ($dewey.=" $subclass");
630 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
631 # print $results[$i];
635 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
636 like '%$search->{'keyword'}%' group by biblionumber");
638 while (my $data=$sth->fetchrow_hashref){
639 $query="Select * from biblio,biblioitems where
640 biblio.biblionumber=$data->{'biblionumber'} and
641 biblio.biblionumber=biblioitems.biblionumber ";
642 if ($search->{'class'} ne ''){
643 my @temp=split(/\|/,$search->{'class'});
645 $query.= " and ( itemtype='$temp[0]'";
646 for (my $i=1;$i<$count;$i++){
647 $query.=" or itemtype='$temp[$i]'";
652 if ($search->{'dewey'} ne ''){
653 $query.= "and (dewey like '$search->{'dewey'}%') ";
655 my $sth2=$dbh->prepare($query);
658 while (my $data2=$sth2->fetchrow_hashref){
659 my $dewey= $data2->{'dewey'};
660 my $subclass=$data2->{'subclass'};
662 ($dewey == 0) && ($dewey='');
663 ($dewey) && ($dewey.=" $subclass") ;
665 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
666 # print $results[$i];
672 @results=sort @results;
679 while ($i2 < $count){
680 if ($results[$i2] ne $res[$i-1]){
681 $res[$i]=$results[$i2];
689 while ($i2 < $num && $i2 < $count){
690 $res2[$i2]=$res[$i2+$offset];
702 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
704 C<&CatSearch> searches the Koha catalog. It returns a list whose first
705 element is the number of returned results, and whose subsequent
706 elements are the results themselves.
708 Each returned element is a reference-to-hash. Most of the keys are
709 simply the fields from the C<biblio> table in the Koha database, but
710 the following keys may also be present:
716 The book's illustrator.
726 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
727 high-level behavior of C<&CatSearch>, as described below.
729 In many cases, the description below says that a certain field in the
730 database must match the search string. In these cases, it means that
731 the beginning of some word in the field must match the search string.
732 Thus, an author search for "sm" will return books whose author is
733 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
734 does not occur at the beginning of a word.
736 Note that within each search mode, the criteria are and-ed together.
737 That is, if you perform a loose search on the author "Jerome" and the
738 title "Boat", the search will only return books by Jerome containing
741 It is not possible to cross modes, e.g., set the author to "Asimov"
742 and the subject to "Math" in hopes of finding books on math by Asimov.
746 If C<$type> is set to C<loose>, the following search criteria may be
751 =item C<$search-E<gt>{author}>
753 The search string is a space-separated list of words. Each word must
754 match either the C<author> or C<additionalauthors> field.
756 =item C<$search-E<gt>{title}>
758 Each word in the search string must match the book title. If no author
759 is specified, the book subtitle will also be searched.
761 =item C<$search-E<gt>{abstract}>
763 Searches for the given search string in the book's abstract.
765 =item C<$search-E<gt>{'date-before'}>
767 Searches for books whose copyright date matches the search string.
768 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
769 books written in 1985, and setting it to "198" will find books written
770 between 1980 and 1989.
772 =item C<$search-E<gt>{title}>
774 Searches by title are also affected by the value of
775 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
776 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
777 match the search string exactly (the subtitle is not searched).
779 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
780 each word in the search string must match the title, subtitle,
781 unititle, or series title.
783 =item C<$search-E<gt>{class}>
785 Restricts the search to certain item classes. The value of
786 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
787 Thus, setting it to "F" restricts the search to fiction, and setting
788 it to "CD|CAS" will only look in compact disks and cassettes.
790 =item C<$search-E<gt>{dewey}>
792 Searches for books whose Dewey Decimal Classification code matches the
793 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
794 search for all books in 5I<xx> (Science and mathematics), setting it
795 to "54" will search for all books in 54I<x> (Chemistry), and setting
796 it to "546" will search for books on inorganic chemistry.
798 =item C<$search-E<gt>{publisher}>
800 Searches for books whose publisher contains the search string (unlike
801 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
806 =head2 Subject search
808 If C<$type> is set to C<subject>, the following search criterion may
813 =item C<$search-E<gt>{subject}>
815 The search string is a space-separated list of words, each of which
816 must match the book's subject.
818 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
819 C<&CatSearch> will search for books whose subject is "New Zealand".
820 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
821 search for books on "nz" and "football", not books on "New Zealand"
826 =head2 Precise search
828 If C<$type> is set to C<precise>, the following search criteria may be
833 =item C<$search-E<gt>{item}>
835 Searches for books whose barcode exactly matches the search string.
837 =item C<$search-E<gt>{isbn}>
839 Searches for books whose ISBN exactly matches the search string.
843 For a loose search, if an author was specified, the results are
844 ordered by author and title. If no author was specified, the results
845 are ordered by title.
847 For other (non-loose) searches, if a subject was specified, the
848 results are ordered alphabetically by subject.
850 In all other cases (e.g., loose search by keyword), the results are
856 my ($env,$type,$search,$num,$offset)=@_;
858 my $dbh = C4::Context->dbh;
862 # Why not just use quotemeta to escape all questionable characters,
863 # not just single-quotes? Because that would also escape spaces,
864 # which would cause titles/authors/illustrators with a space to
865 # become unsearchable (Bug 197)
867 for my $field ('title', 'author', 'illustrator') {
868 $search->{$field} =~ s/['"]/\\\1/g;
871 my $title = lc($search->{'title'});
872 if ($type eq 'loose') {
873 if ($search->{'author'} ne ''){
874 my @key=split(' ',$search->{'author'});
877 $query="select *,biblio.author,biblio.biblionumber from
879 left join additionalauthors
880 on additionalauthors.biblionumber =biblio.biblionumber
882 ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
883 additionalauthors.author like '$key[0]%' or additionalauthors.author
888 biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
889 additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
894 if ($search->{'title'} ne ''){
895 my @key=split(' ',$search->{'title'});
898 $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
900 $query .= " and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
903 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
904 for ($i=1;$i<$count;$i++){
905 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
907 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
908 for ($i=1;$i<$count;$i++){
909 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
912 #$query=$query. " and (title like '%$search->{'title'}%'
913 #or seriestitle like '%$search->{'title'}%')";
915 if ($search->{'abstract'} ne ''){
916 $query.= " and (abstract like '%$search->{'abstract'}%')";
918 if ($search->{'date-before'} ne ''){
919 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
921 $query.=" group by biblio.biblionumber";
923 if ($search->{'title'} ne '') {
924 if ($search->{'ttype'} eq 'exact'){
925 $query="select * from biblio
927 (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
928 or biblio.unititle like '$search->{'title'} |%' or
929 biblio.unititle like '%| $search->{'title'} |%' or
930 biblio.unititle like '%| $search->{'title'}') or
931 (biblio.seriestitle = '$search->{'title'}' or
932 biblio.seriestitle like '$search->{'title'} |%' or
933 biblio.seriestitle like '%| $search->{'title'} |%' or
934 biblio.seriestitle like '%| $search->{'title'}')
937 my @key=split(' ',$search->{'title'});
940 $query="select biblio.biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp,subtitle from biblio
941 left join bibliosubtitle on
942 biblio.biblionumber=bibliosubtitle.biblionumber
944 (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
946 $query .= " and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
949 $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
950 for ($i=1;$i<$count;$i++){
951 $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
953 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
954 for ($i=1;$i<$count;$i++){
955 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
957 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
958 for ($i=1;$i<$count;$i++){
959 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
963 if ($search->{'abstract'} ne ''){
964 $query.= " and (abstract like '%$search->{'abstract'}%')";
966 if ($search->{'date-before'} ne ''){
967 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
969 } elsif ($search->{'class'} ne ''){
970 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
971 my @temp=split(/\|/,$search->{'class'});
973 $query.= " and ( itemtype='$temp[0]'";
974 for (my $i=1;$i<$count;$i++){
975 $query.=" or itemtype='$temp[$i]'";
978 if ($search->{'illustrator'} ne ''){
979 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
981 if ($search->{'dewey'} ne ''){
982 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
984 } elsif ($search->{'dewey'} ne ''){
985 $query="select * from biblioitems,biblio
986 where biblio.biblionumber=biblioitems.biblionumber
987 and biblioitems.dewey like '$search->{'dewey'}%'";
988 } elsif ($search->{'illustrator'} ne '') {
989 $query="select * from biblioitems,biblio
990 where biblio.biblionumber=biblioitems.biblionumber
991 and biblioitems.illus like '%".$search->{'illustrator'}."%'";
992 } elsif ($search->{'publisher'} ne ''){
993 $query.= "Select * from biblio,biblioitems where biblio.biblionumber
994 =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
995 } elsif ($search->{'abstract'} ne ''){
996 $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
997 } elsif ($search->{'date-before'} ne ''){
998 $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
1000 $query .=" group by biblio.biblionumber";
1003 if ($type eq 'subject'){
1004 # FIXME - Subject search is badly broken. The query defined by
1005 # $query returns a single item (the subject), but later code
1006 # expects a ref-to-hash with all sorts of stuff in it.
1007 # Also, the count of items (biblios?) with the given subject is
1010 my @key=split(' ',$search->{'subject'});
1013 $query="select distinct(subject) from bibliosubject where( subject like
1014 '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
1016 $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
1017 or subject like '% $key[$i]'
1018 or subject like '%($key[$i])%')";
1022 # FIXME - Wouldn't it be better to fix the database so that if a
1023 # book has a subject "NZ", then it also gets added the subject
1025 # This can also be generalized by adding a table of subject
1026 # synonyms to the database: just declare "NZ" to be a synonym for
1027 # "New Zealand", "SF" a synonym for both "Science fiction" and
1028 # "Fantastic fiction", etc.
1030 if (lc($search->{'subject'}) eq 'nz'){
1031 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1032 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1033 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1034 $query=~ s/ nz/ NEW ZEALAND/ig;
1035 $query=~ s/nz /NEW ZEALAND /ig;
1036 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1039 if ($type eq 'precise'){
1040 if ($search->{'itemnumber'} ne ''){
1041 $query="select * from items,biblio ";
1042 my $search2=uc $search->{'itemnumber'};
1043 $query=$query." where
1044 items.biblionumber=biblio.biblionumber
1045 and barcode='$search2'";
1048 if ($search->{'isbn'} ne ''){
1049 my $search2=uc $search->{'isbn'};
1050 my $query1 = "select * from biblioitems where isbn='$search2'";
1051 my $sth1=$dbh->prepare($query1);
1052 # print STDERR "$query1\n";
1055 while (my $data=$sth1->fetchrow_hashref) {
1056 $query="select * from biblioitems,biblio where
1057 biblio.biblionumber = $data->{'biblionumber'}
1058 and biblioitems.biblionumber = biblio.biblionumber";
1059 my $sth=$dbh->prepare($query);
1061 # FIXME - There's already a $data in this scope.
1062 my $data=$sth->fetchrow_hashref;
1063 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1064 # FIXME - The following assumes that the Dewey code is a
1065 # floating-point number. It isn't: it's a string.
1067 ($dewey == 0) && ($dewey='');
1068 ($dewey) && ($dewey.=" $subclass");
1069 $data->{'dewey'}=$dewey;
1070 $results[$i2]=$data;
1071 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1078 if ($type ne 'precise' && $type ne 'subject'){
1079 if ($search->{'author'} ne ''){
1080 $query .= " order by biblio.author,title";
1082 $query .= " order by title";
1085 if ($type eq 'subject'){
1086 $query .= " order by subject";
1089 my $sth=$dbh->prepare($query);
1093 my $limit= $num+$offset;
1094 while (my $data=$sth->fetchrow_hashref){
1095 my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
1096 if ($search->{'class'} ne ''){
1097 my @temp=split(/\|/,$search->{'class'});
1099 $query.= " and ( itemtype='$temp[0]'";
1100 for (my $i=1;$i<$count;$i++){
1101 $query.=" or itemtype='$temp[$i]'";
1105 if ($search->{'dewey'} ne ''){
1106 $query.=" and dewey='$search->{'dewey'}' ";
1108 if ($search->{'illustrator'} ne ''){
1109 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
1111 if ($search->{'publisher'} ne ''){
1112 $query.= " and (publishercode like '%$search->{'publisher'}%')";
1115 my $sti=$dbh->prepare($query);
1122 if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
1124 $dewey=$bibitemdata->{'dewey'};
1125 $subclass=$bibitemdata->{'subclass'};
1126 $publishercode=$bibitemdata->{'publishercode'};
1128 # print STDERR "$dewey $subclass $publishercode\n";
1129 # FIXME - The Dewey code is a string, not a number.
1131 ($dewey == 0) && ($dewey='');
1132 ($dewey) && ($dewey.=" $subclass");
1133 $data->{'dewey'}=$dewey;
1134 $data->{'publishercode'}=$publishercode;
1137 if ($count > $offset && $count <= $limit){
1146 return($count,@results);
1149 sub updatesearchstats{
1150 my ($dbh,$query)=@_;
1156 @results = &subsearch($env, $subject);
1158 Searches for books that have a subject that exactly matches
1161 C<&subsearch> returns an array of results. Each element of this array
1162 is a string, containing the book's title, author, and biblionumber,
1170 my ($env,$subject)=@_;
1171 my $dbh = C4::Context->dbh;
1172 $subject=$dbh->quote($subject);
1173 my $query="Select * from biblio,bibliosubject where
1174 biblio.biblionumber=bibliosubject.biblionumber and
1175 bibliosubject.subject=$subject group by biblio.biblionumber
1176 order by biblio.title";
1177 my $sth=$dbh->prepare($query);
1182 while (my $data=$sth->fetchrow_hashref){
1183 $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
1192 @results = &ItemInfo($env, $biblionumber, $type);
1194 Returns information about books with the given biblionumber.
1196 C<$type> may be either C<intra> or anything else. If it is not set to
1197 C<intra>, then the search will exclude lost, very overdue, and
1202 C<&ItemInfo> returns a list of references-to-hash. Each element
1203 contains a number of keys. Most of them are table items from the
1204 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1205 Koha database. Other keys include:
1209 =item C<$data-E<gt>{branchname}>
1211 The name (not the code) of the branch to which the book belongs.
1213 =item C<$data-E<gt>{datelastseen}>
1215 This is simply C<items.datelastseen>, except that while the date is
1216 stored in YYYY-MM-DD format in the database, here it is converted to
1217 DD/MM/YYYY format. A NULL date is returned as C<//>.
1219 =item C<$data-E<gt>{datedue}>
1221 =item C<$data-E<gt>{class}>
1223 This is the concatenation of C<biblioitems.classification>, the book's
1224 Dewey code, and C<biblioitems.subclass>.
1226 =item C<$data-E<gt>{ocount}>
1228 I think this is the number of copies of the book available.
1230 =item C<$data-E<gt>{order}>
1232 If this is set, it is set to C<One Order>.
1239 my ($env,$biblionumber,$type) = @_;
1240 my $dbh = C4::Context->dbh;
1241 my $query = "SELECT * FROM items, biblio, biblioitems, itemtypes
1242 WHERE items.biblionumber = ?
1243 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1244 AND biblioitems.itemtype = itemtypes.itemtype
1245 AND biblio.biblionumber = items.biblionumber";
1246 if ($type ne 'intra'){
1247 $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1248 or items.itemlost is NULL)
1249 and (wthdrawn <> 1 or wthdrawn is NULL)";
1251 $query .= " order by items.dateaccessioned desc";
1253 my $sth=$dbh->prepare($query);
1254 $sth->execute($biblionumber);
1258 while (my $data=$sth->fetchrow_hashref){
1259 my $iquery = "Select * from issues
1260 where itemnumber = '$data->{'itemnumber'}'
1261 and returndate is null";
1263 my $isth=$dbh->prepare($iquery);
1265 if (my $idata=$isth->fetchrow_hashref){
1266 # FIXME - The date ought to be properly parsed, and printed
1267 # according to local convention.
1268 my @temp=split('-',$idata->{'date_due'});
1269 $datedue = "$temp[2]/$temp[1]/$temp[0]";
1271 if ($data->{'itemlost'} eq '2'){
1272 $datedue='Very Overdue';
1274 if ($data->{'itemlost'} eq '1'){
1277 if ($data->{'wthdrawn'} eq '1'){
1278 $datedue="Cancelled";
1280 if ($datedue eq ''){
1281 $datedue="Available";
1282 my ($restype,$reserves)=CheckReserves($data->{'itemnumber'});
1288 #get branch information.....
1289 my $bquery = "SELECT * FROM branches
1290 WHERE branchcode = '$data->{'holdingbranch'}'";
1291 my $bsth=$dbh->prepare($bquery);
1293 if (my $bdata=$bsth->fetchrow_hashref){
1294 $data->{'branchname'} = $bdata->{'branchname'};
1297 my $class = $data->{'classification'};
1298 my $dewey = $data->{'dewey'};
1300 if ($dewey eq "000.") { $dewey = "";}; # FIXME - "000" is general
1301 # books about computer science
1302 if ($dewey < 10){$dewey='00'.$dewey;}
1303 if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
1310 $class .= $data->{'subclass'};
1312 # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
1313 # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
1314 # to leave it empty, rather than convert it to "//".
1315 # Also ideally this should use the local format for displaying dates.
1316 my @temp=split('-',$data->{'datelastseen'});
1317 my $date="$temp[2]/$temp[1]/$temp[0]";
1318 $data->{'datelastseen'}=$date;
1319 $data->{'datedue'}=$datedue;
1320 $data->{'class'}=$class;
1325 my $query2="Select * from aqorders where biblionumber=$biblionumber";
1326 my $sth2=$dbh->prepare($query2);
1330 if ($data=$sth2->fetchrow_hashref){
1331 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1333 $data->{'ocount'}=$ocount;
1334 $data->{'order'}="One Order";
1345 @results = &GetItems($env, $biblionumber);
1347 Returns information about books with the given biblionumber.
1351 C<&GetItems> returns an array of strings. Each element is a
1352 tab-separated list of values: biblioitemnumber, itemtype,
1353 classification, Dewey number, subclass, ISBN, volume, number, and
1356 Itemdata, in turn, is a string of the form
1357 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1358 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1364 my ($env,$biblionumber)=@_;
1365 #debug_msg($env,"GetItems");
1366 my $dbh = C4::Context->dbh;
1367 my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
1368 #debug_msg($env,$query);
1369 my $sth=$dbh->prepare($query);
1371 #debug_msg($env,"executed query");
1374 while (my $data=$sth->fetchrow_hashref) {
1375 #debug_msg($env,$data->{'biblioitemnumber'});
1376 my $dewey = $data->{'dewey'};
1378 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1379 $line .= "\t$data->{'classification'}\t$dewey";
1380 $line .= "\t$data->{'subclass'}\t$data->{isbn}";
1381 $line .= "\t$data->{'volume'}\t$data->{number}";
1382 my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
1384 while (my $idata = $isth->fetchrow_hashref) {
1385 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1386 if ($idata->{'notforloan'} == 1) {
1389 if ($idata->{'itemlost'} == 1) {
1392 $line .= "\t$iline";
1395 $results[$i] = $line;
1404 $item = &itemdata($barcode);
1406 Looks up the item with the given barcode, and returns a
1407 reference-to-hash containing information about that item. The keys of
1408 the hash are the fields from the C<items> and C<biblioitems> tables in
1415 my $dbh = C4::Context->dbh;
1416 my $query="Select * from items,biblioitems where barcode='$barcode'
1417 and items.biblioitemnumber=biblioitems.biblioitemnumber";
1419 my $sth=$dbh->prepare($query);
1421 my $data=$sth->fetchrow_hashref;
1428 $data = &bibdata($biblionumber, $type);
1430 Returns information about the book with the given biblionumber.
1432 C<$type> is ignored.
1434 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1435 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1438 In addition, C<$data-E<gt>{subject}> is the list of the book's
1439 subjects, separated by C<" , "> (space, comma, space).
1441 If there are multiple biblioitems with the given biblionumber, only
1442 the first one is considered.
1447 my ($bibnum, $type) = @_;
1448 my $dbh = C4::Context->dbh;
1449 my $query = "Select *, biblio.notes
1450 from biblio, biblioitems
1451 left join bibliosubtitle on
1452 biblio.biblionumber = bibliosubtitle.biblionumber
1453 where biblio.biblionumber = $bibnum
1454 and biblioitems.biblionumber = $bibnum";
1455 my $sth = $dbh->prepare($query);
1459 $data = $sth->fetchrow_hashref;
1462 $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
1463 $sth = $dbh->prepare($query);
1465 while (my $dat = $sth->fetchrow_hashref){
1466 $data->{'subject'} .= " , $dat->{'subject'}";
1475 $itemdata = &bibitemdata($biblioitemnumber);
1477 Looks up the biblioitem with the given biblioitemnumber. Returns a
1478 reference-to-hash. The keys are the fields from the C<biblio>,
1479 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1480 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1486 my $dbh = C4::Context->dbh;
1487 my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
1488 where biblio.biblionumber = biblioitems.biblionumber
1489 and biblioitemnumber = $bibitem
1490 and biblioitems.itemtype = itemtypes.itemtype";
1491 my $sth = $dbh->prepare($query);
1496 $data = $sth->fetchrow_hashref;
1504 ($count, $subjects) = &subject($biblionumber);
1506 Looks up the subjects of the book with the given biblionumber. Returns
1507 a two-element list. C<$subjects> is a reference-to-array, where each
1508 element is a subject of the book, and C<$count> is the number of
1509 elements in C<$subjects>.
1515 my $dbh = C4::Context->dbh;
1516 my $query="Select * from bibliosubject where biblionumber=$bibnum";
1517 my $sth=$dbh->prepare($query);
1521 while (my $data=$sth->fetchrow_hashref){
1526 return($i,\@results);
1531 ($count, $authors) = &addauthors($biblionumber);
1533 Looks up the additional authors for the book with the given
1536 Returns a two-element list. C<$authors> is a reference-to-array, where
1537 each element is an additional author, and C<$count> is the number of
1538 elements in C<$authors>.
1544 my $dbh = C4::Context->dbh;
1545 my $query="Select * from additionalauthors where biblionumber=$bibnum";
1546 my $sth=$dbh->prepare($query);
1550 while (my $data=$sth->fetchrow_hashref){
1555 return($i,\@results);
1560 ($count, $subtitles) = &subtitle($biblionumber);
1562 Looks up the subtitles for the book with the given biblionumber.
1564 Returns a two-element list. C<$subtitles> is a reference-to-array,
1565 where each element is a subtitle, and C<$count> is the number of
1566 elements in C<$subtitles>.
1572 my $dbh = C4::Context->dbh;
1573 my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1574 my $sth=$dbh->prepare($query);
1578 while (my $data=$sth->fetchrow_hashref){
1583 return($i,\@results);
1588 @issues = &itemissues($biblioitemnumber, $biblio);
1590 Looks up information about who has borrowed the bookZ<>(s) with the
1591 given biblioitemnumber.
1593 C<$biblio> is ignored.
1595 C<&itemissues> returns an array of references-to-hash. The keys
1596 include the fields from the C<items> table in the Koha database.
1597 Additional keys include:
1603 If the item is currently on loan, this gives the due date.
1605 If the item is not on loan, then this is either "Available" or
1606 "Cancelled", if the item has been withdrawn.
1610 If the item is currently on loan, this gives the card number of the
1611 patron who currently has the item.
1613 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1615 These give the timestamp for the last three times the item was
1618 =item C<card0>, C<card1>, C<card2>
1620 The card number of the last three patrons who borrowed this item.
1622 =item C<borrower0>, C<borrower1>, C<borrower2>
1624 The borrower number of the last three patrons who borrowed this item.
1631 my ($bibitem, $biblio)=@_;
1632 my $dbh = C4::Context->dbh;
1633 my $query = "Select * from items where
1634 items.biblioitemnumber = '$bibitem'";
1635 # FIXME - If this function die()s, the script will abort, and the
1636 # user won't get anything; depending on how far the script has
1637 # gotten, the user might get a blank page. It would be much better
1638 # to at least print an error message. The easiest way to do this
1639 # is to set $SIG{__DIE__}.
1640 my $sth = $dbh->prepare($query)
1641 || die $dbh->errstr;
1646 || die $sth->errstr;
1648 while (my $data = $sth->fetchrow_hashref) {
1649 # Find out who currently has this item.
1650 # FIXME - Wouldn't it be better to do this as a left join of
1651 # some sort? Currently, this code assumes that if
1652 # fetchrow_hashref() fails, then the book is on the shelf.
1653 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1654 # database server crash), not just because no items match the
1656 my $query2 = "select * from issues,borrowers
1657 where itemnumber = $data->{'itemnumber'}
1658 and returndate is NULL
1659 and issues.borrowernumber = borrowers.borrowernumber";
1660 my $sth2 = $dbh->prepare($query2);
1663 if (my $data2 = $sth2->fetchrow_hashref) {
1664 $data->{'date_due'} = $data2->{'date_due'};
1665 $data->{'card'} = $data2->{'cardnumber'};
1667 if ($data->{'wthdrawn'} eq '1') {
1668 $data->{'date_due'} = 'Cancelled';
1670 $data->{'date_due'} = 'Available';
1676 # Find the last 3 people who borrowed this item.
1677 $query2 = "select * from issues, borrowers
1678 where itemnumber = ?
1679 and issues.borrowernumber = borrowers.borrowernumber
1680 and returndate is not NULL
1681 order by returndate desc,timestamp desc";
1683 $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
1684 $sth2->execute($data->{'itemnumber'}) || die $sth2->errstr;
1685 for (my $i2 = 0; $i2 < 2; $i2++) { # FIXME : error if there is less than 3 pple borrowing this item
1686 if (my $data2 = $sth2->fetchrow_hashref) {
1687 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1688 $data->{"card$i2"} = $data2->{'cardnumber'};
1689 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1694 $results[$i] = $data;
1704 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1706 Looks up the item with the given biblioitemnumber.
1708 C<$env> and C<$dbh> are ignored.
1710 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1711 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1717 my ($env,$dbh,$itemnumber) = @_;
1718 $dbh = C4::Context->dbh;
1719 my $query="Select * from biblio,items,biblioitems
1720 where items.itemnumber = '$itemnumber'
1721 and biblio.biblionumber = items.biblionumber
1722 and biblioitems.biblioitemnumber = items.biblioitemnumber";
1723 my $sth=$dbh->prepare($query);
1726 my $data=$sth->fetchrow_hashref;
1733 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1735 Looks up patrons (borrowers) by name.
1737 C<$env> and C<$type> are ignored.
1739 C<$searchstring> is a space-separated list of search terms. Each term
1740 must match the beginning a borrower's surname, first name, or other
1743 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1744 reference-to-array; each element is a reference-to-hash, whose keys
1745 are the fields of the C<borrowers> table in the Koha database.
1746 C<$count> is the number of elements in C<$borrowers>.
1750 #used by member enquiries from the intranet
1751 #called by member.pl
1753 my ($env,$searchstring,$type)=@_;
1754 my $dbh = C4::Context->dbh;
1755 $searchstring=~ s/\'/\\\'/g;
1756 my @data=split(' ',$searchstring);
1758 my $query="Select * from borrowers
1759 where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\"
1760 or firstname like \"$data[0]%\" or firstname like \"% $data[0]%\"
1761 or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1763 for (my $i=1;$i<$count;$i++){
1764 $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"
1765 or firstname like \"$data[$i]%\" or firstname like \"% $data[$i]%\"
1766 or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1769 $query=$query.") or cardnumber = \"$searchstring\"
1770 order by surname,firstname";
1772 # print $query,"\n";
1773 my $sth=$dbh->prepare($query);
1777 while (my $data=$sth->fetchrow_hashref){
1778 push(@results,$data);
1783 return ($cnt,\@results);
1788 $borrower = &borrdata($cardnumber, $borrowernumber);
1790 Looks up information about a patron (borrower) by either card number
1791 or borrower number. If $borrowernumber is specified, C<&borrdata>
1792 searches by borrower number; otherwise, it searches by card number.
1794 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1795 the C<borrowers> table in the Koha database.
1800 my ($cardnumber,$bornum)=@_;
1801 $cardnumber = uc $cardnumber;
1802 my $dbh = C4::Context->dbh;
1805 $query="Select * from borrowers where cardnumber='$cardnumber'";
1807 $query="Select * from borrowers where borrowernumber='$bornum'";
1810 my $sth=$dbh->prepare($query);
1812 my $data=$sth->fetchrow_hashref;
1819 ($count, $issues) = &borrissues($borrowernumber);
1821 Looks up what the patron with the given borrowernumber has borrowed.
1823 C<&borrissues> returns a two-element array. C<$issues> is a
1824 reference-to-array, where each element is a reference-to-hash; the
1825 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1826 in the Koha database. C<$count> is the number of elements in
1833 my $dbh = C4::Context->dbh;
1835 $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1836 items.itemnumber=issues.itemnumber and
1837 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1840 my $sth=$dbh->prepare($query);
1843 while (my $data = $sth->fetchrow_hashref) {
1844 push @result, $data;
1847 return(scalar(@result), \@result);
1852 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1854 Looks up what the patron with the given borrowernumber has borrowed,
1855 and sorts the results.
1857 C<$sortkey> is the name of a field on which to sort the results. This
1858 should be the name of a field in the C<issues>, C<biblio>,
1859 C<biblioitems>, or C<items> table in the Koha database.
1861 C<$limit> is the maximum number of results to return.
1863 C<&allissues> returns a two-element array. C<$issues> is a
1864 reference-to-array, where each element is a reference-to-hash; the
1865 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1866 C<items> tables of the Koha database. C<$count> is the number of
1867 elements in C<$issues>
1872 my ($bornum,$order,$limit)=@_;
1873 my $dbh = C4::Context->dbh;
1875 $query="Select * from issues,biblio,items,biblioitems
1876 where borrowernumber='$bornum' and
1877 items.biblioitemnumber=biblioitems.biblioitemnumber and
1878 items.itemnumber=issues.itemnumber and
1879 items.biblionumber=biblio.biblionumber";
1880 $query.=" order by $order";
1882 $query.=" limit $limit";
1885 my $sth=$dbh->prepare($query);
1889 while (my $data=$sth->fetchrow_hashref){
1894 return($i,\@result);
1899 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
1901 Returns aggregate data about items borrowed by the patron with the
1902 given borrowernumber.
1906 C<&borrdata2> returns a three-element array. C<$borrowed> is the
1907 number of books the patron currently has borrowed. C<$due> is the
1908 number of overdue items the patron currently has borrowed. C<$fine> is
1909 the total fine currently due by the borrower.
1914 my ($env,$bornum)=@_;
1915 my $dbh = C4::Context->dbh;
1916 my $query="Select count(*) from issues where borrowernumber='$bornum' and
1917 returndate is NULL";
1919 my $sth=$dbh->prepare($query);
1921 my $data=$sth->fetchrow_hashref;
1923 $sth=$dbh->prepare("Select count(*) from issues where
1924 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1926 my $data2=$sth->fetchrow_hashref;
1928 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1929 borrowernumber='$bornum'");
1931 my $data3=$sth->fetchrow_hashref;
1934 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1937 =item getboracctrecord
1939 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1941 Looks up accounting data for the patron with the given borrowernumber.
1945 (FIXME - I'm not at all sure what this is about.)
1947 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1948 reference-to-array, where each element is a reference-to-hash; the
1949 keys are the fields of the C<accountlines> table in the Koha database.
1950 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1951 total amount outstanding for all of the account lines.
1955 sub getboracctrecord {
1956 my ($env,$params) = @_;
1957 my $dbh = C4::Context->dbh;
1960 my $query= "Select * from accountlines where
1961 borrowernumber=? order by date desc,timestamp desc";
1962 my $sth=$dbh->prepare($query);
1964 $sth->execute($params->{'borrowernumber'});
1966 while (my $data=$sth->fetchrow_hashref){
1967 # if ($data->{'itemnumber'} ne ''){
1968 # $query="Select * from items,biblio where items.itemnumber=
1969 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1970 # my $sth2=$dbh->prepare($query);
1972 # my $data2=$sth2->fetchrow_hashref;
1976 $acctlines[$numlines] = $data;
1978 $total += $data->{'amountoutstanding'};
1981 return ($numlines,\@acctlines,$total);
1986 ($count, $lcount, $nacount, $fcount, $scount, $lostcount,
1987 $mending, $transit,$ocount) =
1988 &itemcount($env, $biblionumber, $type);
1990 Counts the number of items with the given biblionumber, broken down by
1995 If C<$type> is not set to C<intra>, lost, very overdue, and withdrawn
1996 items will not be counted.
1998 C<&itemcount> returns a nine-element list:
2000 C<$count> is the total number of items with the given biblionumber.
2002 C<$lcount> is the number of items at the Levin branch.
2004 C<$nacount> is the number of items that are neither borrowed, lost,
2005 nor withdrawn (and are therefore presumably on a shelf somewhere).
2007 C<$fcount> is the number of items at the Foxton branch.
2009 C<$scount> is the number of items at the Shannon branch.
2011 C<$lostcount> is the number of lost and very overdue items.
2013 C<$mending> is the number of items at the Mending branch (being
2016 C<$transit> is the number of items at the Transit branch (in transit
2019 C<$ocount> is the number of items that haven't arrived yet
2020 (aqorders.quantity - aqorders.quantityreceived).
2025 # FIXME - There's also a &C4::Biblio::itemcount.
2026 # Since they're all exported, acqui/acquire.pl doesn't compile with -w.
2028 my ($env,$bibnum,$type)=@_;
2029 my $dbh = C4::Context->dbh;
2030 my $query="Select * from items where
2031 biblionumber=$bibnum ";
2032 if ($type ne 'intra'){
2033 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2034 (wthdrawn <> 1 or wthdrawn is NULL)";
2036 my $sth=$dbh->prepare($query);
2048 while (my $data=$sth->fetchrow_hashref){
2050 my $query2="select * from issues,items where issues.itemnumber=
2051 '$data->{'itemnumber'}' and returndate is NULL
2052 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2053 items.itemlost <> 2) or items.itemlost is NULL)
2054 and (wthdrawn <> 1 or wthdrawn is NULL)";
2056 my $sth2=$dbh->prepare($query2);
2058 if (my $data2=$sth2->fetchrow_hashref){
2061 if ($data->{'holdingbranch'} eq 'C' || $data->{'holdingbranch'} eq 'LT'){
2064 if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
2067 if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
2070 if ($data->{'itemlost'} eq '1'){
2073 if ($data->{'itemlost'} eq '2'){
2076 if ($data->{'holdingbranch'} eq 'FM'){
2079 if ($data->{'holdingbranch'} eq 'TR'){
2086 my $query2="Select * from aqorders where biblionumber=$bibnum";
2087 my $sth2=$dbh->prepare($query2);
2089 if (my $data=$sth2->fetchrow_hashref){
2090 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
2095 return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
2100 $counts = &itemcount2($env, $biblionumber, $type);
2102 Counts the number of items with the given biblionumber, broken down by
2107 C<$type> may be either C<intra> or anything else. If it is not set to
2108 C<intra>, then the search will exclude lost, very overdue, and
2111 C<$&itemcount2> returns a reference-to-hash, with the following fields:
2117 The total number of items with this biblionumber.
2121 The number of items on order (aqorders.quantity -
2122 aqorders.quantityreceived).
2126 For each branch that has at least one copy of the book, C<$counts>
2127 will have a key with the branch name, giving the number of copies at
2135 my ($env,$bibnum,$type)=@_;
2136 my $dbh = C4::Context->dbh;
2137 my $query="Select * from items,branches where
2138 biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
2139 if ($type ne 'intra'){
2140 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2141 (wthdrawn <> 1 or wthdrawn is NULL)";
2143 my $sth=$dbh->prepare($query);
2148 while (my $data=$sth->fetchrow_hashref){
2155 'select * from items
2157 and not ((items.itemlost <>1 and items.itemlost <> 2)
2158 or items.itemlost is NULL)'
2161 'select * from items
2162 where itemnumber=? and not (wthdrawn <> 1 or wthdrawn is NULL)'
2164 'On Loan', "select * from issues,items
2165 where issues.itemnumber=? and returndate is NULL
2166 and items.itemnumber=issues.itemnumber"
2169 my($testlabel, $query2) = @$test;
2171 my $sth2=$dbh->prepare($query2);
2172 $sth2->execute($data->{'itemnumber'});
2174 # FIXME - fetchrow_hashref() can fail for any number of reasons
2175 # (e.g., a database server crash). Perhaps use a left join of some
2177 $status = $testlabel if $sth2->fetchrow_hashref;
2179 last if defined $status;
2181 $status = $data->{'branchname'} unless defined $status;
2184 my $query2="Select * from aqorders where biblionumber=$bibnum and
2185 datecancellationprinted is NULL and quantity > quantityreceived";
2186 my $sth2=$dbh->prepare($query2);
2188 if (my $data=$sth2->fetchrow_hashref){
2189 $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
2198 $description = &ItemType($itemtype);
2200 Given an item type code, returns the description for that type.
2205 # FIXME - I'm pretty sure that after the initial setup, the list of
2206 # item types doesn't change very often. Hence, it seems slow and
2207 # inefficient to make yet another database call to look up information
2208 # that'll only change every few months or years.
2210 # Much better, I think, to automatically build a Perl file that can be
2211 # included in those scripts that require it, e.g.:
2212 # @itemtypes = qw( ART BCD CAS CD F ... );
2214 # ART => "Art Prints",
2215 # BCD => "CD-ROM from book",
2216 # CD => "Compact disc (WN)",
2217 # F => "Free Fiction",
2220 # The web server can then run a cron job to rebuild this file from the
2221 # database every hour or so.
2223 # The same thing goes for branches, book funds, book sellers, currency
2224 # rates, printers, stopwords, and perhaps others.
2227 my $dbh = C4::Context->dbh;
2228 my $query="select description from itemtypes where itemtype='$type'";
2229 my $sth=$dbh->prepare($query);
2231 my $dat=$sth->fetchrow_hashref;
2233 return ($dat->{'description'});
2238 ($count, @results) = &bibitems($biblionumber);
2240 Given the biblionumber for a book, C<&bibitems> looks up that book's
2241 biblioitems (different publications of the same book, the audio book
2242 and film versions, etc.).
2244 C<$count> is the number of elements in C<@results>.
2246 C<@results> is an array of references-to-hash; the keys are the fields
2247 of the C<biblioitems> and C<itemtypes> tables of the Koha database. In
2248 addition, C<itemlost> indicates the availability of the item: if it is
2249 "2", then all copies of the item are long overdue; if it is "1", then
2250 all copies are lost; otherwise, there is at least one copy available.
2256 my $dbh = C4::Context->dbh;
2257 my $query = "SELECT biblioitems.*,
2259 MIN(items.itemlost) as itemlost,
2260 MIN(items.dateaccessioned) as dateaccessioned
2261 FROM biblioitems, itemtypes, items
2262 WHERE biblioitems.biblionumber = ?
2263 AND biblioitems.itemtype = itemtypes.itemtype
2264 AND biblioitems.biblioitemnumber = items.biblioitemnumber
2265 GROUP BY items.biblioitemnumber";
2266 my $sth = $dbh->prepare($query);
2269 $sth->execute($bibnum);
2270 while (my $data = $sth->fetchrow_hashref) {
2271 $results[$count] = $data;
2275 return($count, @results);
2280 @barcodes = &barcodes($biblioitemnumber);
2282 Given a biblioitemnumber, looks up the corresponding items.
2284 Returns an array of references-to-hash; the keys are C<barcode> and
2287 The returned items include very overdue items, but not lost ones.
2292 #called from request.pl
2293 my ($biblioitemnumber)=@_;
2294 my $dbh = C4::Context->dbh;
2295 my $query="SELECT barcode, itemlost, holdingbranch FROM items
2296 WHERE biblioitemnumber = ?
2297 AND (wthdrawn <> 1 OR wthdrawn IS NULL)";
2298 my $sth=$dbh->prepare($query);
2299 $sth->execute($biblioitemnumber);
2302 while (my $data=$sth->fetchrow_hashref){
2303 $barcodes[$i]=$data;
2312 ($count, @websites) = &getwebsites($biblionumber);
2314 Looks up the web sites pertaining to the book with the given
2317 C<$count> is the number of elements in C<@websites>.
2319 C<@websites> is an array of references-to-hash; the keys are the
2320 fields from the C<websites> table in the Koha database.
2325 my ($biblionumber) = @_;
2326 my $dbh = C4::Context->dbh;
2327 my $query = "Select * from websites where biblionumber = $biblionumber";
2328 my $sth = $dbh->prepare($query);
2333 while (my $data = $sth->fetchrow_hashref) {
2334 # FIXME - The URL scheme shouldn't be stripped off, at least
2335 # not here, since it's part of the URL, and will be useful in
2336 # constructing a link to the site. If you don't want the user
2337 # to see the "http://" part, strip that off when building the
2339 $data->{'url'} =~ s/^http:\/\///; # FIXME - Leaning toothpick
2341 $results[$count] = $data;
2346 return($count, @results);
2349 =item getwebbiblioitems
2351 ($count, @results) = &getwebbiblioitems($biblionumber);
2353 Given a book's biblionumber, looks up the web versions of the book
2354 (biblioitems with itemtype C<WEB>).
2356 C<$count> is the number of items in C<@results>. C<@results> is an
2357 array of references-to-hash; the keys are the items from the
2358 C<biblioitems> table of the Koha database.
2362 sub getwebbiblioitems {
2363 my ($biblionumber) = @_;
2364 my $dbh = C4::Context->dbh;
2365 my $query = "Select * from biblioitems where biblionumber = $biblionumber
2366 and itemtype = 'WEB'";
2367 my $sth = $dbh->prepare($query);
2372 while (my $data = $sth->fetchrow_hashref) {
2373 $data->{'url'} =~ s/^http:\/\///;
2374 $results[$count] = $data;
2379 return($count, @results);
2380 } # sub getwebbiblioitems
2383 =item breedingsearch
2385 ($count, @results) = &breedingsearch($title);
2387 C<$count> is the number of items in C<@results>. C<@results> is an
2388 array of references-to-hash; the keys are the items from the
2389 C<marc_breeding> table of the Koha database.
2393 sub breedingsearch {
2394 my ($title,$isbn) = @_;
2395 my $dbh = C4::Context->dbh;
2401 $query = "Select id,file,isbn,title,author from marc_breeding where ";
2403 $query .= "title like \"$title%\"";
2405 if ($title && $isbn) {
2409 $query .= "isbn like \"$isbn%\"";
2411 $sth = $dbh->prepare($query);
2413 while (my $data = $sth->fetchrow_hashref) {
2414 $results[$count] = $data;
2419 return($count, @results);
2420 } # sub breedingsearch
2424 ($count, @results) = &isbnsearch($isbn,$title);
2426 Given an isbn and/or a title, returns the biblios having it.
2427 Used in acqui.simple, isbnsearch.pl only
2429 C<$count> is the number of items in C<@results>. C<@results> is an
2430 array of references-to-hash; the keys are the items from the
2431 C<biblioitems> table of the Koha database.
2436 my ($isbn,$title) = @_;
2437 my $dbh = C4::Context->dbh;
2443 $query = "Select distinct biblio.* from biblio, biblioitems where
2444 biblio.biblionumber = biblioitems.biblionumber";
2446 $query .= " and isbn=".$dbh->quote($isbn);
2449 $query .= " and title like ".$dbh->quote($title."%");
2452 $sth = $dbh->prepare($query);
2455 while (my $data = $sth->fetchrow_hashref) {
2456 $results[$count] = $data;
2461 return($count, @results);
2464 END { } # module clean-up code here (global destructor)
2473 Koha Developement team <info@koha.org>