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 {
15 my @part_names = qw/class contains query/;
16 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
19 for (my $i = 0; $i < scalar @{$parts{'class'}}; $i++) {
20 my ($class, $contains, $query) = map { $parts{$_}->[$i] } @part_names;
22 push(@chunks, $class . ':') unless $class eq 'keyword' and $i == 0;
24 # This stuff probably will need refined or rethought to better handle
25 # the weird things Real Users will surely type in.
26 if ($contains eq 'nocontains') {
28 $query = ('"' . $query . '"') if index $query, ' ';
29 $query = '-' . $query;
30 } elsif ($contains eq 'exact') {
32 $query = ('"' . $query . '"') if index $query, ' ';
37 return join(' ', @chunks);
40 sub _prepare_biblio_search {
43 my $query = _prepare_biblio_search_basics($cgi);
46 $args->{'org_unit'} = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
47 $args->{'depth'} = defined $cgi->param('depth') ?
48 $cgi->param('depth') :
49 $ctx->{find_aou}->($args->{'org_unit'})->ou_type->depth;
51 if ($cgi->param('available')) {
52 $query = '#available ' . $query;
55 if ($cgi->param('format')) {
56 $args->{'format'} = join('', $cgi->param('format'));
59 if ($cgi->param('lang')) {
60 # XXX TODO find out how to build query with multiple langs, if that
61 # even needs to be a feature of adv search.
62 $query .= ' lang:' . $cgi->param('lang');
65 if ($cgi->param('audience')) {
66 $query .= ' audience(' . $cgi->param('audience') . ')';
69 if (defined $cgi->param('sort')) {
70 my $sort = $cgi->param('sort');
71 my $sort_order = $cgi->param('sort_order');
72 $query .= " sort($sort)";
73 $query .= '#' . $sort_order if $sort_order and $sort ne 'rel';
76 if ($cgi->param('pubyear_how') && $cgi->param('pubyear1')) {
77 if ($cgi->param('pubyear_how') eq 'between') {
78 $query .= ' between(' . $cgi->param('pubyear1');
79 $query .= ',' . $cgi->param('pubyear2') if $cgi->param('pubyear2');
82 $query .= ' ' . $cgi->param('pubyear_how') .
83 '(' . $cgi->param('pubyear1') . ')';
87 return ($args, $query);
93 # records : list of bre's and copy-count objects
98 my $e = $self->editor;
100 $ctx->{page} = 'rresult';
101 my $page = $cgi->param('page') || 0;
102 my $facet = $cgi->param('facet');
103 my $limit = $cgi->param('limit') || 10; # TODO user settings
105 my ($args, $query) = _prepare_biblio_search($cgi, $ctx);
107 # Stuff these into the TT context so that templates can use them in redrawing forms
108 $ctx->{processed_search_query} = $query;
109 $ctx->{processed_search_args} = $args;
111 $args->{'limit'} = $limit;
112 $args->{'offset'} = $page * $limit;
114 $query = "$query $facet" if $facet; # TODO
120 my $method = 'open-ils.search.biblio.multiclass.query';
121 $method .= '.staff' if $ctx->{is_staff};
122 $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
126 $logger->error("multiclass search error: $err");
127 $results = {count => 0, ids => []};
130 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
132 $ctx->{records} = [];
133 $ctx->{search_facets} = {};
134 $ctx->{page_size} = $limit;
135 $ctx->{hit_count} = $results->{count};
137 return Apache2::Const::OK if @$rec_ids == 0;
139 my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
140 my $bre_req = $cstore1->request(
141 'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
143 my $search = OpenSRF::AppSession->create('open-ils.search');
144 my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
147 while(my $resp = $bre_req->recv) {
148 my $bre = $resp->content;
150 # XXX farm out to multiple cstore sessions before loop, then collect after
151 my $copy_counts = $e->json_query(
152 {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
157 marc_xml => XML::LibXML->new->parse_string($bre->marc),
158 copy_counts => $copy_counts
165 # shove recs into context in search results order
166 for my $rec_id (@$rec_ids) {
169 grep { $_->{bre}->id == $rec_id } @data
173 my $facets = $facet_req->gather(1);
175 $facets->{$_} = {cmf => $ctx->{find_cmf}->($_), data => $facets->{$_}} for keys %$facets; # quick-n-dirty
176 $ctx->{search_facets} = $facets;
178 return Apache2::Const::OK;