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';
11 sub _prepare_biblio_search_basics {
14 return $cgi->param('query') unless $cgi->param('qtype');
17 my @part_names = qw/qtype contains query/;
18 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
21 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
22 my ($qtype, $contains, $query) = map { $parts{$_}->[$i] } @part_names;
24 next unless $query =~ /\S/;
25 push(@chunks, $qtype . ':') unless $qtype eq 'keyword' and $i == 0;
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') {
32 $query = ('"' . $query . '"') if index $query, ' ';
33 $query = '-' . $query;
34 } elsif ($contains eq 'phrase') {
36 $query = ('"' . $query . '"') if index $query, ' ';
37 } elsif ($contains eq 'exact') {
38 $query =~ s/[\^\$]//g;
39 $query = '^' . $query . '$';
44 return join(' ', @chunks);
47 sub _prepare_biblio_search {
50 my $query = _prepare_biblio_search_basics($cgi);
52 $query = ('#' . $_ . ' ' . $query) foreach ($cgi->param('modifier'));
54 foreach (grep /^fi:/, $cgi->param) {
56 my $term = join(",", $cgi->param($_));
57 $query .= " $1($term)" if length $term;
60 if ($cgi->param('sort')) {
61 my ($axis, $desc) = split /\./, $cgi->param('sort');
62 $query .= " sort($axis)";
63 $query .= '#descending' if $desc;
66 if ($cgi->param('pubdate') && $cgi->param('date1')) {
67 if ($cgi->param('pubdate') eq 'between') {
68 $query .= ' between(' . $cgi->param('date1');
69 $query .= ',' . $cgi->param('date2') if $cgi->param('date2');
71 } elsif ($cgi->param('pubdate') eq 'is') {
72 $query .= ' between(' . $cgi->param('date1') .
73 ',' . $cgi->param('date1') . ')'; # sic, date1 twice
75 $query .= ' ' . $cgi->param('pubdate') .
76 '(' . $cgi->param('date1') . ')';
80 my $site = $cgi->param('loc');
81 if (defined($site) and $site ne '' and ($site ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\d+\)/) {
82 $query .= " site($site)";
84 if (defined($cgi->param('depth')) and not $query =~ /depth\(\d+\)/) {
85 my $depth = defined $cgi->param('depth') ?
86 $cgi->param('depth') : $ctx->{find_aou}->($site)->ou_type->depth;
87 $query .= " depth($depth)";
96 # records : list of bre's and copy-count objects
100 my $ctx = $self->ctx;
101 my $e = $self->editor;
103 $ctx->{page} = 'rresult';
104 my $page = $cgi->param('page') || 0;
105 my $facet = $cgi->param('facet');
106 my $limit = $cgi->param('limit') || 10; # TODO user settings
107 my $offset = $page * $limit;
109 my $query = _prepare_biblio_search($cgi, $ctx);
110 # Limit and offset will stay here. Everything else should be part of
111 # the query string, not special args.
112 my $args = {'limit' => $limit, 'offset' => $offset};
114 # Stuff these into the TT context so that templates can use them in redrawing forms
115 $ctx->{processed_search_query} = $query;
117 $query = "$query $facet" if $facet; # TODO
123 my $method = 'open-ils.search.biblio.multiclass.query';
124 $method .= '.staff' if $ctx->{is_staff};
125 $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
129 $logger->error("multiclass search error: $err");
130 $results = {count => 0, ids => []};
133 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
135 $ctx->{records} = [];
136 $ctx->{search_facets} = {};
137 $ctx->{page_size} = $limit;
138 $ctx->{hit_count} = $results->{count};
140 return Apache2::Const::OK if @$rec_ids == 0;
142 my ($facets, @data) = $self->get_records_and_facets($rec_ids, $results->{facet_key});
144 # shove recs into context in search results order
145 for my $rec_id (@$rec_ids) {
148 grep { $_->{bre}->id == $rec_id } @data
152 $ctx->{search_facets} = $facets;
154 return Apache2::Const::OK;