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';
15 sub _prepare_biblio_search_basics {
18 return scalar($cgi->param('query')) unless scalar($cgi->param('qtype'));
20 # fetch, once per mod_perl backend, the list of valid classes and aliases
21 unless (@_qtype_list) {
22 my $editor = new_editor();
23 my $classes = $editor->retrieve_all_config_metabib_class();
24 my $aliases = $editor->retrieve_all_config_metabib_search_alias();
26 push @_qtype_list, map { $_->name } @$classes;
27 push @_qtype_list, map { $_->alias } @$aliases;
31 my @part_names = qw/qtype contains query bool/;
32 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
35 my @bookplate_queries = ();
36 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
37 my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
39 next unless $query =~ /\S/;
41 # Hack for bookplates; "bookplate" is not a real search
42 # class, so grabbing them out of the advanced search query
43 # params to convert to a copy_tag(*,...) filter later
44 if ($qtype eq 'bookplate') {
45 $query =~ s/[)(]/ /g; # don't break on 'foo(bar)baz'
46 push @bookplate_queries, $query;
50 # Hack for journal title
52 if ($qtype eq 'jtitle') {
57 # This restricts qtype to classes, aliases, and field lists (approximately)
58 next unless grep { $qtype =~ /^$_(?:\|\w+)*$/ } @_qtype_list;
60 # This stuff probably will need refined or rethought to better handle
61 # the weird things Real Users will surely type in.
62 $contains = "" unless defined $contains; # silence warning
63 if ($contains eq 'nocontains') {
65 $query = ('"' . $query . '"') if index $query, ' ';
66 $query = '-' . $query;
67 } elsif ($contains eq 'phrase') {
69 $query = ('"' . $query . '"') if index $query, ' ';
70 } elsif ($contains eq 'exact') {
71 $query =~ s/[\^\$]//g;
72 $query = '^' . $query . '$';
73 } elsif ($contains eq 'starts') {
75 $query =~ s/[\^\$]//g;
76 $query = '^' . $query;
77 $query = ('"' . $query . '"') if index $query, ' ';
80 # Journal title hackery complete
82 $query = "bib_level(s) $query";
85 $query = "$qtype:$query" unless ($query =~ /^$qtype:/ or ($qtype eq 'keyword' and $i == 0));
87 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
88 $full_query = $full_query ? "($full_query $bool $query)" : $query;
91 return $full_query, \@bookplate_queries;
94 sub _prepare_biblio_search {
97 # XXX This will still contain the jtitle hack...
98 my ($user_query, $bookplate_queries) = _prepare_biblio_search_basics($cgi);
100 $bookplate_queries //= [];
101 my $query = $user_query;
102 if (@$bookplate_queries) {
103 $query .= " " . join(" ", map { "copy_tag(*,$_)" } @$bookplate_queries);
104 # hack to handle the case where a bookplate comes from the
106 $user_query = $bookplate_queries->[0] if $user_query eq '';
109 $query .= ' ' . $ctx->{global_search_filter} if $ctx->{global_search_filter};
111 foreach ($cgi->multi_param('modifier')) {
112 # The unless bit is to avoid stacking modifiers.
113 $query = ('#' . $_ . ' ' . $query) unless
114 $query =~ qr/\#\Q$_/ or $_ eq 'metabib';
118 foreach (grep /^fi:/, $cgi->multi_param) {
120 my $term = join(",", $cgi->multi_param($_));
121 $query .= " $1($term)" if length $term;
124 # filter group entries. Entries from like filters are grouped into a single
125 # filter_group_entry() filter (ORed). Each collection is ANDed together.
126 # fg:foo_group=foo_entry_id
127 foreach (grep /^fg:/, $cgi->multi_param) {
129 my $term = join(",", $cgi->multi_param($_));
130 $query = "filter_group_entry($term) $query" if length $term;
133 if ($cgi->param("bookbag")) {
134 $query = "container(bre,bookbag," . int($cgi->param("bookbag")) . ") $query";
137 if ($cgi->param('pubdate') && $cgi->param('date1')) {
138 if ($cgi->param('pubdate') eq 'between') {
139 my $btw = 'between(' . $cgi->param('date1');
140 $btw .= ',' . $cgi->param('date2') if $cgi->param('date2');
142 $query = "$btw $query";
143 } elsif ($cgi->param('pubdate') eq 'is') {
144 $query = 'date1(' . $cgi->param('date1') . ") $query";
146 $query = $cgi->param('pubdate') .
147 '(' . $cgi->param('date1') . ") $query";
151 # ---------------------------------------------------------------------
152 # Nothing below here constitutes a query by itself. If the query value
153 # is still empty up to this point, there is no query. abandon ship.
154 return () unless $query;
156 # The search_scope param comes from a mixed-use dropdown containing depth,
157 # lasso, and location_groups filters.
158 $query .= ' ' . $cgi->param('search_scope') if defined $cgi->param('search_scope');
160 # The search_lasso context param comes from the locg dropdown, like the copy location.
161 $query .= ' lasso(' . $ctx->{search_lasso} .')' if $ctx->{search_lasso};
163 # sort is treated specially, even though it's actually a filter
164 if (defined($cgi->param('sort'))) {
165 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
166 my ($axis, $desc) = split /\./, $cgi->param('sort');
167 $query = "sort($axis) $query" if $axis;
168 if ($desc and not $query =~ /\#descending/) {
169 $query = "#descending $query";
170 } elsif (not $desc) {
171 $query =~ s/\#descending//;
178 my (@naive_query_re, $site);
180 my $org = $ctx->{search_ou};
181 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
182 my $thing = " site(" . $ctx->{get_aou}->($org)->shortname . ")";
185 push @naive_query_re, $thing;
188 my $pref_ou = $ctx->{pref_ou};
189 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+\)/) {
190 my $plib = $ctx->{get_aou}->($pref_ou)->shortname;
191 $query = "pref_ou($plib) $query";
194 if (my $grp = $ctx->{copy_location_group}) {
195 $query = "location_groups($grp) $query";
199 ($site) = ($query =~ /site\(([^\)]+)\)/);
200 $site ||= $ctx->{aou_tree}->()->shortname;
204 if ($query =~ /depth\(\d+\)/) {
206 # depth is encoded in the search query
207 ($depth) = ($query =~ /depth\((\d+)\)/);
211 if (defined $cgi->param('depth')) {
212 $depth = $cgi->param('depth');
214 # no depth specified. match the depth to the search org
215 my ($org) = grep { $_->shortname eq $site } @{$ctx->{aou_list}->()};
216 $depth = $org->ou_type->depth;
218 my $thing = " depth($depth)";
221 push @naive_query_re, $thing;
224 # This gives templates a way to take site() and depth() back out of
225 # query strings when they shouldn't be there (because they're controllable
226 # with other widgets).
227 $ctx->{naive_query_scrub} = sub {
229 $query =~ s/\Q$_\E// foreach (@naive_query_re);
233 $logger->info("tpac: site=$site, depth=$depth, user_query=$user_query, query=$query");
235 return ($user_query, $query, $site, $depth);
238 sub _get_search_limit {
241 # param takes precedence
242 my $limit = $self->cgi->param('limit');
243 return $limit if $limit;
245 if($self->editor->requestor) {
246 $self->timelog("Checking for opac.hits_per_page preference");
247 # See if the user has a hit count preference
248 my $lset = $self->editor->search_actor_user_setting({
249 usr => $self->editor->requestor->id,
250 name => 'opac.hits_per_page'
252 $self->timelog("Got opac.hits_per_page preference");
253 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
259 sub tag_circed_items {
261 my $e = $self->editor;
263 $self->timelog("Tag circed items?");
264 return 0 unless $e->requestor;
265 $self->timelog("Checking for opac.search.tag_circulated_items");
266 return 0 unless $self->ctx->{get_org_setting}->(
267 $e->requestor->home_ou,
268 'opac.search.tag_circulated_items');
270 # user has to be opted-in to circ history in some capacity
271 $self->timelog("Checking for history.circ.retention_*");
272 my $sets = $e->search_actor_user_setting({
273 usr => $e->requestor->id,
275 'history.circ.retention_age',
276 'history.circ.retention_start'
280 $self->timelog("Return from checking for history.circ.retention_*");
282 return 0 unless @$sets;
287 # This only loads the bookbag itself (in support of a record results page)
288 # if a "bookbag" CGI parameter is specified and if the bookbag is public
289 # or owned by the logged-in user (if any). Bookbag notes are fetched
290 # later if applicable.
291 sub load_rresults_bookbag {
294 my $bookbag_id = int($self->cgi->param("bookbag") || 0);
295 return if $bookbag_id < 1;
297 my %authz = $self->ctx->{"user"} ?
298 ("-or" => {"pub" => "t", "owner" => $self->ctx->{"user"}->id}) :
301 $self->timelog("Load results bookbag");
302 my $bbag = $self->editor->search_container_biblio_record_entry_bucket(
303 {"id" => $bookbag_id, "btype" => "bookbag", %authz}
305 $self->timelog("Got results bookbag");
308 $self->apache->log->warn(
309 "error from cstore retrieving bookbag $bookbag_id!"
311 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
313 $self->ctx->{"bookbag"} = shift @$bbag;
319 # assumes context has a bookbag we're already authorized to look at, and
320 # a list of rec_ids, reasonably sized (from paged search).
321 sub load_rresults_bookbag_item_notes {
322 my ($self, $rec_ids) = @_;
324 $self->timelog("Load results bookbag item notes");
325 my $items_with_notes =
326 $self->editor->search_container_biblio_record_entry_bucket_item([
327 {"target_biblio_record_entry" => $rec_ids,
328 "bucket" => $self->ctx->{"bookbag"}->id},
329 {"flesh" => 1, "flesh_fields" => {"cbrebi" => ["notes"]},
330 "order_by" => {"cbrebi" => ["id"]}}
332 $self->timelog("Got results bookbag item notes");
334 if (!$items_with_notes) {
335 $self->apache->log->warn("error from cstore retrieving cbrebi objects");
336 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
339 $self->ctx->{"bookbag_items_by_bre_id"} = +{
340 map { $_->target_biblio_record_entry => $_ } @$items_with_notes
346 # $filter -- CCVM filter expression (see also composite attr def)
347 sub recs_from_metarecord {
348 my ($self, $mr_id, $org, $depth, $filter) = @_;
349 $self->timelog("Getting metarecords to records");
351 # in the context of searching, we include all records which
352 # have an opac-visible copy.
353 my $bre_ids = $U->simplereq(
355 'open-ils.circ.holds.metarecord.filtered_records',
356 $mr_id, $filter, $org, $depth, 1);
358 $self->timelog("Got metarecords to records");
367 # records : list of bre's and copy-count objects
371 my $internal = $args{internal};
372 my $cgi = $self->cgi;
373 my $ctx = $self->ctx;
374 my $e = $self->editor;
376 # 1. param->metarecord : view constituent bib records for a metarecord
377 # 2. param->modifier=metabib : perform a metarecord search
378 my $metarecord = $ctx->{metarecord} = $cgi->param('metarecord');
379 my @mods = $cgi->multi_param('modifier');
380 my $is_meta = (@mods and grep {$_ eq 'metabib'} @mods and !$metarecord);
381 my $id_key = $is_meta ? 'mmr_id' : 'bre_id';
383 # find the last record in the set, then redirect
384 my $find_last = $cgi->param('find_last');
386 $self->timelog("Loading results");
387 # load bookbag metadata, if requested.
388 if (my $bbag_err = $self->load_rresults_bookbag) {
392 $ctx->{page} = 'rresult' unless $internal;
394 $ctx->{records} = [];
395 $ctx->{search_facets} = {};
396 $ctx->{hit_count} = 0;
397 $ctx->{is_meta} = $is_meta;
399 # Special alternative searches here. This could all stand to be cleaner.
400 if ($cgi->param("_special")) {
401 $self->timelog("Calling MARC expert search");
402 return $self->marc_expert_search(%args) if (scalar($cgi->param("tag")) and
403 (!defined $cgi->param("query") or $cgi->param("query") =~ /^\s*$/));
404 $self->timelog("Calling item barcode search");
405 return $self->item_barcode_shortcut if (
406 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode") and not $internal
408 $self->timelog("Calling call number browse");
409 return $self->call_number_browse_standalone if (
410 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
414 $self->timelog("Getting search parameters");
415 my $page = $cgi->param('page') || 0;
416 my @facets = $cgi->multi_param('facet');
417 my $limit = $self->_get_search_limit;
418 $ctx->{search_ou} = $self->_get_search_lib();
419 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
420 my $offset = $page * $limit;
422 my $tag_circs = $self->tag_circed_items;
423 $self->timelog("Got search parameters");
425 $ctx->{page_size} = $limit;
426 $ctx->{search_page} = $page;
428 # fetch this page plus the first hit from the next page
430 $limit = $offset + $limit + 1;
434 my ($user_query, $query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
436 $self->get_staff_search_settings;
438 if (!$find_last and $ctx->{staff_saved_search_size}) {
439 my ($key, $list) = $self->staff_save_search($query);
441 $self->apache->headers_out->add(
442 "Set-Cookie" => $self->cgi->cookie(
443 -name => (ref $self)->COOKIE_ANON_CACHE,
445 -value => ($key || ''),
446 -expires => ($key ? undef : "-1h")
449 $ctx->{saved_searches} = $list;
452 # Limit and offset will stay here. Everything else should be part of
453 # the query string, not special args.
454 my $args = {'limit' => $limit, 'offset' => $offset};
456 return Apache2::Const::OK unless $query;
459 $args->{tag_circulated_records} = 1;
460 $args->{authtoken} = $self->editor->authtoken;
462 $args->{from_metarecord} = $metarecord if $metarecord;
464 # Stuff these into the TT context so that templates can use them in redrawing forms
465 $ctx->{user_query} = $user_query;
466 $ctx->{processed_search_query} = $query;
468 $query = "$_ $query" for @facets;
470 my $ltag = $is_meta ? '[mmr search]' : '[bre search]';
471 $logger->activity("EGWeb: $ltag $query");
475 my $method = 'open-ils.search.biblio.multiclass.query';
476 $method .= '.staff' if $ctx->{is_staff};
477 $method =~ s/biblio/metabib/ if $is_meta;
479 my $ses = OpenSRF::AppSession->create('open-ils.search');
481 $self->timelog("Firing off the multiclass query");
482 my $req = $ses->request($method, $args, $query, 1, $ctx->{physical_loc});
483 $results = $req->gather(1);
484 $self->timelog("Returned from the multiclass query");
488 $logger->error("multiclass search error: $err");
489 $results = {count => 0, ids => []};
492 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
494 $ctx->{ids} = $rec_ids;
495 $ctx->{hit_count} = $results->{count};
496 $ctx->{superpage} = $results->{superpage};
497 $ctx->{superpage_size} = $results->{superpage_size};
498 $ctx->{pagable_limit} = $results->{core_limit};
499 $ctx->{query_struct} = $results->{global_summary}{query_struct};
500 $logger->debug('query struct: '. Dumper($ctx->{query_struct}));
501 $ctx->{canonicalized_query} = $results->{global_summary}{canonicalized_query};
502 $ctx->{search_summary} = $results->{global_summary};
505 # redirect to the record detail page for the last record in the results
506 my $rec_id = pop @$rec_ids;
507 $cgi->delete('find_last');
508 my $url = $cgi->url(-full => 1, -path => 1, -query => 1);
509 # TODO: metarecord => /rresults?metarecord=$mmr_id
510 $url =~ s|/results|/record/$rec_id|;
511 return $self->generic_redirect($url);
514 return Apache2::Const::OK if @$rec_ids == 0 or $internal;
516 $self->load_rresults_bookbag_item_notes($rec_ids) if $ctx->{bookbag};
518 my $fetch_recs = $rec_ids;
520 $self->timelog("Calling get_records_and_facets()");
521 my ($facets, @data) = $self->get_records_and_facets(
522 $fetch_recs, $results->{facet_key},
524 flesh => '{holdings_xml,mra,acp,acnp,acns,bmp,cbs}',
526 metarecord => $is_meta,
528 pref_lib => $ctx->{pref_ou},
531 $self->timelog("Returned from get_records_and_facets()");
533 if ($page == 0 and @$rec_ids == 1) {
536 # if the MR has a single constituent record, it will
537 # be in array position 4 of the result blob.
538 # otherwise, we don't want to redirect anyway.
539 my $bre_id = $results->{ids}->[0]->[4];
540 $stat = $self->check_1hit_redirect([$bre_id]) if $bre_id;
542 my $stat = $self->check_1hit_redirect($rec_ids);
544 return $stat if $stat;
547 # load temporary_list settings for user and ou:
548 $self->_load_lists_and_settings if ($ctx->{user});
551 # shove recs into context in search results order
552 for my $rec_id (@$rec_ids) {
553 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
554 push(@{$ctx->{records}}, $rec);
558 my $method = 'open-ils.search.biblio.multiclass.query';
559 $method .= '.staff' if $ctx->{is_staff};
560 my $ses = OpenSRF::AppSession->create('open-ils.search');
561 $self->timelog("Firing off the multiclass query: ". $rec_id);
562 $args->{from_metarecord} = $rec_id;
563 # offset of main search does not apply to the MR
565 my $save_offset = $args->{offset};
567 $mr_contents{$rec_id} = $ses->request($method, $args, $query, 1, $ctx->{physical_loc});
568 $args->{offset} = $save_offset;
571 $logger->error("multiclass search error: $err");
576 # above we fire all searches in parallel, now we collect the results
578 for my $rec_id (@$rec_ids) {
579 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
581 if ($mr_contents{$rec_id}) {
582 $meta_results = $mr_contents{$rec_id}->gather(1);
583 $self->timelog("Returned from the multiclass query: ". $rec_id);
585 $meta_results = {count => 0, ids => []};
587 my $meta_rec_ids = [map { $_->[0] } @{$meta_results->{ids}}];
588 $rec->{mr_constituent_count} = $meta_results->{count};
589 $rec->{mr_constituent_ids} = $meta_rec_ids;
593 my $course_module_opt_in = 0;
594 if ($ctx->{get_org_setting}->($self->_get_search_lib, "circ.course_materials_opt_in")) {
595 $course_module_opt_in = 1;
598 for my $rec (@{$ctx->{records}}) {
599 my ($res_rec) = grep { $_->[0] == $rec->{$id_key} } @{$results->{ids}};
600 $rec->{badges} = [split(',', $res_rec->[1])] if $res_rec->[1];
601 $rec->{popularity} = $res_rec->[2];
602 if ($course_module_opt_in) {
603 $rec->{course_materials} = $U->simplereq(
605 'open-ils.courses.course_materials.retrieve.atomic',
606 {record => $rec->{id}}
609 for my $material (@{$rec->{course_materials}}) {
610 $course_ids{$material->course} = 1;
613 $rec->{courses} = $U->simplereq(
615 'open-ils.courses.courses.retrieve',
620 # index 3 (5 for MR) in the per-record result array is a boolean which
621 # indicates whether the record in question is in the users
622 # accessible circ history list
623 my $index = $is_meta ? 5 : 3;
624 $rec->{user_circulated} = 1 if $res_rec->[$index];
629 $ctx->{search_facets} = $facets;
631 return Apache2::Const::OK;
634 # If the calling search results in 1 record and the client
635 # is configured to do so, redirect the search results to
636 # the record details page.
637 sub check_1hit_redirect {
638 my ($self, $rec_ids) = @_;
639 my $ctx = $self->ctx;
641 return undef unless $rec_ids and @$rec_ids == 1;
645 $self->timelog("Checking whether to jump to details on a single hit");
646 if ($ctx->{is_staff}) {
647 $sname = 'opac.staff.jump_to_details_on_single_hit';
648 $org = $ctx->{user}->ws_ou;
651 $sname = 'opac.patron.jump_to_details_on_single_hit';
652 $org = $self->_get_search_lib();
655 $self->timelog("Return from checking whether to jump to details on a single hit");
658 $self->ctx->{get_org_setting}->($org, $sname);
660 my $base_url = sprintf(
661 '%s://%s%s/record/%s',
663 $self->ctx->{hostname},
664 $self->ctx->{opac_root},
668 # If we get here from the same record detail page to which we
669 # now wish to redirect, do not perform the redirect. This
670 # approach seems to work well, with the rare exception of
671 # performing a new search directly from the detail page that
672 # happens to result in the same single hit. In this case, the
673 # user will be left on the search results page. This could be
674 # overcome w/ additional CGI, etc., but I'm not sure it's necessary.
675 if (my $referer = $ctx->{referer}) {
676 $referer =~ s/([^?]*).*/$1/g;
677 return undef if $base_url eq $referer;
680 return $self->generic_redirect($base_url . '?' . $self->cgi->query_string);
683 # Searching by barcode is a special search that does /not/ respect any other
684 # of the usual search parameters, not even the ones for sorting and paging!
685 sub item_barcode_shortcut {
688 $self->timelog("Searching for item_barcode");
689 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
690 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
691 my $rec_ids = $search->request(
692 $method, $self->cgi->param("query")
695 $self->timelog("Finished searching for item_barcode");
697 if (ref $rec_ids ne 'ARRAY') {
699 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
703 if (defined $U->event_code($rec_ids)) {
704 $self->apache->log->warn(
705 "$method returned event: " . $U->event_code($rec_ids)
708 $self->apache->log->warn(
709 "$method returned something unexpected: $rec_ids"
712 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
716 $self->timelog("Calling get_records_and_facets() for item_barcode");
717 my ($facets, @data) = $self->get_records_and_facets(
718 $rec_ids, undef, {flesh => "{holdings_xml,mra,acnp,acns,bmp,cbs}"}
720 $self->timelog("Returned from calling get_records_and_facets() for item_barcode");
722 my $stat = $self->check_1hit_redirect($rec_ids);
723 return $stat if $stat;
725 $self->ctx->{records} = [@data];
726 $self->ctx->{search_facets} = {};
727 $self->ctx->{hit_count} = scalar @data;
728 $self->ctx->{page_size} = $self->ctx->{hit_count};
729 # load temporary_list settings for user and ou:
730 $self->_load_lists_and_settings if ($self->ctx->{user});
732 return Apache2::Const::OK;
734 $self->apache->log->warn("couldn't connect to open-ils.search");
735 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
739 # like item_barcode_search, this can't take all the usual search params, but
740 # this one will at least do site, limit and page
741 sub marc_expert_search {
742 my ($self, %args) = @_;
744 my @tags = $self->cgi->multi_param("tag");
745 my @subfields = $self->cgi->multi_param("subfield");
746 my @terms = $self->cgi->multi_param("term");
749 for (my $i = 0; $i < scalar @tags; $i++) {
750 next if ($tags[$i] eq "" || $terms[$i] eq "");
751 $subfields[$i] = '_' unless $subfields[$i];
753 "term" => $terms[$i],
754 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
758 $logger->info("query for expert search: " . Dumper($query));
760 $self->timelog("Getting search parameters");
761 # loc, limit and offset
762 my $page = $self->cgi->param("page") || 0;
763 my $limit = $self->_get_search_limit;
764 $self->ctx->{search_ou} = $self->_get_search_lib();
765 $self->ctx->{pref_ou} = $self->_get_pref_lib();
766 my $offset = $page * $limit;
767 $self->timelog("Got search parameters");
769 $self->ctx->{records} = [];
770 $self->ctx->{search_facets} = {};
771 $self->ctx->{page_size} = $limit;
772 $self->ctx->{hit_count} = 0;
773 $self->ctx->{ids} = [];
774 $self->ctx->{search_page} = $page;
777 return Apache2::Const::OK if @$query == 0;
779 if ($args{internal}) {
780 $limit = $offset + $limit + 1;
784 $self->timelog("Searching for MARC expert");
785 my $method = 'open-ils.search.biblio.marc';
786 $method .= '.staff' if $self->ctx->{is_staff};
788 my $ses = OpenSRF::AppSession->create('open-ils.search');
790 #when a sort variable is passed in we need to add its value to the arguments
791 my $arghash = {searches => $query, org_unit => $self->ctx->{search_ou}};
792 if (defined $self->cgi->param("sort")) {
793 my ($sort, $sort_dir) = split /\./, $self->cgi->param('sort');
794 $arghash = {%$arghash, sort => "$sort"};
795 if (!defined $sort_dir) {
796 $arghash = {%$arghash, sort_dir => "ASC"};
800 #add the offset and limit to the argash, so we can go past 100 results
801 #if offset and limit are not in the args then they default to 0 and 100
802 #respectively in biblio_multi_search_full_rec, which limits the results to 100
804 $arghash = {%$arghash, offset => $offset, limit => $limit};
806 my $req = $ses->request(
811 my $resp = $req->recv($timeout);
812 my $results = $resp ? $resp->content : undef;
814 $self->timelog("Got our MARC expert results");
816 if (defined $U->event_code($results)) {
817 $self->apache->log->warn(
818 "open-ils.search.biblio.marc returned event: " .
819 $U->event_code($results)
821 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
824 $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
825 $self->ctx->{hit_count} = $results->{count};
827 return Apache2::Const::OK if @{$self->ctx->{ids}} == 0 or $args{internal};
830 my $stat = $self->check_1hit_redirect($self->ctx->{ids});
831 return $stat if $stat;
834 $self->timelog("Calling get_records_and_facets() for MARC expert");
835 my ($facets, @data) = $self->get_records_and_facets(
836 $self->ctx->{ids}, undef, {
837 flesh => "{holdings_xml,mra,acnp,acns,cbs}",
838 pref_lib => $self->ctx->{pref_ou},
841 $self->timelog("Returned from calling get_records_and_facets() for MARC expert");
843 # load temporary_list settings for user and ou:
844 $self->_load_lists_and_settings if ($self->ctx->{user});
846 $self->ctx->{records} = [@data];
848 return Apache2::Const::OK;
851 sub call_number_browse_standalone {
854 if (my $cnfrag = $self->cgi->param("query")) {
856 '%s://%s%s/cnbrowse?cn=%s',
858 $self->ctx->{hostname},
859 $self->ctx->{opac_root},
860 $cnfrag # XXX some kind of escaping needed here?
862 $url .= '&locg=' . $self->_get_search_lib() if ($self->_get_search_lib());
863 return $self->generic_redirect($url);
865 return $self->generic_redirect; # return to search page
872 $self->prepare_browse_call_numbers();
874 return Apache2::Const::OK;
877 sub get_staff_search_settings {
880 unless ($self->ctx->{is_staff}) {
881 $self->ctx->{staff_saved_search_size} = 0;
885 $self->timelog("Getting staff search size");
886 my $sss_size = $self->ctx->{get_org_setting}->(
887 $self->ctx->{physical_loc} || $self->ctx->{aou_tree}->()->id,
888 "opac.staff_saved_search.size",
890 $self->timelog("Got staff search size");
892 # Sic: 0 is 0 (off), but undefined is 10.
893 $sss_size = 10 unless defined $sss_size;
895 $self->ctx->{staff_saved_search_size} = $sss_size;
898 sub staff_load_searches {
901 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
905 $self->timelog("Getting anon_cache value");
906 $list = $U->simplereq(
908 "open-ils.actor.anon_cache.get_value",
909 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
911 $self->timelog("Got anon_cache value");
919 return ($cache_key, $list);
922 sub staff_save_search {
923 my ($self, $query) = @_;
925 my $sss_size = $self->ctx->{staff_saved_search_size};
926 return unless $sss_size > 0;
928 my ($cache_key, $list) = $self->staff_load_searches;
929 my %already = ( map { $_ => 1 } @$list );
931 unshift @$list, $query unless $already{$query};
933 splice @$list, $sss_size if scalar @$list > $sss_size;
935 $self->timelog("Setting anon_cache value");
936 $cache_key = $U->simplereq(
938 "open-ils.actor.anon_cache.set_value",
939 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
941 $self->timelog("Set anon_cache value");
943 return ($cache_key, $list);