]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen into template-toolkit-opac
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Record.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 my $U = 'OpenILS::Application::AppUtils';
9
10 # context additions: 
11 #   record : bre object
12 sub load_record {
13     my $self = shift;
14     my $ctx = $self->ctx;
15     $ctx->{page} = 'record';
16
17     my $org = $self->cgi->param('loc') || $ctx->{aou_tree}->()->id;
18     my $depth = $self->cgi->param('depth') || 0;
19     my $copy_limit = int($self->cgi->param('copy_limit') || 10);
20     my $copy_offset = int($self->cgi->param('copy_offset') || 0);
21
22     my $rec_id = $ctx->{page_args}->[0]
23         or return Apache2::Const::HTTP_BAD_REQUEST;
24
25     # run copy retrieval in parallel to bib retrieval
26     # XXX unapi
27     my $copy_rec = OpenSRF::AppSession->create('open-ils.cstore')->request(
28         'open-ils.cstore.json_query.atomic', 
29         $self->mk_copy_query($rec_id, $org, $depth, $copy_limit, $copy_offset));
30
31     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {flesh => '{holdings_xml,mra}'});
32     $ctx->{bre_id} = $rec_data[0]->{id};
33     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
34
35     $ctx->{copies} = $copy_rec->gather(1);
36     $ctx->{copy_limit} = $copy_limit;
37     $ctx->{copy_offset} = $copy_offset;
38
39     $ctx->{have_holdings_to_show} = 0;
40
41     # XXX TODO we'll also need conditional logic to show MFHD-based holdings
42     if (
43         $ctx->{get_org_setting}->
44             ($org, "opac.fully_compressed_serial_holdings")
45     ) {
46         $ctx->{holding_summaries} =
47             $self->get_holding_summaries($rec_id, $org, $depth);
48
49         $ctx->{have_holdings_to_show} =
50             scalar(@{$ctx->{holding_summaries}->{basic}}) ||
51             scalar(@{$ctx->{holding_summaries}->{index}}) ||
52             scalar(@{$ctx->{holding_summaries}->{supplement}});
53     }
54
55     for my $expand ($self->cgi->param('expand')) {
56         $ctx->{"expand_$expand"} = 1;
57         if ($expand eq 'marchtml') {
58             $ctx->{marchtml} = $self->mk_marc_html($rec_id);
59         } elsif ($expand eq 'issues' and $ctx->{have_holdings_to_show}) {
60             $ctx->{expanded_holdings} =
61                 $self->get_expanded_holdings($rec_id, $org, $depth);
62         }
63     }
64
65     return Apache2::Const::OK;
66 }
67
68 sub mk_copy_query {
69     my $self = shift;
70     my $rec_id = shift;
71     my $org = shift;
72     my $depth = shift;
73     my $copy_limit = shift;
74     my $copy_offset = shift;
75
76     my $query = {
77         select => {
78             acp => ['id', 'barcode', 'circ_lib', 'create_date', 'age_protect', 'holdable'],
79             acpl => [
80                 {column => 'name', alias => 'copy_location'},
81                 {column => 'holdable', alias => 'location_holdable'}
82             ],
83             ccs => [
84                 {column => 'name', alias => 'copy_status'},
85                 {column => 'holdable', alias => 'status_holdable'}
86             ],
87             acn => [
88                 {column => 'label', alias => 'call_number_label'},
89                 {column => 'id', alias => 'call_number'}
90             ],
91             circ => ['due_date'],
92         },
93         from => {
94             acp => {
95                 acn => {},
96                 acpl => {},
97                 ccs => {},
98                 circ => {type => 'left'},
99                 aou => {}
100             }
101         },
102         where => {
103             '+acp' => {
104                 deleted => 'f',
105                 call_number => {
106                     in => {
107                         select => {acn => ['id']},
108                         from => 'acn',
109                         where => {record => $rec_id}
110                     }
111                 },
112                 circ_lib => {
113                     in => {
114                         select => {aou => [{
115                             column => 'id', 
116                             transform => 'actor.org_unit_descendants', 
117                             result_field => 'id', 
118                             params => [$depth]
119                         }]},
120                         from => 'aou',
121                         where => {id => $org}
122                     }
123                 }
124             },
125             '+acn' => {deleted => 'f'},
126             '+circ' => {checkin_time => undef}
127         },
128
129         # Order is: copies with circ_lib=org, followed by circ_lib name, followed by call_number label
130         order_by => [
131             {class => 'aou', field => 'name'}, 
132             {class => 'acn', field => 'label'}
133         ],
134
135         limit => $copy_limit,
136         offset => $copy_offset
137     };
138
139     # Filter hidden items if this is the public catalog
140     unless($self->ctx->{is_staff}) { 
141         $query->{where}->{'+acp'}->{opac_visible} = 't';
142         $query->{where}->{'+acpl'}->{opac_visible} = 't';
143         $query->{where}->{'+ccs'}->{opac_visible} = 't';
144     }
145
146     return $query;
147     #return $self->editor->json_query($query);
148 }
149
150 sub mk_marc_html {
151     my($self, $rec_id) = @_;
152
153     # could be optimized considerably by performing the xslt on the already fetched record
154     return $U->simplereq(
155         'open-ils.search', 
156         'open-ils.search.biblio.record.html', $rec_id, 1);
157 }
158
159 sub get_holding_summaries {
160     my ($self, $rec_id, $org, $depth) = @_;
161
162     return (
163         create OpenSRF::AppSession("open-ils.serial")->request(
164             "open-ils.serial.bib.summary_statements",
165             $rec_id, {"org_id" => $org, "depth" => $depth}
166         )->gather(1)
167     );
168 }
169
170 sub get_expanded_holdings {
171     my ($self, $rec_id, $org, $depth) = @_;
172
173     my $holding_limit = int($self->cgi->param("holding_limit") || 10);
174     my $holding_offset = int($self->cgi->param("holding_offset") || 0);
175     my $type = $self->cgi->param("expand_holding_type");
176
177     return create OpenSRF::AppSession("open-ils.serial")->request(
178         "open-ils.serial.received_siss.retrieve.by_bib.atomic",
179         $rec_id, {
180             "ou" => $org, "depth" => $depth,
181             "limit" => $holding_limit, "offset" => $holding_offset,
182             "type" => $type
183         }
184     )->gather(1);
185 }
186
187
188 1;