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
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;
135 =item NewBorrowerNumber
137 $num = &NewBorrowerNumber();
139 Allocates a new, unused borrower number, and returns it.
143 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
144 # Pick one and stick with it. Preferably use the other one. This function
145 # doesn't belong in C4::Search.
146 sub NewBorrowerNumber {
147 my $dbh = C4::Context->dbh;
148 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
150 my $data=$sth->fetchrow_hashref;
152 $data->{'max(borrowernumber)'}++;
153 return($data->{'max(borrowernumber)'});
158 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
160 This is primarily a front-end to other, more specialized catalog
161 search functions: if C<$search-E<gt>{itemnumber}> or
162 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
163 C<&CatSearch>. If $search->{subject} is given, it runs a subject
164 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
165 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
167 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
168 the items for each result, and adds several keys:
174 The total number of copies of this book.
176 =item C<locationhash>
178 This is a reference-to-hash; the keys are the names of branches where
179 this book may be found, and the values are the number of copies at
184 A descriptive string saying where the book is located, and how many
185 copies there are, if greater than 1.
189 The book's subject, with spaces replaced with C<%20>, presumably for
197 my ($env,$type,$search,$num,$offset)=@_;
198 my $dbh = C4::Context->dbh;
199 # foreach my $key (%$search){
200 # $search->{$key}=$dbh->quote($search->{$key});
202 my ($count,@results);
203 # print STDERR "Doing a search \n";
204 # FIXME - Use "elsif" to avoid this sort of deep nesting
205 if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
206 print STDERR "Doing a precise search\n";
207 ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
210 if ($search->{'subject'} ne ''){
211 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
213 if ($search->{'keyword'} ne ''){
214 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
216 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
221 if ($env->{itemcount} eq '1') {
222 foreach my $data (@results){
223 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
224 my $subject2=$data->{'subject'};
225 $subject2=~ s/ /%20/g;
226 $data->{'itemcount'}=$counts->{'total'};
227 my $totalitemcounts=0;
228 foreach my $key (keys %$counts){
229 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
230 #$data->{'location'}.="$key $counts->{$key} ";
231 $totalitemcounts+=$counts->{$key};
232 $data->{'locationhash'}->{$key}=$counts->{$key};
236 my $notavailabletext='';
237 foreach (sort keys %{$data->{'locationhash'}}) {
238 if ($_ eq 'notavailable') {
239 $notavailabletext="Not available";
240 my $c=$data->{'locationhash'}->{$_};
241 if ($totalitemcounts>1) {
242 $notavailabletext.=" ($c)";
246 my $c=$data->{'locationhash'}->{$_};
247 if ($totalitemcounts>1) {
248 $locationtext.=" ($c), ";
252 if ($notavailabletext) {
253 $locationtext.=$notavailabletext;
255 $locationtext=~s/, $//;
257 $data->{'location'}=$locationtext;
258 $data->{'subject2'}=$subject2;
261 return ($count,@results);
266 $search = { "keyword" => "One or more keywords",
267 "class" => "VID|CD", # Limit search to fiction and CDs
270 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
272 C<&KeywordSearch> searches the catalog by keyword: given a string
273 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
274 keywords, it looks for books that contain any of those keywords in any
275 of a number of places.
277 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
278 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
281 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
282 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
283 etc.). In this case, the search will be restricted to just those
286 If C<$search-E<gt>{"class"}> is not specified, you may specify
287 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
288 particular Dewey Decimal Classification category. Setting
289 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
290 whereas setting it to "5" will return all books with Dewey code 5I<xx>
291 (Science and Mathematics).
293 C<$env> and C<$type> are ignored.
295 C<$offset> and C<$num> specify the subset of results to return.
296 C<$num> specifies the number of results to return, and C<$offset> is
297 the number of the first result. Thus, setting C<$offset> to 100 and
298 C<$num> to 5 will return results 100 through 104 inclusive.
303 # FIXME - Everything that's being done here with Set::Scalar can be
304 # done with hashes: to add a result to the set, just use:
305 # $results{$foo} = 1;
306 # To get the list of results, just use
307 # @biblionumbers = sort keys %results;
308 # This would remove the dependency on Set::Scalar, which means one
309 # fewer package that people have to install.
312 my ($env,$type,$search,$num,$offset)=@_;
313 my $dbh = C4::Context->dbh;
314 $search->{'keyword'}=~ s/ +$//;
315 $search->{'keyword'}=~ s/'/\\'/;
316 my @key=split(' ',$search->{'keyword'});
321 # Look for keywords in table 'biblio'.
323 # FIXME - The next 15 lines can be rewritten in somewhat Lisp-ish
324 # fashion as follows:
326 # my $query = "select biblionumber from biblio where (" .
332 # "($field like '$_\%' or $field like '\% $_\%')"
336 # qw( title biblio.notes seriestitle )) .
339 # field like 'string%' or field like '% string%'
340 # can be rewritten (in MySQL, at least) as
341 # field regexp '(^| )string';
342 # However, this isn't portable. Though PostgreSQL allows you to use "~"
343 # instead of "regexp".
344 my $query="Select biblionumber from biblio
345 where ((title like '$key[0]%' or title like '% $key[0]%')";
347 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
350 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
351 for ($i=1;$i<$count;$i++){
352 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
354 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
355 for ($i=1;$i<$count;$i++){
356 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
360 my $sth=$dbh->prepare($query);
363 while (my @res=$sth->fetchrow_array){
364 $results[$i]=$res[0];
368 my $set1=Set::Scalar->new(@results);
370 # Now look for keywords in the 'bibliosubtitle' table.
371 $query="Select biblionumber from bibliosubtitle where
372 ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
373 for ($i=1;$i<$count;$i++){
374 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
378 $sth=$dbh->prepare($query);
381 while (my @res=$sth->fetchrow_array){
382 $results[$i]=$res[0];
386 my $set2=Set::Scalar->new(@results);
391 # Look for the keywords in the notes for individual items
392 # ('biblioitems.notes')
393 $query ="Select biblionumber from biblioitems where
394 ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
395 for ($i=1;$i<$count;$i++){
396 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
400 $sth=$dbh->prepare($query);
403 while (my @res=$sth->fetchrow_array){
404 $results[$i]=$res[0];
408 my $set3=Set::Scalar->new(@results);
413 # Look for keywords in the 'bibliosubject' table.
414 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
415 like '%$search->{'keyword'}%' group by biblionumber");
418 while (my @res=$sth->fetchrow_array){
419 $results[$i]=$res[0];
423 my $set4=Set::Scalar->new(@results);
432 my @res = $set1->members;
436 # print "count $count";
437 if ($search->{'class'} ne ''){
439 my $query="select * from biblio,biblioitems where
440 biblio.biblionumber='$res[$i2]' and
441 biblio.biblionumber=biblioitems.biblionumber ";
442 if ($search->{'class'} ne ''){ # FIXME - Redundant
443 my @temp=split(/\|/,$search->{'class'});
445 $query.= "and ( itemtype='$temp[0]'";
446 for (my $i=1;$i<$count;$i++){
447 $query.=" or itemtype='$temp[$i]'";
451 my $sth=$dbh->prepare($query);
454 if (my $data2=$sth->fetchrow_hashref){
455 my $dewey= $data2->{'dewey'};
456 my $subclass=$data2->{'subclass'};
457 # FIXME - This next bit is bogus, because it assumes that the
458 # Dewey code is a floating-point number. It isn't. It's
459 # actually a string that mainly consists of numbers. In
460 # particular, "4" is not a valid Dewey code, although "004"
461 # is ("Data processing; Computer science"). Likewise, zeros
462 # after the decimal are significant ("575" is not the same as
463 # "575.0"; the latter is more specific). And "000" is a
464 # perfectly good Dewey code ("General works; computer
465 # science") and should not be interpreted to mean "this
466 # database entry does not have a Dewey code". That's what
469 ($dewey == 0) && ($dewey='');
470 ($dewey) && ($dewey.=" $subclass") ;
472 my $end=$offset +$num;
477 if ($i4 <=$end && $i4 > $offset){
478 $data2->{'dewey'}=$dewey;
481 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
484 # print "in here $i3<br>";
495 # $search->{'class'} was not specified
496 while ($i2 < $num && $i2 < $count){
497 my $query="select * from biblio,biblioitems where
498 biblio.biblionumber='$res[$i2+$offset]' and
499 biblio.biblionumber=biblioitems.biblionumber ";
501 if ($search->{'class'} ne ''){ # FIXME - Ignored: we already know
502 # that $search->{'class'} eq '';
503 my @temp=split(/\|/,$search->{'class'});
505 $query.= "and ( itemtype='$temp[0]'";
506 for (my $i=1;$i<$count;$i++){
507 $query.=" or itemtype='$temp[$i]'";
511 if ($search->{'dewey'} ne ''){
512 $query.= "and (dewey like '$search->{'dewey'}%') ";
515 my $sth=$dbh->prepare($query);
518 if (my $data2=$sth->fetchrow_hashref){
519 my $dewey= $data2->{'dewey'};
520 my $subclass=$data2->{'subclass'};
522 ($dewey == 0) && ($dewey='');
523 ($dewey) && ($dewey.=" $subclass") ;
525 $data2->{'dewey'}=$dewey;
528 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
537 return($count,@res2);
541 my ($env,$type,$search,$num,$offset)=@_;
542 my $dbh = C4::Context->dbh;
543 $search->{'keyword'}=~ s/ +$//;
544 $search->{'keyword'}=~ s/'/\\'/;
545 my @key=split(' ',$search->{'keyword'});
549 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
550 biblio.biblionumber=biblioitems.biblionumber and
551 biblio.biblionumber=bibliosubtitle.biblionumber and
552 (((title like '$key[0]%' or title like '% $key[0]%')";
554 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
557 $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
558 for ($i=1;$i<$count;$i++){
559 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
561 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
562 for ($i=1;$i<$count;$i++){
563 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
565 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
566 for ($i=1;$i<$count;$i++){
567 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
569 $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
570 for ($i=1;$i<$count;$i++){
571 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
573 if ($search->{'keyword'} =~ /new zealand/i){
574 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
575 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
576 or author like '% nz %' or author like '% nz')"
578 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
579 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
580 $search->{'keyword'} =~ / nz/i){
581 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
582 or title like '% new zealand' or subtitle like 'new zealand%' or
583 subtitle like '% new zealand %'
584 or subtitle like '% new zealand' or author like 'new zealand%'
585 or author like '% new zealand %' or author like '% new zealand' or
586 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
587 or seriestitle like '% new zealand')"
590 if ($search->{'class'} ne ''){
591 my @temp=split(/\|/,$search->{'class'});
593 $query.= "and ( itemtype='$temp[0]'";
594 for (my $i=1;$i<$count;$i++){
595 $query.=" or itemtype='$temp[$i]'";
599 if ($search->{'dewey'} ne ''){
600 $query.= "and (dewey like '$search->{'dewey'}%') ";
602 $query.="group by biblio.biblionumber";
603 #$query.=" order by author,title";
605 my $sth=$dbh->prepare($query);
608 while (my $data=$sth->fetchrow_hashref){
609 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
612 # my ($dewey, $subclass) = $sti->fetchrow;
613 my $dewey=$data->{'dewey'};
614 my $subclass=$data->{'subclass'};
616 ($dewey == 0) && ($dewey='');
617 ($dewey) && ($dewey.=" $subclass");
619 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
620 # print $results[$i];
624 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
625 like '%$search->{'keyword'}%' group by biblionumber");
627 while (my $data=$sth->fetchrow_hashref){
628 $query="Select * from biblio,biblioitems where
629 biblio.biblionumber=$data->{'biblionumber'} and
630 biblio.biblionumber=biblioitems.biblionumber ";
631 if ($search->{'class'} ne ''){
632 my @temp=split(/\|/,$search->{'class'});
634 $query.= " and ( itemtype='$temp[0]'";
635 for (my $i=1;$i<$count;$i++){
636 $query.=" or itemtype='$temp[$i]'";
641 if ($search->{'dewey'} ne ''){
642 $query.= "and (dewey like '$search->{'dewey'}%') ";
644 my $sth2=$dbh->prepare($query);
647 while (my $data2=$sth2->fetchrow_hashref){
648 my $dewey= $data2->{'dewey'};
649 my $subclass=$data2->{'subclass'};
651 ($dewey == 0) && ($dewey='');
652 ($dewey) && ($dewey.=" $subclass") ;
654 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
655 # print $results[$i];
661 @results=sort @results;
668 while ($i2 < $count){
669 if ($results[$i2] ne $res[$i-1]){
670 $res[$i]=$results[$i2];
678 while ($i2 < $num && $i2 < $count){
679 $res2[$i2]=$res[$i2+$offset];
691 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
693 C<&CatSearch> searches the Koha catalog. It returns a list whose first
694 element is the number of returned results, and whose subsequent
695 elements are the results themselves.
697 Each returned element is a reference-to-hash. Most of the keys are
698 simply the fields from the C<biblio> table in the Koha database, but
699 the following keys may also be present:
705 The book's illustrator.
715 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
716 high-level behavior of C<&CatSearch>, as described below.
718 In many cases, the description below says that a certain field in the
719 database must match the search string. In these cases, it means that
720 the beginning of some word in the field must match the search string.
721 Thus, an author search for "sm" will return books whose author is
722 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
723 does not occur at the beginning of a word.
725 Note that within each search mode, the criteria are and-ed together.
726 That is, if you perform a loose search on the author "Jerome" and the
727 title "Boat", the search will only return books by Jerome containing
730 It is not possible to cross modes, e.g., set the author to "Asimov"
731 and the subject to "Math" in hopes of finding books on math by Asimov.
735 If C<$type> is set to C<loose>, the following search criteria may be
740 =item C<$search-E<gt>{author}>
742 The search string is a space-separated list of words. Each word must
743 match either the C<author> or C<additionalauthors> field.
745 =item C<$search-E<gt>{title}>
747 Each word in the search string must match the book title. If no author
748 is specified, the book subtitle will also be searched.
750 =item C<$search-E<gt>{abstract}>
752 Searches for the given search string in the book's abstract.
754 =item C<$search-E<gt>{'date-before'}>
756 Searches for books whose copyright date matches the search string.
757 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
758 books written in 1985, and setting it to "198" will find books written
759 between 1980 and 1989.
761 =item C<$search-E<gt>{title}>
763 Searches by title are also affected by the value of
764 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
765 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
766 match the search string exactly (the subtitle is not searched).
768 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
769 each word in the search string must match the title, subtitle,
770 unititle, or series title.
772 =item C<$search-E<gt>{class}>
774 Restricts the search to certain item classes. The value of
775 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
776 Thus, setting it to "F" restricts the search to fiction, and setting
777 it to "CD|CAS" will only look in compact disks and cassettes.
779 =item C<$search-E<gt>{dewey}>
781 Searches for books whose Dewey Decimal Classification code matches the
782 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
783 search for all books in 5I<xx> (Science and mathematics), setting it
784 to "54" will search for all books in 54I<x> (Chemistry), and setting
785 it to "546" will search for books on inorganic chemistry.
787 =item C<$search-E<gt>{publisher}>
789 Searches for books whose publisher contains the search string (unlike
790 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
795 =head2 Subject search
797 If C<$type> is set to C<subject>, the following search criterion may
802 =item C<$search-E<gt>{subject}>
804 The search string is a space-separated list of words, each of which
805 must match the book's subject.
807 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
808 C<&CatSearch> will search for books whose subject is "New Zealand".
809 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
810 search for books on "nz" and "football", not books on "New Zealand"
815 =head2 Precise search
817 If C<$type> is set to C<precise>, the following search criteria may be
822 =item C<$search-E<gt>{item}>
824 Searches for books whose barcode exactly matches the search string.
826 =item C<$search-E<gt>{isbn}>
828 Searches for books whose ISBN exactly matches the search string.
832 For a loose search, if an author was specified, the results are
833 ordered by author and title. If no author was specified, the results
834 are ordered by title.
836 For other (non-loose) searches, if a subject was specified, the
837 results are ordered alphabetically by subject.
839 In all other cases (e.g., loose search by keyword), the results are
845 my ($env,$type,$search,$num,$offset)=@_;
846 my $dbh = C4::Context->dbh;
849 # FIXME - Why not just
850 # $search->{'title'} = quotemeta($search->{'title'})
851 # to escape all questionable characters, not just single-quotes?
852 $search->{'title'}=~ s/'/\\'/g;
853 $search->{'author'}=~ s/'/\\'/g;
854 $search->{'illustrator'}=~ s/'/\\'/g;
855 my $title = lc($search->{'title'});
857 if ($type eq 'loose') {
858 if ($search->{'author'} ne ''){
859 my @key=split(' ',$search->{'author'});
862 $query="select *,biblio.author,biblio.biblionumber from
864 left join additionalauthors
865 on additionalauthors.biblionumber =biblio.biblionumber
867 ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
868 additionalauthors.author like '$key[0]%' or additionalauthors.author
872 $query=$query." and (
873 biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
874 additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
879 if ($search->{'title'} ne ''){
880 my @key=split(' ',$search->{'title'});
883 $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
885 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
888 # $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')";
889 # for ($i=1;$i<$count;$i++){
890 # $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";
892 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
893 for ($i=1;$i<$count;$i++){
894 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
896 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
897 for ($i=1;$i<$count;$i++){
898 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
901 #$query=$query. " and (title like '%$search->{'title'}%'
902 #or seriestitle like '%$search->{'title'}%')";
904 if ($search->{'abstract'} ne ''){
905 $query.= " and (abstract like '%$search->{'abstract'}%')";
907 if ($search->{'date-before'} ne ''){
908 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
911 $query.=" group by biblio.biblionumber";
913 if ($search->{'title'} ne '') {
914 if ($search->{'ttype'} eq 'exact'){
915 $query="select * from biblio
917 (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
918 or biblio.unititle like '$search->{'title'} |%' or
919 biblio.unititle like '%| $search->{'title'} |%' or
920 biblio.unititle like '%| $search->{'title'}') or
921 (biblio.seriestitle = '$search->{'title'}' or
922 biblio.seriestitle like '$search->{'title'} |%' or
923 biblio.seriestitle like '%| $search->{'title'} |%' or
924 biblio.seriestitle like '%| $search->{'title'}')
927 my @key=split(' ',$search->{'title'});
930 $query="select * from biblio
931 left join bibliosubtitle on
932 biblio.biblionumber=bibliosubtitle.biblionumber
934 (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
936 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
939 $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
940 for ($i=1;$i<$count;$i++){
941 $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
943 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
944 for ($i=1;$i<$count;$i++){
945 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
947 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
948 for ($i=1;$i<$count;$i++){
949 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
953 if ($search->{'abstract'} ne ''){
954 $query.= " and (abstract like '%$search->{'abstract'}%')";
956 if ($search->{'date-before'} ne ''){
957 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
959 } elsif ($search->{'class'} ne ''){
960 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
961 my @temp=split(/\|/,$search->{'class'});
963 $query.= " and ( itemtype='$temp[0]'";
964 for (my $i=1;$i<$count;$i++){
965 $query.=" or itemtype='$temp[$i]'";
968 if ($search->{'illustrator'} ne ''){
969 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
971 if ($search->{'dewey'} ne ''){
972 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
974 } elsif ($search->{'dewey'} ne ''){
975 $query="select * from biblioitems,biblio
976 where biblio.biblionumber=biblioitems.biblionumber
977 and biblioitems.dewey like '$search->{'dewey'}%'";
978 } elsif ($search->{'illustrator'} ne '') {
979 $query="select * from biblioitems,biblio
980 where biblio.biblionumber=biblioitems.biblionumber
981 and biblioitems.illus like '%".$search->{'illustrator'}."%'";
982 } elsif ($search->{'publisher'} ne ''){
983 $query.= "Select * from biblio,biblioitems where biblio.biblionumber
984 =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
985 } elsif ($search->{'abstract'} ne ''){
986 $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
988 } elsif ($search->{'date-before'} ne ''){
989 $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
991 $query .=" group by biblio.biblionumber";
994 if ($type eq 'subject'){
995 my @key=split(' ',$search->{'subject'});
998 $query="select distinct(subject) from bibliosubject where( subject like
999 '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
1001 $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
1002 or subject like '% $key[$i]'
1003 or subject like '%($key[$i])%')";
1007 # FIXME - Wouldn't it be better to fix the database so that if a
1008 # book has a subject "NZ", then it also gets added the subject
1010 # This can also be generalized by adding a table of subject
1011 # synonyms to the database: just declare "NZ" to be a synonym for
1012 # "New Zealand", "SF" a synonym for both "Science fiction" and
1013 # "Fantastic fiction", etc.
1015 # FIXME - This can be rewritten as
1016 # if (lc($search->{"subject"}) eq "nz") {
1017 if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){
1018 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1019 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1020 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1021 $query=~ s/ nz/ NEW ZEALAND/ig;
1022 $query=~ s/nz /NEW ZEALAND /ig;
1023 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1026 if ($type eq 'precise'){
1028 if ($search->{'item'} ne ''){
1029 $query="select * from items,biblio ";
1030 my $search2=uc $search->{'item'};
1031 $query=$query." where
1032 items.biblionumber=biblio.biblionumber
1033 and barcode='$search2'";
1035 if ($search->{'isbn'} ne ''){
1036 my $search2=uc $search->{'isbn'};
1037 my $query1 = "select * from biblioitems where isbn='$search2'";
1038 my $sth1=$dbh->prepare($query1);
1039 # print STDERR "$query1\n";
1042 while (my $data=$sth1->fetchrow_hashref) {
1043 $query="select * from biblioitems,biblio where
1044 biblio.biblionumber = $data->{'biblionumber'}
1045 and biblioitems.biblionumber = biblio.biblionumber";
1046 my $sth=$dbh->prepare($query);
1048 # FIXME - There's already a $data in this scope.
1049 my $data=$sth->fetchrow_hashref;
1050 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1051 # FIXME - The following assumes that the Dewey code is a
1052 # floating-point number. It isn't: it's a string.
1054 ($dewey == 0) && ($dewey='');
1055 ($dewey) && ($dewey.=" $subclass");
1056 $data->{'dewey'}=$dewey;
1057 $results[$i2]=$data;
1058 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1066 if ($type ne 'precise' && $type ne 'subject'){
1067 if ($search->{'author'} ne ''){
1068 $query=$query." order by biblio.author,title";
1070 $query=$query." order by title";
1073 if ($type eq 'subject'){
1074 $query=$query." order by subject";
1077 #print STDERR "$query\n";
1078 my $sth=$dbh->prepare($query);
1082 my $limit= $num+$offset;
1083 while (my $data=$sth->fetchrow_hashref){
1084 my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
1085 if ($search->{'class'} ne ''){
1086 my @temp=split(/\|/,$search->{'class'});
1088 $query.= " and ( itemtype='$temp[0]'";
1089 for (my $i=1;$i<$count;$i++){
1090 $query.=" or itemtype='$temp[$i]'";
1094 if ($search->{'dewey'} ne ''){
1095 $query.=" and dewey='$search->{'dewey'}' ";
1097 if ($search->{'illustrator'} ne ''){
1098 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
1100 if ($search->{'publisher'} ne ''){
1101 $query.= " and (publishercode like '%$search->{'publisher'}%')";
1104 my $sti=$dbh->prepare($query);
1111 if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
1113 $dewey=$bibitemdata->{'dewey'};
1114 $subclass=$bibitemdata->{'subclass'};
1115 $publishercode=$bibitemdata->{'publishercode'};
1117 print STDERR "$dewey $subclass $publishercode\n";
1119 ($dewey == 0) && ($dewey='');
1120 ($dewey) && ($dewey.=" $subclass");
1121 $data->{'dewey'}=$dewey;
1122 $data->{'publishercode'}=$publishercode;
1125 if ($count > $offset && $count <= $limit){
1133 #if ($type ne 'precise'){
1137 return($count,@results);
1140 sub updatesearchstats{
1141 my ($dbh,$query)=@_;
1147 @results = &subsearch($env, $subject);
1149 Searches for books that have a subject that exactly matches
1152 C<&subsearch> returns an array of results. Each element of this array
1153 is a string, containing the book's title, author, and biblionumber,
1161 my ($env,$subject)=@_;
1162 my $dbh = C4::Context->dbh;
1163 $subject=$dbh->quote($subject);
1164 my $query="Select * from biblio,bibliosubject where
1165 biblio.biblionumber=bibliosubject.biblionumber and
1166 bibliosubject.subject=$subject group by biblio.biblionumber
1167 order by biblio.title";
1168 my $sth=$dbh->prepare($query);
1173 while (my $data=$sth->fetchrow_hashref){
1174 $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
1183 @results = &ItemInfo($env, $biblionumber, $type);
1185 Returns information about books with the given biblionumber.
1187 C<$type> may be either C<intra> or anything else. If it is not set to
1188 C<intra>, then the search will exclude lost, very overdue, and
1193 C<&ItemInfo> returns a list of references-to-hash. Each element
1194 contains a number of keys. Most of them are table items from the
1195 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1196 Koha database. Other keys include:
1200 =item C<$data-E<gt>{branchname}>
1202 The name (not the code) of the branch to which the book belongs.
1204 =item C<$data-E<gt>{datelastseen}>
1206 This is simply C<items.datelastseen>, except that while the date is
1207 stored in YYYY-MM-DD format in the database, here it is converted to
1208 DD/MM/YYYY format. A NULL date is returned as C<//>.
1210 =item C<$data-E<gt>{datedue}>
1212 =item C<$data-E<gt>{class}>
1214 This is the concatenation of C<biblioitems.classification>, the book's
1215 Dewey code, and C<biblioitems.subclass>.
1217 =item C<$data-E<gt>{ocount}>
1219 I think this is the number of copies of the book available.
1221 =item C<$data-E<gt>{order}>
1223 If this is set, it is set to C<One Order>.
1230 my ($env,$biblionumber,$type) = @_;
1231 my $dbh = C4::Context->dbh;
1232 my $query = "SELECT * FROM items, biblio, biblioitems, itemtypes
1233 WHERE items.biblionumber = ?
1234 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1235 AND biblioitems.itemtype = itemtypes.itemtype
1236 AND biblio.biblionumber = items.biblionumber";
1237 if ($type ne 'intra'){
1238 $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1239 or items.itemlost is NULL)
1240 and (wthdrawn <> 1 or wthdrawn is NULL)";
1242 $query .= " order by items.dateaccessioned desc";
1244 my $sth=$dbh->prepare($query);
1245 $sth->execute($biblionumber);
1249 while (my $data=$sth->fetchrow_hashref){
1250 my $iquery = "Select * from issues
1251 where itemnumber = '$data->{'itemnumber'}'
1252 and returndate is null";
1254 my $isth=$dbh->prepare($iquery);
1256 if (my $idata=$isth->fetchrow_hashref){
1257 # FIXME - The date ought to be properly parsed, and printed
1258 # according to local convention.
1259 my @temp=split('-',$idata->{'date_due'});
1260 $datedue = "$temp[2]/$temp[1]/$temp[0]";
1262 if ($data->{'itemlost'} eq '2'){
1263 $datedue='Very Overdue';
1265 if ($data->{'itemlost'} eq '1'){
1268 if ($data->{'wthdrawn'} eq '1'){
1269 $datedue="Cancelled";
1271 if ($datedue eq ''){
1272 $datedue="Available";
1273 my ($restype,$reserves)=CheckReserves($data->{'itemnumber'});
1279 #get branch information.....
1280 my $bquery = "SELECT * FROM branches
1281 WHERE branchcode = '$data->{'holdingbranch'}'";
1282 my $bsth=$dbh->prepare($bquery);
1284 if (my $bdata=$bsth->fetchrow_hashref){
1285 $data->{'branchname'} = $bdata->{'branchname'};
1288 my $class = $data->{'classification'};
1289 my $dewey = $data->{'dewey'};
1291 if ($dewey eq "000.") { $dewey = "";}; # FIXME - "000" is general
1292 # books about computer science
1293 if ($dewey < 10){$dewey='00'.$dewey;}
1294 if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
1299 $class = $class.$dewey;
1301 $class = $class.$data->{'subclass'};
1303 # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
1304 # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
1305 # to leave it empty, rather than convert it to "//".
1306 # Also ideally this should use the local format for displaying dates.
1307 my @temp=split('-',$data->{'datelastseen'});
1308 my $date="$temp[2]/$temp[1]/$temp[0]";
1309 $data->{'datelastseen'}=$date;
1310 $data->{'datedue'}=$datedue;
1311 $data->{'class'}=$class;
1316 my $query2="Select * from aqorders where biblionumber=$biblionumber";
1317 my $sth2=$dbh->prepare($query2);
1321 if ($data=$sth2->fetchrow_hashref){
1322 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1324 $data->{'ocount'}=$ocount;
1325 $data->{'order'}="One Order";
1336 @results = &GetItems($env, $biblionumber);
1338 Returns information about books with the given biblionumber.
1342 C<&GetItems> returns an array of strings. Each element is a
1343 tab-separated list of values: biblioitemnumber, itemtype,
1344 classification, Dewey number, subclass, ISBN, volume, number, and
1347 Itemdata, in turn, is a string of the form
1348 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1349 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1355 my ($env,$biblionumber)=@_;
1356 #debug_msg($env,"GetItems");
1357 my $dbh = C4::Context->dbh;
1358 my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
1359 #debug_msg($env,$query);
1360 my $sth=$dbh->prepare($query);
1362 #debug_msg($env,"executed query");
1365 while (my $data=$sth->fetchrow_hashref) {
1366 #debug_msg($env,$data->{'biblioitemnumber'});
1367 my $dewey = $data->{'dewey'};
1369 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1370 $line = $line."\t$data->{'classification'}\t$dewey";
1371 $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
1372 $line = $line."\t$data->{'volume'}\t$data->{number}";
1373 my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
1375 while (my $idata = $isth->fetchrow_hashref) {
1376 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1377 if ($idata->{'notforloan'} == 1) {
1378 $iline = $iline."NFL ";
1380 if ($idata->{'itemlost'} == 1) {
1381 $iline = $iline."LOST ";
1383 $line = $line."\t$iline";
1386 $results[$i] = $line;
1395 $item = &itemdata($barcode);
1397 Looks up the item with the given barcode, and returns a
1398 reference-to-hash containing information about that item. The keys of
1399 the hash are the fields from the C<items> and C<biblioitems> tables in
1406 my $dbh = C4::Context->dbh;
1407 my $query="Select * from items,biblioitems where barcode='$barcode'
1408 and items.biblioitemnumber=biblioitems.biblioitemnumber";
1410 my $sth=$dbh->prepare($query);
1412 my $data=$sth->fetchrow_hashref;
1419 $data = &bibdata($biblionumber, $type);
1421 Returns information about the book with the given biblionumber.
1423 C<$type> is ignored.
1425 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1426 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1429 In addition, C<$data-E<gt>{subject}> is the list of the book's
1430 subjects, separated by C<" , "> (space, comma, space).
1432 If there are multiple biblioitems with the given biblionumber, only
1433 the first one is considered.
1438 my ($bibnum, $type) = @_;
1439 my $dbh = C4::Context->dbh;
1440 my $query = "Select *, biblio.notes
1441 from biblio, biblioitems
1442 left join bibliosubtitle on
1443 biblio.biblionumber = bibliosubtitle.biblionumber
1444 where biblio.biblionumber = $bibnum
1445 and biblioitems.biblionumber = $bibnum";
1446 my $sth = $dbh->prepare($query);
1450 $data = $sth->fetchrow_hashref;
1453 $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
1454 $sth = $dbh->prepare($query);
1456 while (my $dat = $sth->fetchrow_hashref){
1457 $data->{'subject'} .= " , $dat->{'subject'}";
1466 $itemdata = &bibitemdata($biblioitemnumber);
1468 Looks up the biblioitem with the given biblioitemnumber. Returns a
1469 reference-to-hash. The keys are the fields from the C<biblio>,
1470 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1471 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1477 my $dbh = C4::Context->dbh;
1478 my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
1479 where biblio.biblionumber = biblioitems.biblionumber
1480 and biblioitemnumber = $bibitem
1481 and biblioitems.itemtype = itemtypes.itemtype";
1482 my $sth = $dbh->prepare($query);
1487 $data = $sth->fetchrow_hashref;
1495 ($count, $subjects) = &subject($biblionumber);
1497 Looks up the subjects of the book with the given biblionumber. Returns
1498 a two-element list. C<$subjects> is a reference-to-array, where each
1499 element is a subject of the book, and C<$count> is the number of
1500 elements in C<$subjects>.
1506 my $dbh = C4::Context->dbh;
1507 my $query="Select * from bibliosubject where biblionumber=$bibnum";
1508 my $sth=$dbh->prepare($query);
1512 while (my $data=$sth->fetchrow_hashref){
1517 return($i,\@results);
1522 ($count, $authors) = &addauthors($biblionumber);
1524 Looks up the additional authors for the book with the given
1527 Returns a two-element list. C<$authors> is a reference-to-array, where
1528 each element is an additional author, and C<$count> is the number of
1529 elements in C<$authors>.
1535 my $dbh = C4::Context->dbh;
1536 my $query="Select * from additionalauthors where biblionumber=$bibnum";
1537 my $sth=$dbh->prepare($query);
1541 while (my $data=$sth->fetchrow_hashref){
1546 return($i,\@results);
1551 ($count, $subtitles) = &subtitle($biblionumber);
1553 Looks up the subtitles for the book with the given biblionumber.
1555 Returns a two-element list. C<$subtitles> is a reference-to-array,
1556 where each element is a subtitle, and C<$count> is the number of
1557 elements in C<$subtitles>.
1563 my $dbh = C4::Context->dbh;
1564 my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1565 my $sth=$dbh->prepare($query);
1569 while (my $data=$sth->fetchrow_hashref){
1574 return($i,\@results);
1579 @issues = &itemissues($biblioitemnumber, $biblio);
1581 Looks up information about who has borrowed the bookZ<>(s) with the
1582 given biblioitemnumber.
1584 C<$biblio> is ignored.
1586 C<&itemissues> returns an array of references-to-hash. The keys
1587 include the fields from the C<items> table in the Koha database.
1588 Additional keys include:
1594 If the item is currently on loan, this gives the due date.
1596 If the item is not on loan, then this is either "Available" or
1597 "Cancelled", if the item has been withdrawn.
1601 If the item is currently on loan, this gives the card number of the
1602 patron who currently has the item.
1604 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1606 These give the timestamp for the last three times the item was
1609 =item C<card0>, C<card1>, C<card2>
1611 The card number of the last three patrons who borrowed this item.
1613 =item C<borrower0>, C<borrower1>, C<borrower2>
1615 The borrower number of the last three patrons who borrowed this item.
1622 my ($bibitem, $biblio)=@_;
1623 my $dbh = C4::Context->dbh;
1624 my $query = "Select * from items where
1625 items.biblioitemnumber = '$bibitem'";
1626 # FIXME - If this function die()s, the script will abort, and the
1627 # user won't get anything; depending on how far the script has
1628 # gotten, the user might get a blank page. It would be much better
1629 # to at least print an error message. The easiest way to do this
1630 # is to set $SIG{__DIE__}.
1631 my $sth = $dbh->prepare($query)
1632 || die $dbh->errstr;
1637 || die $sth->errstr;
1639 while (my $data = $sth->fetchrow_hashref) {
1640 # Find out who currently has this item.
1641 # FIXME - Wouldn't it be better to do this as a left join of
1642 # some sort? Currently, this code assumes that if
1643 # fetchrow_hashref() fails, then the book is on the shelf.
1644 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1645 # database server crash), not just because no items match the
1647 my $query2 = "select * from issues,borrowers
1648 where itemnumber = $data->{'itemnumber'}
1649 and returndate is NULL
1650 and issues.borrowernumber = borrowers.borrowernumber";
1651 my $sth2 = $dbh->prepare($query2);
1654 if (my $data2 = $sth2->fetchrow_hashref) {
1655 $data->{'date_due'} = $data2->{'date_due'};
1656 $data->{'card'} = $data2->{'cardnumber'};
1658 if ($data->{'wthdrawn'} eq '1') {
1659 $data->{'date_due'} = 'Cancelled';
1661 $data->{'date_due'} = 'Available';
1667 # Find the last 3 people who borrowed this item.
1668 $query2 = "select * from issues, borrowers
1669 where itemnumber = '$data->{'itemnumber'}'
1670 and issues.borrowernumber = borrowers.borrowernumber
1671 and returndate is not NULL
1672 order by returndate desc,timestamp desc";
1673 $sth2 = $dbh->prepare($query2)
1674 || die $dbh->errstr;
1676 || die $sth2->errstr;
1678 for (my $i2 = 0; $i2 < 2; $i2++) {
1679 if (my $data2 = $sth2->fetchrow_hashref) {
1680 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1681 $data->{"card$i2"} = $data2->{'cardnumber'};
1682 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1687 $results[$i] = $data;
1697 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1699 Looks up the item with the given biblioitemnumber.
1701 C<$env> and C<$dbh> are ignored.
1703 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1704 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1710 my ($env,$dbh,$itemnumber) = @_;
1711 $dbh = C4::Context->dbh;
1712 my $query="Select * from biblio,items,biblioitems
1713 where items.itemnumber = '$itemnumber'
1714 and biblio.biblionumber = items.biblionumber
1715 and biblioitems.biblioitemnumber = items.biblioitemnumber";
1716 my $sth=$dbh->prepare($query);
1719 my $data=$sth->fetchrow_hashref;
1726 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1728 Looks up patrons (borrowers) by name.
1730 C<$env> and C<$type> are ignored.
1732 C<$searchstring> is a space-separated list of search terms. Each term
1733 must match the beginning a borrower's surname, first name, or other
1736 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1737 reference-to-array; each element is a reference-to-hash, whose keys
1738 are the fields of the C<borrowers> table in the Koha database.
1739 C<$count> is the number of elements in C<$borrowers>.
1743 #used by member enquiries from the intranet
1744 #called by member.pl
1746 my ($env,$searchstring,$type)=@_;
1747 my $dbh = C4::Context->dbh;
1748 $searchstring=~ s/\'/\\\'/g;
1749 my @data=split(' ',$searchstring);
1751 my $query="Select * from borrowers
1752 where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\"
1753 or firstname like \"$data[0]%\" or firstname like \"% $data[0]%\"
1754 or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1756 for (my $i=1;$i<$count;$i++){
1757 $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"
1758 or firstname like \"$data[$i]%\" or firstname like \"% $data[$i]%\"
1759 or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1761 $query=$query.") or cardnumber = \"$searchstring\"
1762 order by surname,firstname";
1763 # print $query,"\n";
1764 my $sth=$dbh->prepare($query);
1768 while (my $data=$sth->fetchrow_hashref){
1769 push(@results,$data);
1774 return ($cnt,\@results);
1779 $borrower = &borrdata($cardnumber, $borrowernumber);
1781 Looks up information about a patron (borrower) by either card number
1782 or borrower number. If $borrowernumber is specified, C<&borrdata>
1783 searches by borrower number; otherwise, it searches by card number.
1785 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1786 the C<borrowers> table in the Koha database.
1791 my ($cardnumber,$bornum)=@_;
1792 $cardnumber = uc $cardnumber;
1793 my $dbh = C4::Context->dbh;
1796 $query="Select * from borrowers where cardnumber='$cardnumber'";
1798 $query="Select * from borrowers where borrowernumber='$bornum'";
1801 my $sth=$dbh->prepare($query);
1803 my $data=$sth->fetchrow_hashref;
1810 ($count, $issues) = &borrissues($borrowernumber);
1812 Looks up what the patron with the given borrowernumber has borrowed.
1814 C<&borrissues> returns a two-element array. C<$issues> is a
1815 reference-to-array, where each element is a reference-to-hash; the
1816 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1817 in the Koha database. C<$count> is the number of elements in
1824 my $dbh = C4::Context->dbh;
1826 $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1827 items.itemnumber=issues.itemnumber and
1828 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1831 my $sth=$dbh->prepare($query);
1834 while (my $data = $sth->fetchrow_hashref) {
1835 push @result, $data;
1838 return(scalar(@result), \@result);
1843 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1845 Looks up what the patron with the given borrowernumber has borrowed,
1846 and sorts the results.
1848 C<$sortkey> is the name of a field on which to sort the results. This
1849 should be the name of a field in the C<issues>, C<biblio>,
1850 C<biblioitems>, or C<items> table in the Koha database.
1852 C<$limit> is the maximum number of results to return.
1854 C<&allissues> returns a two-element array. C<$issues> is a
1855 reference-to-array, where each element is a reference-to-hash; the
1856 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1857 C<items> tables of the Koha database. C<$count> is the number of
1858 elements in C<$issues>
1863 my ($bornum,$order,$limit)=@_;
1864 my $dbh = C4::Context->dbh;
1866 $query="Select * from issues,biblio,items,biblioitems
1867 where borrowernumber='$bornum' and
1868 items.biblioitemnumber=biblioitems.biblioitemnumber and
1869 items.itemnumber=issues.itemnumber and
1870 items.biblionumber=biblio.biblionumber";
1871 $query.=" order by $order";
1873 $query.=" limit $limit";
1876 my $sth=$dbh->prepare($query);
1880 while (my $data=$sth->fetchrow_hashref){
1885 return($i,\@result);
1890 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
1892 Returns aggregate data about items borrowed by the patron with the
1893 given borrowernumber.
1897 C<&borrdata2> returns a three-element array. C<$borrowed> is the
1898 number of books the patron currently has borrowed. C<$due> is the
1899 number of overdue items the patron currently has borrowed. C<$fine> is
1900 the total fine currently due by the borrower.
1905 my ($env,$bornum)=@_;
1906 my $dbh = C4::Context->dbh;
1907 my $query="Select count(*) from issues where borrowernumber='$bornum' and
1908 returndate is NULL";
1910 my $sth=$dbh->prepare($query);
1912 my $data=$sth->fetchrow_hashref;
1914 $sth=$dbh->prepare("Select count(*) from issues where
1915 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1917 my $data2=$sth->fetchrow_hashref;
1919 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1920 borrowernumber='$bornum'");
1922 my $data3=$sth->fetchrow_hashref;
1925 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1928 =item getboracctrecord
1930 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1932 Looks up accounting data for the patron with the given borrowernumber.
1936 (FIXME - I'm not at all sure what this is about.)
1938 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1939 reference-to-array, where each element is a reference-to-hash; the
1940 keys are the fields of the C<accountlines> table in the Koha database.
1941 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1942 total amount outstanding for all of the account lines.
1946 sub getboracctrecord {
1947 my ($env,$params) = @_;
1948 my $dbh = C4::Context->dbh;
1951 my $query= "Select * from accountlines where
1952 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1953 my $sth=$dbh->prepare($query);
1957 while (my $data=$sth->fetchrow_hashref){
1958 # if ($data->{'itemnumber'} ne ''){
1959 # $query="Select * from items,biblio where items.itemnumber=
1960 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1961 # my $sth2=$dbh->prepare($query);
1963 # my $data2=$sth2->fetchrow_hashref;
1967 $acctlines[$numlines] = $data;
1969 $total = $total+ $data->{'amountoutstanding'};
1972 return ($numlines,\@acctlines,$total);
1977 ($count, $lcount, $nacount, $fcount, $scount, $lostcount,
1978 $mending, $transit,$ocount) =
1979 &itemcount($env, $biblionumber, $type);
1981 Counts the number of items with the given biblionumber, broken down by
1986 If C<$type> is not set to C<intra>, lost, very overdue, and withdrawn
1987 items will not be counted.
1989 C<&itemcount> returns a nine-element list:
1991 C<$count> is the total number of items with the given biblionumber.
1993 C<$lcount> is the number of items at the Levin branch.
1995 C<$nacount> is the number of items that are neither borrowed, lost,
1996 nor withdrawn (and are therefore presumably on a shelf somewhere).
1998 C<$fcount> is the number of items at the Foxton branch.
2000 C<$scount> is the number of items at the Shannon branch.
2002 C<$lostcount> is the number of lost and very overdue items.
2004 C<$mending> is the number of items at the Mending branch (being
2007 C<$transit> is the number of items at the Transit branch (in transit
2010 C<$ocount> is the number of items that haven't arrived yet
2011 (aqorders.quantity - aqorders.quantityreceived).
2016 # FIXME - There's also a &C4::Acquisitions::itemcount and
2017 # &C4::Biblio::itemcount.
2018 # Since they're all exported, acqui/acquire.pl doesn't compile with -w.
2020 my ($env,$bibnum,$type)=@_;
2021 my $dbh = C4::Context->dbh;
2022 my $query="Select * from items where
2023 biblionumber=$bibnum ";
2024 if ($type ne 'intra'){
2025 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2026 (wthdrawn <> 1 or wthdrawn is NULL)";
2028 my $sth=$dbh->prepare($query);
2040 while (my $data=$sth->fetchrow_hashref){
2042 my $query2="select * from issues,items where issues.itemnumber=
2043 '$data->{'itemnumber'}' and returndate is NULL
2044 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2045 items.itemlost <> 2) or items.itemlost is NULL)
2046 and (wthdrawn <> 1 or wthdrawn is NULL)";
2048 my $sth2=$dbh->prepare($query2);
2050 if (my $data2=$sth2->fetchrow_hashref){
2053 if ($data->{'holdingbranch'} eq 'C' || $data->{'holdingbranch'} eq 'LT'){
2056 if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
2059 if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
2062 if ($data->{'itemlost'} eq '1'){
2065 if ($data->{'itemlost'} eq '2'){
2068 if ($data->{'holdingbranch'} eq 'FM'){
2071 if ($data->{'holdingbranch'} eq 'TR'){
2078 my $query2="Select * from aqorders where biblionumber=$bibnum";
2079 my $sth2=$dbh->prepare($query2);
2081 if (my $data=$sth2->fetchrow_hashref){
2082 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
2087 return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
2092 $counts = &itemcount2($env, $biblionumber, $type);
2094 Counts the number of items with the given biblionumber, broken down by
2099 C<$type> may be either C<intra> or anything else. If it is not set to
2100 C<intra>, then the search will exclude lost, very overdue, and
2103 C<$&itemcount2> returns a reference-to-hash, with the following fields:
2109 The total number of items with this biblionumber.
2113 The number of items on order (aqorders.quantity -
2114 aqorders.quantityreceived).
2118 For each branch that has at least one copy of the book, C<$counts>
2119 will have a key with the branch name, giving the number of copies at
2127 my ($env,$bibnum,$type)=@_;
2128 my $dbh = C4::Context->dbh;
2129 my $query="Select * from items,branches where
2130 biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
2131 if ($type ne 'intra'){
2132 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2133 (wthdrawn <> 1 or wthdrawn is NULL)";
2135 my $sth=$dbh->prepare($query);
2140 while (my $data=$sth->fetchrow_hashref){
2142 my $query2="select * from issues,items where issues.itemnumber=
2143 '$data->{'itemnumber'}' and returndate is NULL
2144 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2145 items.itemlost <> 2) or items.itemlost is NULL)
2146 and (wthdrawn <> 1 or wthdrawn is NULL)";
2148 my $sth2=$dbh->prepare($query2);
2150 # FIXME - fetchrow_hashref() can fail for any number of reasons
2151 # (e.g., a database server crash). Perhaps use a left join of some
2153 if (my $data2=$sth2->fetchrow_hashref){
2154 $counts{'not available'}++;
2156 $counts{$data->{'branchname'}}++;
2160 my $query2="Select * from aqorders where biblionumber=$bibnum and
2161 datecancellationprinted is NULL and quantity > quantityreceived";
2162 my $sth2=$dbh->prepare($query2);
2164 if (my $data=$sth2->fetchrow_hashref){
2165 $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
2174 $description = &ItemType($itemtype);
2176 Given an item type code, returns the description for that type.
2181 # FIXME - I'm pretty sure that after the initial setup, the list of
2182 # item types doesn't change very often. Hence, it seems slow and
2183 # inefficient to make yet another database call to look up information
2184 # that'll only change every few months or years.
2186 # Much better, I think, to automatically build a Perl file that can be
2187 # included in those scripts that require it, e.g.:
2188 # @itemtypes = qw( ART BCD CAS CD F ... );
2190 # ART => "Art Prints",
2191 # BCD => "CD-ROM from book",
2192 # CD => "Compact disc (WN)",
2193 # F => "Free Fiction",
2196 # The web server can then run a cron job to rebuild this file from the
2197 # database every hour or so.
2199 # The same thing goes for branches, book funds, book sellers, currency
2200 # rates, printers, stopwords, and perhaps others.
2203 my $dbh = C4::Context->dbh;
2204 my $query="select description from itemtypes where itemtype='$type'";
2205 my $sth=$dbh->prepare($query);
2207 my $dat=$sth->fetchrow_hashref;
2209 return ($dat->{'description'});
2214 ($count, @results) = &bibitems($biblionumber);
2216 Given the biblionumber for a book, C<&bibitems> looks up that book's
2217 biblioitems (different publications of the same book, the audio book
2218 and film versions, etc.).
2220 C<$count> is the number of elements in C<@results>.
2222 C<@results> is an array of references-to-hash; the keys are the fields
2223 of the C<biblioitems> and C<itemtypes> tables of the Koha database. In
2224 addition, C<itemlost> indicates the availability of the item: if it is
2225 "2", then all copies of the item are long overdue; if it is "1", then
2226 all copies are lost; otherwise, there is at least one copy available.
2232 my $dbh = C4::Context->dbh;
2233 my $query = "SELECT biblioitems.*,
2235 MIN(items.itemlost) as itemlost,
2236 MIN(items.dateaccessioned) as dateaccessioned
2237 FROM biblioitems, itemtypes, items
2238 WHERE biblioitems.biblionumber = ?
2239 AND biblioitems.itemtype = itemtypes.itemtype
2240 AND biblioitems.biblioitemnumber = items.biblioitemnumber
2241 GROUP BY items.biblioitemnumber";
2242 my $sth = $dbh->prepare($query);
2245 $sth->execute($bibnum);
2246 while (my $data = $sth->fetchrow_hashref) {
2247 $results[$count] = $data;
2251 return($count, @results);
2256 @barcodes = &barcodes($biblioitemnumber);
2258 Given a biblioitemnumber, looks up the corresponding items.
2260 Returns an array of references-to-hash; the keys are C<barcode> and
2263 The returned items include very overdue items, but not lost ones.
2268 #called from request.pl
2269 my ($biblioitemnumber)=@_;
2270 my $dbh = C4::Context->dbh;
2271 my $query="SELECT barcode, itemlost, holdingbranch FROM items
2272 WHERE biblioitemnumber = ?
2273 AND (wthdrawn <> 1 OR wthdrawn IS NULL)";
2274 my $sth=$dbh->prepare($query);
2275 $sth->execute($biblioitemnumber);
2278 while (my $data=$sth->fetchrow_hashref){
2279 $barcodes[$i]=$data;
2288 ($count, @websites) = &getwebsites($biblionumber);
2290 Looks up the web sites pertaining to the book with the given
2293 C<$count> is the number of elements in C<@websites>.
2295 C<@websites> is an array of references-to-hash; the keys are the
2296 fields from the C<websites> table in the Koha database.
2301 my ($biblionumber) = @_;
2302 my $dbh = C4::Context->dbh;
2303 my $query = "Select * from websites where biblionumber = $biblionumber";
2304 my $sth = $dbh->prepare($query);
2309 while (my $data = $sth->fetchrow_hashref) {
2310 # FIXME - The URL scheme shouldn't be stripped off, at least
2311 # not here, since it's part of the URL, and will be useful in
2312 # constructing a link to the site. If you don't want the user
2313 # to see the "http://" part, strip that off when building the
2315 $data->{'url'} =~ s/^http:\/\///; # FIXME - Leaning toothpick
2317 $results[$count] = $data;
2322 return($count, @results);
2325 =item getwebbiblioitems
2327 ($count, @results) = &getwebbiblioitems($biblionumber);
2329 Given a book's biblionumber, looks up the web versions of the book
2330 (biblioitems with itemtype C<WEB>).
2332 C<$count> is the number of items in C<@results>. C<@results> is an
2333 array of references-to-hash; the keys are the items from the
2334 C<biblioitems> table of the Koha database.
2338 sub getwebbiblioitems {
2339 my ($biblionumber) = @_;
2340 my $dbh = C4::Context->dbh;
2341 my $query = "Select * from biblioitems where biblionumber = $biblionumber
2342 and itemtype = 'WEB'";
2343 my $sth = $dbh->prepare($query);
2348 while (my $data = $sth->fetchrow_hashref) {
2349 $data->{'url'} =~ s/^http:\/\///;
2350 $results[$count] = $data;
2355 return($count, @results);
2356 } # sub getwebbiblioitems
2359 END { } # module clean-up code here (global destructor)
2368 Koha Developement team <info@koha.org>