]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
added support for copy paging/sorting
[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     $self->ctx->{page} = 'record';
15
16     my $org = $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
17     my $depth = $self->cgi->param('depth') || 0;
18     my $copy_limit = $self->cgi->param('copy_limit');
19     my $copy_offset = $self->cgi->param('copy_offset');
20
21     my $rec_id = $self->ctx->{page_args}->[0]
22         or return Apache2::Const::HTTP_BAD_REQUEST;
23
24     # run copy retrieval in parallel to bib retrieval
25     my $copy_rec = OpenSRF::AppSession->create('open-ils.cstore')->request(
26         'open-ils.cstore.json_query.atomic', 
27         $self->mk_copy_query($rec_id, $org, $depth, $copy_limit, $copy_offset));
28
29     $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry($rec_id);
30     $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
31
32     $self->ctx->{copies} = $copy_rec->gather(1);
33
34     return Apache2::Const::OK;
35 }
36
37 sub mk_copy_query {
38     my $self = shift;
39     my $rec_id = shift;
40     my $org = shift;
41     my $depth = shift;
42     my $copy_limit = shift || 20;
43     my $copy_offset = shift || 0;
44
45     my $query = {
46         select => {
47             acp => ['id', 'barcode', 'circ_lib'],
48             acpl => [{column => 'name', alias => 'copy_location'}],
49             ccs => [{column => 'name', alias => 'copy_status'}],
50             acn => [
51                 {column => 'label', alias => 'call_number_label'},
52                 {column => 'id', alias => 'call_number'}
53             ],
54             circ => ['due_date'],
55         },
56         from => {
57             acp => {
58                 acn => {},
59                 acpl => {},
60                 ccs => {},
61                 circ => {type => 'left'},
62                 aou => {}
63             }
64         },
65         where => {
66             '+acp' => {
67                 deleted => 'f',
68                 call_number => {
69                     in => {
70                         select => {acn => ['id']},
71                         from => 'acn',
72                         where => {record => $rec_id}
73                     }
74                 },
75                 circ_lib => {
76                     in => {
77                         select => {aou => [{
78                             column => 'id', 
79                             transform => 'actor.org_unit_descendants', 
80                             result_field => 'id', 
81                             params => [$depth]
82                         }]},
83                         from => 'aou',
84                         where => {id => $org}
85                     }
86                 }
87             },
88             '+acn' => {deleted => 'f'},
89             '+circ' => {checkin_time => undef}
90         },
91
92         # Order is: copies with circ_lib=org, followed by circ_lib name, followed by call_number label
93         order_by => [
94             {class => 'aou', field => 'name'}, 
95             {class => 'acn', field => 'label'}
96         ],
97
98         limit => $copy_limit,
99         offset => $copy_offset
100     };
101
102     # Filter hidden items if this is the public catalog
103     unless($self->ctx->{is_staff}) { 
104         $query->{where}->{'+acp'}->{opac_visible} = 't';
105         $query->{where}->{'+acpl'}->{opac_visible} = 't';
106         $query->{where}->{'+ccs'}->{opac_visible} = 't';
107     }
108
109     return $query;
110     #return $self->editor->json_query($query);
111 }
112
113 1;