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';
12 sub _prepare_biblio_search_basics {
15 return $cgi->param('query') unless $cgi->param('qtype');
18 my @part_names = qw/qtype contains query/;
19 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
22 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
23 my ($qtype, $contains, $query) = map { $parts{$_}->[$i] } @part_names;
25 next unless $query =~ /\S/;
26 push(@chunks, $qtype . ':') unless $qtype eq 'keyword' and $i == 0;
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') {
33 $query = ('"' . $query . '"') if index $query, ' ';
34 $query = '-' . $query;
35 } elsif ($contains eq 'phrase') {
37 $query = ('"' . $query . '"') if index $query, ' ';
38 } elsif ($contains eq 'exact') {
39 $query =~ s/[\^\$]//g;
40 $query = '^' . $query . '$';
45 return join(' ', @chunks);
48 sub _prepare_biblio_search {
51 my $query = _prepare_biblio_search_basics($cgi) || '';
53 $query = ('#' . $_ . ' ' . $query) foreach ($cgi->param('modifier'));
56 foreach (grep /^fi:/, $cgi->param) {
58 my $term = join(",", $cgi->param($_));
59 $query .= " $1($term)" if length $term;
62 if ($cgi->param('sort')) {
63 my ($axis, $desc) = split /\./, $cgi->param('sort');
64 $query .= " sort($axis)";
65 $query .= '#descending' if $desc;
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');
73 } elsif ($cgi->param('pubdate') eq 'is') {
74 $query .= ' between(' . $cgi->param('date1') .
75 ',' . $cgi->param('date1') . ')'; # sic, date1 twice
77 $query .= ' ' . $cgi->param('pubdate') .
78 '(' . $cgi->param('date1') . ')';
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)";
90 ($site) = ($query =~ /site\(([^\)]+)\)/);
91 $site ||= $ctx->{aou_tree}->()->shortname;
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)";
102 return ($query, $site, $depth);
105 sub _get_search_limit {
108 # param takes precedence
109 my $limit = $self->cgi->param('limit');
110 return $limit if $limit;
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'
118 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
127 # records : list of bre's and copy-count objects
130 my $cgi = $self->cgi;
131 my $ctx = $self->ctx;
132 my $e = $self->editor;
134 $ctx->{page} = 'rresult';
136 return $self->item_barcode_shortcut if $cgi->param("qtype") eq "item_barcode";
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');
146 my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
150 # TODO: other limits, like SVF/format, etc.
151 $results = $U->simplereq(
153 'open-ils.search.biblio.metarecord_to_records',
154 $metarecord, {org => $loc, depth => $depth}
157 # force the metarecord result blob to match the format of regular search results
158 $results->{ids} = [map { [$_] } @{$results->{ids}}];
162 return $self->generic_redirect unless $query;
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};
168 # Stuff these into the TT context so that templates can use them in redrawing forms
169 $ctx->{processed_search_query} = $query;
171 $query = "$query $facet" if $facet; # TODO
173 $logger->activity("EGWeb: [search] $query");
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);
183 $logger->error("multiclass search error: $err");
184 $results = {count => 0, ids => []};
188 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
190 $ctx->{records} = [];
191 $ctx->{search_facets} = {};
192 $ctx->{page_size} = $limit;
193 $ctx->{hit_count} = $results->{count};
195 return Apache2::Const::OK if @$rec_ids == 0;
197 my ($facets, @data) = $self->get_records_and_facets(
198 $rec_ids, $results->{facet_key},
200 flesh => '{holdings_xml,mra}',
206 # shove recs into context in search results order
207 for my $rec_id (@$rec_ids) {
210 grep { $_->{id} == $rec_id } @data
214 $ctx->{search_facets} = $facets;
216 return Apache2::Const::OK;
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 {
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")
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)
236 $self->apache->log->warn(
237 "$method returned something unexpected: $rec_ids"
240 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
243 my ($facets, @data) = $self->get_records_and_facets(
244 $rec_ids, undef, {flesh => "{holdings_xml,mra}"}
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};
252 return Apache2::Const::OK;
254 $self->apache->log->warn("couldn't connect to open-ils.search");
255 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;