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 # make all your functions, whether exported or not;
68 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
69 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
70 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
72 C<&findguarantees> takes a borrower number (e.g., that of a patron
73 with children) and looks up the borrowers who are guaranteed by that
74 borrower (i.e., the patron's children).
76 C<&findguarantees> returns two values: an integer giving the number of
77 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
78 of references to hash, which gives the actual results.
84 my $dbh = C4::Context->dbh;
85 my $query="select cardnumber,borrowernumber from borrowers where
87 my $sth=$dbh->prepare($query);
91 while (my $data = $sth->fetchrow_hashref)
96 return (scalar(@dat), \@dat);
101 $guarantor = &findguarantor($borrower_no);
102 $guarantor_cardno = $guarantor->{"cardnumber"};
103 $guarantor_surname = $guarantor->{"surname"};
106 C<&findguarantor> takes a borrower number (presumably that of a child
107 patron), finds the guarantor for C<$borrower_no> (the child's parent),
108 and returns the record for the guarantor.
110 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
111 from the C<borrowers> database table;
117 my $dbh = C4::Context->dbh;
118 my $query="select guarantor from borrowers where
119 borrowernumber='$bornum'";
120 my $sth=$dbh->prepare($query);
122 my $data=$sth->fetchrow_hashref;
124 $query="Select * from borrowers where
125 borrowernumber='$data->{'guarantor'}'";
126 $sth=$dbh->prepare($query);
128 $data=$sth->fetchrow_hashref;
133 =item NewBorrowerNumber
135 $num = &NewBorrowerNumber();
137 Allocates a new, unused borrower number, and returns it.
141 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
142 # Pick one and stick with it. Preferably use the other one. This function
143 # doesn't belong in C4::Search.
144 sub NewBorrowerNumber {
145 my $dbh = C4::Context->dbh;
146 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
148 my $data=$sth->fetchrow_hashref;
150 $data->{'max(borrowernumber)'}++;
151 return($data->{'max(borrowernumber)'});
156 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
158 This is primarily a front-end to other, more specialized catalog
159 search functions: if C<$search-E<gt>{itemnumber}> or
160 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
161 C<&CatSearch>. If $search->{subject} is given, it runs a subject
162 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
163 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
165 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
166 the items for each result, and adds several keys:
172 The total number of copies of this book.
174 =item C<locationhash>
176 This is a reference-to-hash; the keys are the names of branches where
177 this book may be found, and the values are the number of copies at
182 A descriptive string saying where the book is located, and how many
183 copies there are, if greater than 1.
187 The book's subject, with spaces replaced with C<%20>, presumably for
195 my ($env,$type,$search,$num,$offset)=@_;
196 my $dbh = C4::Context->dbh;
197 # foreach my $key (%$search){
198 # $search->{$key}=$dbh->quote($search->{$key});
200 my ($count,@results);
201 # print STDERR "Doing a search \n";
202 # FIXME - Use "elsif" to avoid this sort of deep nesting
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);
208 if ($search->{'subject'} ne ''){
209 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
211 if ($search->{'keyword'} ne ''){
212 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
214 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
219 if ($env->{itemcount} eq '1') {
220 foreach my $data (@results){
221 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
222 my $subject2=$data->{'subject'};
223 $subject2=~ s/ /%20/g;
224 $data->{'itemcount'}=$counts->{'total'};
225 my $totalitemcounts=0;
226 foreach my $key (keys %$counts){
227 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
228 #$data->{'location'}.="$key $counts->{$key} ";
229 $totalitemcounts+=$counts->{$key};
230 $data->{'locationhash'}->{$key}=$counts->{$key};
234 my $notavailabletext='';
235 foreach (sort keys %{$data->{'locationhash'}}) {
236 if ($_ eq 'notavailable') {
237 $notavailabletext="Not available";
238 my $c=$data->{'locationhash'}->{$_};
239 if ($totalitemcounts>1) {
240 $notavailabletext.=" ($c)";
244 my $c=$data->{'locationhash'}->{$_};
245 if ($totalitemcounts>1) {
246 $locationtext.=" ($c), ";
250 if ($notavailabletext) {
251 $locationtext.=$notavailabletext;
253 $locationtext=~s/, $//;
255 $data->{'location'}=$locationtext;
256 $data->{'subject2'}=$subject2;
259 return ($count,@results);
264 $search = { "keyword" => "One or more keywords",
265 "class" => "VID|CD", # Limit search to fiction and CDs
268 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
270 C<&KeywordSearch> searches the catalog by keyword: given a string
271 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
272 keywords, it looks for books that contain any of those keywords in any
273 of a number of places.
275 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
276 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
279 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
280 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
281 etc.). In this case, the search will be restricted to just those
284 If C<$search-E<gt>{"class"}> is not specified, you may specify
285 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
286 particular Dewey Decimal Classification category. Setting
287 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
288 whereas setting it to "5" will return all books with Dewey code 5I<xx>
289 (Science and Mathematics).
291 C<$env> and C<$type> are ignored.
293 C<$offset> and C<$num> specify the subset of results to return.
294 C<$num> specifies the number of results to return, and C<$offset> is
295 the number of the first result. Thus, setting C<$offset> to 100 and
296 C<$num> to 5 will return results 100 through 104 inclusive.
301 my ($env,$type,$search,$num,$offset)=@_;
302 my $dbh = C4::Context->dbh;
303 $search->{'keyword'}=~ s/ +$//;
304 $search->{'keyword'}=~ s/'/\\'/;
305 my @key=split(' ',$search->{'keyword'});
306 # FIXME - Naive users might enter comma-separated
307 # words, e.g., "training, animal". Ought to cope with
311 my %biblionumbers; # Set of biblionumbers returned by the
314 # FIXME - Ought to filter the stopwords out of the list of keywords.
315 # @key = map { !defined($stopwords{$_}) } @key;
317 # FIXME - The way this code is currently set up, it looks for all of
318 # the keywords first in (title, notes, seriestitle), then in the
319 # subtitle, then in the subject. Thus, if you look for keywords
320 # "science fiction", this search won't find a book with
321 # title = "How to write fiction"
322 # subtitle = "A science-based approach"
323 # Is this the desired effect? If not, then the first SQL query
324 # should look in the biblio, subtitle, and subject tables all at
325 # once. The way the first query is built can accomodate this easily.
327 # Look for keywords in table 'biblio'.
329 # Build an SQL query that finds each of the keywords in any of the
330 # title, biblio.notes, or seriestitle. To do this, we'll build up an
331 # array of clauses, one for each keyword.
332 my $query; # The SQL query
333 my @clauses = (); # The search clauses
335 $query = <<EOT; # Beginning of the query
340 foreach my $keyword (@key)
342 my @subclauses = (); # Subclauses, one for each field we're
345 # For each field we're searching on, create a subclause that'll
346 # match the current keyword in the current field.
347 foreach my $field (qw(title notes seriestitle))
350 "$field LIKE '\Q$keyword\E%' OR $field LIKE '% \Q$keyword\E%'";
352 # (Yes, this could have been done as
353 # @subclauses = map {...} qw(field1 field2 ...)
354 # )but I think this way is more readable.
356 # Construct the current clause by joining the subclauses.
357 push @clauses, "(" . join(")\n\tOR (", @subclauses) . ")";
359 # Now join all of the clauses together and append to the query.
360 $query .= "(" . join(")\nAND (", @clauses) . ")";
362 # FIXME - Perhaps use $sth->bind_columns() ? Documented as the most
363 # efficient way to fetch data.
364 my $sth=$dbh->prepare($query);
366 while (my @res = $sth->fetchrow_array) {
369 $biblionumbers{$_} = 1; # Add these results to the set
374 # Now look for keywords in the 'bibliosubtitle' table.
376 # Again, we build a list of clauses from the keywords.
378 $query = "SELECT biblionumber FROM bibliosubtitle WHERE ";
379 foreach my $keyword (@key)
382 "subtitle LIKE '\Q$keyword\E%' OR subtitle like '% \Q$keyword\E%'";
384 $query .= "(" . join(") AND (", @clauses) . ")";
386 $sth=$dbh->prepare($query);
388 while (my @res = $sth->fetchrow_array) {
391 $biblionumbers{$_} = 1; # Add these results to the set
396 # Look for the keywords in the notes for individual items
397 # ('biblioitems.notes')
399 # Again, we build a list of clauses from the keywords.
401 $query = "SELECT biblionumber FROM biblioitems WHERE ";
402 foreach my $keyword (@key)
405 "notes LIKE '\Q$keyword\E%' OR notes like '% \Q$keyword\E%'";
407 $query .= "(" . join(") AND (", @clauses) . ")";
409 $sth=$dbh->prepare($query);
411 while (my @res = $sth->fetchrow_array) {
414 $biblionumbers{$_} = 1; # Add these results to the set
419 # Look for keywords in the 'bibliosubject' table.
421 # FIXME - The other queries look for words in the desired field that
422 # begin with the individual keywords the user entered. This one
423 # searches for the literal string the user entered. Is this the
425 # Note in particular that spaces are retained: if the user typed
427 # (with two spaces), this won't find the subject "science fiction"
428 # (one space). Likewise, a search for "%" will return absolutely
430 # If this isn't the desired effect, see the previous searches for
433 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
434 like '%$search->{'keyword'}%' group by biblionumber");
437 while (my @res = $sth->fetchrow_array) {
440 $biblionumbers{$_} = 1; # Add these results to the set
450 my @res = keys %biblionumbers;
454 # print "count $count";
455 if ($search->{'class'} ne ''){
457 my $query="select * from biblio,biblioitems where
458 biblio.biblionumber='$res[$i2]' and
459 biblio.biblionumber=biblioitems.biblionumber ";
460 if ($search->{'class'} ne ''){ # FIXME - Redundant
461 my @temp=split(/\|/,$search->{'class'});
463 $query.= "and ( itemtype='$temp[0]'";
464 for (my $i=1;$i<$count;$i++){
465 $query.=" or itemtype='$temp[$i]'";
469 my $sth=$dbh->prepare($query);
472 if (my $data2=$sth->fetchrow_hashref){
473 my $dewey= $data2->{'dewey'};
474 my $subclass=$data2->{'subclass'};
475 # FIXME - This next bit is bogus, because it assumes that the
476 # Dewey code is a floating-point number. It isn't. It's
477 # actually a string that mainly consists of numbers. In
478 # particular, "4" is not a valid Dewey code, although "004"
479 # is ("Data processing; Computer science"). Likewise, zeros
480 # after the decimal are significant ("575" is not the same as
481 # "575.0"; the latter is more specific). And "000" is a
482 # perfectly good Dewey code ("General works; computer
483 # science") and should not be interpreted to mean "this
484 # database entry does not have a Dewey code". That's what
487 ($dewey == 0) && ($dewey='');
488 ($dewey) && ($dewey.=" $subclass") ;
490 my $end=$offset +$num;
495 if ($i4 <=$end && $i4 > $offset){
496 $data2->{'dewey'}=$dewey;
499 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
502 # print "in here $i3<br>";
513 # $search->{'class'} was not specified
515 # FIXME - This is bogus: it makes a separate query for each
516 # biblioitem, and returns results in apparently random order. It'd
517 # be much better to combine all of the previous queries into one big
518 # one (building it up a little at a time, of course), and have that
519 # big query select all of the desired fields, instead of just
522 while ($i2 < $num && $i2 < $count){
523 my $query="select * from biblio,biblioitems where
524 biblio.biblionumber='$res[$i2+$offset]' and
525 biblio.biblionumber=biblioitems.biblionumber ";
527 if ($search->{'dewey'} ne ''){
528 $query.= "and (dewey like '$search->{'dewey'}%') ";
531 my $sth=$dbh->prepare($query);
534 if (my $data2=$sth->fetchrow_hashref){
535 my $dewey= $data2->{'dewey'};
536 my $subclass=$data2->{'subclass'};
538 ($dewey == 0) && ($dewey='');
539 ($dewey) && ($dewey.=" $subclass") ;
541 $data2->{'dewey'}=$dewey;
544 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
553 return($count,@res2);
557 my ($env,$type,$search,$num,$offset)=@_;
558 my $dbh = C4::Context->dbh;
559 $search->{'keyword'}=~ s/ +$//;
560 $search->{'keyword'}=~ s/'/\\'/;
561 my @key=split(' ',$search->{'keyword'});
565 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
566 biblio.biblionumber=biblioitems.biblionumber and
567 biblio.biblionumber=bibliosubtitle.biblionumber and
568 (((title like '$key[0]%' or title like '% $key[0]%')";
570 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
573 $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
574 for ($i=1;$i<$count;$i++){
575 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
577 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
578 for ($i=1;$i<$count;$i++){
579 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
581 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
582 for ($i=1;$i<$count;$i++){
583 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
585 $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
586 for ($i=1;$i<$count;$i++){
587 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
589 if ($search->{'keyword'} =~ /new zealand/i){
590 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
591 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
592 or author like '% nz %' or author like '% nz')"
594 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
595 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
596 $search->{'keyword'} =~ / nz/i){
597 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
598 or title like '% new zealand' or subtitle like 'new zealand%' or
599 subtitle like '% new zealand %'
600 or subtitle like '% new zealand' or author like 'new zealand%'
601 or author like '% new zealand %' or author like '% new zealand' or
602 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
603 or seriestitle like '% new zealand')"
606 if ($search->{'class'} ne ''){
607 my @temp=split(/\|/,$search->{'class'});
609 $query.= "and ( itemtype='$temp[0]'";
610 for (my $i=1;$i<$count;$i++){
611 $query.=" or itemtype='$temp[$i]'";
615 if ($search->{'dewey'} ne ''){
616 $query.= "and (dewey like '$search->{'dewey'}%') ";
618 $query.="group by biblio.biblionumber";
619 #$query.=" order by author,title";
621 my $sth=$dbh->prepare($query);
624 while (my $data=$sth->fetchrow_hashref){
625 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
628 # my ($dewey, $subclass) = $sti->fetchrow;
629 my $dewey=$data->{'dewey'};
630 my $subclass=$data->{'subclass'};
632 ($dewey == 0) && ($dewey='');
633 ($dewey) && ($dewey.=" $subclass");
635 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
636 # print $results[$i];
640 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
641 like '%$search->{'keyword'}%' group by biblionumber");
643 while (my $data=$sth->fetchrow_hashref){
644 $query="Select * from biblio,biblioitems where
645 biblio.biblionumber=$data->{'biblionumber'} and
646 biblio.biblionumber=biblioitems.biblionumber ";
647 if ($search->{'class'} ne ''){
648 my @temp=split(/\|/,$search->{'class'});
650 $query.= " and ( itemtype='$temp[0]'";
651 for (my $i=1;$i<$count;$i++){
652 $query.=" or itemtype='$temp[$i]'";
657 if ($search->{'dewey'} ne ''){
658 $query.= "and (dewey like '$search->{'dewey'}%') ";
660 my $sth2=$dbh->prepare($query);
663 while (my $data2=$sth2->fetchrow_hashref){
664 my $dewey= $data2->{'dewey'};
665 my $subclass=$data2->{'subclass'};
667 ($dewey == 0) && ($dewey='');
668 ($dewey) && ($dewey.=" $subclass") ;
670 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
671 # print $results[$i];
677 @results=sort @results;
684 while ($i2 < $count){
685 if ($results[$i2] ne $res[$i-1]){
686 $res[$i]=$results[$i2];
694 while ($i2 < $num && $i2 < $count){
695 $res2[$i2]=$res[$i2+$offset];
707 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
709 C<&CatSearch> searches the Koha catalog. It returns a list whose first
710 element is the number of returned results, and whose subsequent
711 elements are the results themselves.
713 Each returned element is a reference-to-hash. Most of the keys are
714 simply the fields from the C<biblio> table in the Koha database, but
715 the following keys may also be present:
721 The book's illustrator.
731 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
732 high-level behavior of C<&CatSearch>, as described below.
734 In many cases, the description below says that a certain field in the
735 database must match the search string. In these cases, it means that
736 the beginning of some word in the field must match the search string.
737 Thus, an author search for "sm" will return books whose author is
738 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
739 does not occur at the beginning of a word.
741 Note that within each search mode, the criteria are and-ed together.
742 That is, if you perform a loose search on the author "Jerome" and the
743 title "Boat", the search will only return books by Jerome containing
746 It is not possible to cross modes, e.g., set the author to "Asimov"
747 and the subject to "Math" in hopes of finding books on math by Asimov.
751 If C<$type> is set to C<loose>, the following search criteria may be
756 =item C<$search-E<gt>{author}>
758 The search string is a space-separated list of words. Each word must
759 match either the C<author> or C<additionalauthors> field.
761 =item C<$search-E<gt>{title}>
763 Each word in the search string must match the book title. If no author
764 is specified, the book subtitle will also be searched.
766 =item C<$search-E<gt>{abstract}>
768 Searches for the given search string in the book's abstract.
770 =item C<$search-E<gt>{'date-before'}>
772 Searches for books whose copyright date matches the search string.
773 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
774 books written in 1985, and setting it to "198" will find books written
775 between 1980 and 1989.
777 =item C<$search-E<gt>{title}>
779 Searches by title are also affected by the value of
780 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
781 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
782 match the search string exactly (the subtitle is not searched).
784 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
785 each word in the search string must match the title, subtitle,
786 unititle, or series title.
788 =item C<$search-E<gt>{class}>
790 Restricts the search to certain item classes. The value of
791 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
792 Thus, setting it to "F" restricts the search to fiction, and setting
793 it to "CD|CAS" will only look in compact disks and cassettes.
795 =item C<$search-E<gt>{dewey}>
797 Searches for books whose Dewey Decimal Classification code matches the
798 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
799 search for all books in 5I<xx> (Science and mathematics), setting it
800 to "54" will search for all books in 54I<x> (Chemistry), and setting
801 it to "546" will search for books on inorganic chemistry.
803 =item C<$search-E<gt>{publisher}>
805 Searches for books whose publisher contains the search string (unlike
806 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
811 =head2 Subject search
813 If C<$type> is set to C<subject>, the following search criterion may
818 =item C<$search-E<gt>{subject}>
820 The search string is a space-separated list of words, each of which
821 must match the book's subject.
823 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
824 C<&CatSearch> will search for books whose subject is "New Zealand".
825 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
826 search for books on "nz" and "football", not books on "New Zealand"
831 =head2 Precise search
833 If C<$type> is set to C<precise>, the following search criteria may be
838 =item C<$search-E<gt>{item}>
840 Searches for books whose barcode exactly matches the search string.
842 =item C<$search-E<gt>{isbn}>
844 Searches for books whose ISBN exactly matches the search string.
848 For a loose search, if an author was specified, the results are
849 ordered by author and title. If no author was specified, the results
850 are ordered by title.
852 For other (non-loose) searches, if a subject was specified, the
853 results are ordered alphabetically by subject.
855 In all other cases (e.g., loose search by keyword), the results are
861 my ($env,$type,$search,$num,$offset)=@_;
862 my $dbh = C4::Context->dbh;
865 # FIXME - Why not just
866 # $search->{'title'} = quotemeta($search->{'title'})
867 # to escape all questionable characters, not just single-quotes?
868 $search->{'title'}=~ s/'/\\'/g;
869 $search->{'author'}=~ s/'/\\'/g;
870 $search->{'illustrator'}=~ s/'/\\'/g;
871 my $title = lc($search->{'title'});
873 if ($type eq 'loose') {
874 if ($search->{'author'} ne ''){
875 my @key=split(' ',$search->{'author'});
878 $query="select *,biblio.author,biblio.biblionumber from
880 left join additionalauthors
881 on additionalauthors.biblionumber =biblio.biblionumber
883 ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
884 additionalauthors.author like '$key[0]%' or additionalauthors.author
888 $query=$query." and (
889 biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
890 additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
895 if ($search->{'title'} ne ''){
896 my @key=split(' ',$search->{'title'});
899 $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
901 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
904 # $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')";
905 # for ($i=1;$i<$count;$i++){
906 # $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";
908 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
909 for ($i=1;$i<$count;$i++){
910 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
912 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
913 for ($i=1;$i<$count;$i++){
914 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
917 #$query=$query. " and (title like '%$search->{'title'}%'
918 #or seriestitle like '%$search->{'title'}%')";
920 if ($search->{'abstract'} ne ''){
921 $query.= " and (abstract like '%$search->{'abstract'}%')";
923 if ($search->{'date-before'} ne ''){
924 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
927 $query.=" group by biblio.biblionumber";
929 if ($search->{'title'} ne '') {
930 if ($search->{'ttype'} eq 'exact'){
931 $query="select * from biblio
933 (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
934 or biblio.unititle like '$search->{'title'} |%' or
935 biblio.unititle like '%| $search->{'title'} |%' or
936 biblio.unititle like '%| $search->{'title'}') or
937 (biblio.seriestitle = '$search->{'title'}' or
938 biblio.seriestitle like '$search->{'title'} |%' or
939 biblio.seriestitle like '%| $search->{'title'} |%' or
940 biblio.seriestitle like '%| $search->{'title'}')
943 my @key=split(' ',$search->{'title'});
946 $query="select * from biblio
947 left join bibliosubtitle on
948 biblio.biblionumber=bibliosubtitle.biblionumber
950 (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
952 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
955 $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
956 for ($i=1;$i<$count;$i++){
957 $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
959 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
960 for ($i=1;$i<$count;$i++){
961 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
963 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
964 for ($i=1;$i<$count;$i++){
965 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
969 if ($search->{'abstract'} ne ''){
970 $query.= " and (abstract like '%$search->{'abstract'}%')";
972 if ($search->{'date-before'} ne ''){
973 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
975 } elsif ($search->{'class'} ne ''){
976 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
977 my @temp=split(/\|/,$search->{'class'});
979 $query.= " and ( itemtype='$temp[0]'";
980 for (my $i=1;$i<$count;$i++){
981 $query.=" or itemtype='$temp[$i]'";
984 if ($search->{'illustrator'} ne ''){
985 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
987 if ($search->{'dewey'} ne ''){
988 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
990 } elsif ($search->{'dewey'} ne ''){
991 $query="select * from biblioitems,biblio
992 where biblio.biblionumber=biblioitems.biblionumber
993 and biblioitems.dewey like '$search->{'dewey'}%'";
994 } elsif ($search->{'illustrator'} ne '') {
995 $query="select * from biblioitems,biblio
996 where biblio.biblionumber=biblioitems.biblionumber
997 and biblioitems.illus like '%".$search->{'illustrator'}."%'";
998 } elsif ($search->{'publisher'} ne ''){
999 $query.= "Select * from biblio,biblioitems where biblio.biblionumber
1000 =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
1001 } elsif ($search->{'abstract'} ne ''){
1002 $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
1004 } elsif ($search->{'date-before'} ne ''){
1005 $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
1007 $query .=" group by biblio.biblionumber";
1010 if ($type eq 'subject'){
1011 # FIXME - Subject search is badly broken. The query defined by
1012 # $query returns a single item (the subject), but later code
1013 # expects a ref-to-hash with all sorts of stuff in it.
1014 # Also, the count of items (biblios?) with the given subject is
1017 my @key=split(' ',$search->{'subject'});
1020 $query="select distinct(subject) from bibliosubject where( subject like
1021 '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
1023 $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
1024 or subject like '% $key[$i]'
1025 or subject like '%($key[$i])%')";
1029 # FIXME - Wouldn't it be better to fix the database so that if a
1030 # book has a subject "NZ", then it also gets added the subject
1032 # This can also be generalized by adding a table of subject
1033 # synonyms to the database: just declare "NZ" to be a synonym for
1034 # "New Zealand", "SF" a synonym for both "Science fiction" and
1035 # "Fantastic fiction", etc.
1037 # FIXME - This can be rewritten as
1038 # if (lc($search->{"subject"}) eq "nz") {
1039 if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){
1040 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1041 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1042 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1043 $query=~ s/ nz/ NEW ZEALAND/ig;
1044 $query=~ s/nz /NEW ZEALAND /ig;
1045 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1048 if ($type eq 'precise'){
1050 if ($search->{'item'} ne ''){
1051 $query="select * from items,biblio ";
1052 my $search2=uc $search->{'item'};
1053 $query=$query." where
1054 items.biblionumber=biblio.biblionumber
1055 and barcode='$search2'";
1057 if ($search->{'isbn'} ne ''){
1058 my $search2=uc $search->{'isbn'};
1059 my $query1 = "select * from biblioitems where isbn='$search2'";
1060 my $sth1=$dbh->prepare($query1);
1061 # print STDERR "$query1\n";
1064 while (my $data=$sth1->fetchrow_hashref) {
1065 $query="select * from biblioitems,biblio where
1066 biblio.biblionumber = $data->{'biblionumber'}
1067 and biblioitems.biblionumber = biblio.biblionumber";
1068 my $sth=$dbh->prepare($query);
1070 # FIXME - There's already a $data in this scope.
1071 my $data=$sth->fetchrow_hashref;
1072 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1073 # FIXME - The following assumes that the Dewey code is a
1074 # floating-point number. It isn't: it's a string.
1076 ($dewey == 0) && ($dewey='');
1077 ($dewey) && ($dewey.=" $subclass");
1078 $data->{'dewey'}=$dewey;
1079 $results[$i2]=$data;
1080 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1088 if ($type ne 'precise' && $type ne 'subject'){
1089 if ($search->{'author'} ne ''){
1090 $query=$query." order by biblio.author,title";
1092 $query=$query." order by title";
1095 if ($type eq 'subject'){
1096 $query=$query." order by subject";
1099 #print STDERR "$query\n";
1100 my $sth=$dbh->prepare($query);
1104 my $limit= $num+$offset;
1105 while (my $data=$sth->fetchrow_hashref){
1106 my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
1107 if ($search->{'class'} ne ''){
1108 my @temp=split(/\|/,$search->{'class'});
1110 $query.= " and ( itemtype='$temp[0]'";
1111 for (my $i=1;$i<$count;$i++){
1112 $query.=" or itemtype='$temp[$i]'";
1116 if ($search->{'dewey'} ne ''){
1117 $query.=" and dewey='$search->{'dewey'}' ";
1119 if ($search->{'illustrator'} ne ''){
1120 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
1122 if ($search->{'publisher'} ne ''){
1123 $query.= " and (publishercode like '%$search->{'publisher'}%')";
1126 my $sti=$dbh->prepare($query);
1133 if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
1135 $dewey=$bibitemdata->{'dewey'};
1136 $subclass=$bibitemdata->{'subclass'};
1137 $publishercode=$bibitemdata->{'publishercode'};
1139 # print STDERR "$dewey $subclass $publishercode\n";
1140 # FIXME - The Dewey code is a string, not a number.
1142 ($dewey == 0) && ($dewey='');
1143 ($dewey) && ($dewey.=" $subclass");
1144 $data->{'dewey'}=$dewey;
1145 $data->{'publishercode'}=$publishercode;
1148 if ($count > $offset && $count <= $limit){
1156 #if ($type ne 'precise'){
1160 return($count,@results);
1163 sub updatesearchstats{
1164 my ($dbh,$query)=@_;
1170 @results = &subsearch($env, $subject);
1172 Searches for books that have a subject that exactly matches
1175 C<&subsearch> returns an array of results. Each element of this array
1176 is a string, containing the book's title, author, and biblionumber,
1184 my ($env,$subject)=@_;
1185 my $dbh = C4::Context->dbh;
1186 $subject=$dbh->quote($subject);
1187 my $query="Select * from biblio,bibliosubject where
1188 biblio.biblionumber=bibliosubject.biblionumber and
1189 bibliosubject.subject=$subject group by biblio.biblionumber
1190 order by biblio.title";
1191 my $sth=$dbh->prepare($query);
1196 while (my $data=$sth->fetchrow_hashref){
1197 $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
1206 @results = &ItemInfo($env, $biblionumber, $type);
1208 Returns information about books with the given biblionumber.
1210 C<$type> may be either C<intra> or anything else. If it is not set to
1211 C<intra>, then the search will exclude lost, very overdue, and
1216 C<&ItemInfo> returns a list of references-to-hash. Each element
1217 contains a number of keys. Most of them are table items from the
1218 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1219 Koha database. Other keys include:
1223 =item C<$data-E<gt>{branchname}>
1225 The name (not the code) of the branch to which the book belongs.
1227 =item C<$data-E<gt>{datelastseen}>
1229 This is simply C<items.datelastseen>, except that while the date is
1230 stored in YYYY-MM-DD format in the database, here it is converted to
1231 DD/MM/YYYY format. A NULL date is returned as C<//>.
1233 =item C<$data-E<gt>{datedue}>
1235 =item C<$data-E<gt>{class}>
1237 This is the concatenation of C<biblioitems.classification>, the book's
1238 Dewey code, and C<biblioitems.subclass>.
1240 =item C<$data-E<gt>{ocount}>
1242 I think this is the number of copies of the book available.
1244 =item C<$data-E<gt>{order}>
1246 If this is set, it is set to C<One Order>.
1253 my ($env,$biblionumber,$type) = @_;
1254 my $dbh = C4::Context->dbh;
1255 my $query = "SELECT * FROM items, biblio, biblioitems, itemtypes
1256 WHERE items.biblionumber = ?
1257 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1258 AND biblioitems.itemtype = itemtypes.itemtype
1259 AND biblio.biblionumber = items.biblionumber";
1260 if ($type ne 'intra'){
1261 $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1262 or items.itemlost is NULL)
1263 and (wthdrawn <> 1 or wthdrawn is NULL)";
1265 $query .= " order by items.dateaccessioned desc";
1267 my $sth=$dbh->prepare($query);
1268 $sth->execute($biblionumber);
1272 while (my $data=$sth->fetchrow_hashref){
1273 my $iquery = "Select * from issues
1274 where itemnumber = '$data->{'itemnumber'}'
1275 and returndate is null";
1277 my $isth=$dbh->prepare($iquery);
1279 if (my $idata=$isth->fetchrow_hashref){
1280 # FIXME - The date ought to be properly parsed, and printed
1281 # according to local convention.
1282 my @temp=split('-',$idata->{'date_due'});
1283 $datedue = "$temp[2]/$temp[1]/$temp[0]";
1285 if ($data->{'itemlost'} eq '2'){
1286 $datedue='Very Overdue';
1288 if ($data->{'itemlost'} eq '1'){
1291 if ($data->{'wthdrawn'} eq '1'){
1292 $datedue="Cancelled";
1294 if ($datedue eq ''){
1295 $datedue="Available";
1296 my ($restype,$reserves)=CheckReserves($data->{'itemnumber'});
1302 #get branch information.....
1303 my $bquery = "SELECT * FROM branches
1304 WHERE branchcode = '$data->{'holdingbranch'}'";
1305 my $bsth=$dbh->prepare($bquery);
1307 if (my $bdata=$bsth->fetchrow_hashref){
1308 $data->{'branchname'} = $bdata->{'branchname'};
1311 my $class = $data->{'classification'};
1312 my $dewey = $data->{'dewey'};
1314 if ($dewey eq "000.") { $dewey = "";}; # FIXME - "000" is general
1315 # books about computer science
1316 if ($dewey < 10){$dewey='00'.$dewey;}
1317 if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
1322 $class = $class.$dewey;
1324 $class = $class.$data->{'subclass'};
1326 # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
1327 # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
1328 # to leave it empty, rather than convert it to "//".
1329 # Also ideally this should use the local format for displaying dates.
1330 my @temp=split('-',$data->{'datelastseen'});
1331 my $date="$temp[2]/$temp[1]/$temp[0]";
1332 $data->{'datelastseen'}=$date;
1333 $data->{'datedue'}=$datedue;
1334 $data->{'class'}=$class;
1339 my $query2="Select * from aqorders where biblionumber=$biblionumber";
1340 my $sth2=$dbh->prepare($query2);
1344 if ($data=$sth2->fetchrow_hashref){
1345 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1347 $data->{'ocount'}=$ocount;
1348 $data->{'order'}="One Order";
1359 @results = &GetItems($env, $biblionumber);
1361 Returns information about books with the given biblionumber.
1365 C<&GetItems> returns an array of strings. Each element is a
1366 tab-separated list of values: biblioitemnumber, itemtype,
1367 classification, Dewey number, subclass, ISBN, volume, number, and
1370 Itemdata, in turn, is a string of the form
1371 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1372 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1378 my ($env,$biblionumber)=@_;
1379 #debug_msg($env,"GetItems");
1380 my $dbh = C4::Context->dbh;
1381 my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
1382 #debug_msg($env,$query);
1383 my $sth=$dbh->prepare($query);
1385 #debug_msg($env,"executed query");
1388 while (my $data=$sth->fetchrow_hashref) {
1389 #debug_msg($env,$data->{'biblioitemnumber'});
1390 my $dewey = $data->{'dewey'};
1392 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1393 $line = $line."\t$data->{'classification'}\t$dewey";
1394 $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
1395 $line = $line."\t$data->{'volume'}\t$data->{number}";
1396 my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
1398 while (my $idata = $isth->fetchrow_hashref) {
1399 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1400 if ($idata->{'notforloan'} == 1) {
1401 $iline = $iline."NFL ";
1403 if ($idata->{'itemlost'} == 1) {
1404 $iline = $iline."LOST ";
1406 $line = $line."\t$iline";
1409 $results[$i] = $line;
1418 $item = &itemdata($barcode);
1420 Looks up the item with the given barcode, and returns a
1421 reference-to-hash containing information about that item. The keys of
1422 the hash are the fields from the C<items> and C<biblioitems> tables in
1429 my $dbh = C4::Context->dbh;
1430 my $query="Select * from items,biblioitems where barcode='$barcode'
1431 and items.biblioitemnumber=biblioitems.biblioitemnumber";
1433 my $sth=$dbh->prepare($query);
1435 my $data=$sth->fetchrow_hashref;
1442 $data = &bibdata($biblionumber, $type);
1444 Returns information about the book with the given biblionumber.
1446 C<$type> is ignored.
1448 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1449 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1452 In addition, C<$data-E<gt>{subject}> is the list of the book's
1453 subjects, separated by C<" , "> (space, comma, space).
1455 If there are multiple biblioitems with the given biblionumber, only
1456 the first one is considered.
1461 my ($bibnum, $type) = @_;
1462 my $dbh = C4::Context->dbh;
1463 my $query = "Select *, biblio.notes
1464 from biblio, biblioitems
1465 left join bibliosubtitle on
1466 biblio.biblionumber = bibliosubtitle.biblionumber
1467 where biblio.biblionumber = $bibnum
1468 and biblioitems.biblionumber = $bibnum";
1469 my $sth = $dbh->prepare($query);
1473 $data = $sth->fetchrow_hashref;
1476 $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
1477 $sth = $dbh->prepare($query);
1479 while (my $dat = $sth->fetchrow_hashref){
1480 $data->{'subject'} .= " , $dat->{'subject'}";
1489 $itemdata = &bibitemdata($biblioitemnumber);
1491 Looks up the biblioitem with the given biblioitemnumber. Returns a
1492 reference-to-hash. The keys are the fields from the C<biblio>,
1493 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1494 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1500 my $dbh = C4::Context->dbh;
1501 my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
1502 where biblio.biblionumber = biblioitems.biblionumber
1503 and biblioitemnumber = $bibitem
1504 and biblioitems.itemtype = itemtypes.itemtype";
1505 my $sth = $dbh->prepare($query);
1510 $data = $sth->fetchrow_hashref;
1518 ($count, $subjects) = &subject($biblionumber);
1520 Looks up the subjects of the book with the given biblionumber. Returns
1521 a two-element list. C<$subjects> is a reference-to-array, where each
1522 element is a subject of the book, and C<$count> is the number of
1523 elements in C<$subjects>.
1529 my $dbh = C4::Context->dbh;
1530 my $query="Select * from bibliosubject where biblionumber=$bibnum";
1531 my $sth=$dbh->prepare($query);
1535 while (my $data=$sth->fetchrow_hashref){
1540 return($i,\@results);
1545 ($count, $authors) = &addauthors($biblionumber);
1547 Looks up the additional authors for the book with the given
1550 Returns a two-element list. C<$authors> is a reference-to-array, where
1551 each element is an additional author, and C<$count> is the number of
1552 elements in C<$authors>.
1558 my $dbh = C4::Context->dbh;
1559 my $query="Select * from additionalauthors where biblionumber=$bibnum";
1560 my $sth=$dbh->prepare($query);
1564 while (my $data=$sth->fetchrow_hashref){
1569 return($i,\@results);
1574 ($count, $subtitles) = &subtitle($biblionumber);
1576 Looks up the subtitles for the book with the given biblionumber.
1578 Returns a two-element list. C<$subtitles> is a reference-to-array,
1579 where each element is a subtitle, and C<$count> is the number of
1580 elements in C<$subtitles>.
1586 my $dbh = C4::Context->dbh;
1587 my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1588 my $sth=$dbh->prepare($query);
1592 while (my $data=$sth->fetchrow_hashref){
1597 return($i,\@results);
1602 @issues = &itemissues($biblioitemnumber, $biblio);
1604 Looks up information about who has borrowed the bookZ<>(s) with the
1605 given biblioitemnumber.
1607 C<$biblio> is ignored.
1609 C<&itemissues> returns an array of references-to-hash. The keys
1610 include the fields from the C<items> table in the Koha database.
1611 Additional keys include:
1617 If the item is currently on loan, this gives the due date.
1619 If the item is not on loan, then this is either "Available" or
1620 "Cancelled", if the item has been withdrawn.
1624 If the item is currently on loan, this gives the card number of the
1625 patron who currently has the item.
1627 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1629 These give the timestamp for the last three times the item was
1632 =item C<card0>, C<card1>, C<card2>
1634 The card number of the last three patrons who borrowed this item.
1636 =item C<borrower0>, C<borrower1>, C<borrower2>
1638 The borrower number of the last three patrons who borrowed this item.
1645 my ($bibitem, $biblio)=@_;
1646 my $dbh = C4::Context->dbh;
1647 my $query = "Select * from items where
1648 items.biblioitemnumber = '$bibitem'";
1649 # FIXME - If this function die()s, the script will abort, and the
1650 # user won't get anything; depending on how far the script has
1651 # gotten, the user might get a blank page. It would be much better
1652 # to at least print an error message. The easiest way to do this
1653 # is to set $SIG{__DIE__}.
1654 my $sth = $dbh->prepare($query)
1655 || die $dbh->errstr;
1660 || die $sth->errstr;
1662 while (my $data = $sth->fetchrow_hashref) {
1663 # Find out who currently has this item.
1664 # FIXME - Wouldn't it be better to do this as a left join of
1665 # some sort? Currently, this code assumes that if
1666 # fetchrow_hashref() fails, then the book is on the shelf.
1667 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1668 # database server crash), not just because no items match the
1670 my $query2 = "select * from issues,borrowers
1671 where itemnumber = $data->{'itemnumber'}
1672 and returndate is NULL
1673 and issues.borrowernumber = borrowers.borrowernumber";
1674 my $sth2 = $dbh->prepare($query2);
1677 if (my $data2 = $sth2->fetchrow_hashref) {
1678 $data->{'date_due'} = $data2->{'date_due'};
1679 $data->{'card'} = $data2->{'cardnumber'};
1681 if ($data->{'wthdrawn'} eq '1') {
1682 $data->{'date_due'} = 'Cancelled';
1684 $data->{'date_due'} = 'Available';
1690 # Find the last 3 people who borrowed this item.
1691 $query2 = "select * from issues, borrowers
1692 where itemnumber = '$data->{'itemnumber'}'
1693 and issues.borrowernumber = borrowers.borrowernumber
1694 and returndate is not NULL
1695 order by returndate desc,timestamp desc";
1696 $sth2 = $dbh->prepare($query2)
1697 || die $dbh->errstr;
1699 || die $sth2->errstr;
1701 for (my $i2 = 0; $i2 < 2; $i2++) {
1702 if (my $data2 = $sth2->fetchrow_hashref) {
1703 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1704 $data->{"card$i2"} = $data2->{'cardnumber'};
1705 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1710 $results[$i] = $data;
1720 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1722 Looks up the item with the given biblioitemnumber.
1724 C<$env> and C<$dbh> are ignored.
1726 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1727 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1733 my ($env,$dbh,$itemnumber) = @_;
1734 $dbh = C4::Context->dbh;
1735 my $query="Select * from biblio,items,biblioitems
1736 where items.itemnumber = '$itemnumber'
1737 and biblio.biblionumber = items.biblionumber
1738 and biblioitems.biblioitemnumber = items.biblioitemnumber";
1739 my $sth=$dbh->prepare($query);
1742 my $data=$sth->fetchrow_hashref;
1749 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1751 Looks up patrons (borrowers) by name.
1753 C<$env> and C<$type> are ignored.
1755 C<$searchstring> is a space-separated list of search terms. Each term
1756 must match the beginning a borrower's surname, first name, or other
1759 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1760 reference-to-array; each element is a reference-to-hash, whose keys
1761 are the fields of the C<borrowers> table in the Koha database.
1762 C<$count> is the number of elements in C<$borrowers>.
1766 #used by member enquiries from the intranet
1767 #called by member.pl
1769 my ($env,$searchstring,$type)=@_;
1770 my $dbh = C4::Context->dbh;
1771 $searchstring=~ s/\'/\\\'/g;
1772 my @data=split(' ',$searchstring);
1774 my $query="Select * from borrowers
1775 where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\"
1776 or firstname like \"$data[0]%\" or firstname like \"% $data[0]%\"
1777 or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1779 for (my $i=1;$i<$count;$i++){
1780 $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"
1781 or firstname like \"$data[$i]%\" or firstname like \"% $data[$i]%\"
1782 or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1784 $query=$query.") or cardnumber = \"$searchstring\"
1785 order by surname,firstname";
1786 # print $query,"\n";
1787 my $sth=$dbh->prepare($query);
1791 while (my $data=$sth->fetchrow_hashref){
1792 push(@results,$data);
1797 return ($cnt,\@results);
1802 $borrower = &borrdata($cardnumber, $borrowernumber);
1804 Looks up information about a patron (borrower) by either card number
1805 or borrower number. If $borrowernumber is specified, C<&borrdata>
1806 searches by borrower number; otherwise, it searches by card number.
1808 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1809 the C<borrowers> table in the Koha database.
1814 my ($cardnumber,$bornum)=@_;
1815 $cardnumber = uc $cardnumber;
1816 my $dbh = C4::Context->dbh;
1819 $query="Select * from borrowers where cardnumber='$cardnumber'";
1821 $query="Select * from borrowers where borrowernumber='$bornum'";
1824 my $sth=$dbh->prepare($query);
1826 my $data=$sth->fetchrow_hashref;
1833 ($count, $issues) = &borrissues($borrowernumber);
1835 Looks up what the patron with the given borrowernumber has borrowed.
1837 C<&borrissues> returns a two-element array. C<$issues> is a
1838 reference-to-array, where each element is a reference-to-hash; the
1839 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1840 in the Koha database. C<$count> is the number of elements in
1847 my $dbh = C4::Context->dbh;
1849 $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1850 items.itemnumber=issues.itemnumber and
1851 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1854 my $sth=$dbh->prepare($query);
1857 while (my $data = $sth->fetchrow_hashref) {
1858 push @result, $data;
1861 return(scalar(@result), \@result);
1866 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1868 Looks up what the patron with the given borrowernumber has borrowed,
1869 and sorts the results.
1871 C<$sortkey> is the name of a field on which to sort the results. This
1872 should be the name of a field in the C<issues>, C<biblio>,
1873 C<biblioitems>, or C<items> table in the Koha database.
1875 C<$limit> is the maximum number of results to return.
1877 C<&allissues> returns a two-element array. C<$issues> is a
1878 reference-to-array, where each element is a reference-to-hash; the
1879 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1880 C<items> tables of the Koha database. C<$count> is the number of
1881 elements in C<$issues>
1886 my ($bornum,$order,$limit)=@_;
1887 my $dbh = C4::Context->dbh;
1889 $query="Select * from issues,biblio,items,biblioitems
1890 where borrowernumber='$bornum' and
1891 items.biblioitemnumber=biblioitems.biblioitemnumber and
1892 items.itemnumber=issues.itemnumber and
1893 items.biblionumber=biblio.biblionumber";
1894 $query.=" order by $order";
1896 $query.=" limit $limit";
1899 my $sth=$dbh->prepare($query);
1903 while (my $data=$sth->fetchrow_hashref){
1908 return($i,\@result);
1913 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
1915 Returns aggregate data about items borrowed by the patron with the
1916 given borrowernumber.
1920 C<&borrdata2> returns a three-element array. C<$borrowed> is the
1921 number of books the patron currently has borrowed. C<$due> is the
1922 number of overdue items the patron currently has borrowed. C<$fine> is
1923 the total fine currently due by the borrower.
1928 my ($env,$bornum)=@_;
1929 my $dbh = C4::Context->dbh;
1930 my $query="Select count(*) from issues where borrowernumber='$bornum' and
1931 returndate is NULL";
1933 my $sth=$dbh->prepare($query);
1935 my $data=$sth->fetchrow_hashref;
1937 $sth=$dbh->prepare("Select count(*) from issues where
1938 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1940 my $data2=$sth->fetchrow_hashref;
1942 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1943 borrowernumber='$bornum'");
1945 my $data3=$sth->fetchrow_hashref;
1948 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1951 =item getboracctrecord
1953 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1955 Looks up accounting data for the patron with the given borrowernumber.
1959 (FIXME - I'm not at all sure what this is about.)
1961 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1962 reference-to-array, where each element is a reference-to-hash; the
1963 keys are the fields of the C<accountlines> table in the Koha database.
1964 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1965 total amount outstanding for all of the account lines.
1969 sub getboracctrecord {
1970 my ($env,$params) = @_;
1971 my $dbh = C4::Context->dbh;
1974 my $query= "Select * from accountlines where
1975 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1976 my $sth=$dbh->prepare($query);
1980 while (my $data=$sth->fetchrow_hashref){
1981 # if ($data->{'itemnumber'} ne ''){
1982 # $query="Select * from items,biblio where items.itemnumber=
1983 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1984 # my $sth2=$dbh->prepare($query);
1986 # my $data2=$sth2->fetchrow_hashref;
1990 $acctlines[$numlines] = $data;
1992 $total = $total+ $data->{'amountoutstanding'};
1995 return ($numlines,\@acctlines,$total);
2000 ($count, $lcount, $nacount, $fcount, $scount, $lostcount,
2001 $mending, $transit,$ocount) =
2002 &itemcount($env, $biblionumber, $type);
2004 Counts the number of items with the given biblionumber, broken down by
2009 If C<$type> is not set to C<intra>, lost, very overdue, and withdrawn
2010 items will not be counted.
2012 C<&itemcount> returns a nine-element list:
2014 C<$count> is the total number of items with the given biblionumber.
2016 C<$lcount> is the number of items at the Levin branch.
2018 C<$nacount> is the number of items that are neither borrowed, lost,
2019 nor withdrawn (and are therefore presumably on a shelf somewhere).
2021 C<$fcount> is the number of items at the Foxton branch.
2023 C<$scount> is the number of items at the Shannon branch.
2025 C<$lostcount> is the number of lost and very overdue items.
2027 C<$mending> is the number of items at the Mending branch (being
2030 C<$transit> is the number of items at the Transit branch (in transit
2033 C<$ocount> is the number of items that haven't arrived yet
2034 (aqorders.quantity - aqorders.quantityreceived).
2039 # FIXME - There's also a &C4::Acquisitions::itemcount and
2040 # &C4::Biblio::itemcount.
2041 # Since they're all exported, acqui/acquire.pl doesn't compile with -w.
2043 my ($env,$bibnum,$type)=@_;
2044 my $dbh = C4::Context->dbh;
2045 my $query="Select * from items where
2046 biblionumber=$bibnum ";
2047 if ($type ne 'intra'){
2048 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2049 (wthdrawn <> 1 or wthdrawn is NULL)";
2051 my $sth=$dbh->prepare($query);
2063 while (my $data=$sth->fetchrow_hashref){
2065 my $query2="select * from issues,items where issues.itemnumber=
2066 '$data->{'itemnumber'}' and returndate is NULL
2067 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2068 items.itemlost <> 2) or items.itemlost is NULL)
2069 and (wthdrawn <> 1 or wthdrawn is NULL)";
2071 my $sth2=$dbh->prepare($query2);
2073 if (my $data2=$sth2->fetchrow_hashref){
2076 if ($data->{'holdingbranch'} eq 'C' || $data->{'holdingbranch'} eq 'LT'){
2079 if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
2082 if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
2085 if ($data->{'itemlost'} eq '1'){
2088 if ($data->{'itemlost'} eq '2'){
2091 if ($data->{'holdingbranch'} eq 'FM'){
2094 if ($data->{'holdingbranch'} eq 'TR'){
2101 my $query2="Select * from aqorders where biblionumber=$bibnum";
2102 my $sth2=$dbh->prepare($query2);
2104 if (my $data=$sth2->fetchrow_hashref){
2105 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
2110 return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
2115 $counts = &itemcount2($env, $biblionumber, $type);
2117 Counts the number of items with the given biblionumber, broken down by
2122 C<$type> may be either C<intra> or anything else. If it is not set to
2123 C<intra>, then the search will exclude lost, very overdue, and
2126 C<$&itemcount2> returns a reference-to-hash, with the following fields:
2132 The total number of items with this biblionumber.
2136 The number of items on order (aqorders.quantity -
2137 aqorders.quantityreceived).
2141 For each branch that has at least one copy of the book, C<$counts>
2142 will have a key with the branch name, giving the number of copies at
2150 my ($env,$bibnum,$type)=@_;
2151 my $dbh = C4::Context->dbh;
2152 my $query="Select * from items,branches where
2153 biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
2154 if ($type ne 'intra'){
2155 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2156 (wthdrawn <> 1 or wthdrawn is NULL)";
2158 my $sth=$dbh->prepare($query);
2163 while (my $data=$sth->fetchrow_hashref){
2165 my $query2="select * from issues,items where issues.itemnumber=
2166 '$data->{'itemnumber'}' and returndate is NULL
2167 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2168 items.itemlost <> 2) or items.itemlost is NULL)
2169 and (wthdrawn <> 1 or wthdrawn is NULL)";
2171 my $sth2=$dbh->prepare($query2);
2173 # FIXME - fetchrow_hashref() can fail for any number of reasons
2174 # (e.g., a database server crash). Perhaps use a left join of some
2176 if (my $data2=$sth2->fetchrow_hashref){
2177 $counts{'not available'}++;
2179 $counts{$data->{'branchname'}}++;
2183 my $query2="Select * from aqorders where biblionumber=$bibnum and
2184 datecancellationprinted is NULL and quantity > quantityreceived";
2185 my $sth2=$dbh->prepare($query2);
2187 if (my $data=$sth2->fetchrow_hashref){
2188 $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
2197 $description = &ItemType($itemtype);
2199 Given an item type code, returns the description for that type.
2204 # FIXME - I'm pretty sure that after the initial setup, the list of
2205 # item types doesn't change very often. Hence, it seems slow and
2206 # inefficient to make yet another database call to look up information
2207 # that'll only change every few months or years.
2209 # Much better, I think, to automatically build a Perl file that can be
2210 # included in those scripts that require it, e.g.:
2211 # @itemtypes = qw( ART BCD CAS CD F ... );
2213 # ART => "Art Prints",
2214 # BCD => "CD-ROM from book",
2215 # CD => "Compact disc (WN)",
2216 # F => "Free Fiction",
2219 # The web server can then run a cron job to rebuild this file from the
2220 # database every hour or so.
2222 # The same thing goes for branches, book funds, book sellers, currency
2223 # rates, printers, stopwords, and perhaps others.
2226 my $dbh = C4::Context->dbh;
2227 my $query="select description from itemtypes where itemtype='$type'";
2228 my $sth=$dbh->prepare($query);
2230 my $dat=$sth->fetchrow_hashref;
2232 return ($dat->{'description'});
2237 ($count, @results) = &bibitems($biblionumber);
2239 Given the biblionumber for a book, C<&bibitems> looks up that book's
2240 biblioitems (different publications of the same book, the audio book
2241 and film versions, etc.).
2243 C<$count> is the number of elements in C<@results>.
2245 C<@results> is an array of references-to-hash; the keys are the fields
2246 of the C<biblioitems> and C<itemtypes> tables of the Koha database. In
2247 addition, C<itemlost> indicates the availability of the item: if it is
2248 "2", then all copies of the item are long overdue; if it is "1", then
2249 all copies are lost; otherwise, there is at least one copy available.
2255 my $dbh = C4::Context->dbh;
2256 my $query = "SELECT biblioitems.*,
2258 MIN(items.itemlost) as itemlost,
2259 MIN(items.dateaccessioned) as dateaccessioned
2260 FROM biblioitems, itemtypes, items
2261 WHERE biblioitems.biblionumber = ?
2262 AND biblioitems.itemtype = itemtypes.itemtype
2263 AND biblioitems.biblioitemnumber = items.biblioitemnumber
2264 GROUP BY items.biblioitemnumber";
2265 my $sth = $dbh->prepare($query);
2268 $sth->execute($bibnum);
2269 while (my $data = $sth->fetchrow_hashref) {
2270 $results[$count] = $data;
2274 return($count, @results);
2279 @barcodes = &barcodes($biblioitemnumber);
2281 Given a biblioitemnumber, looks up the corresponding items.
2283 Returns an array of references-to-hash; the keys are C<barcode> and
2286 The returned items include very overdue items, but not lost ones.
2291 #called from request.pl
2292 my ($biblioitemnumber)=@_;
2293 my $dbh = C4::Context->dbh;
2294 my $query="SELECT barcode, itemlost, holdingbranch FROM items
2295 WHERE biblioitemnumber = ?
2296 AND (wthdrawn <> 1 OR wthdrawn IS NULL)";
2297 my $sth=$dbh->prepare($query);
2298 $sth->execute($biblioitemnumber);
2301 while (my $data=$sth->fetchrow_hashref){
2302 $barcodes[$i]=$data;
2311 ($count, @websites) = &getwebsites($biblionumber);
2313 Looks up the web sites pertaining to the book with the given
2316 C<$count> is the number of elements in C<@websites>.
2318 C<@websites> is an array of references-to-hash; the keys are the
2319 fields from the C<websites> table in the Koha database.
2324 my ($biblionumber) = @_;
2325 my $dbh = C4::Context->dbh;
2326 my $query = "Select * from websites where biblionumber = $biblionumber";
2327 my $sth = $dbh->prepare($query);
2332 while (my $data = $sth->fetchrow_hashref) {
2333 # FIXME - The URL scheme shouldn't be stripped off, at least
2334 # not here, since it's part of the URL, and will be useful in
2335 # constructing a link to the site. If you don't want the user
2336 # to see the "http://" part, strip that off when building the
2338 $data->{'url'} =~ s/^http:\/\///; # FIXME - Leaning toothpick
2340 $results[$count] = $data;
2345 return($count, @results);
2348 =item getwebbiblioitems
2350 ($count, @results) = &getwebbiblioitems($biblionumber);
2352 Given a book's biblionumber, looks up the web versions of the book
2353 (biblioitems with itemtype C<WEB>).
2355 C<$count> is the number of items in C<@results>. C<@results> is an
2356 array of references-to-hash; the keys are the items from the
2357 C<biblioitems> table of the Koha database.
2361 sub getwebbiblioitems {
2362 my ($biblionumber) = @_;
2363 my $dbh = C4::Context->dbh;
2364 my $query = "Select * from biblioitems where biblionumber = $biblionumber
2365 and itemtype = 'WEB'";
2366 my $sth = $dbh->prepare($query);
2371 while (my $data = $sth->fetchrow_hashref) {
2372 $data->{'url'} =~ s/^http:\/\///;
2373 $results[$count] = $data;
2378 return($count, @results);
2379 } # sub getwebbiblioitems
2382 END { } # module clean-up code here (global destructor)
2391 Koha Developement team <info@koha.org>