]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
It's ugly and hacky, but numeric search fully works (item barcode included)
[Evergreen.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 use OpenSRF::Utils::JSON;
9 my $U = 'OpenILS::Application::AppUtils';
10
11
12 sub _prepare_biblio_search_basics {
13     my ($cgi) = @_;
14
15     return $cgi->param('query') unless $cgi->param('qtype');
16
17     my %parts;
18     my @part_names = qw/qtype contains query/;
19     $parts{$_} = [ $cgi->param($_) ] for (@part_names);
20
21     my @chunks = ();
22     for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
23         my ($qtype, $contains, $query) = map { $parts{$_}->[$i] } @part_names;
24
25         next unless $query =~ /\S/;
26         push(@chunks, $qtype . ':') unless $qtype eq 'keyword' and $i == 0;
27
28         # This stuff probably will need refined or rethought to better handle
29         # the weird things Real Users will surely type in.
30         $contains = "" unless defined $contains; # silence warning
31         if ($contains eq 'nocontains') {
32             $query =~ s/"//g;
33             $query = ('"' . $query . '"') if index $query, ' ';
34             $query = '-' . $query;
35         } elsif ($contains eq 'phrase') {
36             $query =~ s/"//g;
37             $query = ('"' . $query . '"') if index $query, ' ';
38         } elsif ($contains eq 'exact') {
39             $query =~ s/[\^\$]//g;
40             $query = '^' . $query . '$';
41         }
42         push @chunks, $query;
43     }
44
45     return join(' ', @chunks);
46 }
47
48 sub _prepare_biblio_search {
49     my ($cgi, $ctx) = @_;
50
51     my $query = _prepare_biblio_search_basics($cgi) || '';
52
53     $query = ('#' . $_ . ' ' . $query) foreach ($cgi->param('modifier'));
54
55     # filters
56     foreach (grep /^fi:/, $cgi->param) {
57         /:(-?\w+)$/ or next;
58         my $term = join(",", $cgi->param($_));
59         $query .= " $1($term)" if length $term;
60     }
61
62     if ($cgi->param('sort')) {
63         my ($axis, $desc) = split /\./, $cgi->param('sort');
64         $query .= " sort($axis)";
65         $query .= '#descending' if $desc;
66     }
67
68     if ($cgi->param('pubdate') && $cgi->param('date1')) {
69         if ($cgi->param('pubdate') eq 'between') {
70             $query .= ' between(' . $cgi->param('date1');
71             $query .= ',' .  $cgi->param('date2') if $cgi->param('date2');
72             $query .= ')';
73         } elsif ($cgi->param('pubdate') eq 'is') {
74             $query .= ' between(' . $cgi->param('date1') .
75                 ',' .  $cgi->param('date1') . ')';  # sic, date1 twice
76         } else {
77             $query .= ' ' . $cgi->param('pubdate') .
78                 '(' . $cgi->param('date1') . ')';
79         }
80     }
81
82     my $site;
83     my $org = $cgi->param('loc');
84     if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
85         $site = $ctx->{get_aou}->($org)->shortname;
86         $query .= " site($site)";
87     }
88
89     if(!$site) {
90         ($site) = ($query =~ /site\(([^\)]+)\)/);
91         $site ||= $ctx->{aou_tree}->()->shortname;
92     }
93
94
95     my $depth;
96     if (defined($cgi->param('depth')) and not $query =~ /depth\(\d+\)/) {
97         $depth = defined $cgi->param('depth') ?
98             $cgi->param('depth') : $ctx->{get_aou}->($site)->ou_type->depth;
99         $query .= " depth($depth)";
100     }
101
102     return ($query, $site, $depth);
103 }
104
105 sub _get_search_limit {
106     my $self = shift;
107
108     # param takes precedence
109     my $limit = $self->cgi->param('limit');
110     return $limit if $limit;
111
112     if($self->editor->requestor) {
113         # See if the user has a hit count preference
114         my $lset = $self->editor->search_actor_user_setting({
115             usr => $self->editor->requestor->id, 
116             name => 'opac.hits_per_page'
117         })->[0];
118         return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
119     }
120
121     return 10; # default
122 }
123
124 # context additions: 
125 #   page_size
126 #   hit_count
127 #   records : list of bre's and copy-count objects
128 sub load_rresults {
129     my $self = shift;
130     my $cgi = $self->cgi;
131     my $ctx = $self->ctx;
132     my $e = $self->editor;
133
134     $ctx->{page} = 'rresult';
135
136     return $self->item_barcode_shortcut if $cgi->param("qtype") eq "item_barcode";
137
138     my $page = $cgi->param('page') || 0;
139     my $facet = $cgi->param('facet');
140     my $limit = $self->_get_search_limit;
141     my $loc = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
142     my $offset = $page * $limit;
143     my $metarecord = $cgi->param('metarecord');
144     my $results; 
145
146     my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
147
148     if ($metarecord) {
149
150         # TODO: other limits, like SVF/format, etc.
151         $results = $U->simplereq(
152             'open-ils.search', 
153             'open-ils.search.biblio.metarecord_to_records',
154             $metarecord, {org => $loc, depth => $depth}
155         );
156
157         # force the metarecord result blob to match the format of regular search results
158         $results->{ids} = [map { [$_] } @{$results->{ids}}]; 
159
160     } else {
161
162         return $self->generic_redirect unless $query;
163
164         # Limit and offset will stay here. Everything else should be part of
165         # the query string, not special args.
166         my $args = {'limit' => $limit, 'offset' => $offset};
167
168         # Stuff these into the TT context so that templates can use them in redrawing forms
169         $ctx->{processed_search_query} = $query;
170
171         $query = "$query $facet" if $facet; # TODO
172
173         $logger->activity("EGWeb: [search] $query");
174
175         try {
176
177             my $method = 'open-ils.search.biblio.multiclass.query';
178             $method .= '.staff' if $ctx->{is_staff};
179             $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
180
181         } catch Error with {
182             my $err = shift;
183             $logger->error("multiclass search error: $err");
184             $results = {count => 0, ids => []};
185         };
186     }
187
188     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
189
190     $ctx->{records} = [];
191     $ctx->{search_facets} = {};
192     $ctx->{page_size} = $limit;
193     $ctx->{hit_count} = $results->{count};
194
195     return Apache2::Const::OK if @$rec_ids == 0;
196
197     my ($facets, @data) = $self->get_records_and_facets(
198         $rec_ids, $results->{facet_key}, 
199         {
200             flesh => '{holdings_xml,mra}',
201             site => $site,
202             depth => $depth
203         }
204     );
205
206     # shove recs into context in search results order
207     for my $rec_id (@$rec_ids) {
208         push(
209             @{$ctx->{records}},
210             grep { $_->{id} == $rec_id } @data
211         );
212     }
213
214     $ctx->{search_facets} = $facets;
215
216     return Apache2::Const::OK;
217 }
218
219 # Searching by barcode is a special search that does /not/ respect any other
220 # of the usual search parameters, not even the ones for sorting and paging!
221 sub item_barcode_shortcut {
222     my ($self) = @_;
223
224     my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
225     if (my $search = create OpenSRF::AppSession("open-ils.search")) {
226         my $rec_ids = $search->request(
227             $method, $self->cgi->param("query")
228         )->gather(1);
229
230         if (ref $rec_ids ne 'ARRAY') {
231             if (defined $U->event_code($rec_ids)) {
232                 $self->apache->log->warn(
233                     "$method returned event: " . $U->event_code($rec_ids)
234                 );
235             } else {
236                 $self->apache->log->warn(
237                     "$method returned something unexpected: $rec_ids"
238                 );
239             }
240             return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
241         }
242
243         my ($facets, @data) = $self->get_records_and_facets(
244             $rec_ids, undef, {flesh => "{holdings_xml,mra}"}
245         );
246
247         $self->ctx->{records} = [@data];
248         $self->ctx->{search_facets} = {};
249         $self->ctx->{hit_count} = scalar @data;
250         $self->ctx->{page_size} = $self->ctx->{hit_count};
251
252         return Apache2::Const::OK;
253     } {
254         $self->apache->log->warn("couldn't connect to open-ils.search");
255         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
256     }
257 }
258 1;