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';
135 my $page = $cgi->param('page') || 0;
136 my $facet = $cgi->param('facet');
137 my $limit = $self->_get_search_limit;
138 my $loc = $cgi->param('loc');
139 my $offset = $page * $limit;
141 my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
143 return $self->generic_redirect unless $query;
145 # Limit and offset will stay here. Everything else should be part of
146 # the query string, not special args.
147 my $args = {'limit' => $limit, 'offset' => $offset};
149 # Stuff these into the TT context so that templates can use them in redrawing forms
150 $ctx->{processed_search_query} = $query;
152 $query = "$query $facet" if $facet; # TODO
154 $logger->activity("EGWeb: [search] $query");
160 my $method = 'open-ils.search.biblio.multiclass.query';
161 $method .= '.staff' if $ctx->{is_staff};
162 $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
166 $logger->error("multiclass search error: $err");
167 $results = {count => 0, ids => []};
170 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
172 $ctx->{records} = [];
173 $ctx->{search_facets} = {};
174 $ctx->{page_size} = $limit;
175 $ctx->{hit_count} = $results->{count};
177 return Apache2::Const::OK if @$rec_ids == 0;
179 my ($facets, @data) = $self->get_records_and_facets(
180 $rec_ids, $results->{facet_key},
182 flesh => '{holdings_xml,mra,acp}',
188 # shove recs into context in search results order
189 for my $rec_id (@$rec_ids) {
192 grep { $_->{id} == $rec_id } @data
196 $ctx->{search_facets} = $facets;
198 return Apache2::Const::OK;