- $i2++;
- }
- $count=$i;
-
- } else {
- # $search->{'class'} was not specified
-
- # FIXME - This is bogus: it makes a separate query for each
- # biblioitem, and returns results in apparently random order. It'd
- # be much better to combine all of the previous queries into one big
- # one (building it up a little at a time, of course), and have that
- # big query select all of the desired fields, instead of just
- # 'biblionumber'.
-
- while ($i2 < $num && $i2 < $count){
- my $query="select * from biblio,biblioitems where
- biblio.biblionumber=? and
- biblio.biblionumber=biblioitems.biblionumber ";
- my @bind=($res[$i2+$offset]);
-
- if ($search->{'dewey'} ne ''){
- $query.= "and (dewey like ?)";
- push(@bind,"$search->{'dewey'}%");
- }
-
- my $sth=$dbh->prepare($query);
-# print $query;
- $sth->execute(@bind);
- if (my $data2=$sth->fetchrow_hashref){
- my $dewey= $data2->{'dewey'};
- my $subclass=$data2->{'subclass'};
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass") ;
- $sth->finish;
- $data2->{'dewey'}=$dewey;
-
- $res2[$i]=$data2;
-# $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
- $i++;
- }
- $i2++;
-
- }
- }
-
- #$count=$i;
- return($count,@res2);
-}
-
-sub KeywordSearch2 {
- my ($env,$type,$search,$num,$offset)=@_;
- my $dbh = C4::Context->dbh;
- $search->{'keyword'}=~ s/ +$//;
- my @key=split(' ',$search->{'keyword'});
- my $count=@key;
- my $i=1;
- my @results;
- my $query ="Select * from biblio,bibliosubtitle,biblioitems where
- biblio.biblionumber=biblioitems.biblionumber and
- biblio.biblionumber=bibliosubtitle.biblionumber and
- (((title like ? or title like ?)";
- my @bind=("$key[0]%","% $key[0]%");
- while ($i < $count){
- $query .= " and (title like ? or title like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- $i++;
- }
- $query.= ") or ((subtitle like ? or subtitle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.= " and (subtitle like ? or subtitle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query.= ") or ((seriestitle like ? or seriestitle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (seriestitle like ? or seriestitle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query.= ") or ((biblio.notes like ? or biblio.notes like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (biblio.notes like ? or biblio.notes like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query.= ") or ((biblioitems.notes like ? or biblioitems.notes like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (biblioitems.notes like ? or biblioitems.notes like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- if ($search->{'keyword'} =~ /new zealand/i){
- $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
- or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %'
- or author like '% nz %' or author like '% nz')"
- }
- if ($search->{'keyword'} eq 'nz' || $search->{'keyword'} eq 'NZ' ||
- $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
- $search->{'keyword'} =~ / nz/i){
- $query.= "or (title like 'new zealand%' or title like '% new zealand %'
- or title like '% new zealand' or subtitle like 'new zealand%' or
- subtitle like '% new zealand %'
- or subtitle like '% new zealand' or author like 'new zealand%'
- or author like '% new zealand %' or author like '% new zealand' or
- seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
- or seriestitle like '% new zealand')"
- }
- $query .= "))";
- if ($search->{'class'} ne ''){
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= "and ( itemtype=?";
- push(@bind,"$temp[0]");
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype=?";
- push(@bind,"$temp[$i]");
- }
- $query.=")";
- }
- if ($search->{'dewey'} ne ''){
- $query.= "and (dewey like '$search->{'dewey'}%') ";
- }
- $query.="group by biblio.biblionumber";
- #$query.=" order by author,title";
-# print $query;
- my $sth=$dbh->prepare($query);
- $sth->execute(@bind);
- $i=0;
- while (my $data=$sth->fetchrow_hashref){
-#FIXME: rewrite to use ? before uncomment
-# my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
-# ");
-# $sti->execute;
-# my ($dewey, $subclass) = $sti->fetchrow;
- my $dewey=$data->{'dewey'};
- my $subclass=$data->{'subclass'};
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass");
-# $sti->finish;
- $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
-# print $results[$i];
- $i++;
- }
- $sth->finish;
- $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
- like ? group by biblionumber");
- $sth->execute("%".$search->{'keyword'}."%");
- while (my $data=$sth->fetchrow_hashref){
- $query="Select * from biblio,biblioitems where
- biblio.biblionumber=? and
- biblio.biblionumber=biblioitems.biblionumber ";
- @bind=($data->{'biblionumber'});
- if ($search->{'class'} ne ''){
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= " and ( itemtype=?";
- push(@bind,$temp[0]);
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype=?";
- push(@bind,$temp[$i]);
- }
- $query.=")";
-
- }
- if ($search->{'dewey'} ne ''){
- $query.= "and (dewey like ?)";
- push(@bind,"$search->{'dewey'}%");
- }
- my $sth2=$dbh->prepare($query);
- $sth2->execute(@bind);
-# print $query;
- while (my $data2=$sth2->fetchrow_hashref){
- my $dewey= $data2->{'dewey'};
- my $subclass=$data2->{'subclass'};
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass") ;
-# $sti->finish;
- $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
-# print $results[$i];
- $i++;
- }
- $sth2->finish;
- }
- my $i2=1;
- @results=sort @results;
- my @res;
- $count=@results;
- $i=1;
- if ($count > 0){
- $res[0]=$results[0];
- }
- while ($i2 < $count){
- if ($results[$i2] ne $res[$i-1]){
- $res[$i]=$results[$i2];
- $i++;
- }
- $i2++;
- }
- $i2=0;
- my @res2;
- $count=@res;
- while ($i2 < $num && $i2 < $count){
- $res2[$i2]=$res[$i2+$offset];
-# print $res2[$i2];
- $i2++;
- }
- $sth->finish;
-# $i--;
-# $i++;
- return($i,@res2);
-}
-
-=item CatSearch
-
- ($count, @results) = &CatSearch($env, $type, $search, $num, $offset);
-
-C<&CatSearch> searches the Koha catalog. It returns a list whose first
-element is the number of returned results, and whose subsequent
-elements are the results themselves.
-
-Each returned element is a reference-to-hash. Most of the keys are
-simply the fields from the C<biblio> table in the Koha database, but
-the following keys may also be present:
-
-=over 4
-
-=item C<illustrator>
-
-The book's illustrator.
-
-=item C<publisher>
-
-The publisher.
-
-=back
-
-C<$env> is ignored.
-
-C<$type> may be C<subject>, C<loose>, or C<precise>. This controls the
-high-level behavior of C<&CatSearch>, as described below.
-
-In many cases, the description below says that a certain field in the
-database must match the search string. In these cases, it means that
-the beginning of some word in the field must match the search string.
-Thus, an author search for "sm" will return books whose author is
-"John Smith" or "Mike Smalls", but not "Paul Grossman", since the "sm"
-does not occur at the beginning of a word.
-
-Note that within each search mode, the criteria are and-ed together.
-That is, if you perform a loose search on the author "Jerome" and the
-title "Boat", the search will only return books by Jerome containing
-"Boat" in the title.
-
-It is not possible to cross modes, e.g., set the author to "Asimov"
-and the subject to "Math" in hopes of finding books on math by Asimov.
-
-=head2 Loose search
-
-If C<$type> is set to C<loose>, the following search criteria may be
-used:
-
-=over 4
-
-=item C<$search-E<gt>{author}>
-
-The search string is a space-separated list of words. Each word must
-match either the C<author> or C<additionalauthors> field.
-
-=item C<$search-E<gt>{title}>
-
-Each word in the search string must match the book title. If no author
-is specified, the book subtitle will also be searched.
-
-=item C<$search-E<gt>{abstract}>
-
-Searches for the given search string in the book's abstract.
-
-=item C<$search-E<gt>{'date-before'}>
-
-Searches for books whose copyright date matches the search string.
-That is, setting C<$search-E<gt>{'date-before'}> to "1985" will find
-books written in 1985, and setting it to "198" will find books written
-between 1980 and 1989.
-
-=item C<$search-E<gt>{title}>
-
-Searches by title are also affected by the value of
-C<$search-E<gt>{"ttype"}>; if it is set to C<exact>, then the book
-title, (one of) the series titleZ<>(s), or (one of) the unititleZ<>(s) must
-match the search string exactly (the subtitle is not searched).
-
-If C<$search-E<gt>{"ttype"}> is set to anything other than C<exact>,
-each word in the search string must match the title, subtitle,
-unititle, or series title.
-
-=item C<$search-E<gt>{class}>
-
-Restricts the search to certain item classes. The value of
-C<$search-E<gt>{"class"}> is a | (pipe)-separated list of item types.
-Thus, setting it to "F" restricts the search to fiction, and setting
-it to "CD|CAS" will only look in compact disks and cassettes.
-
-=item C<$search-E<gt>{dewey}>
-
-Searches for books whose Dewey Decimal Classification code matches the
-search string. That is, setting C<$search-E<gt>{"dewey"}> to "5" will
-search for all books in 5I<xx> (Science and mathematics), setting it
-to "54" will search for all books in 54I<x> (Chemistry), and setting
-it to "546" will search for books on inorganic chemistry.
-
-=item C<$search-E<gt>{publisher}>
-
-Searches for books whose publisher contains the search string (unlike
-other search criteria, C<$search-E<gt>{publisher}> is a string, not a
-set of words.
-
-=back
-
-=head2 Subject search
-
-If C<$type> is set to C<subject>, the following search criterion may
-be used:
-
-=over 4
-
-=item C<$search-E<gt>{subject}>
-
-The search string is a space-separated list of words, each of which
-must match the book's subject.
-
-Special case: if C<$search-E<gt>{subject}> is set to C<nz>,
-C<&CatSearch> will search for books whose subject is "New Zealand".
-However, setting C<$search-E<gt>{subject}> to C<"nz football"> will
-search for books on "nz" and "football", not books on "New Zealand"
-and "football".
-
-=back
-
-=head2 Precise search
-
-If C<$type> is set to C<precise>, the following search criteria may be
-used:
-
-=over 4
-
-=item C<$search-E<gt>{item}>
-
-Searches for books whose barcode exactly matches the search string.
-
-=item C<$search-E<gt>{isbn}>
-
-Searches for books whose ISBN exactly matches the search string.
-
-=back
-
-For a loose search, if an author was specified, the results are
-ordered by author and title. If no author was specified, the results
-are ordered by title.
-
-For other (non-loose) searches, if a subject was specified, the
-results are ordered alphabetically by subject.
-
-In all other cases (e.g., loose search by keyword), the results are
-not ordered.
-
-=cut
-#'
-sub CatSearch {
- my ($env,$type,$search,$num,$offset)=@_;
- my $dbh = C4::Context->dbh;
- my $query = '';
- my @bind = ();
- my @results;
-
- my $title = lc($search->{'title'});
-
- if ($type eq 'loose') {
- if ($search->{'author'} ne ''){
- my @key=split(' ',$search->{'author'});
- my $count=@key;
- my $i=1;
- $query="select *,biblio.author,biblio.biblionumber from
- biblio
- left join additionalauthors
- on additionalauthors.biblionumber =biblio.biblionumber
- where
- ((biblio.author like ? or biblio.author like ? or
- additionalauthors.author like ? or additionalauthors.author
- like ?
- )";
- @bind=("$key[0]%","% $key[0]%","$key[0]%","% $key[0]%");
- while ($i < $count){
- $query .= " and (
- biblio.author like ? or biblio.author like ? or
- additionalauthors.author like ? or additionalauthors.author like ?
- )";
- push(@bind,"$key[$i]%","% $key[$i]%","$key[$i]%","% $key[$i]%");
- $i++;
- }
- $query .= ")";
- if ($search->{'title'} ne ''){
- my @key=split(' ',$search->{'title'});
- my $count=@key;
- my $i=0;
- $query.= " and (((title like ? or title like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- while ($i<$count){
- $query .= " and (title like ? or title like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- $i++;
- }
- $query.=") or ((seriestitle like ? or seriestitle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (seriestitle like ? or seriestitle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query.=") or ((unititle like ? or unititle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (unititle like ? or unititle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query .= "))";
- }
- if ($search->{'abstract'} ne ''){
- $query.= " and (abstract like ?)";
- push(@bind,"%$search->{'abstract'}%");
- }
- if ($search->{'date-before'} ne ''){
- $query.= " and (copyrightdate like ?)";
- push(@bind,"%$search->{'date-before'}%");
- }
- $query.=" group by biblio.biblionumber";
- } else {
- if ($search->{'title'} ne '') {
- if ($search->{'ttype'} eq 'exact'){
- $query="select * from biblio
- where
- (biblio.title=? or (biblio.unititle = ?
- or biblio.unititle like ? or
- biblio.unititle like ? or
- biblio.unititle like ?) or
- (biblio.seriestitle = ? or
- biblio.seriestitle like ? or
- biblio.seriestitle like ? or
- biblio.seriestitle like ?)
- )";
- @bind=($search->{'title'},$search->{'title'},"$search->{'title'} |%","%| $search->{'title'} |%","%| $search->{'title'}",$search->{'title'},"$search->{'title'} |%","%| $search->{'title'} |%","%| $search->{'title'}");
- } else {
- my @key=split(' ',$search->{'title'});
- my $count=@key;
- my $i=1;
- $query="select biblio.biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp,subtitle from biblio
- left join bibliosubtitle on
- biblio.biblionumber=bibliosubtitle.biblionumber
- where
- (((title like ? or title like ?)";
- @bind=("$key[0]%","% $key[0]%");
- while ($i<$count){
- $query .= " and (title like ? or title like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- $i++;
- }
- $query.=") or ((subtitle like ? or subtitle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (subtitle like ? or subtitle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query.=") or ((seriestitle like ? or seriestitle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (seriestitle like ? or seriestitle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query.=") or ((unititle like ? or unititle like ?)";
- push(@bind,"$key[0]%","% $key[0]%");
- for ($i=1;$i<$count;$i++){
- $query.=" and (unititle like ? or unititle like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%");
- }
- $query .= "))";
- }
- if ($search->{'abstract'} ne ''){
- $query.= " and (abstract like ?)";
- push(@bind,"%$search->{'abstract'}%");
- }
- if ($search->{'date-before'} ne ''){
- $query.= " and (copyrightdate like ?)";
- push(@bind,"%$search->{'date-before'}%");
- }
- } elsif ($search->{'class'} ne ''){
- $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= " and ( itemtype= ?)";
- @bind=($temp[0]);
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype=?";
- push(@bind,$temp[$i]);
- }
- $query.=")";
- if ($search->{'illustrator'} ne ''){
- $query.=" and illus like ?";
- push(@bind,"%".$search->{'illustrator'}."%");
- }
- if ($search->{'dewey'} ne ''){
- $query.=" and biblioitems.dewey like ?";
- push(@bind,"$search->{'dewey'}%");
- }
- } elsif ($search->{'dewey'} ne ''){
- $query="select * from biblioitems,biblio
- where biblio.biblionumber=biblioitems.biblionumber
- and biblioitems.dewey like ?";
- @bind=("$search->{'dewey'}%");
- } elsif ($search->{'illustrator'} ne '') {
- $query="select * from biblioitems,biblio
- where biblio.biblionumber=biblioitems.biblionumber
- and biblioitems.illus like ?";
- @bind=("%".$search->{'illustrator'}."%");
- } elsif ($search->{'publisher'} ne ''){
- $query = "Select * from biblio,biblioitems where biblio.biblionumber
- =biblioitems.biblionumber and (publishercode like ?)";
- @bind=("%$search->{'publisher'}%");
- } elsif ($search->{'abstract'} ne ''){
- $query = "Select * from biblio where abstract like ?";
- @bind=("%$search->{'abstract'}%");
- } elsif ($search->{'date-before'} ne ''){
- $query = "Select * from biblio where copyrightdate like ?";
- @bind=("%$search->{'date-before'}%");
- }
- $query .=" group by biblio.biblionumber";
- }
- }
- if ($type eq 'subject'){
- my @key=split(' ',$search->{'subject'});
- my $count=@key;
- my $i=1;
- $query="select * from bibliosubject, biblioitems where
-(bibliosubject.biblionumber = biblioitems.biblionumber) and ( subject like ? or subject like ? or subject like ?)";
- @bind=("$key[0]%","% $key[0]%","%($key[0])%");
- while ($i<$count){
- $query.=" and (subject like ? or subject like ? or subject like ?)";
- push(@bind,"$key[$i]%","% $key[$i]%","%($key[$i])%");
- $i++;
- }
-
- # FIXME - Wouldn't it be better to fix the database so that if a
- # book has a subject "NZ", then it also gets added the subject
- # "New Zealand"?
- # This can also be generalized by adding a table of subject
- # synonyms to the database: just declare "NZ" to be a synonym for
- # "New Zealand", "SF" a synonym for both "Science fiction" and
- # "Fantastic fiction", etc.
-
- if (lc($search->{'subject'}) eq 'nz'){
- $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
- or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
- } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
- $query=~ s/ nz/ NEW ZEALAND/ig;
- $query=~ s/nz /NEW ZEALAND /ig;
- $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
- }
- }
- if ($type eq 'precise'){
- if ($search->{'itemnumber'} ne ''){
- $query="select * from items,biblio ";
- my $search2=uc $search->{'itemnumber'};
- $query=$query." where
- items.biblionumber=biblio.biblionumber
- and barcode=?";
- @bind=($search2);
- # FIXME - .= <<EOT;
- }
- if ($search->{'isbn'} ne ''){
- my $search2=uc $search->{'isbn'};
- my $sth1=$dbh->prepare("select * from biblioitems where isbn=?");
- $sth1->execute($search2);
- my $i2=0;
- while (my $data=$sth1->fetchrow_hashref) {
- my $sth=$dbh->prepare("select * from biblioitems,biblio where
- biblio.biblionumber = ?
- and biblioitems.biblionumber = biblio.biblionumber");
- $sth->execute($data->{'biblionumber'});
- # FIXME - There's already a $data in this scope.
- my $data=$sth->fetchrow_hashref;
- my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
- # FIXME - The following assumes that the Dewey code is a
- # floating-point number. It isn't: it's a string.
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass");
- $data->{'dewey'}=$dewey;
- $results[$i2]=$data;
- # $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
- $i2++;
- $sth->finish;
- }
- $sth1->finish;
- }
- }
- if ($type ne 'precise' && $type ne 'subject'){
- if ($search->{'author'} ne ''){
- $query .= " order by biblio.author,title";
- } else {
- $query .= " order by title";
- }
- } else {
- if ($type eq 'subject'){
- $query .= " group by subject ";
- }
- }
- my $sth=$dbh->prepare($query);
- $sth->execute(@bind);
- my $count=1;
- my $i=0;
- my $limit= $num+$offset;
- while (my $data=$sth->fetchrow_hashref){
- my $query="select dewey,subclass,publishercode from biblioitems where biblionumber=?";
- my @bind=($data->{'biblionumber'});
- if ($search->{'class'} ne ''){
- my @temp=split(/\|/,$search->{'class'});
- my $count=@temp;
- $query.= " and ( itemtype= ?";
- push(@bind,$temp[0]);
- for (my $i=1;$i<$count;$i++){
- $query.=" or itemtype=?";
- push(@bind,$temp[$i]);
- }
- $query.=")";
- }
- if ($search->{'dewey'} ne ''){
- $query.=" and dewey=? ";
- push(@bind,$search->{'dewey'});
- }
- if ($search->{'illustrator'} ne ''){
- $query.=" and illus like ?";
- push(@bind,"%$search->{'illustrator'}%");
- }
- if ($search->{'publisher'} ne ''){
- $query.= " and (publishercode like ?)";
- push(@bind,"%$search->{'publisher'}%");
- }
- my $sti=$dbh->prepare($query);
- $sti->execute(@bind);
- my $dewey;
- my $subclass;
- my $true=0;
- my $publishercode;
- my $bibitemdata;
- if ($bibitemdata = $sti->fetchrow_hashref()){
- $true=1;
- $dewey=$bibitemdata->{'dewey'};
- $subclass=$bibitemdata->{'subclass'};
- $publishercode=$bibitemdata->{'publishercode'};
- }
- # print STDERR "$dewey $subclass $publishercode\n";
- # FIXME - The Dewey code is a string, not a number.
- $dewey=~s/\.*0*$//;
- ($dewey == 0) && ($dewey='');
- ($dewey) && ($dewey.=" $subclass");
- $data->{'dewey'}=$dewey;
- $data->{'publishercode'}=$publishercode;
- $sti->finish;
- if ($true == 1){
- if ($count > $offset && $count <= $limit){
- $results[$i]=$data;
- $i++;
- }
- $count++;
- }
- }
- $sth->finish;
- $count--;
- return($count,@results);
-}
-
-sub updatesearchstats{
- my ($dbh,$query)=@_;
-
-}
-
-=item subsearch
-
- @results = &subsearch($env, $subject);
-
-Searches for books that have a subject that exactly matches
-C<$subject>.
-
-C<&subsearch> returns an array of results. Each element of this array
-is a string, containing the book's title, author, and biblionumber,
-separated by tabs.
-
-C<$env> is ignored.
-
-=cut
-#'
-sub subsearch {
- my ($env,$subject)=@_;
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("Select * from biblio,bibliosubject where
- biblio.biblionumber=bibliosubject.biblionumber and
- bibliosubject.subject=? group by biblio.biblionumber
- order by biblio.title");
- $sth->execute($subject);
- my $i=0;
- my @results;
- while (my $data=$sth->fetchrow_hashref){
- push @results, $data;
- $i++;
- }
- $sth->finish;
- return(@results);
-}
-
-=item ItemInfo
-
- @results = &ItemInfo($env, $biblionumber, $type);
-
-Returns information about books with the given biblionumber.
-
-C<$type> may be either C<intra> or anything else. If it is not set to
-C<intra>, then the search will exclude lost, very overdue, and
-withdrawn items.
-
-C<$env> is ignored.
-
-C<&ItemInfo> returns a list of references-to-hash. Each element
-contains a number of keys. Most of them are table items from the
-C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
-Koha database. Other keys include:
-
-=over 4
-
-=item C<$data-E<gt>{branchname}>
-
-The name (not the code) of the branch to which the book belongs.
-
-=item C<$data-E<gt>{datelastseen}>
-
-This is simply C<items.datelastseen>, except that while the date is
-stored in YYYY-MM-DD format in the database, here it is converted to
-DD/MM/YYYY format. A NULL date is returned as C<//>.
-
-=item C<$data-E<gt>{datedue}>
-
-=item C<$data-E<gt>{class}>
-
-This is the concatenation of C<biblioitems.classification>, the book's
-Dewey code, and C<biblioitems.subclass>.
-
-=item C<$data-E<gt>{ocount}>
-
-I think this is the number of copies of the book available.
-
-=item C<$data-E<gt>{order}>
-
-If this is set, it is set to C<One Order>.
-
-=back
-
-=cut
-#'
-sub ItemInfo {
- my ($env,$biblionumber,$type) = @_;
- my $dbh = C4::Context->dbh;
- my $query = "SELECT *,items.notforloan as itemnotforloan FROM items, biblio, biblioitems left join itemtypes on biblioitems.itemtype = itemtypes.itemtype
- WHERE items.biblionumber = ?
- AND biblioitems.biblioitemnumber = items.biblioitemnumber
- AND biblio.biblionumber = items.biblionumber";
- if ($type ne 'intra'){
- $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
- or items.itemlost is NULL)
- and (wthdrawn <> 1 or wthdrawn is NULL)";
- }
- $query .= " order by items.dateaccessioned desc";
- #warn $query;
- my $sth=$dbh->prepare($query);
- $sth->execute($biblionumber);
- my $i=0;
- my @results;
- while (my $data=$sth->fetchrow_hashref){
- my $datedue = '';
- my $isth=$dbh->prepare("Select * from issues where itemnumber = ? and returndate is null");
- $isth->execute($data->{'itemnumber'});
- if (my $idata=$isth->fetchrow_hashref){
- $datedue = format_date($idata->{'date_due'});
- }
- if ($data->{'itemlost'} eq '2'){
- $datedue='Very Overdue';
- }
- if ($data->{'itemlost'} eq '1'){
- $datedue='Lost';
- }
- if ($data->{'wthdrawn'} eq '1'){
- $datedue="Cancelled";
- }
- if ($datedue eq ''){
-# $datedue="Available";
- my ($restype,$reserves)=C4::Reserves2::CheckReserves($data->{'itemnumber'});
- if ($restype){
- $datedue=$restype;
- }
- }
- $isth->finish;
-#get branch information.....
- my $bsth=$dbh->prepare("SELECT * FROM branches
- WHERE branchcode = ?");
- $bsth->execute($data->{'holdingbranch'});
- if (my $bdata=$bsth->fetchrow_hashref){
- $data->{'branchname'} = $bdata->{'branchname'};
- }
-
- my $class = $data->{'classification'};# FIXME : $class is useless
- my $dewey = $data->{'dewey'};
- $dewey =~ s/0+$//;
- if ($dewey eq "000.") { $dewey = "";}; # FIXME - "000" is general books about computer science
- if ($dewey < 10){$dewey='00'.$dewey;}
- if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
- if ($dewey <= 0){
- $dewey='';
- }
- $dewey=~ s/\.$//;
- $class .= $dewey;
- if ($dewey ne ''){
- $class .= $data->{'subclass'};
- }
- # $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
- # FIXME - If $data->{'datelastseen'} is NULL, perhaps it'd be prettier
- # to leave it empty, rather than convert it to "//".
- # Also ideally this should use the local format for displaying dates.
- my $date=format_date($data->{'datelastseen'});
- $data->{'datelastseen'}=$date;
- $data->{'datedue'}=$datedue;
- $data->{'class'}=$class;
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- #FIXME: ordering/indentation here looks wrong
- my $sth2=$dbh->prepare("Select * from aqorders where biblionumber=?");
- $sth2->execute($biblionumber);
- my $data;
- my $ocount;
- if ($data=$sth2->fetchrow_hashref){
- $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
- if ($ocount > 0){
- $data->{'ocount'}=$ocount;
- $data->{'order'}="One Order";
- $results[$i]=$data;
- }
- }
- $sth2->finish;
-
- return(@results);
-}
-
-=item GetItems
-
- @results = &GetItems($env, $biblionumber);
-
-Returns information about books with the given biblionumber.
-
-C<$env> is ignored.
-
-C<&GetItems> returns an array of strings. Each element is a
-tab-separated list of values: biblioitemnumber, itemtype,
-classification, Dewey number, subclass, ISBN, volume, number, and
-itemdata.
-
-Itemdata, in turn, is a string of the form
-"I<barcode>C<[>I<holdingbranch>C<[>I<flags>" where I<flags> contains
-the string C<NFL> if the item is not for loan, and C<LOST> if the item
-is lost.
-
-=cut
-#'
-sub GetItems {
- my ($env,$biblionumber)=@_;
- #debug_msg($env,"GetItems");
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("Select * from biblioitems where (biblionumber = ?)");
- $sth->execute($biblionumber);
- #debug_msg($env,"executed query");
- my $i=0;
- my @results;
- while (my $data=$sth->fetchrow_hashref) {
- #debug_msg($env,$data->{'biblioitemnumber'});
- my $dewey = $data->{'dewey'};
- $dewey =~ s/0+$//;
- my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
- $line .= "\t$data->{'classification'}\t$dewey";
- $line .= "\t$data->{'subclass'}\t$data->{isbn}";
- $line .= "\t$data->{'volume'}\t$data->{number}";
- my $isth= $dbh->prepare("select * from items where biblioitemnumber = ?");
- $isth->execute($data->{'biblioitemnumber'});
- while (my $idata = $isth->fetchrow_hashref) {
- my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
- if ($idata->{'notforloan'} == 1) {
- $iline .= "NFL ";
- }
- if ($idata->{'itemlost'} == 1) {
- $iline .= "LOST ";
- }
- $line .= "\t$iline";
- }
- $isth->finish;
- $results[$i] = $line;
- $i++;
- }
- $sth->finish;
- return(@results);
-}
-
-=item itemdata
-
- $item = &itemdata($barcode);
-
-Looks up the item with the given barcode, and returns a
-reference-to-hash containing information about that item. The keys of
-the hash are the fields from the C<items> and C<biblioitems> tables in
-the Koha database.
-
-=cut
-#'
-sub itemdata {
- my ($barcode)=@_;
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("Select * from items,biblioitems where barcode=?
- and items.biblioitemnumber=biblioitems.biblioitemnumber");
- $sth->execute($barcode);
- my $data=$sth->fetchrow_hashref;
- $sth->finish;
- return($data);
-}
-
-=item bibdata
-
- $data = &bibdata($biblionumber, $type);
-
-Returns information about the book with the given biblionumber.
-
-C<$type> is ignored.
-
-C<&bibdata> returns a reference-to-hash. The keys are the fields in
-the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
-Koha database.
-
-In addition, C<$data-E<gt>{subject}> is the list of the book's
-subjects, separated by C<" , "> (space, comma, space).
-
-If there are multiple biblioitems with the given biblionumber, only
-the first one is considered.
-
-=cut
-#'
-sub bibdata {
- my ($bibnum, $type) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("Select *, biblioitems.notes AS bnotes, biblio.notes
- from biblio, biblioitems
- left join bibliosubtitle on
- biblio.biblionumber = bibliosubtitle.biblionumber
- where biblio.biblionumber = ?
- and biblioitems.biblionumber = biblio.biblionumber");
- $sth->execute($bibnum);
- my $data;
- $data = $sth->fetchrow_hashref;
- $sth->finish;
- $sth = $dbh->prepare("Select * from bibliosubject where biblionumber = ?");
- $sth->execute($bibnum);
- while (my $dat = $sth->fetchrow_hashref){
- $data->{'subject'} .= "$dat->{'subject'}, ";
- } # while
- chop $data->{'subject'};
- chop $data->{'subject'};
- $sth->finish;
- $sth = $dbh->prepare("Select * from additionalauthors where biblionumber = ?");
- $sth->execute($bibnum);
- while (my $dat = $sth->fetchrow_hashref){
- $data->{'additionalauthors'} .= "$dat->{'author'}, ";
- } # while
- chop $data->{'additionalauthors'};
- chop $data->{'additionalauthors'};
- $sth->finish;
- return($data);
-} # sub bibdata
-
-=item bibitemdata
-
- $itemdata = &bibitemdata($biblioitemnumber);
-
-Looks up the biblioitem with the given biblioitemnumber. Returns a
-reference-to-hash. The keys are the fields from the C<biblio>,
-C<biblioitems>, and C<itemtypes> tables in the Koha database, except
-that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
-
-=cut
-#'
-sub bibitemdata {
- my ($bibitem) = @_;
- my $dbh = C4::Context->dbh;
- 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");
- my $data;
-
- $sth->execute($bibitem);
-
- $data = $sth->fetchrow_hashref;
-
- $sth->finish;
- return($data);
-} # sub bibitemdata
-
-=item subject
-
- ($count, $subjects) = &subject($biblionumber);
-
-Looks up the subjects of the book with the given biblionumber. Returns
-a two-element list. C<$subjects> is a reference-to-array, where each
-element is a subject of the book, and C<$count> is the number of
-elements in C<$subjects>.
-
-=cut
-#'
-sub subject {
- my ($bibnum)=@_;
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("Select * from bibliosubject where biblionumber=?");
- $sth->execute($bibnum);
- my @results;
- my $i=0;
- while (my $data=$sth->fetchrow_hashref){
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- return($i,\@results);
-}
-
-=item addauthor
-
- ($count, $authors) = &addauthors($biblionumber);
-
-Looks up the additional authors for the book with the given
-biblionumber.
-
-Returns a two-element list. C<$authors> is a reference-to-array, where
-each element is an additional author, and C<$count> is the number of
-elements in C<$authors>.
-
-=cut
-#'
-sub addauthor {
- my ($bibnum)=@_;
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("Select * from additionalauthors where biblionumber=?");
- $sth->execute($bibnum);
- my @results;
- my $i=0;
- while (my $data=$sth->fetchrow_hashref){
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- return($i,\@results);
-}
-
-=item subtitle
-
- ($count, $subtitles) = &subtitle($biblionumber);
-
-Looks up the subtitles for the book with the given biblionumber.
-
-Returns a two-element list. C<$subtitles> is a reference-to-array,
-where each element is a subtitle, and C<$count> is the number of
-elements in C<$subtitles>.
-
-=cut
-#'
-sub subtitle {
- my ($bibnum)=@_;
- my $dbh = C4::Context->dbh;
- my $sth=$dbh->prepare("Select * from bibliosubtitle where biblionumber=?");
- $sth->execute($bibnum);
- my @results;
- my $i=0;
- while (my $data=$sth->fetchrow_hashref){
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- return($i,\@results);
-}
-
-=item itemissues
-
- @issues = &itemissues($biblioitemnumber, $biblio);
-
-Looks up information about who has borrowed the bookZ<>(s) with the
-given biblioitemnumber.
-
-C<$biblio> is ignored.
-
-C<&itemissues> returns an array of references-to-hash. The keys
-include the fields from the C<items> table in the Koha database.
-Additional keys include:
-
-=over 4
-
-=item C<date_due>
-
-If the item is currently on loan, this gives the due date.
-
-If the item is not on loan, then this is either "Available" or
-"Cancelled", if the item has been withdrawn.
-
-=item C<card>
-
-If the item is currently on loan, this gives the card number of the
-patron who currently has the item.
-
-=item C<timestamp0>, C<timestamp1>, C<timestamp2>
-
-These give the timestamp for the last three times the item was
-borrowed.
-
-=item C<card0>, C<card1>, C<card2>
-
-The card number of the last three patrons who borrowed this item.
-
-=item C<borrower0>, C<borrower1>, C<borrower2>
-
-The borrower number of the last three patrons who borrowed this item.
-
-=back
-
-=cut
-#'
-sub itemissues {
- my ($bibitem, $biblio)=@_;
- my $dbh = C4::Context->dbh;
- # FIXME - If this function die()s, the script will abort, and the
- # user won't get anything; depending on how far the script has
- # gotten, the user might get a blank page. It would be much better
- # to at least print an error message. The easiest way to do this
- # is to set $SIG{__DIE__}.
- my $sth = $dbh->prepare("Select * from items where
-items.biblioitemnumber = ?")
- || die $dbh->errstr;
- my $i = 0;