]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen into template-toolkit-opac
[working/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     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;
140
141     my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
142
143     return $self->generic_redirect unless $query;
144
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};
148
149     # Stuff these into the TT context so that templates can use them in redrawing forms
150     $ctx->{processed_search_query} = $query;
151
152     $query = "$query $facet" if $facet; # TODO
153
154     $logger->activity("EGWeb: [search] $query");
155
156     my $results;
157
158     try {
159
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);
163
164     } catch Error with {
165         my $err = shift;
166         $logger->error("multiclass search error: $err");
167         $results = {count => 0, ids => []};
168     };
169
170     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
171
172     $ctx->{records} = [];
173     $ctx->{search_facets} = {};
174     $ctx->{page_size} = $limit;
175     $ctx->{hit_count} = $results->{count};
176
177     return Apache2::Const::OK if @$rec_ids == 0;
178
179     my ($facets, @data) = $self->get_records_and_facets(
180         $rec_ids, $results->{facet_key}, 
181         {
182             flesh => '{holdings_xml,mra}',
183             site => $site,
184             depth => $depth
185         }
186     );
187
188     # shove recs into context in search results order
189     for my $rec_id (@$rec_ids) {
190         push(
191             @{$ctx->{records}},
192             grep { $_->{id} == $rec_id } @data
193         );
194     }
195
196     $ctx->{search_facets} = $facets;
197
198     return Apache2::Const::OK;
199 }
200
201 1;