]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Actor/Carousel.pm
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Actor / Carousel.pm
1 package OpenILS::Application::Actor::Carousel;
2 use base 'OpenILS::Application';
3 use strict; use warnings;
4 use OpenILS::Application::AppUtils;
5 use OpenILS::Perm;
6 use Data::Dumper;
7 use OpenSRF::EX qw(:try);
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::CStoreEditor qw/:funcs/;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenSRF::Utils::Cache;
12 use Digest::MD5 qw(md5_hex);
13 use OpenSRF::Utils::JSON;
14
15 my $apputils = "OpenILS::Application::AppUtils";
16 my $U = $apputils;
17 my $logger = "OpenSRF::Utils::Logger";
18
19 sub initialize { return 1; }
20
21 __PACKAGE__->register_method(
22     method  => "get_carousel_contents",
23     api_name    => "open-ils.actor.carousel.get_contents",
24     authoritative => 1,
25     notes        => <<"    NOTES");
26         Given a carousel ID, returns the carousel name and any publicly-visible
27         bibs from the associated bucket
28         PARAMS(carousel_id)
29     NOTES
30
31 sub get_carousel_contents {
32     my($self, $client, $id) = @_;
33     my $e = new_editor();
34     my $carousel = $e->retrieve_container_carousel($id);
35     my $ret = {
36         id   => $id,
37         name => $carousel->name
38     };
39     my $q = {
40         select => { bre => ['id'], mfde => [{ column => 'value', alias => 'title' }] },
41         from   => {
42             bre => {
43                 cbrebi => {
44                     join => {
45                         cbreb => {
46                             join => { cc => {} }
47                         }
48                     }
49                 },
50                 mfde => {}
51             }
52         },
53         where  => {
54             '+cc' => { id => $id },
55             '+bre' => { deleted => 'f' },
56             '+mfde' => { name => 'title' }
57         },
58         order_by => {cbrebi => ['pos','create_time']}
59     };
60     my $r = $e->json_query($q);
61     $ret->{bibs} = $r;
62     return $ret;
63 }
64
65 __PACKAGE__->register_method(
66     method  => "retrieve_carousels_at_org",
67     api_name    => "open-ils.actor.carousel.retrieve_by_org",
68     authoritative => 1,
69     notes        => <<"    NOTES");
70         Retrieves the IDs and override names of all carousels visible
71         at the specified org unit sorted by their sequence number at
72         that library
73         PARAMS(OrgId)
74     NOTES
75
76 sub retrieve_carousels_at_org {
77     my($self, $client, $org_id) = @_;
78     my $e = new_editor();
79
80     my $carousels = $e->json_query({
81         select => { ccou => ['carousel','override_name','seq'] },
82         distinct => 'true',
83         from => { ccou => 'cc' } ,
84         where => {
85             '+ccou' => { org_unit => $org_id },
86             '+cc'   => { active => 't' }
87         },
88         order_by => {
89             'ccou' => ['seq']
90         }
91     });
92
93     return $carousels;
94 }
95
96 __PACKAGE__->register_method(
97     method  => "retrieve_manual_carousels_for_staff",
98     api_name    => "open-ils.actor.carousel.retrieve_manual_by_staff",
99     authoritative => 1,
100     notes        => <<"    NOTES");
101         Retrieves the IDs, buckets, and names of all manually-maintained
102         carousels visible at any of the staff members working
103         locations.
104         PARAMS(authtoken)
105     NOTES
106
107 sub retrieve_manual_carousels_for_staff {
108     my($self, $client, $auth) = @_;
109     my $e = new_editor(authtoken => $auth);
110     return $e->die_event unless $e->checkauth;
111
112     my $orgs = [];
113     if ($e->requestor->super_user eq 't') {
114         # super users can act/see at all OUs
115         my $ous = $e->json_query({
116             select => { aou => ['id'] },
117             from => 'aou'
118         });
119         $orgs = [ map { $_->{id} } @$ous ];
120     } else {
121         my $ous = $e->json_query({
122             select => { puwoum => ['work_ou'] },
123             from => 'puwoum',
124             where => {
125                 '+puwoum' => { usr => $e->requestor->id }
126             }
127         });
128         $orgs = [ map { $_->{work_ou} } @$ous ];
129     }
130
131     my $carousels = $e->json_query({
132         select => { cc => ['id','name','bucket'] },
133         distinct => 'true',
134         from => { cc => 'ccou' },
135         where => {
136             '+ccou' => { org_unit => $orgs },
137             '+cc'   => { type => 1, active => 't' }, # FIXME
138         },
139         order_by => {
140             'cc' => ['name']
141         }
142     });
143
144     return $carousels;
145 }
146
147 __PACKAGE__->register_method(
148     method  => "refresh_carousel",
149     api_name    => "open-ils.actor.carousel.refresh",
150     authoritative => 1,
151     notes        => <<"    NOTES");
152         Refreshes the specified carousel
153         PARAMS(authtoken, carousel_id)
154     NOTES
155
156 sub refresh_carousel {
157     my ($self, $client, $auth, $carousel_id) = @_;
158
159     my $e = new_editor(authtoken => $auth);
160     return $e->event unless $e->checkauth;
161     return $e->event unless $e->allowed('REFRESH_CAROUSEL');
162
163     my $carousel;
164     $carousel = $e->retrieve_container_carousel($carousel_id) or return $e->event;
165
166     return $e->event unless $e->allowed('REFRESH_CAROUSEL', $carousel->owner, $carousel);
167
168     my $ctype;
169     $ctype = $e->retrieve_config_carousel_type($carousel->type) or return $e->event;
170     return new OpenILS::Event('CANNOT_REFRESH_MANUAL_CAROUSEL') unless $ctype->automatic eq 't';
171
172     my $orgs = [];
173     my $locs = [];
174     if (defined($carousel->owning_lib_filter)) {
175         my $ou_filter = $carousel->owning_lib_filter;
176         $ou_filter =~ s/[{}]//g;
177         @$orgs = split /,/, $ou_filter;
178     }
179     if (defined($carousel->copy_location_filter)) {
180         my $loc_filter = $carousel->copy_location_filter;
181         $loc_filter =~ s/[{}]//g;
182         @$locs = split /,/, $loc_filter;
183     }
184
185     my $num_updated = $U->simplereq(
186         'open-ils.storage',
187         'open-ils.storage.container.refresh_from_carousel',
188         $carousel->bucket,
189         $carousel->type,
190         $carousel->age_filter,
191         $orgs,
192         $locs,
193         $carousel->max_items,
194     );
195
196     $carousel->last_refresh_time('now');
197     $e->xact_begin;
198     $e->update_container_carousel($carousel) or return $e->event;
199     $e->xact_commit or return $e->event;
200
201     return $num_updated;
202 }
203
204 __PACKAGE__->register_method(
205     method  => "add_carousel_from_bucket",
206     api_name    => "open-ils.actor.carousel.create.from_bucket",
207     authoritative => 1,
208     notes        => <<"    NOTES");
209         Creates new carousel and its container by copying the
210         contents of an existing bucket.
211         PARAMS(authtoken, carousel_name, bucket_id)
212     NOTES
213
214 sub add_carousel_from_bucket {
215     my ($self, $client, $auth, $carousel_name, $bucket_id) = @_;
216
217     my $e = new_editor(authtoken => $auth);
218     return $e->event unless $e->checkauth;
219     return $e->event unless $e->allowed('ADMIN_CAROUSEL');
220
221     $e->xact_begin;
222
223     # gather old entries to get a count and set max_items appropriately
224     my $entries = $e->search_container_biblio_record_entry_bucket_item({ bucket => $bucket_id });
225
226     my $carousel = Fieldmapper::container::carousel->new;
227     $carousel->name($carousel_name);
228     $carousel->type(1); # manual
229     $carousel->owner($e->requestor->ws_ou);
230     $carousel->creator($e->requestor->id);
231     $carousel->editor($e->requestor->id);
232     $carousel->max_items(scalar(@$entries));
233     $e->create_container_carousel($carousel) or return $e->event;
234
235     # and the bucket
236     my $bucket = Fieldmapper::container::biblio_record_entry_bucket->new;
237     $bucket->owner($e->requestor->id);
238     $bucket->name('System-created bucket for carousel ' . $carousel->id . ' copied from bucket ' . $bucket_id);
239     $bucket->btype('carousel');
240     $bucket->pub('t');
241     $bucket->owning_lib($e->requestor->ws_ou);
242     $e->create_container_biblio_record_entry_bucket($bucket) or return $e->event;
243
244     # link it to the container;
245     $carousel = $e->retrieve_container_carousel($carousel->id) or return $e->event;
246     $carousel->bucket($bucket->id);
247     $e->update_container_carousel($carousel) or return $e->event;
248
249     # and fill it
250     foreach my $entry (@$entries) {
251         $entry->clear_id;
252         $entry->bucket($bucket->id);
253         $entry->create_time('now');
254         $e->create_container_biblio_record_entry_bucket_item($entry) or return $e->event;
255     }
256
257     $e->xact_commit or return $e->event;
258
259     return $carousel->id;
260 }
261
262 1;