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