Sub renamed according to the coding guidelines
[koha.git] / C4 / Search.pm
1 package C4::Search;
2
3 # Copyright 2000-2006 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 use ZOOM;
22 use Smart::Comments;
23 use C4::Context;
24 use MARC::Record;
25 use MARC::File::XML;
26 use C4::Biblio;
27
28 require Exporter;
29
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
32 # set the version for version checking
33 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
34     shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
35 };
36
37 =head1 NAME
38
39 C4::Search - Functions for searching the Koha catalog and other databases
40
41 =head1 SYNOPSIS
42
43   use C4::Search;
44
45 =head1 DESCRIPTION
46
47 This module provides the searching facilities for the Koha catalog and
48 other databases.
49
50 =head1 FUNCTIONS
51
52 =over 2
53
54 =cut
55
56 @ISA    = qw(Exporter);
57 @EXPORT = qw(search get_record get_xml_record);
58
59 # make all your functions, whether exported or not;
60
61 sub search {
62     my ( $search, $type, $number ) = @_;
63     my $dbh = C4::Context->dbh();
64     my $q;
65     my $Zconn = C4::Context->Zconn;
66     my $raw;
67
68     if ( $type eq 'CQL' ) {
69         my $string;
70         if ( $search->{'cql'} ) {
71             $string = $search->{'cql'};
72         }
73         else {
74             foreach my $var ( keys %$search ) {
75                 $string .= "$var=\"$search->{$var}\" ";
76             }
77         }
78         $q = new ZOOM::Query::CQL2RPN( $string, $Zconn );
79     }
80     my $rs;
81     my $n;
82     eval {
83         $rs = $Zconn->search($q);
84         $n  = $rs->size();
85     };
86     if ($@) {
87         print "Error ", $@->code(), ": ", $@->message(), "\n";
88     }
89     my $i = 0;
90     my @results;
91     while ( $i < $n && $i < $number ) {
92         $raw = $rs->record($i)->raw();
93         my $record = MARC::Record->new_from_xml($raw, 'UTF-8');
94         my $line = MARCmarc2koha( $dbh, $record );
95         push @results, $line;
96 #        push @results,$raw;
97         $i++;
98     }
99     return ( \@results );
100
101 }
102
103 sub get_record {
104
105     # pass in an id (biblionumber at this stage) and get back a MARC record
106     my ($id) = @_;
107     my $q;
108     my $Zconn = C4::Context->Zconn;
109     my $raw;
110     my $string = "identifier=$id";
111 #    my $string = "title=delete";
112 #    warn $string;
113
114         $q = new ZOOM::Query::CQL2RPN( $string, $Zconn);
115     eval {
116 #        my $rs = $Zconn->search_pqf("\@attr 1=12 $id");
117         my $rs = $Zconn->search($q);
118         my $n  = $rs->size();
119         if ( $n > 0 ) {
120             $raw = $rs->record(0)->raw();
121         }
122     };
123     if ($@) {
124
125         warn "Error ", $@->code(), ": ", $@->message(), "\n";
126     }
127     ###$raw
128     my $record = MARC::Record->new_from_xml($raw, 'UTF-8');
129     ###$record
130     return ($record);
131 }
132
133
134 sub get_xml_record {
135     # pass in an id (biblionumber at this stage) and get back a MARC record
136     my ($id) = @_;
137     my $q;
138     my $Zconn = C4::Context->Zconn;
139     my $raw;
140     my $string = "identifier=$id";
141 #    my $string = "title=delete";
142 #    warn $string;
143
144         $q = new ZOOM::Query::CQL2RPN( $string, $Zconn);
145     eval {
146 #        my $rs = $Zconn->search_pqf("\@attr 1=12 $id");
147         my $rs = $Zconn->search($q);
148         my $n  = $rs->size();
149         if ( $n > 0 ) {
150             $raw = $rs->record(0)->raw();
151         }
152     };
153     if ($@) {
154
155         warn "Error ", $@->code(), ": ", $@->message(), "\n";
156     }
157     ###$raw
158     my $record = $raw;
159     ###$record
160     return ($record);
161 }    
162
163 1;
164 __END__
165
166 =back
167
168 =head1 AUTHOR
169
170 Koha Developement team <info@koha.org>
171
172 =cut
173