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 # sort is treated specially, even though it's actually a filter
142 if (defined($cgi->param('sort'))) {
143 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
144 my ($axis, $desc) = split /\./, $cgi->param('sort');
145 $query = "sort($axis) $query" if $axis;
146 if ($desc and not $query =~ /\#descending/) {
147 $query = "#descending $query";
148 } elsif (not $desc) {
149 $query =~ s/\#descending//;
156 my (@naive_query_re, $site);
158 my $org = $ctx->{search_ou};
159 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
160 my $thing = " site(" . $ctx->{get_aou}->($org)->shortname . ")";
163 push @naive_query_re, $thing;
166 my $pref_ou = $ctx->{pref_ou};
167 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+\)/) {
168 my $plib = $ctx->{get_aou}->($pref_ou)->shortname;
169 $query = "pref_ou($plib) $query";
172 if (my $grp = $ctx->{copy_location_group}) {
173 $query = "location_groups($grp) $query";
177 ($site) = ($query =~ /site\(([^\)]+)\)/);
178 $site ||= $ctx->{aou_tree}->()->shortname;
182 if ($query =~ /depth\(\d+\)/) {
184 # depth is encoded in the search query
185 ($depth) = ($query =~ /depth\((\d+)\)/);
189 if (defined $cgi->param('depth')) {
190 $depth = $cgi->param('depth');
192 # no depth specified. match the depth to the search org
193 my ($org) = grep { $_->shortname eq $site } @{$ctx->{aou_list}->()};
194 $depth = $org->ou_type->depth;
196 my $thing = " depth($depth)";
199 push @naive_query_re, $thing;
202 # This gives templates a way to take site() and depth() back out of
203 # query strings when they shouldn't be there (because they're controllable
204 # with other widgets).
205 $ctx->{naive_query_scrub} = sub {
207 $query =~ s/\Q$_\E// foreach (@naive_query_re);
211 $logger->info("tpac: site=$site, depth=$depth, user_query=$user_query, query=$query");
213 return ($user_query, $query, $site, $depth);
216 sub _get_search_limit {
219 # param takes precedence
220 my $limit = $self->cgi->param('limit');
221 return $limit if $limit;
223 if($self->editor->requestor) {
224 $self->timelog("Checking for opac.hits_per_page preference");
225 # See if the user has a hit count preference
226 my $lset = $self->editor->search_actor_user_setting({
227 usr => $self->editor->requestor->id,
228 name => 'opac.hits_per_page'
230 $self->timelog("Got opac.hits_per_page preference");
231 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
237 sub tag_circed_items {
239 my $e = $self->editor;
241 $self->timelog("Tag circed items?");
242 return 0 unless $e->requestor;
243 $self->timelog("Checking for opac.search.tag_circulated_items");
244 return 0 unless $self->ctx->{get_org_setting}->(
245 $e->requestor->home_ou,
246 'opac.search.tag_circulated_items');
248 # user has to be opted-in to circ history in some capacity
249 $self->timelog("Checking for history.circ.retention_*");
250 my $sets = $e->search_actor_user_setting({
251 usr => $e->requestor->id,
253 'history.circ.retention_age',
254 'history.circ.retention_start'
258 $self->timelog("Return from checking for history.circ.retention_*");
260 return 0 unless @$sets;
265 # This only loads the bookbag itself (in support of a record results page)
266 # if a "bookbag" CGI parameter is specified and if the bookbag is public
267 # or owned by the logged-in user (if any). Bookbag notes are fetched
268 # later if applicable.
269 sub load_rresults_bookbag {
272 my $bookbag_id = int($self->cgi->param("bookbag") || 0);
273 return if $bookbag_id < 1;
275 my %authz = $self->ctx->{"user"} ?
276 ("-or" => {"pub" => "t", "owner" => $self->ctx->{"user"}->id}) :
279 $self->timelog("Load results bookbag");
280 my $bbag = $self->editor->search_container_biblio_record_entry_bucket(
281 {"id" => $bookbag_id, "btype" => "bookbag", %authz}
283 $self->timelog("Got results bookbag");
286 $self->apache->log->warn(
287 "error from cstore retrieving bookbag $bookbag_id!"
289 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
291 $self->ctx->{"bookbag"} = shift @$bbag;
297 # assumes context has a bookbag we're already authorized to look at, and
298 # a list of rec_ids, reasonably sized (from paged search).
299 sub load_rresults_bookbag_item_notes {
300 my ($self, $rec_ids) = @_;
302 $self->timelog("Load results bookbag item notes");
303 my $items_with_notes =
304 $self->editor->search_container_biblio_record_entry_bucket_item([
305 {"target_biblio_record_entry" => $rec_ids,
306 "bucket" => $self->ctx->{"bookbag"}->id},
307 {"flesh" => 1, "flesh_fields" => {"cbrebi" => ["notes"]},
308 "order_by" => {"cbrebi" => ["id"]}}
310 $self->timelog("Got results bookbag item notes");
312 if (!$items_with_notes) {
313 $self->apache->log->warn("error from cstore retrieving cbrebi objects");
314 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
317 $self->ctx->{"bookbag_items_by_bre_id"} = +{
318 map { $_->target_biblio_record_entry => $_ } @$items_with_notes
324 # $filter -- CCVM filter expression (see also composite attr def)
325 sub recs_from_metarecord {
326 my ($self, $mr_id, $org, $depth, $filter) = @_;
327 $self->timelog("Getting metarecords to records");
329 # in the context of searching, we include all records which
330 # have an opac-visible copy.
331 my $bre_ids = $U->simplereq(
333 'open-ils.circ.holds.metarecord.filtered_records',
334 $mr_id, $filter, $org, $depth, 1);
336 $self->timelog("Got metarecords to records");
345 # records : list of bre's and copy-count objects
349 my $internal = $args{internal};
350 my $cgi = $self->cgi;
351 my $ctx = $self->ctx;
352 my $e = $self->editor;
354 # 1. param->metarecord : view constituent bib records for a metarecord
355 # 2. param->modifier=metabib : perform a metarecord search
356 my $metarecord = $ctx->{metarecord} = $cgi->param('metarecord');
357 my @mods = $cgi->param('modifier');
358 my $is_meta = (@mods and grep {$_ eq 'metabib'} @mods and !$metarecord);
359 my $id_key = $is_meta ? 'mmr_id' : 'bre_id';
361 # find the last record in the set, then redirect
362 my $find_last = $cgi->param('find_last');
364 $self->timelog("Loading results");
365 # load bookbag metadata, if requested.
366 if (my $bbag_err = $self->load_rresults_bookbag) {
370 $ctx->{page} = 'rresult' unless $internal;
372 $ctx->{records} = [];
373 $ctx->{search_facets} = {};
374 $ctx->{hit_count} = 0;
375 $ctx->{is_meta} = $is_meta;
377 # Special alternative searches here. This could all stand to be cleaner.
378 if ($cgi->param("_special")) {
379 $self->timelog("Calling MARC expert search");
380 return $self->marc_expert_search(%args) if (scalar($cgi->param("tag")) and
381 (!defined $cgi->param("query") or $cgi->param("query") =~ /^\s*$/));
382 $self->timelog("Calling item barcode search");
383 return $self->item_barcode_shortcut if (
384 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode") and not $internal
386 $self->timelog("Calling call number browse");
387 return $self->call_number_browse_standalone if (
388 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
392 $self->timelog("Getting search parameters");
393 my $page = $cgi->param('page') || 0;
394 my @facets = $cgi->param('facet');
395 my $limit = $self->_get_search_limit;
396 $ctx->{search_ou} = $self->_get_search_lib();
397 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
398 my $offset = $page * $limit;
400 my $tag_circs = $self->tag_circed_items;
401 $self->timelog("Got search parameters");
403 $ctx->{page_size} = $limit;
404 $ctx->{search_page} = $page;
406 # fetch this page plus the first hit from the next page
408 $limit = $offset + $limit + 1;
412 my ($user_query, $query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
414 $self->get_staff_search_settings;
416 if (!$find_last and $ctx->{staff_saved_search_size}) {
417 my ($key, $list) = $self->staff_save_search($query);
419 $self->apache->headers_out->add(
420 "Set-Cookie" => $self->cgi->cookie(
421 -name => (ref $self)->COOKIE_ANON_CACHE,
423 -value => ($key || ''),
424 -expires => ($key ? undef : "-1h")
427 $ctx->{saved_searches} = $list;
430 # Limit and offset will stay here. Everything else should be part of
431 # the query string, not special args.
432 my $args = {'limit' => $limit, 'offset' => $offset};
434 return Apache2::Const::OK unless $query;
437 $args->{tag_circulated_records} = 1;
438 $args->{authtoken} = $self->editor->authtoken;
440 $args->{from_metarecord} = $metarecord if $metarecord;
442 # Stuff these into the TT context so that templates can use them in redrawing forms
443 $ctx->{user_query} = $user_query;
444 $ctx->{processed_search_query} = $query;
446 $query = "$_ $query" for @facets;
448 my $ltag = $is_meta ? '[mmr search]' : '[bre search]';
449 $logger->activity("EGWeb: $ltag $query");
453 my $method = 'open-ils.search.biblio.multiclass.query';
454 $method .= '.staff' if $ctx->{is_staff};
455 $method =~ s/biblio/metabib/ if $is_meta;
457 my $ses = OpenSRF::AppSession->create('open-ils.search');
459 $self->timelog("Firing off the multiclass query");
460 my $req = $ses->request($method, $args, $query, 1);
461 $results = $req->gather(1);
462 $self->timelog("Returned from the multiclass query");
466 $logger->error("multiclass search error: $err");
467 $results = {count => 0, ids => []};
470 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
472 $ctx->{ids} = $rec_ids;
473 $ctx->{hit_count} = $results->{count};
474 $ctx->{superpage} = $results->{superpage};
475 $ctx->{superpage_size} = $results->{superpage_size};
476 $ctx->{pagable_limit} = $results->{core_limit};
477 $ctx->{query_struct} = $results->{global_summary}{query_struct};
478 $logger->debug('query struct: '. Dumper($ctx->{query_struct}));
479 $ctx->{canonicalized_query} = $results->{global_summary}{canonicalized_query};
480 $ctx->{search_summary} = $results->{global_summary};
483 # redirect to the record detail page for the last record in the results
484 my $rec_id = pop @$rec_ids;
485 $cgi->delete('find_last');
486 my $url = $cgi->url(-full => 1, -path => 1, -query => 1);
487 # TODO: metarecord => /rresults?metarecord=$mmr_id
488 $url =~ s|/results|/record/$rec_id|;
489 return $self->generic_redirect($url);
492 return Apache2::Const::OK if @$rec_ids == 0 or $internal;
494 $self->load_rresults_bookbag_item_notes($rec_ids) if $ctx->{bookbag};
496 my $fetch_recs = $rec_ids;
498 $self->timelog("Calling get_records_and_facets()");
499 my ($facets, @data) = $self->get_records_and_facets(
500 $fetch_recs, $results->{facet_key},
502 flesh => '{holdings_xml,mra,acp,acnp,acns,bmp,cbs}',
504 metarecord => $is_meta,
506 pref_lib => $ctx->{pref_ou},
509 $self->timelog("Returned from get_records_and_facets()");
511 if ($page == 0 and @$rec_ids == 1) {
514 # if the MR has a single constituent record, it will
515 # be in array position 4 of the result blob.
516 # otherwise, we don't want to redirect anyway.
517 my $bre_id = $results->{ids}->[0]->[4];
518 $stat = $self->check_1hit_redirect([$bre_id]) if $bre_id;
520 my $stat = $self->check_1hit_redirect($rec_ids);
522 return $stat if $stat;
525 # load temporary_list settings for user and ou:
526 $self->_load_lists_and_settings if ($ctx->{user});
529 # shove recs into context in search results order
530 for my $rec_id (@$rec_ids) {
531 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
532 push(@{$ctx->{records}}, $rec);
536 my $method = 'open-ils.search.biblio.multiclass.query';
537 $method .= '.staff' if $ctx->{is_staff};
538 my $ses = OpenSRF::AppSession->create('open-ils.search');
539 $self->timelog("Firing off the multiclass query: ". $rec_id);
540 $args->{from_metarecord} = $rec_id;
541 # offset of main search does not apply to the MR
543 my $save_offset = $args->{offset};
545 $mr_contents{$rec_id} = $ses->request($method, $args, $query, 1);
546 $args->{offset} = $save_offset;
549 $logger->error("multiclass search error: $err");
554 # above we fire all searches in parallel, now we collect the results
556 for my $rec_id (@$rec_ids) {
557 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
559 if ($mr_contents{$rec_id}) {
560 $meta_results = $mr_contents{$rec_id}->gather(1);
561 $self->timelog("Returned from the multiclass query: ". $rec_id);
563 $meta_results = {count => 0, ids => []};
565 my $meta_rec_ids = [map { $_->[0] } @{$meta_results->{ids}}];
566 $rec->{mr_constituent_count} = $meta_results->{count};
567 $rec->{mr_constituent_ids} = $meta_rec_ids;
571 my $course_module_opt_in = 0;
572 if ($ctx->{get_org_setting}->($self->_get_search_lib, "circ.course_materials_opt_in")) {
573 $course_module_opt_in = 1;
576 for my $rec (@{$ctx->{records}}) {
577 my ($res_rec) = grep { $_->[0] == $rec->{$id_key} } @{$results->{ids}};
578 $rec->{badges} = [split(',', $res_rec->[1])] if $res_rec->[1];
579 $rec->{popularity} = $res_rec->[2];
580 if ($course_module_opt_in) {
581 $rec->{course_materials} = $U->simplereq(
583 'open-ils.courses.course_materials.retrieve.atomic',
584 {record => $rec->{id}}
587 for my $material (@{$rec->{course_materials}}) {
588 $course_ids{$material->course} = 1;
591 $rec->{courses} = $U->simplereq(
593 'open-ils.courses.courses.retrieve',
598 # index 3 (5 for MR) in the per-record result array is a boolean which
599 # indicates whether the record in question is in the users
600 # accessible circ history list
601 my $index = $is_meta ? 5 : 3;
602 $rec->{user_circulated} = 1 if $res_rec->[$index];
607 $ctx->{search_facets} = $facets;
609 return Apache2::Const::OK;
612 # If the calling search results in 1 record and the client
613 # is configured to do so, redirect the search results to
614 # the record details page.
615 sub check_1hit_redirect {
616 my ($self, $rec_ids) = @_;
617 my $ctx = $self->ctx;
619 return undef unless $rec_ids and @$rec_ids == 1;
623 $self->timelog("Checking whether to jump to details on a single hit");
624 if ($ctx->{is_staff}) {
625 $sname = 'opac.staff.jump_to_details_on_single_hit';
626 $org = $ctx->{user}->ws_ou;
629 $sname = 'opac.patron.jump_to_details_on_single_hit';
630 $org = $self->_get_search_lib();
633 $self->timelog("Return from checking whether to jump to details on a single hit");
636 $self->ctx->{get_org_setting}->($org, $sname);
638 my $base_url = sprintf(
639 '%s://%s%s/record/%s',
641 $self->ctx->{hostname},
642 $self->ctx->{opac_root},
646 # If we get here from the same record detail page to which we
647 # now wish to redirect, do not perform the redirect. This
648 # approach seems to work well, with the rare exception of
649 # performing a new search directly from the detail page that
650 # happens to result in the same single hit. In this case, the
651 # user will be left on the search results page. This could be
652 # overcome w/ additional CGI, etc., but I'm not sure it's necessary.
653 if (my $referer = $ctx->{referer}) {
654 $referer =~ s/([^?]*).*/$1/g;
655 return undef if $base_url eq $referer;
658 return $self->generic_redirect($base_url . '?' . $self->cgi->query_string);
661 # Searching by barcode is a special search that does /not/ respect any other
662 # of the usual search parameters, not even the ones for sorting and paging!
663 sub item_barcode_shortcut {
666 $self->timelog("Searching for item_barcode");
667 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
668 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
669 my $rec_ids = $search->request(
670 $method, $self->cgi->param("query")
673 $self->timelog("Finished searching for item_barcode");
675 if (ref $rec_ids ne 'ARRAY') {
677 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
681 if (defined $U->event_code($rec_ids)) {
682 $self->apache->log->warn(
683 "$method returned event: " . $U->event_code($rec_ids)
686 $self->apache->log->warn(
687 "$method returned something unexpected: $rec_ids"
690 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
694 $self->timelog("Calling get_records_and_facets() for item_barcode");
695 my ($facets, @data) = $self->get_records_and_facets(
696 $rec_ids, undef, {flesh => "{holdings_xml,mra,acnp,acns,bmp,cbs}"}
698 $self->timelog("Returned from calling get_records_and_facets() for item_barcode");
700 my $stat = $self->check_1hit_redirect($rec_ids);
701 return $stat if $stat;
703 $self->ctx->{records} = [@data];
704 $self->ctx->{search_facets} = {};
705 $self->ctx->{hit_count} = scalar @data;
706 $self->ctx->{page_size} = $self->ctx->{hit_count};
707 # load temporary_list settings for user and ou:
708 $self->_load_lists_and_settings if ($self->ctx->{user});
710 return Apache2::Const::OK;
712 $self->apache->log->warn("couldn't connect to open-ils.search");
713 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
717 # like item_barcode_search, this can't take all the usual search params, but
718 # this one will at least do site, limit and page
719 sub marc_expert_search {
720 my ($self, %args) = @_;
722 my @tags = $self->cgi->param("tag");
723 my @subfields = $self->cgi->param("subfield");
724 my @terms = $self->cgi->param("term");
727 for (my $i = 0; $i < scalar @tags; $i++) {
728 next if ($tags[$i] eq "" || $terms[$i] eq "");
729 $subfields[$i] = '_' unless $subfields[$i];
731 "term" => $terms[$i],
732 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
736 $logger->info("query for expert search: " . Dumper($query));
738 $self->timelog("Getting search parameters");
739 # loc, limit and offset
740 my $page = $self->cgi->param("page") || 0;
741 my $limit = $self->_get_search_limit;
742 $self->ctx->{search_ou} = $self->_get_search_lib();
743 $self->ctx->{pref_ou} = $self->_get_pref_lib();
744 my $offset = $page * $limit;
745 $self->timelog("Got search parameters");
747 $self->ctx->{records} = [];
748 $self->ctx->{search_facets} = {};
749 $self->ctx->{page_size} = $limit;
750 $self->ctx->{hit_count} = 0;
751 $self->ctx->{ids} = [];
752 $self->ctx->{search_page} = $page;
755 return Apache2::Const::OK if @$query == 0;
757 if ($args{internal}) {
758 $limit = $offset + $limit + 1;
762 $self->timelog("Searching for MARC expert");
763 my $method = 'open-ils.search.biblio.marc';
764 $method .= '.staff' if $self->ctx->{is_staff};
766 my $ses = OpenSRF::AppSession->create('open-ils.search');
768 #when a sort variable is passed in we need to add its value to the arguments
769 my $arghash = {searches => $query, org_unit => $self->ctx->{search_ou}};
770 if (defined $self->cgi->param("sort")) {
771 my ($sort, $sort_dir) = split /\./, $self->cgi->param('sort');
772 $arghash = {%$arghash, sort => "$sort"};
773 if (!defined $sort_dir) {
774 $arghash = {%$arghash, sort_dir => "ASC"};
778 #add the offset and limit to the argash, so we can go past 100 results
779 #if offset and limit are not in the args then they default to 0 and 100
780 #respectively in biblio_multi_search_full_rec, which limits the results to 100
782 $arghash = {%$arghash, offset => $offset, limit => $limit};
784 my $req = $ses->request(
789 my $resp = $req->recv($timeout);
790 my $results = $resp ? $resp->content : undef;
792 $self->timelog("Got our MARC expert results");
794 if (defined $U->event_code($results)) {
795 $self->apache->log->warn(
796 "open-ils.search.biblio.marc returned event: " .
797 $U->event_code($results)
799 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
802 $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
803 $self->ctx->{hit_count} = $results->{count};
805 return Apache2::Const::OK if @{$self->ctx->{ids}} == 0 or $args{internal};
808 my $stat = $self->check_1hit_redirect($self->ctx->{ids});
809 return $stat if $stat;
812 $self->timelog("Calling get_records_and_facets() for MARC expert");
813 my ($facets, @data) = $self->get_records_and_facets(
814 $self->ctx->{ids}, undef, {
815 flesh => "{holdings_xml,mra,acnp,acns,cbs}",
816 pref_lib => $self->ctx->{pref_ou},
819 $self->timelog("Returned from calling get_records_and_facets() for MARC expert");
821 # load temporary_list settings for user and ou:
822 $self->_load_lists_and_settings if ($self->ctx->{user});
824 $self->ctx->{records} = [@data];
826 return Apache2::Const::OK;
829 sub call_number_browse_standalone {
832 if (my $cnfrag = $self->cgi->param("query")) {
834 '%s://%s%s/cnbrowse?cn=%s',
836 $self->ctx->{hostname},
837 $self->ctx->{opac_root},
838 $cnfrag # XXX some kind of escaping needed here?
840 $url .= '&locg=' . $self->_get_search_lib() if ($self->_get_search_lib());
841 return $self->generic_redirect($url);
843 return $self->generic_redirect; # return to search page
850 $self->prepare_browse_call_numbers();
852 return Apache2::Const::OK;
855 sub get_staff_search_settings {
858 unless ($self->ctx->{is_staff}) {
859 $self->ctx->{staff_saved_search_size} = 0;
863 $self->timelog("Getting staff search size");
864 my $sss_size = $self->ctx->{get_org_setting}->(
865 $self->ctx->{physical_loc} || $self->ctx->{aou_tree}->()->id,
866 "opac.staff_saved_search.size",
868 $self->timelog("Got staff search size");
870 # Sic: 0 is 0 (off), but undefined is 10.
871 $sss_size = 10 unless defined $sss_size;
873 $self->ctx->{staff_saved_search_size} = $sss_size;
876 sub staff_load_searches {
879 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
883 $self->timelog("Getting anon_cache value");
884 $list = $U->simplereq(
886 "open-ils.actor.anon_cache.get_value",
887 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
889 $self->timelog("Got anon_cache value");
897 return ($cache_key, $list);
900 sub staff_save_search {
901 my ($self, $query) = @_;
903 my $sss_size = $self->ctx->{staff_saved_search_size};
904 return unless $sss_size > 0;
906 my ($cache_key, $list) = $self->staff_load_searches;
907 my %already = ( map { $_ => 1 } @$list );
909 unshift @$list, $query unless $already{$query};
911 splice @$list, $sss_size if scalar @$list > $sss_size;
913 $self->timelog("Setting anon_cache value");
914 $cache_key = $U->simplereq(
916 "open-ils.actor.anon_cache.set_value",
917 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
919 $self->timelog("Set anon_cache value");
921 return ($cache_key, $list);