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;
10 $Data::Dumper::Indent = 0;
11 my $U = 'OpenILS::Application::AppUtils';
13 sub _prepare_biblio_search_basics {
16 return $cgi->param('query') unless $cgi->param('qtype');
19 my @part_names = qw/qtype contains query bool/;
20 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
23 my @bookplate_queries = ();
24 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
25 my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
27 next unless $query =~ /\S/;
29 # Hack for bookplates; "bookplate" is not a real search
30 # class, so grabbing them out of the advanced search query
31 # params to convert to a copy_tag(*,...) filter later
32 if ($qtype eq 'bookplate') {
33 $query =~ s/[)(]/ /g; # don't break on 'foo(bar)baz'
34 push @bookplate_queries, $query;
38 # Hack for journal title
40 if ($qtype eq 'jtitle') {
45 # This stuff probably will need refined or rethought to better handle
46 # the weird things Real Users will surely type in.
47 $contains = "" unless defined $contains; # silence warning
48 if ($contains eq 'nocontains') {
50 $query = ('"' . $query . '"') if index $query, ' ';
51 $query = '-' . $query;
52 } elsif ($contains eq 'phrase') {
54 $query = ('"' . $query . '"') if index $query, ' ';
55 } elsif ($contains eq 'exact') {
56 $query =~ s/[\^\$]//g;
57 $query = '^' . $query . '$';
58 } elsif ($contains eq 'starts') {
60 $query =~ s/[\^\$]//g;
61 $query = '^' . $query;
62 $query = ('"' . $query . '"') if index $query, ' ';
65 # Journal title hackery complete
67 $query = "bib_level(s) $query";
70 $query = "$qtype:$query" unless ($query =~ /^$qtype:/ or ($qtype eq 'keyword' and $i == 0));
72 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
73 $full_query = $full_query ? "($full_query $bool $query)" : $query;
76 return $full_query, \@bookplate_queries;
79 sub _prepare_biblio_search {
82 # XXX This will still contain the jtitle hack...
83 my ($user_query, $bookplate_queries) = _prepare_biblio_search_basics($cgi);
85 $bookplate_queries //= [];
86 my $query = $user_query;
87 if (@$bookplate_queries) {
88 $query .= " " . join(" ", map { "copy_tag(*,$_)" } @$bookplate_queries);
89 # hack to handle the case where a bookplate comes from the
91 $user_query = $bookplate_queries->[0] if $user_query eq '';
94 $query .= ' ' . $ctx->{global_search_filter} if $ctx->{global_search_filter};
96 foreach ($cgi->param('modifier')) {
97 # The unless bit is to avoid stacking modifiers.
98 $query = ('#' . $_ . ' ' . $query) unless
99 $query =~ qr/\#\Q$_/ or $_ eq 'metabib';
103 foreach (grep /^fi:/, $cgi->param) {
105 my $term = join(",", $cgi->param($_));
106 $query .= " $1($term)" if length $term;
109 # filter group entries. Entries from like filters are grouped into a single
110 # filter_group_entry() filter (ORed). Each collection is ANDed together.
111 # fg:foo_group=foo_entry_id
112 foreach (grep /^fg:/, $cgi->param) {
114 my $term = join(",", $cgi->param($_));
115 $query = "filter_group_entry($term) $query" if length $term;
118 if ($cgi->param("bookbag")) {
119 $query = "container(bre,bookbag," . int($cgi->param("bookbag")) . ") $query";
122 if ($cgi->param('pubdate') && $cgi->param('date1')) {
123 if ($cgi->param('pubdate') eq 'between') {
124 my $btw = 'between(' . $cgi->param('date1');
125 $btw .= ',' . $cgi->param('date2') if $cgi->param('date2');
127 $query = "$btw $query";
128 } elsif ($cgi->param('pubdate') eq 'is') {
129 $query = 'date1(' . $cgi->param('date1') . ") $query";
131 $query = $cgi->param('pubdate') .
132 '(' . $cgi->param('date1') . ") $query";
136 # ---------------------------------------------------------------------
137 # Nothing below here constitutes a query by itself. If the query value
138 # is still empty up to this point, there is no query. abandon ship.
139 return () unless $query;
141 # The search_scope param comes from a mixed-use dropdown containing depth,
142 # lasso, and location_groups filters.
143 $query .= ' ' . $cgi->param('search_scope') if defined $cgi->param('search_scope');
145 # sort is treated specially, even though it's actually a filter
146 if (defined($cgi->param('sort'))) {
147 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
148 my ($axis, $desc) = split /\./, $cgi->param('sort');
149 $query = "sort($axis) $query" if $axis;
150 if ($desc and not $query =~ /\#descending/) {
151 $query = "#descending $query";
152 } elsif (not $desc) {
153 $query =~ s/\#descending//;
160 my (@naive_query_re, $site);
162 my $org = $ctx->{search_ou};
163 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
164 my $thing = " site(" . $ctx->{get_aou}->($org)->shortname . ")";
167 push @naive_query_re, $thing;
170 my $pref_ou = $ctx->{pref_ou};
171 if (defined($pref_ou) and $pref_ou ne '' and $pref_ou != $org and ($pref_ou ne $ctx->{aou_tree}->()->id) and not $query =~ / pref_ou\(\S+\)/) {
172 my $plib = $ctx->{get_aou}->($pref_ou)->shortname;
173 $query = "pref_ou($plib) $query";
176 if (my $grp = $ctx->{copy_location_group}) {
177 $query = "location_groups($grp) $query";
181 ($site) = ($query =~ /site\(([^\)]+)\)/);
182 $site ||= $ctx->{aou_tree}->()->shortname;
186 if ($query =~ /depth\(\d+\)/) {
188 # depth is encoded in the search query
189 ($depth) = ($query =~ /depth\((\d+)\)/);
193 if (defined $cgi->param('depth')) {
194 $depth = $cgi->param('depth');
196 # no depth specified. match the depth to the search org
197 my ($org) = grep { $_->shortname eq $site } @{$ctx->{aou_list}->()};
198 $depth = $org->ou_type->depth;
200 my $thing = " depth($depth)";
203 push @naive_query_re, $thing;
206 # This gives templates a way to take site() and depth() back out of
207 # query strings when they shouldn't be there (because they're controllable
208 # with other widgets).
209 $ctx->{naive_query_scrub} = sub {
211 $query =~ s/\Q$_\E// foreach (@naive_query_re);
215 $logger->info("tpac: site=$site, depth=$depth, user_query=$user_query, query=$query");
217 return ($user_query, $query, $site, $depth);
220 sub _get_search_limit {
223 # param takes precedence
224 my $limit = $self->cgi->param('limit');
225 return $limit if $limit;
227 if($self->editor->requestor) {
228 $self->timelog("Checking for opac.hits_per_page preference");
229 # See if the user has a hit count preference
230 my $lset = $self->editor->search_actor_user_setting({
231 usr => $self->editor->requestor->id,
232 name => 'opac.hits_per_page'
234 $self->timelog("Got opac.hits_per_page preference");
235 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
241 sub tag_circed_items {
243 my $e = $self->editor;
245 $self->timelog("Tag circed items?");
246 return 0 unless $e->requestor;
247 $self->timelog("Checking for opac.search.tag_circulated_items");
248 return 0 unless $self->ctx->{get_org_setting}->(
249 $e->requestor->home_ou,
250 'opac.search.tag_circulated_items');
252 # user has to be opted-in to circ history in some capacity
253 $self->timelog("Checking for history.circ.retention_*");
254 my $sets = $e->search_actor_user_setting({
255 usr => $e->requestor->id,
257 'history.circ.retention_age',
258 'history.circ.retention_start'
262 $self->timelog("Return from checking for history.circ.retention_*");
264 return 0 unless @$sets;
269 # This only loads the bookbag itself (in support of a record results page)
270 # if a "bookbag" CGI parameter is specified and if the bookbag is public
271 # or owned by the logged-in user (if any). Bookbag notes are fetched
272 # later if applicable.
273 sub load_rresults_bookbag {
276 my $bookbag_id = int($self->cgi->param("bookbag") || 0);
277 return if $bookbag_id < 1;
279 my %authz = $self->ctx->{"user"} ?
280 ("-or" => {"pub" => "t", "owner" => $self->ctx->{"user"}->id}) :
283 $self->timelog("Load results bookbag");
284 my $bbag = $self->editor->search_container_biblio_record_entry_bucket(
285 {"id" => $bookbag_id, "btype" => "bookbag", %authz}
287 $self->timelog("Got results bookbag");
290 $self->apache->log->warn(
291 "error from cstore retrieving bookbag $bookbag_id!"
293 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
295 $self->ctx->{"bookbag"} = shift @$bbag;
301 # assumes context has a bookbag we're already authorized to look at, and
302 # a list of rec_ids, reasonably sized (from paged search).
303 sub load_rresults_bookbag_item_notes {
304 my ($self, $rec_ids) = @_;
306 $self->timelog("Load results bookbag item notes");
307 my $items_with_notes =
308 $self->editor->search_container_biblio_record_entry_bucket_item([
309 {"target_biblio_record_entry" => $rec_ids,
310 "bucket" => $self->ctx->{"bookbag"}->id},
311 {"flesh" => 1, "flesh_fields" => {"cbrebi" => ["notes"]},
312 "order_by" => {"cbrebi" => ["id"]}}
314 $self->timelog("Got results bookbag item notes");
316 if (!$items_with_notes) {
317 $self->apache->log->warn("error from cstore retrieving cbrebi objects");
318 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
321 $self->ctx->{"bookbag_items_by_bre_id"} = +{
322 map { $_->target_biblio_record_entry => $_ } @$items_with_notes
328 # $filter -- CCVM filter expression (see also composite attr def)
329 sub recs_from_metarecord {
330 my ($self, $mr_id, $org, $depth, $filter) = @_;
331 $self->timelog("Getting metarecords to records");
333 # in the context of searching, we include all records which
334 # have an opac-visible copy.
335 my $bre_ids = $U->simplereq(
337 'open-ils.circ.holds.metarecord.filtered_records',
338 $mr_id, $filter, $org, $depth, 1);
340 $self->timelog("Got metarecords to records");
349 # records : list of bre's and copy-count objects
353 my $internal = $args{internal};
354 my $cgi = $self->cgi;
355 my $ctx = $self->ctx;
356 my $e = $self->editor;
358 # 1. param->metarecord : view constituent bib records for a metarecord
359 # 2. param->modifier=metabib : perform a metarecord search
360 my $metarecord = $ctx->{metarecord} = $cgi->param('metarecord');
361 my @mods = $cgi->param('modifier');
362 my $is_meta = (@mods and grep {$_ eq 'metabib'} @mods and !$metarecord);
363 my $id_key = $is_meta ? 'mmr_id' : 'bre_id';
365 # find the last record in the set, then redirect
366 my $find_last = $cgi->param('find_last');
368 $self->timelog("Loading results");
369 # load bookbag metadata, if requested.
370 if (my $bbag_err = $self->load_rresults_bookbag) {
374 $ctx->{page} = 'rresult' unless $internal;
376 $ctx->{records} = [];
377 $ctx->{search_facets} = {};
378 $ctx->{hit_count} = 0;
379 $ctx->{is_meta} = $is_meta;
381 # Special alternative searches here. This could all stand to be cleaner.
382 if ($cgi->param("_special")) {
383 $self->timelog("Calling MARC expert search");
384 return $self->marc_expert_search(%args) if (scalar($cgi->param("tag")) and
385 (!defined $cgi->param("query") or $cgi->param("query") =~ /^\s*$/));
386 $self->timelog("Calling item barcode search");
387 return $self->item_barcode_shortcut if (
388 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode") and not $internal
390 $self->timelog("Calling call number browse");
391 return $self->call_number_browse_standalone if (
392 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
396 $self->timelog("Getting search parameters");
397 my $page = $cgi->param('page') || 0;
398 my @facets = $cgi->param('facet');
399 my $limit = $self->_get_search_limit;
400 $ctx->{search_ou} = $self->_get_search_lib();
401 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
402 my $offset = $page * $limit;
404 my $tag_circs = $self->tag_circed_items;
405 $self->timelog("Got search parameters");
407 $ctx->{page_size} = $limit;
408 $ctx->{search_page} = $page;
410 # fetch this page plus the first hit from the next page
412 $limit = $offset + $limit + 1;
416 my ($user_query, $query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
418 $self->get_staff_search_settings;
420 if (!$find_last and $ctx->{staff_saved_search_size}) {
421 my ($key, $list) = $self->staff_save_search($query);
423 $self->apache->headers_out->add(
424 "Set-Cookie" => $self->cgi->cookie(
425 -name => (ref $self)->COOKIE_ANON_CACHE,
427 -value => ($key || ''),
428 -expires => ($key ? undef : "-1h")
431 $ctx->{saved_searches} = $list;
434 # Limit and offset will stay here. Everything else should be part of
435 # the query string, not special args.
436 my $args = {'limit' => $limit, 'offset' => $offset};
438 return Apache2::Const::OK unless $query;
441 $args->{tag_circulated_records} = 1;
442 $args->{authtoken} = $self->editor->authtoken;
444 $args->{from_metarecord} = $metarecord if $metarecord;
446 # Stuff these into the TT context so that templates can use them in redrawing forms
447 $ctx->{user_query} = $user_query;
448 $ctx->{processed_search_query} = $query;
450 $query = "$_ $query" for @facets;
452 my $ltag = $is_meta ? '[mmr search]' : '[bre search]';
453 $logger->activity("EGWeb: $ltag $query");
457 my $method = 'open-ils.search.biblio.multiclass.query';
458 $method .= '.staff' if $ctx->{is_staff};
459 $method =~ s/biblio/metabib/ if $is_meta;
461 my $ses = OpenSRF::AppSession->create('open-ils.search');
463 $self->timelog("Firing off the multiclass query");
464 my $req = $ses->request($method, $args, $query, 1);
465 $results = $req->gather(1);
466 $self->timelog("Returned from the multiclass query");
470 $logger->error("multiclass search error: $err");
471 $results = {count => 0, ids => []};
474 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
476 $ctx->{ids} = $rec_ids;
477 $ctx->{hit_count} = $results->{count};
478 $ctx->{superpage} = $results->{superpage};
479 $ctx->{superpage_size} = $results->{superpage_size};
480 $ctx->{pagable_limit} = $results->{core_limit};
481 $ctx->{query_struct} = $results->{global_summary}{query_struct};
482 $logger->debug('query struct: '. Dumper($ctx->{query_struct}));
483 $ctx->{canonicalized_query} = $results->{global_summary}{canonicalized_query};
484 $ctx->{search_summary} = $results->{global_summary};
487 # redirect to the record detail page for the last record in the results
488 my $rec_id = pop @$rec_ids;
489 $cgi->delete('find_last');
490 my $url = $cgi->url(-full => 1, -path => 1, -query => 1);
491 # TODO: metarecord => /rresults?metarecord=$mmr_id
492 $url =~ s|/results|/record/$rec_id|;
493 return $self->generic_redirect($url);
496 return Apache2::Const::OK if @$rec_ids == 0 or $internal;
498 $self->load_rresults_bookbag_item_notes($rec_ids) if $ctx->{bookbag};
500 my $fetch_recs = $rec_ids;
502 $self->timelog("Calling get_records_and_facets()");
503 my ($facets, @data) = $self->get_records_and_facets(
504 $fetch_recs, $results->{facet_key},
506 flesh => '{holdings_xml,mra,acp,acnp,acns,bmp,cbs}',
508 metarecord => $is_meta,
510 pref_lib => $ctx->{pref_ou},
513 $self->timelog("Returned from get_records_and_facets()");
515 if ($page == 0 and @$rec_ids == 1) {
518 # if the MR has a single constituent record, it will
519 # be in array position 4 of the result blob.
520 # otherwise, we don't want to redirect anyway.
521 my $bre_id = $results->{ids}->[0]->[4];
522 $stat = $self->check_1hit_redirect([$bre_id]) if $bre_id;
524 my $stat = $self->check_1hit_redirect($rec_ids);
526 return $stat if $stat;
529 # load temporary_list settings for user and ou:
530 $self->_load_lists_and_settings if ($ctx->{user});
533 # shove recs into context in search results order
534 for my $rec_id (@$rec_ids) {
535 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
536 push(@{$ctx->{records}}, $rec);
540 my $method = 'open-ils.search.biblio.multiclass.query';
541 $method .= '.staff' if $ctx->{is_staff};
542 my $ses = OpenSRF::AppSession->create('open-ils.search');
543 $self->timelog("Firing off the multiclass query: ". $rec_id);
544 $args->{from_metarecord} = $rec_id;
545 # offset of main search does not apply to the MR
547 my $save_offset = $args->{offset};
549 $mr_contents{$rec_id} = $ses->request($method, $args, $query, 1);
550 $args->{offset} = $save_offset;
553 $logger->error("multiclass search error: $err");
558 # above we fire all searches in parallel, now we collect the results
560 for my $rec_id (@$rec_ids) {
561 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
563 if ($mr_contents{$rec_id}) {
564 $meta_results = $mr_contents{$rec_id}->gather(1);
565 $self->timelog("Returned from the multiclass query: ". $rec_id);
567 $meta_results = {count => 0, ids => []};
569 my $meta_rec_ids = [map { $_->[0] } @{$meta_results->{ids}}];
570 $rec->{mr_constituent_count} = $meta_results->{count};
571 $rec->{mr_constituent_ids} = $meta_rec_ids;
575 my $course_module_opt_in = 0;
576 if ($ctx->{get_org_setting}->($self->_get_search_lib, "circ.course_materials_opt_in")) {
577 $course_module_opt_in = 1;
580 for my $rec (@{$ctx->{records}}) {
581 my ($res_rec) = grep { $_->[0] == $rec->{$id_key} } @{$results->{ids}};
582 $rec->{badges} = [split(',', $res_rec->[1])] if $res_rec->[1];
583 $rec->{popularity} = $res_rec->[2];
584 if ($course_module_opt_in) {
585 $rec->{course_materials} = $U->simplereq(
587 'open-ils.courses.course_materials.retrieve.atomic',
588 {record => $rec->{id}}
591 for my $material (@{$rec->{course_materials}}) {
592 $course_ids{$material->course} = 1;
595 $rec->{courses} = $U->simplereq(
597 'open-ils.courses.courses.retrieve',
602 # index 3 (5 for MR) in the per-record result array is a boolean which
603 # indicates whether the record in question is in the users
604 # accessible circ history list
605 my $index = $is_meta ? 5 : 3;
606 $rec->{user_circulated} = 1 if $res_rec->[$index];
611 $ctx->{search_facets} = $facets;
613 return Apache2::Const::OK;
616 # If the calling search results in 1 record and the client
617 # is configured to do so, redirect the search results to
618 # the record details page.
619 sub check_1hit_redirect {
620 my ($self, $rec_ids) = @_;
621 my $ctx = $self->ctx;
623 return undef unless $rec_ids and @$rec_ids == 1;
627 $self->timelog("Checking whether to jump to details on a single hit");
628 if ($ctx->{is_staff}) {
629 $sname = 'opac.staff.jump_to_details_on_single_hit';
630 $org = $ctx->{user}->ws_ou;
633 $sname = 'opac.patron.jump_to_details_on_single_hit';
634 $org = $self->_get_search_lib();
637 $self->timelog("Return from checking whether to jump to details on a single hit");
640 $self->ctx->{get_org_setting}->($org, $sname);
642 my $base_url = sprintf(
643 '%s://%s%s/record/%s',
645 $self->ctx->{hostname},
646 $self->ctx->{opac_root},
650 # If we get here from the same record detail page to which we
651 # now wish to redirect, do not perform the redirect. This
652 # approach seems to work well, with the rare exception of
653 # performing a new search directly from the detail page that
654 # happens to result in the same single hit. In this case, the
655 # user will be left on the search results page. This could be
656 # overcome w/ additional CGI, etc., but I'm not sure it's necessary.
657 if (my $referer = $ctx->{referer}) {
658 $referer =~ s/([^?]*).*/$1/g;
659 return undef if $base_url eq $referer;
662 return $self->generic_redirect($base_url . '?' . $self->cgi->query_string);
665 # Searching by barcode is a special search that does /not/ respect any other
666 # of the usual search parameters, not even the ones for sorting and paging!
667 sub item_barcode_shortcut {
670 $self->timelog("Searching for item_barcode");
671 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
672 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
673 my $rec_ids = $search->request(
674 $method, $self->cgi->param("query")
677 $self->timelog("Finished searching for item_barcode");
679 if (ref $rec_ids ne 'ARRAY') {
681 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
685 if (defined $U->event_code($rec_ids)) {
686 $self->apache->log->warn(
687 "$method returned event: " . $U->event_code($rec_ids)
690 $self->apache->log->warn(
691 "$method returned something unexpected: $rec_ids"
694 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
698 $self->timelog("Calling get_records_and_facets() for item_barcode");
699 my ($facets, @data) = $self->get_records_and_facets(
700 $rec_ids, undef, {flesh => "{holdings_xml,mra,acnp,acns,bmp,cbs}"}
702 $self->timelog("Returned from calling get_records_and_facets() for item_barcode");
704 my $stat = $self->check_1hit_redirect($rec_ids);
705 return $stat if $stat;
707 $self->ctx->{records} = [@data];
708 $self->ctx->{search_facets} = {};
709 $self->ctx->{hit_count} = scalar @data;
710 $self->ctx->{page_size} = $self->ctx->{hit_count};
711 # load temporary_list settings for user and ou:
712 $self->_load_lists_and_settings if ($self->ctx->{user});
714 return Apache2::Const::OK;
716 $self->apache->log->warn("couldn't connect to open-ils.search");
717 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
721 # like item_barcode_search, this can't take all the usual search params, but
722 # this one will at least do site, limit and page
723 sub marc_expert_search {
724 my ($self, %args) = @_;
726 my @tags = $self->cgi->param("tag");
727 my @subfields = $self->cgi->param("subfield");
728 my @terms = $self->cgi->param("term");
731 for (my $i = 0; $i < scalar @tags; $i++) {
732 next if ($tags[$i] eq "" || $terms[$i] eq "");
733 $subfields[$i] = '_' unless $subfields[$i];
735 "term" => $terms[$i],
736 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
740 $logger->info("query for expert search: " . Dumper($query));
742 $self->timelog("Getting search parameters");
743 # loc, limit and offset
744 my $page = $self->cgi->param("page") || 0;
745 my $limit = $self->_get_search_limit;
746 $self->ctx->{search_ou} = $self->_get_search_lib();
747 $self->ctx->{pref_ou} = $self->_get_pref_lib();
748 my $offset = $page * $limit;
749 $self->timelog("Got search parameters");
751 $self->ctx->{records} = [];
752 $self->ctx->{search_facets} = {};
753 $self->ctx->{page_size} = $limit;
754 $self->ctx->{hit_count} = 0;
755 $self->ctx->{ids} = [];
756 $self->ctx->{search_page} = $page;
759 return Apache2::Const::OK if @$query == 0;
761 if ($args{internal}) {
762 $limit = $offset + $limit + 1;
766 $self->timelog("Searching for MARC expert");
767 my $method = 'open-ils.search.biblio.marc';
768 $method .= '.staff' if $self->ctx->{is_staff};
770 my $ses = OpenSRF::AppSession->create('open-ils.search');
772 #when a sort variable is passed in we need to add its value to the arguments
773 my $arghash = {searches => $query, org_unit => $self->ctx->{search_ou}};
774 if (defined $self->cgi->param("sort")) {
775 my ($sort, $sort_dir) = split /\./, $self->cgi->param('sort');
776 $arghash = {%$arghash, sort => "$sort"};
777 if (!defined $sort_dir) {
778 $arghash = {%$arghash, sort_dir => "ASC"};
782 #add the offset and limit to the argash, so we can go past 100 results
783 #if offset and limit are not in the args then they default to 0 and 100
784 #respectively in biblio_multi_search_full_rec, which limits the results to 100
786 $arghash = {%$arghash, offset => $offset, limit => $limit};
788 my $req = $ses->request(
793 my $resp = $req->recv($timeout);
794 my $results = $resp ? $resp->content : undef;
796 $self->timelog("Got our MARC expert results");
798 if (defined $U->event_code($results)) {
799 $self->apache->log->warn(
800 "open-ils.search.biblio.marc returned event: " .
801 $U->event_code($results)
803 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
806 $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
807 $self->ctx->{hit_count} = $results->{count};
809 return Apache2::Const::OK if @{$self->ctx->{ids}} == 0 or $args{internal};
812 my $stat = $self->check_1hit_redirect($self->ctx->{ids});
813 return $stat if $stat;
816 $self->timelog("Calling get_records_and_facets() for MARC expert");
817 my ($facets, @data) = $self->get_records_and_facets(
818 $self->ctx->{ids}, undef, {
819 flesh => "{holdings_xml,mra,acnp,acns,cbs}",
820 pref_lib => $self->ctx->{pref_ou},
823 $self->timelog("Returned from calling get_records_and_facets() for MARC expert");
825 # load temporary_list settings for user and ou:
826 $self->_load_lists_and_settings if ($self->ctx->{user});
828 $self->ctx->{records} = [@data];
830 return Apache2::Const::OK;
833 sub call_number_browse_standalone {
836 if (my $cnfrag = $self->cgi->param("query")) {
838 '%s://%s%s/cnbrowse?cn=%s',
840 $self->ctx->{hostname},
841 $self->ctx->{opac_root},
842 $cnfrag # XXX some kind of escaping needed here?
844 $url .= '&locg=' . $self->_get_search_lib() if ($self->_get_search_lib());
845 return $self->generic_redirect($url);
847 return $self->generic_redirect; # return to search page
854 $self->prepare_browse_call_numbers();
856 return Apache2::Const::OK;
859 sub get_staff_search_settings {
862 unless ($self->ctx->{is_staff}) {
863 $self->ctx->{staff_saved_search_size} = 0;
867 $self->timelog("Getting staff search size");
868 my $sss_size = $self->ctx->{get_org_setting}->(
869 $self->ctx->{physical_loc} || $self->ctx->{aou_tree}->()->id,
870 "opac.staff_saved_search.size",
872 $self->timelog("Got staff search size");
874 # Sic: 0 is 0 (off), but undefined is 10.
875 $sss_size = 10 unless defined $sss_size;
877 $self->ctx->{staff_saved_search_size} = $sss_size;
880 sub staff_load_searches {
883 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
887 $self->timelog("Getting anon_cache value");
888 $list = $U->simplereq(
890 "open-ils.actor.anon_cache.get_value",
891 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
893 $self->timelog("Got anon_cache value");
901 return ($cache_key, $list);
904 sub staff_save_search {
905 my ($self, $query) = @_;
907 my $sss_size = $self->ctx->{staff_saved_search_size};
908 return unless $sss_size > 0;
910 my ($cache_key, $list) = $self->staff_load_searches;
911 my %already = ( map { $_ => 1 } @$list );
913 unshift @$list, $query unless $already{$query};
915 splice @$list, $sss_size if scalar @$list > $sss_size;
917 $self->timelog("Setting anon_cache value");
918 $cache_key = $U->simplereq(
920 "open-ils.actor.anon_cache.set_value",
921 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
923 $self->timelog("Set anon_cache value");
925 return ($cache_key, $list);