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
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
35 C4::Search - Functions for searching the Koha catalog and other databases
41 my ($count, @results) = catalogsearch($env, $type, $search, $num, $offset);
45 This module provides the searching facilities for the Koha catalog and
48 C<&catalogsearch> is a front end to all the other searches. Depending
49 on what is passed to it, it calls the appropriate search function.
58 @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
59 &itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
60 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
61 &getboracctrecord &ItemType &itemissues &subject &subtitle
62 &addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs
63 &findguarantor &getwebsites &getwebbiblioitems &catalogsearch &itemcount2);
64 # make all your functions, whether exported or not;
68 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
69 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
70 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
72 C<&findguarantees> takes a borrower number (e.g., that of a patron
73 with children) and looks up the borrowers who are guaranteed by that
74 borrower (i.e., the patron's children).
76 C<&findguarantees> returns two values: an integer giving the number of
77 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
78 of references to hash, which gives the actual results.
85 my $query="select cardnumber,borrowernumber from borrowers where
87 my $sth=$dbh->prepare($query);
91 while (my $data=$sth->fetchrow_hashref){
102 $guarantor = &findguarantor($borrower_no);
103 $guarantor_cardno = $guarantor->{"cardnumber"};
104 $guarantor_surname = $guarantor->{"surname"};
107 C<&findguarantor> takes a borrower number (presumably that of a child
108 patron), finds the guarantor for C<$borrower_no> (the child's parent),
109 and returns the record for the guarantor.
111 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
112 from the C<borrowers> database table;
119 my $query="select guarantor from borrowers where
120 borrowernumber='$bornum'";
121 my $sth=$dbh->prepare($query);
123 my $data=$sth->fetchrow_hashref;
125 $query="Select * from borrowers where
126 borrowernumber='$data->{'guarantor'}'";
127 $sth=$dbh->prepare($query);
129 $data=$sth->fetchrow_hashref;
137 %prefs = &systemprefs();
139 Returns a hash giving the system preferences. This is basically just a
140 dump of the C<systempreferences> database table.
147 my $sth=$dbh->prepare("select variable,value from systempreferences");
149 while (my ($variable,$value)=$sth->fetchrow) {
150 $systemprefs{$variable}=$value;
154 return(%systemprefs);
157 =item NewBorrowerNumber
159 $num = &NewBorrowerNumber();
161 Allocates a new, unused borrower number, and returns it.
165 sub NewBorrowerNumber {
167 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
169 my $data=$sth->fetchrow_hashref;
171 $data->{'max(borrowernumber)'}++;
173 return($data->{'max(borrowernumber)'});
178 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
180 This is primarily a front-end to other, more specialized catalog
181 search functions: if C<$search-E<gt>{itemnumber}> or
182 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
183 C<&CatSearch>. If $search->{subject} is given, it runs a subject
184 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
185 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
187 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
188 the items for each result, and adds several keys:
194 The total number of copies of this book.
196 =item C<locationhash>
198 This is a reference-to-hash; the keys are the names of branches where
199 this book may be found, and the values are the number of copies at
204 A descriptive string saying where the book is located, and how many
205 copies there are, if greater than 1.
209 The book's subject, with spaces replaced with C<%20>, presumably for
217 my ($env,$type,$search,$num,$offset)=@_;
218 my $dbh = C4Connect();
219 # foreach my $key (%$search){
220 # $search->{$key}=$dbh->quote($search->{$key});
222 my ($count,@results);
223 # print STDERR "Doing a search \n";
224 # FIXME - Use "elsif" to avoid this sort of deep nesting
225 if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
226 print STDERR "Doing a precise search\n";
227 ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
230 if ($search->{'subject'} ne ''){
231 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
233 if ($search->{'keyword'} ne ''){
234 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
236 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
241 if ($env->{itemcount} eq '1') {
242 foreach my $data (@results){
243 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
244 my $subject2=$data->{'subject'};
245 $subject2=~ s/ /%20/g;
246 $data->{'itemcount'}=$counts->{'total'};
247 my $totalitemcounts=0;
248 foreach my $key (keys %$counts){
249 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
250 #$data->{'location'}.="$key $counts->{$key} ";
251 $totalitemcounts+=$counts->{$key};
252 $data->{'locationhash'}->{$key}=$counts->{$key};
256 my $notavailabletext='';
257 foreach (sort keys %{$data->{'locationhash'}}) {
258 if ($_ eq 'notavailable') {
259 $notavailabletext="Not available";
260 my $c=$data->{'locationhash'}->{$_};
261 if ($totalitemcounts>1) {
262 $notavailabletext.=" ($c)";
266 my $c=$data->{'locationhash'}->{$_};
267 if ($totalitemcounts>1) {
268 $locationtext.=" ($c), ";
272 if ($notavailabletext) {
273 $locationtext.=$notavailabletext;
275 $locationtext=~s/, $//;
277 $data->{'location'}=$locationtext;
278 $data->{'subject2'}=$subject2;
281 return ($count,@results);
286 $search = { "keyword" => "One or more keywords",
287 "class" => "VID|CD", # Limit search to fiction and CDs
290 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
292 C<&KeywordSearch> searches the catalog by keyword: given a string
293 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
294 keywords, it looks for books that contain any of those keywords in any
295 of a number of places.
297 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
298 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
301 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
302 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
303 etc.). In this case, the search will be restricted to just those
306 If C<$search-E<gt>{"class"}> is not specified, you may specify
307 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
308 particular Dewey Decimal Classification category. Setting
309 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
310 whereas setting it to "5" will return all books with Dewey code 5I<xx>
311 (Science and Mathematics).
313 C<$env> and C<$type> are ignored.
315 C<$offset> and C<$num> specify the subset of results to return.
316 C<$num> specifies the number of results to return, and C<$offset> is
317 the number of the first result. Thus, setting C<$offset> to 100 and
318 C<$num> to 5 will return results 100 through 104 inclusive.
323 # FIXME - Everything that's being done here with Set::Scalar can be
324 # done with hashes: to add a result to the set, just use:
325 # $results{$foo} = 1;
326 # To get the list of results, just use
327 # @biblionumbers = sort keys %results;
328 # This would remove the dependency on Set::Scalar, which means one
329 # fewer package that people have to install.
332 my ($env,$type,$search,$num,$offset)=@_;
333 my $dbh = &C4Connect;
334 $search->{'keyword'}=~ s/ +$//;
335 $search->{'keyword'}=~ s/'/\\'/;
336 my @key=split(' ',$search->{'keyword'});
341 # Look for keywords in table 'biblio'.
343 # FIXME - The next 15 lines can be rewritten in somewhat Lisp-ish
344 # fashion as follows:
346 # my $query = "select biblionumber from biblio where (" .
352 # "($field like '$_\%' or $field like '\% $_\%')"
356 # qw( title biblio.notes seriestitle )) .
359 # field like 'string%' or field like '% string%'
360 # can be rewritten (in MySQL, at least) as
361 # field regexp '(^| )string';
362 # However, this isn't portable. Though PostgreSQL allows you to use "~"
363 # instead of "regexp".
364 my $query="Select biblionumber from biblio
365 where ((title like '$key[0]%' or title like '% $key[0]%')";
367 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
370 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
371 for ($i=1;$i<$count;$i++){
372 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
374 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
375 for ($i=1;$i<$count;$i++){
376 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
380 my $sth=$dbh->prepare($query);
383 while (my @res=$sth->fetchrow_array){
384 $results[$i]=$res[0];
388 my $set1=Set::Scalar->new(@results);
390 # Now look for keywords in the 'bibliosubtitle' table.
391 $query="Select biblionumber from bibliosubtitle where
392 ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
393 for ($i=1;$i<$count;$i++){
394 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
398 $sth=$dbh->prepare($query);
401 while (my @res=$sth->fetchrow_array){
402 $results[$i]=$res[0];
406 my $set2=Set::Scalar->new(@results);
411 # Look for the keywords in the notes for individual items
412 # ('biblioitems.notes')
413 $query ="Select biblionumber from biblioitems where
414 ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
415 for ($i=1;$i<$count;$i++){
416 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
420 $sth=$dbh->prepare($query);
423 while (my @res=$sth->fetchrow_array){
424 $results[$i]=$res[0];
428 my $set3=Set::Scalar->new(@results);
433 # Look for keywords in the 'bibliosubject' table.
434 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
435 like '%$search->{'keyword'}%' group by biblionumber");
438 while (my @res=$sth->fetchrow_array){
439 $results[$i]=$res[0];
443 my $set4=Set::Scalar->new(@results);
452 my @res = $set1->members;
456 # print "count $count";
457 if ($search->{'class'} ne ''){
459 my $query="select * from biblio,biblioitems where
460 biblio.biblionumber='$res[$i2]' and
461 biblio.biblionumber=biblioitems.biblionumber ";
462 if ($search->{'class'} ne ''){ # FIXME - Redundant
463 my @temp=split(/\|/,$search->{'class'});
465 $query.= "and ( itemtype='$temp[0]'";
466 for (my $i=1;$i<$count;$i++){
467 $query.=" or itemtype='$temp[$i]'";
471 my $sth=$dbh->prepare($query);
474 if (my $data2=$sth->fetchrow_hashref){
475 my $dewey= $data2->{'dewey'};
476 my $subclass=$data2->{'subclass'};
477 # FIXME - This next bit is bogus, because it assumes that the
478 # Dewey code is a floating-point number. It isn't. It's
479 # actually a string that mainly consists of numbers. In
480 # particular, "4" is not a valid Dewey code, although "004"
481 # is ("Data processing; Computer science"). Likewise, zeros
482 # after the decimal are significant ("575" is not the same as
483 # "575.0"; the latter is more specific). And "000" is a
484 # perfectly good Dewey code ("General works; computer
485 # science") and should not be interpreted to mean "this
486 # database entry does not have a Dewey code". That's what
489 ($dewey == 0) && ($dewey='');
490 ($dewey) && ($dewey.=" $subclass") ;
492 my $end=$offset +$num;
497 if ($i4 <=$end && $i4 > $offset){
498 $data2->{'dewey'}=$dewey;
501 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
504 # print "in here $i3<br>";
515 # $search->{'class'} was not specified
516 while ($i2 < $num && $i2 < $count){
517 my $query="select * from biblio,biblioitems where
518 biblio.biblionumber='$res[$i2+$offset]' and
519 biblio.biblionumber=biblioitems.biblionumber ";
521 if ($search->{'class'} ne ''){ # FIXME - Ignored: we already know
522 # that $search->{'class'} eq '';
523 my @temp=split(/\|/,$search->{'class'});
525 $query.= "and ( itemtype='$temp[0]'";
526 for (my $i=1;$i<$count;$i++){
527 $query.=" or itemtype='$temp[$i]'";
531 if ($search->{'dewey'} ne ''){
532 $query.= "and (dewey like '$search->{'dewey'}%') ";
535 my $sth=$dbh->prepare($query);
538 if (my $data2=$sth->fetchrow_hashref){
539 my $dewey= $data2->{'dewey'};
540 my $subclass=$data2->{'subclass'};
542 ($dewey == 0) && ($dewey='');
543 ($dewey) && ($dewey.=" $subclass") ;
545 $data2->{'dewey'}=$dewey;
548 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
558 return($count,@res2);
562 my ($env,$type,$search,$num,$offset)=@_;
563 my $dbh = &C4Connect;
564 $search->{'keyword'}=~ s/ +$//;
565 $search->{'keyword'}=~ s/'/\\'/;
566 my @key=split(' ',$search->{'keyword'});
570 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
571 biblio.biblionumber=biblioitems.biblionumber and
572 biblio.biblionumber=bibliosubtitle.biblionumber and
573 (((title like '$key[0]%' or title like '% $key[0]%')";
575 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
578 $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
579 for ($i=1;$i<$count;$i++){
580 $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
582 $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
583 for ($i=1;$i<$count;$i++){
584 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
586 $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
587 for ($i=1;$i<$count;$i++){
588 $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
590 $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
591 for ($i=1;$i<$count;$i++){
592 $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
594 if ($search->{'keyword'} =~ /new zealand/i){
595 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
596 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
597 or author like '% nz %' or author like '% nz')"
599 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
600 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
601 $search->{'keyword'} =~ / nz/i){
602 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
603 or title like '% new zealand' or subtitle like 'new zealand%' or
604 subtitle like '% new zealand %'
605 or subtitle like '% new zealand' or author like 'new zealand%'
606 or author like '% new zealand %' or author like '% new zealand' or
607 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
608 or seriestitle like '% new zealand')"
611 if ($search->{'class'} ne ''){
612 my @temp=split(/\|/,$search->{'class'});
614 $query.= "and ( itemtype='$temp[0]'";
615 for (my $i=1;$i<$count;$i++){
616 $query.=" or itemtype='$temp[$i]'";
620 if ($search->{'dewey'} ne ''){
621 $query.= "and (dewey like '$search->{'dewey'}%') ";
623 $query.="group by biblio.biblionumber";
624 #$query.=" order by author,title";
626 my $sth=$dbh->prepare($query);
629 while (my $data=$sth->fetchrow_hashref){
630 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
633 # my ($dewey, $subclass) = $sti->fetchrow;
634 my $dewey=$data->{'dewey'};
635 my $subclass=$data->{'subclass'};
637 ($dewey == 0) && ($dewey='');
638 ($dewey) && ($dewey.=" $subclass");
640 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
641 # print $results[$i];
645 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
646 like '%$search->{'keyword'}%' group by biblionumber");
648 while (my $data=$sth->fetchrow_hashref){
649 $query="Select * from biblio,biblioitems where
650 biblio.biblionumber=$data->{'biblionumber'} and
651 biblio.biblionumber=biblioitems.biblionumber ";
652 if ($search->{'class'} ne ''){
653 my @temp=split(/\|/,$search->{'class'});
655 $query.= " and ( itemtype='$temp[0]'";
656 for (my $i=1;$i<$count;$i++){
657 $query.=" or itemtype='$temp[$i]'";
662 if ($search->{'dewey'} ne ''){
663 $query.= "and (dewey like '$search->{'dewey'}%') ";
665 my $sth2=$dbh->prepare($query);
668 while (my $data2=$sth2->fetchrow_hashref){
669 my $dewey= $data2->{'dewey'};
670 my $subclass=$data2->{'subclass'};
672 ($dewey == 0) && ($dewey='');
673 ($dewey) && ($dewey.=" $subclass") ;
675 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
676 # print $results[$i];
682 @results=sort @results;
689 while ($i2 < $count){
690 if ($results[$i2] ne $res[$i-1]){
691 $res[$i]=$results[$i2];
699 while ($i2 < $num && $i2 < $count){
700 $res2[$i2]=$res[$i2+$offset];
713 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
715 C<&CatSearch> searches the Koha catalog. It returns a list whose first
716 element is the number of returned results, and whose subsequent
717 elements are the results themselves.
719 Each returned element is a reference-to-hash. Most of the keys are
720 simply the fields from the C<biblio> table in the Koha database, but
721 the following keys may also be present:
727 The book's illustrator.
737 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
738 high-level behavior of C<&CatSearch>, as described below.
740 In many cases, the description below says that a certain field in the
741 database must match the search string. In these cases, it means that
742 the beginning of some word in the field must match the search string.
743 Thus, an author search for "sm" will return books whose author is
744 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
745 does not occur at the beginning of a word.
747 Note that within each search mode, the criteria are and-ed together.
748 That is, if you perform a loose search on the author "Jerome" and the
749 title "Boat", the search will only return books by Jerome containing
752 It is not possible to cross modes, e.g., set the author to "Asimov"
753 and the subject to "Math" in hopes of finding books on math by Asimov.
757 If C<$type> is set to C<loose>, the following search criteria may be
762 =item C<$search-E<gt>{author}>
764 The search string is a space-separated list of words. Each word must
765 match either the C<author> or C<additionalauthors> field.
767 =item C<$search-E<gt>{title}>
769 Each word in the search string must match the book title. If no author
770 is specified, the book subtitle will also be searched.
772 =item C<$search-E<gt>{abstract}>
774 Searches for the given search string in the book's abstract.
776 =item C<$search-E<gt>{'date-before'}>
778 Searches for books whose copyright date matches the search string.
779 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
780 books written in 1985, and setting it to "198" will find books written
781 between 1980 and 1989.
783 =item C<$search-E<gt>{title}>
785 Searches by title are also affected by the value of
786 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
787 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
788 match the search string exactly (the subtitle is not searched).
790 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
791 each word in the search string must match the title, subtitle,
792 unititle, or series title.
794 =item C<$search-E<gt>{class}>
796 Restricts the search to certain item classes. The value of
797 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
798 Thus, setting it to "F" restricts the search to fiction, and setting
799 it to "CD|CAS" will only look in compact disks and cassettes.
801 =item C<$search-E<gt>{dewey}>
803 Searches for books whose Dewey Decimal Classification code matches the
804 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
805 search for all books in 5I<xx> (Science and mathematics), setting it
806 to "54" will search for all books in 54I<x> (Chemistry), and setting
807 it to "546" will search for books on inorganic chemistry.
809 =item C<$search-E<gt>{publisher}>
811 Searches for books whose publisher contains the search string (unlike
812 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
817 =head2 Subject search
819 If C<$type> is set to C<subject>, the following search criteria may be
824 =item C<$search-E<gt>{subject}>
826 The search string is a space-separated list of words, each of which
827 must match the book's subject.
829 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
830 C<&CatSearch> will search for books whose subject is "New Zealand".
831 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
832 search for books on "nz" and "football", not books on "New Zealand"
837 =head2 Precise search
839 If C<$type> is set to C<precise>, the following search criteria may be
844 =item C<$search-E<gt>{item}>
846 Searches for books whose barcode exactly matches the search string.
848 =item C<$search-E<gt>{isbn}>
850 Searches for books whose ISBN exactly matches the search string.
854 For a loose search, if an author was specified, the results are
855 ordered by author and title. If no author was specified, the results
856 are ordered by title.
858 For other (non-loose) searches, if a subject was specified, the
859 results are ordered alphabetically by subject.
861 In all other cases (e.g., loose search by keyword), the results are
867 my ($env,$type,$search,$num,$offset)=@_;
868 my $dbh = &C4Connect;
871 # FIXME - Why not just
872 # $search->{'title'} = quotemeta($search->{'title'})
873 # to escape all questionable characters, not just single-quotes?
874 $search->{'title'}=~ s/'/\\'/g;
875 $search->{'author'}=~ s/'/\\'/g;
876 $search->{'illustrator'}=~ s/'/\\'/g;
877 my $title = lc($search->{'title'});
879 if ($type eq 'loose') {
880 if ($search->{'author'} ne ''){
881 my @key=split(' ',$search->{'author'});
884 $query="select *,biblio.author,biblio.biblionumber from
886 left join additionalauthors
887 on additionalauthors.biblionumber =biblio.biblionumber
889 ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
890 additionalauthors.author like '$key[0]%' or additionalauthors.author
894 $query=$query." and (
895 biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
896 additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
901 if ($search->{'title'} ne ''){
902 my @key=split(' ',$search->{'title'});
905 $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
907 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
910 # $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')";
911 # for ($i=1;$i<$count;$i++){
912 # $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";
914 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
915 for ($i=1;$i<$count;$i++){
916 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
918 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
919 for ($i=1;$i<$count;$i++){
920 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
923 #$query=$query. " and (title like '%$search->{'title'}%'
924 #or seriestitle like '%$search->{'title'}%')";
926 if ($search->{'abstract'} ne ''){
927 $query.= " and (abstract like '%$search->{'abstract'}%')";
929 if ($search->{'date-before'} ne ''){
930 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
933 $query.=" group by biblio.biblionumber";
935 if ($search->{'title'} ne '') {
936 if ($search->{'ttype'} eq 'exact'){
937 $query="select * from biblio
939 (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
940 or biblio.unititle like '$search->{'title'} |%' or
941 biblio.unititle like '%| $search->{'title'} |%' or
942 biblio.unititle like '%| $search->{'title'}') or
943 (biblio.seriestitle = '$search->{'title'}' or
944 biblio.seriestitle like '$search->{'title'} |%' or
945 biblio.seriestitle like '%| $search->{'title'} |%' or
946 biblio.seriestitle like '%| $search->{'title'}')
949 my @key=split(' ',$search->{'title'});
952 $query="select * from biblio
953 left join bibliosubtitle on
954 biblio.biblionumber=bibliosubtitle.biblionumber
956 (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
958 $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
961 $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
962 for ($i=1;$i<$count;$i++){
963 $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
965 $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
966 for ($i=1;$i<$count;$i++){
967 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
969 $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
970 for ($i=1;$i<$count;$i++){
971 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
975 if ($search->{'abstract'} ne ''){
976 $query.= " and (abstract like '%$search->{'abstract'}%')";
978 if ($search->{'date-before'} ne ''){
979 $query.= " and (copyrightdate like '%$search->{'date-before'}%')";
981 } elsif ($search->{'class'} ne ''){
982 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
983 my @temp=split(/\|/,$search->{'class'});
985 $query.= " and ( itemtype='$temp[0]'";
986 for (my $i=1;$i<$count;$i++){
987 $query.=" or itemtype='$temp[$i]'";
990 if ($search->{'illustrator'} ne ''){
991 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
993 if ($search->{'dewey'} ne ''){
994 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
996 } elsif ($search->{'dewey'} ne ''){
997 $query="select * from biblioitems,biblio
998 where biblio.biblionumber=biblioitems.biblionumber
999 and biblioitems.dewey like '$search->{'dewey'}%'";
1000 } elsif ($search->{'illustrator'} ne '') {
1001 $query="select * from biblioitems,biblio
1002 where biblio.biblionumber=biblioitems.biblionumber
1003 and biblioitems.illus like '%".$search->{'illustrator'}."%'";
1004 } elsif ($search->{'publisher'} ne ''){
1005 $query.= "Select * from biblio,biblioitems where biblio.biblionumber
1006 =biblioitems.biblionumber and (publishercode like '%$search->{'publisher'}%')";
1007 } elsif ($search->{'abstract'} ne ''){
1008 $query.= "Select * from biblio where abstract like '%$search->{'abstract'}%'";
1010 } elsif ($search->{'date-before'} ne ''){
1011 $query.= "Select * from biblio where copyrightdate like '%$search->{'date-before'}%'";
1013 $query .=" group by biblio.biblionumber";
1016 if ($type eq 'subject'){
1017 my @key=split(' ',$search->{'subject'});
1020 $query="select distinct(subject) from bibliosubject where( subject like
1021 '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
1023 $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
1024 or subject like '% $key[$i]'
1025 or subject like '%($key[$i])%')";
1029 # FIXME - Wouldn't it be better to fix the database so that if a
1030 # book has a subject "NZ", then it also gets added the subject
1032 # This can also be generalized by adding a table of subject
1033 # synonyms to the database: just declare "NZ" to be a synonym for
1034 # "New Zealand", "SF" a synonym for both "Science fiction" and
1035 # "Fantastic fiction", etc.
1037 # FIXME - This can be rewritten as
1038 # if (lc($search->{"subject"}) eq "nz") {
1039 if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){
1040 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1041 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1042 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1043 $query=~ s/ nz/ NEW ZEALAND/ig;
1044 $query=~ s/nz /NEW ZEALAND /ig;
1045 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1048 if ($type eq 'precise'){
1050 if ($search->{'item'} ne ''){
1051 $query="select * from items,biblio ";
1052 my $search2=uc $search->{'item'};
1053 $query=$query." where
1054 items.biblionumber=biblio.biblionumber
1055 and barcode='$search2'";
1057 if ($search->{'isbn'} ne ''){
1058 my $search2=uc $search->{'isbn'};
1059 my $query1 = "select * from biblioitems where isbn='$search2'";
1060 my $sth1=$dbh->prepare($query1);
1061 # print STDERR "$query1\n";
1064 while (my $data=$sth1->fetchrow_hashref) {
1065 $query="select * from biblioitems,biblio where
1066 biblio.biblionumber = $data->{'biblionumber'}
1067 and biblioitems.biblionumber = biblio.biblionumber";
1068 my $sth=$dbh->prepare($query);
1070 my $data=$sth->fetchrow_hashref;
1071 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1072 # FIXME - The following assumes that the Dewey code is a
1073 # floating-point number. It isn't: it's a string.
1075 ($dewey == 0) && ($dewey='');
1076 ($dewey) && ($dewey.=" $subclass");
1077 $data->{'dewey'}=$dewey;
1078 $results[$i2]=$data;
1079 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1087 if ($type ne 'precise' && $type ne 'subject'){
1088 if ($search->{'author'} ne ''){
1089 $query=$query." order by biblio.author,title";
1091 $query=$query." order by title";
1094 if ($type eq 'subject'){
1095 $query=$query." order by subject";
1098 #print STDERR "$query\n";
1099 my $sth=$dbh->prepare($query);
1103 my $limit= $num+$offset;
1104 while (my $data=$sth->fetchrow_hashref){
1105 my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=$data->{'biblionumber'}";
1106 if ($search->{'class'} ne ''){
1107 my @temp=split(/\|/,$search->{'class'});
1109 $query.= " and ( itemtype='$temp[0]'";
1110 for (my $i=1;$i<$count;$i++){
1111 $query.=" or itemtype='$temp[$i]'";
1115 if ($search->{'dewey'} ne ''){
1116 $query.=" and dewey='$search->{'dewey'}' ";
1118 if ($search->{'illustrator'} ne ''){
1119 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
1121 if ($search->{'publisher'} ne ''){
1122 $query.= " and (publishercode like '%$search->{'publisher'}%')";
1125 my $sti=$dbh->prepare($query);
1132 if ($bibitemdata = $sti->fetchrow_hashref() || $type eq 'subject'){
1134 $dewey=$bibitemdata->{'dewey'};
1135 $subclass=$bibitemdata->{'subclass'};
1136 $publishercode=$bibitemdata->{'publishercode'};
1138 print STDERR "$dewey $subclass $publishercode\n";
1140 ($dewey == 0) && ($dewey='');
1141 ($dewey) && ($dewey.=" $subclass");
1142 $data->{'dewey'}=$dewey;
1143 $data->{'publishercode'}=$publishercode;
1146 if ($count > $offset && $count <= $limit){
1154 #if ($type ne 'precise'){
1158 return($count,@results);
1161 sub updatesearchstats{
1162 my ($dbh,$query)=@_;
1168 @results = &subsearch($env, $subject);
1170 Searches for books that have a subject that exactly matches
1173 C<&subsearch> returns an array of results. Each element of this array
1174 is a string, containing the book's title, author, and biblionumber,
1182 my ($env,$subject)=@_;
1183 my $dbh=C4Connect();
1184 $subject=$dbh->quote($subject);
1185 my $query="Select * from biblio,bibliosubject where
1186 biblio.biblionumber=bibliosubject.biblionumber and
1187 bibliosubject.subject=$subject group by biblio.biblionumber
1188 order by biblio.title";
1189 my $sth=$dbh->prepare($query);
1194 while (my $data=$sth->fetchrow_hashref){
1195 $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
1205 @results = &ItemInfo($env, $biblionumber, $type);
1207 Returns information about books with the given biblionumber.
1209 C<$type> may be either C<intra> or anything else. If it is not set to
1210 C<intra>, then the search will exclude lost, very overdue, and
1215 C<&ItemInfo> returns a list of references-to-hash. Each element
1216 contains a number of keys. Most of them are table items from the
1217 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1218 Koha database. Other keys include:
1222 =item C<$data-E<gt>{branchname}>
1224 The name (not the code) of the branch to which the book belongs.
1226 =item C<$data-E<gt>{datelastseen}>
1228 This is simply C<items.datelastseen>, except that while the date is
1229 stored in YYYY-MM-DD format in the database, here it is converted to
1230 DD/MM/YYYY format. A NULL date is returned as C<//>.
1232 =item C<$data-E<gt>{datedue}>
1234 =item C<$data-E<gt>{class}>
1236 This is the concatenation of C<biblioitems.classification>, the book's
1237 Dewey code, and C<biblioitems.subclass>.
1239 =item C<$data-E<gt>{ocount}>
1241 I think this is the number of copies of the book available.
1243 =item C<$data-E<gt>{order}>
1245 If this is set, it is set to C<One Order>.
1250 my ($env,$biblionumber,$type) = @_;
1251 my $dbh = &C4Connect;
1252 my $query = "SELECT * FROM items, biblio, biblioitems, itemtypes
1253 WHERE items.biblionumber = ?
1254 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1255 AND biblioitems.itemtype = itemtypes.itemtype
1256 AND biblio.biblionumber = items.biblionumber";
1257 if ($type ne 'intra'){
1258 $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1259 or items.itemlost is NULL)
1260 and (wthdrawn <> 1 or wthdrawn is NULL)";
1262 $query .= " order by items.dateaccessioned desc";
1264 my $sth=$dbh->prepare($query);
1265 $sth->execute($biblionumber);
1269 while (my $data=$sth->fetchrow_hashref){
1270 my $iquery = "Select * from issues
1271 where itemnumber = '$data->{'itemnumber'}'
1272 and returndate is null";
1274 my $isth=$dbh->prepare($iquery);
1276 if (my $idata=$isth->fetchrow_hashref){
1277 # FIXME - The date ought to be properly parsed, and printed
1278 # according to local convention.
1279 my @temp=split('-',$idata->{'date_due'});
1280 $datedue = "$temp[2]/$temp[1]/$temp[0]";
1282 if ($data->{'itemlost'} eq '2'){
1283 $datedue='Very Overdue';
1285 if ($data->{'itemlost'} eq '1'){
1288 if ($data->{'wthdrawn'} eq '1'){
1289 $datedue="Cancelled";
1291 if ($datedue eq ''){
1292 $datedue="Available";
1293 my ($restype,$reserves)=CheckReserves($data->{'itemnumber'});
1299 #get branch information.....
1300 my $bquery = "SELECT * FROM branches
1301 WHERE branchcode = '$data->{'holdingbranch'}'";
1302 my $bsth=$dbh->prepare($bquery);
1304 if (my $bdata=$bsth->fetchrow_hashref){
1305 $data->{'branchname'} = $bdata->{'branchname'};
1308 my $class = $data->{'classification'};
1309 my $dewey = $data->{'dewey'};
1311 if ($dewey eq "000.") { $dewey = "";}; # FIXME - "000" is general
1312 # books about computer science
1313 if ($dewey < 10){$dewey='00'.$dewey;}
1314 if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
1319 $class = $class.$dewey;
1321 $class = $class.$data->{'subclass'};
1323 # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
1324 # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
1325 # to leave it empty, rather than convert it to "//".
1326 # Also ideally this should use the local format for displaying dates.
1327 my @temp=split('-',$data->{'datelastseen'});
1328 my $date="$temp[2]/$temp[1]/$temp[0]";
1329 $data->{'datelastseen'}=$date;
1330 $data->{'datedue'}=$datedue;
1331 $data->{'class'}=$class;
1336 my $query2="Select * from aqorders where biblionumber=$biblionumber";
1337 my $sth2=$dbh->prepare($query2);
1341 if ($data=$sth2->fetchrow_hashref){
1342 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1344 $data->{'ocount'}=$ocount;
1345 $data->{'order'}="One Order";
1357 @results = &GetItems($env, $biblionumber);
1359 Returns information about books with the given biblionumber.
1363 C<&GetItems> returns an array of strings. Each element is a
1364 tab-separated list of values: biblioitemnumber, itemtype,
1365 classification, Dewey number, subclass, ISBN, volume, number, and
1368 Itemdata, in turn, is a string of the form
1369 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1370 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1376 my ($env,$biblionumber)=@_;
1377 #debug_msg($env,"GetItems");
1378 my $dbh = &C4Connect;
1379 my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
1380 #debug_msg($env,$query);
1381 my $sth=$dbh->prepare($query);
1383 #debug_msg($env,"executed query");
1386 while (my $data=$sth->fetchrow_hashref) {
1387 #debug_msg($env,$data->{'biblioitemnumber'});
1388 my $dewey = $data->{'dewey'};
1390 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1391 $line = $line."\t$data->{'classification'}\t$dewey";
1392 $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
1393 $line = $line."\t$data->{'volume'}\t$data->{number}";
1394 my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
1396 while (my $idata = $isth->fetchrow_hashref) {
1397 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1398 if ($idata->{'notforloan'} == 1) {
1399 $iline = $iline."NFL ";
1401 if ($idata->{'itemlost'} == 1) {
1402 $iline = $iline."LOST ";
1404 $line = $line."\t$iline";
1407 $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
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;
1442 $data = &bibdata($biblionumber, $type);
1444 Returns information about the book with the given biblionumber.
1446 C<$type> is ignored.
1448 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1449 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1452 In addition, C<$data-E<gt>{subject}> is the list of the book's
1453 subjects, separated by C<" , "> (space, comma, space).
1455 If there are multiple biblioitems with the given biblionumber, only
1456 the first one is considered.
1461 my ($bibnum, $type) = @_;
1462 my $dbh = C4Connect;
1463 my $query = "Select *, biblio.notes
1464 from biblio, biblioitems
1465 left join bibliosubtitle on
1466 biblio.biblionumber = bibliosubtitle.biblionumber
1467 where biblio.biblionumber = $bibnum
1468 and biblioitems.biblionumber = $bibnum";
1469 my $sth = $dbh->prepare($query);
1473 $data = $sth->fetchrow_hashref;
1476 $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
1477 $sth = $dbh->prepare($query);
1479 while (my $dat = $sth->fetchrow_hashref){
1480 $data->{'subject'} .= " , $dat->{'subject'}";
1490 $itemdata = &bibitemdata($biblioitemnumber);
1492 Looks up the biblioitem with the given biblioitemnumber. Returns a
1493 reference-to-hash. The keys are the fields from the C<biblio>,
1494 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1495 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1501 my $dbh = C4Connect;
1502 my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
1503 where biblio.biblionumber = biblioitems.biblionumber
1504 and biblioitemnumber = $bibitem
1505 and biblioitems.itemtype = itemtypes.itemtype";
1506 my $sth = $dbh->prepare($query);
1511 $data = $sth->fetchrow_hashref;
1520 ($count, $subjects) = &subject($biblionumber);
1522 Looks up the subjects of the book with the given biblionumber. Returns
1523 a two-element list. C<$subjects> is a reference-to-array, where each
1524 element is a subject of the book, and C<$count> is the number of
1525 elements in C<$subjects>.
1532 my $query="Select * from bibliosubject where biblionumber=$bibnum";
1533 my $sth=$dbh->prepare($query);
1537 while (my $data=$sth->fetchrow_hashref){
1543 return($i,\@results);
1548 ($count, $authors) = &addauthors($biblionumber);
1550 Looks up the additional authors for the book with the given
1553 Returns a two-element list. C<$authors> is a reference-to-array, where
1554 each element is an additional author, and C<$count> is the number of
1555 elements in C<$authors>.
1562 my $query="Select * from additionalauthors where biblionumber=$bibnum";
1563 my $sth=$dbh->prepare($query);
1567 while (my $data=$sth->fetchrow_hashref){
1573 return($i,\@results);
1578 ($count, $subtitles) = &subtitle($biblionumber);
1580 Looks up the subtitles for the book with the given biblionumber.
1582 Returns a two-element list. C<$subtitles> is a reference-to-array,
1583 where each element is a subtitle, and C<$count> is the number of
1584 elements in C<$subtitles>.
1591 my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1592 my $sth=$dbh->prepare($query);
1596 while (my $data=$sth->fetchrow_hashref){
1602 return($i,\@results);
1607 @issues = &itemissues($biblioitemnumber, $biblio);
1609 Looks up information about who has borrowed the bookZ<>(s) with the
1610 given biblioitemnumber.
1612 C<$biblio> is ignored.
1614 C<&itemissues> returns an array of references-to-hash. The keys
1615 include the fields from the C<items> table in the Koha database.
1616 Additional keys include:
1622 If the item is currently on loan, this gives the due date.
1624 If the item is not on loan, then this is either "Available" or
1625 "Cancelled", if the item has been withdrawn.
1629 If the item is currently on loan, this gives the card number of the
1630 patron who currently has the item.
1632 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1634 These give the timestamp for the last three times the item was
1637 =item C<card0>, C<card1>, C<card2>
1639 The card number of the last three patrons who borrowed this item.
1641 =item C<borrower0>, C<borrower1>, C<borrower2>
1643 The borrower number of the last three patrons who borrowed this item.
1650 my ($bibitem, $biblio)=@_;
1651 my $dbh = C4Connect;
1652 my $query = "Select * from items where
1653 items.biblioitemnumber = '$bibitem'";
1654 # FIXME - If this function die()s, the script will abort, and the
1655 # user won't get anything; depending on how far the script has
1656 # gotten, the user might get a blank page. It would be much better
1657 # to at least print an error message. The easiest way to do this
1658 # is to set $SIG{__DIE__}.
1659 my $sth = $dbh->prepare($query)
1660 || die $dbh->errstr;
1665 || die $sth->errstr;
1667 while (my $data = $sth->fetchrow_hashref) {
1668 # Find out who currently has this item.
1669 # FIXME - Wouldn't it be better to do this as a left join of
1670 # some sort? Currently, this code assumes that if
1671 # fetchrow_hashref() fails, then the book is on the shelf.
1672 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1673 # database server crash), not just because no items match the
1675 my $query2 = "select * from issues,borrowers
1676 where itemnumber = $data->{'itemnumber'}
1677 and returndate is NULL
1678 and issues.borrowernumber = borrowers.borrowernumber";
1679 my $sth2 = $dbh->prepare($query2);
1682 if (my $data2 = $sth2->fetchrow_hashref) {
1683 $data->{'date_due'} = $data2->{'date_due'};
1684 $data->{'card'} = $data2->{'cardnumber'};
1686 if ($data->{'wthdrawn'} eq '1') {
1687 $data->{'date_due'} = 'Cancelled';
1689 $data->{'date_due'} = 'Available';
1695 # Find the last 3 people who borrowed this item.
1696 $query2 = "select * from issues, borrowers
1697 where itemnumber = '$data->{'itemnumber'}'
1698 and issues.borrowernumber = borrowers.borrowernumber
1699 and returndate is not NULL
1700 order by returndate desc,timestamp desc";
1701 $sth2 = $dbh->prepare($query2)
1702 || die $dbh->errstr;
1704 || die $sth2->errstr;
1706 for (my $i2 = 0; $i2 < 2; $i2++) {
1707 if (my $data2 = $sth2->fetchrow_hashref) {
1708 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1709 $data->{"card$i2"} = $data2->{'cardnumber'};
1710 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1715 $results[$i] = $data;
1726 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1728 Looks up the item with the given biblioitemnumber.
1730 C<$env> and C<$dbh> are ignored.
1732 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1733 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1739 my ($env,$dbh,$itemnumber) = @_;
1741 my $query="Select * from biblio,items,biblioitems
1742 where items.itemnumber = '$itemnumber'
1743 and biblio.biblionumber = items.biblionumber
1744 and biblioitems.biblioitemnumber = items.biblioitemnumber";
1745 my $sth=$dbh->prepare($query);
1748 my $data=$sth->fetchrow_hashref;
1756 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1758 Looks up patrons (borrowers) by name.
1760 C<$env> and C<$type> are ignored.
1762 C<$searchstring> is a space-separated list of search terms. Each term
1763 must match the beginning a borrower's surname, first name, or other
1766 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1767 reference-to-array; each element is a reference-to-hash, whose keys
1768 are the fields of the C<borrowers> table in the Koha database.
1769 C<$count> is the number of elements in C<$borrowers>.
1773 #used by member enquiries from the intranet
1774 #called by member.pl
1776 my ($env,$searchstring,$type)=@_;
1777 my $dbh = &C4Connect;
1778 $searchstring=~ s/\'/\\\'/g;
1779 my @data=split(' ',$searchstring);
1781 my $query="Select * from borrowers
1782 where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\"
1783 or firstname like \"$data[0]%\" or firstname like \"% $data[0]%\"
1784 or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1786 for (my $i=1;$i<$count;$i++){
1787 $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"
1788 or firstname like \"$data[$i]%\" or firstname like \"% $data[$i]%\"
1789 or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1791 $query=$query.") or cardnumber = \"$searchstring\"
1792 order by surname,firstname";
1793 # print $query,"\n";
1794 my $sth=$dbh->prepare($query);
1798 while (my $data=$sth->fetchrow_hashref){
1799 push(@results,$data);
1805 return ($cnt,\@results);
1810 $borrower = &borrdata($cardnumber, $borrowernumber);
1812 Looks up information about a patron (borrower) by either card number
1813 or borrower number. If $borrowernumber is specified, C<&borrdata>
1814 searches by borrower number; otherwise, it searches by card number.
1816 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1817 the C<borrowers> table in the Koha database.
1822 my ($cardnumber,$bornum)=@_;
1823 $cardnumber = uc $cardnumber;
1827 $query="Select * from borrowers where cardnumber='$cardnumber'";
1829 $query="Select * from borrowers where borrowernumber='$bornum'";
1832 my $sth=$dbh->prepare($query);
1834 my $data=$sth->fetchrow_hashref;
1842 ($count, $issues) = &borrissues($borrowernumber);
1844 Looks up what the patron with the given borrowernumber has borrowed.
1846 C<&borrissues> returns a two-element array. C<$issues> is a
1847 reference-to-array, where each element is a reference-to-hash; the
1848 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1849 in the Koha database. C<$count> is the number of elements in
1858 $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1859 items.itemnumber=issues.itemnumber and
1860 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1863 my $sth=$dbh->prepare($query);
1867 while (my $data=$sth->fetchrow_hashref){
1873 return($i,\@result);
1878 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1880 Looks up what the patron with the given borrowernumber has borrowed,
1881 and sorts the results.
1883 C<$sortkey> is the name of a field on which to sort the results. This
1884 should be the name of a field in the C<issues>, C<biblio>,
1885 C<biblioitems>, or C<items> table in the Koha database.
1887 C<$limit> is the maximum number of results to return.
1889 C<&allissues> returns a two-element array. C<$issues> is a
1890 reference-to-array, where each element is a reference-to-hash; the
1891 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1892 C<items> tables of the Koha database. C<$count> is the number of
1893 elements in C<$issues>
1898 my ($bornum,$order,$limit)=@_;
1901 $query="Select * from issues,biblio,items,biblioitems
1902 where borrowernumber='$bornum' and
1903 items.biblioitemnumber=biblioitems.biblioitemnumber and
1904 items.itemnumber=issues.itemnumber and
1905 items.biblionumber=biblio.biblionumber";
1906 $query.=" order by $order";
1908 $query.=" limit $limit";
1911 my $sth=$dbh->prepare($query);
1915 while (my $data=$sth->fetchrow_hashref){
1921 return($i,\@result);
1926 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
1928 Returns aggregate data about items borrowed by the patron with the
1929 given borrowernumber.
1933 C<&borrdata2> returns a three-element array. C<$borrowed> is the
1934 number of books the patron currently has borrowed. C<$due> is the
1935 number of overdue items the patron currently has borrowed. C<$fine> is
1936 the total fine currently due by the borrower.
1941 my ($env,$bornum)=@_;
1943 my $query="Select count(*) from issues where borrowernumber='$bornum' and
1944 returndate is NULL";
1946 my $sth=$dbh->prepare($query);
1948 my $data=$sth->fetchrow_hashref;
1950 $sth=$dbh->prepare("Select count(*) from issues where
1951 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1953 my $data2=$sth->fetchrow_hashref;
1955 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1956 borrowernumber='$bornum'");
1958 my $data3=$sth->fetchrow_hashref;
1962 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1965 =item getboracctrecord
1967 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1969 Looks up accounting data for the patron with the given borrowernumber.
1973 (FIXME - I'm not at all sure what this is about.)
1975 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1976 reference-to-array, where each element is a reference-to-hash; the
1977 keys are the fields of the C<accountlines> table in the Koha database.
1978 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1979 total amount outstanding for all of the account lines.
1983 sub getboracctrecord {
1984 my ($env,$params) = @_;
1988 my $query= "Select * from accountlines where
1989 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1990 my $sth=$dbh->prepare($query);
1994 while (my $data=$sth->fetchrow_hashref){
1995 # if ($data->{'itemnumber'} ne ''){
1996 # $query="Select * from items,biblio where items.itemnumber=
1997 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1998 # my $sth2=$dbh->prepare($query);
2000 # my $data2=$sth2->fetchrow_hashref;
2004 $acctlines[$numlines] = $data;
2006 $total = $total+ $data->{'amountoutstanding'};
2010 return ($numlines,\@acctlines,$total);
2015 ($count, $lcount, $nacount, $fcount, $scount, $lostcount,
2016 $mending, $transit,$ocount) =
2017 &itemcount($env, $biblionumber, $type);
2019 Counts the number of items with the given biblionumber, broken down by
2024 If C<$type> is not set to C<intra>, lost, very overdue, and withdrawn
2025 items will not be counted.
2027 C<&itemcount> returns a nine-element list:
2029 C<$count> is the total number of items with the given biblionumber.
2031 C<$lcount> is the number of items at the Levin branch.
2033 C<$nacount> is the number of items that are neither borrowed, lost,
2034 nor withdrawn (and are therefore presumably on a shelf somewhere).
2036 C<$fcount> is the number of items at the Foxton branch.
2038 C<$scount> is the number of items at the Shannon branch.
2040 C<$lostcount> is the number of lost and very overdue items.
2042 C<$mending> is the number of items at the Mending branch (being
2045 C<$transit> is the number of items at the Transit branch (in transit
2048 C<$ocount> is the number of items that haven't arrived yet
2049 (aqorders.quantity - aqorders.quantityreceived).
2054 my ($env,$bibnum,$type)=@_;
2056 my $query="Select * from items where
2057 biblionumber=$bibnum ";
2058 if ($type ne 'intra'){
2059 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2060 (wthdrawn <> 1 or wthdrawn is NULL)";
2062 my $sth=$dbh->prepare($query);
2074 while (my $data=$sth->fetchrow_hashref){
2076 my $query2="select * from issues,items where issues.itemnumber=
2077 '$data->{'itemnumber'}' and returndate is NULL
2078 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2079 items.itemlost <> 2) or items.itemlost is NULL)
2080 and (wthdrawn <> 1 or wthdrawn is NULL)";
2082 my $sth2=$dbh->prepare($query2);
2084 if (my $data2=$sth2->fetchrow_hashref){
2087 if ($data->{'holdingbranch'} eq 'C' || $data->{'holdingbranch'} eq 'LT'){
2090 if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
2093 if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
2096 if ($data->{'itemlost'} eq '1'){
2099 if ($data->{'itemlost'} eq '2'){
2102 if ($data->{'holdingbranch'} eq 'FM'){
2105 if ($data->{'holdingbranch'} eq 'TR'){
2112 my $query2="Select * from aqorders where biblionumber=$bibnum";
2113 my $sth2=$dbh->prepare($query2);
2115 if (my $data=$sth2->fetchrow_hashref){
2116 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
2122 return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
2127 $counts = &itemcount2($env, $biblionumber, $type);
2129 Counts the number of items with the given biblionumber, broken down by
2134 C<$type> may be either C<intra> or anything else. If it is not set to
2135 C<intra>, then the search will exclude lost, very overdue, and
2138 C<$&itemcount2> returns a reference-to-hash, with the following fields:
2144 The total number of items with this biblionumber.
2148 The number of items on order (aqorders.quantity -
2149 aqorders.quantityreceived).
2153 For each branch that has at least one copy of the book, C<$counts>
2154 will have a key with the branch name, giving the number of copies at
2162 my ($env,$bibnum,$type)=@_;
2164 my $query="Select * from items,branches where
2165 biblionumber=$bibnum and items.holdingbranch=branches.branchcode";
2166 if ($type ne 'intra'){
2167 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2168 (wthdrawn <> 1 or wthdrawn is NULL)";
2170 my $sth=$dbh->prepare($query);
2175 while (my $data=$sth->fetchrow_hashref){
2177 my $query2="select * from issues,items where issues.itemnumber=
2178 '$data->{'itemnumber'}' and returndate is NULL
2179 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2180 items.itemlost <> 2) or items.itemlost is NULL)
2181 and (wthdrawn <> 1 or wthdrawn is NULL)";
2183 my $sth2=$dbh->prepare($query2);
2185 # FIXME - fetchrow_hashref() can fail for any number of reasons
2186 # (e.g., a database server crash). Perhaps use a left join of some
2188 if (my $data2=$sth2->fetchrow_hashref){
2189 $counts{'not available'}++;
2191 $counts{$data->{'branchname'}}++;
2195 my $query2="Select * from aqorders where biblionumber=$bibnum and
2196 datecancellationprinted is NULL and quantity > quantityreceived";
2197 my $sth2=$dbh->prepare($query2);
2199 if (my $data=$sth2->fetchrow_hashref){
2200 $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
2210 $description = &ItemType($itemtype);
2212 Given an item type code, returns the description for that type.
2217 # FIXME - I'm pretty sure that after the initial setup, the list of
2218 # item types doesn't change very often. Hence, it seems slow and
2219 # inefficient to make yet another database call to look up information
2220 # that'll only change every few months or years.
2222 # Much better, I think, to automatically build a Perl file that can be
2223 # included in those scripts that require it, e.g.:
2224 # @itemtypes = qw( ART BCD CAS CD F ... );
2226 # ART => "Art Prints",
2227 # BCD => "CD-ROM from book",
2228 # CD => "Compact disc (WN)",
2229 # F => "Free Fiction",
2232 # The web server can then run a cron job to rebuild this file from the
2233 # database every hour or so.
2235 # The same thing goes for branches, book funds, book sellers, currency
2236 # rates, printers, stopwords, and perhaps others.
2240 my $query="select description from itemtypes where itemtype='$type'";
2241 my $sth=$dbh->prepare($query);
2243 my $dat=$sth->fetchrow_hashref;
2246 return ($dat->{'description'});
2251 ($count, @results) = &bibitems($biblionumber);
2253 Given the biblionumber for a book, C<&bibitems> looks up that book's
2254 biblioitems (different publications of the same book, the audio book
2255 and film versions, etc.).
2257 C<$count> is the number of elements in C<@results>.
2259 C<@results> is an array of references-to-hash; the keys are the fields
2260 of the C<biblioitems> and C<itemtypes> tables of the Koha database. In
2261 addition, C<itemlost> indicates the availability of the item: if it is
2262 "2", then all copies of the item are long overdue; if it is "1", then
2263 all copies are lost; otherwise, there is at least one copy available.
2269 my $dbh = C4Connect;
2270 my $query = "SELECT biblioitems.*,
2272 MIN(items.itemlost) as itemlost,
2273 MIN(items.dateaccessioned) as dateaccessioned
2274 FROM biblioitems, itemtypes, items
2275 WHERE biblioitems.biblionumber = ?
2276 AND biblioitems.itemtype = itemtypes.itemtype
2277 AND biblioitems.biblioitemnumber = items.biblioitemnumber
2278 GROUP BY items.biblioitemnumber";
2279 my $sth = $dbh->prepare($query);
2282 $sth->execute($bibnum);
2283 while (my $data = $sth->fetchrow_hashref) {
2284 $results[$count] = $data;
2289 return($count, @results);
2294 @barcodes = &barcodes($biblioitemnumber);
2296 Given a biblioitemnumber, looks up the corresponding items.
2298 Returns an array of references-to-hash; the keys are C<barcode> and
2301 The returned items include very overdue items, but not lost ones.
2306 #called from request.pl
2307 my ($biblioitemnumber)=@_;
2309 my $query="SELECT barcode, itemlost, holdingbranch FROM items
2310 WHERE biblioitemnumber = ?
2311 AND (wthdrawn <> 1 OR wthdrawn IS NULL)";
2312 my $sth=$dbh->prepare($query);
2313 $sth->execute($biblioitemnumber);
2316 while (my $data=$sth->fetchrow_hashref){
2317 $barcodes[$i]=$data;
2327 ($count, @websites) = &getwebsites($biblionumber);
2329 Looks up the web sites pertaining to the book with the given
2332 C<$count> is the number of elements in C<@websites>.
2334 C<@websites> is an array of references-to-hash; the keys are the
2335 fields from the C<websites> table in the Koha database.
2340 my ($biblionumber) = @_;
2341 my $dbh = C4Connect;
2342 my $query = "Select * from websites where biblionumber = $biblionumber";
2343 my $sth = $dbh->prepare($query);
2348 while (my $data = $sth->fetchrow_hashref) {
2349 # FIXME - The URL scheme shouldn't be stripped off, at least
2350 # not here, since it's part of the URL, and will be useful in
2351 # constructing a link to the site. If you don't want the user
2352 # to see the "http://" part, strip that off when building the
2354 $data->{'url'} =~ s/^http:\/\///; # FIXME - Leaning toothpick
2356 $results[$count] = $data;
2362 return($count, @results);
2365 =item getwebbiblioitems
2367 ($count, @results) = &getwebbiblioitems($biblionumber);
2369 Given a book's biblionumber, looks up the web versions of the book
2370 (biblioitems with itemtype C<WEB>).
2372 C<$count> is the number of items in C<@results>. C<@results> is an
2373 array of references-to-hash; the keys are the items from the
2374 C<biblioitems> table of the Koha database.
2378 sub getwebbiblioitems {
2379 my ($biblionumber) = @_;
2380 my $dbh = C4Connect;
2381 my $query = "Select * from biblioitems where biblionumber = $biblionumber
2382 and itemtype = 'WEB'";
2383 my $sth = $dbh->prepare($query);
2388 while (my $data = $sth->fetchrow_hashref) {
2389 $data->{'url'} =~ s/^http:\/\///;
2390 $results[$count] = $data;
2396 return($count, @results);
2397 } # sub getwebbiblioitems
2400 END { } # module clean-up code here (global destructor)
2409 Koha Developement team <info@koha.org>