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
36 C4::Search - Functions for searching the Koha catalog and other databases
42 my ($count, @results) = catalogsearch($env, $type, $search, $num, $offset);
46 This module provides the searching facilities for the Koha catalog and
49 C<&catalogsearch> is a front end to all the other searches. Depending
50 on what is passed to it, it calls the appropriate search function.
61 &CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
62 &itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
63 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
64 &getboracctrecord &ItemType &itemissues &subject &subtitle
65 &addauthor &bibitems &barcodes &findguarantees &allissues
66 &findguarantor &getwebsites &getwebbiblioitems &catalogsearch &itemcount2
67 &isbnsearch &breedingsearch &getbranchname &getborrowercategory);
68 # make all your functions, whether exported or not;
72 my (@results) = newsearch($itemtype,$duration,$number_of_results,$startfrom);
73 c<newsearch> find biblio acquired recently (last 30 days)
76 my ($itemtype,$duration,$num,$offset)=@_;
78 my $dbh = C4::Context->dbh;
79 my $sth=$dbh->prepare("SELECT to_days( now( ) ) - to_days( dateaccessioned ) AS duration, biblio.biblionumber, barcode, title, author, classification, itemtype, dewey, dateaccessioned, price, replacementprice
80 FROM items, biblio, biblioitems
81 WHERE biblio.biblionumber = biblioitems.biblionumber AND
82 items.biblionumber = biblio.biblionumber AND
83 to_days( now( ) ) - to_days( dateaccessioned ) < ? and itemtype=?
84 ORDER BY duration ASC ");
85 $sth->execute($duration,$itemtype);
88 while (my $data = $sth->fetchrow_hashref) {
89 if ($i>=$offset && $i<$num+$offset) {
90 my ($counts) = itemcount2("", $data->{'biblionumber'}, 'intra');
91 my $subject2=$data->{'subject'};
92 $subject2=~ s/ /%20/g;
93 $data->{'itemcount'}=$counts->{'total'};
94 my $totalitemcounts=0;
95 foreach my $key (keys %$counts){
96 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
97 #$data->{'location'}.="$key $counts->{$key} ";
98 $totalitemcounts+=$counts->{$key};
99 $data->{'locationhash'}->{$key}=$counts->{$key};
103 my $locationtextonly='';
104 my $notavailabletext='';
105 foreach (sort keys %{$data->{'locationhash'}}) {
106 if ($_ eq 'notavailable') {
107 $notavailabletext="Not available";
108 my $c=$data->{'locationhash'}->{$_};
109 $data->{'not-available-p'}=$totalitemcounts;
110 if ($totalitemcounts>1) {
111 $notavailabletext.=" ($c)";
112 $data->{'not-available-plural-p'}=1;
115 $locationtext.="$_ ";
116 my $c=$data->{'locationhash'}->{$_};
117 if ($_ eq 'Item Lost') {
118 $data->{'lost-p'}=$totalitemcounts;
119 $data->{'lost-plural-p'}=1
120 if $totalitemcounts > 1;
121 } elsif ($_ eq 'Withdrawn') {
122 $data->{'withdrawn-p'}=$totalitemcounts;
123 $data->{'withdrawn-plural-p'}=1
124 if $totalitemcounts > 1;
125 } elsif ($_ eq 'On Loan') {
126 $data->{'on-loan-p'}=$totalitemcounts;
127 $data->{'on-loan-plural-p'}=1
128 if $totalitemcounts > 1;
130 $locationtextonly.=$_;
131 $locationtextonly.=" ($c), "
132 if $totalitemcounts>1;
134 if ($totalitemcounts>1) {
135 $locationtext.=" ($c), ";
139 if ($notavailabletext) {
140 $locationtext.=$notavailabletext;
142 $locationtext=~s/, $//;
144 $data->{'location'}=$locationtext;
145 $data->{'location-only'}=$locationtextonly;
146 $data->{'subject2'}=$subject2;
147 $data->{'use-location-flags-p'}=1; # XXX
159 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
160 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
161 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
163 C<&findguarantees> takes a borrower number (e.g., that of a patron
164 with children) and looks up the borrowers who are guaranteed by that
165 borrower (i.e., the patron's children).
167 C<&findguarantees> returns two values: an integer giving the number of
168 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
169 of references to hash, which gives the actual results.
175 my $dbh = C4::Context->dbh;
176 my $sth=$dbh->prepare("select cardnumber,borrowernumber, firstname, surname from borrowers where guarantor=?");
177 $sth->execute($bornum);
180 while (my $data = $sth->fetchrow_hashref)
185 return (scalar(@dat), \@dat);
190 $guarantor = &findguarantor($borrower_no);
191 $guarantor_cardno = $guarantor->{"cardnumber"};
192 $guarantor_surname = $guarantor->{"surname"};
195 C<&findguarantor> takes a borrower number (presumably that of a child
196 patron), finds the guarantor for C<$borrower_no> (the child's parent),
197 and returns the record for the guarantor.
199 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
200 from the C<borrowers> database table;
206 my $dbh = C4::Context->dbh;
207 my $sth=$dbh->prepare("select guarantor from borrowers where borrowernumber=?");
208 $sth->execute($bornum);
209 my $data=$sth->fetchrow_hashref;
211 $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
212 $sth->execute($data->{'guarantor'});
213 $data=$sth->fetchrow_hashref;
218 =item NewBorrowerNumber
220 $num = &NewBorrowerNumber();
222 Allocates a new, unused borrower number, and returns it.
226 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
227 # Pick one and stick with it. Preferably use the other one. This function
228 # doesn't belong in C4::Search.
229 sub NewBorrowerNumber {
230 my $dbh = C4::Context->dbh;
231 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
233 my $data=$sth->fetchrow_hashref;
235 $data->{'max(borrowernumber)'}++;
236 return($data->{'max(borrowernumber)'});
241 ($count, @results) = &catalogsearch($env, $type, $search, $num, $offset);
243 This is primarily a front-end to other, more specialized catalog
244 search functions: if C<$search-E<gt>{itemnumber}> or
245 C<$search-E<gt>{isbn}> is given, C<&catalogsearch> uses a precise
246 C<&CatSearch>. If $search->{subject} is given, it runs a subject
247 C<&CatSearch>. If C<$search-E<gt>{keyword}> is given, it runs a
248 C<&KeywordSearch>. Otherwise, it runs a loose C<&CatSearch>.
250 If C<$env-E<gt>{itemcount}> is 1, then C<&catalogsearch> also counts
251 the items for each result, and adds several keys:
257 The total number of copies of this book.
259 =item C<locationhash>
261 This is a reference-to-hash; the keys are the names of branches where
262 this book may be found, and the values are the number of copies at
267 A descriptive string saying where the book is located, and how many
268 copies there are, if greater than 1.
272 The book's subject, with spaces replaced with C<%20>, presumably for
280 my ($env,$type,$search,$num,$offset)=@_;
281 my $dbh = C4::Context->dbh;
282 # foreach my $key (%$search){
283 # $search->{$key}=$dbh->quote($search->{$key});
285 my ($count,@results);
286 if ($search->{'itemnumber'} ne '' || $search->{'isbn'} ne ''){
287 print STDERR "Doing a precise search\n";
288 ($count,@results)=CatSearch($env,'precise',$search,$num,$offset);
289 } elsif ($search->{'subject'} ne ''){
290 ($count,@results)=CatSearch($env,'subject',$search,$num,$offset);
291 } elsif ($search->{'keyword'} ne ''){
292 ($count,@results)=&KeywordSearch($env,'keyword',$search,$num,$offset);
294 ($count,@results)=CatSearch($env,'loose',$search,$num,$offset);
297 if ($env->{itemcount} eq '1') {
298 foreach my $data (@results){
299 my ($counts) = itemcount2($env, $data->{'biblionumber'}, 'intra');
300 my $subject2=$data->{'subject'};
301 $subject2=~ s/ /%20/g;
302 $data->{'itemcount'}=$counts->{'total'};
303 my $totalitemcounts=0;
304 foreach my $key (keys %$counts){
305 if ($key ne 'total'){ # FIXME - Should ignore 'order', too.
306 #$data->{'location'}.="$key $counts->{$key} ";
307 $totalitemcounts+=$counts->{$key};
308 $data->{'locationhash'}->{$key}=$counts->{$key};
312 my $locationtextonly='';
313 my $notavailabletext='';
314 foreach (sort keys %{$data->{'locationhash'}}) {
315 if ($_ eq 'notavailable') {
316 $notavailabletext="Not available";
317 my $c=$data->{'locationhash'}->{$_};
318 $data->{'not-available-p'}=$totalitemcounts;
319 if ($totalitemcounts>1) {
320 $notavailabletext.=" ($c)";
321 $data->{'not-available-plural-p'}=1;
325 my $c=$data->{'locationhash'}->{$_};
326 if ($_ eq 'Item Lost') {
327 $data->{'lost-p'}=$totalitemcounts;
328 $data->{'lost-plural-p'}=1
329 if $totalitemcounts > 1;
330 } elsif ($_ eq 'Withdrawn') {
331 $data->{'withdrawn-p'}=$totalitemcounts;
332 $data->{'withdrawn-plural-p'}=1
333 if $totalitemcounts > 1;
334 } elsif ($_ eq 'On Loan') {
335 $data->{'on-loan-p'}=$totalitemcounts;
336 $data->{'on-loan-plural-p'}=1
337 if $totalitemcounts > 1;
339 $locationtextonly.=$_;
340 $locationtextonly.=" ($c), "
341 if $totalitemcounts>1;
343 if ($totalitemcounts>1) {
344 $locationtext.=" ($c), ";
348 if ($notavailabletext) {
349 $locationtext.=$notavailabletext;
351 $locationtext=~s/, $//;
353 $data->{'location'}=$locationtext;
354 $data->{'location-only'}=$locationtextonly;
355 $data->{'subject2'}=$subject2;
356 $data->{'use-location-flags-p'}=1; # XXX
359 return ($count,@results);
364 $search = { "keyword" => "One or more keywords",
365 "class" => "VID|CD", # Limit search to fiction and CDs
368 ($count, @results) = &KeywordSearch($env, $type, $search, $num, $offset);
370 C<&KeywordSearch> searches the catalog by keyword: given a string
371 (C<$search-E<gt>{"keyword"}> consisting of a space-separated list of
372 keywords, it looks for books that contain any of those keywords in any
373 of a number of places.
375 C<&KeywordSearch> looks for keywords in the book title (and subtitle),
376 series name, notes (both C<biblio.notes> and C<biblioitems.notes>),
379 C<$search-E<gt>{"class"}> can be set to a C<|> (pipe)-separated list of
380 item class codes (e.g., "F" for fiction, "JNF" for junior nonfiction,
381 etc.). In this case, the search will be restricted to just those
384 If C<$search-E<gt>{"class"}> is not specified, you may specify
385 C<$search-E<gt>{"dewey"}>. This will restrict the search to that
386 particular Dewey Decimal Classification category. Setting
387 C<$search-E<gt>{"dewey"}> to "513" will return books about arithmetic,
388 whereas setting it to "5" will return all books with Dewey code 5I<xx>
389 (Science and Mathematics).
391 C<$env> and C<$type> are ignored.
393 C<$offset> and C<$num> specify the subset of results to return.
394 C<$num> specifies the number of results to return, and C<$offset> is
395 the number of the first result. Thus, setting C<$offset> to 100 and
396 C<$num> to 5 will return results 100 through 104 inclusive.
401 my ($env,$type,$search,$num,$offset)=@_;
402 my $dbh = C4::Context->dbh;
403 $search->{'keyword'}=~ s/ +$//;
404 my @key=split(' ',$search->{'keyword'});
405 # FIXME - Naive users might enter comma-separated
406 # words, e.g., "training, animal". Ought to cope with
410 my %biblionumbers; # Set of biblionumbers returned by the
413 # FIXME - Ought to filter the stopwords out of the list of keywords.
414 # @key = map { !defined($stopwords{$_}) } @key;
416 # FIXME - The way this code is currently set up, it looks for all of
417 # the keywords first in (title, notes, seriestitle), then in the
418 # subtitle, then in the subject. Thus, if you look for keywords
419 # "science fiction", this search won't find a book with
420 # title = "How to write fiction"
421 # subtitle = "A science-based approach"
422 # Is this the desired effect? If not, then the first SQL query
423 # should look in the biblio, subtitle, and subject tables all at
424 # once. The way the first query is built can accomodate this easily.
426 # Look for keywords in table 'biblio'.
428 # Build an SQL query that finds each of the keywords in any of the
429 # title, biblio.notes, or seriestitle. To do this, we'll build up an
430 # array of clauses, one for each keyword.
431 my $query; # The SQL query
432 my @clauses = (); # The search clauses
433 my @bind = (); # The term bindings
435 $query = <<EOT; # Beginning of the query
440 foreach my $keyword (@key)
442 my @subclauses = (); # Subclauses, one for each field we're
445 # For each field we're searching on, create a subclause that'll
446 # match the current keyword in the current field.
447 foreach my $field (qw(title notes seriestitle author))
450 "$field LIKE ? OR $field LIKE ?";
451 push(@bind,"\Q$keyword\E%","% \Q$keyword\E%");
453 # (Yes, this could have been done as
454 # @subclauses = map {...} qw(field1 field2 ...)
455 # )but I think this way is more readable.
457 # Construct the current clause by joining the subclauses.
458 push @clauses, "(" . join(")\n\tOR (", @subclauses) . ")";
460 # Now join all of the clauses together and append to the query.
461 $query .= "(" . join(")\nAND (", @clauses) . ")";
463 # FIXME - Perhaps use $sth->bind_columns() ? Documented as the most
464 # efficient way to fetch data.
465 my $sth=$dbh->prepare($query);
466 $sth->execute(@bind);
467 while (my @res = $sth->fetchrow_array) {
470 $biblionumbers{$_} = 1; # Add these results to the set
475 # Now look for keywords in the 'bibliosubtitle' table.
477 # Again, we build a list of clauses from the keywords.
480 $query = "SELECT biblionumber FROM bibliosubtitle WHERE ";
481 foreach my $keyword (@key)
484 "subtitle LIKE ? OR subtitle like ?";
485 push(@bind,"\Q$keyword\E%","% \Q$keyword\E%");
487 $query .= "(" . join(") AND (", @clauses) . ")";
489 $sth=$dbh->prepare($query);
490 $sth->execute(@bind);
491 while (my @res = $sth->fetchrow_array) {
494 $biblionumbers{$_} = 1; # Add these results to the set
499 # Look for the keywords in the notes for individual items
500 # ('biblioitems.notes')
502 # Again, we build a list of clauses from the keywords.
505 $query = "SELECT biblionumber FROM biblioitems WHERE ";
506 foreach my $keyword (@key)
509 "notes LIKE ? OR notes like ?";
510 push(@bind,"\Q$keyword\E%","% \Q$keyword\E%");
512 $query .= "(" . join(") AND (", @clauses) . ")";
514 $sth=$dbh->prepare($query);
515 $sth->execute(@bind);
516 while (my @res = $sth->fetchrow_array) {
519 $biblionumbers{$_} = 1; # Add these results to the set
524 # Look for keywords in the 'bibliosubject' table.
526 # FIXME - The other queries look for words in the desired field that
527 # begin with the individual keywords the user entered. This one
528 # searches for the literal string the user entered. Is this the
530 # Note in particular that spaces are retained: if the user typed
532 # (with two spaces), this won't find the subject "science fiction"
533 # (one space). Likewise, a search for "%" will return absolutely
535 # If this isn't the desired effect, see the previous searches for
538 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
539 like ? group by biblionumber");
540 $sth->execute("%$search->{'keyword'}%");
542 while (my @res = $sth->fetchrow_array) {
545 $biblionumbers{$_} = 1; # Add these results to the set
555 my @res = keys %biblionumbers;
559 # print "count $count";
560 if ($search->{'class'} ne ''){
562 my $query="select * from biblio,biblioitems where
563 biblio.biblionumber=? and
564 biblio.biblionumber=biblioitems.biblionumber ";
565 my @bind = ($res[$i2]);
566 if ($search->{'class'} ne ''){ # FIXME - Redundant
567 my @temp=split(/\|/,$search->{'class'});
569 $query.= "and ( itemtype=?";
570 push(@bind,$temp[0]);
571 for (my $i=1;$i<$count;$i++){
572 $query.=" or itemtype=?";
573 push(@bind,$temp[$i]);
577 my $sth=$dbh->prepare($query);
579 $sth->execute(@bind);
580 if (my $data2=$sth->fetchrow_hashref){
581 my $dewey= $data2->{'dewey'};
582 my $subclass=$data2->{'subclass'};
583 # FIXME - This next bit is bogus, because it assumes that the
584 # Dewey code is a floating-point number. It isn't. It's
585 # actually a string that mainly consists of numbers. In
586 # particular, "4" is not a valid Dewey code, although "004"
587 # is ("Data processing; Computer science"). Likewise, zeros
588 # after the decimal are significant ("575" is not the same as
589 # "575.0"; the latter is more specific). And "000" is a
590 # perfectly good Dewey code ("General works; computer
591 # science") and should not be interpreted to mean "this
592 # database entry does not have a Dewey code". That's what
595 ($dewey == 0) && ($dewey='');
596 ($dewey) && ($dewey.=" $subclass") ;
598 my $end=$offset +$num;
603 if ($i4 <=$end && $i4 > $offset){
604 $data2->{'dewey'}=$dewey;
607 # $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
610 # print "in here $i3<br>";
621 # $search->{'class'} was not specified
623 # FIXME - This is bogus: it makes a separate query for each
624 # biblioitem, and returns results in apparently random order. It'd
625 # be much better to combine all of the previous queries into one big
626 # one (building it up a little at a time, of course), and have that
627 # big query select all of the desired fields, instead of just
630 while ($i2 < $num && $i2 < $count){
631 my $query="select * from biblio,biblioitems where
632 biblio.biblionumber=? and
633 biblio.biblionumber=biblioitems.biblionumber ";
634 my @bind=($res[$i2+$offset]);
636 if ($search->{'dewey'} ne ''){
637 $query.= "and (dewey like ?)";
638 push(@bind,"$search->{'dewey'}%");
641 my $sth=$dbh->prepare($query);
643 $sth->execute(@bind);
644 if (my $data2=$sth->fetchrow_hashref){
645 my $dewey= $data2->{'dewey'};
646 my $subclass=$data2->{'subclass'};
648 ($dewey == 0) && ($dewey='');
649 ($dewey) && ($dewey.=" $subclass") ;
651 $data2->{'dewey'}=$dewey;
654 # $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
663 return($count,@res2);
667 my ($env,$type,$search,$num,$offset)=@_;
668 my $dbh = C4::Context->dbh;
669 $search->{'keyword'}=~ s/ +$//;
670 my @key=split(' ',$search->{'keyword'});
674 my $query ="Select * from biblio,bibliosubtitle,biblioitems where
675 biblio.biblionumber=biblioitems.biblionumber and
676 biblio.biblionumber=bibliosubtitle.biblionumber and
677 (((title like ? or title like ?)";
678 my @bind=("$key[0]%","% $key[0]%");
680 $query .= " and (title like ? or title like ?)";
681 push(@bind,"$key[$i]%","% $key[$i]%");
684 $query.= ") or ((subtitle like ? or subtitle like ?)";
685 push(@bind,"$key[0]%","% $key[0]%");
686 for ($i=1;$i<$count;$i++){
687 $query.= " and (subtitle like ? or subtitle like ?)";
688 push(@bind,"$key[$i]%","% $key[$i]%");
690 $query.= ") or ((seriestitle like ? or seriestitle like ?)";
691 push(@bind,"$key[0]%","% $key[0]%");
692 for ($i=1;$i<$count;$i++){
693 $query.=" and (seriestitle like ? or seriestitle like ?)";
694 push(@bind,"$key[$i]%","% $key[$i]%");
696 $query.= ") or ((biblio.notes like ? or biblio.notes like ?)";
697 push(@bind,"$key[0]%","% $key[0]%");
698 for ($i=1;$i<$count;$i++){
699 $query.=" and (biblio.notes like ? or biblio.notes like ?)";
700 push(@bind,"$key[$i]%","% $key[$i]%");
702 $query.= ") or ((biblioitems.notes like ? or biblioitems.notes like ?)";
703 push(@bind,"$key[0]%","% $key[0]%");
704 for ($i=1;$i<$count;$i++){
705 $query.=" and (biblioitems.notes like ? or biblioitems.notes like ?)";
706 push(@bind,"$key[$i]%","% $key[$i]%");
708 if ($search->{'keyword'} =~ /new zealand/i){
709 $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
710 or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
711 or author like '% nz %' or author like '% nz')"
713 if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
714 $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
715 $search->{'keyword'} =~ / nz/i){
716 $query.= "or (title like 'new zealand%' or title like '% new zealand %'
717 or title like '% new zealand' or subtitle like 'new zealand%' or
718 subtitle like '% new zealand %'
719 or subtitle like '% new zealand' or author like 'new zealand%'
720 or author like '% new zealand %' or author like '% new zealand' or
721 seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
722 or seriestitle like '% new zealand')"
725 if ($search->{'class'} ne ''){
726 my @temp=split(/\|/,$search->{'class'});
728 $query.= "and ( itemtype=?";
729 push(@bind,"$temp[0]");
730 for (my $i=1;$i<$count;$i++){
731 $query.=" or itemtype=?";
732 push(@bind,"$temp[$i]");
736 if ($search->{'dewey'} ne ''){
737 $query.= "and (dewey like '$search->{'dewey'}%') ";
739 $query.="group by biblio.biblionumber";
740 #$query.=" order by author,title";
742 my $sth=$dbh->prepare($query);
743 $sth->execute(@bind);
745 while (my $data=$sth->fetchrow_hashref){
746 #FIXME: rewrite to use ? before uncomment
747 # my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
750 # my ($dewey, $subclass) = $sti->fetchrow;
751 my $dewey=$data->{'dewey'};
752 my $subclass=$data->{'subclass'};
754 ($dewey == 0) && ($dewey='');
755 ($dewey) && ($dewey.=" $subclass");
757 $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
758 # print $results[$i];
762 $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
763 like ? group by biblionumber");
764 $sth->execute("%".$search->{'keyword'}."%");
765 while (my $data=$sth->fetchrow_hashref){
766 $query="Select * from biblio,biblioitems where
767 biblio.biblionumber=? and
768 biblio.biblionumber=biblioitems.biblionumber ";
769 @bind=($data->{'biblionumber'});
770 if ($search->{'class'} ne ''){
771 my @temp=split(/\|/,$search->{'class'});
773 $query.= " and ( itemtype=?";
774 push(@bind,$temp[0]);
775 for (my $i=1;$i<$count;$i++){
776 $query.=" or itemtype=?";
777 push(@bind,$temp[$i]);
782 if ($search->{'dewey'} ne ''){
783 $query.= "and (dewey like ?)";
784 push(@bind,"$search->{'dewey'}%");
786 my $sth2=$dbh->prepare($query);
787 $sth2->execute(@bind);
789 while (my $data2=$sth2->fetchrow_hashref){
790 my $dewey= $data2->{'dewey'};
791 my $subclass=$data2->{'subclass'};
793 ($dewey == 0) && ($dewey='');
794 ($dewey) && ($dewey.=" $subclass") ;
796 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
797 # print $results[$i];
803 @results=sort @results;
810 while ($i2 < $count){
811 if ($results[$i2] ne $res[$i-1]){
812 $res[$i]=$results[$i2];
820 while ($i2 < $num && $i2 < $count){
821 $res2[$i2]=$res[$i2+$offset];
833 ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
835 C<&CatSearch> searches the Koha catalog. It returns a list whose first
836 element is the number of returned results, and whose subsequent
837 elements are the results themselves.
839 Each returned element is a reference-to-hash. Most of the keys are
840 simply the fields from the C<biblio> table in the Koha database, but
841 the following keys may also be present:
847 The book's illustrator.
857 C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
858 high-level behavior of C<&CatSearch>, as described below.
860 In many cases, the description below says that a certain field in the
861 database must match the search string. In these cases, it means that
862 the beginning of some word in the field must match the search string.
863 Thus, an author search for "sm" will return books whose author is
864 "John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
865 does not occur at the beginning of a word.
867 Note that within each search mode, the criteria are and-ed together.
868 That is, if you perform a loose search on the author "Jerome" and the
869 title "Boat", the search will only return books by Jerome containing
872 It is not possible to cross modes, e.g., set the author to "Asimov"
873 and the subject to "Math" in hopes of finding books on math by Asimov.
877 If C<$type> is set to C<loose>, the following search criteria may be
882 =item C<$search-E<gt>{author}>
884 The search string is a space-separated list of words. Each word must
885 match either the C<author> or C<additionalauthors> field.
887 =item C<$search-E<gt>{title}>
889 Each word in the search string must match the book title. If no author
890 is specified, the book subtitle will also be searched.
892 =item C<$search-E<gt>{abstract}>
894 Searches for the given search string in the book's abstract.
896 =item C<$search-E<gt>{'date-before'}>
898 Searches for books whose copyright date matches the search string.
899 That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
900 books written in 1985, and setting it to "198" will find books written
901 between 1980 and 1989.
903 =item C<$search-E<gt>{title}>
905 Searches by title are also affected by the value of
906 C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
907 title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
908 match the search string exactly (the subtitle is not searched).
910 If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
911 each word in the search string must match the title, subtitle,
912 unititle, or series title.
914 =item C<$search-E<gt>{class}>
916 Restricts the search to certain item classes. The value of
917 C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
918 Thus, setting it to "F" restricts the search to fiction, and setting
919 it to "CD|CAS" will only look in compact disks and cassettes.
921 =item C<$search-E<gt>{dewey}>
923 Searches for books whose Dewey Decimal Classification code matches the
924 search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
925 search for all books in 5I<xx> (Science and mathematics), setting it
926 to "54" will search for all books in 54I<x> (Chemistry), and setting
927 it to "546" will search for books on inorganic chemistry.
929 =item C<$search-E<gt>{publisher}>
931 Searches for books whose publisher contains the search string (unlike
932 other search criteria, C<$search-E<gt>{publisher}> is a string, not a
937 =head2 Subject search
939 If C<$type> is set to C<subject>, the following search criterion may
944 =item C<$search-E<gt>{subject}>
946 The search string is a space-separated list of words, each of which
947 must match the book's subject.
949 Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
950 C<&CatSearch> will search for books whose subject is "New Zealand".
951 However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
952 search for books on "nz" and "football", not books on "New Zealand"
957 =head2 Precise search
959 If C<$type> is set to C<precise>, the following search criteria may be
964 =item C<$search-E<gt>{item}>
966 Searches for books whose barcode exactly matches the search string.
968 =item C<$search-E<gt>{isbn}>
970 Searches for books whose ISBN exactly matches the search string.
974 For a loose search, if an author was specified, the results are
975 ordered by author and title. If no author was specified, the results
976 are ordered by title.
978 For other (non-loose) searches, if a subject was specified, the
979 results are ordered alphabetically by subject.
981 In all other cases (e.g., loose search by keyword), the results are
987 my ($env,$type,$search,$num,$offset)=@_;
988 my $dbh = C4::Context->dbh;
993 my $title = lc($search->{'title'});
995 if ($type eq 'loose') {
996 if ($search->{'author'} ne ''){
997 my @key=split(' ',$search->{'author'});
1000 $query="select *,biblio.author,biblio.biblionumber from
1002 left join additionalauthors
1003 on additionalauthors.biblionumber =biblio.biblionumber
1005 ((biblio.author like ? or biblio.author like ? or
1006 additionalauthors.author like ? or additionalauthors.author
1009 @bind=("$key[0]%","% $key[0]%","$key[0]%","% $key[0]%");
1010 while ($i < $count){
1012 biblio.author like ? or biblio.author like ? or
1013 additionalauthors.author like ? or additionalauthors.author like ?
1015 push(@bind,"$key[$i]%","% $key[$i]%","$key[$i]%","% $key[$i]%");
1019 if ($search->{'title'} ne ''){
1020 my @key=split(' ',$search->{'title'});
1023 $query.= " and (((title like ? or title like ?)";
1024 push(@bind,"$key[0]%","% $key[0]%");
1026 $query .= " and (title like ? or title like ?)";
1027 push(@bind,"$key[$i]%","% $key[$i]%");
1030 $query.=") or ((seriestitle like ? or seriestitle like ?)";
1031 push(@bind,"$key[0]%","% $key[0]%");
1032 for ($i=1;$i<$count;$i++){
1033 $query.=" and (seriestitle like ? or seriestitle like ?)";
1034 push(@bind,"$key[$i]%","% $key[$i]%");
1036 $query.=") or ((unititle like ? or unititle like ?)";
1037 push(@bind,"$key[0]%","% $key[0]%");
1038 for ($i=1;$i<$count;$i++){
1039 $query.=" and (unititle like ? or unititle like ?)";
1040 push(@bind,"$key[$i]%","% $key[$i]%");
1044 if ($search->{'abstract'} ne ''){
1045 $query.= " and (abstract like ?)";
1046 push(@bind,"%$search->{'abstract'}%");
1048 if ($search->{'date-before'} ne ''){
1049 $query.= " and (copyrightdate like ?)";
1050 push(@bind,"%$search->{'date-before'}%");
1052 $query.=" group by biblio.biblionumber";
1054 if ($search->{'title'} ne '') {
1055 if ($search->{'ttype'} eq 'exact'){
1056 $query="select * from biblio
1058 (biblio.title=? or (biblio.unititle = ?
1059 or biblio.unititle like ? or
1060 biblio.unititle like ? or
1061 biblio.unititle like ?) or
1062 (biblio.seriestitle = ? or
1063 biblio.seriestitle like ? or
1064 biblio.seriestitle like ? or
1065 biblio.seriestitle like ?)
1067 @bind=($search->{'title'},$search->{'title'},"$search->{'title'} |%","%| $search->{'title'} |%","%| $search->{'title'}",$search->{'title'},"$search->{'title'} |%","%| $search->{'title'} |%","%| $search->{'title'}");
1069 my @key=split(' ',$search->{'title'});
1072 $query="select biblio.biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp,subtitle from biblio
1073 left join bibliosubtitle on
1074 biblio.biblionumber=bibliosubtitle.biblionumber
1076 (((title like ? or title like ?)";
1077 @bind=("$key[0]%","% $key[0]%");
1079 $query .= " and (title like ? or title like ?)";
1080 push(@bind,"$key[$i]%","% $key[$i]%");
1083 $query.=") or ((subtitle like ? or subtitle like ?)";
1084 push(@bind,"$key[0]%","% $key[0]%");
1085 for ($i=1;$i<$count;$i++){
1086 $query.=" and (subtitle like ? or subtitle like ?)";
1087 push(@bind,"$key[$i]%","% $key[$i]%");
1089 $query.=") or ((seriestitle like ? or seriestitle like ?)";
1090 push(@bind,"$key[0]%","% $key[0]%");
1091 for ($i=1;$i<$count;$i++){
1092 $query.=" and (seriestitle like ? or seriestitle like ?)";
1093 push(@bind,"$key[$i]%","% $key[$i]%");
1095 $query.=") or ((unititle like ? or unititle like ?)";
1096 push(@bind,"$key[0]%","% $key[0]%");
1097 for ($i=1;$i<$count;$i++){
1098 $query.=" and (unititle like ? or unititle like ?)";
1099 push(@bind,"$key[$i]%","% $key[$i]%");
1103 if ($search->{'abstract'} ne ''){
1104 $query.= " and (abstract like ?)";
1105 push(@bind,"%$search->{'abstract'}%");
1107 if ($search->{'date-before'} ne ''){
1108 $query.= " and (copyrightdate like ?)";
1109 push(@bind,"%$search->{'date-before'}%");
1111 } elsif ($search->{'class'} ne ''){
1112 $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
1113 my @temp=split(/\|/,$search->{'class'});
1115 $query.= " and ( itemtype= ?)";
1117 for (my $i=1;$i<$count;$i++){
1118 $query.=" or itemtype=?";
1119 push(@bind,$temp[$i]);
1122 if ($search->{'illustrator'} ne ''){
1123 $query.=" and illus like ?";
1124 push(@bind,"%".$search->{'illustrator'}."%");
1126 if ($search->{'dewey'} ne ''){
1127 $query.=" and biblioitems.dewey like ?";
1128 push(@bind,"$search->{'dewey'}%");
1130 } elsif ($search->{'dewey'} ne ''){
1131 $query="select * from biblioitems,biblio
1132 where biblio.biblionumber=biblioitems.biblionumber
1133 and biblioitems.dewey like ?";
1134 @bind=("$search->{'dewey'}%");
1135 } elsif ($search->{'illustrator'} ne '') {
1136 $query="select * from biblioitems,biblio
1137 where biblio.biblionumber=biblioitems.biblionumber
1138 and biblioitems.illus like ?";
1139 @bind=("%".$search->{'illustrator'}."%");
1140 } elsif ($search->{'publisher'} ne ''){
1141 $query = "Select * from biblio,biblioitems where biblio.biblionumber
1142 =biblioitems.biblionumber and (publishercode like ?)";
1143 @bind=("%$search->{'publisher'}%");
1144 } elsif ($search->{'abstract'} ne ''){
1145 $query = "Select * from biblio where abstract like ?";
1146 @bind=("%$search->{'abstract'}%");
1147 } elsif ($search->{'date-before'} ne ''){
1148 $query = "Select * from biblio where copyrightdate like ?";
1149 @bind=("%$search->{'date-before'}%");
1151 $query .=" group by biblio.biblionumber";
1154 if ($type eq 'subject'){
1155 my @key=split(' ',$search->{'subject'});
1158 $query="select * from bibliosubject, biblioitems where
1159 (bibliosubject.biblionumber = biblioitems.biblionumber) and ( subject like ? or subject like ? or subject like ?)";
1160 @bind=("$key[0]%","% $key[0]%","%($key[0])%");
1162 $query.=" and (subject like ? or subject like ? or subject like ?)";
1163 push(@bind,"$key[$i]%","% $key[$i]%","%($key[$i])%");
1167 # FIXME - Wouldn't it be better to fix the database so that if a
1168 # book has a subject "NZ", then it also gets added the subject
1170 # This can also be generalized by adding a table of subject
1171 # synonyms to the database: just declare "NZ" to be a synonym for
1172 # "New Zealand", "SF" a synonym for both "Science fiction" and
1173 # "Fantastic fiction", etc.
1175 if (lc($search->{'subject'}) eq 'nz'){
1176 $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
1177 or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
1178 } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
1179 $query=~ s/ nz/ NEW ZEALAND/ig;
1180 $query=~ s/nz /NEW ZEALAND /ig;
1181 $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
1184 if ($type eq 'precise'){
1185 if ($search->{'itemnumber'} ne ''){
1186 $query="select * from items,biblio ";
1187 my $search2=uc $search->{'itemnumber'};
1188 $query=$query." where
1189 items.biblionumber=biblio.biblionumber
1194 if ($search->{'isbn'} ne ''){
1195 my $search2=uc $search->{'isbn'};
1196 my $sth1=$dbh->prepare("select * from biblioitems where isbn=?");
1197 $sth1->execute($search2);
1199 while (my $data=$sth1->fetchrow_hashref) {
1200 my $sth=$dbh->prepare("select * from biblioitems,biblio where
1201 biblio.biblionumber = ?
1202 and biblioitems.biblionumber = biblio.biblionumber");
1203 $sth->execute($data->{'biblionumber'});
1204 # FIXME - There's already a $data in this scope.
1205 my $data=$sth->fetchrow_hashref;
1206 my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
1207 # FIXME - The following assumes that the Dewey code is a
1208 # floating-point number. It isn't: it's a string.
1210 ($dewey == 0) && ($dewey='');
1211 ($dewey) && ($dewey.=" $subclass");
1212 $data->{'dewey'}=$dewey;
1213 $results[$i2]=$data;
1214 # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
1221 if ($type ne 'precise' && $type ne 'subject'){
1222 if ($search->{'author'} ne ''){
1223 $query .= " order by biblio.author,title";
1225 $query .= " order by title";
1228 if ($type eq 'subject'){
1229 $query .= " group by subject ";
1232 my $sth=$dbh->prepare($query);
1233 $sth->execute(@bind);
1236 my $limit= $num+$offset;
1237 while (my $data=$sth->fetchrow_hashref){
1238 my $query="select classification,dewey,subclass,publishercode from biblioitems where biblionumber=?";
1239 my @bind=($data->{'biblionumber'});
1240 if ($search->{'class'} ne ''){
1241 my @temp=split(/\|/,$search->{'class'});
1243 $query.= " and ( itemtype= ?";
1244 push(@bind,$temp[0]);
1245 for (my $i=1;$i<$count;$i++){
1246 $query.=" or itemtype=?";
1247 push(@bind,$temp[$i]);
1251 if ($search->{'dewey'} ne ''){
1252 $query.=" and dewey=? ";
1253 push(@bind,$search->{'dewey'});
1255 if ($search->{'illustrator'} ne ''){
1256 $query.=" and illus like ?";
1257 push(@bind,"%$search->{'illustrator'}%");
1259 if ($search->{'publisher'} ne ''){
1260 $query.= " and (publishercode like ?)";
1261 push(@bind,"%$search->{'publisher'}%");
1263 my $sti=$dbh->prepare($query);
1264 $sti->execute(@bind);
1271 if ($bibitemdata = $sti->fetchrow_hashref()){
1273 $classification=$bibitemdata->{'classification'};
1274 $dewey=$bibitemdata->{'dewey'};
1275 $subclass=$bibitemdata->{'subclass'};
1276 $publishercode=$bibitemdata->{'publishercode'};
1278 # print STDERR "$dewey $subclass $publishercode\n";
1279 # FIXME - The Dewey code is a string, not a number.
1281 ($dewey == 0) && ($dewey='');
1282 ($dewey) && ($dewey.=" $subclass");
1283 $data->{'classification'}=$classification;
1284 $data->{'dewey'}=$dewey;
1285 $data->{'publishercode'}=$publishercode;
1288 if ($count > $offset && $count <= $limit){
1297 return($count,@results);
1300 sub updatesearchstats{
1301 my ($dbh,$query)=@_;
1307 @results = &subsearch($env, $subject);
1309 Searches for books that have a subject that exactly matches
1312 C<&subsearch> returns an array of results. Each element of this array
1313 is a string, containing the book's title, author, and biblionumber,
1321 my ($env,$subject)=@_;
1322 my $dbh = C4::Context->dbh;
1323 my $sth=$dbh->prepare("Select * from biblio,bibliosubject where
1324 biblio.biblionumber=bibliosubject.biblionumber and
1325 bibliosubject.subject=? group by biblio.biblionumber
1326 order by biblio.title");
1327 $sth->execute($subject);
1330 while (my $data=$sth->fetchrow_hashref){
1331 push @results, $data;
1340 @results = &ItemInfo($env, $biblionumber, $type);
1342 Returns information about books with the given biblionumber.
1344 C<$type> may be either C<intra> or anything else. If it is not set to
1345 C<intra>, then the search will exclude lost, very overdue, and
1350 C<&ItemInfo> returns a list of references-to-hash. Each element
1351 contains a number of keys. Most of them are table items from the
1352 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1353 Koha database. Other keys include:
1357 =item C<$data-E<gt>{branchname}>
1359 The name (not the code) of the branch to which the book belongs.
1361 =item C<$data-E<gt>{datelastseen}>
1363 This is simply C<items.datelastseen>, except that while the date is
1364 stored in YYYY-MM-DD format in the database, here it is converted to
1365 DD/MM/YYYY format. A NULL date is returned as C<//>.
1367 =item C<$data-E<gt>{datedue}>
1369 =item C<$data-E<gt>{class}>
1371 This is the concatenation of C<biblioitems.classification>, the book's
1372 Dewey code, and C<biblioitems.subclass>.
1374 =item C<$data-E<gt>{ocount}>
1376 I think this is the number of copies of the book available.
1378 =item C<$data-E<gt>{order}>
1380 If this is set, it is set to C<One Order>.
1387 my ($env,$biblionumber,$type) = @_;
1388 my $dbh = C4::Context->dbh;
1389 my $query = "SELECT *,items.notforloan as itemnotforloan FROM items, biblio, biblioitems
1390 left join itemtypes on biblioitems.itemtype = itemtypes.itemtype
1391 WHERE items.biblionumber = ?
1392 AND biblioitems.biblioitemnumber = items.biblioitemnumber
1393 AND biblio.biblionumber = items.biblionumber";
1394 if ($type ne 'intra'){
1395 $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
1396 or items.itemlost is NULL)
1397 and (wthdrawn <> 1 or wthdrawn is NULL)";
1399 $query .= " order by items.dateaccessioned desc";
1400 my $sth=$dbh->prepare($query);
1401 $sth->execute($biblionumber);
1404 while (my $data=$sth->fetchrow_hashref){
1406 my $isth=$dbh->prepare("Select * from issues where itemnumber = ? and returndate is null");
1407 $isth->execute($data->{'itemnumber'});
1408 if (my $idata=$isth->fetchrow_hashref){
1409 $datedue = format_date($idata->{'date_due'});
1411 if ($data->{'itemlost'} eq '2'){
1412 $datedue='Very Overdue';
1414 if ($data->{'itemlost'} eq '1'){
1417 if ($data->{'wthdrawn'} eq '1'){
1418 $datedue="Cancelled";
1420 if ($datedue eq ''){
1421 # $datedue="Available";
1422 my ($restype,$reserves)=C4::Reserves2::CheckReserves($data->{'itemnumber'});
1428 #get branch information.....
1429 my $bsth=$dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
1430 $bsth->execute($data->{'holdingbranch'});
1431 if (my $bdata=$bsth->fetchrow_hashref){
1432 $data->{'branchname'} = $bdata->{'branchname'};
1434 # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
1435 # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
1436 # to leave it empty, rather than convert it to "//".
1437 # Also ideally this should use the local format for displaying dates.
1438 my $date=format_date($data->{'datelastseen'});
1439 $data->{'datelastseen'}=$date;
1440 $data->{'datedue'}=$datedue;
1445 #FIXME: ordering/indentation here looks wrong
1446 my $sth2=$dbh->prepare("Select * from aqorders where biblionumber=?");
1447 $sth2->execute($biblionumber);
1450 if ($data=$sth2->fetchrow_hashref){
1451 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1453 $data->{'ocount'}=$ocount;
1454 $data->{'order'}="One Order";
1465 @results = &GetItems($env, $biblionumber);
1467 Returns information about books with the given biblionumber.
1471 C<&GetItems> returns an array of strings. Each element is a
1472 tab-separated list of values: biblioitemnumber, itemtype,
1473 classification, Dewey number, subclass, ISBN, volume, number, and
1476 Itemdata, in turn, is a string of the form
1477 "I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
1478 the string C<NFL> if the item is not for loan, and C<LOST> if the item
1484 my ($env,$biblionumber)=@_;
1485 #debug_msg($env,"GetItems");
1486 my $dbh = C4::Context->dbh;
1487 my $sth=$dbh->prepare("Select * from biblioitems where (biblionumber = ?)");
1488 $sth->execute($biblionumber);
1489 #debug_msg($env,"executed query");
1492 while (my $data=$sth->fetchrow_hashref) {
1493 #debug_msg($env,$data->{'biblioitemnumber'});
1494 my $dewey = $data->{'dewey'};
1496 my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
1497 $line .= "\t$data->{'classification'}\t$dewey";
1498 $line .= "\t$data->{'subclass'}\t$data->{isbn}";
1499 $line .= "\t$data->{'volume'}\t$data->{number}";
1500 my $isth= $dbh->prepare("select * from items where biblioitemnumber = ?");
1501 $isth->execute($data->{'biblioitemnumber'});
1502 while (my $idata = $isth->fetchrow_hashref) {
1503 my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
1504 if ($idata->{'notforloan'} == 1) {
1507 if ($idata->{'itemlost'} == 1) {
1510 $line .= "\t$iline";
1513 $results[$i] = $line;
1522 $item = &itemdata($barcode);
1524 Looks up the item with the given barcode, and returns a
1525 reference-to-hash containing information about that item. The keys of
1526 the hash are the fields from the C<items> and C<biblioitems> tables in
1533 my $dbh = C4::Context->dbh;
1534 my $sth=$dbh->prepare("Select * from items,biblioitems where barcode=?
1535 and items.biblioitemnumber=biblioitems.biblioitemnumber");
1536 $sth->execute($barcode);
1537 my $data=$sth->fetchrow_hashref;
1544 $data = &bibdata($biblionumber, $type);
1546 Returns information about the book with the given biblionumber.
1548 C<$type> is ignored.
1550 C<&bibdata> returns a reference-to-hash. The keys are the fields in
1551 the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
1554 In addition, C<$data-E<gt>{subject}> is the list of the book's
1555 subjects, separated by C<" , "> (space, comma, space).
1557 If there are multiple biblioitems with the given biblionumber, only
1558 the first one is considered.
1563 my ($bibnum, $type) = @_;
1564 my $dbh = C4::Context->dbh;
1565 my $sth = $dbh->prepare("Select *, biblioitems.notes AS bnotes, biblio.notes
1566 from biblio, biblioitems
1567 left join bibliosubtitle on
1568 biblio.biblionumber = bibliosubtitle.biblionumber
1569 where biblio.biblionumber = ?
1570 and biblioitems.biblionumber = biblio.biblionumber");
1571 $sth->execute($bibnum);
1573 $data = $sth->fetchrow_hashref;
1575 $sth = $dbh->prepare("Select * from bibliosubject where biblionumber = ?");
1576 $sth->execute($bibnum);
1578 while (my $dat = $sth->fetchrow_hashref){
1580 $line{subject} = $dat->{'subject'};
1581 push @subjects, \%line;
1583 $data->{subjects} = \@subjects;
1585 $sth = $dbh->prepare("Select * from additionalauthors where biblionumber = ?");
1586 $sth->execute($bibnum);
1587 while (my $dat = $sth->fetchrow_hashref){
1588 $data->{'additionalauthors'} .= "$dat->{'author'}, ";
1590 chop $data->{'additionalauthors'};
1591 chop $data->{'additionalauthors'};
1598 $itemdata = &bibitemdata($biblioitemnumber);
1600 Looks up the biblioitem with the given biblioitemnumber. Returns a
1601 reference-to-hash. The keys are the fields from the C<biblio>,
1602 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1603 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1609 my $dbh = C4::Context->dbh;
1610 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");
1613 $sth->execute($bibitem);
1615 $data = $sth->fetchrow_hashref;
1623 ($count, $subjects) = &subject($biblionumber);
1625 Looks up the subjects of the book with the given biblionumber. Returns
1626 a two-element list. C<$subjects> is a reference-to-array, where each
1627 element is a subject of the book, and C<$count> is the number of
1628 elements in C<$subjects>.
1634 my $dbh = C4::Context->dbh;
1635 my $sth=$dbh->prepare("Select * from bibliosubject where biblionumber=?");
1636 $sth->execute($bibnum);
1639 while (my $data=$sth->fetchrow_hashref){
1644 return($i,\@results);
1649 ($count, $authors) = &addauthors($biblionumber);
1651 Looks up the additional authors for the book with the given
1654 Returns a two-element list. C<$authors> is a reference-to-array, where
1655 each element is an additional author, and C<$count> is the number of
1656 elements in C<$authors>.
1662 my $dbh = C4::Context->dbh;
1663 my $sth=$dbh->prepare("Select * from additionalauthors where biblionumber=?");
1664 $sth->execute($bibnum);
1667 while (my $data=$sth->fetchrow_hashref){
1672 return($i,\@results);
1677 ($count, $subtitles) = &subtitle($biblionumber);
1679 Looks up the subtitles for the book with the given biblionumber.
1681 Returns a two-element list. C<$subtitles> is a reference-to-array,
1682 where each element is a subtitle, and C<$count> is the number of
1683 elements in C<$subtitles>.
1689 my $dbh = C4::Context->dbh;
1690 my $sth=$dbh->prepare("Select * from bibliosubtitle where biblionumber=?");
1691 $sth->execute($bibnum);
1694 while (my $data=$sth->fetchrow_hashref){
1699 return($i,\@results);
1704 @issues = &itemissues($biblioitemnumber, $biblio);
1706 Looks up information about who has borrowed the bookZ<>(s) with the
1707 given biblioitemnumber.
1709 C<$biblio> is ignored.
1711 C<&itemissues> returns an array of references-to-hash. The keys
1712 include the fields from the C<items> table in the Koha database.
1713 Additional keys include:
1719 If the item is currently on loan, this gives the due date.
1721 If the item is not on loan, then this is either "Available" or
1722 "Cancelled", if the item has been withdrawn.
1726 If the item is currently on loan, this gives the card number of the
1727 patron who currently has the item.
1729 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
1731 These give the timestamp for the last three times the item was
1734 =item C<card0>, C<card1>, C<card2>
1736 The card number of the last three patrons who borrowed this item.
1738 =item C<borrower0>, C<borrower1>, C<borrower2>
1740 The borrower number of the last three patrons who borrowed this item.
1747 my ($bibitem, $biblio)=@_;
1748 my $dbh = C4::Context->dbh;
1749 # FIXME - If this function die()s, the script will abort, and the
1750 # user won't get anything; depending on how far the script has
1751 # gotten, the user might get a blank page. It would be much better
1752 # to at least print an error message. The easiest way to do this
1753 # is to set $SIG{__DIE__}.
1754 my $sth = $dbh->prepare("Select * from items where
1755 items.biblioitemnumber = ?")
1756 || die $dbh->errstr;
1760 $sth->execute($bibitem)
1761 || die $sth->errstr;
1763 while (my $data = $sth->fetchrow_hashref) {
1764 # Find out who currently has this item.
1765 # FIXME - Wouldn't it be better to do this as a left join of
1766 # some sort? Currently, this code assumes that if
1767 # fetchrow_hashref() fails, then the book is on the shelf.
1768 # fetchrow_hashref() can fail for any number of reasons (e.g.,
1769 # database server crash), not just because no items match the
1771 my $sth2 = $dbh->prepare("select * from issues,borrowers
1772 where itemnumber = ?
1773 and returndate is NULL
1774 and issues.borrowernumber = borrowers.borrowernumber");
1776 $sth2->execute($data->{'itemnumber'});
1777 if (my $data2 = $sth2->fetchrow_hashref) {
1778 $data->{'date_due'} = $data2->{'date_due'};
1779 $data->{'card'} = $data2->{'cardnumber'};
1780 $data->{'borrower'} = $data2->{'borrowernumber'};
1782 if ($data->{'wthdrawn'} eq '1') {
1783 $data->{'date_due'} = 'Cancelled';
1785 $data->{'date_due'} = 'Available';
1791 # Find the last 3 people who borrowed this item.
1792 $sth2 = $dbh->prepare("select * from issues, borrowers
1793 where itemnumber = ?
1794 and issues.borrowernumber = borrowers.borrowernumber
1795 and returndate is not NULL
1796 order by returndate desc,timestamp desc") || die $dbh->errstr;
1797 $sth2->execute($data->{'itemnumber'}) || die $sth2->errstr;
1798 for (my $i2 = 0; $i2 < 2; $i2++) { # FIXME : error if there is less than 3 pple borrowing this item
1799 if (my $data2 = $sth2->fetchrow_hashref) {
1800 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1801 $data->{"card$i2"} = $data2->{'cardnumber'};
1802 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1807 $results[$i] = $data;
1817 $item = &itemnodata($env, $dbh, $biblioitemnumber);
1819 Looks up the item with the given biblioitemnumber.
1821 C<$env> and C<$dbh> are ignored.
1823 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1824 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1830 my ($env,$dbh,$itemnumber) = @_;
1831 $dbh = C4::Context->dbh;
1832 my $sth=$dbh->prepare("Select * from biblio,items,biblioitems
1833 where items.itemnumber = ?
1834 and biblio.biblionumber = items.biblionumber
1835 and biblioitems.biblioitemnumber = items.biblioitemnumber");
1837 $sth->execute($itemnumber);
1838 my $data=$sth->fetchrow_hashref;
1845 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
1847 Looks up patrons (borrowers) by name.
1851 BUGFIX 499: C<$type> is now used to determine type of search.
1852 if $type is "simple", search is performed on the first letter of the
1855 C<$searchstring> is a space-separated list of search terms. Each term
1856 must match the beginning a borrower's surname, first name, or other
1859 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
1860 reference-to-array; each element is a reference-to-hash, whose keys
1861 are the fields of the C<borrowers> table in the Koha database.
1862 C<$count> is the number of elements in C<$borrowers>.
1866 #used by member enquiries from the intranet
1867 #called by member.pl
1869 my ($env,$searchstring,$type)=@_;
1870 my $dbh = C4::Context->dbh;
1871 my $query = ""; my $count; my @data;
1874 if($type eq "simple") # simple search for one letter only
1876 $query="Select * from borrowers where surname like ? order by surname,firstname";
1877 @bind=("$searchstring%");
1879 else # advanced search looking in surname, firstname and othernames
1881 @data=split(' ',$searchstring);
1883 $query="Select * from borrowers
1884 where ((surname like ? or surname like ?
1885 or firstname like ? or firstname like ?
1886 or othernames like ? or othernames like ?)
1888 @bind=("$data[0]%","% $data[0]%","$data[0]%","% $data[0]%","$data[0]%","% $data[0]%");
1889 for (my $i=1;$i<$count;$i++){
1890 $query=$query." and (".
1891 " surname like ? or surname like ?
1892 or firstname like ? or firstname like ?
1893 or othernames like ? or othernames like ?)";
1894 push(@bind,"$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%");
1897 $query=$query.") or cardnumber = ?
1898 order by surname,firstname";
1899 push(@bind,$searchstring);
1903 my $sth=$dbh->prepare($query);
1904 $sth->execute(@bind);
1907 while (my $data=$sth->fetchrow_hashref){
1908 push(@results,$data);
1912 return ($cnt,\@results);
1917 $borrower = &borrdata($cardnumber, $borrowernumber);
1919 Looks up information about a patron (borrower) by either card number
1920 or borrower number. If $borrowernumber is specified, C<&borrdata>
1921 searches by borrower number; otherwise, it searches by card number.
1923 C<&borrdata> returns a reference-to-hash whose keys are the fields of
1924 the C<borrowers> table in the Koha database.
1929 my ($cardnumber,$bornum)=@_;
1930 $cardnumber = uc $cardnumber;
1931 my $dbh = C4::Context->dbh;
1934 $sth=$dbh->prepare("Select * from borrowers where cardnumber=?");
1935 $sth->execute($cardnumber);
1937 $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
1938 $sth->execute($bornum);
1940 my $data=$sth->fetchrow_hashref;
1944 } else { # try with firstname
1946 my $sth=$dbh->prepare("select * from borrowers where firstname=?");
1947 $sth->execute($cardnumber);
1948 my $data=$sth->fetchrow_hashref;
1958 ($count, $issues) = &borrissues($borrowernumber);
1960 Looks up what the patron with the given borrowernumber has borrowed.
1962 C<&borrissues> returns a two-element array. C<$issues> is a
1963 reference-to-array, where each element is a reference-to-hash; the
1964 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1965 in the Koha database. C<$count> is the number of elements in
1972 my $dbh = C4::Context->dbh;
1973 my $sth=$dbh->prepare("Select * from issues,biblio,items where borrowernumber=?
1974 and items.itemnumber=issues.itemnumber
1975 and items.biblionumber=biblio.biblionumber
1976 and issues.returndate is NULL order by date_due");
1977 $sth->execute($bornum);
1979 while (my $data = $sth->fetchrow_hashref) {
1980 push @result, $data;
1983 return(scalar(@result), \@result);
1988 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1990 Looks up what the patron with the given borrowernumber has borrowed,
1991 and sorts the results.
1993 C<$sortkey> is the name of a field on which to sort the results. This
1994 should be the name of a field in the C<issues>, C<biblio>,
1995 C<biblioitems>, or C<items> table in the Koha database.
1997 C<$limit> is the maximum number of results to return.
1999 C<&allissues> returns a two-element array. C<$issues> is a
2000 reference-to-array, where each element is a reference-to-hash; the
2001 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
2002 C<items> tables of the Koha database. C<$count> is the number of
2003 elements in C<$issues>
2008 my ($bornum,$order,$limit)=@_;
2009 #FIXME: sanity-check order and limit
2010 my $dbh = C4::Context->dbh;
2011 my $query="Select * from issues,biblio,items,biblioitems
2012 where borrowernumber=? and
2013 items.biblioitemnumber=biblioitems.biblioitemnumber and
2014 items.itemnumber=issues.itemnumber and
2015 items.biblionumber=biblio.biblionumber order by $order";
2017 $query.=" limit $limit";
2020 my $sth=$dbh->prepare($query);
2021 $sth->execute($bornum);
2024 while (my $data=$sth->fetchrow_hashref){
2029 return($i,\@result);
2034 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
2036 Returns aggregate data about items borrowed by the patron with the
2037 given borrowernumber.
2041 C<&borrdata2> returns a three-element array. C<$borrowed> is the
2042 number of books the patron currently has borrowed. C<$due> is the
2043 number of overdue items the patron currently has borrowed. C<$fine> is
2044 the total fine currently due by the borrower.
2049 my ($env,$bornum)=@_;
2050 my $dbh = C4::Context->dbh;
2051 my $query="Select count(*) from issues where borrowernumber='$bornum' and
2052 returndate is NULL";
2054 my $sth=$dbh->prepare($query);
2056 my $data=$sth->fetchrow_hashref;
2058 $sth=$dbh->prepare("Select count(*) from issues where
2059 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
2061 my $data2=$sth->fetchrow_hashref;
2063 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
2064 borrowernumber='$bornum'");
2066 my $data3=$sth->fetchrow_hashref;
2069 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
2072 =item getboracctrecord
2074 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
2076 Looks up accounting data for the patron with the given borrowernumber.
2080 (FIXME - I'm not at all sure what this is about.)
2082 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
2083 reference-to-array, where each element is a reference-to-hash; the
2084 keys are the fields of the C<accountlines> table in the Koha database.
2085 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
2086 total amount outstanding for all of the account lines.
2090 sub getboracctrecord {
2091 my ($env,$params) = @_;
2092 my $dbh = C4::Context->dbh;
2095 my $sth=$dbh->prepare("Select * from accountlines where
2096 borrowernumber=? order by date desc,timestamp desc");
2098 $sth->execute($params->{'borrowernumber'});
2100 while (my $data=$sth->fetchrow_hashref){
2101 #FIXME before reinstating: insecure?
2102 # if ($data->{'itemnumber'} ne ''){
2103 # $query="Select * from items,biblio where items.itemnumber=
2104 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
2105 # my $sth2=$dbh->prepare($query);
2107 # my $data2=$sth2->fetchrow_hashref;
2111 $acctlines[$numlines] = $data;
2113 $total += $data->{'amountoutstanding'};
2116 return ($numlines,\@acctlines,$total);
2121 ($count, $lcount, $nacount, $fcount, $scount, $lostcount,
2122 $mending, $transit,$ocount) =
2123 &itemcount($env, $biblionumber, $type);
2125 Counts the number of items with the given biblionumber, broken down by
2130 If C<$type> is not set to C<intra>, lost, very overdue, and withdrawn
2131 items will not be counted.
2133 C<&itemcount> returns a nine-element list:
2135 C<$count> is the total number of items with the given biblionumber.
2137 C<$lcount> is the number of items at the Levin branch.
2139 C<$nacount> is the number of items that are neither borrowed, lost,
2140 nor withdrawn (and are therefore presumably on a shelf somewhere).
2142 C<$fcount> is the number of items at the Foxton branch.
2144 C<$scount> is the number of items at the Shannon branch.
2146 C<$lostcount> is the number of lost and very overdue items.
2148 C<$mending> is the number of items at the Mending branch (being
2151 C<$transit> is the number of items at the Transit branch (in transit
2154 C<$ocount> is the number of items that haven't arrived yet
2155 (aqorders.quantity - aqorders.quantityreceived).
2160 # FIXME - There's also a &C4::Biblio::itemcount.
2161 # Since they're all exported, acqui/acquire.pl doesn't compile with -w.
2163 my ($env,$bibnum,$type)=@_;
2164 my $dbh = C4::Context->dbh;
2165 my $query="Select * from items where
2167 if ($type ne 'intra'){
2168 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2169 (wthdrawn <> 1 or wthdrawn is NULL)";
2171 my $sth=$dbh->prepare($query);
2173 $sth->execute($bibnum);
2183 while (my $data=$sth->fetchrow_hashref){
2186 my $sth2=$dbh->prepare("select * from issues,items where issues.itemnumber=
2187 ? and returndate is NULL
2188 and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
2189 items.itemlost <> 2) or items.itemlost is NULL)
2190 and (wthdrawn <> 1 or wthdrawn is NULL)");
2191 $sth2->execute($data->{'itemnumber'});
2192 if (my $data2=$sth2->fetchrow_hashref){
2195 if ($data->{'holdingbranch'} eq 'C' || $data->{'holdingbranch'} eq 'LT'){
2198 if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){
2201 if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){
2204 if ($data->{'itemlost'} eq '1'){
2207 if ($data->{'itemlost'} eq '2'){
2210 if ($data->{'holdingbranch'} eq 'FM'){
2213 if ($data->{'holdingbranch'} eq 'TR'){
2220 my $sth2=$dbh->prepare("Select * from aqorders where biblionumber=?");
2221 $sth2->execute($bibnum);
2222 if (my $data=$sth2->fetchrow_hashref){
2223 $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
2228 return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount);
2233 $counts = &itemcount2($env, $biblionumber, $type);
2235 Counts the number of items with the given biblionumber, broken down by
2240 C<$type> may be either C<intra> or anything else. If it is not set to
2241 C<intra>, then the search will exclude lost, very overdue, and
2244 C<$&itemcount2> returns a reference-to-hash, with the following fields:
2250 The total number of items with this biblionumber.
2254 The number of items on order (aqorders.quantity -
2255 aqorders.quantityreceived).
2259 For each branch that has at least one copy of the book, C<$counts>
2260 will have a key with the branch name, giving the number of copies at
2268 my ($env,$bibnum,$type)=@_;
2269 my $dbh = C4::Context->dbh;
2270 my $query="Select * from items,branches where
2271 biblionumber=? and items.holdingbranch=branches.branchcode";
2272 if ($type ne 'intra'){
2273 $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
2274 (wthdrawn <> 1 or wthdrawn is NULL)";
2276 my $sth=$dbh->prepare($query);
2278 $sth->execute($bibnum);
2281 while (my $data=$sth->fetchrow_hashref){
2288 'select * from items
2290 and not ((items.itemlost <>1 and items.itemlost <> 2)
2291 or items.itemlost is NULL)'
2294 'select * from items
2295 where itemnumber=? and not (wthdrawn <> 1 or wthdrawn is NULL)'
2297 'On Loan', "select * from issues,items
2298 where issues.itemnumber=? and returndate is NULL
2299 and items.itemnumber=issues.itemnumber"
2302 my($testlabel, $query2) = @$test;
2304 my $sth2=$dbh->prepare($query2);
2305 $sth2->execute($data->{'itemnumber'});
2307 # FIXME - fetchrow_hashref() can fail for any number of reasons
2308 # (e.g., a database server crash). Perhaps use a left join of some
2310 $status = $testlabel if $sth2->fetchrow_hashref;
2312 last if defined $status;
2314 $status = $data->{'branchname'} unless defined $status;
2317 my $sth2=$dbh->prepare("Select * from aqorders where biblionumber=? and
2318 datecancellationprinted is NULL and quantity > quantityreceived");
2319 $sth2->execute($bibnum);
2320 if (my $data=$sth2->fetchrow_hashref){
2321 $counts{'order'}=$data->{'quantity'} - $data->{'quantityreceived'};
2330 $description = &ItemType($itemtype);
2332 Given an item type code, returns the description for that type.
2337 # FIXME - I'm pretty sure that after the initial setup, the list of
2338 # item types doesn't change very often. Hence, it seems slow and
2339 # inefficient to make yet another database call to look up information
2340 # that'll only change every few months or years.
2342 # Much better, I think, to automatically build a Perl file that can be
2343 # included in those scripts that require it, e.g.:
2344 # @itemtypes = qw( ART BCD CAS CD F ... );
2346 # ART => "Art Prints",
2347 # BCD => "CD-ROM from book",
2348 # CD => "Compact disc (WN)",
2349 # F => "Free Fiction",
2352 # The web server can then run a cron job to rebuild this file from the
2353 # database every hour or so.
2355 # The same thing goes for branches, book funds, book sellers, currency
2356 # rates, printers, stopwords, and perhaps others.
2359 my $dbh = C4::Context->dbh;
2360 my $sth=$dbh->prepare("select description from itemtypes where itemtype=?");
2361 $sth->execute($type);
2362 my $dat=$sth->fetchrow_hashref;
2364 return ($dat->{'description'});
2369 ($count, @results) = &bibitems($biblionumber);
2371 Given the biblionumber for a book, C<&bibitems> looks up that book's
2372 biblioitems (different publications of the same book, the audio book
2373 and film versions, etc.).
2375 C<$count> is the number of elements in C<@results>.
2377 C<@results> is an array of references-to-hash; the keys are the fields
2378 of the C<biblioitems> and C<itemtypes> tables of the Koha database. In
2379 addition, C<itemlost> indicates the availability of the item: if it is
2380 "2", then all copies of the item are long overdue; if it is "1", then
2381 all copies are lost; otherwise, there is at least one copy available.
2387 my $dbh = C4::Context->dbh;
2388 my $sth = $dbh->prepare("SELECT biblioitems.*,
2390 MIN(items.itemlost) as itemlost,
2391 MIN(items.dateaccessioned) as dateaccessioned
2392 FROM biblioitems, itemtypes, items
2393 WHERE biblioitems.biblionumber = ?
2394 AND biblioitems.itemtype = itemtypes.itemtype
2395 AND biblioitems.biblioitemnumber = items.biblioitemnumber
2396 GROUP BY items.biblioitemnumber");
2399 $sth->execute($bibnum);
2400 while (my $data = $sth->fetchrow_hashref) {
2401 $results[$count] = $data;
2405 return($count, @results);
2410 @barcodes = &barcodes($biblioitemnumber);
2412 Given a biblioitemnumber, looks up the corresponding items.
2414 Returns an array of references-to-hash; the keys are C<barcode> and
2417 The returned items include very overdue items, but not lost ones.
2422 #called from request.pl
2423 my ($biblioitemnumber)=@_;
2424 my $dbh = C4::Context->dbh;
2425 my $sth=$dbh->prepare("SELECT barcode, itemlost, holdingbranch FROM items
2426 WHERE biblioitemnumber = ?
2427 AND (wthdrawn <> 1 OR wthdrawn IS NULL)");
2428 $sth->execute($biblioitemnumber);
2431 while (my $data=$sth->fetchrow_hashref){
2432 $barcodes[$i]=$data;
2441 ($count, @websites) = &getwebsites($biblionumber);
2443 Looks up the web sites pertaining to the book with the given
2446 C<$count> is the number of elements in C<@websites>.
2448 C<@websites> is an array of references-to-hash; the keys are the
2449 fields from the C<websites> table in the Koha database.
2454 my ($biblionumber) = @_;
2455 my $dbh = C4::Context->dbh;
2456 my $sth = $dbh->prepare("Select * from websites where biblionumber = ?");
2460 $sth->execute($biblionumber);
2461 while (my $data = $sth->fetchrow_hashref) {
2462 # FIXME - The URL scheme shouldn't be stripped off, at least
2463 # not here, since it's part of the URL, and will be useful in
2464 # constructing a link to the site. If you don't want the user
2465 # to see the "http://" part, strip that off when building the
2467 $data->{'url'} =~ s/^http:\/\///; # FIXME - Leaning toothpick
2469 $results[$count] = $data;
2474 return($count, @results);
2477 =item getwebbiblioitems
2479 ($count, @results) = &getwebbiblioitems($biblionumber);
2481 Given a book's biblionumber, looks up the web versions of the book
2482 (biblioitems with itemtype C<WEB>).
2484 C<$count> is the number of items in C<@results>. C<@results> is an
2485 array of references-to-hash; the keys are the items from the
2486 C<biblioitems> table of the Koha database.
2490 sub getwebbiblioitems {
2491 my ($biblionumber) = @_;
2492 my $dbh = C4::Context->dbh;
2493 my $sth = $dbh->prepare("Select * from biblioitems where biblionumber = ?
2494 and itemtype = 'WEB'");
2498 $sth->execute($biblionumber);
2499 while (my $data = $sth->fetchrow_hashref) {
2500 $data->{'url'} =~ s/^http:\/\///;
2501 $results[$count] = $data;
2506 return($count, @results);
2507 } # sub getwebbiblioitems
2510 =item breedingsearch
2512 ($count, @results) = &breedingsearch($title,$isbn,$random);
2513 C<$title> contains the title,
2514 C<$isbn> contains isbn or issn,
2515 C<$random> contains the random seed from a z3950 search.
2517 C<$count> is the number of items in C<@results>. C<@results> is an
2518 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
2522 sub breedingsearch {
2523 my ($title,$isbn,$z3950random) = @_;
2524 my $dbh = C4::Context->dbh;
2530 $query = "Select id,file,isbn,title,author from marc_breeding where ";
2532 $query .= "z3950random = ?";
2533 @bind=($z3950random);
2537 $query .= "title like ?";
2538 push(@bind,"$title%");
2540 if ($title && $isbn) {
2544 $query .= "isbn like ?";
2545 push(@bind,"$isbn%");
2548 $sth = $dbh->prepare($query);
2549 $sth->execute(@bind);
2550 while (my $data = $sth->fetchrow_hashref) {
2551 $results[$count] = $data;
2556 return($count, @results);
2557 } # sub breedingsearch
2565 ($count, @results) = &isbnsearch($isbn,$title);
2567 Given an isbn and/or a title, returns the biblios having it.
2568 Used in acqui.simple, isbnsearch.pl only
2570 C<$count> is the number of items in C<@results>. C<@results> is an
2571 array of references-to-hash; the keys are the items from the
2572 C<biblioitems> table of the Koha database.
2577 my ($isbn,$title) = @_;
2578 my $dbh = C4::Context->dbh;
2584 $query = "Select distinct biblio.* from biblio, biblioitems where
2585 biblio.biblionumber = biblioitems.biblionumber";
2588 $query .= " and isbn like ?";
2589 @bind=(uc($isbn)."%");
2592 $query .= " and title like ?";
2595 $sth = $dbh->prepare($query);
2597 $sth->execute(@bind);
2598 while (my $data = $sth->fetchrow_hashref) {
2599 $results[$count] = $data;
2604 return($count, @results);
2609 $branchname = &getbranchname($branchcode);
2611 Given the branch code, the function returns the corresponding
2612 branch name for a comprehensive information display
2618 my ($branchcode) = @_;
2619 my $dbh = C4::Context->dbh;
2620 my $sth = $dbh->prepare("SELECT branchname FROM branches WHERE branchcode = ?");
2621 $sth->execute($branchcode);
2622 my $branchname = $sth->fetchrow();
2625 } # sub getbranchname
2627 =item getborrowercategory
2629 $description = &getborrowercategory($categorycode);
2631 Given the borrower's category code, the function returns the corresponding
2632 description for a comprehensive information display.
2636 sub getborrowercategory
2639 my $dbh = C4::Context->dbh;
2640 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
2641 $sth->execute($catcode);
2642 my $description = $sth->fetchrow();
2644 return $description;
2645 } # sub getborrowercategory
2648 END { } # module clean-up code here (global destructor)
2657 Koha Developement team <info@koha.org>