]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/holds.js
LP#1697954 Notify correct page of patron holds
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / holds.js
1 /**
2  * List of patron holds
3  */
4
5 angular.module('egPatronApp').controller('PatronHoldsCtrl',
6
7        ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
8         'egGridDataProvider','egHolds','$window','$location','egCirc','egHoldGridActions',
9 function($scope,  $q,  $routeParams,  egCore,  egUser,  patronSvc,  
10         egGridDataProvider , egHolds , $window , $location , egCirc, egHoldGridActions) {
11
12     $scope.initTab('holds', $routeParams.id);
13     $scope.holds_display = 'main';
14     $scope.detail_hold_id = $routeParams.hold_id;
15     $scope.grid_actions = egHoldGridActions;
16
17     function refresh_all() {
18         patronSvc.refreshPrimary();
19         patronSvc.holds = [];
20         patronSvc.hold_ids = [];
21         provider.refresh() 
22     }
23     $scope.grid_actions.refresh = refresh_all;
24
25     $scope.show_main_list = function() {
26         // don't need a full reset_page() to swap tabs
27         $scope.holds_display = 'main';
28         patronSvc.holds = [];
29         patronSvc.hold_ids = [];
30         provider.refresh();
31     }
32
33     $scope.show_alt_list = function() {
34         // don't need a full reset_page() to swap tabs
35         $scope.holds_display = 'alt';
36         patronSvc.holds = [];
37         patronSvc.hold_ids = [];
38         provider.refresh();
39     }
40
41     $scope.hide_cancel_hold = function(action) { 
42         return $scope.holds_display == 'alt';
43     }
44
45     $scope.hide_uncancel_hold = function(action) {
46         return !$scope.hide_cancel_hold();
47     }
48
49     var provider = egGridDataProvider.instance({});
50     $scope.gridDataProvider = provider;
51
52     function fetchHolds(offset, count) {
53         // TODO: LP#1697954 Fetch all holds on grid render to support
54         // client-side sorting.  Migrate to server-side sorting to avoid
55         // the need for fetching all items.
56
57         // we're going to just fetch all the holds up front
58         //var ids = patronSvc.hold_ids.slice(offset, offset + count); 
59         return egHolds.fetch_holds(patronSvc.hold_ids).then(null, null,
60             function(hold_data) { 
61                 patronSvc.holds.push(hold_data);
62                 return hold_data;
63             }
64         );
65     }
66
67     provider.get = function(offset, count) {
68
69         // see if we have the requested range cached
70         if (patronSvc.holds[offset]) {
71             return provider.arrayNotifier(patronSvc.holds, offset, count);
72         }
73
74         // see if we have the holds IDs for this range already loaded
75         if (patronSvc.hold_ids[offset]) {
76             return fetchHolds(offset, count);
77         }
78
79         var deferred = $q.defer();
80         patronSvc.hold_ids = [];
81
82         var method = 'open-ils.circ.holds.id_list.retrieve.authoritative';
83         if ($scope.holds_display == 'alt')
84             method = 'open-ils.circ.holds.canceled.id_list.retrieve.authoritative';
85
86         var current = 0;
87         egCore.net.request(
88             'open-ils.circ', method,
89             egCore.auth.token(), $scope.patron_id
90
91         ).then(function(hold_ids) {
92             if (!hold_ids.length) { deferred.resolve(); return; }
93
94             patronSvc.hold_ids = hold_ids;
95             fetchHolds(offset, count)
96             .then(deferred.resolve, null, function (data) {
97                 if (data) {
98                     if (current >= offset && current < count) {
99                         deferred.notify(data);
100                     }
101                     current++;
102                 }
103             });
104         });
105
106         return deferred.promise;
107     }
108
109     $scope.print = function() {
110         var holds = [];
111         angular.forEach(patronSvc.holds, function(item) {
112             holds.push({
113                 hold : egCore.idl.toHash(item.hold),
114                 copy : egCore.idl.toHash(item.copy),
115                 volume : egCore.idl.toHash(item.volume),
116                 title : item.mvr.title(),
117                 author : item.mvr.author()
118             });
119         });
120
121         egCore.print.print({
122             context : 'receipt', 
123             template : 'holds_for_patron', 
124             scope : {holds : holds}
125         });
126     }
127
128     $scope.detail_view = function(action, user_data, items) {
129         if (h = items[0]) {
130             $location.path('/circ/patron/' + 
131                 $scope.patron_id + '/holds/' + h.hold.id());
132         }
133     }
134
135     $scope.list_view = function(items) {
136         $location.path('/circ/patron/' + $scope.patron_id + '/holds');
137     }
138
139     $scope.place_hold = function() {
140         $location.path($location.path() + '/create');
141     }
142
143     // when the detail hold is fetched (and updated), update the bib
144     // record summary display record id.
145     $scope.set_hold = function(hold_data) {
146         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
147     }
148
149 }])
150
151
152 .controller('PatronHoldsCreateCtrl',
153        ['$scope','$routeParams','$location','egCore','egWorkLog','patronSvc',
154 function($scope , $routeParams , $location , egCore , egWorkLog , patronSvc) {
155
156     $scope.handlers = {
157         opac_hold_placed : function(hold) {
158             patronSvc.fetchUserStats(); // update hold counts
159             egWorkLog.record(
160                 egCore.strings.EG_WORK_LOG_REQUESTED_HOLD,{
161                     'action' : 'requested_hold',
162                     'patron_id' : patronSvc.current.id(),
163                     'hold_id' : hold
164                 }
165             );
166         }
167     }
168
169     $scope.initTab('holds', $routeParams.id).then(function(isAlert) {
170         if (isAlert) return;
171         // not guarenteed to have a barcode until init fetches the user
172         $scope.handlers.patron_barcode = patronSvc.current.card().barcode();
173     });
174
175     $scope.catalog_url = 
176         $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
177
178     $scope.handle_page = function(url) {
179     }
180
181 }])
182