]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Search.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Search.pm
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;
9 use Data::Dumper;
10 $Data::Dumper::Indent = 0;
11 my $U = 'OpenILS::Application::AppUtils';
12
13 sub _prepare_biblio_search_basics {
14     my ($cgi) = @_;
15
16     return $cgi->param('query') unless $cgi->param('qtype');
17
18     my %parts;
19     my @part_names = qw/qtype contains query bool/;
20     $parts{$_} = [ $cgi->param($_) ] for (@part_names);
21
22     my $full_query = '';
23     for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
24         my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
25
26         next unless $query =~ /\S/;
27
28         # This stuff probably will need refined or rethought to better handle
29         # the weird things Real Users will surely type in.
30         $contains = "" unless defined $contains; # silence warning
31         if ($contains eq 'nocontains') {
32             $query =~ s/"//g;
33             $query = ('"' . $query . '"') if index $query, ' ';
34             $query = '-' . $query;
35         } elsif ($contains eq 'phrase') {
36             $query =~ s/"//g;
37             $query = ('"' . $query . '"') if index $query, ' ';
38         } elsif ($contains eq 'exact') {
39             $query =~ s/[\^\$]//g;
40             $query = '^' . $query . '$';
41         }
42         $query = "$qtype:$query" unless $qtype eq 'keyword' and $i == 0;
43
44         $bool = ($bool and $bool eq 'or') ? '||' : '&&';
45         $full_query = $full_query ? "($full_query $bool $query)" : $query;
46     }
47
48     return $full_query;
49 }
50
51 sub _prepare_biblio_search {
52     my ($cgi, $ctx) = @_;
53
54     my $query = _prepare_biblio_search_basics($cgi) || '';
55
56     foreach ($cgi->param('modifier')) {
57         # The unless bit is to avoid stacking modifiers.
58         $query = ('#' . $_ . ' ' . $query) unless $query =~ qr/\#\Q$_/;
59     }
60
61     # filters
62     foreach (grep /^fi:/, $cgi->param) {
63         /:(-?\w+)$/ or next;
64         my $term = join(",", $cgi->param($_));
65         $query .= " $1($term)" if length $term;
66     }
67
68     if ($cgi->param("bookbag")) {
69         $query .= " container(bre,bookbag," . int($cgi->param("bookbag")) . ")";
70     }
71
72     if ($cgi->param('pubdate') && $cgi->param('date1')) {
73         if ($cgi->param('pubdate') eq 'between') {
74             $query .= ' between(' . $cgi->param('date1');
75             $query .= ',' .  $cgi->param('date2') if $cgi->param('date2');
76             $query .= ')';
77         } elsif ($cgi->param('pubdate') eq 'is') {
78             $query .= ' between(' . $cgi->param('date1') .
79                 ',' .  $cgi->param('date1') . ')';  # sic, date1 twice
80         } else {
81             $query .= ' ' . $cgi->param('pubdate') .
82                 '(' . $cgi->param('date1') . ')';
83         }
84     }
85
86     # ---------------------------------------------------------------------
87     # Nothing below here constitutes a query by itself.  If the query value 
88     # is still empty up to this point, there is no query.  abandon ship.
89     return () unless $query;
90
91     # sort is treated specially, even though it's actually a filter
92     if ($cgi->param('sort')) {
93         $query =~ s/sort\([^\)]*\)//g;  # override existing sort(). no stacking.
94         my ($axis, $desc) = split /\./, $cgi->param('sort');
95         $query .= " sort($axis)";
96         if ($desc and not $query =~ /\#descending/) {
97             $query .= '#descending';
98         } elsif (not $desc) {
99             $query =~ s/\#descending//;
100         }
101     }
102
103     my (@naive_query_re, $site);
104
105     my $org = $ctx->{search_ou};
106     if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
107         my $thing = " site(" . $ctx->{get_aou}->($org)->shortname . ")";
108
109         $query .= $thing;
110         push @naive_query_re, $thing;
111     }
112
113     my $pref_ou = $ctx->{pref_ou};
114     if (defined($pref_ou) and $pref_ou ne '' and $pref_ou != $org and ($pref_ou ne $ctx->{aou_tree}->()->id)) {
115         my $plib = $ctx->{get_aou}->($pref_ou)->shortname;
116         $query .= " pref_ou($plib)";
117     }
118
119     if (my $grp = $ctx->{copy_location_group}) {
120         $query .= " location_groups($grp)";
121     }
122
123     if(!$site) {
124         ($site) = ($query =~ /site\(([^\)]+)\)/);
125         $site ||= $ctx->{aou_tree}->()->shortname;
126     }
127
128     my $depth;
129     if ($query =~ /depth\(\d+\)/) {
130
131         # depth is encoded in the search query
132         ($depth) = ($query =~ /depth\((\d+)\)/);
133
134     } else {
135
136         if (defined $cgi->param('depth')) {
137             $depth = $cgi->param('depth');
138         } else {
139             # no depth specified.  match the depth to the search org
140             my ($org) = grep { $_->shortname eq $site } @{$ctx->{aou_list}->()};
141             $depth = $org->ou_type->depth;
142         }
143         my $thing = " depth($depth)";
144
145         $query .= $thing;
146         push @naive_query_re, $thing;
147     }
148
149     # This gives templates a way to take site() and depth() back out of
150     # query strings when they shouldn't be there (because they're controllable
151     # with other widgets).
152     $ctx->{naive_query_scrub} = sub {
153         my ($query) = @_;
154         $query =~ s/\Q$_\E// foreach (@naive_query_re);
155         return $query;
156     };
157
158     $logger->info("tpac: site=$site, depth=$depth, query=$query");
159
160     return ($query, $site, $depth);
161 }
162
163 sub _get_search_limit {
164     my $self = shift;
165
166     # param takes precedence
167     my $limit = $self->cgi->param('limit');
168     return $limit if $limit;
169
170     if($self->editor->requestor) {
171         $self->timelog("Checking for opac.hits_per_page preference");
172         # See if the user has a hit count preference
173         my $lset = $self->editor->search_actor_user_setting({
174             usr => $self->editor->requestor->id, 
175             name => 'opac.hits_per_page'
176         })->[0];
177         $self->timelog("Got opac.hits_per_page preference");
178         return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
179     }
180
181     return 10; # default
182 }
183
184 sub tag_circed_items {
185     my $self = shift;
186     my $e = $self->editor;
187
188     $self->timelog("Tag circed items?");
189     return 0 unless $e->requestor;
190     $self->timelog("Checking for opac.search.tag_circulated_items");
191     return 0 unless $self->ctx->{get_org_setting}->(
192         $e->requestor->home_ou, 
193         'opac.search.tag_circulated_items');
194
195     # user has to be opted-in to circ history in some capacity
196     $self->timelog("Checking for history.circ.retention_*");
197     my $sets = $e->search_actor_user_setting({
198         usr => $e->requestor->id, 
199         name => [
200             'history.circ.retention_age', 
201             'history.circ.retention_start'
202         ]
203     });
204
205     $self->timelog("Return from checking for history.circ.retention_*");
206
207     return 0 unless @$sets;
208     return 1;
209
210 }
211
212 # This only loads the bookbag itself (in support of a record results page)
213 # if a "bookbag" CGI parameter is specified and if the bookbag is public
214 # or owned by the logged-in user (if any).  Bookbag notes are fetched
215 # later if applicable.
216 sub load_rresults_bookbag {
217     my ($self) = @_;
218
219     my $bookbag_id = int($self->cgi->param("bookbag") || 0);
220     return if $bookbag_id < 1;
221
222     my %authz = $self->ctx->{"user"} ?
223         ("-or" => {"pub" => "t", "owner" => $self->ctx->{"user"}->id}) :
224         ("pub" => "t");
225
226     $self->timelog("Load results bookbag");
227     my $bbag = $self->editor->search_container_biblio_record_entry_bucket(
228         {"id" => $bookbag_id, "btype" => "bookbag", %authz}
229     );
230     $self->timelog("Got results bookbag");
231
232     if (!$bbag) {
233         $self->apache->log->warn(
234             "error from cstore retrieving bookbag $bookbag_id!"
235         );
236         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
237     } elsif (@$bbag) {
238         $self->ctx->{"bookbag"} = shift @$bbag;
239     }
240
241     return;
242 }
243
244 # assumes context has a bookbag we're already authorized to look at, and
245 # a list of rec_ids, reasonably sized (from paged search).
246 sub load_rresults_bookbag_item_notes {
247     my ($self, $rec_ids) = @_;
248
249     $self->timelog("Load results bookbag item notes");
250     my $items_with_notes =
251         $self->editor->search_container_biblio_record_entry_bucket_item([
252             {"target_biblio_record_entry" => $rec_ids,
253                 "bucket" => $self->ctx->{"bookbag"}->id},
254             {"flesh" => 1, "flesh_fields" => {"cbrebi" => ["notes"]},
255                 "order_by" => {"cbrebi" => ["id"]}}
256         ]);
257     $self->timelog("Got results bookbag item notes");
258
259     if (!$items_with_notes) {
260         $self->apache->log->warn("error from cstore retrieving cbrebi objects");
261         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
262     }
263
264     $self->ctx->{"bookbag_items_by_bre_id"} = +{
265         map { $_->target_biblio_record_entry => $_ } @$items_with_notes
266     };
267
268     return;
269 }
270
271 # context additions: 
272 #   page_size
273 #   hit_count
274 #   records : list of bre's and copy-count objects
275 sub load_rresults {
276     my $self = shift;
277     my %args = @_;
278     my $internal = $args{internal};
279     my $cgi = $self->cgi;
280     my $ctx = $self->ctx;
281     my $e = $self->editor;
282
283     # find the last record in the set, then redirect
284     my $find_last = $cgi->param('find_last');
285
286     $self->timelog("Loading results");
287     # load bookbag metadata, if requested.
288     if (my $bbag_err = $self->load_rresults_bookbag) {
289         return $bbag_err;
290     }
291
292     $ctx->{page} = 'rresult' unless $internal;
293     $ctx->{ids} = [];
294     $ctx->{records} = [];
295     $ctx->{search_facets} = {};
296     $ctx->{hit_count} = 0;
297
298     # Special alternative searches here.  This could all stand to be cleaner.
299     if ($cgi->param("_special")) {
300         $self->timelog("Calling MARC expert search");
301         return $self->marc_expert_search(%args) if scalar($cgi->param("tag"));
302         $self->timelog("Calling item barcode search");
303         return $self->item_barcode_shortcut if (
304             $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode")
305         );
306         $self->timelog("Calling call number browse");
307         return $self->call_number_browse_standalone if (
308             $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
309         );
310     }
311
312     $self->timelog("Getting search parameters");
313     my $page = $cgi->param('page') || 0;
314     my @facets = $cgi->param('facet');
315     my $limit = $self->_get_search_limit;
316     $ctx->{search_ou} = $self->_get_search_lib();
317     $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
318     my $offset = $page * $limit;
319     my $metarecord = $cgi->param('metarecord');
320     my $results; 
321     my $tag_circs = $self->tag_circed_items;
322     $self->timelog("Got search parameters");
323
324     $ctx->{page_size} = $limit;
325     $ctx->{search_page} = $page;
326
327     # fetch this page plus the first hit from the next page
328     if ($internal) {
329         $limit = $offset + $limit + 1;
330         $offset = 0;
331     }
332
333     my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
334
335     $self->get_staff_search_settings;
336
337     if (!$find_last and $ctx->{staff_saved_search_size}) {
338         my ($key, $list) = $self->staff_save_search($query);
339         if ($key) {
340             $self->apache->headers_out->add(
341                 "Set-Cookie" => $self->cgi->cookie(
342                     -name => (ref $self)->COOKIE_ANON_CACHE,
343                     -path => "/",
344                     -value => ($key || ''),
345                     -expires => ($key ? undef : "-1h")
346                 )
347             );
348             $ctx->{saved_searches} = $list;
349         }
350     }
351
352     if ($metarecord and !$internal) {
353
354         # TODO: other limits, like SVF/format, etc.
355         $self->timelog("Getting metarecords to records");
356         $results = $U->simplereq(
357             'open-ils.search', 
358             'open-ils.search.biblio.metarecord_to_records',
359             $metarecord, {org => $ctx->{search_ou}, depth => $depth}
360         );
361         $self->timelog("Got metarecords to records");
362
363         # force the metarecord result blob to match the format of regular search results
364         $results->{ids} = [map { [$_] } @{$results->{ids}}]; 
365
366     } else {
367
368         if (!$query) {
369             return Apache2::Const::OK if $internal;
370             return $self->generic_redirect;
371         }
372
373         # Limit and offset will stay here. Everything else should be part of
374         # the query string, not special args.
375         my $args = {'limit' => $limit, 'offset' => $offset};
376
377         if ($tag_circs) {
378             $args->{tag_circulated_records} = 1;
379             $args->{authtoken} = $self->editor->authtoken;
380         }
381
382         # Stuff these into the TT context so that templates can use them in redrawing forms
383         $ctx->{processed_search_query} = $query;
384
385         $query .= " $_" for @facets;
386
387         $logger->activity("EGWeb: [search] $query");
388
389         try {
390
391             my $method = 'open-ils.search.biblio.multiclass.query';
392             $method .= '.staff' if $ctx->{is_staff};
393
394             $self->timelog("Firing off the multiclass query");
395             $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
396             $self->timelog("Returned from the multiclass query");
397
398         } catch Error with {
399             my $err = shift;
400             $logger->error("multiclass search error: $err");
401             $results = {count => 0, ids => []};
402         };
403     }
404
405     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
406
407     $ctx->{ids} = $rec_ids;
408     $ctx->{hit_count} = $results->{count};
409     $ctx->{parsed_query} = $results->{parsed_query};
410
411     if ($find_last) {
412         # redirect to the record detail page for the last record in the results
413         my $rec_id = pop @$rec_ids;
414         $cgi->delete('find_last');
415         my $url = $cgi->url(-full => 1, -path => 1, -query => 1);
416         $url =~ s|/results|/record/$rec_id|;
417         return $self->generic_redirect($url);
418     }
419
420     return Apache2::Const::OK if @$rec_ids == 0 or $internal;
421
422     $self->load_rresults_bookbag_item_notes($rec_ids) if $ctx->{bookbag};
423
424     $self->timelog("Calling get_records_and_facets()");
425     my ($facets, @data) = $self->get_records_and_facets(
426         $rec_ids, $results->{facet_key}, 
427         {
428             flesh => '{holdings_xml,mra,acp,acnp,acns,bmp}',
429             site => $site,
430             depth => $depth,
431             pref_lib => $ctx->{pref_ou},
432         }
433     );
434     $self->timelog("Returned from get_records_and_facets()");
435
436     if ($page == 0) {
437         my $stat = $self->check_1hit_redirect($rec_ids);
438         return $stat if $stat;
439     }
440
441     # shove recs into context in search results order
442     for my $rec_id (@$rec_ids) {
443         push(
444             @{$ctx->{records}},
445             grep { $_->{id} == $rec_id } @data
446         );
447     }
448
449     if ($tag_circs) {
450         for my $rec (@{$ctx->{records}}) {
451             my ($res_rec) = grep { $_->[0] == $rec->{id} } @{$results->{ids}};
452             # index 1 in the per-record result array is a boolean which
453             # indicates whether the record in question is in the users
454             # accessible circ history list
455             $rec->{user_circulated} = 1 if $res_rec->[1];
456         }
457     }
458
459     $ctx->{search_facets} = $facets;
460
461     return Apache2::Const::OK;
462 }
463
464 # If the calling search results in 1 record and the client
465 # is configured to do so, redirect the search results to 
466 # the record details page.
467 sub check_1hit_redirect {
468     my ($self, $rec_ids) = @_;
469     my $ctx = $self->ctx;
470
471     return undef unless $rec_ids and @$rec_ids == 1;
472
473     my ($sname, $org);
474
475     $self->timelog("Checking whether to jump to details on a single hit");
476     if ($ctx->{is_staff}) {
477         $sname = 'opac.staff.jump_to_details_on_single_hit';
478         $org = $ctx->{user}->ws_ou;
479
480     } else {
481         $sname = 'opac.patron.jump_to_details_on_single_hit';
482         $org = $self->_get_search_lib();
483     }
484
485     $self->timelog("Return from checking whether to jump to details on a single hit");
486
487     return undef unless 
488         $self->ctx->{get_org_setting}->($org, $sname);
489
490     my $base_url = sprintf(
491         '%s://%s%s/record/%s',
492         $ctx->{proto}, 
493         $self->apache->hostname,
494         $self->ctx->{opac_root},
495         $$rec_ids[0],
496     );
497     
498     # If we get here from the same record detail page to which we
499     # now wish to redirect, do not perform the redirect.  This
500     # approach seems to work well, with the rare exception of 
501     # performing a new serach directly from the detail page that 
502     # happens to result in the same single hit.  In this case, the 
503     # user will be left on the search results page.  This could be 
504     # overcome w/ additional CGI, etc., but I'm not sure it's necessary.
505     if (my $referer = $ctx->{referer}) {
506         $referer =~ s/([^?]*).*/$1/g;
507         return undef if $base_url eq $referer;
508     }
509
510     return $self->generic_redirect($base_url . '?' . $self->cgi->query_string);
511 }
512
513 # Searching by barcode is a special search that does /not/ respect any other
514 # of the usual search parameters, not even the ones for sorting and paging!
515 sub item_barcode_shortcut {
516     my ($self) = @_;
517
518     $self->timelog("Searching for item_barcode");
519     my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
520     if (my $search = create OpenSRF::AppSession("open-ils.search")) {
521         my $rec_ids = $search->request(
522             $method, $self->cgi->param("query")
523         )->gather(1);
524         $search->kill_me;
525         $self->timelog("Finished searching for item_barcode");
526
527         if (ref $rec_ids ne 'ARRAY') {
528
529             if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
530                 $rec_ids = [];
531
532             } else {
533                 if (defined $U->event_code($rec_ids)) {
534                     $self->apache->log->warn(
535                         "$method returned event: " . $U->event_code($rec_ids)
536                     );
537                 } else {
538                     $self->apache->log->warn(
539                         "$method returned something unexpected: $rec_ids"
540                     );
541                 }
542                 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
543             }
544         }
545
546         $self->timelog("Calling get_records_and_facets() for item_barcode");
547         my ($facets, @data) = $self->get_records_and_facets(
548             $rec_ids, undef, {flesh => "{holdings_xml,mra,acnp,acns,bmp}"}
549         );
550         $self->timelog("Returned from calling get_records_and_facets() for item_barcode");
551
552         $self->ctx->{records} = [@data];
553         $self->ctx->{search_facets} = {};
554         $self->ctx->{hit_count} = scalar @data;
555         $self->ctx->{page_size} = $self->ctx->{hit_count};
556
557         return Apache2::Const::OK;
558     } {
559         $self->apache->log->warn("couldn't connect to open-ils.search");
560         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
561     }
562 }
563
564 # like item_barcode_search, this can't take all the usual search params, but
565 # this one will at least do site, limit and page
566 sub marc_expert_search {
567     my ($self, %args) = @_;
568
569     my @tags = $self->cgi->param("tag");
570     my @subfields = $self->cgi->param("subfield");
571     my @terms = $self->cgi->param("term");
572
573     my $query = [];
574     for (my $i = 0; $i < scalar @tags; $i++) {
575         next if ($tags[$i] eq "" || $terms[$i] eq "");
576         $subfields[$i] = '_' unless $subfields[$i];
577         push @$query, {
578             "term" => $terms[$i],
579             "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
580         };
581     }
582
583     $logger->info("query for expert search: " . Dumper($query));
584
585     $self->timelog("Getting search parameters");
586     # loc, limit and offset
587     my $page = $self->cgi->param("page") || 0;
588     my $limit = $self->_get_search_limit;
589     $self->ctx->{search_ou} = $self->_get_search_lib();
590     $self->ctx->{pref_ou} = $self->_get_pref_lib();
591     my $offset = $page * $limit;
592     $self->timelog("Got search parameters");
593
594     $self->ctx->{records} = [];
595     $self->ctx->{search_facets} = {};
596     $self->ctx->{page_size} = $limit;
597     $self->ctx->{hit_count} = 0;
598     $self->ctx->{ids} = [];
599     $self->ctx->{search_page} = $page;
600         
601     # nothing to do
602     return Apache2::Const::OK if @$query == 0;
603
604     if ($args{internal}) {
605         $limit = $offset + $limit + 1;
606         $offset = 0;
607     }
608
609     $self->timelog("Searching for MARC expert");
610     my $timeout = 120;
611     my $ses = OpenSRF::AppSession->create('open-ils.search');
612     my $req = $ses->request(
613         'open-ils.search.biblio.marc',
614         {searches => $query, org_unit => $self->ctx->{search_ou}}, 
615         $limit, $offset, $timeout);
616
617     my $resp = $req->recv($timeout);
618     my $results = $resp ? $resp->content : undef;
619     $ses->kill_me;
620     $self->timelog("Got our MARC expert results");
621
622     if (defined $U->event_code($results)) {
623         $self->apache->log->warn(
624             "open-ils.search.biblio.marc returned event: " .
625             $U->event_code($results)
626         );
627         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
628     }
629
630     $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
631     $self->ctx->{hit_count} = $results->{count};
632
633     return Apache2::Const::OK if @{$self->ctx->{ids}} == 0 or $args{internal};
634
635     if ($page == 0) {
636         my $stat = $self->check_1hit_redirect($self->ctx->{ids});
637         return $stat if $stat;
638     }
639
640     $self->timelog("Calling get_records_and_facets() for MARC expert");
641     my ($facets, @data) = $self->get_records_and_facets(
642         $self->ctx->{ids}, undef, {
643             flesh => "{holdings_xml,mra,acnp,acns}",
644             pref_lib => $self->ctx->{pref_ou},
645         }
646     );
647     $self->timelog("Returned from calling get_records_and_facets() for MARC expert");
648
649     $self->ctx->{records} = [@data];
650
651     return Apache2::Const::OK;
652 }
653
654 sub call_number_browse_standalone {
655     my ($self) = @_;
656
657     if (my $cnfrag = $self->cgi->param("query")) {
658         my $url = sprintf(
659             'http%s://%s%s/cnbrowse?cn=%s',
660             $self->cgi->https ? "s" : "",
661             $self->apache->hostname,
662             $self->ctx->{opac_root},
663             $cnfrag # XXX some kind of escaping needed here?
664         );
665         return $self->generic_redirect($url);
666     } else {
667         return $self->generic_redirect; # return to search page
668     }
669 }
670
671 sub load_cnbrowse {
672     my ($self) = @_;
673
674     $self->prepare_browse_call_numbers();
675
676     return Apache2::Const::OK;
677 }
678
679 sub get_staff_search_settings {
680     my ($self) = @_;
681
682     unless ($self->ctx->{is_staff}) {
683         $self->ctx->{staff_saved_search_size} = 0;
684         return;
685     }
686
687     $self->timelog("Getting staff search size");
688     my $sss_size = $self->ctx->{get_org_setting}->(
689         $self->ctx->{physical_loc} || $self->ctx->{aou_tree}->()->id,
690         "opac.staff_saved_search.size",
691     );
692     $self->timelog("Got staff search size");
693
694     # Sic: 0 is 0 (off), but undefined is 10.
695     $sss_size = 10 unless defined $sss_size;
696
697     $self->ctx->{staff_saved_search_size} = $sss_size;
698 }
699
700 sub staff_load_searches {
701     my ($self) = @_;
702
703     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
704
705     my $list = [];
706     if ($cache_key) {
707         $self->timelog("Getting anon_cache value");
708         $list = $U->simplereq(
709             "open-ils.actor",
710             "open-ils.actor.anon_cache.get_value",
711             $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
712         );
713         $self->timelog("Got anon_cache value");
714
715         unless ($list) {
716             undef $cache_key;
717             $list = [];
718         }
719     }
720
721     return ($cache_key, $list);
722 }
723
724 sub staff_save_search {
725     my ($self, $query) = @_;
726
727     my $sss_size = $self->ctx->{staff_saved_search_size}; 
728     return unless $sss_size > 0;
729
730     my ($cache_key, $list) = $self->staff_load_searches;
731     my %already = ( map { $_ => 1 } @$list );
732
733     unshift @$list, $query unless $already{$query};
734
735     splice @$list, $sss_size if scalar @$list > $sss_size;
736
737     $self->timelog("Setting anon_cache value");
738     $cache_key = $U->simplereq(
739         "open-ils.actor",
740         "open-ils.actor.anon_cache.set_value",
741         $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
742     );
743     $self->timelog("Set anon_cache value");
744
745     return ($cache_key, $list);
746 }
747
748 1;