use OpenILS::Utils::CStoreEditor qw/:funcs/;
use OpenILS::Utils::Fieldmapper;
use OpenILS::Application::AppUtils;
+use Net::HTTP::NB;
+use IO::Select;
my $U = 'OpenILS::Application::AppUtils';
our $ac_types = ['toc', 'anotes', 'excerpt', 'summary', 'reviews'];
$ctx->{page} = 'record';
$self->timelog("load_record() began");
+
+ my $rec_id = $ctx->{page_args}->[0]
+ or return Apache2::Const::HTTP_BAD_REQUEST;
+
+ $self->added_content_stage1($rec_id);
+ $self->timelog("past added content stage 1");
+
my $org = $self->_get_search_lib();
my $org_name = $ctx->{get_aou}->($org)->shortname;
my $pref_ou = $self->_get_pref_lib();
my $copy_limit = int($self->cgi->param('copy_limit') || 10);
my $copy_offset = int($self->cgi->param('copy_offset') || 0);
- my $rec_id = $ctx->{page_args}->[0]
- or return Apache2::Const::HTTP_BAD_REQUEST;
-
$self->get_staff_search_settings;
if ($ctx->{staff_saved_search_size}) {
$ctx->{saved_searches} = ($self->staff_load_searches)[1];
}
$self->timelog("past expandies");
+
+ $self->added_content_stage2($rec_id);
+
+ $self->timelog("past added content stage 2");
+
return Apache2::Const::OK;
}
return Apache2::Const::OK;
}
-sub added_content_avail1 {
+# ctx.added_content.$type.status:
+# 1 == available
+# 2 == not available
+# 3 == unknown
+sub added_content_stage1 {
my $self = shift;
+ my $rec_id = shift;
my $ctx = $self->ctx;
- $ctx->{added_content} = {
- map {$_ => {status => 3, content => ''}} @$ac_types;
- };
+ my $sel_type = $self->cgi->param('ac') || '';
+ my $key = $self->get_ac_key($rec_id);
+ ($key = $key->{value}) =~ s/^\s+//g if $key;
+
+ $ctx->{added_content} = {};
+ for my $type (@$ac_types) {
+ $ctx->{added_content}->{$type} = {content => ''};
+ $ctx->{added_content}->{$type}->{status} = $key ? 3 : 2;
+
+ if ($key) {
+ $logger->debug("tpac: starting added content request for $key => $type");
+
+ my $req = Net::HTTP::NB->new(Host => $self->apache->hostname);
+
+ if (!$req) {
+ $logger->warn("Unable to fetch added content from " . $self->apache->hostname . ": $@");
+ next;
+ }
- # fire requests
+ my $http_type = ($type eq $sel_type) ? 'GET' : 'HEAD';
+ $req->write_request($http_type => "/opac/extras/ac/$type/html/$key");
+ $ctx->{added_content}->{$type}->{request} = $req;
+ }
+ }
}
-sub added_content_avail2 {
+# check each outstanding request. If it's ready to be read, read
+# the HTTP status and use it to determine if content is available.
+sub added_content_stage2 {
my $self = shift;
my $ctx = $self->ctx;
+ my $sel_type = $self->cgi->param('ac') || '';
+
+ for my $type (keys %{$ctx->{added_content}}) {
+ my $content = $ctx->{added_content}->{$type};
+
+ if ($content->{status} == 3) {
+ $logger->debug("tpac: finishing added content request for $type");
+
+ my $req = $content->{request};
+ my $sel = IO::Select->new($req);
- # collect requests
+ if ($sel->can_read(0)) {
+ my ($code) = $req->read_response_headers;
+ $content->{status} = $code eq '200' ? 1 : 2;
+ $logger->debug("tpac: added content request for $type returned $code");
+
+ if ($type eq $sel_type) {
+ while (1) {
+ my $buf;
+ my $n = $req->read_entity_body($buf, 1024);
+ last unless $n;
+ $content->{content} .= $buf;
+ }
+ }
+ }
+ }
+ }
}
-sub added_content_get_type {
+# XXX this is copied directly from AddedContent.pm in
+# working/user/jeff/ac_by_record_id_rebase. When Jeff's
+# branch is merged and Evergreen gets added content
+# lookup by ID, this can be removed.
+# returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
+sub get_ac_key {
my $self = shift;
- my $ctx = $self->ctx;
+ my $rec_id = shift;
+ my $key_data = $self->editor->json_query({
+ select => {mfr => ['tag', 'value']},
+ from => 'mfr',
+ where => {
+ record => $rec_id,
+ '-or' => [
+ {
+ '-and' => [
+ {tag => '020'},
+ {subfield => 'a'}
+ ]
+ }, {
+ '-and' => [
+ {tag => '024'},
+ {subfield => 'a'},
+ {ind1 => 1}
+ ]
+ }
+ ]
+ }
+ });
+
+ return (
+ grep {$_->{tag} eq '020'} @$key_data,
+ grep {$_->{tag} eq '024'} @$key_data
+ )[0];
}
1;
summary => l('Summary')
};
- tab_class = 'ac_tab';
+ selected_type = CGI.param('ac');
# For each type of added content, render the link if it's known to have
# content, do not render the link if it's known to not have content. If
# the content status is unknown, render the link, but hide the link via CSS
# if dojo is enabled. If dojo is not enabled, render and display the link.
- %]
+%]
<div id='ac_tab_wrapper'>
- [% FOR type IN ac_types.keys
+ [% FOR type IN ac_types.keys;
+ tab_class = 'ac_tab';
IF ctx.added_content.$type.status != '2'; # no content
IF ctx.added_content.$type.status == '3' AND want_dojo; # status unknown
- tab_class = tab_class _ 'hidden';
+ tab_class = tab_class _ ' hidden';
END %]
<div class="[% tab_class %]" id="ac:[% type %]">
<a href="[% mkurl('', {ac => type}) _ '#addedcontent' %]">[% ac_types.$type %]</a>
</div>
- [% END %]
+ [% END;
+ END %]
</div>
<div id='ac_content'>
<hr/>
[%
- ac_type = CGI.param('ac');
- IF ac_type;
- content = ctx.added_content.$ac_type.content;
+ IF selected_type;
+ content = ctx.added_content.$selected_type.content;
IF content;
content;
ELSE;