]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
LP#1452352: don't leak $ctx when initializing TPAC R/O object cache
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Util.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 File::Spec;
5 use Time::HiRes qw/time sleep/;
6 use OpenSRF::Utils::Cache;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 use OpenILS::Utils::CStoreEditor qw/:funcs/;
9 use OpenILS::Utils::Fieldmapper;
10 use OpenILS::Application::AppUtils;
11 use OpenSRF::MultiSession;
12
13 my $U = 'OpenILS::Application::AppUtils';
14
15 my $ro_object_subs; # cached subs
16 our %cache = ( # cached data
17     map => {en_us => {}},
18     list => {en_us => {}},
19     search => {en_us => {}},
20     org_settings => {en_us => {}},
21     search_filter_groups => {en_us => {}},
22     aou_tree => {en_us => undef},
23     aouct_tree => {},
24     eg_cache_hash => undef,
25     authority_fields => {en_us => {}}
26 );
27
28 sub init_ro_object_cache {
29     my $self = shift;
30     my $e = $self->editor;
31     my $ctx = $self->ctx;
32
33     # reset org unit setting cache on each page load to avoid the
34     # requirement of reloading apache with each org-setting change
35     $cache{org_settings} = {};
36
37     if($ro_object_subs->{$ctx->{locale}}) {
38         # subs have been built.  insert into the context then move along.
39         $ctx->{$_} = $ro_object_subs->{$ctx->{locale}}->{$_} for keys %{ $ro_object_subs->{$ctx->{locale}} };
40         return;
41     }
42
43     my $locale_subs = {};
44     my $locale = $ctx->{locale};
45
46     # make all "field_safe" classes accesible by default in the template context
47     my @classes = grep {
48         ($Fieldmapper::fieldmap->{$_}->{field_safe} || '') =~ /true/i
49     } keys %{ $Fieldmapper::fieldmap };
50
51     for my $class (@classes) {
52
53         my $hint = $Fieldmapper::fieldmap->{$class}->{hint};
54         next if $hint eq 'aou'; # handled separately
55
56         my $ident_field =  $Fieldmapper::fieldmap->{$class}->{identity};
57         (my $eclass = $class) =~ s/Fieldmapper:://o;
58         $eclass =~ s/::/_/g;
59
60         my $list_key = "${hint}_list";
61         my $get_key = "get_$hint";
62         my $search_key = "search_$hint";
63
64         # Retrieve the full set of objects with class $hint
65         $locale_subs->{$list_key} = sub {
66             my $method = "retrieve_all_$eclass";
67             $cache{list}{$locale}{$hint} = $e->$method() unless $cache{list}{$locale}{$hint};
68             return $cache{list}{$locale}{$hint};
69         };
70
71         # locate object of class $hint with Ident field $id
72         $cache{map}{$hint} = {};
73         $locale_subs->{$get_key} = sub {
74             my $id = shift;
75             return $cache{map}{$locale}{$hint}{$id} if $cache{map}{$locale}{$hint}{$id};
76             ($cache{map}{$locale}{$hint}{$id}) = grep { $_->$ident_field eq $id } @{$locale_subs->{$list_key}->()};
77             return $cache{map}{$locale}{$hint}{$id};
78         };
79
80         # search for objects of class $hint where field=value
81         $cache{search}{$hint} = {};
82         $locale_subs->{$search_key} = sub {
83             my ($field, $val, $filterfield, $filterval) = @_;
84             my $method = "search_$eclass";
85             my $cacheval = $val;
86             if (ref $val) {
87                 $val = [sort(@$val)] if ref $val eq 'ARRAY';
88                 $cacheval = OpenSRF::Utils::JSON->perl2JSON($val);
89                 #$self->apache->log->info("cacheval : $cacheval");
90             }
91             my $search_obj = {$field => $val};
92             if($filterfield) {
93                 $search_obj->{$filterfield} = $filterval;
94                 $cacheval .= ':' . $filterfield . ':' . $filterval;
95             }
96             #$cache{search}{$locale}{$hint}{$field} = {} unless $cache{search}{$locale}{$hint}{$field};
97             $cache{search}{$locale}{$hint}{$field}{$cacheval} = $e->$method($search_obj)
98                 unless $cache{search}{$locale}{$hint}{$field}{$cacheval};
99             return $cache{search}{$locale}{$hint}{$field}{$cacheval};
100         };
101     }
102
103     $locale_subs->{aou_tree} = sub {
104
105         # fetch the org unit tree
106         unless($cache{aou_tree}{$locale}) {
107             my $tree = $e->search_actor_org_unit([
108                 {   parent_ou => undef},
109                 {   flesh            => -1,
110                     flesh_fields    => {aou =>  ['children']},
111                     order_by        => {aou => 'name'}
112                 }
113             ])->[0];
114
115             # flesh the org unit type for each org unit
116             # and simultaneously set the id => aou map cache
117             sub flesh_aout {
118                 my $node = shift;
119                 my $locale_subs = shift;
120                 my $locale = shift;
121                 $node->ou_type( $locale_subs->{get_aout}->($node->ou_type) );
122                 $cache{map}{$locale}{aou}{$node->id} = $node;
123                 flesh_aout($_, $locale_subs, $locale) foreach @{$node->children};
124             };
125             flesh_aout($tree, $locale_subs, $locale);
126
127             $cache{aou_tree}{$locale} = $tree;
128         }
129
130         return $cache{aou_tree}{$locale};
131     };
132
133     # Add a special handler for the tree-shaped org unit cache
134     $locale_subs->{get_aou} = sub {
135         my $org_id = shift;
136         return undef unless defined $org_id;
137         $locale_subs->{aou_tree}->(); # force the org tree to load
138         return $cache{map}{$locale}{aou}{$org_id};
139     };
140
141     # Returns a flat list of aou objects.  often easier to manage than a tree.
142     $locale_subs->{aou_list} = sub {
143         $locale_subs->{aou_tree}->(); # force the org tree to load
144         return [ values %{$cache{map}{$locale}{aou}} ];
145     };
146
147     # returns the org unit object by shortname
148     $locale_subs->{get_aou_by_shortname} = sub {
149         my $sn = shift or return undef;
150         my $list = $locale_subs->{aou_list}->();
151         return (grep {$_->shortname eq $sn} @$list)[0];
152     };
153
154     $locale_subs->{aouct_tree} = sub {
155
156         # fetch the org unit tree
157         unless(exists $cache{aouct_tree}{$locale}) {
158             $cache{aouct_tree}{$locale} = undef;
159
160             my $tree_id = $e->search_actor_org_unit_custom_tree(
161                 {purpose => 'opac', active => 't'},
162                 {idlist => 1}
163             )->[0];
164
165             if ($tree_id) {
166                 my $node_tree = $e->search_actor_org_unit_custom_tree_node([
167                 {parent_node => undef, tree => $tree_id},
168                 {   flesh        => -1,
169                     flesh_fields => {aouctn => ['children', 'org_unit']},
170                     order_by     => {aouctn => 'sibling_order'}
171                 }
172                 ])->[0];
173
174                 # tree-ify the org units.  note that since the orgs are fleshed
175                 # upon retrieval, this org tree will not clobber ctx->{aou_tree}.
176                 my @nodes = ($node_tree);
177                 while (my $node = shift(@nodes)) {
178                     my $aou = $node->org_unit;
179                     $aou->children([]);
180                     for my $cnode (@{$node->children}) {
181                         my $child_org = $cnode->org_unit;
182                         $child_org->parent_ou($aou->id);
183                         $child_org->ou_type( $locale_subs->{get_aout}->($child_org->ou_type) );
184                         push(@{$aou->children}, $child_org);
185                         push(@nodes, $cnode);
186                     }
187                 }
188
189                 $cache{aouct_tree}{$locale} = 
190                     $node_tree->org_unit if $node_tree;
191             }
192         }
193
194         return $cache{aouct_tree}{$locale};
195     };
196
197     # turns an ISO date into something TT can understand
198     $locale_subs->{parse_datetime} = sub {
199         my $date = shift;
200
201         # Calling parse_datetime() with empty $date will lead to Internal Server Error
202         return '' if (!defined($date) or $date eq '');
203
204         # Probably an accidental entry like '0212' instead of '2012',
205         # but 1) the leading 0 may get stripped in cstore and
206         # 2) DateTime::Format::ISO8601 returns an error as years
207         # must be 2 or 4 digits
208         if ($date =~ m/^\d{3}-/) {
209             $logger->warn("Invalid date had a 3-digit year: $date");
210             $date = '0' . $date;
211         } elsif ($date =~ m/^\d{1}-/) {
212             $logger->warn("Invalid date had a 1-digit year: $date");
213             $date = '000' . $date;
214         }
215
216         my $cleansed_date = cleanse_ISO8601($date);
217
218         $date = DateTime::Format::ISO8601->new->parse_datetime($cleansed_date);
219         return sprintf(
220             "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
221             $date->hour,
222             $date->minute,
223             $date->second,
224             $date->day,
225             $date->month,
226             $date->year
227         );
228     };
229
230     # retrieve and cache org unit setting values
231     $locale_subs->{get_org_setting} = sub {
232         my($org_id, $setting) = @_;
233
234         $cache{org_settings}{$locale}{$org_id}{$setting} =
235             $U->ou_ancestor_setting_value($org_id, $setting)
236                 unless exists $cache{org_settings}{$locale}{$org_id}{$setting};
237
238         return $cache{org_settings}{$locale}{$org_id}{$setting};
239     };
240
241     # retrieve and cache acsaf values
242     $locale_subs->{get_authority_fields} = sub {
243         my ($control_set) = @_;
244
245         if (not exists $cache{authority_fields}{$locale}{$control_set}) {
246             my $acs = $e->search_authority_control_set_authority_field(
247                 {control_set => $control_set}
248             ) or return;
249             $cache{authority_fields}{$locale}{$control_set} =
250                 +{ map { $_->id => $_ } @$acs };
251         }
252
253         return $cache{authority_fields}{$locale}{$control_set};
254     };
255
256     $ctx->{$_} = $locale_subs->{$_} for keys %$locale_subs;
257     $ro_object_subs->{$locale} = $locale_subs;
258 }
259
260 sub generic_redirect {
261     my $self = shift;
262     my $url = shift;
263     my $cookie = shift; # can be an array of cgi.cookie's
264
265     $self->apache->print(
266         $self->cgi->redirect(
267             -url => $url || 
268                 $self->cgi->param('redirect_to') || 
269                 $self->ctx->{referer} || 
270                 $self->ctx->{home_page},
271             -cookie => $cookie
272         )
273     );
274
275     return Apache2::Const::REDIRECT;
276 }
277
278 my $unapi_cache;
279 sub get_records_and_facets {
280     my ($self, $rec_ids, $facet_key, $unapi_args) = @_;
281
282     $unapi_args ||= {};
283     $unapi_args->{site} ||= $self->ctx->{aou_tree}->()->shortname;
284     $unapi_args->{depth} ||= $self->ctx->{aou_tree}->()->ou_type->depth;
285     $unapi_args->{flesh_depth} ||= 5;
286
287     my $is_meta = delete $unapi_args->{metarecord};
288     my $unapi_type = $is_meta ? 'unapi.mmr' : 'unapi.bre';
289
290     $unapi_cache ||= OpenSRF::Utils::Cache->new('global');
291     my $unapi_cache_key_suffix = join(
292         '_',
293         $is_meta || 0,
294         $unapi_args->{site},
295         $unapi_args->{depth},
296         $unapi_args->{flesh_depth},
297         ($unapi_args->{pref_lib} || '')
298     );
299
300     my %tmp_data;
301     my $outer_self = $self;
302     $self->timelog("get_records_and_facets(): about to call multisession");
303     my $ses = OpenSRF::MultiSession->new(
304         app => 'open-ils.cstore',
305         cap => 10, # XXX config
306         success_handler => sub {
307             my($self, $req) = @_;
308             my $data = $req->{response}->[0]->content;
309
310             $outer_self->timelog("get_records_and_facets(): got response content");
311
312             # Protect against requests for non-existent records
313             return unless $data->{$unapi_type};
314
315             my $xml = XML::LibXML->new->parse_string($data->{$unapi_type})->documentElement;
316
317             $outer_self->timelog("get_records_and_facets(): parsed xml");
318             # Protect against legacy invalid MARCXML that might not have a 901c
319             my $bre_id;
320             my $mmr_id;
321             my $bre_id_nodes =  $xml->find('*[@tag="901"]/*[@code="c"]');
322             if ($bre_id_nodes) {
323                 $bre_id =  $bre_id_nodes->[0]->textContent;
324             } else {
325                 $logger->warn("Missing 901 subfield 'c' in " . $xml->toString());
326             }
327
328             if ($is_meta) {
329                 # extract metarecord ID from mmr.unapi tag
330                 for my $node ($xml->getElementsByTagName('abbr')) {
331                     my $title = $node->getAttribute('title');
332                     ($mmr_id = $title) =~ 
333                         s/tag:open-ils.org:U2\@mmr\/(\d+)\/.*/$1/g;
334                     last if $mmr_id;
335                 }
336             }
337
338             my $rec_id = $mmr_id ? $mmr_id : $bre_id;
339             $tmp_data{$rec_id} = {
340                 id => $rec_id, 
341                 bre_id => $bre_id, 
342                 mmr_id => $mmr_id,
343                 marc_xml => $xml
344             };
345
346             if ($rec_id) {
347                 # Let other backends grab our data now that we're done.
348                 my $key = 'TPAC_unapi_cache_'.$rec_id.'_'.$unapi_cache_key_suffix;
349                 my $cache_data = $unapi_cache->get_cache($key);
350                 if ($$cache_data{running}) {
351                     $unapi_cache->put_cache($key, {
352                         bre_id => $bre_id,
353                         mmr_id => $mmr_id,
354                         id => $rec_id, 
355                         marc_xml => $data->{$unapi_type} 
356                     }, 10);
357                 }
358             }
359
360             $outer_self->timelog("get_records_and_facets(): end of success handler");
361         }
362     );
363
364     $self->timelog("get_records_and_facets(): about to call ".
365         "$unapi_type via json_query (rec_ids has " . scalar(@$rec_ids));
366
367     my @loop_recs = @$rec_ids;
368     my %rec_timeout;
369
370     while (my $bid = shift @loop_recs) {
371
372         sleep(0.1) if $rec_timeout{$bid};
373
374         my $unapi_cache_key = 'TPAC_unapi_cache_'.$bid.'_'.$unapi_cache_key_suffix;
375         my $unapi_data = $unapi_cache->get_cache($unapi_cache_key) || {};
376
377         if ($unapi_data->{running}) { #cache entry from ongoing, concurrent retrieval
378             if (!$rec_timeout{$bid}) {
379                 $rec_timeout{$bid} = time() + 10;
380             }
381
382             if ( time() > $rec_timeout{$bid} ) { # we've waited too long. just do it
383                 $unapi_data = {};
384                 delete $rec_timeout{$bid};
385             } else { # we'll pause next time around to let this one try again
386                 push(@loop_recs, $bid);
387                 next;
388             }
389         }
390
391         if ($unapi_data->{marc_xml}) { # we got data from the cache
392             $unapi_data->{marc_xml} = XML::LibXML->new->parse_string($unapi_data->{marc_xml})->documentElement;
393             $tmp_data{$unapi_data->{id}} = $unapi_data;
394         } else { # we're the first or we timed out. success_handler will populate the real value
395             $unapi_cache->put_cache($unapi_cache_key, { running => $$ }, 10);
396
397             my $sdepth = $unapi_args->{flesh_depth};
398             my $slimit = "acn=>$sdepth,acp=>$sdepth";
399             $slimit .= ",bre=>$sdepth" if $is_meta;
400             my $flesh = $unapi_args->{flesh} || '';
401
402             # tag the record with the MR id
403             $flesh =~ s/}$/,mmr.unapi}/g if $is_meta;
404
405             $ses->request(
406                 'open-ils.cstore.json_query',
407                  {from => [
408                     $unapi_type, $bid, 'marcxml','record', $flesh,
409                     $unapi_args->{site}, 
410                     $unapi_args->{depth}, 
411                     $slimit,
412                     undef, undef, $unapi_args->{pref_lib}
413                 ]}
414             );
415         }
416     }
417
418
419     $self->timelog("get_records_and_facets():almost ready to fetch facets");
420     # collect the facet data
421     my $search = OpenSRF::AppSession->create('open-ils.search');
422     my $facet_req = $search->request(
423         'open-ils.search.facet_cache.retrieve', $facet_key
424     ) if $facet_key;
425
426     # gather up the unapi recs
427     $ses->session_wait(1);
428     $self->timelog("get_records_and_facets():past session wait");
429
430     my $facets = {};
431     if ($facet_key) {
432         my $tmp_facets = $facet_req->gather(1);
433         $self->timelog("get_records_and_facets(): gathered facet data");
434         for my $cmf_id (keys %$tmp_facets) {
435
436             # sort highest to lowest match count
437             my @entries;
438             my $entries = $tmp_facets->{$cmf_id};
439             for my $ent (keys %$entries) {
440                 push(@entries, {value => $ent, count => $$entries{$ent}});
441             };
442
443             # Sort facet entries by 1) count descending, 2) text ascending
444             @entries = sort {
445                 $b->{count} <=> $a->{count} ||
446                 $a->{value} cmp $b->{value}
447             } @entries;
448
449             $facets->{$cmf_id} = {
450                 cmf => $self->ctx->{get_cmf}->($cmf_id),
451                 data => \@entries
452             }
453         }
454         $self->timelog("get_records_and_facets(): gathered/sorted facet data");
455     } else {
456         $facets = undef;
457     }
458
459     $search->kill_me;
460
461     return ($facets, map { $tmp_data{$_} } @$rec_ids);
462 }
463
464 sub _resolve_org_id_or_shortname {
465     my ($self, $str) = @_;
466
467     if (length $str) {
468         # Match on shortname case insensitively, but only if there's exactly
469         # one match.  We wouldn't want the system to arbitrarily interpret
470         # 'foo' as either the org unit with shortname 'FOO' or 'Foo' and fail
471         # to make it clear to the user which one was chosen and why.
472         my $res = $self->editor->search_actor_org_unit({
473             shortname => {
474                 '=' => {
475                     transform => 'evergreen.lowercase',
476                     value => lc($str)
477                 }
478             }
479         });
480         return $res->[0]->id if $res and @$res == 1;
481     }
482
483     # Note that we don't validate IDs; we only try a shortname lookup and then
484     # assume anything else must be an ID.
485     return int($str); # Wrapping in int() prevents 500 on unmatched string.
486 }
487
488 sub _get_search_lib {
489     my $self = shift;
490     my $ctx = $self->ctx;
491
492     # avoid duplicate lookups
493     return $ctx->{search_ou} if $ctx->{search_ou};
494
495     my $loc = $ctx->{copy_location_group_org};
496     return $loc if $loc;
497
498     # loc param takes precedence
499     # XXX ^-- over what exactly? We could use clarification here. To me it looks
500     # like locg takes precedence over loc which in turn takes precedence over
501     # request headers which take precedence over pref_lib (which can be
502     # specified a lot of different ways and eventually falls back to
503     # physical_loc) and it all finally defaults to top of the org tree.
504     # To say nothing of all the code that doesn't look to this function at all
505     # but rather accesses some subset of these inputs directly.
506
507     $loc = $self->cgi->param('loc');
508     return $loc if $loc;
509
510     if ($self->apache->headers_in->get('OILS-Search-Lib')) {
511         return $self->apache->headers_in->get('OILS-Search-Lib');
512     }
513
514     my $pref_lib = $self->_get_pref_lib();
515     return $pref_lib if $pref_lib;
516
517     return $ctx->{aou_tree}->()->id;
518 }
519
520 sub _get_pref_lib {
521     my $self = shift;
522     my $ctx = $self->ctx;
523
524     # plib param takes precedence
525     my $plib = $self->cgi->param('plib');
526     return $plib if $plib;
527
528     if ($self->apache->headers_in->get('OILS-Pref-Lib')) {
529         return $self->apache->headers_in->get('OILS-Pref-Lib');
530     }
531
532     if ($ctx->{user}) {
533         # See if the user has a search library preference
534         my $lset = $self->editor->search_actor_user_setting({
535             usr => $ctx->{user}->id, 
536             name => 'opac.default_search_location'
537         })->[0];
538         return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
539
540         # Otherwise return the user's home library
541         my $ou = $ctx->{user}->home_ou;
542         return ref($ou) ? $ou->id : $ou;
543     }
544
545     if ($ctx->{physical_loc}) {
546         return $ctx->{physical_loc};
547     }
548
549 }
550
551 # This is defensively coded since we don't do much manual reading from the
552 # file system in this module.
553 sub load_eg_cache_hash {
554     my ($self) = @_;
555
556     # just a context helper
557     $self->ctx->{eg_cache_hash} = sub { return $cache{eg_cache_hash}; };
558
559     # Need to actually load the value? If already done, move on.
560     return if defined $cache{eg_cache_hash};
561
562     # In this way even if we fail, we won't slow things down by ever trying
563     # again within this Apache process' lifetime.
564     $cache{eg_cache_hash} = 0;
565
566     my $path = File::Spec->catfile(
567         $self->apache->document_root, "eg_cache_hash"
568     );
569
570     if (not open FH, "<$path") {
571         $self->apache->log->warn("error opening $path : $!");
572         return;
573     } else {
574         my $buf;
575         my $rv = read FH, $buf, 64;  # defensive
576         close FH;
577
578         if (not defined $rv) {  # error
579             $self->apache->log->warn("error reading $path : $!");
580         } elsif ($rv > 0) {     # no error, something read
581             chomp $buf;
582             $cache{eg_cache_hash} = $buf;
583         }
584     }
585 }
586
587 # Extracts the copy location org unit and group from the 
588 # "logc" param, which takes the form org_id:grp_id.
589 sub extract_copy_location_group_info {
590     my $self = shift;
591     my $ctx = $self->ctx;
592     if (my $clump = $self->cgi->param('locg')) {
593         my ($org, $grp) = split(/:/, $clump);
594         $ctx->{copy_location_group_org} =
595             $self->_resolve_org_id_or_shortname($org);
596         $ctx->{copy_location_group} = $grp if $grp;
597     }
598 }
599
600 sub load_copy_location_groups {
601     my $self = shift;
602     my $ctx = $self->ctx;
603
604     # User can access to the search location groups at the current 
605     # search lib, the physical location lib, and the patron's home ou.
606     my @ctx_orgs = $ctx->{search_ou};
607     push(@ctx_orgs, $ctx->{physical_loc}) if $ctx->{physical_loc};
608     push(@ctx_orgs, $ctx->{user}->home_ou) if $ctx->{user};
609
610     my $grps = $self->editor->search_asset_copy_location_group([
611         {
612             opac_visible => 't',
613             owner => {
614                 in => {
615                     select => {aou => [{
616                         column => 'id', 
617                         transform => 'actor.org_unit_full_path',
618                         result_field => 'id',
619                     }]},
620                     from => 'aou',
621                     where => {id => \@ctx_orgs}
622                 }
623             }
624         },
625         {order_by => {acplg => 'pos'}}
626     ]);
627
628     my %buckets;
629     push(@{$buckets{$_->owner}}, $_) for @$grps;
630     $ctx->{copy_location_groups} = \%buckets;
631 }
632
633 sub set_file_download_headers {
634     my $self = shift;
635     my $filename = shift;
636     my $ctype = shift || "text/plain; encoding=utf8";
637
638     $self->apache->content_type($ctype);
639
640     $self->apache->headers_out->add(
641         "Content-Disposition",
642         "attachment;filename=$filename"
643     );
644
645     return Apache2::Const::OK;
646 }
647
648 sub apache_log_if_event {
649     my ($self, $event, $prefix_text, $success_ok, $level) = @_;
650
651     $prefix_text ||= "Evergreen returned event";
652     $success_ok ||= 0;
653     $level ||= "warn";
654
655     chomp $prefix_text;
656     $prefix_text .= ": ";
657
658     my $code = $U->event_code($event);
659     if (defined $code and ($code or not $success_ok)) {
660         $self->apache->log->$level(
661             $prefix_text .
662             ($event->{textcode} || "") . " ($code)" .
663             ($event->{note} ? (": " . $event->{note}) : "")
664         );
665         return 1;
666     }
667
668     return;
669 }
670
671 sub load_search_filter_groups {
672     my $self = shift;
673     my $ctx_org = shift;
674     my $org_list = $U->get_org_ancestors($ctx_org, 1);
675
676     my %seen;
677     for my $org_id (@$org_list) {
678
679         my $grps;
680         if (! ($grps = $cache{search_filter_groups}{$org_id}) ) {
681             $grps = $self->editor->search_actor_search_filter_group([
682                 {owner => $org_id},
683                 {   flesh => 2, 
684                     flesh_fields => {
685                         asfg => ['entries'],
686                         asfge => ['query']
687                     },
688                     order_by => {asfge => 'pos'}
689                 }
690             ]);
691             $cache{search_filter_groups}{$org_id} = $grps;
692         }
693
694         # for the current context, if a descendant org has a group 
695         # with a matching code replace the group from the parent.
696         $seen{$_->code} = $_ for @$grps;
697     }
698
699     return $self->ctx->{search_filter_groups} = \%seen;
700 }
701
702
703 sub check_for_temp_list_warning {
704     my $self = shift;
705     my $ctx = $self->ctx;
706     my $cgi = $self->cgi;
707
708     my $lib = $self->_get_search_lib;
709     my $warn = ($ctx->{get_org_setting}->($lib || 1, 'opac.patron.temporary_list_warn')) ? 1 : 0;
710
711     if ($warn && $ctx->{user}) {
712         $self->_load_user_with_prefs;
713         my $map = $ctx->{user_setting_map};
714         $warn = 0 if ($$map{'opac.temporary_list_no_warn'});
715     }
716
717     # Check for a cookie disabling the warning.
718     $warn = 0 if ($warn && $cgi->cookie('no_temp_list_warn'));
719
720     return $warn;
721 }
722
723 sub load_org_util_funcs {
724     my $self = shift;
725     my $ctx = $self->ctx;
726
727     # evaluates to true if test_ou is within the same depth-
728     # scoped tree as ctx_ou. both ou's are org unit objects.
729     $ctx->{org_within_scope} = sub {
730         my ($ctx_ou, $test_ou, $depth) = @_;
731
732         return 1 if $ctx_ou->id == $test_ou->id;
733
734         if ($depth) {
735
736             # find the top-most ctx-org ancestor at the provided depth
737             while ($depth < $ctx_ou->ou_type->depth 
738                     and $ctx_ou->id != $test_ou->id) {
739                 $ctx_ou = $ctx->{get_aou}->($ctx_ou->parent_ou);
740             }
741
742             # the preceeding loop may have landed on our org
743             return 1 if $ctx_ou->id == $test_ou->id;
744
745         } else {
746
747             return 1 if defined $depth; # $depth == 0;
748         }
749
750         for my $child (@{$ctx_ou->children}) {
751             return 1 if $ctx->{org_within_scope}->($child, $test_ou);
752         }
753
754         return 0;
755     };
756
757     # Returns true if the provided org unit is within the same 
758     # org unit hiding depth-scoped tree as the physical location.
759     # Org unit hiding is based on the immutable physical_loc
760     # and is not meant to change as search/pref/etc libs change
761     $ctx->{org_within_hiding_scope} = sub {
762         my $org_id = shift;
763         my $ploc = $ctx->{physical_loc} or return 1;
764
765         my $depth = $ctx->{get_org_setting}->(
766             $ploc, 'opac.org_unit_hiding.depth');
767
768         return 1 unless $depth; # 0 or undef
769
770         return $ctx->{org_within_scope}->( 
771             $ctx->{get_aou}->($ploc), 
772             $ctx->{get_aou}->($org_id), $depth);
773  
774     };
775
776     # Evaluates to true if the context org (defaults to get_library) 
777     # is not within the hiding scope.  Also evaluates to true if the 
778     # user's pref_ou is set and it's out of hiding scope.
779     # Always evaluates to true when ctx.is_staff
780     $ctx->{org_hiding_disabled} = sub {
781         my $ctx_org = shift || $ctx->{search_ou};
782
783         return 1 if $ctx->{is_staff};
784
785         # beware locg values formatted as org:loc
786         $ctx_org =~ s/:.*//g;
787
788         return 1 if !$ctx->{org_within_hiding_scope}->($ctx_org);
789
790         return 1 if $ctx->{pref_ou} and $ctx->{pref_ou} != $ctx_org 
791             and !$ctx->{org_within_hiding_scope}->($ctx->{pref_ou});
792
793         return 0;
794     };
795
796 }
797
798 # returns the list of org unit IDs for which the 
799 # selected org unit setting returned a true value
800 sub setting_is_true_for_orgs {
801     my ($self, $setting) = @_;
802     my $ctx = $self->ctx;
803     my @valid_orgs;
804
805     my $test_org;
806     $test_org = sub {
807         my $org = shift;
808         push (@valid_orgs, $org->id) if
809             $ctx->{get_org_setting}->($org->id, $setting);
810         $test_org->($_) for @{$org->children};
811     };
812
813     $test_org->($ctx->{aou_tree}->());
814     return \@valid_orgs;
815 }
816
817 # Builds and links a perm checking function, testing permissions against
818 # the currently logged in user.  
819 # ctx->{has_perm}->(perm_code, org_id) => 1/undef
820 # For security, perm checks are cached per page, not per process.
821 sub load_perm_funcs {
822     my $self = shift;
823     my %perm_cache;
824     $self->ctx->{has_perm} = sub {
825         my ($perm_code, $org_id) = @_;
826         return 0 unless $self->editor->requestor;
827
828         if ($perm_cache{$org_id}) {
829             return $perm_cache{$org_id}{$perm_code} 
830                 if exists $perm_cache{$org_id}{$perm_code};
831         } else {
832             $perm_cache{$org_id} = {};
833         }
834         return $perm_cache{$org_id}{$perm_code} =
835             $self->editor->allowed($perm_code, $org_id);
836     }
837 }
838     
839
840
841 1;