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