search on MARC datas.
[koha.git] / C4 / SearchMarc.pm
1 package C4::SearchMarc;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 require Exporter;
22 use DBI;
23 use C4::Context;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 # set the version for version checking
28 $VERSION = 0.02;
29
30 =head1 NAME
31
32 C4::Search - Functions for searching the Koha MARC catalog
33
34 =head1 SYNOPSIS
35
36   use C4::Search;
37
38   my ($count, @results) = catalogsearch();
39
40 =head1 DESCRIPTION
41
42 This module provides the searching facilities for the Koha MARC catalog
43
44 C<&catalogsearch> is a front end to all the other searches. Depending
45 on what is passed to it, it calls the appropriate search function.
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 @ISA = qw(Exporter);
54 @EXPORT = qw(&catalogsearch);
55 # make all your functions, whether exported or not;
56
57 # marcsearch : search in the MARC biblio table.
58 # everything is choosen by the user : what to search, the conditions...
59 # FIXME this is a 1st version of the Marc Search. It does not use fulltext searching or marc_words table
60
61 sub catalogsearch {
62         my ($dbh, $tags, $subfields, $and_or, $excluding, $operator, $value, $offset,$length) = @_;
63         # build the sql request. She will look like :
64         # select m1.bibid
65         #               from marc_subfield_table as m1, marc_subfield_table as m2
66         #               where m1.bibid=m2.bibid and
67         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
68
69         my $sql_tables; # will contain marc_subfield_table as m1,...
70         my $sql_where1; # will contain the "true" where
71         my $sql_where2; # will contain m1.bibid=m2.bibid
72         my $nb=1;
73         for(my $i=0; $i<=@$tags;$i++) {
74                 if (@$tags[$i] && @$value[$i]) {
75                         $sql_tables .= "marc_subfield_table as m$nb,";
76                         if ($nb==1) {
77                                 if (@$operator[$i] eq "starts") {
78                                         $sql_where1 .= "@$excluding[$i](m1.subfieldvalue like '@$value[$i] %' and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]')";
79                                 } elsif (@$operator[$i] eq "contains") {
80                                         $sql_where1 .= "@$excluding[$i](m1.subfieldvalue like '%@$value[$i]%' and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]')";
81                                 } else {
82                                         $sql_where1 .= "@$excluding[$i](m1.subfieldvalue @$operator[$i] '@$value[$i]' and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]')";
83                                 }
84                         } else {
85                                 if (@$operator[$i] eq "starts") {
86                                         $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb.subfieldvalue like '@$value[$i]%' and m$nb.tag=@$tags[$i] and m$nb.subfieldcode='@$subfields[$i]')";
87                                         $sql_where2 .= "m1.bibid=m$nb.bibid";
88                                 } elsif (@$operator[$i] eq "contains") {
89                                         $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb.subfieldvalue like '%@$value[$i]%' and m$nb.tag=@$tags[$i] and m$nb.subfieldcode='@$subfields[$i]')";
90                                         $sql_where2 .= "m1.bibid=m$nb.bibid";
91                                 } else {
92                                         $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb.subfieldvalue @$operator[$i] '@$value[$i]' and m$nb.tag=@$tags[$i] and m$nb.subfieldcode='@$subfields[$i]')";
93                                         $sql_where2 .= "m1.bibid=m$nb.bibid";
94                                 }
95                         }
96                         $nb++;
97                 }
98         }
99         chop $sql_tables;
100         warn "select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)";
101         my $sth;
102         if ($sql_where2) {
103                 $sth = $dbh->prepare("select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
104         } else {
105                 $sth = $dbh->prepare("select m1.bibid from $sql_tables where $sql_where1");
106         }
107         $sth->execute;
108         my @result;
109         while (my ($bibid) = $sth->fetchrow) {
110                 push @result,$bibid;
111         }
112         # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
113         my $counter = $offset;
114         $sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
115         my @finalresult = ();
116         while ($counter <= ($offset + $length)) {
117                 $sth->execute($result[$counter]);
118                 my ($author,$title) = $sth->fetchrow;
119                 my %line;
120                 $line{bibid}=$result[$counter];
121                 $line{author}=$author;
122                 $line{title}=$title;
123                 push @finalresult, \%line;
124                 $counter++;
125         }
126         return @finalresult;
127 }
128
129 END { }       # module clean-up code here (global destructor)
130
131 1;
132 __END__
133
134 =back
135
136 =head1 AUTHOR
137
138 Koha Developement team <info@koha.org>
139
140 =cut