typo fixed: a ";" was missing at the end of Zebra connection retrieving in
[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);
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);
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     warn $string;
112
113         $q = new ZOOM::Query::CQL2RPN( $string, $Zconn);
114     eval {
115 #        my $rs = $Zconn->search_pqf("\@attr 1=12 $id");
116         my $rs = $Zconn->search($q);
117         my $n  = $rs->size();
118         if ( $n > 0 ) {
119             $raw = $rs->record(0)->raw();
120         }
121     };
122     if ($@) {
123
124         print "Error ", $@->code(), ": ", $@->message(), "\n";
125     }
126     ###$raw
127     my $record = MARC::Record->new_from_xml($raw);
128     ###$record
129     return ($record);
130 }
131
132 1;
133 __END__
134
135 =back
136
137 =head1 AUTHOR
138
139 Koha Developement team <info@koha.org>
140
141 =cut
142