]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
45057161b1fd94f72d5bbb13cac27727b4650113
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
1 /**
2  * TPAC Frame App
3  *
4  * currently, this app doesn't use routes for each sub-ui, because 
5  * reloading the catalog each time is sloooow.  better so far to 
6  * swap out divs w/ ng-if / ng-show / ng-hide as needed.
7  *
8  */
9
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','ngLocationUpdate','egCoreMod','egGridMod', 'egMarcMod', 'egUserMod', 'egHoldingsMod', 'ngToast','egPatronSearchMod',
11 'egSerialsMod','egSerialsAppDep'])
12
13 .config(['ngToastProvider', function(ngToastProvider) {
14   ngToastProvider.configure({
15     verticalPosition: 'bottom',
16     animation: 'fade'
17   });
18 }])
19
20 .config(function($routeProvider, $locationProvider, $compileProvider) {
21     $locationProvider.html5Mode(true);
22     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
23         
24     var resolver = {delay : ['egCore','egStartup','egUser', function(egCore, egStartup, egUser) {
25         egCore.env.classLoaders.aous = function() {
26             return egCore.org.settings([
27                 'cat.marc_control_number_identifier'
28             ]).then(function(settings) {
29                 // local settings are cached within egOrg.  Caching them
30                 // again in egEnv just simplifies the syntax for access.
31                 egCore.env.aous = settings;
32             });
33         }
34         egCore.env.loadClasses.push('aous');
35         return egStartup.go()
36     }]};
37
38     $routeProvider.when('/cat/catalog/index', {
39         templateUrl: './cat/catalog/t_catalog',
40         controller: 'CatalogCtrl',
41         resolve : resolver
42     });
43
44     // Jump directly to the results page.  Any URL parameter 
45     // supported by the embedded catalog is supported here.
46     $routeProvider.when('/cat/catalog/results', {
47         templateUrl: './cat/catalog/t_catalog',
48         controller: 'CatalogCtrl',
49         resolve : resolver
50     });
51
52     $routeProvider.when('/cat/catalog/retrieve_by_id', {
53         templateUrl: './cat/catalog/t_retrieve_by_id',
54         controller: 'CatalogRecordRetrieve',
55         resolve : resolver
56     });
57
58     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
59         templateUrl: './cat/catalog/t_retrieve_by_tcn',
60         controller: 'CatalogRecordRetrieve',
61         resolve : resolver
62     });
63
64     $routeProvider.when('/cat/catalog/retrieve_by_authority_id', {
65         templateUrl: './cat/catalog/t_retrieve_by_authority_id',
66         controller: 'CatalogRecordRetrieve',
67         resolve : resolver
68     });
69
70     $routeProvider.when('/cat/catalog/new_bib', {
71         templateUrl: './cat/catalog/t_new_bib',
72         controller: 'NewBibCtrl',
73         resolve : resolver
74     });
75
76     // create some catalog page-specific mappings
77     $routeProvider.when('/cat/catalog/record/:record_id', {
78         templateUrl: './cat/catalog/t_catalog',
79         controller: 'CatalogCtrl',
80         resolve : resolver
81     });
82
83     // create some catalog page-specific mappings
84     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
85         templateUrl: './cat/catalog/t_catalog',
86         controller: 'CatalogCtrl',
87         resolve : resolver
88     });
89
90     $routeProvider.when('/cat/catalog/batchEdit', {
91         templateUrl: './cat/catalog/t_batchedit',
92         controller: 'BatchEditCtrl',
93         resolve : resolver
94     });
95
96     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
97         templateUrl: './cat/catalog/t_batchedit',
98         controller: 'BatchEditCtrl',
99         resolve : resolver
100     });
101
102     $routeProvider.when('/cat/catalog/vandelay', {
103         templateUrl: './cat/catalog/t_vandelay',
104         controller: 'VandelayCtrl',
105         resolve : resolver
106     });
107
108     $routeProvider.when('/cat/catalog/verifyURLs', {
109         templateUrl: './cat/catalog/t_verifyurls',
110         controller: 'URLVerifyCtrl',
111         resolve : resolver
112     });
113
114     $routeProvider.when('/cat/catalog/manageAuthorities', {
115         templateUrl: './cat/catalog/t_manageauthorities',
116         controller: 'ManageAuthoritiesCtrl',
117         resolve : resolver
118     });
119
120     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
121         templateUrl: './cat/catalog/t_authority',
122         controller: 'AuthorityCtrl',
123         resolve : resolver
124     });
125
126     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
127 })
128
129
130 /**
131  * */
132 .controller('CatalogRecordRetrieve',
133        ['$scope','$routeParams','$location','$q','egCore',
134 function($scope , $routeParams , $location , $q , egCore ) {
135
136     $scope.focusMe = true;
137
138     // jump to the patron checkout UI
139     function loadRecord(record_id) {
140         $location
141         .path('/cat/catalog/record/' + record_id);
142     }
143
144     function loadAuthorityRecord(record_id) {
145         $location
146         .path('/cat/catalog/authority/' + record_id + '/marc_edit');
147     }
148
149     $scope.submitId = function(args) {
150         $scope.recordNotFound = null;
151         if (!args.record_id) return;
152
153         // blur so next time it's set to true it will re-apply select()
154         $scope.selectMe = false;
155
156         return loadRecord(args.record_id);
157     }
158
159     $scope.submitAuthorityId = function(args) {
160         if (!args.record_id) return;
161
162         // blur so next time it's set to true it will re-apply select()
163         $scope.selectMe = false;
164
165         return loadAuthorityRecord(args.record_id);
166     }
167
168     $scope.submitTCN = function(args) {
169         $scope.recordNotFound = null;
170         $scope.moreRecordsFound = null;
171         if (!args.record_tcn) return;
172
173         // blur so next time it's set to true it will re-apply select()
174         $scope.selectMe = false;
175
176         // lookup TCN
177         egCore.net.request(
178             'open-ils.search',
179             'open-ils.search.biblio.tcn',
180             args.record_tcn)
181
182         .then(function(resp) { // get_barcodes
183
184             if (evt = egCore.evt.parse(resp)) {
185                 alert(evt); // FIXME
186                 return;
187             }
188
189             if (!resp.count) {
190                 $scope.recordNotFound = args.record_tcn;
191                 $scope.selectMe = true;
192                 return;
193             }
194
195             if (resp.count > 1) {
196                 $scope.moreRecordsFound = args.record_tcn;
197                 $scope.selectMe = true;
198                 return;
199             }
200
201             var record_id = resp.ids[0];
202             return loadRecord(record_id);
203         });
204     }
205
206 }])
207
208 .controller('NewBibCtrl',
209        ['$scope','$routeParams','$location','$window','$q','egCore',
210         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
211 function($scope , $routeParams , $location , $window , $q , egCore) {
212
213     $scope.have_template = false;
214     $scope.marc_template = '';
215     $scope.stop_unload = false;
216     $scope.template_list = [];
217     $scope.template_name = '';
218     $scope.new_bib_id = 0;
219
220     egCore.net.request(
221         'open-ils.cat',
222         'open-ils.cat.marc_template.types.retrieve'
223     ).then(function(resp) {
224         angular.forEach(resp, function(name) {
225             $scope.template_list.push(name);
226         });
227         $scope.template_list.sort();
228     });
229     $scope.template_name = egCore.hatch.getSessionItem('eg.cat.last_bib_marc_template');
230     if (!$scope.template_name) {
231         egCore.hatch.getItem('cat.default_bib_marc_template').then(function(template) {
232             $scope.template_name = template;
233         });
234     }
235
236     $scope.loadTemplate = function() {
237         if ($scope.template_name) {
238             egCore.net.request(
239                 'open-ils.cat',
240                 'open-ils.cat.biblio.marc_template.retrieve',
241                 $scope.template_name
242             ).then(function(template) {
243                 $scope.marc_template = template;
244                 $scope.have_template = true;
245                 egCore.hatch.setSessionItem('eg.cat.last_bib_marc_template', $scope.template_name);
246             });
247         }
248     }
249
250     $scope.setDefaultTemplate = function() {
251         var hatch_key = "cat.default_bib_marc_template";
252         if ($scope.template_name) {
253             egCore.hatch.setItem(hatch_key, $scope.template_name);
254         } else {
255             egCore.hatch.removeItem(hatch_key);
256         }
257     }
258
259     $scope.$watch('new_bib_id', function(newVal, oldVal) {
260         if (newVal) {
261             $location.path('/cat/catalog/record/' + $scope.new_bib_id);
262         }
263     });
264     
265
266 }])
267 .controller('CatalogCtrl',
268        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog','ngToast',
269         'egGridDataProvider','egHoldGridActions','egProgressDialog','$timeout','$uibModal','holdingsSvc','egUser','conjoinedSvc',
270         '$cookies','egSerialsCoreSvc',
271 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc , egConfirmDialog , ngToast ,
272          egGridDataProvider , egHoldGridActions , egProgressDialog , $timeout , $uibModal , holdingsSvc , egUser , conjoinedSvc,
273          $cookies , egSerialsCoreSvc
274 ) {
275
276     var holdingsSvcInst = new holdingsSvc();
277
278     // set record ID on page load if available...
279     $scope.record_id = $routeParams.record_id;
280     $scope.summary_pane_record;
281
282     if ($scope.record_id) {
283         // TODO: Apply tab-specific title contexts
284         egCore.strings.setPageTitle(
285             egCore.strings.PAGE_TITLE_BIB_DETAIL,
286             egCore.strings.PAGE_TITLE_CATALOG_CONTEXT,
287             {record_id : $scope.record_id}
288         );
289     } else {
290         // Default to title = Catalog
291         egCore.strings.setPageTitle(
292             egCore.strings.PAGE_TITLE_CATALOG_CONTEXT);
293     }
294
295     if ($routeParams.record_id) $scope.from_route = true;
296     else $scope.from_route = false;
297
298     // set search and preferred library cookies
299     egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
300         $cookies.put('eg_search_lib', val, { path : '/' });
301     });
302     egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
303         $cookies.put('eg_pref_lib', val, { path : '/' });
304     });
305
306     // will hold a ref to the opac iframe
307     $scope.opac_iframe = null;
308     $scope.parts_iframe = null;
309
310     $scope.search_result_index = 1;
311     $scope.search_result_hit_count = 1;
312
313     $scope.$watch(
314         'opac_iframe.dom.contentWindow.search_result_index',
315         function (n,o) {
316             if (!isNaN(parseInt(n)))
317                 $scope.search_result_index = n + 1;
318         }
319     );
320
321     $scope.$watch(
322         'opac_iframe.dom.contentWindow.search_result_hit_count',
323         function (n,o) {
324             if (!isNaN(parseInt(n)))
325                 $scope.search_result_hit_count = n;
326         }
327     );
328
329     $scope.in_opac_call = false;
330     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
331         if ($scope.opac_iframe) {
332             if (force_opac_tab) $scope.record_tab = 'catalog';
333             $scope.in_opac_call = true;
334             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
335             if (opac_frame_function == 'rdetailBackToResults') {
336                 $location.update_path('/cat/catalog/index');
337             }
338         }
339     }
340
341     $scope.add_cart_to_record_bucket = function() {
342         var cartkey = $cookies.get('cartcache');
343         if (!cartkey) return;
344         egCore.net.request(
345             'open-ils.actor',
346             'open-ils.actor.anon_cache.get_value',
347             cartkey,
348             'mylist'
349         ).then(function(list) {
350             list = list.map(function(x) {
351                 return parseInt(x);
352             });
353             $scope.add_to_record_bucket(list);
354         });
355     }
356
357     $scope.add_to_record_bucket = function(recs) {
358         if (!angular.isArray(recs)) {
359             recs = [ $scope.record_id ];
360         }
361         return $uibModal.open({
362             templateUrl: './cat/catalog/t_add_to_bucket',
363             backdrop: 'static',
364             animation: true,
365             size: 'md',
366             controller:
367                    ['$scope','$uibModalInstance',
368             function($scope , $uibModalInstance) {
369
370                 $scope.bucket_id = 0;
371                 $scope.newBucketName = '';
372                 $scope.allBuckets = [];
373                 egCore.net.request(
374                     'open-ils.actor',
375                     'open-ils.actor.container.retrieve_by_class.authoritative',
376                     egCore.auth.token(), egCore.auth.user().id(),
377                     'biblio', 'staff_client'
378                 ).then(function(buckets) { $scope.allBuckets = buckets; });
379
380                 $scope.add_to_bucket = function() {
381                     var promises = [];
382                     angular.forEach(recs, function(recId) {
383                         var item = new egCore.idl.cbrebi();
384                         item.bucket($scope.bucket_id);
385                         item.target_biblio_record_entry(recId);
386                         promises.push(egCore.net.request(
387                             'open-ils.actor',
388                             'open-ils.actor.container.item.create',
389                             egCore.auth.token(), 'biblio', item
390                         ));
391                     });
392                     $q.all(promises).then(function(resp) {
393                         $uibModalInstance.close();
394                     });
395                 }
396
397                 $scope.add_to_new_bucket = function() {
398                     var bucket = new egCore.idl.cbreb();
399                     bucket.owner(egCore.auth.user().id());
400                     bucket.name($scope.newBucketName);
401                     bucket.description('');
402                     bucket.btype('staff_client');
403
404                     egCore.net.request(
405                         'open-ils.actor',
406                         'open-ils.actor.container.create',
407                         egCore.auth.token(), 'biblio', bucket
408                     ).then(function(bucket) {
409                         $scope.bucket_id = bucket;
410                         $scope.add_to_bucket();
411                     });
412                 }
413
414                 $scope.cancel = function() {
415                     $uibModalInstance.dismiss();
416                 }
417             }]
418         });
419     }
420
421     $scope.current_overlay_target     = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record');
422     $scope.current_transfer_target    = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
423     $scope.current_conjoined_target   = egCore.hatch.getLocalItem('eg.cat.marked_conjoined_record');
424
425     $scope.quickReceive = function () {
426         var list = [];
427         var next_per_stream = {};
428
429         var recId = $scope.record_id;
430         return $uibModal.open({
431             templateUrl: './share/t_subscription_select_dialog',
432             backdrop: 'static',
433             controller: ['$scope', '$uibModalInstance',
434                 function($scope, $uibModalInstance) {
435
436                     $scope.focus = true;
437                     $scope.rememberMe = 'eg.serials.quickreceive.last_org';
438                     $scope.record_id = recId;
439                     $scope.ssubId = null;
440
441                     $scope.ok = function() { $uibModalInstance.close($scope.ssubId) }
442                     $scope.cancel = function() { $uibModalInstance.dismiss(); }
443                 }
444             ]
445         }).result.then(function(ssubId) {
446             if (ssubId) {
447                 var promises = [];
448                 promises.push(egSerialsCoreSvc.fetchItemsForSub(ssubId,{status:'Expected'}).then(function(){
449                     angular.forEach(egSerialsCoreSvc.itemTree, function (item) {
450                         if (next_per_stream[item.stream().id()]) return;
451                         if (item.status() == 'Expected') {
452                             next_per_stream[item.stream().id()] = item;
453                             list.push(egCore.idl.Clone(item));
454                         }
455                     });
456                 }));
457
458                 return $q.all(promises).then(function() {
459
460                     if (!list.length) {
461                         ngToast.warning(egCore.strings.SERIALS_NO_ITEMS);
462                         return $q.reject();
463                     }
464
465                     return egSerialsCoreSvc.process_items(
466                         'receive',
467                         $scope.record_id,
468                         list,
469                         true, // barcode
470                         false,// bind
471                         false, // print by default
472                         function() { $scope.holdings_record_id_changed($scope.record_id) }
473                     );
474                 });
475             } else {
476                 ngToast.warning(egCore.strings.SERIALS_NO_SUBS);
477                 return $q.reject();
478             }
479         });
480     }
481
482     $scope.markConjoined = function () {
483         $scope.current_conjoined_target = $scope.record_id;
484         egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.record_id);
485         ngToast.create(egCore.strings.MARK_CONJ_TARGET);
486     };
487
488     $scope.markHoldingsTransfer = function () {
489         $scope.current_transfer_target = $scope.record_id;
490         egCore.hatch.setLocalItem('eg.cat.transfer_target_record',$scope.record_id);
491         egCore.hatch.removeLocalItem('eg.cat.transfer_target_lib');
492         egCore.hatch.removeLocalItem('eg.cat.transfer_target_vol');
493         ngToast.create(egCore.strings.MARK_HOLDINGS_TARGET);
494     };
495
496     $scope.markOverlay = function () {
497         $scope.current_overlay_target = $scope.record_id;
498         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.record_id);
499         ngToast.create(egCore.strings.MARK_OVERLAY_TARGET);
500     };
501
502     $scope.clearRecordMarks = function () {
503         $scope.current_overlay_target     = null;
504         $scope.current_transfer_target    = null;
505         $scope.current_conjoined_target   = null;
506         $scope.current_hold_transfer_dest = null;
507         egCore.hatch.removeLocalItem('eg.cat.transfer_target_record');
508         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
509         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
510         egCore.hatch.removeLocalItem('eg.circ.hold.title_transfer_target');
511     }
512
513     $scope.stop_unload = false;
514     $scope.$watch('stop_unload',
515         function(newVal, oldVal) {
516             if (newVal && newVal != oldVal && $scope.opac_iframe) {
517                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
518                     return 'There is unsaved data in this record.'
519                 });
520             } else {
521                 if ($scope.opac_iframe)
522                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
523             }
524         }
525     );
526
527     // Set the "last bib" cookie, if we have that
528     if ($scope.record_id)
529         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
530
531     $scope.refresh_record_callback = function (record_id) {
532         egCore.pcrud.retrieve('bre', record_id, {
533             flesh : 1,
534             flesh_fields : {
535                 bre : ['simple_record','creator','editor']
536             }
537         }).then(function(rec) {
538             rec.owner(egCore.org.get(rec.owner()));
539             $scope.summary_pane_record = rec;
540         });
541
542         return record_id;
543     }
544
545     patron_search_dialog = function() {
546         return $uibModal.open({
547             templateUrl: './share/t_patron_selector',
548             backdrop: 'static',
549             size: 'lg',
550             animation: true,
551             controller:
552                    ['$scope','$uibModalInstance','$controller',
553             function($scope , $uibModalInstance , $controller) {
554                 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
555                 $scope.clearForm();
556                 $scope.need_one_selected = function() {
557                     var items = $scope.gridControls.selectedItems();
558                     return (items.length == 1) ? false : true
559                 }
560                 $scope.ok = function() {
561                     var items = $scope.gridControls.selectedItems();
562                     if (items.length == 1) {
563                         $uibModalInstance.close(items[0].card().barcode());
564                     } else {
565                         $uibModalInstance.close()
566                     }
567                 }
568                 $scope.cancel = function($event) {
569                     $uibModalInstance.dismiss();
570                     $event.preventDefault();
571                 }
572             }]
573         });
574     }
575
576     // also set it when the iframe changes to a new record
577     $scope.handle_page = function(url) {
578
579         if (!url || url == 'about:blank') {
580             // nothing loaded.  If we already have a record ID, leave it.
581             return;
582         }
583
584         var prev_record_id = $scope.record_id;
585         var match = url.match(/\/+opac\/+record\/+(\d+)/);
586         if (match) {
587             $scope.record_id = match[1];
588             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
589             $scope.holdings_record_id_changed($scope.record_id);
590             conjoinedSvc.fetch($scope.record_id).then(function(){
591                 $scope.conjoinedGridDataProvider.refresh();
592             });
593             init_parts_url();
594             $location.update_path('/cat/catalog/record/' + $scope.record_id);
595             // update_path() bypasses the controller for path 
596             // /cat/catalog/record/:record_id. Manually set title here too.
597             egCore.strings.setPageTitle(
598                 egCore.strings.PAGE_TITLE_BIB_DETAIL,
599                 egCore.strings.PAGE_TITLE_CATALOG_CONTEXT,
600                 {record_id : $scope.record_id}
601             );
602         } else {
603             delete $scope.record_id;
604             $scope.from_route = false;
605         }
606
607         // child scope is executing this function, so our digest doesn't fire ... thus,
608         $scope.$apply();
609
610         // don't change tabs if we are using the OPAC nav buttons,
611         // or we didn't change records on the OPAC load
612         if (!$scope.in_opac_call && ($scope.record_id != prev_record_id)) {
613             if ($scope.record_id) {
614                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
615                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
616             } else {
617                 tab = $routeParams.record_tab || 'catalog';
618             }
619             $scope.set_record_tab(tab);
620         } else {
621             $scope.in_opac_call = false;
622         }
623
624         if ($scope.opac_iframe && $location.path().match(/cat\/catalog/)) {
625             var doc = $scope.opac_iframe.dom.contentWindow.document;
626             $(doc).find('#hold_usr_search').show();
627             $(doc).find('#hold_usr_search').on('click', function() {
628                 patron_search_dialog().result.then(function(barc) {
629                     $(doc).find('#hold_usr_input').val(barc);
630                     $(doc).find('#hold_usr_input').change();
631                 });
632             });
633             $(doc).find('#select_basket_action').on('change', function() {
634                 if (this.options[this.selectedIndex].value && this.options[this.selectedIndex].value == "add_cart_to_bucket") {
635                     $scope.add_cart_to_record_bucket();
636                 }
637             });
638         }
639
640     }
641
642     // xulG catalog handlers
643     $scope.handlers = { }
644
645     // ------------------------------------------------------------------
646     // Conjoined items
647
648     $scope.conjoinedGridControls = {};
649     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
650         get : function(offset, count) {
651             return this.arrayNotifier(conjoinedSvc.items, offset, count);
652         }
653     });
654
655     $scope.changeConjoinedType = function () {
656         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
657         angular.forEach(peers, function (p) {
658             p.target_copy(p.target_copy().id());
659             p.peer_type(p.peer_type().id());
660         });
661
662         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
663
664         return $uibModal.open({
665             templateUrl: './cat/catalog/t_conjoined_selector',
666             backdrop: 'static',
667             animation: true,
668             controller:
669                    ['$scope','$uibModalInstance',
670             function($scope , $uibModalInstance) {
671                 $scope.update = true;
672
673                 $scope.peer_type = null;
674                 $scope.peer_type_list = [];
675                 conjoinedSvc.get_peer_types().then(function(list){
676                     $scope.peer_type_list = list;
677                 });
678     
679                 $scope.ok = function(type) {
680                     var promises = [];
681     
682                     angular.forEach(peers, function (p) {
683                         p.ischanged(1);
684                         p.peer_type(type);
685                         promises.push(egCore.pcrud.update(p));
686                     });
687     
688                     return $q.all(promises)
689                         .then(function(){$uibModalInstance.close()})
690                         .then(function(){return conjoinedSvc.fetch()})
691                         .then(function(){conjoinedGridDataProviderRef.refresh()});
692                 }
693     
694                 $scope.cancel = function($event) {
695                     $uibModalInstance.dismiss();
696                     $event.preventDefault();
697                 }
698             }]
699         });
700         
701     }
702
703     $scope.refreshConjoined = function () {
704         conjoinedSvc.fetch($scope.record_id)
705         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
706     }
707
708     $scope.deleteSelectedConjoined = function () {
709         var peers = $scope.conjoinedGridControls.selectedItems();
710
711         if (peers.length > 0) {
712             egConfirmDialog.open(
713                 egCore.strings.CONFIRM_DELETE_PEERS,
714                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
715                 {peers : peers.length}
716             ).result.then(function() {
717                 angular.forEach(peers, function (p) {
718                     p.isdeleted(1);
719                 });
720
721                 egCore.pcrud.remove(peers).then(function() {
722                     return conjoinedSvc.fetch();
723                 }).then(function() {
724                     $scope.conjoinedGridDataProvider.refresh();
725                 });
726             });
727         }
728     }
729     if ($scope.record_id)
730         conjoinedSvc.fetch($scope.record_id);
731
732     // ------------------------------------------------------------------
733     // Holdings
734
735     $scope.holdingsGridControls = {
736         activateItem : function (item) {
737             $scope.selectedHoldingsVolCopyEdit();
738         }
739     };
740     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
741         get : function(offset, count) {
742             return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
743         }
744     });
745
746     $scope.add_copies_to_bucket = function() {
747         var copy_list = gatherSelectedHoldingsIds();
748         if (copy_list.length == 0) return;
749
750         return $uibModal.open({
751             templateUrl: './cat/catalog/t_add_to_bucket',
752             backdrop: 'static',
753             animation: true,
754             size: 'md',
755             controller:
756                    ['$scope','$uibModalInstance',
757             function($scope , $uibModalInstance) {
758
759                 $scope.bucket_id = 0;
760                 $scope.newBucketName = '';
761                 $scope.allBuckets = [];
762
763                 egCore.net.request(
764                     'open-ils.actor',
765                     'open-ils.actor.container.retrieve_by_class.authoritative',
766                     egCore.auth.token(), egCore.auth.user().id(),
767                     'copy', 'staff_client'
768                 ).then(function(buckets) { $scope.allBuckets = buckets; });
769
770                 $scope.add_to_bucket = function() {
771                     var promises = [];
772                     angular.forEach(copy_list, function (cp) {
773                         var item = new egCore.idl.ccbi()
774                         item.bucket($scope.bucket_id);
775                         item.target_copy(cp);
776                         promises.push(
777                             egCore.net.request(
778                                 'open-ils.actor',
779                                 'open-ils.actor.container.item.create',
780                                 egCore.auth.token(), 'copy', item
781                             )
782                         );
783
784                         return $q.all(promises).then(function() {
785                             $uibModalInstance.close();
786                         });
787                     });
788                 }
789
790                 $scope.add_to_new_bucket = function() {
791                     var bucket = new egCore.idl.ccb();
792                     bucket.owner(egCore.auth.user().id());
793                     bucket.name($scope.newBucketName);
794                     bucket.description('');
795                     bucket.btype('staff_client');
796
797                     return egCore.net.request(
798                         'open-ils.actor',
799                         'open-ils.actor.container.create',
800                         egCore.auth.token(), 'copy', bucket
801                     ).then(function(bucket) {
802                         $scope.bucket_id = bucket;
803                         $scope.add_to_bucket();
804                     });
805                 }
806
807                 $scope.cancel = function() {
808                     $uibModalInstance.dismiss();
809                 }
810             }]
811         });
812     }
813
814     // TODO: refactor common code between cat/catalog/app.js and cat/item/app.js 
815
816     $scope.need_one_selected = function() {
817         var items = $scope.holdingsGridControls.selectedItems();
818         if (items.length == 1) return false;
819         return true;
820     };
821
822     $scope.make_copies_bookable = function() {
823
824         var copies_by_record = {};
825         var record_list = [];
826         angular.forEach(
827             $scope.holdingsGridControls.selectedItems(),
828             function (item) {
829                 var record_id = item['call_number.record.id'];
830                 if (typeof copies_by_record[ record_id ] == 'undefined') {
831                     copies_by_record[ record_id ] = [];
832                     record_list.push( record_id );
833                 }
834                 copies_by_record[ record_id ].push(item.id);
835             }
836         );
837
838         var promises = [];
839         var combined_results = [];
840         angular.forEach(record_list, function(record_id) {
841             promises.push(
842                 egCore.net.request(
843                     'open-ils.booking',
844                     'open-ils.booking.resources.create_from_copies',
845                     egCore.auth.token(),
846                     copies_by_record[record_id]
847                 ).then(function(results) {
848                     if (results && results['brsrc']) {
849                         combined_results = combined_results.concat(results['brsrc']);
850                     }
851                 })
852             );
853         });
854
855         $q.all(promises).then(function() {
856             if (combined_results.length > 0) {
857                 $uibModal.open({
858                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
859                     backdrop: 'static',
860                     animation: true,
861                     size: 'md',
862                     controller:
863                            ['$scope','$location','egCore','$uibModalInstance',
864                     function($scope , $location , egCore , $uibModalInstance) {
865
866                         $scope.funcs = {
867                             ses : egCore.auth.token(),
868                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
869                         }
870
871                         var booking_path = '/eg/conify/global/booking/resource';
872
873                         $scope.booking_admin_url =
874                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
875                     }]
876                 });
877             }
878         });
879     }
880
881     $scope.book_copies_now = function() {
882         var copies_by_record = {};
883         var record_list = [];
884         angular.forEach(
885             $scope.holdingsGridControls.selectedItems(),
886             function (item) {
887                 var record_id = item['call_number.record.id'];
888                 if (typeof copies_by_record[ record_id ] == 'undefined') {
889                     copies_by_record[ record_id ] = [];
890                     record_list.push( record_id );
891                 }
892                 copies_by_record[ record_id ].push(item.id);
893             }
894         );
895
896         var promises = [];
897         var combined_brt = [];
898         var combined_brsrc = [];
899         angular.forEach(record_list, function(record_id) {
900             promises.push(
901                 egCore.net.request(
902                     'open-ils.booking',
903                     'open-ils.booking.resources.create_from_copies',
904                     egCore.auth.token(),
905                     copies_by_record[record_id]
906                 ).then(function(results) {
907                     if (results && results['brt']) {
908                         combined_brt = combined_brt.concat(results['brt']);
909                     }
910                     if (results && results['brsrc']) {
911                         combined_brsrc = combined_brsrc.concat(results['brsrc']);
912                     }
913                 })
914             );
915         });
916
917         $q.all(promises).then(function() {
918             if (combined_brt.length > 0 || combined_brsrc.length > 0) {
919                 $uibModal.open({
920                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
921                     backdrop: 'static',
922                     animation: true,
923                     size: 'md',
924                     controller:
925                            ['$scope','$location','egCore','$uibModalInstance',
926                     function($scope , $location , egCore , $uibModalInstance) {
927
928                         $scope.funcs = {
929                             ses : egCore.auth.token(),
930                             bresv_interface_opts : {
931                                 booking_results : {
932                                      brt : combined_brt
933                                     ,brsrc : combined_brsrc
934                                 }
935                             }
936                         }
937
938                         var booking_path = '/eg/booking/reservation';
939
940                         $scope.booking_admin_url =
941                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
942
943                     }]
944                 });
945             }
946         });
947     }
948
949
950     $scope.requestItems = function() {
951         var copy_list = gatherSelectedHoldingsIds();
952         if (copy_list.length == 0) return;
953
954         return $uibModal.open({
955             templateUrl: './cat/catalog/t_request_items',
956             animation: true,
957             controller:
958                    ['$scope','$uibModalInstance',
959             function($scope , $uibModalInstance) {
960                 $scope.user = null;
961                 $scope.first_user_fetch = true;
962
963                 $scope.hold_data = {
964                     hold_type : 'C',
965                     copy_list : copy_list,
966                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
967                     user      : egCore.auth.user().id()
968                 };
969
970                 egUser.get( $scope.hold_data.user ).then(function(u) {
971                     $scope.user = u;
972                     $scope.barcode = u.card().barcode();
973                     $scope.user_name = egUser.format_name(u);
974                     $scope.hold_data.user = u.id();
975                 });
976
977                 $scope.user_name = '';
978                 $scope.barcode = '';
979                 $scope.$watch('barcode', function (n) {
980                     if (!$scope.first_user_fetch) {
981                         egUser.getByBarcode(n).then(function(u) {
982                             $scope.user = u;
983                             $scope.user_name = egUser.format_name(u);
984                             $scope.hold_data.user = u.id();
985                         }, function() {
986                             $scope.user = null;
987                             $scope.user_name = '';
988                             delete $scope.hold_data.user;
989                         });
990                     }
991                     $scope.first_user_fetch = false;
992                 });
993
994                 $scope.ok = function(h) {
995                     var args = {
996                         patronid  : h.user,
997                         hold_type : h.hold_type,
998                         pickup_lib: h.pickup_lib.id(),
999                         depth     : 0
1000                     };
1001
1002                     egCore.net.request(
1003                         'open-ils.circ',
1004                         'open-ils.circ.holds.test_and_create.batch.override',
1005                         egCore.auth.token(), args, h.copy_list
1006                     );
1007
1008                     $uibModalInstance.close();
1009                 }
1010
1011                 $scope.cancel = function($event) {
1012                     $uibModalInstance.dismiss();
1013                     $event.preventDefault();
1014                 }
1015             }]
1016         });
1017     }
1018
1019     $scope.view_place_orders = function() {
1020         if (!$scope.record_id) return;
1021         var url = egCore.env.basePath + 'acq/legacy/lineitem/related/' + $scope.record_id + '?target=bib';
1022         $timeout(function() { $window.open(url, '_blank') });
1023     }
1024
1025     $scope.replaceBarcodes = function() {
1026         var copy_list = gatherSelectedRawCopies();
1027         if (copy_list.length == 0) return;
1028
1029         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
1030
1031         angular.forEach(copy_list, function (cp) {
1032             $uibModal.open({
1033                 templateUrl: './cat/share/t_replace_barcode',
1034                 backdrop: 'static',
1035                 animation: true,
1036                 controller:
1037                            ['$scope','$uibModalInstance',
1038                     function($scope , $uibModalInstance) {
1039                         $scope.isModal = true;
1040                         $scope.focusBarcode = false;
1041                         $scope.focusBarcode2 = true;
1042                         $scope.barcode1 = cp.barcode();
1043
1044                         $scope.updateBarcode = function() {
1045                             $scope.copyNotFound = false;
1046                             $scope.updateOK = false;
1047                 
1048                             egCore.pcrud.search('acp',
1049                                 {deleted : 'f', barcode : $scope.barcode1})
1050                             .then(function(copy) {
1051                 
1052                                 if (!copy) {
1053                                     $scope.focusBarcode = true;
1054                                     $scope.copyNotFound = true;
1055                                     return;
1056                                 }
1057                 
1058                                 $scope.copyId = copy.id();
1059                                 copy.barcode($scope.barcode2);
1060                 
1061                                 egCore.pcrud.update(copy).then(function(stat) {
1062                                     $scope.updateOK = stat;
1063                                     $scope.focusBarcode = true;
1064                                     holdingsSvc.fetchAgain().then(function (){
1065                                         holdingsGridDataProviderRef.refresh();
1066                                     });
1067                                 });
1068
1069                             });
1070                             $uibModalInstance.close();
1071                         }
1072
1073                         $scope.cancel = function($event) {
1074                             $uibModalInstance.dismiss();
1075                             $event.preventDefault();
1076                         }
1077                     }
1078                 ]
1079             });
1080         });
1081     }
1082
1083     // refresh the list of holdings when the record_id is changed.
1084     $scope.holdings_record_id_changed = function(id) {
1085         if ($scope.record_id != id) $scope.record_id = id;
1086         console.log('record id changed to ' + id + ', loading new holdings');
1087         holdingsSvcInst.fetch({
1088             rid : $scope.record_id,
1089             org : $scope.holdings_ou,
1090             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1091             vol : $scope.holdings_show_vols,
1092             empty: $scope.holdings_show_empty,
1093             empty_org: $scope.holdings_show_empty_org
1094         }).then(function() {
1095             $scope.holdingsGridDataProvider.refresh();
1096         });
1097     }
1098
1099     // refresh the list of holdings when the filter lib is changed.
1100     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
1101     $scope.holdings_ou_changed = function(org) {
1102         $scope.holdings_ou = org;
1103         holdingsSvcInst.fetch({
1104             rid : $scope.record_id,
1105             org : $scope.holdings_ou,
1106             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1107             vol : $scope.holdings_show_vols,
1108             empty: $scope.holdings_show_empty,
1109             empty_org: $scope.holdings_show_empty_org
1110         }).then(function() {
1111             $scope.holdingsGridDataProvider.refresh();
1112         });
1113     }
1114
1115     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
1116         $scope[cb] = newVal;
1117         var x = $scope.holdings_show_vols ? $scope.holdings_show_copies : false;
1118         $('#holdings_show_copies').prop('checked', x);
1119         egCore.hatch.setItem('cat.' + cb, newVal);
1120         if (!norefresh) holdingsSvcInst.fetch({
1121             rid : $scope.record_id,
1122             org : $scope.holdings_ou,
1123             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1124             vol : $scope.holdings_show_vols,
1125             empty: $scope.holdings_show_empty,
1126             empty_org: $scope.holdings_show_empty_org
1127         }).then(function() {
1128             $scope.holdingsGridDataProvider.refresh();
1129         });
1130     }
1131
1132     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
1133         if (typeof x ==  'undefined') x = true;
1134         $scope.holdings_cb_changed('holdings_show_vols',x,true);
1135         $('#holdings_show_vols').prop('checked', x);
1136     }).then(function(){
1137         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
1138             if (typeof x ==  'undefined') x = true;
1139             $scope.holdings_cb_changed('holdings_show_copies',x,true);
1140             x = $scope.holdings_show_vols ? x : false;
1141             $('#holdings_show_copies').prop('checked', x);
1142         }).then(function(){
1143             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
1144                 if (typeof x ==  'undefined') x = true;
1145                 $scope.holdings_cb_changed('holdings_show_empty',x);
1146                 $('#holdings_show_empty').prop('checked', x);
1147             }).then(function(){
1148                 egCore.hatch.getItem('cat.holdings_show_empty_org').then(function(x){
1149                     if (typeof x ==  'undefined') x = true;
1150                     $scope.holdings_cb_changed('holdings_show_empty_org',x);
1151                     $('#holdings_show_empty_org').prop('checked', x);
1152                 })
1153             })
1154         })
1155     });
1156
1157     $scope.vols_not_shown = function () {
1158         return !$scope.holdings_show_vols;
1159     }
1160
1161     $scope.copies_not_shown = function () {
1162         return !$scope.holdings_show_copies;
1163     }
1164
1165     $scope.empty_org_not_shown = function () {
1166         return !$scope.holdings_show_empty_org;
1167     }
1168
1169     $scope.holdings_checkbox_handler = function (item) {
1170         $scope.holdings_cb_changed(item.checkbox,item.checked);
1171     }
1172
1173     function gatherSelectedHoldingsIds () {
1174         var cp_id_list = [];
1175         angular.forEach(
1176             $scope.holdingsGridControls.selectedItems(),
1177             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
1178         );
1179         return cp_id_list;
1180     }
1181
1182     function gatherSelectedRawCopies () {
1183         var cp_list = [];
1184         angular.forEach(
1185             $scope.holdingsGridControls.selectedItems(),
1186             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
1187         );
1188         return cp_list;
1189     }
1190
1191     function gatherSelectedEmptyVolumeIds () {
1192         var cn_id_list = [];
1193         angular.forEach(
1194             $scope.holdingsGridControls.selectedItems(),
1195             function (item) {
1196                 if (item.copy_count == 0 || (!item.id && item.call_number))
1197                     // we are in a compressed row with no copies, or we are in a single
1198                     // call number row with no copy (testing for presence of 'id')
1199                     // In either case, the call number is 'empty'
1200                     cn_id_list.push(item.call_number.id)
1201             }
1202         );
1203         return cn_id_list;
1204     }
1205
1206     function gatherSelectedVolumeIds () {
1207         var cn_id_list = [];
1208         angular.forEach(
1209             $scope.holdingsGridControls.selectedItems(),
1210             function (item) {
1211                 if (cn_id_list.indexOf(item.call_number.id) == -1)
1212                     cn_id_list.push(item.call_number.id)
1213             }
1214         );
1215         return cn_id_list;
1216     }
1217
1218     $scope.selectedHoldingsDelete = function (vols, copies) {
1219
1220         var cnHash = {};
1221         var perCnCopies = {};
1222
1223         var cn_count = 0;
1224         var cp_count = 0;
1225
1226         angular.forEach(
1227             $scope.holdingsGridControls.selectedItems(),
1228             function (item) {
1229                 if (vols && item.raw_call_number) {
1230                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
1231                     cnHash[item.call_number.id].isdeleted(1);
1232                     cn_count++;
1233                 } else if (copies) {
1234                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
1235                         cp.isdeleted(1);
1236                         cp_count++;
1237                         var cn_id = cp.call_number().id();
1238                         if (!cnHash[cn_id]) {
1239                             cnHash[cn_id] = cp.call_number();
1240                             perCnCopies[cn_id] = [cp];
1241                         } else {
1242                             perCnCopies[cn_id].push(cp);
1243                         }
1244                         cp.call_number(cn_id); // prevent loops in JSON-ification
1245                     });
1246
1247                 }
1248             }
1249         );
1250
1251         angular.forEach(perCnCopies, function (v, k) {
1252             if (vols) {
1253                 cnHash[k].isdeleted(1);
1254                 cn_count++;
1255             }
1256             cnHash[k].copies(v);
1257         });
1258
1259         cnList = [];
1260         angular.forEach(cnHash, function (v, k) {
1261             cnList.push(v);
1262         });
1263
1264         if (cnList.length == 0) return;
1265
1266         var flags = {};
1267         if (vols && copies) flags.force_delete_copies = 1;
1268
1269         egConfirmDialog.open(
1270             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
1271             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
1272             {copies : cp_count, volumes : cn_count}
1273         ).result.then(function() {
1274             egCore.net.request(
1275                 'open-ils.cat',
1276                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1277                 egCore.auth.token(), cnList, 1, flags
1278             ).then(function(update_count) {
1279                 holdingsSvcInst.fetchAgain().then(function() {
1280                     $scope.holdingsGridDataProvider.refresh();
1281                 });
1282             });
1283         });
1284     }
1285     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
1286     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
1287     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
1288
1289     spawnHoldingsAdd = function (add_vols,add_copies){
1290         var raw = [];
1291         if (!add_vols && add_copies) { // just a copy on existing volumes
1292             angular.forEach(gatherSelectedVolumeIds(), function (v) {
1293                 raw.push( {callnumber : v} );
1294             });
1295         } else if (add_vols) {
1296             if (typeof $scope.holdingsGridControls.selectedItems == "function" &&
1297                 $scope.holdingsGridControls.selectedItems().length > 0) {
1298                 angular.forEach($scope.holdingsGridControls.selectedItems(),
1299                     function (item) {
1300                         raw.push({
1301                             owner : item.owner_id,
1302                             label : ((item.call_number) ? item.call_number.label : null)
1303                         });
1304                     });
1305             } else {
1306                 raw.push({
1307                     owner : egCore.auth.user().ws_ou()
1308                 });
1309             }
1310         }
1311
1312         if (raw.length == 0) raw.push({});
1313
1314         egCore.net.request(
1315             'open-ils.actor',
1316             'open-ils.actor.anon_cache.set_value',
1317             null, 'edit-these-copies', {
1318                 record_id: $scope.record_id,
1319                 raw: raw,
1320                 hide_vols : false,
1321                 hide_copies : !add_copies
1322             }
1323         ).then(function(key) {
1324             if (key) {
1325                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1326                 $timeout(function() { $window.open(url, '_blank') });
1327             } else {
1328                 alert('Could not create anonymous cache key!');
1329             }
1330         });
1331     }
1332     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,true) }
1333     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
1334     $scope.selectedHoldingsVolAdd = function () { spawnHoldingsAdd(true,false) }
1335
1336     spawnHoldingsEdit = function (hide_vols,hide_copies){
1337         egCore.net.request(
1338             'open-ils.actor',
1339             'open-ils.actor.anon_cache.set_value',
1340             null, 'edit-these-copies', {
1341                 record_id: $scope.record_id,
1342                 copies: gatherSelectedHoldingsIds(),
1343                 raw: gatherSelectedEmptyVolumeIds().map(
1344                     function(v){ return { callnumber : v } }
1345                 ),
1346                 hide_vols : hide_vols,
1347                 hide_copies : hide_copies
1348             }
1349         ).then(function(key) {
1350             if (key) {
1351                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1352                 $timeout(function() { $window.open(url, '_blank') });
1353             } else {
1354                 alert('Could not create anonymous cache key!');
1355             }
1356         });
1357     }
1358     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
1359     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
1360     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
1361
1362     $scope.selectedHoldingsItemStatus = function (){
1363         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
1364         $timeout(function() { $window.open(url, '_blank') });
1365     }
1366
1367     $scope.markFromSelectedAsHoldingsTarget = function() {
1368         egCore.hatch.setLocalItem(
1369             'eg.cat.transfer_target_lib',
1370             $scope.holdingsGridControls.selectedItems()[0].owner_id
1371         );
1372         egCore.hatch.setLocalItem(
1373             'eg.cat.transfer_target_record',
1374             $scope.record_id
1375         );
1376         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed, or we are on an empty lib
1377             egCore.hatch.setLocalItem(
1378                 'eg.cat.transfer_target_vol',
1379                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
1380             );
1381         } else {
1382             // clear out the stale value if we're on a lib-only
1383             // or vol-collapsed row
1384             egCore.hatch.removeLocalItem('eg.cat.transfer_target_vol');
1385         }
1386         ngToast.create(egCore.strings.MARK_HOLDINGS_TARGET);
1387     }
1388
1389     $scope.selectedHoldingsItemStatusDetail = function (){
1390         angular.forEach(
1391             gatherSelectedHoldingsIds(),
1392             function (cid) {
1393                 var url = egCore.env.basePath +
1394                           'cat/item/' + cid;
1395                 $timeout(function() { $window.open(url, '_blank') });
1396             }
1397         );
1398     }
1399
1400     $scope.transferVolumes = function (){
1401         var target_record = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
1402         var target_lib = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
1403         if (!target_lib
1404             && (!target_record || ($scope.record_id == target_record) )
1405         ) return;
1406
1407         var vols_to_move = {};
1408         if (target_lib) {
1409             // we're moving volumes to a different library
1410             var vol_ids = gatherSelectedVolumeIds();
1411             if (vol_ids.length) {
1412                 vols_to_move[target_lib] = vol_ids;
1413
1414                 // if we're *only* switching libs,
1415                 // grab the current record as the target
1416                 target_record = target_record || $scope.record_id;
1417             }
1418         } else {
1419             // we're moving volumes to the same library they exist in
1420             // currently, but on a different record
1421             var items = $scope.holdingsGridControls.selectedItems();
1422             angular.forEach(items, function(item) {
1423                 if (!(item.call_number.owning_lib in vols_to_move)) {
1424                     vols_to_move[item.call_number.owning_lib] = new Array;
1425                 }
1426                 vols_to_move[item.call_number.owning_lib].push(item.call_number.id);
1427             });
1428         }
1429
1430         var promises = [];        
1431         angular.forEach(vols_to_move, function(vols, owning_lib) {
1432             promises.push(egCore.net.request(
1433                 'open-ils.cat',
1434                 'open-ils.cat.asset.volume.batch.transfer.override',
1435                 egCore.auth.token(), {
1436                     docid   : target_record,
1437                     lib     : owning_lib,
1438                     volumes : vols
1439                 }
1440             ));
1441         });
1442         $q.all(promises).then(function(success) {
1443             if (success) {
1444                 ngToast.create(egCore.strings.VOLS_TRANSFERED);
1445                 holdingsSvcInst.fetchAgain().then(function() {
1446                     $scope.holdingsGridDataProvider.refresh();
1447                 });
1448             } else {
1449                 alert('Could not transfer volumes!');
1450             }
1451         });
1452     }
1453
1454     // this "transfers" selected copies to a new owning library,
1455     // auto-creating volumes as required
1456     $scope.transferItemsAutoFill = function() {
1457         var target_record = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
1458         var target_lib = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
1459         if (!target_lib
1460             && (!target_record || ($scope.record_id == target_record) )
1461         ) return;
1462
1463         var items = $scope.holdingsGridControls.selectedItems();
1464         if (!items.length) {
1465             return;
1466         }
1467
1468         var vols_to_move   = {};
1469         var copies_to_move = {};
1470         angular.forEach(items, function(item) {
1471             var needs_move = false;
1472             if (target_lib
1473                 && (item.call_number.owning_lib != target_lib)) {
1474                     item.call_number.owning_lib = target_lib;
1475                     needs_move = true;
1476             }
1477             if (target_record
1478                 && (item.call_number.record != target_record)) {
1479                     item.call_number.record = target_record;
1480                     needs_move = true;
1481             }
1482             if (needs_move) {
1483                 if (item.call_number.id in vols_to_move) {
1484                     copies_to_move[item.call_number.id].push(item.id);
1485                 } else {
1486                     vols_to_move[item.call_number.id] = item.call_number;
1487                     copies_to_move[item.call_number.id] = new Array;
1488                     copies_to_move[item.call_number.id].push(item.id);
1489                 }
1490             }
1491         });
1492
1493         var promises = [];
1494         angular.forEach(vols_to_move, function(vol) {
1495             promises.push(egCore.net.request(
1496                 'open-ils.cat',
1497                 'open-ils.cat.call_number.find_or_create',
1498                 egCore.auth.token(),
1499                 vol.label,
1500                 vol.record, // may be new
1501                 vol.owning_lib, // may be new
1502                 vol.prefix.id,
1503                 vol.suffix.id,
1504                 vol.label_class
1505             ).then(function(resp) {
1506                 var evt = egCore.evt.parse(resp);
1507                 if (evt) return;
1508                 return egCore.net.request(
1509                     'open-ils.cat',
1510                     'open-ils.cat.transfer_copies_to_volume',
1511                     egCore.auth.token(),
1512                     resp.acn_id,
1513                     copies_to_move[vol.id]
1514                 );
1515             }));
1516         });
1517         $q.all(promises).then(function() {
1518             ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1519             holdingsSvcInst.fetchAgain().then(function() {
1520                 $scope.holdingsGridDataProvider.refresh();
1521             });
1522         });
1523     }
1524
1525     $scope.gridCellHandlers = {};
1526     $scope.gridCellHandlers.copyAlertsEdit = function(id) {
1527         egCirc.manage_copy_alerts([id]).then(function() {
1528             // update grid items?
1529         });
1530     };
1531
1532     $scope.transferItems = function (){
1533         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
1534
1535         if (!xfer_target) {
1536             // we have no specific volume, let's try to fill in the
1537             // blanks instead
1538             return $scope.transferItemsAutoFill();
1539         }
1540
1541         var copy_ids = gatherSelectedHoldingsIds();
1542         if (copy_ids.length > 0) {
1543             egCore.net.request(
1544                 'open-ils.cat',
1545                 'open-ils.cat.transfer_copies_to_volume',
1546                 egCore.auth.token(),
1547                 xfer_target,
1548                 copy_ids
1549             ).then(
1550                 function(resp) { // oncomplete
1551                     var evt = egCore.evt.parse(resp);
1552                     if (evt) {
1553                         egConfirmDialog.open(
1554                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1555                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1556                             {'evt_desc': evt.desc}
1557                         ).result.then(function() {
1558                             egCore.net.request(
1559                                 'open-ils.cat',
1560                                 'open-ils.cat.transfer_copies_to_volume.override',
1561                                 egCore.auth.token(),
1562                                 xfer_target,
1563                                 copy_ids,
1564                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1565                             ).then(function(resp) {
1566                                 holdingsSvcInst.fetchAgain().then(function() {
1567                                     $scope.holdingsGridDataProvider.refresh();
1568                                 });
1569                             });
1570                         });
1571                     } else {
1572                         ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1573                         holdingsSvcInst.fetchAgain().then(function() {
1574                             $scope.holdingsGridDataProvider.refresh();
1575                         });
1576                     }
1577                 },
1578                 null, // onerror
1579                 null // onprogress
1580             )
1581         }
1582     }
1583
1584     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1585         angular.forEach(
1586             gatherSelectedHoldingsIds(),
1587             function (cid) {
1588                 var url = egCore.env.basePath +
1589                           'cat/item/' + cid + '/triggered_events';
1590                 $timeout(function() { $window.open(url, '_blank') });
1591             }
1592         );
1593     }
1594
1595     $scope.selectedHoldingsItemStatusHolds = function (){
1596         angular.forEach(
1597             gatherSelectedHoldingsIds(),
1598             function (cid) {
1599                 var url = egCore.env.basePath +
1600                           'cat/item/' + cid + '/holds';
1601                 $timeout(function() { $window.open(url, '_blank') });
1602             }
1603         );
1604     }
1605
1606     $scope.selectedHoldingsPrintLabels = function() {
1607         egCore.net.request(
1608             'open-ils.actor',
1609             'open-ils.actor.anon_cache.set_value',
1610             null, 'print-labels-these-copies', {
1611                 copies : gatherSelectedHoldingsIds()
1612             }
1613         ).then(function(key) {
1614             if (key) {
1615                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
1616                 $timeout(function() { $window.open(url, '_blank') });
1617             } else {
1618                 alert('Could not create anonymous cache key!');
1619             }
1620         });
1621     }
1622
1623     $scope.selectedHoldingsDamaged = function () {
1624         var copy_list = gatherSelectedRawCopies();
1625         if (copy_list.length == 0) return;
1626
1627         angular.forEach(copy_list, function(cp) {
1628             egCirc.mark_damaged({
1629                 id: cp.id(),
1630                 barcode: cp.barcode(),
1631                 circ_lib: cp.circ_lib().id()
1632             }).then(function() {
1633                 holdingsSvcInst.fetchAgain().then(function() {
1634                     $scope.holdingsGridDataProvider.refresh();
1635                 });
1636             });
1637         });
1638     }
1639
1640     $scope.selectedHoldingsMissing = function () {
1641         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
1642             holdingsSvcInst.fetchAgain().then(function() {
1643                 $scope.holdingsGridDataProvider.refresh();
1644             });
1645         });
1646     }
1647
1648     $scope.selectedHoldingsCopyAlertsAdd = function() {
1649         egCirc.add_copy_alerts(gatherSelectedHoldingsIds()).then(function() {
1650             // no need to refresh grid
1651         });
1652     }
1653     $scope.selectedHoldingsCopyAlertsManage = function() {
1654         egCirc.manage_copy_alerts(gatherSelectedHoldingsIds()).then(function() {
1655             // no need to refresh grid
1656         });
1657     }
1658
1659     $scope.attach_to_peer_bib = function() {
1660         var copy_list = gatherSelectedHoldingsIds();
1661         if (copy_list.length == 0) return;
1662
1663         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1664             if (!target_record) return;
1665
1666             return $uibModal.open({
1667                 templateUrl: './cat/catalog/t_conjoined_selector',
1668                 backdrop: 'static',
1669                 animation: true,
1670                 controller:
1671                        ['$scope','$uibModalInstance',
1672                 function($scope , $uibModalInstance) {
1673                     $scope.update = false;
1674
1675                     $scope.peer_type = null;
1676                     $scope.peer_type_list = [];
1677                     conjoinedSvc.get_peer_types().then(function(list){
1678                         $scope.peer_type_list = list;
1679                     });
1680     
1681                     $scope.ok = function(type) {
1682                         var promises = [];
1683     
1684                         angular.forEach(copy_list, function (cp) {
1685                             var n = new egCore.idl.bpbcm();
1686                             n.isnew(true);
1687                             n.peer_record(target_record);
1688                             n.target_copy(cp);
1689                             n.peer_type(type);
1690                             promises.push(egCore.pcrud.create(n));
1691                         });
1692     
1693                         return $q.all(promises).then(function(){$uibModalInstance.close()});
1694                     }
1695     
1696                     $scope.cancel = function($event) {
1697                         $uibModalInstance.dismiss();
1698                         $event.preventDefault();
1699                     }
1700                 }]
1701             });
1702         });
1703     }
1704
1705
1706     // ------------------------------------------------------------------
1707     // Holds 
1708     var provider = egGridDataProvider.instance({});
1709     $scope.hold_grid_data_provider = provider;
1710     $scope.grid_actions = egHoldGridActions;
1711     $scope.grid_actions.refresh = function () { holds = []; hold_count = 0; provider.refresh() };
1712     $scope.hold_grid_controls = {};
1713
1714     var holds = []; // current list of holds
1715     var hold_count = 0;
1716     provider.get = function(offset, count) {
1717         if ($scope.record_tab != 'holds') return $q.when();
1718
1719         // see if we have the requested range cached
1720         if (holds[offset]) {
1721             return provider.arrayNotifier(holds, offset, count);
1722         }
1723
1724         hold_count = 0;
1725         holds = [];
1726         var restrictions = {
1727                 is_staff_request : 'true',
1728                 fulfillment_time : null,
1729                 cancel_time      : null,
1730                 record_id        : $scope.record_id,
1731                 pickup_lib       : egCore.org.descendants($scope.pickup_ou.id(), true)
1732         };
1733
1734         var order_by = [{ request_time : null }];
1735         if (provider.sort && provider.sort.length) {
1736             order_by = [];
1737             angular.forEach(provider.sort, function (c) {
1738                 if (!angular.isObject(c)) {
1739                     if (c.match(/^hold\./)) {
1740                         var i = c.replace('hold.','');
1741                         var ob = {};
1742                         ob[i] = null;
1743                         order_by.push(ob);
1744                     }
1745                 } else {
1746                     var i = Object.keys(c)[0];
1747                     var direction = c[i];
1748                     if (i.match(/^hold\./)) {
1749                         i = i.replace('hold.','');
1750                         var ob = {}
1751                         ob[i] = {dir:direction};
1752                         order_by.push(ob);
1753                     }
1754                 }
1755             });
1756         }
1757
1758         egProgressDialog.open({max : 1, value : 0});
1759         var first = true;
1760         return egHolds.fetch_wide_holds(
1761             restrictions,
1762             order_by
1763         ).then(function () {
1764                 return provider.arrayNotifier(holds, offset, count);
1765             },
1766             null,
1767             function(hold_data) {
1768                 if (first) {
1769                     hold_count = hold_data;
1770                     first = false;
1771                     egProgressDialog.update({max:hold_count});
1772                 } else {
1773                     egProgressDialog.increment();
1774                     var new_item = { id : hold_data.id, hold : hold_data };
1775                     new_item.status_string =
1776                         egCore.strings['HOLD_STATUS_' + hold_data.hold_status]
1777                         || hold_data.hold_status;
1778
1779                     holds.push(new_item);
1780                 }
1781             }
1782         ).finally(egProgressDialog.close);
1783
1784     }
1785
1786     $scope.detail_view = function(action, user_data, items) {
1787         if (h = items[0]) {
1788             $scope.detail_hold_id = h.hold.id;
1789         }
1790     }
1791
1792     $scope.list_view = function(items) {
1793          $scope.detail_hold_id = null;
1794     }
1795
1796     // refresh the list of record holds when the pickup lib is changed.
1797     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1798     $scope.pickup_ou_changed = function(org) {
1799         $scope.pickup_ou = org;
1800         holds = []
1801         hold_count = 0;
1802         provider.refresh();
1803     }
1804
1805     function map_prefix_to_subhash (h,pf) {
1806         var newhash = {};
1807         angular.forEach(Object.keys(h), function (k) {
1808             if (k.startsWith(pf)) {
1809                 var nk = k.substr(pf.length);
1810                 newhash[nk] = h[k];
1811             }
1812         });
1813         return newhash;
1814     }
1815
1816     $scope.print_holds = function() {
1817         var pholds = [];
1818         angular.forEach(holds, function(item) {
1819             pholds.push({
1820                 hold : item.hold,
1821                 status_string : item.status_string,
1822                 patron_first : item.hold.usr_first_given_name,
1823                 patron_last : item.hold.usr_family_name,
1824                 patron_alias : item.hold.usr_alias,
1825                 patron_barcode : item.hold.ucard_barcode,
1826                 copy : map_prefix_to_subhash(item.hold,'cp_'),
1827                 volume : map_prefix_to_subhash(item.hold,'cn_'),
1828                 title : item.hold.title,
1829                 author : item.hold.author
1830             });
1831         });
1832
1833         egCore.print.print({
1834             context : 'receipt', 
1835             template : 'holds_for_bib', 
1836             scope : {holds : pholds}
1837         });
1838     }
1839
1840     $scope.current_hold_transfer_dest = egCore.hatch.getLocalItem ('eg.circ.hold.title_transfer_target');
1841
1842     $scope.mark_hold_transfer_dest = function() {
1843         $scope.current_hold_transfer_dest = $scope.record_id;
1844         egCore.hatch.setLocalItem(
1845             'eg.circ.hold.title_transfer_target', $scope.record_id);
1846         ngToast.create(egCore.strings.HOLD_TRANSFER_DEST_MARKED);
1847     }
1848
1849     // UI presents this option as "all holds"
1850     $scope.transfer_holds_to_marked = function() {
1851         var hold_ids = $scope.hold_grid_controls.allItems().map(
1852             function(hold_data) {return hold_data.hold.id});
1853         egHolds.transfer_to_marked_title(hold_ids);
1854     }
1855
1856     // ------------------------------------------------------------------
1857     // Initialize the selected tab
1858
1859     // we explicitly initialize catalog_url because otherwise Firefox
1860     // ends up setting it to $BASE_URL/{{url}}, which then messes
1861     // things up. See LP#1708951
1862     $scope.catalog_url = '';
1863
1864     function init_cat_url() {
1865         // Set the initial catalog URL.  This only happens once.
1866         // The URL is otherwise generated through user navigation.
1867         if ($scope.catalog_url) return;
1868
1869         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1870
1871         // A record ID in the path indicates a request for the record-
1872         // specific page.
1873         if ($routeParams.record_id) {
1874             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1875         }
1876
1877         // Jumping directly to the results page by passing a search
1878         // query via the URL.  Copy all URL params to the iframe url.
1879         if ($location.path().match(/catalog\/results/)) {
1880             url = url.replace(/advanced/, '/results?');
1881             var first = true;
1882             angular.forEach($location.search(), function(val, key) {
1883                 if (!first) url += '&';
1884                 first = false;
1885                 url += encodeURIComponent(key) 
1886                     + '=' + encodeURIComponent(val);
1887             });
1888         }
1889
1890         // if we're displaying the advanced search form, select
1891         // whatever default pane the user has chosen via workstation
1892         // preference
1893         if (url.match(/\/opac\/advanced$/)) {
1894             var adv_pane = egCore.hatch.getLocalItem('eg.search.adv_pane');
1895             if (adv_pane) {
1896                 url += '?pane=' + encodeURIComponent(adv_pane);
1897             }
1898         }
1899
1900         $scope.catalog_url = url;
1901     }
1902
1903     function init_parts_url() {
1904         $scope.parts_url = $location
1905             .absUrl()
1906             .replace(
1907                 /\/staff.*/,
1908                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1909             );
1910     }
1911
1912     $scope.set_record_tab = function(tab) {
1913         $scope.record_tab = tab;
1914
1915         switch(tab) {
1916
1917             case 'monoparts':
1918                 init_parts_url();
1919                 break;
1920
1921             case 'catalog':
1922                 init_cat_url();
1923                 break;
1924
1925             case 'holds':
1926                 $scope.detail_hold_record_id = $scope.record_id; 
1927                 // refresh the holds grid
1928                 provider.refresh();
1929
1930                 break;
1931         }
1932     }
1933
1934     $scope.set_default_record_tab = function() {
1935         egCore.hatch.setLocalItem(
1936             'eg.cat.default_record_tab', $scope.record_tab);
1937         $timeout(function(){$scope.default_tab = $scope.record_tab});
1938     }
1939
1940     var tab;
1941     if ($scope.record_id) {
1942         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1943         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1944
1945     } else {
1946         tab = $routeParams.record_tab || 'catalog';
1947     }
1948     $scope.set_record_tab(tab);
1949
1950 }])
1951
1952 .controller('AuthorityCtrl',
1953        ['$scope','$routeParams','$location','$window','$q','egCore',
1954 function($scope , $routeParams , $location , $window , $q , egCore) {
1955
1956     // set record ID on page load if available...
1957     $scope.authority_id = $routeParams.authority_id;
1958
1959     if ($routeParams.authority_id) $scope.from_route = true;
1960     else $scope.from_route = false;
1961
1962     $scope.stop_unload = false;
1963 }])
1964
1965 .controller('URLVerifyCtrl',
1966        ['$scope','$location',
1967 function($scope , $location) {
1968     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
1969 }])
1970
1971 .controller('VandelayCtrl',
1972        ['$scope','$location', 'egCore', '$uibModal',
1973 function($scope , $location, egCore, $uibModal) {
1974     $scope.vandelay_url = $location.absUrl().replace(/\/staff\/cat\/catalog\/vandelay/, '/vandelay/vandelay');
1975     $scope.funcs = {};
1976     $scope.funcs.edit_marc_modal = function(bre, callback){
1977         var marcArgs = { 'marc_xml': bre.marc() };
1978         var vqbibrecId = bre.id();
1979         $uibModal.open({
1980             templateUrl: './cat/catalog/t_edit_marc_modal',
1981             backdrop: 'static',
1982             size: 'lg',
1983             controller: ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
1984                 $scope.focusMe = true;
1985                 $scope.recordId = vqbibrecId;
1986                 $scope.args = marcArgs;
1987                 $scope.dirty_flag = false;
1988                 $scope.ok = function(marg){
1989                     $uibModalInstance.close(marg);
1990                 };
1991                 $scope.cancel = function(){ $uibModalInstance.dismiss() }
1992             }]
1993         }).result.then(function(res){
1994             var new_xml = res.marc_xml;
1995             egCore.pcrud.retrieve('vqbr', vqbibrecId).then(function(vqbib){
1996                 vqbib.marc(new_xml);
1997                 egCore.pcrud.update(vqbib).then( function(){ callback(vqbibrecId); });
1998             });
1999         });
2000     };
2001 }])
2002
2003 .controller('ManageAuthoritiesCtrl',
2004        ['$scope','$location',
2005 function($scope , $location) {
2006     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
2007 }])
2008
2009 .controller('BatchEditCtrl',
2010        ['$scope','$location','$routeParams',
2011 function($scope , $location , $routeParams) {
2012     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
2013     if ($routeParams.container_type) {
2014         switch ($routeParams.container_type) {
2015             case 'bucket':
2016                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
2017                 break;
2018             case 'record':
2019                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
2020                 break;
2021         };
2022     }
2023 }])
2024
2025  
2026 .filter('boolText', function(){
2027     return function (v) {
2028         return v == 't';
2029     }
2030 })
2031
2032 .factory('conjoinedSvc', 
2033        ['egCore','$q',
2034 function(egCore , $q) {
2035
2036     var service = {
2037         items : [], // record search results
2038         index : 0, // search grid index
2039         rid : null
2040     };
2041
2042     service.flesh = {   
2043         flesh : 4, 
2044         flesh_fields : {
2045             bpbcm : ['target_copy','peer_type'],
2046             acp : ['call_number'],
2047             acn : ['record'],
2048             bre : ['simple_record']
2049         },
2050         // avoid fetching the MARC blob by specifying which
2051         // fields on the bre to select.  More may be needed.
2052         // note that fleshed fields are explicitly selected.
2053         select : { bre : ['id'] },
2054         order_by : { bpbcm : ['id'] },
2055     }
2056
2057     // resolved with the last received copy
2058     service.fetch = function(rid) {
2059         if (!rid && !service.rid) return $q.when();
2060
2061         if (rid) service.rid = rid;
2062         service.items = [];
2063         service.index = 0;
2064
2065         return egCore.pcrud.search(
2066             'bpbcm',
2067             {peer_record : service.rid},
2068             service.flesh,
2069             {atomic : true}
2070         ).then( function(list) { // finished
2071             service.items = list;
2072             return service.items;
2073         });
2074     }
2075
2076     // returns a promise resolved with the list of peer bib types
2077     service.get_peer_types = function() {
2078         if (egCore.env.bpt)
2079             return $q.when(egCore.env.bpt.list);
2080
2081         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
2082         .then(function(list) {
2083             egCore.env.absorbList(list, 'bpt');
2084             return list;
2085         });
2086     };
2087
2088     return service;
2089 }])
2090
2091