3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
25 # FIXME - C4::Search uses C4::Reserves2, which uses C4::Search.
26 # So Perl complains that all of the functions here get redefined.
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 # set the version for version checking
32 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
33 shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
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.
61 &CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
62 &itemdata &bibdata &GetItems &borrdata &itemnodata
63 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
64 &getboracctrecord &ItemType &itemissues &subject &subtitle
65 &addauthor &bibitems &barcodes &allissues &getwebsites &getwebbiblioitems &catalogsearch &itemcount2
66 &isbnsearch &getbranchname &getborrowercategory);
67 # make all your functions, whether exported or not;
71 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
73 This is primarily a front-end to other, more specialized catalog
74 search functions: if C<$search-E<gt>{itemnumber}> or
75 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
76 C<&CatSearch>. If $search->{subject} is given, it runs a subject
77 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
78 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
80 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
81 the items for each result, and adds several keys:
87 The total number of copies of this book.
91 This is a reference-to-hash; the keys are the names of branches where
92 this book may be found, and the values are the number of copies at
97 A descriptive string saying where the book is located, and how many
98 copies there are, if greater than 1.
102 The book's subject, with spaces replaced with C<%20>, presumably for
110 my ($env,$type,$search,$num,$offset)=@_;
111 my $dbh = C4::Context->dbh;
112 # foreach my $key (%$search){
113 # $search->{$key}=$dbh->quote($search->{$key});
115 my ($count,@results);
116 if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
117 print STDERR "Doing a precise search\n";
118 ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
119 } elsif ($search->{'subject'} ne ''){
120 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
121 } elsif ($search->{'keyword'} ne ''){
122 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
124 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
127 if ($env->{itemcount} eq '1') {
128 foreach my $data (@results){
129 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
130 my $subject2=$data->{'subject'};
131 $subject2=~ s/ /%20/g;
132 $data->{'itemcount'}=$counts->{'total'};
133 my $totalitemcounts=0;
134 foreach my $key (keys %$counts){
135 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
136 #$data->{'location'}.="$key $counts->{$key} ";
137 $totalitemcounts+=$counts->{$key};
138 $data->{'locationhash'}->{$key}=$counts->{$key};
142 my $locationtextonly='';
143 my $notavailabletext='';
144 foreach (sort keys %{$data->{'locationhash'}}) {
145 if ($_ eq 'notavailable') {
146 $notavailabletext="Not available";
147 my $c=$data->{'locationhash'}->{$_};
148 $data->{'not-available-p'}=$totalitemcounts;
149 if ($totalitemcounts>1) {
150 $notavailabletext.=" ($c)";
151 $data->{'not-available-plural-p'}=1;
155 my $c=$data->{'locationhash'}->{$_};
156 if ($_ eq 'Item Lost') {
157 $data->{'lost-p'}=$totalitemcounts;
158 $data->{'lost-plural-p'}=1
159 if $totalitemcounts > 1;
160 } elsif ($_ eq 'Withdrawn') {
161 $data->{'withdrawn-p'}=$totalitemcounts;
162 $data->{'withdrawn-plural-p'}=1
163 if $totalitemcounts > 1;
164 } elsif ($_ eq 'On Loan') {
165 $data->{'on-loan-p'}=$totalitemcounts;
166 $data->{'on-loan-plural-p'}=1
167 if $totalitemcounts > 1;
169 $locationtextonly.=$_;
170 $locationtextonly.=" ($c), "
171 if $totalitemcounts>1;
173 if ($totalitemcounts>1) {
174 $locationtext.=" ($c), ";
178 if ($notavailabletext) {
179 $locationtext.=$notavailabletext;
181 $locationtext=~s/, $//;
183 $data->{'location'}=$locationtext;
184 $data->{'location-only'}=$locationtextonly;
185 $data->{'subject2'}=$subject2;
186 $data->{'use-location-flags-p'}=1; # XXX
189 return ($count,@results);
194 $search = { "keyword" => "One or more keywords",
195 "class" => "VID|CD", # Limit search to fiction and CDs
198 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
200 C<&KeywordSearch> searches the catalog by keyword: given a string
201 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
202 keywords, it looks for books that contain any of those keywords in any
203 of a number of places.
205 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
206 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
209 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
210 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
211 etc.). In this case, the search will be restricted to just those
214 If C<$search-E<gt>{"class"}> is not specified, you may specify
215 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
216 particular Dewey Decimal Classification category. Setting
217 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
218 whereas setting it to "5" will return all books with Dewey code 5I<xx>
219 (Science and Mathematics).
221 C<$env> and C<$type> are ignored.
223 C<$offset> and C<$num> specify the subset of results to return.
224 C<$num> specifies the number of results to return, and C<$offset> is
225 the number of the first result. Thus, setting C<$offset> to 100 and
226 C<$num> to 5 will return results 100 through 104 inclusive.
231 my ($env,$type,$search,$num,$offset)=@_;
232 my $dbh = C4::Context->dbh;
233 $search->{'keyword'}=~ s/ +$//;
234 my @key=split(' ',$search->{'keyword'});
235 # FIXME - Naive users might enter comma-separated
236 # words, e.g., "training, animal". Ought to cope with
240 my %biblionumbers; # Set of biblionumbers returned by the
243 # FIXME - Ought to filter the stopwords out of the list of keywords.
244 # @key = map { !defined($stopwords{$_}) } @key;
246 # FIXME - The way this code is currently set up, it looks for all of
247 # the keywords first in (title, notes, seriestitle), then in the
248 # subtitle, then in the subject. Thus, if you look for keywords
249 # "science fiction", this search won't find a book with
250 # title = "How to write fiction"
251 # subtitle = "A science-based approach"
252 # Is this the desired effect? If not, then the first SQL query
253 # should look in the biblio, subtitle, and subject tables all at
254 # once. The way the first query is built can accomodate this easily.
256 # Look for keywords in table 'biblio'.
258 # Build an SQL query that finds each of the keywords in any of the
259 # title, biblio.notes, or seriestitle. To do this, we'll build up an
260 # array of clauses, one for each keyword.
261 my $query; # The SQL query
262 my @clauses = (); # The search clauses
263 my @bind = (); # The term bindings
265 $query = <<EOT; # Beginning of the query
270 foreach my $keyword (@key)
272 my @subclauses = (); # Subclauses, one for each field we're
275 # For each field we're searching on, create a subclause that'll
276 # match the current keyword in the current field.
277 foreach my $field (qw(title notes seriestitle author))
280 "$field LIKE ? OR $field LIKE ?";
281 push(@bind,"\Q$keyword\E%","% \Q$keyword\E%");
283 # (Yes, this could have been done as
284 # @subclauses = map {...} qw(field1 field2 ...)
285 # )but I think this way is more readable.
287 # Construct the current clause by joining the subclauses.
288 push @clauses, "(" . join(")\n\tOR (", @subclauses) . ")";
290 # Now join all of the clauses together and append to the query.
291 $query .= "(" . join(")\nAND (", @clauses) . ")";
293 # FIXME - Perhaps use $sth->bind_columns() ? Documented as the most
294 # efficient way to fetch data.
295 my $sth=$dbh->prepare($query);
296 $sth->execute(@bind);
297 while (my @res = $sth->fetchrow_array) {
300 $biblionumbers{$_} = 1; # Add these results to the set
305 # Now look for keywords in the 'bibliosubtitle' table.
307 # Again, we build a list of clauses from the keywords.
310 $query = "SELECT biblionumber FROM bibliosubtitle WHERE ";
311 foreach my $keyword (@key)
314 "subtitle LIKE ? OR subtitle like ?";
315 push(@bind,"\Q$keyword\E%","% \Q$keyword\E%");
317 $query .= "(" . join(") AND (", @clauses) . ")";
319 $sth=$dbh->prepare($query);
320 $sth->execute(@bind);
321 while (my @res = $sth->fetchrow_array) {
324 $biblionumbers{$_} = 1; # Add these results to the set
329 # Look for the keywords in the notes for individual items
330 # ('biblioitems.notes')
332 # Again, we build a list of clauses from the keywords.
335 $query = "SELECT biblionumber FROM biblioitems WHERE ";
336 foreach my $keyword (@key)
339 "notes LIKE ? OR notes like ?";
340 push(@bind,"\Q$keyword\E%","% \Q$keyword\E%");
342 $query .= "(" . join(") AND (", @clauses) . ")";
344 $sth=$dbh->prepare($query);
345 $sth->execute(@bind);
346 while (my @res = $sth->fetchrow_array) {
349 $biblionumbers{$_} = 1; # Add these results to the set
354 # Look for keywords in the 'bibliosubject' table.
356 # FIXME - The other queries look for words in the desired field that
357 # begin with the individual keywords the user entered. This one
358 # searches for the literal string the user entered. Is this the
360 # Note in particular that spaces are retained: if the user typed
362 # (with two spaces), this won't find the subject "science fiction"
363 # (one space). Likewise, a search for "%" will return absolutely
365 # If this isn't the desired effect, see the previous searches for
368 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
369 like ? group by biblionumber");
370 $sth->execute("%$search->{'keyword'}%");
372 while (my @res = $sth->fetchrow_array) {
375 $biblionumbers{$_} = 1; # Add these results to the set
385 my @res = keys %biblionumbers;
389 # print "count $count";
390 if ($search->{'class'} ne ''){
392 my $query="select * from biblio,biblioitems where
393 biblio.biblionumber=? and
394 biblio.biblionumber=biblioitems.biblionumber ";
395 my @bind = ($res[$i2]);
396 if ($search->{'class'} ne ''){ # FIXME - Redundant
397 my @temp=split(/\|/,$search->{'class'});
399 $query.= "and ( itemtype=?";
400 push(@bind,$temp[0]);
401 for (my $i=1;$i<$count;$i++){
402 $query.=" or itemtype=?";
403 push(@bind,$temp[$i]);
407 my $sth=$dbh->prepare($query);
409 $sth->execute(@bind);
410 if (my $data2=$sth->fetchrow_hashref){
411 my $dewey= $data2->{'dewey'};
412 my $subclass=$data2->{'subclass'};
413 # FIXME - This next bit is bogus, because it assumes that the
414 # Dewey code is a floating-point number. It isn't. It's
415 # actually a string that mainly consists of numbers. In
416 # particular, "4" is not a valid Dewey code, although "004"
417 # is ("Data processing; Computer science"). Likewise, zeros
418 # after the decimal are significant ("575" is not the same as
419 # "575.0"; the latter is more specific). And "000" is a
420 # perfectly good Dewey code ("General works; computer
421 # science") and should not be interpreted to mean "this
422 # database entry does not have a Dewey code". That's what
425 ($dewey == 0) && ($dewey='');
426 ($dewey) && ($dewey.=" $subclass") ;
428 my $end=$offset +$num;
433 if ($i4 <=$end && $i4 > $offset){
434 $data2->{'dewey'}=$dewey;
437 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
440 # print "in here $i3<br>";
451 # $search->{'class'} was not specified
453 # FIXME - This is bogus: it makes a separate query for each
454 # biblioitem, and returns results in apparently random order. It'd
455 # be much better to combine all of the previous queries into one big
456 # one (building it up a little at a time, of course), and have that
457 # big query select all of the desired fields, instead of just
460 while ($i2 < $num && $i2 < $count){
461 my $query="select * from biblio,biblioitems where
462 biblio.biblionumber=? and
463 biblio.biblionumber=biblioitems.biblionumber ";
464 my @bind=($res[$i2+$offset]);
466 if ($search->{'dewey'} ne ''){
467 $query.= "and (dewey like ?)";
468 push(@bind,"$search->{'dewey'}%");
471 my $sth=$dbh->prepare($query);
473 $sth->execute(@bind);
474 if (my $data2=$sth->fetchrow_hashref){
475 my $dewey= $data2->{'dewey'};
476 my $subclass=$data2->{'subclass'};
478 ($dewey == 0) && ($dewey='');
479 ($dewey) && ($dewey.=" $subclass") ;
481 $data2->{'dewey'}=$dewey;
484 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
493 return($count,@res2);
497 my ($env,$type,$search,$num,$offset)=@_;
498 my $dbh = C4::Context->dbh;
499 $search->{'keyword'}=~ s/ +$//;
500 my @key=split(' ',$search->{'keyword'});
504 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
505 biblio.biblionumber=biblioitems.biblionumber and
506 biblio.biblionumber=bibliosubtitle.biblionumber and
507 (((title like ? or title like ?)";
508 my @bind=("$key[0]%","% $key[0]%");
510 $query .= " and (title like ? or title like ?)";
511 push(@bind,"$key[$i]%","% $key[$i]%");
514 $query.= ") or ((subtitle like ? or subtitle like ?)";
515 push(@bind,"$key[0]%","% $key[0]%");
516 for ($i=1;$i<$count;$i++){
517 $query.= " and (subtitle like ? or subtitle like ?)";
518 push(@bind,"$key[$i]%","% $key[$i]%");
520 $query.= ") or ((seriestitle like ? or seriestitle like ?)";
521 push(@bind,"$key[0]%","% $key[0]%");
522 for ($i=1;$i<$count;$i++){
523 $query.=" and (seriestitle like ? or seriestitle like ?)";
524 push(@bind,"$key[$i]%","% $key[$i]%");
526 $query.= ") or ((biblio.notes like ? or biblio.notes like ?)";
527 push(@bind,"$key[0]%","% $key[0]%");
528 for ($i=1;$i<$count;$i++){
529 $query.=" and (biblio.notes like ? or biblio.notes like ?)";
530 push(@bind,"$key[$i]%","% $key[$i]%");
532 $query.= ") or ((biblioitems.notes like ? or biblioitems.notes like ?)";
533 push(@bind,"$key[0]%","% $key[0]%");
534 for ($i=1;$i<$count;$i++){
535 $query.=" and (biblioitems.notes like ? or biblioitems.notes like ?)";
536 push(@bind,"$key[$i]%","% $key[$i]%");
538 if ($search->{'keyword'} =~ /new zealand/i){
539 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
540 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
541 or author like '% nz %' or author like '% nz')"
543 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
544 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
545 $search->{'keyword'} =~ / nz/i){
546 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
547 or title like '% new zealand' or subtitle like 'new zealand%' or
548 subtitle like '% new zealand %'
549 or subtitle like '% new zealand' or author like 'new zealand%'
550 or author like '% new zealand %' or author like '% new zealand' or
551 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
552 or seriestitle like '% new zealand')"
555 if ($search->{'class'} ne ''){
556 my @temp=split(/\|/,$search->{'class'});
558 $query.= "and ( itemtype=?";
559 push(@bind,"$temp[0]");
560 for (my $i=1;$i<$count;$i++){
561 $query.=" or itemtype=?";
562 push(@bind,"$temp[$i]");
566 if ($search->{'dewey'} ne ''){
567 $query.= "and (dewey like '$search->{'dewey'}%') ";
569 $query.="group by biblio.biblionumber";
570 #$query.=" order by author,title";
572 my $sth=$dbh->prepare($query);
573 $sth->execute(@bind);
575 while (my $data=$sth->fetchrow_hashref){
576 #FIXME: rewrite to use ? before uncomment
577 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
580 # my ($dewey, $subclass) = $sti->fetchrow;
581 my $dewey=$data->{'dewey'};
582 my $subclass=$data->{'subclass'};
584 ($dewey == 0) && ($dewey='');
585 ($dewey) && ($dewey.=" $subclass");
587 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
588 # print $results[$i];
592 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
593 like ? group by biblionumber");
594 $sth->execute("%".$search->{'keyword'}."%");
595 while (my $data=$sth->fetchrow_hashref){
596 $query="Select * from biblio,biblioitems where
597 biblio.biblionumber=? and
598 biblio.biblionumber=biblioitems.biblionumber ";
599 @bind=($data->{'biblionumber'});
600 if ($search->{'class'} ne ''){
601 my @temp=split(/\|/,$search->{'class'});
603 $query.= " and ( itemtype=?";
604 push(@bind,$temp[0]);
605 for (my $i=1;$i<$count;$i++){
606 $query.=" or itemtype=?";
607 push(@bind,$temp[$i]);
612 if ($search->{'dewey'} ne ''){
613 $query.= "and (dewey like ?)";
614 push(@bind,"$search->{'dewey'}%");
616 my $sth2=$dbh->prepare($query);
617 $sth2->execute(@bind);
619 while (my $data2=$sth2->fetchrow_hashref){
620 my $dewey= $data2->{'dewey'};
621 my $subclass=$data2->{'subclass'};
623 ($dewey == 0) && ($dewey='');
624 ($dewey) && ($dewey.=" $subclass") ;
626 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
627 # print $results[$i];
633 @results=sort @results;
640 while ($i2 < $count){
641 if ($results[$i2] ne $res[$i-1]){
642 $res[$i]=$results[$i2];
650 while ($i2 < $num && $i2 < $count){
651 $res2[$i2]=$res[$i2+$offset];
663 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
665 C<&CatSearch> searches the Koha catalog. It returns a list whose first
666 element is the number of returned results, and whose subsequent
667 elements are the results themselves.
669 Each returned element is a reference-to-hash. Most of the keys are
670 simply the fields from the C<biblio> table in the Koha database, but
671 the following keys may also be present:
677 The book's illustrator.
687 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
688 high-level behavior of C<&CatSearch>, as described below.
690 In many cases, the description below says that a certain field in the
691 database must match the search string. In these cases, it means that
692 the beginning of some word in the field must match the search string.
693 Thus, an author search for "sm" will return books whose author is
694 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
695 does not occur at the beginning of a word.
697 Note that within each search mode, the criteria are and-ed together.
698 That is, if you perform a loose search on the author "Jerome" and the
699 title "Boat", the search will only return books by Jerome containing
702 It is not possible to cross modes, e.g., set the author to "Asimov"
703 and the subject to "Math" in hopes of finding books on math by Asimov.
707 If C<$type> is set to C<loose>, the following search criteria may be
712 =item C<$search-E<gt>{author}>
714 The search string is a space-separated list of words. Each word must
715 match either the C<author> or C<additionalauthors> field.
717 =item C<$search-E<gt>{title}>
719 Each word in the search string must match the book title. If no author
720 is specified, the book subtitle will also be searched.
722 =item C<$search-E<gt>{abstract}>
724 Searches for the given search string in the book's abstract.
726 =item C<$search-E<gt>{'date-before'}>
728 Searches for books whose copyright date matches the search string.
729 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
730 books written in 1985, and setting it to "198" will find books written
731 between 1980 and 1989.
733 =item C<$search-E<gt>{title}>
735 Searches by title are also affected by the value of
736 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
737 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
738 match the search string exactly (the subtitle is not searched).
740 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
741 each word in the search string must match the title, subtitle,
742 unititle, or series title.
744 =item C<$search-E<gt>{class}>
746 Restricts the search to certain item classes. The value of
747 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
748 Thus, setting it to "F" restricts the search to fiction, and setting
749 it to "CD|CAS" will only look in compact disks and cassettes.
751 =item C<$search-E<gt>{dewey}>
753 Searches for books whose Dewey Decimal Classification code matches the
754 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
755 search for all books in 5I<xx> (Science and mathematics), setting it
756 to "54" will search for all books in 54I<x> (Chemistry), and setting
757 it to "546" will search for books on inorganic chemistry.
759 =item C<$search-E<gt>{publisher}>
761 Searches for books whose publisher contains the search string (unlike
762 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
767 =head2 Subject search
769 If C<$type> is set to C<subject>, the following search criterion may
774 =item C<$search-E<gt>{subject}>
776 The search string is a space-separated list of words, each of which
777 must match the book's subject.
779 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
780 C<&CatSearch> will search for books whose subject is "New Zealand".
781 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
782 search for books on "nz" and "football", not books on "New Zealand"
787 =head2 Precise search
789 If C<$type> is set to C<precise>, the following search criteria may be
794 =item C<$search-E<gt>{item}>
796 Searches for books whose barcode exactly matches the search string.
798 =item C<$search-E<gt>{isbn}>
800 Searches for books whose ISBN exactly matches the search string.
804 For a loose search, if an author was specified, the results are
805 ordered by author and title. If no author was specified, the results
806 are ordered by title.
808 For other (non-loose) searches, if a subject was specified, the
809 results are ordered alphabetically by subject.
811 In all other cases (e.g., loose search by keyword), the results are
817 my ($env,$type,$search,$num,$offset)=@_;
818 my $dbh = C4::Context->dbh;
823 my $title = lc($search->{'title'});
825 if ($type eq 'loose') {
826 if ($search->{'author'} ne ''){
827 my @key=split(' ',$search->{'author'});
830 $query="select *,biblio.author,biblio.biblionumber from
832 left join additionalauthors
833 on additionalauthors.biblionumber =biblio.biblionumber
835 ((biblio.author like ? or biblio.author like ? or
836 additionalauthors.author like ? or additionalauthors.author
839 @bind=("$key[0]%","% $key[0]%","$key[0]%","% $key[0]%");
842 biblio.author like ? or biblio.author like ? or
843 additionalauthors.author like ? or additionalauthors.author like ?
845 push(@bind,"$key[$i]%","% $key[$i]%","$key[$i]%","% $key[$i]%");
849 if ($search->{'title'} ne ''){
850 my @key=split(' ',$search->{'title'});
853 $query.= " and (((title like ? or title like ?)";
854 push(@bind,"$key[0]%","% $key[0]%");
856 $query .= " and (title like ? or title like ?)";
857 push(@bind,"$key[$i]%","% $key[$i]%");
860 $query.=") or ((seriestitle like ? or seriestitle like ?)";
861 push(@bind,"$key[0]%","% $key[0]%");
862 for ($i=1;$i<$count;$i++){
863 $query.=" and (seriestitle like ? or seriestitle like ?)";
864 push(@bind,"$key[$i]%","% $key[$i]%");
866 $query.=") or ((unititle like ? or unititle like ?)";
867 push(@bind,"$key[0]%","% $key[0]%");
868 for ($i=1;$i<$count;$i++){
869 $query.=" and (unititle like ? or unititle like ?)";
870 push(@bind,"$key[$i]%","% $key[$i]%");
874 if ($search->{'abstract'} ne ''){
875 $query.= " and (abstract like ?)";
876 push(@bind,"%$search->{'abstract'}%");
878 if ($search->{'date-before'} ne ''){
879 $query.= " and (copyrightdate like ?)";
880 push(@bind,"%$search->{'date-before'}%");
882 $query.=" group by biblio.biblionumber";
884 if ($search->{'title'} ne '') {
885 if ($search->{'ttype'} eq 'exact'){
886 $query="select * from biblio
888 (biblio.title=? or (biblio.unititle = ?
889 or biblio.unititle like ? or
890 biblio.unititle like ? or
891 biblio.unititle like ?) or
892 (biblio.seriestitle = ? or
893 biblio.seriestitle like ? or
894 biblio.seriestitle like ? or
895 biblio.seriestitle like ?)
897 @bind=($search->{'title'},$search->{'title'},"$search->{'title'} |%","%| $search->{'title'} |%","%| $search->{'title'}",$search->{'title'},"$search->{'title'} |%","%| $search->{'title'} |%","%| $search->{'title'}");
899 my @key=split(' ',$search->{'title'});
902 $query="select biblio.biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp,subtitle from biblio
903 left join bibliosubtitle on
904 biblio.biblionumber=bibliosubtitle.biblionumber
906 (((title like ? or title like ?)";
907 @bind=("$key[0]%","% $key[0]%");
909 $query .= " and (title like ? or title like ?)";
910 push(@bind,"$key[$i]%","% $key[$i]%");
913 $query.=") or ((subtitle like ? or subtitle like ?)";
914 push(@bind,"$key[0]%","% $key[0]%");
915 for ($i=1;$i<$count;$i++){
916 $query.=" and (subtitle like ? or subtitle like ?)";
917 push(@bind,"$key[$i]%","% $key[$i]%");
919 $query.=") or ((seriestitle like ? or seriestitle like ?)";
920 push(@bind,"$key[0]%","% $key[0]%");
921 for ($i=1;$i<$count;$i++){
922 $query.=" and (seriestitle like ? or seriestitle like ?)";
923 push(@bind,"$key[$i]%","% $key[$i]%");
925 $query.=") or ((unititle like ? or unititle like ?)";
926 push(@bind,"$key[0]%","% $key[0]%");
927 for ($i=1;$i<$count;$i++){
928 $query.=" and (unititle like ? or unititle like ?)";
929 push(@bind,"$key[$i]%","% $key[$i]%");
933 if ($search->{'abstract'} ne ''){
934 $query.= " and (abstract like ?)";
935 push(@bind,"%$search->{'abstract'}%");
937 if ($search->{'date-before'} ne ''){
938 $query.= " and (copyrightdate like ?)";
939 push(@bind,"%$search->{'date-before'}%");
941 } elsif ($search->{'class'} ne ''){
942 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
943 my @temp=split(/\|/,$search->{'class'});
945 $query.= " and ( itemtype= ?)";
947 for (my $i=1;$i<$count;$i++){
948 $query.=" or itemtype=?";
949 push(@bind,$temp[$i]);
952 if ($search->{'illustrator'} ne ''){
953 $query.=" and illus like ?";
954 push(@bind,"%".$search->{'illustrator'}."%");
956 if ($search->{'dewey'} ne ''){
957 $query.=" and biblioitems.dewey like ?";
958 push(@bind,"$search->{'dewey'}%");
960 } elsif ($search->{'dewey'} ne ''){
961 $query="select * from biblioitems,biblio
962 where biblio.biblionumber=biblioitems.biblionumber
963 and biblioitems.dewey like ?";
964 @bind=("$search->{'dewey'}%");
965 } elsif ($search->{'illustrator'} ne '') {
966 $query="select * from biblioitems,biblio
967 where biblio.biblionumber=biblioitems.biblionumber
968 and biblioitems.illus like ?";
969 @bind=("%".$search->{'illustrator'}."%");
970 } elsif ($search->{'publisher'} ne ''){
971 $query = "Select * from biblio,biblioitems where biblio.biblionumber
972 =biblioitems.biblionumber and (publishercode like ?)";
973 @bind=("%$search->{'publisher'}%");
974 } elsif ($search->{'abstract'} ne ''){
975 $query = "Select * from biblio where abstract like ?";
976 @bind=("%$search->{'abstract'}%");
977 } elsif ($search->{'date-before'} ne ''){
978 $query = "Select * from biblio where copyrightdate like ?";
979 @bind=("%$search->{'date-before'}%");
981 $query .=" group by biblio.biblionumber";
984 if ($type eq 'subject'){
985 my @key=split(' ',$search->{'subject'});
988 $query="select * from bibliosubject, biblioitems where
989 (bibliosubject.biblionumber = biblioitems.biblionumber) and ( subject like ? or subject like ? or subject like ?)";
990 @bind=("$key[0]%","% $key[0]%","%($key[0])%");
992 $query.=" and (subject like ? or subject like ? or subject like ?)";
993 push(@bind,"$key[$i]%","% $key[$i]%","%($key[$i])%");
997 # FIXME - Wouldn't it be better to fix the database so that if a
998 # book has a subject "NZ", then it also gets added the subject
1000 # This can also be generalized by adding a table of subject
1001 # synonyms to the database: just declare "NZ" to be a synonym for
1002 # "New Zealand", "SF" a synonym for both "Science fiction" and
1003 # "Fantastic fiction", etc.
1005 if (lc($search->{'subject'}) eq 'nz'){
1006 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1007 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1008 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1009 $query=~ s/ nz/ NEW ZEALAND/ig;
1010 $query=~ s/nz /NEW ZEALAND /ig;
1011 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1014 if ($type eq 'precise'){
1015 if ($search->{'itemnumber'} ne ''){
1016 $query="select * from items,biblio ";
1017 my $search2=uc $search->{'itemnumber'};
1018 $query=$query." where
1019 items.biblionumber=biblio.biblionumber
1024 if ($search->{'isbn'} ne ''){
1025 my $search2=uc $search->{'isbn'};
1026 my $sth1=$dbh->prepare("select * from biblioitems where isbn=?");
1027 $sth1->execute($search2);
1029 while (my $data=$sth1->fetchrow_hashref) {
1030 my $sth=$dbh->prepare("select * from biblioitems,biblio where
1031 biblio.biblionumber = ?
1032 and biblioitems.biblionumber = biblio.biblionumber");
1033 $sth->execute($data->{'biblionumber'});
1034 # FIXME - There's already a $data in this scope.
1035 my $data=$sth->fetchrow_hashref;
1036 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1037 # FIXME - The following assumes that the Dewey code is a
1038 # floating-point number. It isn't: it's a string.
1040 ($dewey == 0) && ($dewey='');
1041 ($dewey) && ($dewey.=" $subclass");
1042 $data->{'dewey'}=$dewey;
1043 $results[$i2]=$data;
1044 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1051 if ($type ne 'precise' && $type ne 'subject'){
1052 if ($search->{'author'} ne ''){
1053 $query .= " order by biblio.author,title";
1055 $query .= " order by title";
1058 if ($type eq 'subject'){
1059 $query .= " group by subject ";
1062 my $sth=$dbh->prepare($query);
1063 $sth->execute(@bind);
1066 my $limit= $num+$offset;
1067 while (my $data=$sth->fetchrow_hashref){
1068 my $query="select classification,dewey,subclass,publishercode from biblioitems where biblionumber=?";
1069 my @bind=($data->{'biblionumber'});
1070 if ($search->{'class'} ne ''){
1071 my @temp=split(/\|/,$search->{'class'});
1073 $query.= " and ( itemtype= ?";
1074 push(@bind,$temp[0]);
1075 for (my $i=1;$i<$count;$i++){
1076 $query.=" or itemtype=?";
1077 push(@bind,$temp[$i]);
1081 if ($search->{'dewey'} ne ''){
1082 $query.=" and dewey=? ";
1083 push(@bind,$search->{'dewey'});
1085 if ($search->{'illustrator'} ne ''){
1086 $query.=" and illus like ?";
1087 push(@bind,"%$search->{'illustrator'}%");
1089 if ($search->{'publisher'} ne ''){
1090 $query.= " and (publishercode like ?)";
1091 push(@bind,"%$search->{'publisher'}%");
1093 my $sti=$dbh->prepare($query);
1094 $sti->execute(@bind);
1101 if ($bibitemdata = $sti->fetchrow_hashref()){
1103 $classification=$bibitemdata->{'classification'};
1104 $dewey=$bibitemdata->{'dewey'};
1105 $subclass=$bibitemdata->{'subclass'};
1106 $publishercode=$bibitemdata->{'publishercode'};
1108 # print STDERR "$dewey $subclass $publishercode\n";
1109 # FIXME - The Dewey code is a string, not a number.
1111 ($dewey == 0) && ($dewey='');
1112 ($dewey) && ($dewey.=" $subclass");
1113 $data->{'classification'}=$classification;
1114 $data->{'dewey'}=$dewey;
1115 $data->{'publishercode'}=$publishercode;
1118 if ($count > $offset && $count <= $limit){
1127 return($count,@results);
1130 sub updatesearchstats{
1131 my ($dbh,$query)=@_;
1137 @results = &subsearch($env, $subject);
1139 Searches for books that have a subject that exactly matches
1142 C<&subsearch> returns an array of results. Each element of this array
1143 is a string, containing the book's title, author, and biblionumber,
1151 my ($env,$subject)=@_;
1152 my $dbh = C4::Context->dbh;
1153 my $sth=$dbh->prepare("Select * from biblio,bibliosubject where
1154 biblio.biblionumber=bibliosubject.biblionumber and
1155 bibliosubject.subject=? group by biblio.biblionumber
1156 order by biblio.title");
1157 $sth->execute($subject);
1160 while (my $data=$sth->fetchrow_hashref){
1161 push @results, $data;
1170 @results = &ItemInfo($env, $biblionumber, $type);
1172 Returns information about books with the given biblionumber.
1174 C<$type> may be either C<intra> or anything else. If it is not set to
1175 C<intra>, then the search will exclude lost, very overdue, and
1180 C<&ItemInfo> returns a list of references-to-hash. Each element
1181 contains a number of keys. Most of them are table items from the
1182 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1183 Koha database. Other keys include:
1187 =item C<$data-E<gt>{branchname}>
1189 The name (not the code) of the branch to which the book belongs.
1191 =item C<$data-E<gt>{datelastseen}>
1193 This is simply C<items.datelastseen>, except that while the date is
1194 stored in YYYY-MM-DD format in the database, here it is converted to
1195 DD/MM/YYYY format. A NULL date is returned as C<//>.
1197 =item C<$data-E<gt>{datedue}>
1199 =item C<$data-E<gt>{class}>
1201 This is the concatenation of C<biblioitems.classification>, the book's
1202 Dewey code, and C<biblioitems.subclass>.
1204 =item C<$data-E<gt>{ocount}>
1206 I think this is the number of copies of the book available.
1208 =item C<$data-E<gt>{order}>
1210 If this is set, it is set to C<One Order>.
1217 my ($env,$biblionumber,$type) = @_;
1218 my $dbh = C4::Context->dbh;
1219 my $query = "SELECT *,items.notforloan as itemnotforloan FROM items, biblio, biblioitems
1220 left join itemtypes on biblioitems.itemtype = itemtypes.itemtype
1221 WHERE items.biblionumber = ?
1222 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1223 AND biblio.biblionumber = items.biblionumber";
1224 # buggy : opac & librarian interface can show the same info level & itemstatus should not be hardcoded
1225 # if ($type ne 'intra'){
1226 # $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1227 # or items.itemlost is NULL)
1228 # and (wthdrawn <> 1 or wthdrawn is NULL)";
1230 $query .= " order by items.dateaccessioned desc";
1231 my $sth=$dbh->prepare($query);
1232 $sth->execute($biblionumber);
1235 while (my $data=$sth->fetchrow_hashref){
1237 my $isth=$dbh->prepare("Select issues.*,borrowers.cardnumber from issues,borrowers where itemnumber = ? and returndate is null and issues.borrowernumber=borrowers.borrowernumber");
1238 $isth->execute($data->{'itemnumber'});
1239 if (my $idata=$isth->fetchrow_hashref){
1240 $data->{borrowernumber} = $idata->{borrowernumber};
1241 $data->{cardnumber} = $idata->{cardnumber};
1242 $datedue = format_date($idata->{'date_due'});
1244 # buggy : hardcoded & non-translatable
1245 # more : why don't you want to show the datedue if it's very very overdue ?
1246 # if ($data->{'itemlost'} eq '2'){
1247 # $datedue='Very Overdue';
1249 # if ($data->{'itemlost'} eq '1'){
1252 # if ($data->{'wthdrawn'} eq '1'){
1253 # $datedue="Cancelled";
1255 if ($datedue eq ''){
1256 # $datedue="Available";
1257 my ($restype,$reserves)=C4::Reserves2::CheckReserves($data->{'itemnumber'});
1263 #get branch information.....
1264 my $bsth=$dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
1265 $bsth->execute($data->{'holdingbranch'});
1266 if (my $bdata=$bsth->fetchrow_hashref){
1267 $data->{'branchname'} = $bdata->{'branchname'};
1269 my $date=format_date($data->{'datelastseen'});
1270 $data->{'datelastseen'}=$date;
1271 $data->{'datedue'}=$datedue;
1272 # get notforloan complete status if applicable
1273 my $sthnflstatus = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield="items.notforloan"');
1274 $sthnflstatus->execute;
1275 my ($authorised_valuecode) = $sthnflstatus->fetchrow;
1276 if ($authorised_valuecode) {
1277 $sthnflstatus = $dbh->prepare("select lib from authorised_values where category=? and authorised_value=?");
1278 $sthnflstatus->execute($authorised_valuecode,$data->{itemnotforloan});
1279 my ($lib) = $sthnflstatus->fetchrow;
1280 $data->{notforloan} = $lib;
1286 #FIXME: ordering/indentation here looks wrong
1287 # buggy : count in $i+1 the info on qty ordered for $i : total shown is real total +1
1288 # useless : Koha 2.2.2 now automatically show the existing number of items
1289 # and if there is no items, and at least one is on order, show "on order".
1290 # my $sth2=$dbh->prepare("Select * from aqorders where biblionumber=?");
1291 # $sth2->execute($biblionumber);
1294 # if ($data=$sth2->fetchrow_hashref){
1295 # $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1297 # $data->{'ocount'}=$ocount;
1298 # $data->{'order'}="One Order";
1299 # $results[$i]=$data;
1308 @results = &GetItems($env, $biblionumber);
1310 Returns information about books with the given biblionumber.
1314 C<&GetItems> returns an array of strings. Each element is a
1315 tab-separated list of values: biblioitemnumber, itemtype,
1316 classification, Dewey number, subclass, ISBN, volume, number, and
1319 Itemdata, in turn, is a string of the form
1320 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1321 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1327 my ($env,$biblionumber)=@_;
1328 #debug_msg($env,"GetItems");
1329 my $dbh = C4::Context->dbh;
1330 my $sth=$dbh->prepare("Select * from biblioitems where (biblionumber = ?)");
1331 $sth->execute($biblionumber);
1332 #debug_msg($env,"executed query");
1335 while (my $data=$sth->fetchrow_hashref) {
1336 #debug_msg($env,$data->{'biblioitemnumber'});
1337 my $dewey = $data->{'dewey'};
1339 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1340 $line .= "\t$data->{'classification'}\t$dewey";
1341 $line .= "\t$data->{'subclass'}\t$data->{isbn}";
1342 $line .= "\t$data->{'volume'}\t$data->{number}";
1343 my $isth= $dbh->prepare("select * from items where biblioitemnumber = ?");
1344 $isth->execute($data->{'biblioitemnumber'});
1345 while (my $idata = $isth->fetchrow_hashref) {
1346 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1347 if ($idata->{'notforloan'} == 1) {
1350 if ($idata->{'itemlost'} == 1) {
1353 $line .= "\t$iline";
1356 $results[$i] = $line;
1365 $item = &itemdata($barcode);
1367 Looks up the item with the given barcode, and returns a
1368 reference-to-hash containing information about that item. The keys of
1369 the hash are the fields from the C<items> and C<biblioitems> tables in
1376 my $dbh = C4::Context->dbh;
1377 my $sth=$dbh->prepare("Select * from items,biblioitems where barcode=?
1378 and items.biblioitemnumber=biblioitems.biblioitemnumber");
1379 $sth->execute($barcode);
1380 my $data=$sth->fetchrow_hashref;
1387 $data = &bibdata($biblionumber, $type);
1389 Returns information about the book with the given biblionumber.
1391 C<$type> is ignored.
1393 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1394 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1397 In addition, C<$data-E<gt>{subject}> is the list of the book's
1398 subjects, separated by C<" , "> (space, comma, space).
1400 If there are multiple biblioitems with the given biblionumber, only
1401 the first one is considered.
1406 my ($bibnum, $type) = @_;
1407 my $dbh = C4::Context->dbh;
1408 my $sth = $dbh->prepare("Select *, biblioitems.notes AS bnotes, biblio.notes
1409 from biblio, biblioitems
1410 left join bibliosubtitle on
1411 biblio.biblionumber = bibliosubtitle.biblionumber
1412 left join itemtypes on biblioitems.itemtype=itemtypes.itemtype
1413 where biblio.biblionumber = ?
1414 and biblioitems.biblionumber = biblio.biblionumber");
1415 $sth->execute($bibnum);
1417 $data = $sth->fetchrow_hashref;
1419 # handle management of repeated subtitle
1420 $sth = $dbh->prepare("Select * from bibliosubtitle where biblionumber = ?");
1421 $sth->execute($bibnum);
1423 while (my $dat = $sth->fetchrow_hashref){
1425 $line{subtitle} = $dat->{subtitle};
1426 push @subtitles, \%line;
1428 $data->{subtitles} = \@subtitles;
1430 $sth = $dbh->prepare("Select * from bibliosubject where biblionumber = ?");
1431 $sth->execute($bibnum);
1433 while (my $dat = $sth->fetchrow_hashref){
1435 $line{subject} = $dat->{'subject'};
1436 push @subjects, \%line;
1438 $data->{subjects} = \@subjects;
1440 $sth = $dbh->prepare("Select * from additionalauthors where biblionumber = ?");
1441 $sth->execute($bibnum);
1442 while (my $dat = $sth->fetchrow_hashref){
1443 $data->{'additionalauthors'} .= "$dat->{'author'} - ";
1445 chop $data->{'additionalauthors'};
1446 chop $data->{'additionalauthors'};
1447 chop $data->{'additionalauthors'};
1454 $itemdata = &bibitemdata($biblioitemnumber);
1456 Looks up the biblioitem with the given biblioitemnumber. Returns a
1457 reference-to-hash. The keys are the fields from the C<biblio>,
1458 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1459 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1465 my $dbh = C4::Context->dbh;
1466 my $sth = $dbh->prepare("Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes where biblio.biblionumber = biblioitems.biblionumber and biblioitemnumber = ? and biblioitems.itemtype = itemtypes.itemtype");
1469 $sth->execute($bibitem);
1471 $data = $sth->fetchrow_hashref;
1479 ($count, $subjects) = &subject($biblionumber);
1481 Looks up the subjects of the book with the given biblionumber. Returns
1482 a two-element list. C<$subjects> is a reference-to-array, where each
1483 element is a subject of the book, and C<$count> is the number of
1484 elements in C<$subjects>.
1490 my $dbh = C4::Context->dbh;
1491 my $sth=$dbh->prepare("Select * from bibliosubject where biblionumber=?");
1492 $sth->execute($bibnum);
1495 while (my $data=$sth->fetchrow_hashref){
1500 return($i,\@results);
1505 ($count, $authors) = &addauthors($biblionumber);
1507 Looks up the additional authors for the book with the given
1510 Returns a two-element list. C<$authors> is a reference-to-array, where
1511 each element is an additional author, and C<$count> is the number of
1512 elements in C<$authors>.
1518 my $dbh = C4::Context->dbh;
1519 my $sth=$dbh->prepare("Select * from additionalauthors where biblionumber=?");
1520 $sth->execute($bibnum);
1523 while (my $data=$sth->fetchrow_hashref){
1528 return($i,\@results);
1533 ($count, $subtitles) = &subtitle($biblionumber);
1535 Looks up the subtitles for the book with the given biblionumber.
1537 Returns a two-element list. C<$subtitles> is a reference-to-array,
1538 where each element is a subtitle, and C<$count> is the number of
1539 elements in C<$subtitles>.
1545 my $dbh = C4::Context->dbh;
1546 my $sth=$dbh->prepare("Select * from bibliosubtitle where biblionumber=?");
1547 $sth->execute($bibnum);
1550 while (my $data=$sth->fetchrow_hashref){
1555 return($i,\@results);
1560 @issues = &itemissues($biblioitemnumber, $biblio);
1562 Looks up information about who has borrowed the bookZ<>(s) with the
1563 given biblioitemnumber.
1565 C<$biblio> is ignored.
1567 C<&itemissues> returns an array of references-to-hash. The keys
1568 include the fields from the C<items> table in the Koha database.
1569 Additional keys include:
1575 If the item is currently on loan, this gives the due date.
1577 If the item is not on loan, then this is either "Available" or
1578 "Cancelled", if the item has been withdrawn.
1582 If the item is currently on loan, this gives the card number of the
1583 patron who currently has the item.
1585 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1587 These give the timestamp for the last three times the item was
1590 =item C<card0>, C<card1>, C<card2>
1592 The card number of the last three patrons who borrowed this item.
1594 =item C<borrower0>, C<borrower1>, C<borrower2>
1596 The borrower number of the last three patrons who borrowed this item.
1603 my ($bibitem, $biblio)=@_;
1604 my $dbh = C4::Context->dbh;
1605 # FIXME - If this function die()s, the script will abort, and the
1606 # user won't get anything; depending on how far the script has
1607 # gotten, the user might get a blank page. It would be much better
1608 # to at least print an error message. The easiest way to do this
1609 # is to set $SIG{__DIE__}.
1610 my $sth = $dbh->prepare("Select * from items where
1611 items.biblioitemnumber = ?")
1612 || die $dbh->errstr;
1616 $sth->execute($bibitem)
1617 || die $sth->errstr;
1619 while (my $data = $sth->fetchrow_hashref) {
1620 # Find out who currently has this item.
1621 # FIXME - Wouldn't it be better to do this as a left join of
1622 # some sort? Currently, this code assumes that if
1623 # fetchrow_hashref() fails, then the book is on the shelf.
1624 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1625 # database server crash), not just because no items match the
1627 my $sth2 = $dbh->prepare("select * from issues,borrowers
1628 where itemnumber = ?
1629 and returndate is NULL
1630 and issues.borrowernumber = borrowers.borrowernumber");
1632 $sth2->execute($data->{'itemnumber'});
1633 if (my $data2 = $sth2->fetchrow_hashref) {
1634 $data->{'date_due'} = $data2->{'date_due'};
1635 $data->{'card'} = $data2->{'cardnumber'};
1636 $data->{'borrower'} = $data2->{'borrowernumber'};
1638 if ($data->{'wthdrawn'} eq '1') {
1639 $data->{'date_due'} = 'Cancelled';
1641 $data->{'date_due'} = 'Available';
1647 # Find the last 3 people who borrowed this item.
1648 $sth2 = $dbh->prepare("select * from issues, borrowers
1649 where itemnumber = ?
1650 and issues.borrowernumber = borrowers.borrowernumber
1651 and returndate is not NULL
1652 order by returndate desc,timestamp desc") || die $dbh->errstr;
1653 $sth2->execute($data->{'itemnumber'}) || die $sth2->errstr;
1654 for (my $i2 = 0; $i2 < 2; $i2++) { # FIXME : error if there is less than 3 pple borrowing this item
1655 if (my $data2 = $sth2->fetchrow_hashref) {
1656 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1657 $data->{"card$i2"} = $data2->{'cardnumber'};
1658 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1663 $results[$i] = $data;
1673 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1675 Looks up the item with the given biblioitemnumber.
1677 C<$env> and C<$dbh> are ignored.
1679 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1680 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1686 my ($env,$dbh,$itemnumber) = @_;
1687 $dbh = C4::Context->dbh;
1688 my $sth=$dbh->prepare("Select * from biblio,items,biblioitems
1689 where items.itemnumber = ?
1690 and biblio.biblionumber = items.biblionumber
1691 and biblioitems.biblioitemnumber = items.biblioitemnumber");
1693 $sth->execute($itemnumber);
1694 my $data=$sth->fetchrow_hashref;
1701 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1703 Looks up patrons (borrowers) by name.
1707 BUGFIX 499: C<$type> is now used to determine type of search.
1708 if $type is "simple", search is performed on the first letter of the
1711 C<$searchstring> is a space-separated list of search terms. Each term
1712 must match the beginning a borrower's surname, first name, or other
1715 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1716 reference-to-array; each element is a reference-to-hash, whose keys
1717 are the fields of the C<borrowers> table in the Koha database.
1718 C<$count> is the number of elements in C<$borrowers>.
1722 #used by member enquiries from the intranet
1723 #called by member.pl
1725 my ($env,$searchstring,$orderby,$type)=@_;
1726 my $dbh = C4::Context->dbh;
1727 my $query = ""; my $count; my @data;
1730 if($type eq "simple") # simple search for one letter only
1732 $query="Select * from borrowers where surname like ? order by $orderby";
1733 @bind=("$searchstring%");
1735 else # advanced search looking in surname, firstname and othernames
1737 @data=split(' ',$searchstring);
1739 $query="Select * from borrowers
1740 where ((surname like ? or surname like ?
1741 or firstname like ? or firstname like ?
1742 or othernames like ? or othernames like ?)
1744 @bind=("$data[0]%","% $data[0]%","$data[0]%","% $data[0]%","$data[0]%","% $data[0]%");
1745 for (my $i=1;$i<$count;$i++){
1746 $query=$query." and (".
1747 " surname like ? or surname like ?
1748 or firstname like ? or firstname like ?
1749 or othernames like ? or othernames like ?)";
1750 push(@bind,"$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%");
1753 $query=$query.") or cardnumber like ?
1755 push(@bind,$searchstring);
1759 my $sth=$dbh->prepare($query);
1760 # warn "Q $orderby : $query";
1761 $sth->execute(@bind);
1764 while (my $data=$sth->fetchrow_hashref){
1765 push(@results,$data);
1769 return ($cnt,\@results);
1774 $borrower = &borrdata($cardnumber, $borrowernumber);
1776 Looks up information about a patron (borrower) by either card number
1777 or borrower number. If $borrowernumber is specified, C<&borrdata>
1778 searches by borrower number; otherwise, it searches by card number.
1780 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1781 the C<borrowers> table in the Koha database.
1786 my ($cardnumber,$bornum)=@_;
1787 $cardnumber = uc $cardnumber;
1788 my $dbh = C4::Context->dbh;
1791 $sth=$dbh->prepare("Select * from borrowers where cardnumber=?");
1792 $sth->execute($cardnumber);
1794 $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
1795 $sth->execute($bornum);
1797 my $data=$sth->fetchrow_hashref;
1801 } else { # try with firstname
1803 my $sth=$dbh->prepare("select * from borrowers where firstname=?");
1804 $sth->execute($cardnumber);
1805 my $data=$sth->fetchrow_hashref;
1815 ($count, $issues) = &borrissues($borrowernumber);
1817 Looks up what the patron with the given borrowernumber has borrowed.
1819 C<&borrissues> returns a two-element array. C<$issues> is a
1820 reference-to-array, where each element is a reference-to-hash; the
1821 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1822 in the Koha database. C<$count> is the number of elements in
1829 my $dbh = C4::Context->dbh;
1830 my $sth=$dbh->prepare("Select * from issues,biblio,items where borrowernumber=?
1831 and items.itemnumber=issues.itemnumber
1832 and items.biblionumber=biblio.biblionumber
1833 and issues.returndate is NULL order by date_due");
1834 $sth->execute($bornum);
1836 while (my $data = $sth->fetchrow_hashref) {
1837 push @result, $data;
1840 return(scalar(@result), \@result);
1845 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1847 Looks up what the patron with the given borrowernumber has borrowed,
1848 and sorts the results.
1850 C<$sortkey> is the name of a field on which to sort the results. This
1851 should be the name of a field in the C<issues>, C<biblio>,
1852 C<biblioitems>, or C<items> table in the Koha database.
1854 C<$limit> is the maximum number of results to return.
1856 C<&allissues> returns a two-element array. C<$issues> is a
1857 reference-to-array, where each element is a reference-to-hash; the
1858 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1859 C<items> tables of the Koha database. C<$count> is the number of
1860 elements in C<$issues>
1865 my ($bornum,$order,$limit)=@_;
1866 #FIXME: sanity-check order and limit
1867 my $dbh = C4::Context->dbh;
1868 my $query="Select * from issues,biblio,items,biblioitems
1869 where borrowernumber=? and
1870 items.biblioitemnumber=biblioitems.biblioitemnumber and
1871 items.itemnumber=issues.itemnumber and
1872 items.biblionumber=biblio.biblionumber order by $order";
1874 $query.=" limit $limit";
1877 my $sth=$dbh->prepare($query);
1878 $sth->execute($bornum);
1881 while (my $data=$sth->fetchrow_hashref){
1886 return($i,\@result);
1891 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
1893 Returns aggregate data about items borrowed by the patron with the
1894 given borrowernumber.
1898 C<&borrdata2> returns a three-element array. C<$borrowed> is the
1899 number of books the patron currently has borrowed. C<$due> is the
1900 number of overdue items the patron currently has borrowed. C<$fine> is
1901 the total fine currently due by the borrower.
1906 my ($env,$bornum)=@_;
1907 my $dbh = C4::Context->dbh;
1908 my $query="Select count(*) from issues where borrowernumber='$bornum' and
1909 returndate is NULL";
1911 my $sth=$dbh->prepare($query);
1913 my $data=$sth->fetchrow_hashref;
1915 $sth=$dbh->prepare("Select count(*) from issues where
1916 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1918 my $data2=$sth->fetchrow_hashref;
1920 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1921 borrowernumber='$bornum'");
1923 my $data3=$sth->fetchrow_hashref;
1926 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1929 =item getboracctrecord
1931 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1933 Looks up accounting data for the patron with the given borrowernumber.
1937 (FIXME - I'm not at all sure what this is about.)
1939 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1940 reference-to-array, where each element is a reference-to-hash; the
1941 keys are the fields of the C<accountlines> table in the Koha database.
1942 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1943 total amount outstanding for all of the account lines.
1947 sub getboracctrecord {
1948 my ($env,$params) = @_;
1949 my $dbh = C4::Context->dbh;
1952 my $sth=$dbh->prepare("Select * from accountlines where
1953 borrowernumber=? order by date desc,timestamp desc");
1955 $sth->execute($params->{'borrowernumber'});
1957 while (my $data=$sth->fetchrow_hashref){
1958 #FIXME before reinstating: insecure?
1959 # if ($data->{'itemnumber'} ne ''){
1960 # $query="Select * from items,biblio where items.itemnumber=
1961 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1962 # my $sth2=$dbh->prepare($query);
1964 # my $data2=$sth2->fetchrow_hashref;
1968 $acctlines[$numlines] = $data;
1970 $total += $data->{'amountoutstanding'};
1973 return ($numlines,\@acctlines,$total);
1978 $counts = &itemcount2($env, $biblionumber, $type);
1980 Counts the number of items with the given biblionumber, broken down by
1985 C<$type> may be either C<intra> or anything else. If it is not set to
1986 C<intra>, then the search will exclude lost, very overdue, and
1989 C<$&itemcount2> returns a reference-to-hash, with the following fields:
1995 The total number of items with this biblionumber.
1999 The number of items on order (aqorders.quantity -
2000 aqorders.quantityreceived).
2004 For each branch that has at least one copy of the book, C<$counts>
2005 will have a key with the branch name, giving the number of copies at
2013 my ($env,$bibnum,$type)=@_;
2014 my $dbh = C4::Context->dbh;
2015 my $query="Select * from items,branches where
2016 biblionumber=? and items.holdingbranch=branches.branchcode";
2017 if ($type ne 'intra'){
2018 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2019 (wthdrawn <> 1 or wthdrawn is NULL)";
2021 my $sth=$dbh->prepare($query);
2023 $sth->execute($bibnum);
2026 while (my $data=$sth->fetchrow_hashref){
2033 'select * from items
2035 and not ((items.itemlost <>1 and items.itemlost <> 2)
2036 or items.itemlost is NULL)'
2039 'select * from items
2040 where itemnumber=? and not (wthdrawn <> 1 or wthdrawn is NULL)'
2042 'On Loan', "select * from issues,items
2043 where issues.itemnumber=? and returndate is NULL
2044 and items.itemnumber=issues.itemnumber"
2047 my($testlabel, $query2) = @$test;
2049 my $sth2=$dbh->prepare($query2);
2050 $sth2->execute($data->{'itemnumber'});
2052 # FIXME - fetchrow_hashref() can fail for any number of reasons
2053 # (e.g., a database server crash). Perhaps use a left join of some
2055 $status = $testlabel if $sth2->fetchrow_hashref;
2057 last if defined $status;
2059 $status = $data->{'branchname'} unless defined $status;
2062 my $sth2=$dbh->prepare("Select * from aqorders where biblionumber=? and
2063 datecancellationprinted is NULL and quantity > quantityreceived");
2064 $sth2->execute($bibnum);
2065 if (my $data=$sth2->fetchrow_hashref){
2066 $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
2075 $description = &ItemType($itemtype);
2077 Given an item type code, returns the description for that type.
2082 # FIXME - I'm pretty sure that after the initial setup, the list of
2083 # item types doesn't change very often. Hence, it seems slow and
2084 # inefficient to make yet another database call to look up information
2085 # that'll only change every few months or years.
2087 # Much better, I think, to automatically build a Perl file that can be
2088 # included in those scripts that require it, e.g.:
2089 # @itemtypes = qw( ART BCD CAS CD F ... );
2091 # ART => "Art Prints",
2092 # BCD => "CD-ROM from book",
2093 # CD => "Compact disc (WN)",
2094 # F => "Free Fiction",
2097 # The web server can then run a cron job to rebuild this file from the
2098 # database every hour or so.
2100 # The same thing goes for branches, book funds, book sellers, currency
2101 # rates, printers, stopwords, and perhaps others.
2104 my $dbh = C4::Context->dbh;
2105 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
2106 $sth->execute($type);
2107 my $dat=$sth->fetchrow_hashref;
2109 return ($dat->{'description'});
2114 ($count, @results) = &bibitems($biblionumber);
2116 Given the biblionumber for a book, C<&bibitems> looks up that book's
2117 biblioitems (different publications of the same book, the audio book
2118 and film versions, etc.).
2120 C<$count> is the number of elements in C<@results>.
2122 C<@results> is an array of references-to-hash; the keys are the fields
2123 of the C<biblioitems> and C<itemtypes> tables of the Koha database. In
2124 addition, C<itemlost> indicates the availability of the item: if it is
2125 "2", then all copies of the item are long overdue; if it is "1", then
2126 all copies are lost; otherwise, there is at least one copy available.
2132 my $dbh = C4::Context->dbh;
2133 my $sth = $dbh->prepare("SELECT biblioitems.*,
2135 MIN(items.itemlost) as itemlost,
2136 MIN(items.dateaccessioned) as dateaccessioned
2137 FROM biblioitems, itemtypes, items
2138 WHERE biblioitems.biblionumber = ?
2139 AND biblioitems.itemtype = itemtypes.itemtype
2140 AND biblioitems.biblioitemnumber = items.biblioitemnumber
2141 GROUP BY items.biblioitemnumber");
2144 $sth->execute($bibnum);
2145 while (my $data = $sth->fetchrow_hashref) {
2146 $results[$count] = $data;
2150 return($count, @results);
2155 @barcodes = &barcodes($biblioitemnumber);
2157 Given a biblioitemnumber, looks up the corresponding items.
2159 Returns an array of references-to-hash; the keys are C<barcode> and
2162 The returned items include very overdue items, but not lost ones.
2167 #called from request.pl
2168 my ($biblioitemnumber)=@_;
2169 my $dbh = C4::Context->dbh;
2170 my $sth=$dbh->prepare("SELECT barcode, itemlost, holdingbranch FROM items
2171 WHERE biblioitemnumber = ?
2172 AND (wthdrawn <> 1 OR wthdrawn IS NULL)");
2173 $sth->execute($biblioitemnumber);
2176 while (my $data=$sth->fetchrow_hashref){
2177 $barcodes[$i]=$data;
2186 ($count, @websites) = &getwebsites($biblionumber);
2188 Looks up the web sites pertaining to the book with the given
2191 C<$count> is the number of elements in C<@websites>.
2193 C<@websites> is an array of references-to-hash; the keys are the
2194 fields from the C<websites> table in the Koha database.
2199 my ($biblionumber) = @_;
2200 my $dbh = C4::Context->dbh;
2201 my $sth = $dbh->prepare("Select * from websites where biblionumber = ?");
2205 $sth->execute($biblionumber);
2206 while (my $data = $sth->fetchrow_hashref) {
2207 # FIXME - The URL scheme shouldn't be stripped off, at least
2208 # not here, since it's part of the URL, and will be useful in
2209 # constructing a link to the site. If you don't want the user
2210 # to see the "http://" part, strip that off when building the
2212 $data->{'url'} =~ s/^http:\/\///; # FIXME - Leaning toothpick
2214 $results[$count] = $data;
2219 return($count, @results);
2222 =item getwebbiblioitems
2224 ($count, @results) = &getwebbiblioitems($biblionumber);
2226 Given a book's biblionumber, looks up the web versions of the book
2227 (biblioitems with itemtype C<WEB>).
2229 C<$count> is the number of items in C<@results>. C<@results> is an
2230 array of references-to-hash; the keys are the items from the
2231 C<biblioitems> table of the Koha database.
2235 sub getwebbiblioitems {
2236 my ($biblionumber) = @_;
2237 my $dbh = C4::Context->dbh;
2238 my $sth = $dbh->prepare("Select * from biblioitems where biblionumber = ?
2239 and itemtype = 'WEB'");
2243 $sth->execute($biblionumber);
2244 while (my $data = $sth->fetchrow_hashref) {
2245 $data->{'url'} =~ s/^http:\/\///;
2246 $results[$count] = $data;
2251 return($count, @results);
2252 } # sub getwebbiblioitems
2258 ($count, @results) = &isbnsearch($isbn,$title);
2260 Given an isbn and/or a title, returns the biblios having it.
2261 Used in acqui.simple, isbnsearch.pl only
2263 C<$count> is the number of items in C<@results>. C<@results> is an
2264 array of references-to-hash; the keys are the items from the
2265 C<biblioitems> table of the Koha database.
2270 my ($isbn,$title) = @_;
2271 my $dbh = C4::Context->dbh;
2277 $query = "Select distinct biblio.*, biblioitems.classification from biblio, biblioitems where
2278 biblio.biblionumber = biblioitems.biblionumber";
2281 $query .= " and isbn like ?";
2282 @bind=(uc($isbn)."%");
2285 $query .= " and title like ?";
2288 $sth = $dbh->prepare($query);
2290 $sth->execute(@bind);
2291 while (my $data = $sth->fetchrow_hashref) {
2292 $results[$count] = $data;
2297 return($count, @results);
2302 $branchname = &getbranchname($branchcode);
2304 Given the branch code, the function returns the corresponding
2305 branch name for a comprehensive information display
2311 my ($branchcode) = @_;
2312 my $dbh = C4::Context->dbh;
2313 my $sth = $dbh->prepare("SELECT branchname FROM branches WHERE branchcode = ?");
2314 $sth->execute($branchcode);
2315 my $branchname = $sth->fetchrow();
2318 } # sub getbranchname
2320 =item getborrowercategory
2322 $description = &getborrowercategory($categorycode);
2324 Given the borrower's category code, the function returns the corresponding
2325 description for a comprehensive information display.
2329 sub getborrowercategory
2332 my $dbh = C4::Context->dbh;
2333 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
2334 $sth->execute($catcode);
2335 my $description = $sth->fetchrow();
2337 return $description;
2338 } # sub getborrowercategory
2341 END { } # module clean-up code here (global destructor)
2350 Koha Developement team <info@koha.org>