]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
Merge branch 'master' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen-equin...
[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 my $U = 'OpenILS::Application::AppUtils';
9
10
11 sub _prepare_biblio_search_basics {
12     my ($cgi) = @_;
13
14     return $cgi->param('query') unless $cgi->param('qtype');
15
16     my %parts;
17     my @part_names = qw/qtype contains query/;
18     $parts{$_} = [ $cgi->param($_) ] for (@part_names);
19
20     my @chunks = ();
21     for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
22         my ($qtype, $contains, $query) = map { $parts{$_}->[$i] } @part_names;
23
24         next unless $query =~ /\S/;
25         push(@chunks, $qtype . ':') unless $qtype eq 'keyword' and $i == 0;
26
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') {
31             $query =~ s/"//g;
32             $query = ('"' . $query . '"') if index $query, ' ';
33             $query = '-' . $query;
34         } elsif ($contains eq 'phrase') {
35             $query =~ s/"//g;
36             $query = ('"' . $query . '"') if index $query, ' ';
37         } elsif ($contains eq 'exact') {
38             $query =~ s/[\^\$]//g;
39             $query = '^' . $query . '$';
40         }
41         push @chunks, $query;
42     }
43
44     return join(' ', @chunks);
45 }
46
47 sub _prepare_biblio_search {
48     my ($cgi, $ctx) = @_;
49
50     my $query = _prepare_biblio_search_basics($cgi);
51
52     $query = ('#' . $_ . ' ' . $query) foreach ($cgi->param('modifier'));
53
54     # filters
55     foreach (grep /^fi:/, $cgi->param) {
56         /:(-?\w+)$/ or next;
57         my $term = join(",", $cgi->param($_));
58         $query .= " $1($term)" if length $term;
59     }
60
61     if ($cgi->param('sort')) {
62         my ($axis, $desc) = split /\./, $cgi->param('sort');
63         $query .= " sort($axis)";
64         $query .= '#descending' if $desc;
65     }
66
67     if ($cgi->param('pubdate') && $cgi->param('date1')) {
68         if ($cgi->param('pubdate') eq 'between') {
69             $query .= ' between(' . $cgi->param('date1');
70             $query .= ',' .  $cgi->param('date2') if $cgi->param('date2');
71             $query .= ')';
72         } elsif ($cgi->param('pubdate') eq 'is') {
73             $query .= ' between(' . $cgi->param('date1') .
74                 ',' .  $cgi->param('date1') . ')';  # sic, date1 twice
75         } else {
76             $query .= ' ' . $cgi->param('pubdate') .
77                 '(' . $cgi->param('date1') . ')';
78         }
79     }
80
81     my $site;
82     my $org = $cgi->param('loc');
83     if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
84         $site = $ctx->{get_aou}->($org)->shortname;
85         $query .= " site($site)";
86     }
87
88     if(!$site) {
89         ($site) = ($query =~ /site\(([^\)]+)\)/);
90         $site ||= $ctx->{aou_tree}->()->shortname;
91     }
92
93
94     my $depth;
95     if (defined($cgi->param('depth')) and not $query =~ /depth\(\d+\)/) {
96         $depth = defined $cgi->param('depth') ?
97             $cgi->param('depth') : $ctx->{get_aou}->($site)->ou_type->depth;
98         $query .= " depth($depth)";
99     }
100
101     return ($query, $site, $depth);
102 }
103
104 # context additions: 
105 #   page_size
106 #   hit_count
107 #   records : list of bre's and copy-count objects
108 sub load_rresults {
109     my $self = shift;
110     my $cgi = $self->cgi;
111     my $ctx = $self->ctx;
112     my $e = $self->editor;
113
114     $ctx->{page} = 'rresult';
115     my $page = $cgi->param('page') || 0;
116     my $facet = $cgi->param('facet');
117     my $limit = $cgi->param('limit') || 10; # TODO user settings
118     my $loc = $cgi->param('loc');
119     my $offset = $page * $limit;
120
121     my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
122
123     # Limit and offset will stay here. Everything else should be part of
124     # the query string, not special args.
125     my $args = {'limit' => $limit, 'offset' => $offset};
126
127     # Stuff these into the TT context so that templates can use them in redrawing forms
128     $ctx->{processed_search_query} = $query;
129
130     $query = "$query $facet" if $facet; # TODO
131
132     $logger->activity("EGWeb: [search] $query");
133
134     my $results;
135
136     try {
137
138         my $method = 'open-ils.search.biblio.multiclass.query';
139         $method .= '.staff' if $ctx->{is_staff};
140         $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
141
142     } catch Error with {
143         my $err = shift;
144         $logger->error("multiclass search error: $err");
145         $results = {count => 0, ids => []};
146     };
147
148     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
149
150     $ctx->{records} = [];
151     $ctx->{search_facets} = {};
152     $ctx->{page_size} = $limit;
153     $ctx->{hit_count} = $results->{count};
154
155     return Apache2::Const::OK if @$rec_ids == 0;
156
157     my ($facets, @data) = $self->get_records_and_facets(
158         $rec_ids, $results->{facet_key}, 
159         {
160             flesh => '{holdings_xml,mra}',
161             site => $site,
162             depth => $depth
163         }
164     );
165
166     # shove recs into context in search results order
167     for my $rec_id (@$rec_ids) {
168         push(
169             @{$ctx->{records}},
170             grep { $_->{id} == $rec_id } @data
171         );
172     }
173
174     $ctx->{search_facets} = $facets;
175
176     return Apache2::Const::OK;
177 }
178
179 1;