pre-svf mattype filter (well, facet) handing for kcls;
[evergreen-equinox.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Search.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 my $U = 'OpenILS::Application::AppUtils';
9
10
11 sub _prepare_biblio_search_basics {
12     my ($cgi) = @_;
13
14     return $cgi->param('query') unless $cgi->param('qtype');
15
16     my %parts;
17     my @part_names = qw/qtype contains query/;
18     $parts{$_} = [ $cgi->param($_) ] for (@part_names);
19
20     my @chunks = ();
21     for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
22         my ($qtype, $contains, $query) = map { $parts{$_}->[$i] } @part_names;
23
24         next unless $query =~ /\S/;
25         push(@chunks, $qtype . ':') unless $qtype eq 'keyword' and $i == 0;
26
27         # This stuff probably will need refined or rethought to better handle
28         # the weird things Real Users will surely type in.
29         $contains = "" unless defined $contains; # silence warning
30         if ($contains eq 'nocontains') {
31             $query =~ s/"//g;
32             $query = ('"' . $query . '"') if index $query, ' ';
33             $query = '-' . $query;
34         } elsif ($contains eq 'phrase') {
35             $query =~ s/"//g;
36             $query = ('"' . $query . '"') if index $query, ' ';
37         } elsif ($contains eq 'exact') {
38             $query =~ s/[\^\$]//g;
39             $query = '^' . $query . '$';
40         }
41         push @chunks, $query;
42     }
43
44     return join(' ', @chunks);
45 }
46
47 sub _prepare_biblio_search {
48     my ($cgi, $ctx) = @_;
49
50     my $query = _prepare_biblio_search_basics($cgi);
51
52     $query = ('#' . $_ . ' ' . $query) foreach ($cgi->param('modifier'));
53
54     foreach (grep /^fi:/, $cgi->param) {
55         /:(\w+)$/ or next;
56         my $term = join(",", $cgi->param($_));
57         next unless length $term;
58         if($_ eq 'fi:format') {
59             # XXX pre-svf KCLS mattype filtering... 
60             $query .= " identifier|mattype[$term]";
61         } else {
62             $query .= " $1($term)";
63         }
64     }
65
66     if ($cgi->param('sort')) {
67         my ($axis, $desc) = split /\./, $cgi->param('sort');
68         $query .= " sort($axis)";
69         $query .= '#descending' if $desc;
70     }
71
72     if ($cgi->param('pubdate') && $cgi->param('date1')) {
73         if ($cgi->param('pubdate') eq 'between') {
74             $query .= ' between(' . $cgi->param('date1');
75             $query .= ',' .  $cgi->param('date2') if $cgi->param('date2');
76             $query .= ')';
77         } elsif ($cgi->param('pubdate') eq 'is') {
78             $query .= ' between(' . $cgi->param('date1') .
79                 ',' .  $cgi->param('date1') . ')';  # sic, date1 twice
80         } else {
81             $query .= ' ' . $cgi->param('pubdate') .
82                 '(' . $cgi->param('date1') . ')';
83         }
84     }
85
86     my $site = $cgi->param('loc');
87     if (defined($site) and $site ne '' and ($site ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\d+\)/) {
88         $query .= " site($site)";
89     }
90     if (defined($cgi->param('depth')) and not $query =~ /depth\(\d+\)/) {
91         my $depth = defined $cgi->param('depth') ?
92             $cgi->param('depth') : $ctx->{find_aou}->($site)->ou_type->depth;
93         $query .= " depth($depth)";
94     }
95
96     return $query;
97 }
98
99 # context additions: 
100 #   page_size
101 #   hit_count
102 #   records : list of bre's and copy-count objects
103 sub load_rresults {
104     my $self = shift;
105     my $cgi = $self->cgi;
106     my $ctx = $self->ctx;
107     my $e = $self->editor;
108
109     $ctx->{page} = 'rresult';
110     my $page = $cgi->param('page') || 0;
111     my $facet = $cgi->param('facet');
112     my $limit = $cgi->param('limit') || 10; # TODO user settings
113     my $offset = $page * $limit;
114
115     my $query = _prepare_biblio_search($cgi, $ctx);
116     # Limit and offset will stay here. Everything else should be part of
117     # the query string, not special args.
118     my $args = {'limit' => $limit, 'offset' => $offset};
119
120     # Stuff these into the TT context so that templates can use them in redrawing forms
121     $ctx->{processed_search_query} = $query;
122
123     $query = "$query $facet" if $facet; # TODO
124
125     $logger->activity("EGWeb: [search] $query");
126
127     my $results;
128
129     try {
130
131         my $method = 'open-ils.search.biblio.multiclass.query';
132         $method .= '.staff' if $ctx->{is_staff};
133         $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
134
135     } catch Error with {
136         my $err = shift;
137         $logger->error("multiclass search error: $err");
138         $results = {count => 0, ids => []};
139     };
140
141     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
142
143     $ctx->{records} = [];
144     $ctx->{search_facets} = {};
145     $ctx->{page_size} = $limit;
146     $ctx->{hit_count} = $results->{count};
147
148     return Apache2::Const::OK if @$rec_ids == 0;
149
150     my ($facets, @data) = $self->get_records_and_facets($rec_ids, $results->{facet_key});
151
152     # shove recs into context in search results order
153     for my $rec_id (@$rec_ids) {
154         push(
155             @{$ctx->{records}},
156             grep { $_->{bre}->id == $rec_id } @data
157         );
158     }
159
160     $ctx->{search_facets} = $facets;
161
162     return Apache2::Const::OK;
163 }
164
165 1;