1 package C4::Search; #assumes C4/Search
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
26 # FIXME - C4::Search uses C4::Reserves2, which uses C4::Search.
27 # So Perl complains that all of the functions here get redefined.
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32 # set the version for version checking
37 C4::Search - Functions for searching the Koha catalog and other databases
43 my ($count, @results) = catalogsearch($env, $type, $search, $num, $offset);
47 This module provides the searching facilities for the Koha catalog and
50 C<&catalogsearch> is a front end to all the other searches. Depending
51 on what is passed to it, it calls the appropriate search function.
60 @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
61 &itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
62 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
63 &getboracctrecord &ItemType &itemissues &subject &subtitle
64 &addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs
65 &findguarantor &getwebsites &getwebbiblioitems &catalogsearch &itemcount2);
66 # make all your functions, whether exported or not;
70 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
71 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
72 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
74 C<&findguarantees> takes a borrower number (e.g., that of a patron
75 with children) and looks up the borrowers who are guaranteed by that
76 borrower (i.e., the patron's children).
78 C<&findguarantees> returns two values: an integer giving the number of
79 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
80 of references to hash, which gives the actual results.
86 my $dbh = C4::Context->dbh;
87 my $query="select cardnumber,borrowernumber from borrowers where
89 my $sth=$dbh->prepare($query);
93 while (my $data = $sth->fetchrow_hashref)
98 return (scalar(@dat), \@dat);
103 $guarantor = &findguarantor($borrower_no);
104 $guarantor_cardno = $guarantor->{"cardnumber"};
105 $guarantor_surname = $guarantor->{"surname"};
108 C<&findguarantor> takes a borrower number (presumably that of a child
109 patron), finds the guarantor for C<$borrower_no> (the child's parent),
110 and returns the record for the guarantor.
112 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
113 from the C<borrowers> database table;
119 my $dbh = C4::Context->dbh;
120 my $query="select guarantor from borrowers where
121 borrowernumber='$bornum'";
122 my $sth=$dbh->prepare($query);
124 my $data=$sth->fetchrow_hashref;
126 $query="Select * from borrowers where
127 borrowernumber='$data->{'guarantor'}'";
128 $sth=$dbh->prepare($query);
130 $data=$sth->fetchrow_hashref;
137 %prefs = &systemprefs();
139 Returns a hash giving the system preferences. This is basically just a
140 dump of the C<systempreferences> database table.
144 # FIXME - This function is no longer used; in cases where you just
145 # care about one preference (which is true for most scripts), use
146 # C4::Context->preference.
149 my $dbh = C4::Context->dbh;
150 my $sth=$dbh->prepare("select variable,value from systempreferences");
152 while (my ($variable,$value)=$sth->fetchrow) {
153 $systemprefs{$variable}=$value;
156 return(%systemprefs);
159 =item NewBorrowerNumber
161 $num = &NewBorrowerNumber();
163 Allocates a new, unused borrower number, and returns it.
167 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
168 # Pick one and stick with it. Preferably use the other one. This function
169 # doesn't belong in C4::Search.
170 sub NewBorrowerNumber {
171 my $dbh = C4::Context->dbh;
172 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
174 my $data=$sth->fetchrow_hashref;
176 $data->{'max(borrowernumber)'}++;
177 return($data->{'max(borrowernumber)'});
182 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
184 This is primarily a front-end to other, more specialized catalog
185 search functions: if C<$search-E<gt>{itemnumber}> or
186 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
187 C<&CatSearch>. If $search->{subject} is given, it runs a subject
188 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
189 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
191 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
192 the items for each result, and adds several keys:
198 The total number of copies of this book.
200 =item C<locationhash>
202 This is a reference-to-hash; the keys are the names of branches where
203 this book may be found, and the values are the number of copies at
208 A descriptive string saying where the book is located, and how many
209 copies there are, if greater than 1.
213 The book's subject, with spaces replaced with C<%20>, presumably for
221 my ($env,$type,$search,$num,$offset)=@_;
222 my $dbh = C4::Context->dbh;
223 # foreach my $key (%$search){
224 # $search->{$key}=$dbh->quote($search->{$key});
226 my ($count,@results);
227 # print STDERR "Doing a search \n";
228 # FIXME - Use "elsif" to avoid this sort of deep nesting
229 if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
230 print STDERR "Doing a precise search\n";
231 ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
234 if ($search->{'subject'} ne ''){
235 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
237 if ($search->{'keyword'} ne ''){
238 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
240 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
245 if ($env->{itemcount} eq '1') {
246 foreach my $data (@results){
247 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
248 my $subject2=$data->{'subject'};
249 $subject2=~ s/ /%20/g;
250 $data->{'itemcount'}=$counts->{'total'};
251 my $totalitemcounts=0;
252 foreach my $key (keys %$counts){
253 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
254 #$data->{'location'}.="$key $counts->{$key} ";
255 $totalitemcounts+=$counts->{$key};
256 $data->{'locationhash'}->{$key}=$counts->{$key};
260 my $notavailabletext='';
261 foreach (sort keys %{$data->{'locationhash'}}) {
262 if ($_ eq 'notavailable') {
263 $notavailabletext="Not available";
264 my $c=$data->{'locationhash'}->{$_};
265 if ($totalitemcounts>1) {
266 $notavailabletext.=" ($c)";
270 my $c=$data->{'locationhash'}->{$_};
271 if ($totalitemcounts>1) {
272 $locationtext.=" ($c), ";
276 if ($notavailabletext) {
277 $locationtext.=$notavailabletext;
279 $locationtext=~s/, $//;
281 $data->{'location'}=$locationtext;
282 $data->{'subject2'}=$subject2;
285 return ($count,@results);
290 $search = { "keyword" => "One or more keywords",
291 "class" => "VID|CD", # Limit search to fiction and CDs
294 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
296 C<&KeywordSearch> searches the catalog by keyword: given a string
297 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
298 keywords, it looks for books that contain any of those keywords in any
299 of a number of places.
301 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
302 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
305 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
306 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
307 etc.). In this case, the search will be restricted to just those
310 If C<$search-E<gt>{"class"}> is not specified, you may specify
311 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
312 particular Dewey Decimal Classification category. Setting
313 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
314 whereas setting it to "5" will return all books with Dewey code 5I<xx>
315 (Science and Mathematics).
317 C<$env> and C<$type> are ignored.
319 C<$offset> and C<$num> specify the subset of results to return.
320 C<$num> specifies the number of results to return, and C<$offset> is
321 the number of the first result. Thus, setting C<$offset> to 100 and
322 C<$num> to 5 will return results 100 through 104 inclusive.
327 # FIXME - Everything that's being done here with Set::Scalar can be
328 # done with hashes: to add a result to the set, just use:
329 # $results{$foo} = 1;
330 # To get the list of results, just use
331 # @biblionumbers = sort keys %results;
332 # This would remove the dependency on Set::Scalar, which means one
333 # fewer package that people have to install.
336 my ($env,$type,$search,$num,$offset)=@_;
337 my $dbh = C4::Context->dbh;
338 $search->{'keyword'}=~ s/ +$//;
339 $search->{'keyword'}=~ s/'/\\'/;
340 my @key=split(' ',$search->{'keyword'});
345 # Look for keywords in table 'biblio'.
347 # FIXME - The next 15 lines can be rewritten in somewhat Lisp-ish
348 # fashion as follows:
350 # my $query = "select biblionumber from biblio where (" .
356 # "($field like '$_\%' or $field like '\% $_\%')"
360 # qw( title biblio.notes seriestitle )) .
363 # field like 'string%' or field like '% string%'
364 # can be rewritten (in MySQL, at least) as
365 # field regexp '(^| )string';
366 # However, this isn't portable. Though PostgreSQL allows you to use "~"
367 # instead of "regexp".
368 my $query="Select biblionumber from biblio
369 where ((title like '$key[0]%' or title like '% $key[0]%')";
371 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
374 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
375 for ($i=1;$i<$count;$i++){
376 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
378 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
379 for ($i=1;$i<$count;$i++){
380 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
384 my $sth=$dbh->prepare($query);
387 while (my @res=$sth->fetchrow_array){
388 $results[$i]=$res[0];
392 my $set1=Set::Scalar->new(@results);
394 # Now look for keywords in the 'bibliosubtitle' table.
395 $query="Select biblionumber from bibliosubtitle where
396 ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
397 for ($i=1;$i<$count;$i++){
398 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
402 $sth=$dbh->prepare($query);
405 while (my @res=$sth->fetchrow_array){
406 $results[$i]=$res[0];
410 my $set2=Set::Scalar->new(@results);
415 # Look for the keywords in the notes for individual items
416 # ('biblioitems.notes')
417 $query ="Select biblionumber from biblioitems where
418 ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
419 for ($i=1;$i<$count;$i++){
420 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
424 $sth=$dbh->prepare($query);
427 while (my @res=$sth->fetchrow_array){
428 $results[$i]=$res[0];
432 my $set3=Set::Scalar->new(@results);
437 # Look for keywords in the 'bibliosubject' table.
438 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
439 like '%$search->{'keyword'}%' group by biblionumber");
442 while (my @res=$sth->fetchrow_array){
443 $results[$i]=$res[0];
447 my $set4=Set::Scalar->new(@results);
456 my @res = $set1->members;
460 # print "count $count";
461 if ($search->{'class'} ne ''){
463 my $query="select * from biblio,biblioitems where
464 biblio.biblionumber='$res[$i2]' and
465 biblio.biblionumber=biblioitems.biblionumber ";
466 if ($search->{'class'} ne ''){ # FIXME - Redundant
467 my @temp=split(/\|/,$search->{'class'});
469 $query.= "and ( itemtype='$temp[0]'";
470 for (my $i=1;$i<$count;$i++){
471 $query.=" or itemtype='$temp[$i]'";
475 my $sth=$dbh->prepare($query);
478 if (my $data2=$sth->fetchrow_hashref){
479 my $dewey= $data2->{'dewey'};
480 my $subclass=$data2->{'subclass'};
481 # FIXME - This next bit is bogus, because it assumes that the
482 # Dewey code is a floating-point number. It isn't. It's
483 # actually a string that mainly consists of numbers. In
484 # particular, "4" is not a valid Dewey code, although "004"
485 # is ("Data processing; Computer science"). Likewise, zeros
486 # after the decimal are significant ("575" is not the same as
487 # "575.0"; the latter is more specific). And "000" is a
488 # perfectly good Dewey code ("General works; computer
489 # science") and should not be interpreted to mean "this
490 # database entry does not have a Dewey code". That's what
493 ($dewey == 0) && ($dewey='');
494 ($dewey) && ($dewey.=" $subclass") ;
496 my $end=$offset +$num;
501 if ($i4 <=$end && $i4 > $offset){
502 $data2->{'dewey'}=$dewey;
505 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
508 # print "in here $i3<br>";
519 # $search->{'class'} was not specified
520 while ($i2 < $num && $i2 < $count){
521 my $query="select * from biblio,biblioitems where
522 biblio.biblionumber='$res[$i2+$offset]' and
523 biblio.biblionumber=biblioitems.biblionumber ";
525 if ($search->{'class'} ne ''){ # FIXME - Ignored: we already know
526 # that $search->{'class'} eq '';
527 my @temp=split(/\|/,$search->{'class'});
529 $query.= "and ( itemtype='$temp[0]'";
530 for (my $i=1;$i<$count;$i++){
531 $query.=" or itemtype='$temp[$i]'";
535 if ($search->{'dewey'} ne ''){
536 $query.= "and (dewey like '$search->{'dewey'}%') ";
539 my $sth=$dbh->prepare($query);
542 if (my $data2=$sth->fetchrow_hashref){
543 my $dewey= $data2->{'dewey'};
544 my $subclass=$data2->{'subclass'};
546 ($dewey == 0) && ($dewey='');
547 ($dewey) && ($dewey.=" $subclass") ;
549 $data2->{'dewey'}=$dewey;
552 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
561 return($count,@res2);
565 my ($env,$type,$search,$num,$offset)=@_;
566 my $dbh = C4::Context->dbh;
567 $search->{'keyword'}=~ s/ +$//;
568 $search->{'keyword'}=~ s/'/\\'/;
569 my @key=split(' ',$search->{'keyword'});
573 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
574 biblio.biblionumber=biblioitems.biblionumber and
575 biblio.biblionumber=bibliosubtitle.biblionumber and
576 (((title like '$key[0]%' or title like '% $key[0]%')";
578 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
581 $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
582 for ($i=1;$i<$count;$i++){
583 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
585 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
586 for ($i=1;$i<$count;$i++){
587 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
589 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
590 for ($i=1;$i<$count;$i++){
591 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
593 $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
594 for ($i=1;$i<$count;$i++){
595 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
597 if ($search->{'keyword'} =~ /new zealand/i){
598 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
599 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
600 or author like '% nz %' or author like '% nz')"
602 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
603 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
604 $search->{'keyword'} =~ / nz/i){
605 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
606 or title like '% new zealand' or subtitle like 'new zealand%' or
607 subtitle like '% new zealand %'
608 or subtitle like '% new zealand' or author like 'new zealand%'
609 or author like '% new zealand %' or author like '% new zealand' or
610 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
611 or seriestitle like '% new zealand')"
614 if ($search->{'class'} ne ''){
615 my @temp=split(/\|/,$search->{'class'});
617 $query.= "and ( itemtype='$temp[0]'";
618 for (my $i=1;$i<$count;$i++){
619 $query.=" or itemtype='$temp[$i]'";
623 if ($search->{'dewey'} ne ''){
624 $query.= "and (dewey like '$search->{'dewey'}%') ";
626 $query.="group by biblio.biblionumber";
627 #$query.=" order by author,title";
629 my $sth=$dbh->prepare($query);
632 while (my $data=$sth->fetchrow_hashref){
633 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
636 # my ($dewey, $subclass) = $sti->fetchrow;
637 my $dewey=$data->{'dewey'};
638 my $subclass=$data->{'subclass'};
640 ($dewey == 0) && ($dewey='');
641 ($dewey) && ($dewey.=" $subclass");
643 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
644 # print $results[$i];
648 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
649 like '%$search->{'keyword'}%' group by biblionumber");
651 while (my $data=$sth->fetchrow_hashref){
652 $query="Select * from biblio,biblioitems where
653 biblio.biblionumber=$data->{'biblionumber'} and
654 biblio.biblionumber=biblioitems.biblionumber ";
655 if ($search->{'class'} ne ''){
656 my @temp=split(/\|/,$search->{'class'});
658 $query.= " and ( itemtype='$temp[0]'";
659 for (my $i=1;$i<$count;$i++){
660 $query.=" or itemtype='$temp[$i]'";
665 if ($search->{'dewey'} ne ''){
666 $query.= "and (dewey like '$search->{'dewey'}%') ";
668 my $sth2=$dbh->prepare($query);
671 while (my $data2=$sth2->fetchrow_hashref){
672 my $dewey= $data2->{'dewey'};
673 my $subclass=$data2->{'subclass'};
675 ($dewey == 0) && ($dewey='');
676 ($dewey) && ($dewey.=" $subclass") ;
678 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
679 # print $results[$i];
685 @results=sort @results;
692 while ($i2 < $count){
693 if ($results[$i2] ne $res[$i-1]){
694 $res[$i]=$results[$i2];
702 while ($i2 < $num && $i2 < $count){
703 $res2[$i2]=$res[$i2+$offset];
715 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
717 C<&CatSearch> searches the Koha catalog. It returns a list whose first
718 element is the number of returned results, and whose subsequent
719 elements are the results themselves.
721 Each returned element is a reference-to-hash. Most of the keys are
722 simply the fields from the C<biblio> table in the Koha database, but
723 the following keys may also be present:
729 The book's illustrator.
739 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
740 high-level behavior of C<&CatSearch>, as described below.
742 In many cases, the description below says that a certain field in the
743 database must match the search string. In these cases, it means that
744 the beginning of some word in the field must match the search string.
745 Thus, an author search for "sm" will return books whose author is
746 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
747 does not occur at the beginning of a word.
749 Note that within each search mode, the criteria are and-ed together.
750 That is, if you perform a loose search on the author "Jerome" and the
751 title "Boat", the search will only return books by Jerome containing
754 It is not possible to cross modes, e.g., set the author to "Asimov"
755 and the subject to "Math" in hopes of finding books on math by Asimov.
759 If C<$type> is set to C<loose>, the following search criteria may be
764 =item C<$search-E<gt>{author}>
766 The search string is a space-separated list of words. Each word must
767 match either the C<author> or C<additionalauthors> field.
769 =item C<$search-E<gt>{title}>
771 Each word in the search string must match the book title. If no author
772 is specified, the book subtitle will also be searched.
774 =item C<$search-E<gt>{abstract}>
776 Searches for the given search string in the book's abstract.
778 =item C<$search-E<gt>{'date-before'}>
780 Searches for books whose copyright date matches the search string.
781 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
782 books written in 1985, and setting it to "198" will find books written
783 between 1980 and 1989.
785 =item C<$search-E<gt>{title}>
787 Searches by title are also affected by the value of
788 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
789 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
790 match the search string exactly (the subtitle is not searched).
792 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
793 each word in the search string must match the title, subtitle,
794 unititle, or series title.
796 =item C<$search-E<gt>{class}>
798 Restricts the search to certain item classes. The value of
799 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
800 Thus, setting it to "F" restricts the search to fiction, and setting
801 it to "CD|CAS" will only look in compact disks and cassettes.
803 =item C<$search-E<gt>{dewey}>
805 Searches for books whose Dewey Decimal Classification code matches the
806 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
807 search for all books in 5I<xx> (Science and mathematics), setting it
808 to "54" will search for all books in 54I<x> (Chemistry), and setting
809 it to "546" will search for books on inorganic chemistry.
811 =item C<$search-E<gt>{publisher}>
813 Searches for books whose publisher contains the search string (unlike
814 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
819 =head2 Subject search
821 If C<$type> is set to C<subject>, the following search criteria may be
826 =item C<$search-E<gt>{subject}>
828 The search string is a space-separated list of words, each of which
829 must match the book's subject.
831 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
832 C<&CatSearch> will search for books whose subject is "New Zealand".
833 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
834 search for books on "nz" and "football", not books on "New Zealand"
839 =head2 Precise search
841 If C<$type> is set to C<precise>, the following search criteria may be
846 =item C<$search-E<gt>{item}>
848 Searches for books whose barcode exactly matches the search string.
850 =item C<$search-E<gt>{isbn}>
852 Searches for books whose ISBN exactly matches the search string.
856 For a loose search, if an author was specified, the results are
857 ordered by author and title. If no author was specified, the results
858 are ordered by title.
860 For other (non-loose) searches, if a subject was specified, the
861 results are ordered alphabetically by subject.
863 In all other cases (e.g., loose search by keyword), the results are
869 my ($env,$type,$search,$num,$offset)=@_;
870 my $dbh = C4::Context->dbh;
873 # FIXME - Why not just
874 # $search->{'title'} = quotemeta($search->{'title'})
875 # to escape all questionable characters, not just single-quotes?
876 $search->{'title'}=~ s/'/\\'/g;
877 $search->{'author'}=~ s/'/\\'/g;
878 $search->{'illustrator'}=~ s/'/\\'/g;
879 my $title = lc($search->{'title'});
881 if ($type eq 'loose') {
882 if ($search->{'author'} ne ''){
883 my @key=split(' ',$search->{'author'});
886 $query="select *,biblio.author,biblio.biblionumber from
888 left join additionalauthors
889 on additionalauthors.biblionumber =biblio.biblionumber
891 ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
892 additionalauthors.author like '$key[0]%' or additionalauthors.author
896 $query=$query." and (
897 biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
898 additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
903 if ($search->{'title'} ne ''){
904 my @key=split(' ',$search->{'title'});
907 $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
909 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
912 # $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')";
913 # for ($i=1;$i<$count;$i++){
914 # $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";
916 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
917 for ($i=1;$i<$count;$i++){
918 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
920 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
921 for ($i=1;$i<$count;$i++){
922 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
925 #$query=$query. " and (title like '%$search->{'title'}%'
926 #or seriestitle like '%$search->{'title'}%')";
928 if ($search->{'abstract'} ne ''){
929 $query.= " and (abstract like '%$search->{'abstract'}%')";
931 if ($search->{'date-before'} ne ''){
932 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
935 $query.=" group by biblio.biblionumber";
937 if ($search->{'title'} ne '') {
938 if ($search->{'ttype'} eq 'exact'){
939 $query="select * from biblio
941 (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
942 or biblio.unititle like '$search->{'title'} |%' or
943 biblio.unititle like '%| $search->{'title'} |%' or
944 biblio.unititle like '%| $search->{'title'}') or
945 (biblio.seriestitle = '$search->{'title'}' or
946 biblio.seriestitle like '$search->{'title'} |%' or
947 biblio.seriestitle like '%| $search->{'title'} |%' or
948 biblio.seriestitle like '%| $search->{'title'}')
951 my @key=split(' ',$search->{'title'});
954 $query="select * from biblio
955 left join bibliosubtitle on
956 biblio.biblionumber=bibliosubtitle.biblionumber
958 (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
960 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
963 $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
964 for ($i=1;$i<$count;$i++){
965 $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
967 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
968 for ($i=1;$i<$count;$i++){
969 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
971 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
972 for ($i=1;$i<$count;$i++){
973 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
977 if ($search->{'abstract'} ne ''){
978 $query.= " and (abstract like '%$search->{'abstract'}%')";
980 if ($search->{'date-before'} ne ''){
981 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
983 } elsif ($search->{'class'} ne ''){
984 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
985 my @temp=split(/\|/,$search->{'class'});
987 $query.= " and ( itemtype='$temp[0]'";
988 for (my $i=1;$i<$count;$i++){
989 $query.=" or itemtype='$temp[$i]'";
992 if ($search->{'illustrator'} ne ''){
993 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
995 if ($search->{'dewey'} ne ''){
996 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
998 } elsif ($search->{'dewey'} ne ''){
999 $query="select * from biblioitems,biblio
1000 where biblio.biblionumber=biblioitems.biblionumber
1001 and biblioitems.dewey like '$search->{'dewey'}%'";
1002 } elsif ($search->{'illustrator'} ne '') {
1003 $query="select * from biblioitems,biblio
1004 where biblio.biblionumber=biblioitems.biblionumber
1005 and biblioitems.illus like '%".$search->{'illustrator'}."%'";
1006 } elsif ($search->{'publisher'} ne ''){
1007 $query.= "Select * from biblio,biblioitems where biblio.biblionumber
1008 =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
1009 } elsif ($search->{'abstract'} ne ''){
1010 $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
1012 } elsif ($search->{'date-before'} ne ''){
1013 $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
1015 $query .=" group by biblio.biblionumber";
1018 if ($type eq 'subject'){
1019 my @key=split(' ',$search->{'subject'});
1022 $query="select distinct(subject) from bibliosubject where( subject like
1023 '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
1025 $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
1026 or subject like '% $key[$i]'
1027 or subject like '%($key[$i])%')";
1031 # FIXME - Wouldn't it be better to fix the database so that if a
1032 # book has a subject "NZ", then it also gets added the subject
1034 # This can also be generalized by adding a table of subject
1035 # synonyms to the database: just declare "NZ" to be a synonym for
1036 # "New Zealand", "SF" a synonym for both "Science fiction" and
1037 # "Fantastic fiction", etc.
1039 # FIXME - This can be rewritten as
1040 # if (lc($search->{"subject"}) eq "nz") {
1041 if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){
1042 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1043 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1044 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1045 $query=~ s/ nz/ NEW ZEALAND/ig;
1046 $query=~ s/nz /NEW ZEALAND /ig;
1047 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1050 if ($type eq 'precise'){
1052 if ($search->{'item'} ne ''){
1053 $query="select * from items,biblio ";
1054 my $search2=uc $search->{'item'};
1055 $query=$query." where
1056 items.biblionumber=biblio.biblionumber
1057 and barcode='$search2'";
1059 if ($search->{'isbn'} ne ''){
1060 my $search2=uc $search->{'isbn'};
1061 my $query1 = "select * from biblioitems where isbn='$search2'";
1062 my $sth1=$dbh->prepare($query1);
1063 # print STDERR "$query1\n";
1066 while (my $data=$sth1->fetchrow_hashref) {
1067 $query="select * from biblioitems,biblio where
1068 biblio.biblionumber = $data->{'biblionumber'}
1069 and biblioitems.biblionumber = biblio.biblionumber";
1070 my $sth=$dbh->prepare($query);
1072 # FIXME - There's already a $data in this scope.
1073 my $data=$sth->fetchrow_hashref;
1074 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1075 # FIXME - The following assumes that the Dewey code is a
1076 # floating-point number. It isn't: it's a string.
1078 ($dewey == 0) && ($dewey='');
1079 ($dewey) && ($dewey.=" $subclass");
1080 $data->{'dewey'}=$dewey;
1081 $results[$i2]=$data;
1082 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1090 if ($type ne 'precise' && $type ne 'subject'){
1091 if ($search->{'author'} ne ''){
1092 $query=$query." order by biblio.author,title";
1094 $query=$query." order by title";
1097 if ($type eq 'subject'){
1098 $query=$query." order by subject";
1101 #print STDERR "$query\n";
1102 my $sth=$dbh->prepare($query);
1106 my $limit= $num+$offset;
1107 while (my $data=$sth->fetchrow_hashref){
1108 my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
1109 if ($search->{'class'} ne ''){
1110 my @temp=split(/\|/,$search->{'class'});
1112 $query.= " and ( itemtype='$temp[0]'";
1113 for (my $i=1;$i<$count;$i++){
1114 $query.=" or itemtype='$temp[$i]'";
1118 if ($search->{'dewey'} ne ''){
1119 $query.=" and dewey='$search->{'dewey'}' ";
1121 if ($search->{'illustrator'} ne ''){
1122 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
1124 if ($search->{'publisher'} ne ''){
1125 $query.= " and (publishercode like '%$search->{'publisher'}%')";
1128 my $sti=$dbh->prepare($query);
1135 if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
1137 $dewey=$bibitemdata->{'dewey'};
1138 $subclass=$bibitemdata->{'subclass'};
1139 $publishercode=$bibitemdata->{'publishercode'};
1141 print STDERR "$dewey $subclass $publishercode\n";
1143 ($dewey == 0) && ($dewey='');
1144 ($dewey) && ($dewey.=" $subclass");
1145 $data->{'dewey'}=$dewey;
1146 $data->{'publishercode'}=$publishercode;
1149 if ($count > $offset && $count <= $limit){
1157 #if ($type ne 'precise'){
1161 return($count,@results);
1164 sub updatesearchstats{
1165 my ($dbh,$query)=@_;
1171 @results = &subsearch($env, $subject);
1173 Searches for books that have a subject that exactly matches
1176 C<&subsearch> returns an array of results. Each element of this array
1177 is a string, containing the book's title, author, and biblionumber,
1185 my ($env,$subject)=@_;
1186 my $dbh = C4::Context->dbh;
1187 $subject=$dbh->quote($subject);
1188 my $query="Select * from biblio,bibliosubject where
1189 biblio.biblionumber=bibliosubject.biblionumber and
1190 bibliosubject.subject=$subject group by biblio.biblionumber
1191 order by biblio.title";
1192 my $sth=$dbh->prepare($query);
1197 while (my $data=$sth->fetchrow_hashref){
1198 $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
1207 @results = &ItemInfo($env, $biblionumber, $type);
1209 Returns information about books with the given biblionumber.
1211 C<$type> may be either C<intra> or anything else. If it is not set to
1212 C<intra>, then the search will exclude lost, very overdue, and
1217 C<&ItemInfo> returns a list of references-to-hash. Each element
1218 contains a number of keys. Most of them are table items from the
1219 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1220 Koha database. Other keys include:
1224 =item C<$data-E<gt>{branchname}>
1226 The name (not the code) of the branch to which the book belongs.
1228 =item C<$data-E<gt>{datelastseen}>
1230 This is simply C<items.datelastseen>, except that while the date is
1231 stored in YYYY-MM-DD format in the database, here it is converted to
1232 DD/MM/YYYY format. A NULL date is returned as C<//>.
1234 =item C<$data-E<gt>{datedue}>
1236 =item C<$data-E<gt>{class}>
1238 This is the concatenation of C<biblioitems.classification>, the book's
1239 Dewey code, and C<biblioitems.subclass>.
1241 =item C<$data-E<gt>{ocount}>
1243 I think this is the number of copies of the book available.
1245 =item C<$data-E<gt>{order}>
1247 If this is set, it is set to C<One Order>.
1252 my ($env,$biblionumber,$type) = @_;
1253 my $dbh = C4::Context->dbh;
1254 my $query = "SELECT * FROM items, biblio, biblioitems, itemtypes
1255 WHERE items.biblionumber = ?
1256 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1257 AND biblioitems.itemtype = itemtypes.itemtype
1258 AND biblio.biblionumber = items.biblionumber";
1259 if ($type ne 'intra'){
1260 $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1261 or items.itemlost is NULL)
1262 and (wthdrawn <> 1 or wthdrawn is NULL)";
1264 $query .= " order by items.dateaccessioned desc";
1266 my $sth=$dbh->prepare($query);
1267 $sth->execute($biblionumber);
1271 while (my $data=$sth->fetchrow_hashref){
1272 my $iquery = "Select * from issues
1273 where itemnumber = '$data->{'itemnumber'}'
1274 and returndate is null";
1276 my $isth=$dbh->prepare($iquery);
1278 if (my $idata=$isth->fetchrow_hashref){
1279 # FIXME - The date ought to be properly parsed, and printed
1280 # according to local convention.
1281 my @temp=split('-',$idata->{'date_due'});
1282 $datedue = "$temp[2]/$temp[1]/$temp[0]";
1284 if ($data->{'itemlost'} eq '2'){
1285 $datedue='Very Overdue';
1287 if ($data->{'itemlost'} eq '1'){
1290 if ($data->{'wthdrawn'} eq '1'){
1291 $datedue="Cancelled";
1293 if ($datedue eq ''){
1294 $datedue="Available";
1295 my ($restype,$reserves)=CheckReserves($data->{'itemnumber'});
1301 #get branch information.....
1302 my $bquery = "SELECT * FROM branches
1303 WHERE branchcode = '$data->{'holdingbranch'}'";
1304 my $bsth=$dbh->prepare($bquery);
1306 if (my $bdata=$bsth->fetchrow_hashref){
1307 $data->{'branchname'} = $bdata->{'branchname'};
1310 my $class = $data->{'classification'};
1311 my $dewey = $data->{'dewey'};
1313 if ($dewey eq "000.") { $dewey = "";}; # FIXME - "000" is general
1314 # books about computer science
1315 if ($dewey < 10){$dewey='00'.$dewey;}
1316 if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
1321 $class = $class.$dewey;
1323 $class = $class.$data->{'subclass'};
1325 # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
1326 # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
1327 # to leave it empty, rather than convert it to "//".
1328 # Also ideally this should use the local format for displaying dates.
1329 my @temp=split('-',$data->{'datelastseen'});
1330 my $date="$temp[2]/$temp[1]/$temp[0]";
1331 $data->{'datelastseen'}=$date;
1332 $data->{'datedue'}=$datedue;
1333 $data->{'class'}=$class;
1338 my $query2="Select * from aqorders where biblionumber=$biblionumber";
1339 my $sth2=$dbh->prepare($query2);
1343 if ($data=$sth2->fetchrow_hashref){
1344 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1346 $data->{'ocount'}=$ocount;
1347 $data->{'order'}="One Order";
1358 @results = &GetItems($env, $biblionumber);
1360 Returns information about books with the given biblionumber.
1364 C<&GetItems> returns an array of strings. Each element is a
1365 tab-separated list of values: biblioitemnumber, itemtype,
1366 classification, Dewey number, subclass, ISBN, volume, number, and
1369 Itemdata, in turn, is a string of the form
1370 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1371 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1377 my ($env,$biblionumber)=@_;
1378 #debug_msg($env,"GetItems");
1379 my $dbh = C4::Context->dbh;
1380 my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
1381 #debug_msg($env,$query);
1382 my $sth=$dbh->prepare($query);
1384 #debug_msg($env,"executed query");
1387 while (my $data=$sth->fetchrow_hashref) {
1388 #debug_msg($env,$data->{'biblioitemnumber'});
1389 my $dewey = $data->{'dewey'};
1391 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1392 $line = $line."\t$data->{'classification'}\t$dewey";
1393 $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
1394 $line = $line."\t$data->{'volume'}\t$data->{number}";
1395 my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
1397 while (my $idata = $isth->fetchrow_hashref) {
1398 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1399 if ($idata->{'notforloan'} == 1) {
1400 $iline = $iline."NFL ";
1402 if ($idata->{'itemlost'} == 1) {
1403 $iline = $iline."LOST ";
1405 $line = $line."\t$iline";
1408 $results[$i] = $line;
1417 $item = &itemdata($barcode);
1419 Looks up the item with the given barcode, and returns a
1420 reference-to-hash containing information about that item. The keys of
1421 the hash are the fields from the C<items> and C<biblioitems> tables in
1428 my $dbh = C4::Context->dbh;
1429 my $query="Select * from items,biblioitems where barcode='$barcode'
1430 and items.biblioitemnumber=biblioitems.biblioitemnumber";
1432 my $sth=$dbh->prepare($query);
1434 my $data=$sth->fetchrow_hashref;
1441 $data = &bibdata($biblionumber, $type);
1443 Returns information about the book with the given biblionumber.
1445 C<$type> is ignored.
1447 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1448 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1451 In addition, C<$data-E<gt>{subject}> is the list of the book's
1452 subjects, separated by C<" , "> (space, comma, space).
1454 If there are multiple biblioitems with the given biblionumber, only
1455 the first one is considered.
1460 my ($bibnum, $type) = @_;
1461 my $dbh = C4::Context->dbh;
1462 my $query = "Select *, biblio.notes
1463 from biblio, biblioitems
1464 left join bibliosubtitle on
1465 biblio.biblionumber = bibliosubtitle.biblionumber
1466 where biblio.biblionumber = $bibnum
1467 and biblioitems.biblionumber = $bibnum";
1468 my $sth = $dbh->prepare($query);
1472 $data = $sth->fetchrow_hashref;
1475 $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
1476 $sth = $dbh->prepare($query);
1478 while (my $dat = $sth->fetchrow_hashref){
1479 $data->{'subject'} .= " , $dat->{'subject'}";
1488 $itemdata = &bibitemdata($biblioitemnumber);
1490 Looks up the biblioitem with the given biblioitemnumber. Returns a
1491 reference-to-hash. The keys are the fields from the C<biblio>,
1492 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1493 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1499 my $dbh = C4::Context->dbh;
1500 my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
1501 where biblio.biblionumber = biblioitems.biblionumber
1502 and biblioitemnumber = $bibitem
1503 and biblioitems.itemtype = itemtypes.itemtype";
1504 my $sth = $dbh->prepare($query);
1509 $data = $sth->fetchrow_hashref;
1517 ($count, $subjects) = &subject($biblionumber);
1519 Looks up the subjects of the book with the given biblionumber. Returns
1520 a two-element list. C<$subjects> is a reference-to-array, where each
1521 element is a subject of the book, and C<$count> is the number of
1522 elements in C<$subjects>.
1528 my $dbh = C4::Context->dbh;
1529 my $query="Select * from bibliosubject where biblionumber=$bibnum";
1530 my $sth=$dbh->prepare($query);
1534 while (my $data=$sth->fetchrow_hashref){
1539 return($i,\@results);
1544 ($count, $authors) = &addauthors($biblionumber);
1546 Looks up the additional authors for the book with the given
1549 Returns a two-element list. C<$authors> is a reference-to-array, where
1550 each element is an additional author, and C<$count> is the number of
1551 elements in C<$authors>.
1557 my $dbh = C4::Context->dbh;
1558 my $query="Select * from additionalauthors where biblionumber=$bibnum";
1559 my $sth=$dbh->prepare($query);
1563 while (my $data=$sth->fetchrow_hashref){
1568 return($i,\@results);
1573 ($count, $subtitles) = &subtitle($biblionumber);
1575 Looks up the subtitles for the book with the given biblionumber.
1577 Returns a two-element list. C<$subtitles> is a reference-to-array,
1578 where each element is a subtitle, and C<$count> is the number of
1579 elements in C<$subtitles>.
1585 my $dbh = C4::Context->dbh;
1586 my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1587 my $sth=$dbh->prepare($query);
1591 while (my $data=$sth->fetchrow_hashref){
1596 return($i,\@results);
1601 @issues = &itemissues($biblioitemnumber, $biblio);
1603 Looks up information about who has borrowed the bookZ<>(s) with the
1604 given biblioitemnumber.
1606 C<$biblio> is ignored.
1608 C<&itemissues> returns an array of references-to-hash. The keys
1609 include the fields from the C<items> table in the Koha database.
1610 Additional keys include:
1616 If the item is currently on loan, this gives the due date.
1618 If the item is not on loan, then this is either "Available" or
1619 "Cancelled", if the item has been withdrawn.
1623 If the item is currently on loan, this gives the card number of the
1624 patron who currently has the item.
1626 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1628 These give the timestamp for the last three times the item was
1631 =item C<card0>, C<card1>, C<card2>
1633 The card number of the last three patrons who borrowed this item.
1635 =item C<borrower0>, C<borrower1>, C<borrower2>
1637 The borrower number of the last three patrons who borrowed this item.
1644 my ($bibitem, $biblio)=@_;
1645 my $dbh = C4::Context->dbh;
1646 my $query = "Select * from items where
1647 items.biblioitemnumber = '$bibitem'";
1648 # FIXME - If this function die()s, the script will abort, and the
1649 # user won't get anything; depending on how far the script has
1650 # gotten, the user might get a blank page. It would be much better
1651 # to at least print an error message. The easiest way to do this
1652 # is to set $SIG{__DIE__}.
1653 my $sth = $dbh->prepare($query)
1654 || die $dbh->errstr;
1659 || die $sth->errstr;
1661 while (my $data = $sth->fetchrow_hashref) {
1662 # Find out who currently has this item.
1663 # FIXME - Wouldn't it be better to do this as a left join of
1664 # some sort? Currently, this code assumes that if
1665 # fetchrow_hashref() fails, then the book is on the shelf.
1666 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1667 # database server crash), not just because no items match the
1669 my $query2 = "select * from issues,borrowers
1670 where itemnumber = $data->{'itemnumber'}
1671 and returndate is NULL
1672 and issues.borrowernumber = borrowers.borrowernumber";
1673 my $sth2 = $dbh->prepare($query2);
1676 if (my $data2 = $sth2->fetchrow_hashref) {
1677 $data->{'date_due'} = $data2->{'date_due'};
1678 $data->{'card'} = $data2->{'cardnumber'};
1680 if ($data->{'wthdrawn'} eq '1') {
1681 $data->{'date_due'} = 'Cancelled';
1683 $data->{'date_due'} = 'Available';
1689 # Find the last 3 people who borrowed this item.
1690 $query2 = "select * from issues, borrowers
1691 where itemnumber = '$data->{'itemnumber'}'
1692 and issues.borrowernumber = borrowers.borrowernumber
1693 and returndate is not NULL
1694 order by returndate desc,timestamp desc";
1695 $sth2 = $dbh->prepare($query2)
1696 || die $dbh->errstr;
1698 || die $sth2->errstr;
1700 for (my $i2 = 0; $i2 < 2; $i2++) {
1701 if (my $data2 = $sth2->fetchrow_hashref) {
1702 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1703 $data->{"card$i2"} = $data2->{'cardnumber'};
1704 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1709 $results[$i] = $data;
1719 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1721 Looks up the item with the given biblioitemnumber.
1723 C<$env> and C<$dbh> are ignored.
1725 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1726 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1732 my ($env,$dbh,$itemnumber) = @_;
1733 $dbh = C4::Context->dbh;
1734 my $query="Select * from biblio,items,biblioitems
1735 where items.itemnumber = '$itemnumber'
1736 and biblio.biblionumber = items.biblionumber
1737 and biblioitems.biblioitemnumber = items.biblioitemnumber";
1738 my $sth=$dbh->prepare($query);
1741 my $data=$sth->fetchrow_hashref;
1748 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1750 Looks up patrons (borrowers) by name.
1752 C<$env> and C<$type> are ignored.
1754 C<$searchstring> is a space-separated list of search terms. Each term
1755 must match the beginning a borrower's surname, first name, or other
1758 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1759 reference-to-array; each element is a reference-to-hash, whose keys
1760 are the fields of the C<borrowers> table in the Koha database.
1761 C<$count> is the number of elements in C<$borrowers>.
1765 #used by member enquiries from the intranet
1766 #called by member.pl
1768 my ($env,$searchstring,$type)=@_;
1769 my $dbh = C4::Context->dbh;
1770 $searchstring=~ s/\'/\\\'/g;
1771 my @data=split(' ',$searchstring);
1773 my $query="Select * from borrowers
1774 where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\"
1775 or firstname like \"$data[0]%\" or firstname like \"% $data[0]%\"
1776 or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1778 for (my $i=1;$i<$count;$i++){
1779 $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"
1780 or firstname like \"$data[$i]%\" or firstname like \"% $data[$i]%\"
1781 or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1783 $query=$query.") or cardnumber = \"$searchstring\"
1784 order by surname,firstname";
1785 # print $query,"\n";
1786 my $sth=$dbh->prepare($query);
1790 while (my $data=$sth->fetchrow_hashref){
1791 push(@results,$data);
1796 return ($cnt,\@results);
1801 $borrower = &borrdata($cardnumber, $borrowernumber);
1803 Looks up information about a patron (borrower) by either card number
1804 or borrower number. If $borrowernumber is specified, C<&borrdata>
1805 searches by borrower number; otherwise, it searches by card number.
1807 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1808 the C<borrowers> table in the Koha database.
1813 my ($cardnumber,$bornum)=@_;
1814 $cardnumber = uc $cardnumber;
1815 my $dbh = C4::Context->dbh;
1818 $query="Select * from borrowers where cardnumber='$cardnumber'";
1820 $query="Select * from borrowers where borrowernumber='$bornum'";
1823 my $sth=$dbh->prepare($query);
1825 my $data=$sth->fetchrow_hashref;
1832 ($count, $issues) = &borrissues($borrowernumber);
1834 Looks up what the patron with the given borrowernumber has borrowed.
1836 C<&borrissues> returns a two-element array. C<$issues> is a
1837 reference-to-array, where each element is a reference-to-hash; the
1838 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1839 in the Koha database. C<$count> is the number of elements in
1846 my $dbh = C4::Context->dbh;
1848 $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1849 items.itemnumber=issues.itemnumber and
1850 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1853 my $sth=$dbh->prepare($query);
1857 while (my $data=$sth->fetchrow_hashref){
1862 return($i,\@result);
1867 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1869 Looks up what the patron with the given borrowernumber has borrowed,
1870 and sorts the results.
1872 C<$sortkey> is the name of a field on which to sort the results. This
1873 should be the name of a field in the C<issues>, C<biblio>,
1874 C<biblioitems>, or C<items> table in the Koha database.
1876 C<$limit> is the maximum number of results to return.
1878 C<&allissues> returns a two-element array. C<$issues> is a
1879 reference-to-array, where each element is a reference-to-hash; the
1880 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1881 C<items> tables of the Koha database. C<$count> is the number of
1882 elements in C<$issues>
1887 my ($bornum,$order,$limit)=@_;
1888 my $dbh = C4::Context->dbh;
1890 $query="Select * from issues,biblio,items,biblioitems
1891 where borrowernumber='$bornum' and
1892 items.biblioitemnumber=biblioitems.biblioitemnumber and
1893 items.itemnumber=issues.itemnumber and
1894 items.biblionumber=biblio.biblionumber";
1895 $query.=" order by $order";
1897 $query.=" limit $limit";
1900 my $sth=$dbh->prepare($query);
1904 while (my $data=$sth->fetchrow_hashref){
1909 return($i,\@result);
1914 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
1916 Returns aggregate data about items borrowed by the patron with the
1917 given borrowernumber.
1921 C<&borrdata2> returns a three-element array. C<$borrowed> is the
1922 number of books the patron currently has borrowed. C<$due> is the
1923 number of overdue items the patron currently has borrowed. C<$fine> is
1924 the total fine currently due by the borrower.
1929 my ($env,$bornum)=@_;
1930 my $dbh = C4::Context->dbh;
1931 my $query="Select count(*) from issues where borrowernumber='$bornum' and
1932 returndate is NULL";
1934 my $sth=$dbh->prepare($query);
1936 my $data=$sth->fetchrow_hashref;
1938 $sth=$dbh->prepare("Select count(*) from issues where
1939 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1941 my $data2=$sth->fetchrow_hashref;
1943 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1944 borrowernumber='$bornum'");
1946 my $data3=$sth->fetchrow_hashref;
1949 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1952 =item getboracctrecord
1954 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1956 Looks up accounting data for the patron with the given borrowernumber.
1960 (FIXME - I'm not at all sure what this is about.)
1962 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1963 reference-to-array, where each element is a reference-to-hash; the
1964 keys are the fields of the C<accountlines> table in the Koha database.
1965 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1966 total amount outstanding for all of the account lines.
1970 sub getboracctrecord {
1971 my ($env,$params) = @_;
1972 my $dbh = C4::Context->dbh;
1975 my $query= "Select * from accountlines where
1976 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1977 my $sth=$dbh->prepare($query);
1981 while (my $data=$sth->fetchrow_hashref){
1982 # if ($data->{'itemnumber'} ne ''){
1983 # $query="Select * from items,biblio where items.itemnumber=
1984 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1985 # my $sth2=$dbh->prepare($query);
1987 # my $data2=$sth2->fetchrow_hashref;
1991 $acctlines[$numlines] = $data;
1993 $total = $total+ $data->{'amountoutstanding'};
1996 return ($numlines,\@acctlines,$total);
2001 ($count, $lcount, $nacount, $fcount, $scount, $lostcount,
2002 $mending, $transit,$ocount) =
2003 &itemcount($env, $biblionumber, $type);
2005 Counts the number of items with the given biblionumber, broken down by
2010 If C<$type> is not set to C<intra>, lost, very overdue, and withdrawn
2011 items will not be counted.
2013 C<&itemcount> returns a nine-element list:
2015 C<$count> is the total number of items with the given biblionumber.
2017 C<$lcount> is the number of items at the Levin branch.
2019 C<$nacount> is the number of items that are neither borrowed, lost,
2020 nor withdrawn (and are therefore presumably on a shelf somewhere).
2022 C<$fcount> is the number of items at the Foxton branch.
2024 C<$scount> is the number of items at the Shannon branch.
2026 C<$lostcount> is the number of lost and very overdue items.
2028 C<$mending> is the number of items at the Mending branch (being
2031 C<$transit> is the number of items at the Transit branch (in transit
2034 C<$ocount> is the number of items that haven't arrived yet
2035 (aqorders.quantity - aqorders.quantityreceived).
2040 # FIXME - There's also a &C4::Acquisitions::itemcount and
2041 # &C4::Biblio::itemcount.
2042 # Since they're all exported, acqui/acquire.pl doesn't compile with -w.
2044 my ($env,$bibnum,$type)=@_;
2045 my $dbh = C4::Context->dbh;
2046 my $query="Select * from items where
2047 biblionumber=$bibnum ";
2048 if ($type ne 'intra'){
2049 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2050 (wthdrawn <> 1 or wthdrawn is NULL)";
2052 my $sth=$dbh->prepare($query);
2064 while (my $data=$sth->fetchrow_hashref){
2066 my $query2="select * from issues,items where issues.itemnumber=
2067 '$data->{'itemnumber'}' and returndate is NULL
2068 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2069 items.itemlost <> 2) or items.itemlost is NULL)
2070 and (wthdrawn <> 1 or wthdrawn is NULL)";
2072 my $sth2=$dbh->prepare($query2);
2074 if (my $data2=$sth2->fetchrow_hashref){
2077 if ($data->{'holdingbranch'} eq 'C' || $data->{'holdingbranch'} eq 'LT'){
2080 if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
2083 if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
2086 if ($data->{'itemlost'} eq '1'){
2089 if ($data->{'itemlost'} eq '2'){
2092 if ($data->{'holdingbranch'} eq 'FM'){
2095 if ($data->{'holdingbranch'} eq 'TR'){
2102 my $query2="Select * from aqorders where biblionumber=$bibnum";
2103 my $sth2=$dbh->prepare($query2);
2105 if (my $data=$sth2->fetchrow_hashref){
2106 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
2111 return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
2116 $counts = &itemcount2($env, $biblionumber, $type);
2118 Counts the number of items with the given biblionumber, broken down by
2123 C<$type> may be either C<intra> or anything else. If it is not set to
2124 C<intra>, then the search will exclude lost, very overdue, and
2127 C<$&itemcount2> returns a reference-to-hash, with the following fields:
2133 The total number of items with this biblionumber.
2137 The number of items on order (aqorders.quantity -
2138 aqorders.quantityreceived).
2142 For each branch that has at least one copy of the book, C<$counts>
2143 will have a key with the branch name, giving the number of copies at
2151 my ($env,$bibnum,$type)=@_;
2152 my $dbh = C4::Context->dbh;
2153 my $query="Select * from items,branches where
2154 biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
2155 if ($type ne 'intra'){
2156 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2157 (wthdrawn <> 1 or wthdrawn is NULL)";
2159 my $sth=$dbh->prepare($query);
2164 while (my $data=$sth->fetchrow_hashref){
2166 my $query2="select * from issues,items where issues.itemnumber=
2167 '$data->{'itemnumber'}' and returndate is NULL
2168 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2169 items.itemlost <> 2) or items.itemlost is NULL)
2170 and (wthdrawn <> 1 or wthdrawn is NULL)";
2172 my $sth2=$dbh->prepare($query2);
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 if (my $data2=$sth2->fetchrow_hashref){
2178 $counts{'not available'}++;
2180 $counts{$data->{'branchname'}}++;
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 END { } # module clean-up code here (global destructor)
2392 Koha Developement team <info@koha.org>