]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/regctl.js
91ea251b5b5d1874a6198d989b03221a03d2d34f
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / regctl.js
1
2 angular.module('egCoreMod')
3 // toss tihs onto egCoreMod since the page app may vary
4
5 .factory('patronRegSvc', ['$q', 'egCore', function($q, egCore) {
6
7     var service = {
8         field_doc : {},              // config.idl_field_doc
9         profiles : [],               // permission groups
10         sms_carriers : [],
11         user_settings : {},          // applied user settings
12         user_setting_types : {},     // config.usr_setting_type
13         virt_id : -1                 // virtual ID for new objects
14     };
15
16     // launch a series of parallel data retrieval calls
17     service.init = function(scope) {
18         return $q.all([
19             service.get_field_doc(),
20             service.get_perm_groups(),
21             service.get_ident_types(),
22             service.get_user_settings(),
23             service.get_org_settings(),
24             service.get_stat_cats(),
25             service.get_surveys(),
26             service.get_net_access_levels()
27         ]);
28     };
29
30     service.get_surveys = function() {
31         var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
32
33         return egCore.pcrud.search('asv', 
34             {owner : org_ids}, 
35             {flesh : 1, flesh_fields : {asv : ['questions']}}, 
36             {atomic : true}
37         ).then(function(surveys) {
38             service.surveys = surveys;
39         });
40     }
41
42     service.get_stat_cats = function() {
43         return egCore.net.request(
44             'open-ils.circ',
45             'open-ils.circ.stat_cat.actor.retrieve.all',
46             egCore.auth.token(), egCore.auth.user().ws_ou()
47         ).then(function(cats) {
48             service.stat_cats = cats;
49         });
50     };
51
52     service.get_org_settings = function() {
53         return egCore.org.settings([
54             'global.password_regex',
55             'global.juvenile_age_threshold',
56             'patron.password.use_phone',
57             'ui.patron.default_inet_access_level',
58             'ui.patron.default_ident_type',
59             'ui.patron.default_country',
60             'ui.patron.registration.require_address',
61             'circ.holds.behind_desk_pickup_supported',
62             'circ.patron_edit.clone.copy_address',
63             'ui.patron.edit.au.prefix.require',
64             'ui.patron.edit.au.prefix.show',
65             'ui.patron.edit.au.prefix.suggest',
66             'ui.patron.edit.ac.barcode.regex',
67             'ui.patron.edit.au.second_given_name.show',
68             'ui.patron.edit.au.second_given_name.suggest',
69             'ui.patron.edit.au.suffix.show',
70             'ui.patron.edit.au.suffix.suggest',
71             'ui.patron.edit.au.alias.show',
72             'ui.patron.edit.au.alias.suggest',
73             'ui.patron.edit.au.dob.require',
74             'ui.patron.edit.au.dob.show',
75             'ui.patron.edit.au.dob.suggest',
76             'ui.patron.edit.au.dob.calendar',
77             'ui.patron.edit.au.juvenile.show',
78             'ui.patron.edit.au.juvenile.suggest',
79             'ui.patron.edit.au.ident_value.show',
80             'ui.patron.edit.au.ident_value.suggest',
81             'ui.patron.edit.au.ident_value2.show',
82             'ui.patron.edit.au.ident_value2.suggest',
83             'ui.patron.edit.au.email.require',
84             'ui.patron.edit.au.email.show',
85             'ui.patron.edit.au.email.suggest',
86             'ui.patron.edit.au.email.regex',
87             'ui.patron.edit.au.email.example',
88             'ui.patron.edit.au.day_phone.require',
89             'ui.patron.edit.au.day_phone.show',
90             'ui.patron.edit.au.day_phone.suggest',
91             'ui.patron.edit.au.day_phone.regex',
92             'ui.patron.edit.au.day_phone.example',
93             'ui.patron.edit.au.evening_phone.require',
94             'ui.patron.edit.au.evening_phone.show',
95             'ui.patron.edit.au.evening_phone.suggest',
96             'ui.patron.edit.au.evening_phone.regex',
97             'ui.patron.edit.au.evening_phone.example',
98             'ui.patron.edit.au.other_phone.require',
99             'ui.patron.edit.au.other_phone.show',
100             'ui.patron.edit.au.other_phone.suggest',
101             'ui.patron.edit.au.other_phone.regex',
102             'ui.patron.edit.au.other_phone.example',
103             'ui.patron.edit.phone.regex',
104             'ui.patron.edit.phone.example',
105             'ui.patron.edit.au.active.show',
106             'ui.patron.edit.au.active.suggest',
107             'ui.patron.edit.au.barred.show',
108             'ui.patron.edit.au.barred.suggest',
109             'ui.patron.edit.au.master_account.show',
110             'ui.patron.edit.au.master_account.suggest',
111             'ui.patron.edit.au.claims_returned_count.show',
112             'ui.patron.edit.au.claims_returned_count.suggest',
113             'ui.patron.edit.au.claims_never_checked_out_count.show',
114             'ui.patron.edit.au.claims_never_checked_out_count.suggest',
115             'ui.patron.edit.au.alert_message.show',
116             'ui.patron.edit.au.alert_message.suggest',
117             'ui.patron.edit.aua.post_code.regex',
118             'ui.patron.edit.aua.post_code.example',
119             'ui.patron.edit.aua.county.require',
120             'format.date',
121             'ui.patron.edit.default_suggested',
122             'opac.barcode_regex',
123             'opac.username_regex',
124             'sms.enable',
125             'ui.patron.edit.aua.state.require',
126             'ui.patron.edit.aua.state.suggest',
127             'ui.patron.edit.aua.state.show'
128         ]).then(function(settings) {
129             service.org_settings = settings;
130             return service.process_org_settings(settings);
131         });
132     };
133
134     // some org settings require the retrieval of additional data
135     service.process_org_settings = function(settings) {
136
137         var promises = [];
138
139         if (settings['sms.enable']) {
140             // fetch SMS carriers
141             promises.push(
142                 egCore.pcrud.search('csc', 
143                     {active: 'true'}, 
144                     {'order_by':[
145                         {'class':'csc', 'field':'name'},
146                         {'class':'csc', 'field':'region'}
147                     ]}, {atomic : true}
148                 ).then(function(carriers) {
149                     service.sms_carriers = carriers;
150                 })
151             );
152         } else {
153             // if other promises are added below, this is not necessary.
154             promises.push($q.when());  
155         }
156
157         // other post-org-settings processing goes here,
158         // adding to promises as needed.
159
160         return $q.all(promises);
161     };
162
163     service.get_ident_types = function() {
164         return egCore.pcrud.retrieveAll('cit', {}, {atomic : true})
165         .then(function(types) { service.ident_types = types });
166     };
167
168     service.get_net_access_levels = function() {
169         return egCore.pcrud.retrieveAll('cnal', {}, {atomic : true})
170         .then(function(levels) { service.net_access_levels = levels });
171     }
172
173     service.get_perm_groups = function() {
174         if (egCore.env.pgt) {
175             service.profiles = egCore.env.pgt.list;
176             return $q.when();
177         } else {
178             return egCore.pcrud.search('pgt', {parent : null}, 
179                 {flesh : -1, flesh_fields : {pgt : ['children']}}
180             ).then(
181                 function(tree) {
182                     egCore.env.absorbTree(tree, 'pgt')
183                     service.profiles = egCore.env.pgt.list;
184                 }
185             );
186         }
187     }
188
189     service.get_field_doc = function() {
190
191         return egCore.pcrud.search('fdoc', {
192             fm_class: ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva']})
193         .then(null, null, function(doc) {
194             if (!service.field_doc[doc.fm_class()]) {
195                 service.field_doc[doc.fm_class()] = {};
196             }
197             service.field_doc[doc.fm_class()][doc.field()] = doc;
198         });
199     };
200
201     service.get_user_settings = function() {
202         var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
203
204         return egCore.pcrud.search('cust', {
205             '-or' : [
206                 {name : [ // common user settings
207                     'circ.holds_behind_desk', 
208                     'circ.collections.exempt', 
209                     'opac.hold_notify', 
210                     'opac.default_phone', 
211                     'opac.default_pickup_location', 
212                     'opac.default_sms_carrier', 
213                     'opac.default_sms_notify']}, 
214                 {name : { // opt-in notification user settings
215                     'in': {
216                         select : {atevdef : ['opt_in_setting']}, 
217                         from : 'atevdef',
218                         // we only care about opt-in settings for 
219                         // event_defs our users encounter
220                         where : {'+atevdef' : {owner : org_ids}}
221                     }
222                 }}
223             ]
224         }, {}, {atomic : true}).then(function(setting_types) {
225
226             angular.forEach(setting_types, function(stype) {
227                 service.user_setting_types[stype.name()] = stype;
228             });
229
230             if (service.patron_id) {
231                 // retrieve applied values for the current user 
232                 // for the setting types we care about.
233
234                 var setting_names = 
235                     setting_types.map(function(obj) { return obj.name() });
236
237                 return egCore.net.request(
238                     'open-ils.actor', 
239                     'open-ils.actor.patron.settings.retrieve.authoritative',
240                     egCore.auth.token(),
241                     service.patron_id,
242                     setting_names
243                 ).then(function(settings) {
244                     service.user_settings = settings;
245                 });
246             } else {
247
248                 // apply default user setting values
249                 angular.forEach(setting_types, function(stype, index) {
250                     if (stype.reg_default() != undefined) {
251                         service.user_settings[setting.name()] = 
252                             setting.reg_default();
253                     }
254                 });
255             }
256         });
257     }
258
259     service.init_patron = function(current) {
260
261         if (!current)
262             return service.init_new_patron();
263
264         service.patron = current;
265         return service.init_existing_patron(current)
266     }
267
268     service.ingest_address = function(patron, addr) {
269         addr.valid = addr.valid == 't';
270         addr.within_city_limits = addr.within_city_limits == 't';
271         addr._is_mailing = (patron.mailing_address && 
272             addr.id == patron.mailing_address.id);
273         addr._is_billing = (patron.billing_address && 
274             addr.id == patron.billing_address.id);
275     }
276
277     /*
278      * Existing patron objects reqire some data munging before insertion
279      * into the scope.
280      *
281      * 1. Turn everything into a hash
282      * 2. ... Except certain fields (selectors) whose widgets require objects
283      * 3. Bools must be Boolean, not t/f.
284      */
285     service.init_existing_patron = function(current) {
286
287         var patron = egCore.idl.toHash(current);
288
289         patron.home_ou = egCore.org.get(patron.home_ou.id);
290         patron.expire_date = new Date(Date.parse(patron.expire_date));
291         patron.dob = new Date(Date.parse(patron.dob));
292         patron.profile = current.profile(); // pre-hash version
293         patron.net_access_level = current.net_access_level();
294         patron.ident_type = current.ident_type();
295
296         angular.forEach(
297             ['juvenile', 'barred', 'active', 'master_account'],
298             function(field) { patron[field] = patron[field] == 't'; }
299         );
300
301         angular.forEach(patron.cards, function(card) {
302             card.active = card.active == 't';
303             if (card.id == patron.card.id) {
304                 patron.card = card;
305                 card._primary = 'on';
306             }
307         });
308
309         angular.forEach(patron.addresses, 
310             function(addr) { service.ingest_address(patron, addr) });
311
312         return patron;
313     }
314
315     service.init_new_patron = function() {
316         var addr = {
317             id : service.virt_id--,
318             isnew : true,
319             valid : true,
320             address_type : egCore.strings.REG_ADDR_TYPE,
321             _is_mailing : true,
322             _is_billing : true,
323             within_city_limits : true
324         };
325
326         var card = {
327             id : service.virt_id--,
328             isnew : true,
329             active : true,
330             _primary : 'on'
331         };
332
333         return {
334             isnew : true,
335             active : true,
336             card : card,
337             cards : [card],
338             home_ou : egCore.org.get(egCore.auth.user().ws_ou()),
339                         
340             // TODO default profile group?
341             addresses : [addr]
342         };
343     }
344
345     // translate the patron back into IDL form
346     service.save_user = function(phash) {
347
348         var patron = egCore.idl.fromHash('au', phash);
349
350         patron.home_ou(patron.home_ou().id());
351         patron.expire_date(
352             patron.expire_date().toISOString().replace(/T.*/,''));
353         patron.profile(patron.profile().id());
354         if (patron.dob()) 
355             patron.dob(patron.dob().toISOString().replace(/T.*/,''));
356         if (patron.ident_type()) 
357             patron.ident_type(patron.ident_type().id());
358         if (patron.net_access_level())
359             patron.net_access_level(patron.net_access_level().id());
360
361         angular.forEach(
362             ['juvenile', 'barred', 'active', 'master_account'],
363             function(field) { patron[field](phash[field] ? 't' : 'f'); }
364         );
365
366         var card_hashes = patron.cards();
367         patron.cards([]);
368         angular.forEach(card_hashes, function(chash) {
369             var card = egCore.idl.fromHash('ac', chash)
370             card.usr(patron.id());
371             card.active(chash.active ? 't' : 'f');
372             patron.cards().push(card);
373             if (chash._primary) {
374                 patron.card(card);
375             }
376         });
377
378         var addr_hashes = patron.addresses();
379         patron.addresses([]);
380         angular.forEach(addr_hashes, function(addr_hash) {
381             if (!addr_hash.isnew && !addr_hash.isdeleted) 
382                 addr_hash.ischanged = true;
383             var addr = egCore.idl.fromHash('aua', addr_hash);
384             patron.addresses().push(addr);
385             addr.valid(addr.valid() ? 't' : 'f');
386             addr.within_city_limits(addr.within_city_limits() ? 't' : 'f');
387             if (addr_hash._is_mailing) patron.mailing_address(addr);
388             if (addr_hash._is_billing) patron.billing_address(addr);
389
390             console.log('deleted? ' + addr.isdeleted());
391         });
392
393         console.log(patron.addresses());
394
395         if (!patron.isnew()) patron.ischanged(true);
396
397         return egCore.net.request(
398             'open-ils.actor', 
399             'open-ils.actor.patron.update',
400             egCore.auth.token(), patron);
401     }
402
403     service.save_user_settings = function(new_user, user_settings) {
404         // user_settings contains the values from the scope/form.
405         // service.user_settings contain the values from page load time.
406
407         var settings = {};
408         if (service.patron_id) {
409             // only update modified settings for existing patrons
410             angular.forEach(user_settings, function(val, key) {
411                 if (val !== service.user_settings[key])
412                     settings[key] = val;
413             });
414
415         } else {
416             // all non-null setting values are updated for new patrons
417             angular.forEach(user_settings, function(val, key) {
418                 if (val !== null) settings[key] = val;
419             });
420         }
421
422         if (Object.keys(settings).length == 0) return $q.when();
423
424         return egCore.net.request(
425             'open-ils.actor',
426             'open-ils.actor.patron.settings.update',
427             egCore.auth.token(), new_user.id(), settings
428         ).then(function(resp) {
429             console.log('settings returned ' + resp);
430             return resp;
431         });
432     }
433
434     return service;
435 }]);
436
437
438 function PatronRegCtrl($scope, $routeParams, 
439     $q, $modal, $window, egCore, patronSvc, patronRegSvc) {
440
441     $scope.clone_id = $routeParams.clone_id;
442     $scope.stage_username = $routeParams.stage_username;
443     $scope.patron_id = 
444         patronRegSvc.patron_id = $routeParams.edit_id || $routeParams.id;
445
446     if (!$scope.edit_passthru) {
447         // in edit more, scope.edit_passthru is delivered to us by
448         // the enclosing controller.  In register mode, there is 
449         // no enclosing controller, so we create our own.
450         $scope.edit_passthru = {};
451     }
452
453     // 0=all, 1=suggested, 2=all
454     $scope.edit_passthru.vis_level = 0; 
455     // TODO: add save/clone handlers here
456
457     $q.all([
458
459         $scope.initTab ? // initTab comes from patron app
460             $scope.initTab('edit', $routeParams.id) : $q.when(),
461
462         patronRegSvc.init()
463
464     ]).then(function() {
465         // called after initTab and patronRegSvc.init have completed
466
467         var prs = patronRegSvc; // brevity
468         // in standalone mode, we have no patronSvc
469         $scope.patron = prs.init_patron(patronSvc ? patronSvc.current : null);
470         $scope.field_doc = prs.field_doc;
471         $scope.profiles = prs.profiles;
472         $scope.ident_types = prs.ident_types;
473         $scope.net_access_levels = prs.net_access_levels;
474         $scope.user_setting_types = prs.user_setting_types;
475         $scope.org_settings = prs.org_settings;
476         $scope.sms_carriers = prs.sms_carriers;
477         $scope.stat_cats = prs.stat_cats;
478         $scope.surveys = prs.surveys;
479
480         $scope.user_settings = prs.user_settings;
481         // clone the user settings back into the patronRegSvc so
482         // we have a copy of the original state of the settings.
483         prs.user_settings = {};
484         angular.forEach($scope.user_settings, function(val, key) {
485             prs.user_settings[key] = val;
486         });
487
488         extract_hold_notify();
489
490         if ($scope.org_settings['ui.patron.edit.default_suggested'])
491             $scope.edit_passthru.vis_level = 1;
492
493         if ($scope.patron.isnew) {
494             $scope.generate_password();
495             $scope.hold_notify_phone = true;
496             $scope.hold_notify_email = true;
497
498             if (prs.org_settings['ui.patron.default_ident_type']) {
499                 $scope.patron.ident_type = 
500                     prs.org_settings['ui.patron.default_ident_type'];
501             }
502             if (prs.org_settings['ui.patron.default_inet_access_level']) {
503                 $scope.patron.ident_type = 
504                     prs.org_settings['ui.patron.default_inet_access_level'];
505             }
506             if (prs.org_settings['ui.patron.default_country']) {
507                 $scope.patron.addresses[0].country = 
508                     prs.org_settings['ui.patron.default_country'];
509             }
510         }
511             
512     });
513
514     // returns the tree depth of the selected profile group tree node.
515     $scope.pgt_depth = function(grp) {
516         var d = 0;
517         while (grp = egCore.env.pgt.map[grp.parent()]) d++;
518         return d;
519     }
520
521     // IDL fields used for labels in the UI.
522     $scope.idl_fields = {
523         au  : egCore.idl.classes.au.field_map,
524         ac  : egCore.idl.classes.ac.field_map,
525         aua : egCore.idl.classes.aua.field_map
526     };
527
528     // field visibility cache.  Some fields are universally required.
529     var field_visibility = {
530         'ac.barcode' : 2,
531         'au.usrname' : 2,
532         'au.passwd' :  2,
533         // TODO passwd2 2,
534         'au.first_given_name' : 2,
535         'au.family_name' : 2,
536         'au.ident_type' : 2,
537         'au.home_ou' : 2,
538         'au.profile' : 2,
539         'au.expire_date' : 2,
540         'au.net_access_level' : 2,
541         'aua.address_type' : 2,
542         'aua.post_code' : 2,
543         'aua.street1' : 2,
544         'aua.street2' : 2,
545         'aua.city' : 2,
546         'aua.county' : 2,
547         'aua.state' : 2,
548         'aua.country' : 2,
549         'aua.valid' : 2,
550         'aua.within_city_limits' : 2,
551         'stat_cats' : 1,
552         'surveys' : 1
553     }; 
554
555     // returns true if the selected field should be visible
556     // given the current required/suggested/all setting.
557     $scope.show_field = function(field_key) {
558
559         if (field_visibility[field_key] == undefined) {
560             // compile and cache the visibility for the selected field
561
562             // org settings have not been received yet.
563             if (!$scope.org_settings) return false;
564
565             var req_set = 'ui.patron.edit.' + field_key + '.require';
566             var sho_set = 'ui.patron.edit.' + field_key + '.show';
567             var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
568
569             if ($scope.org_settings[req_set]) {
570                 field_visibility[field_key] = 2;
571             } else if ($scope.org_settings[sho_set]) {
572                 field_visibility[field_key] = 2;
573             } else if ($scope.org_settings[sug_set]) {
574                 field_visibility[field_key] = 1;
575             } else {
576                 field_visibility[field_key] = 0;
577             }
578         }
579
580         return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
581     }
582
583     // generates a random 4-digit password
584     $scope.generate_password = function() {
585         $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
586     }
587
588     $scope.set_expire_date = function() {
589         if (!$scope.patron.profile) return;
590         var seconds = egCore.date.intervalToSeconds(
591             $scope.patron.profile.perm_interval());
592         var now_epoch = new Date().getTime();
593         $scope.patron.expire_date = new Date(
594             now_epoch + (seconds * 1000 /* milliseconds */))
595     }
596
597     // grp is the pgt object
598     $scope.set_profile = function(grp) {
599         $scope.patron.profile = grp;
600         $scope.set_expire_date();
601     }
602
603     $scope.new_address = function() {
604         var addr = egCore.idl.toHash(new egCore.idl.aua());
605         patronRegSvc.ingest_address($scope.patron, addr);
606         addr.id = patronRegSvc.virt_id--;
607         addr.isnew = true;
608         addr.valid = true;
609         addr.within_city_limits = true;
610         $scope.patron.addresses.push(addr);
611     }
612
613     // keep deleted addresses out of the patron object so
614     // they won't appear in the UI.  They'll be re-inserted
615     // when the patron is updated.
616     deleted_addresses = [];
617     $scope.delete_address = function(id) {
618         var addresses = [];
619         angular.forEach($scope.patron.addresses, function(addr) {
620             if (addr.id == id) {
621                 if (id > 0) {
622                     addr.isdeleted = true;
623                     deleted_addresses.push(addr);
624                 }
625             } else {
626                 addresses.push(addr);
627             }
628         });
629         $scope.patron.addresses = addresses;
630     } 
631
632     $scope.post_code_changed = function(addr) { 
633         egCore.net.request(
634             'open-ils.search', 'open-ils.search.zip', addr.post_code)
635         .then(function(resp) {
636             if (!resp) return;
637             if (resp.city) addr.city = resp.city;
638             if (resp.state) addr.state = resp.state;
639             if (resp.county) addr.county = resp.county;
640             if (resp.alert) alert(resp.alert);
641         });
642     }
643
644     $scope.replace_card = function() {
645         $scope.patron.card.active = false;
646         $scope.patron.card.ischanged = true;
647
648         var new_card = egCore.idl.toHash(new egCore.idl.ac());
649         new_card.id = patronRegSvc.virt_id--;
650         new_card.isnew = true;
651         new_card.active = true;
652         new_card._primary = 'on';
653         $scope.patron.card = new_card;
654         $scope.patron.cards.push(new_card);
655     }
656
657     $scope.barcode_changed = function(bc) {
658         if (!bc) return;
659         egCore.net.request(
660             'open-ils.actor',
661             'open-ils.actor.barcode.exists',
662             egCore.auth.token(), bc
663         ).then(function(resp) {
664             if (resp == '1') {
665                 console.log('duplicate barcode detected: ' + bc);
666                 // DUPLICATE CARD
667             } else {
668                 if (!$scope.patron.usrname)
669                     $scope.patron.usrname = bc;
670                 // No dupe -- A-OK
671             }
672         });
673     }
674
675     $scope.cards_dialog = function() {
676         $modal.open({
677             templateUrl: './circ/patron/t_patron_cards_dialog',
678             controller: 
679                    ['$scope','$modalInstance','cards',
680             function($scope , $modalInstance , cards) {
681                 // scope here is the modal-level scope
682                 $scope.args = {cards : cards};
683                 $scope.ok = function() { $modalInstance.close($scope.args) }
684                 $scope.cancel = function () { $modalInstance.dismiss() }
685             }],
686             resolve : {
687                 cards : function() {
688                     // scope here is the controller-level scope
689                     return $scope.patron.cards;
690                 }
691             }
692         }).result.then(
693             function(args) {
694                 angular.forEach(args.cards, function(card) {
695                     card.ischanged = true; // assume cards need updating, OK?
696                     if (card._primary == 'on' && 
697                         card.id != $scope.patron.card.id) {
698                         $scope.patron.card = card;
699                     }
700                 });
701             }
702         );
703     }
704
705     $scope.set_addr_type = function(addr, type) {
706         var addrs = $scope.patron.addresses;
707         if (addr['_is_'+type]) {
708             angular.forEach(addrs, function(a) {
709                 if (a.id != addr.id) a['_is_'+type] = false;
710             });
711         } else {
712             // unchecking mailing/billing means we have to randomly
713             // select another address to fill that role.  Select the
714             // first address in the list (that does not match the
715             // modifed address)
716             for (var i = 0; i < addrs.length; i++) {
717                 if (addrs[i].id != addr.id) {
718                     addrs[i]['_is_' + type] = true;
719                     break;
720                 }
721             }
722         }
723     }
724
725
726     // Translate hold notify preferences from the form/scope back into a 
727     // single user setting value for opac.hold_notify.
728     function compress_hold_notify() {
729         var hold_notify = '';
730         var splitter = '';
731         if ($scope.hold_notify_phone) {
732             hold_notify = 'phone';
733             splitter = ':';
734         }
735         if ($scope.hold_notify_email) {
736             hold_notify = splitter + 'email';
737             splitter = ':';
738         }
739         if ($scope.hold_notify_sms) {
740             hold_notify = splitter + 'sms';
741             splitter = ':';
742         }
743         $scope.user_settings['opac.hold_notify'] = hold_notify;
744     }
745
746     function extract_hold_notify() {
747         notify = $scope.user_settings['opac.hold_notify'];
748         if (!notify) return;
749         $scope.hold_notify_phone = Boolean(notify.match(/phone/));
750         $scope.hold_notify_email = Boolean(notify.match(/email/));
751         $scope.hold_notify_sms = Boolean(notify.match(/sms/));
752     }
753
754     $scope.edit_passthru.save = function() {
755
756         // toss the deleted addresses back into the patron's list of
757         // addresses so it's included in the update
758         $scope.patron.addresses = 
759             $scope.patron.addresses.concat(deleted_addresses);
760         
761         compress_hold_notify();
762
763         patronRegSvc.save_user($scope.patron)
764         .then(function(new_user) { 
765             if (new_user && new_user.classname) {
766                 return patronRegSvc.save_user_settings(
767                     new_user, $scope.user_settings); 
768             } else {
769                 alert('Patron update failed. \n\n' + js2JSON(new_user));
770                 return true; // ensure page reloads to reset
771             }
772         }).then(function(keep_going) {
773             // reloading the page means potentially losing some information
774             // (e.g. last patron search), but is the only way to ensure all
775             // components are properly updated to reflect the modified patron.
776             $window.location.href = location.href;
777         });
778     }
779 }
780
781 // This controller may be loaded from different modules (patron edit vs.
782 // register new patron), so we have to inject the controller params manually.
783 PatronRegCtrl.$inject = ['$scope', '$routeParams', '$q', '$modal', 
784     '$window', 'egCore', 'patronSvc', 'patronRegSvc'];
785