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 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
24 my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
26 next unless $query =~ /\S/;
28 # Hack for journal title
29 if ($qtype eq 'jtitle') {
33 # This stuff probably will need refined or rethought to better handle
34 # the weird things Real Users will surely type in.
35 $contains = "" unless defined $contains; # silence warning
36 if ($contains eq 'nocontains') {
38 $query = ('"' . $query . '"') if index $query, ' ';
39 $query = '-' . $query;
40 } elsif ($contains eq 'phrase') {
42 $query = ('"' . $query . '"') if index $query, ' ';
43 } elsif ($contains eq 'exact') {
44 $query =~ s/[\^\$]//g;
45 $query = '^' . $query . '$';
46 } elsif ($contains eq 'starts') {
48 $query =~ s/[\^\$]//g;
49 $query = '^' . $query;
50 $query = ('"' . $query . '"') if index $query, ' ';
52 $query = "$qtype:$query" unless $qtype eq 'keyword' and $i == 0;
54 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
55 $full_query = $full_query ? "($full_query $bool $query)" : $query;
61 sub _prepare_biblio_search {
64 my $query = _prepare_biblio_search_basics($cgi) || '';
66 $query .= ' ' . $ctx->{global_search_filter} if $ctx->{global_search_filter};
68 foreach ($cgi->param('modifier')) {
69 # The unless bit is to avoid stacking modifiers.
70 $query = ('#' . $_ . ' ' . $query) unless
71 $query =~ qr/\#\Q$_/ or $_ eq 'metabib';
75 foreach (grep /^fi:/, $cgi->param) {
77 my $term = join(",", $cgi->param($_));
78 $query .= " $1($term)" if length $term;
81 # filter group entries. Entries from like filters are grouped into a single
82 # filter_group_entry() filter (ORed). Each collection is ANDed together.
83 # fg:foo_group=foo_entry_id
84 foreach (grep /^fg:/, $cgi->param) {
86 my $term = join(",", $cgi->param($_));
87 $query = "filter_group_entry($term) $query" if length $term;
90 if ($cgi->param("bookbag")) {
91 $query = "container(bre,bookbag," . int($cgi->param("bookbag")) . ") $query";
94 # Journal title hackery complete
95 if ($cgi->param("qtype") && $cgi->param("qtype") eq "jtitle") {
96 $query = "bib_level(s) $query";
99 if ($cgi->param('pubdate') && $cgi->param('date1')) {
100 if ($cgi->param('pubdate') eq 'between') {
101 my $btw = 'between(' . $cgi->param('date1');
102 $btw .= ',' . $cgi->param('date2') if $cgi->param('date2');
104 $query = "$btw $query";
105 } elsif ($cgi->param('pubdate') eq 'is') {
106 $query = 'between(' . $cgi->param('date1') .
107 ',' . $cgi->param('date1') . ") $query"; # sic, date1 twice
109 $query = $cgi->param('pubdate') .
110 '(' . $cgi->param('date1') . ") $query";
114 # ---------------------------------------------------------------------
115 # Nothing below here constitutes a query by itself. If the query value
116 # is still empty up to this point, there is no query. abandon ship.
117 return () unless $query;
119 # sort is treated specially, even though it's actually a filter
120 if ($cgi->param('sort')) {
121 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
122 my ($axis, $desc) = split /\./, $cgi->param('sort');
123 $query = "sort($axis) $query";
124 if ($desc and not $query =~ /\#descending/) {
125 $query = "#descending $query";
126 } elsif (not $desc) {
127 $query =~ s/\#descending//;
131 my (@naive_query_re, $site);
133 my $org = $ctx->{search_ou};
134 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
135 my $thing = " site(" . $ctx->{get_aou}->($org)->shortname . ")";
138 push @naive_query_re, $thing;
141 my $pref_ou = $ctx->{pref_ou};
142 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+\)/) {
143 my $plib = $ctx->{get_aou}->($pref_ou)->shortname;
144 $query = "pref_ou($plib) $query";
147 if (my $grp = $ctx->{copy_location_group}) {
148 $query = "location_groups($grp) $query";
152 ($site) = ($query =~ /site\(([^\)]+)\)/);
153 $site ||= $ctx->{aou_tree}->()->shortname;
157 if ($query =~ /depth\(\d+\)/) {
159 # depth is encoded in the search query
160 ($depth) = ($query =~ /depth\((\d+)\)/);
164 if (defined $cgi->param('depth')) {
165 $depth = $cgi->param('depth');
167 # no depth specified. match the depth to the search org
168 my ($org) = grep { $_->shortname eq $site } @{$ctx->{aou_list}->()};
169 $depth = $org->ou_type->depth;
171 my $thing = " depth($depth)";
174 push @naive_query_re, $thing;
177 # This gives templates a way to take site() and depth() back out of
178 # query strings when they shouldn't be there (because they're controllable
179 # with other widgets).
180 $ctx->{naive_query_scrub} = sub {
182 $query =~ s/\Q$_\E// foreach (@naive_query_re);
186 $logger->info("tpac: site=$site, depth=$depth, query=$query");
188 return ($query, $site, $depth);
191 sub _get_search_limit {
194 # param takes precedence
195 my $limit = $self->cgi->param('limit');
196 return $limit if $limit;
198 if($self->editor->requestor) {
199 $self->timelog("Checking for opac.hits_per_page preference");
200 # See if the user has a hit count preference
201 my $lset = $self->editor->search_actor_user_setting({
202 usr => $self->editor->requestor->id,
203 name => 'opac.hits_per_page'
205 $self->timelog("Got opac.hits_per_page preference");
206 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
212 sub tag_circed_items {
214 my $e = $self->editor;
216 $self->timelog("Tag circed items?");
217 return 0 unless $e->requestor;
218 $self->timelog("Checking for opac.search.tag_circulated_items");
219 return 0 unless $self->ctx->{get_org_setting}->(
220 $e->requestor->home_ou,
221 'opac.search.tag_circulated_items');
223 # user has to be opted-in to circ history in some capacity
224 $self->timelog("Checking for history.circ.retention_*");
225 my $sets = $e->search_actor_user_setting({
226 usr => $e->requestor->id,
228 'history.circ.retention_age',
229 'history.circ.retention_start'
233 $self->timelog("Return from checking for history.circ.retention_*");
235 return 0 unless @$sets;
240 # This only loads the bookbag itself (in support of a record results page)
241 # if a "bookbag" CGI parameter is specified and if the bookbag is public
242 # or owned by the logged-in user (if any). Bookbag notes are fetched
243 # later if applicable.
244 sub load_rresults_bookbag {
247 my $bookbag_id = int($self->cgi->param("bookbag") || 0);
248 return if $bookbag_id < 1;
250 my %authz = $self->ctx->{"user"} ?
251 ("-or" => {"pub" => "t", "owner" => $self->ctx->{"user"}->id}) :
254 $self->timelog("Load results bookbag");
255 my $bbag = $self->editor->search_container_biblio_record_entry_bucket(
256 {"id" => $bookbag_id, "btype" => "bookbag", %authz}
258 $self->timelog("Got results bookbag");
261 $self->apache->log->warn(
262 "error from cstore retrieving bookbag $bookbag_id!"
264 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
266 $self->ctx->{"bookbag"} = shift @$bbag;
272 # assumes context has a bookbag we're already authorized to look at, and
273 # a list of rec_ids, reasonably sized (from paged search).
274 sub load_rresults_bookbag_item_notes {
275 my ($self, $rec_ids) = @_;
277 $self->timelog("Load results bookbag item notes");
278 my $items_with_notes =
279 $self->editor->search_container_biblio_record_entry_bucket_item([
280 {"target_biblio_record_entry" => $rec_ids,
281 "bucket" => $self->ctx->{"bookbag"}->id},
282 {"flesh" => 1, "flesh_fields" => {"cbrebi" => ["notes"]},
283 "order_by" => {"cbrebi" => ["id"]}}
285 $self->timelog("Got results bookbag item notes");
287 if (!$items_with_notes) {
288 $self->apache->log->warn("error from cstore retrieving cbrebi objects");
289 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
292 $self->ctx->{"bookbag_items_by_bre_id"} = +{
293 map { $_->target_biblio_record_entry => $_ } @$items_with_notes
302 # records : list of bre's and copy-count objects
306 my $internal = $args{internal};
307 my $cgi = $self->cgi;
308 my $ctx = $self->ctx;
309 my $e = $self->editor;
311 # 1. param->metarecord : view constituent bib records for a metarecord
312 # 2. param->modifier=metabib : perform a metarecord search
313 my $metarecord = $ctx->{metarecord} = $cgi->param('metarecord');
314 my @mods = $cgi->param('modifier');
315 my $is_meta = (@mods and grep {$_ eq 'metabib'} @mods and !$metarecord);
316 my $id_key = $is_meta ? 'mmr_id' : 'bre_id';
318 # find the last record in the set, then redirect
319 my $find_last = $cgi->param('find_last');
321 $self->timelog("Loading results");
322 # load bookbag metadata, if requested.
323 if (my $bbag_err = $self->load_rresults_bookbag) {
327 $ctx->{page} = 'rresult' unless $internal;
329 $ctx->{records} = [];
330 $ctx->{search_facets} = {};
331 $ctx->{hit_count} = 0;
333 # Special alternative searches here. This could all stand to be cleaner.
334 if ($cgi->param("_special")) {
335 $self->timelog("Calling MARC expert search");
336 return $self->marc_expert_search(%args) if scalar($cgi->param("tag"));
337 $self->timelog("Calling item barcode search");
338 return $self->item_barcode_shortcut if (
339 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode") and not $internal
341 $self->timelog("Calling call number browse");
342 return $self->call_number_browse_standalone if (
343 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
347 $self->timelog("Getting search parameters");
348 my $page = $cgi->param('page') || 0;
349 my @facets = $cgi->param('facet');
350 my $limit = $self->_get_search_limit;
351 $ctx->{search_ou} = $self->_get_search_lib();
352 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
353 my $offset = $page * $limit;
355 my $tag_circs = $self->tag_circed_items;
356 $self->timelog("Got search parameters");
358 $ctx->{page_size} = $limit;
359 $ctx->{search_page} = $page;
361 # fetch this page plus the first hit from the next page
363 $limit = $offset + $limit + 1;
367 my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
369 $self->get_staff_search_settings;
371 if (!$find_last and $ctx->{staff_saved_search_size}) {
372 my ($key, $list) = $self->staff_save_search($query);
374 $self->apache->headers_out->add(
375 "Set-Cookie" => $self->cgi->cookie(
376 -name => (ref $self)->COOKIE_ANON_CACHE,
378 -value => ($key || ''),
379 -expires => ($key ? undef : "-1h")
382 $ctx->{saved_searches} = $list;
386 if ($metarecord and !$internal) {
388 # TODO: other limits, like SVF/format, etc.
389 $self->timelog("Getting metarecords to records");
390 $results = $U->simplereq(
392 'open-ils.search.biblio.metarecord_to_records',
393 $metarecord, {org => $ctx->{search_ou}, depth => $depth}
395 $self->timelog("Got metarecords to records");
397 # force the metarecord result blob to match the format of regular search results
398 $results->{ids} = [map { [$_] } @{$results->{ids}}];
402 return Apache2::Const::OK unless $query;
404 # Limit and offset will stay here. Everything else should be part of
405 # the query string, not special args.
406 my $args = {'limit' => $limit, 'offset' => $offset};
409 $args->{tag_circulated_records} = 1;
410 $args->{authtoken} = $self->editor->authtoken;
413 # Stuff these into the TT context so that templates can use them in redrawing forms
414 $ctx->{processed_search_query} = $query;
416 $query = "$_ $query" for @facets;
418 my $ltag = $is_meta ? '[mmr search]' : '[bre search]';
419 $logger->activity("EGWeb: $ltag $query");
423 my $method = 'open-ils.search.biblio.multiclass.query';
424 $method .= '.staff' if $ctx->{is_staff};
425 $method =~ s/biblio/metabib/ if $is_meta;
427 my $ses = OpenSRF::AppSession->create('open-ils.search');
429 $self->timelog("Firing off the multiclass query");
430 my $req = $ses->request($method, $args, $query, 1);
431 $results = $req->gather(1);
432 $self->timelog("Returned from the multiclass query");
436 $logger->error("multiclass search error: $err");
437 $results = {count => 0, ids => []};
441 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
443 $ctx->{ids} = $rec_ids;
444 $ctx->{hit_count} = $results->{count};
445 $ctx->{parsed_query} = $results->{parsed_query};
448 # redirect to the record detail page for the last record in the results
449 my $rec_id = pop @$rec_ids;
450 $cgi->delete('find_last');
451 my $url = $cgi->url(-full => 1, -path => 1, -query => 1);
452 # TODO: metarecord => /rresults?metarecord=$mmr_id
453 $url =~ s|/results|/record/$rec_id|;
454 return $self->generic_redirect($url);
457 return Apache2::Const::OK if @$rec_ids == 0 or $internal;
459 $self->load_rresults_bookbag_item_notes($rec_ids) if $ctx->{bookbag};
461 my $fetch_recs = $rec_ids;
463 my $metarecord_master;
465 # when listing the contents of a metarecord, be sure to fetch
466 # the lead record for summary display. Adding the ID to
467 # $fetch_recs lets us grab the record (if necessary) w/o it
468 # unintentially becoming a member of the result set.
469 my $mr = $e->retrieve_metabib_metarecord($metarecord);
470 push(@$fetch_recs, $mr->master_record)
471 unless grep {$_ eq $mr->master_record} @$fetch_recs;
472 $metarecord_master = $mr->master_record;
475 $self->timelog("Calling get_records_and_facets()");
476 my ($facets, @data) = $self->get_records_and_facets(
477 $fetch_recs, $results->{facet_key},
479 flesh => '{holdings_xml,mra,acp,acnp,acns,bmp}',
481 metarecord => $is_meta,
483 pref_lib => $ctx->{pref_ou},
486 $self->timelog("Returned from get_records_and_facets()");
488 if ($page == 0 and @$rec_ids == 1) {
491 # if the MR has a single constituent record, it will
492 # be in array position 2 of the result blob.
493 # otherwise, we don't want to redirect anyway.
494 my $bre_id = $results->{ids}->[0]->[2];
495 $stat = $self->check_1hit_redirect([$bre_id]) if $bre_id;
497 my $stat = $self->check_1hit_redirect($rec_ids);
499 return $stat if $stat;
502 # load temporary_list settings for user and ou:
503 $self->_load_lists_and_settings if ($ctx->{user});
505 # shove recs into context in search results order
506 for my $rec_id (@$rec_ids) {
507 my ($rec) = grep { $_->{$id_key} == $rec_id } @data;
508 push(@{$ctx->{records}}, $rec);
510 $ctx->{metarecord_master} = $rec
511 if $metarecord_master and $metarecord_master eq $rec_id;
513 # MR's with multiple constituent records will have a
514 # null value in position 2 of the result set.
515 my ($res_rec) = grep { $_->[0] == $rec_id} @{$results->{ids}};
516 $rec->{mr_has_multi} = !$res_rec->[2];
520 for my $rec (@{$ctx->{records}}) {
521 my ($res_rec) = grep { $_->[0] == $rec->{$id_key} } @{$results->{ids}};
522 # index 1 in the per-record result array is a boolean which
523 # indicates whether the record in question is in the users
524 # accessible circ history list
525 my $index = $is_meta ? 3 : 1;
526 $rec->{user_circulated} = 1 if $res_rec->[$index];
530 $ctx->{search_facets} = $facets;
532 return Apache2::Const::OK;
535 # If the calling search results in 1 record and the client
536 # is configured to do so, redirect the search results to
537 # the record details page.
538 sub check_1hit_redirect {
539 my ($self, $rec_ids) = @_;
540 my $ctx = $self->ctx;
542 return undef unless $rec_ids and @$rec_ids == 1;
546 $self->timelog("Checking whether to jump to details on a single hit");
547 if ($ctx->{is_staff}) {
548 $sname = 'opac.staff.jump_to_details_on_single_hit';
549 $org = $ctx->{user}->ws_ou;
552 $sname = 'opac.patron.jump_to_details_on_single_hit';
553 $org = $self->_get_search_lib();
556 $self->timelog("Return from checking whether to jump to details on a single hit");
559 $self->ctx->{get_org_setting}->($org, $sname);
561 my $base_url = sprintf(
562 '%s://%s%s/record/%s',
564 $self->ctx->{hostname},
565 $self->ctx->{opac_root},
569 # If we get here from the same record detail page to which we
570 # now wish to redirect, do not perform the redirect. This
571 # approach seems to work well, with the rare exception of
572 # performing a new search directly from the detail page that
573 # happens to result in the same single hit. In this case, the
574 # user will be left on the search results page. This could be
575 # overcome w/ additional CGI, etc., but I'm not sure it's necessary.
576 if (my $referer = $ctx->{referer}) {
577 $referer =~ s/([^?]*).*/$1/g;
578 return undef if $base_url eq $referer;
581 return $self->generic_redirect($base_url . '?' . $self->cgi->query_string);
584 # Searching by barcode is a special search that does /not/ respect any other
585 # of the usual search parameters, not even the ones for sorting and paging!
586 sub item_barcode_shortcut {
589 $self->timelog("Searching for item_barcode");
590 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
591 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
592 my $rec_ids = $search->request(
593 $method, $self->cgi->param("query")
596 $self->timelog("Finished searching for item_barcode");
598 if (ref $rec_ids ne 'ARRAY') {
600 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
604 if (defined $U->event_code($rec_ids)) {
605 $self->apache->log->warn(
606 "$method returned event: " . $U->event_code($rec_ids)
609 $self->apache->log->warn(
610 "$method returned something unexpected: $rec_ids"
613 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
617 $self->timelog("Calling get_records_and_facets() for item_barcode");
618 my ($facets, @data) = $self->get_records_and_facets(
619 $rec_ids, undef, {flesh => "{holdings_xml,mra,acnp,acns,bmp}"}
621 $self->timelog("Returned from calling get_records_and_facets() for item_barcode");
623 my $stat = $self->check_1hit_redirect($rec_ids);
624 return $stat if $stat;
626 $self->ctx->{records} = [@data];
627 $self->ctx->{search_facets} = {};
628 $self->ctx->{hit_count} = scalar @data;
629 $self->ctx->{page_size} = $self->ctx->{hit_count};
630 # load temporary_list settings for user and ou:
631 $self->_load_lists_and_settings if ($self->ctx->{user});
633 return Apache2::Const::OK;
635 $self->apache->log->warn("couldn't connect to open-ils.search");
636 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
640 # like item_barcode_search, this can't take all the usual search params, but
641 # this one will at least do site, limit and page
642 sub marc_expert_search {
643 my ($self, %args) = @_;
645 my @tags = $self->cgi->param("tag");
646 my @subfields = $self->cgi->param("subfield");
647 my @terms = $self->cgi->param("term");
650 for (my $i = 0; $i < scalar @tags; $i++) {
651 next if ($tags[$i] eq "" || $terms[$i] eq "");
652 $subfields[$i] = '_' unless $subfields[$i];
654 "term" => $terms[$i],
655 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
659 $logger->info("query for expert search: " . Dumper($query));
661 $self->timelog("Getting search parameters");
662 # loc, limit and offset
663 my $page = $self->cgi->param("page") || 0;
664 my $limit = $self->_get_search_limit;
665 $self->ctx->{search_ou} = $self->_get_search_lib();
666 $self->ctx->{pref_ou} = $self->_get_pref_lib();
667 my $offset = $page * $limit;
668 $self->timelog("Got search parameters");
670 $self->ctx->{records} = [];
671 $self->ctx->{search_facets} = {};
672 $self->ctx->{page_size} = $limit;
673 $self->ctx->{hit_count} = 0;
674 $self->ctx->{ids} = [];
675 $self->ctx->{search_page} = $page;
678 return Apache2::Const::OK if @$query == 0;
680 if ($args{internal}) {
681 $limit = $offset + $limit + 1;
685 $self->timelog("Searching for MARC expert");
686 my $method = 'open-ils.search.biblio.marc';
687 $method .= '.staff' if $self->ctx->{is_staff};
689 my $ses = OpenSRF::AppSession->create('open-ils.search');
690 my $req = $ses->request(
692 {searches => $query, org_unit => $self->ctx->{search_ou}},
693 $limit, $offset, $timeout);
695 my $resp = $req->recv($timeout);
696 my $results = $resp ? $resp->content : undef;
698 $self->timelog("Got our MARC expert results");
700 if (defined $U->event_code($results)) {
701 $self->apache->log->warn(
702 "open-ils.search.biblio.marc returned event: " .
703 $U->event_code($results)
705 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
708 $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
709 $self->ctx->{hit_count} = $results->{count};
711 return Apache2::Const::OK if @{$self->ctx->{ids}} == 0 or $args{internal};
714 my $stat = $self->check_1hit_redirect($self->ctx->{ids});
715 return $stat if $stat;
718 $self->timelog("Calling get_records_and_facets() for MARC expert");
719 my ($facets, @data) = $self->get_records_and_facets(
720 $self->ctx->{ids}, undef, {
721 flesh => "{holdings_xml,mra,acnp,acns}",
722 pref_lib => $self->ctx->{pref_ou},
725 $self->timelog("Returned from calling get_records_and_facets() for MARC expert");
727 # load temporary_list settings for user and ou:
728 $self->_load_lists_and_settings if ($self->ctx->{user});
730 $self->ctx->{records} = [@data];
732 return Apache2::Const::OK;
735 sub call_number_browse_standalone {
738 if (my $cnfrag = $self->cgi->param("query")) {
740 '%s://%s%s/cnbrowse?cn=%s',
742 $self->ctx->{hostname},
743 $self->ctx->{opac_root},
744 $cnfrag # XXX some kind of escaping needed here?
746 $url .= '&locg=' . $self->_get_search_lib() if ($self->_get_search_lib());
747 return $self->generic_redirect($url);
749 return $self->generic_redirect; # return to search page
756 $self->prepare_browse_call_numbers();
758 return Apache2::Const::OK;
761 sub get_staff_search_settings {
764 unless ($self->ctx->{is_staff}) {
765 $self->ctx->{staff_saved_search_size} = 0;
769 $self->timelog("Getting staff search size");
770 my $sss_size = $self->ctx->{get_org_setting}->(
771 $self->ctx->{physical_loc} || $self->ctx->{aou_tree}->()->id,
772 "opac.staff_saved_search.size",
774 $self->timelog("Got staff search size");
776 # Sic: 0 is 0 (off), but undefined is 10.
777 $sss_size = 10 unless defined $sss_size;
779 $self->ctx->{staff_saved_search_size} = $sss_size;
782 sub staff_load_searches {
785 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
789 $self->timelog("Getting anon_cache value");
790 $list = $U->simplereq(
792 "open-ils.actor.anon_cache.get_value",
793 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
795 $self->timelog("Got anon_cache value");
803 return ($cache_key, $list);
806 sub staff_save_search {
807 my ($self, $query) = @_;
809 my $sss_size = $self->ctx->{staff_saved_search_size};
810 return unless $sss_size > 0;
812 my ($cache_key, $list) = $self->staff_load_searches;
813 my %already = ( map { $_ => 1 } @$list );
815 unshift @$list, $query unless $already{$query};
817 splice @$list, $sss_size if scalar @$list > $sss_size;
819 $self->timelog("Setting anon_cache value");
820 $cache_key = $U->simplereq(
822 "open-ils.actor.anon_cache.set_value",
823 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
825 $self->timelog("Set anon_cache value");
827 return ($cache_key, $list);