removing warnings
[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
60 sub catalogsearch {
61         my ($dbh, $tags, $subfields, $and_or, $excluding, $operator, $value, $offset,$length) = @_;
62         # build the sql request. She will look like :
63         # select m1.bibid
64         #               from marc_subfield_table as m1, marc_subfield_table as m2
65         #               where m1.bibid=m2.bibid and
66         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
67
68         my $sql_tables; # will contain marc_subfield_table as m1,...
69         my $sql_where1; # will contain the "true" where
70         my $sql_where2; # will contain m1.bibid=m2.bibid
71         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
72         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
73
74         for(my $i=0; $i<=@$value;$i++) {
75                 if (@$value[$i]) {
76                         $nb_active++;
77                         if ($nb_active==1) {
78                                 if (@$operator[$i] eq "start") {
79                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
80                                         $sql_where1 .= "@$excluding[$i](m1.subfieldvalue like '@$value[$i]%'";
81                                         if (@$tags[$i]) {
82                                                 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
83                                         }
84                                         $sql_where1.=")";
85                                 } elsif (@$operator[$i] eq "contains") {
86                                         $sql_tables .= "marc_word as m$nb_table,";
87                                         $sql_where1 .= "@$excluding[$i](m1.word  like '@$value[$i]%'";
88                                         if (@$tags[$i]) {
89                                                  $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldid='@$subfields[$i]'";
90                                         }
91                                         $sql_where1.=")";
92                                 } else {
93                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
94                                         $sql_where1 .= "@$excluding[$i](m1.subfieldvalue @$operator[$i] '@$value[$i]' ";
95                                         if (@$tags[$i]) {
96                                                  $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
97                                         }
98                                         $sql_where1.=")";
99                                 }
100                         } else {
101                                 if (@$operator[$i] eq "start") {
102                                         $nb_table++;
103                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
104                                         $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb_table.subfieldvalue like '@$value[$i]%'";
105                                         if (@$tags[$i]) {
106                                                  $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i])";
107                                         }
108                                         $sql_where1.=")";
109                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid";
110                                 } elsif (@$operator[$i] eq "contains") {
111                                         if (@$and_or[$i] eq 'and') {
112                                                 $nb_table++;
113                                                 $sql_tables .= "marc_word as m$nb_table,";
114                                                 $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb_table.word like '@$value[$i]%'";
115                                                 if (@$tags[$i]) {
116                                                         $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
117                                                 }
118                                                 $sql_where1.=")";
119                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid";
120                                         } else {
121                                                 $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb_table.word like '@$value[$i]%'";
122                                                 if (@$tags[$i]) {
123                                                         $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
124                                                 }
125                                                 $sql_where1.=")";
126                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid";
127                                         }
128                                 } else {
129                                         $nb_table++;
130                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
131                                         $sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb_table.subfieldvalue @$operator[$i] '@$value[$i]'";
132                                         if (@$tags[$i]) {
133                                                  $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
134                                         }
135                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid";
136                                         $sql_where1.=")";
137                                 }
138                         }
139                 }
140         }
141         chop $sql_tables;
142         my $sth;
143         if ($sql_where2) {
144                 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
145         } else {
146                 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where1");
147         }
148         $sth->execute;
149         my @result;
150         while (my ($bibid) = $sth->fetchrow) {
151                 push @result,$bibid;
152         }
153         # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
154         my $counter = $offset;
155         $sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
156         my @finalresult = ();
157         while ($counter <= ($offset + $length)) {
158                 $sth->execute($result[$counter]);
159                 my ($author,$title) = $sth->fetchrow;
160                 my %line;
161                 $line{bibid}=$result[$counter];
162                 $line{author}=$author;
163                 $line{title}=$title;
164                 push @finalresult, \%line;
165                 $counter++;
166         }
167         return @finalresult;
168 }
169
170 END { }       # module clean-up code here (global destructor)
171
172 1;
173 __END__
174
175 =back
176
177 =head1 AUTHOR
178
179 Koha Developement team <info@koha.org>
180
181 =cut