]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/regctl.js
52dd1f8eea7e6c3b598fb23635600bdaaffb540d
[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 : false
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     // for existing patrons, disable barcode input by default
447     $scope.disable_bc = $scope.focus_usrname = Boolean($scope.patron_id);
448     $scope.focus_bc = !Boolean($scope.patron_id);
449
450     if (!$scope.edit_passthru) {
451         // in edit more, scope.edit_passthru is delivered to us by
452         // the enclosing controller.  In register mode, there is 
453         // no enclosing controller, so we create our own.
454         $scope.edit_passthru = {};
455     }
456
457     // 0=all, 1=suggested, 2=all
458     $scope.edit_passthru.vis_level = 0; 
459     // TODO: add save/clone handlers here
460
461     $q.all([
462
463         $scope.initTab ? // initTab comes from patron app
464             $scope.initTab('edit', $routeParams.id) : $q.when(),
465
466         patronRegSvc.init()
467
468     ]).then(function() {
469         // called after initTab and patronRegSvc.init have completed
470
471         var prs = patronRegSvc; // brevity
472         // in standalone mode, we have no patronSvc
473         $scope.patron = prs.init_patron(patronSvc ? patronSvc.current : null);
474         $scope.field_doc = prs.field_doc;
475         $scope.profiles = prs.profiles;
476         $scope.ident_types = prs.ident_types;
477         $scope.net_access_levels = prs.net_access_levels;
478         $scope.user_setting_types = prs.user_setting_types;
479         $scope.org_settings = prs.org_settings;
480         $scope.sms_carriers = prs.sms_carriers;
481         $scope.stat_cats = prs.stat_cats;
482         $scope.surveys = prs.surveys;
483
484         $scope.user_settings = prs.user_settings;
485         // clone the user settings back into the patronRegSvc so
486         // we have a copy of the original state of the settings.
487         prs.user_settings = {};
488         angular.forEach($scope.user_settings, function(val, key) {
489             prs.user_settings[key] = val;
490         });
491
492         extract_hold_notify();
493
494         if ($scope.org_settings['ui.patron.edit.default_suggested'])
495             $scope.edit_passthru.vis_level = 1;
496
497         if ($scope.patron.isnew) {
498             $scope.generate_password();
499             $scope.hold_notify_phone = true;
500             $scope.hold_notify_email = true;
501
502             if (prs.org_settings['ui.patron.default_ident_type']) {
503                 $scope.patron.ident_type = 
504                     prs.org_settings['ui.patron.default_ident_type'];
505             }
506             if (prs.org_settings['ui.patron.default_inet_access_level']) {
507                 $scope.patron.ident_type = 
508                     prs.org_settings['ui.patron.default_inet_access_level'];
509             }
510             if (prs.org_settings['ui.patron.default_country']) {
511                 $scope.patron.addresses[0].country = 
512                     prs.org_settings['ui.patron.default_country'];
513             }
514         }
515             
516     });
517
518     // returns the tree depth of the selected profile group tree node.
519     $scope.pgt_depth = function(grp) {
520         var d = 0;
521         while (grp = egCore.env.pgt.map[grp.parent()]) d++;
522         return d;
523     }
524
525     // IDL fields used for labels in the UI.
526     $scope.idl_fields = {
527         au  : egCore.idl.classes.au.field_map,
528         ac  : egCore.idl.classes.ac.field_map,
529         aua : egCore.idl.classes.aua.field_map
530     };
531
532     // field visibility cache.  Some fields are universally required.
533     var field_visibility = {
534         'ac.barcode' : 2,
535         'au.usrname' : 2,
536         'au.passwd' :  2,
537         // TODO passwd2 2,
538         'au.first_given_name' : 2,
539         'au.family_name' : 2,
540         'au.ident_type' : 2,
541         'au.home_ou' : 2,
542         'au.profile' : 2,
543         'au.expire_date' : 2,
544         'au.net_access_level' : 2,
545         'aua.address_type' : 2,
546         'aua.post_code' : 2,
547         'aua.street1' : 2,
548         'aua.street2' : 2,
549         'aua.city' : 2,
550         'aua.county' : 2,
551         'aua.state' : 2,
552         'aua.country' : 2,
553         'aua.valid' : 2,
554         'aua.within_city_limits' : 2,
555         'stat_cats' : 1,
556         'surveys' : 1
557     }; 
558
559     // returns true if the selected field should be visible
560     // given the current required/suggested/all setting.
561     $scope.show_field = function(field_key) {
562
563         if (field_visibility[field_key] == undefined) {
564             // compile and cache the visibility for the selected field
565
566             // org settings have not been received yet.
567             if (!$scope.org_settings) return false;
568
569             var req_set = 'ui.patron.edit.' + field_key + '.require';
570             var sho_set = 'ui.patron.edit.' + field_key + '.show';
571             var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
572
573             if ($scope.org_settings[req_set]) {
574                 field_visibility[field_key] = 2;
575             } else if ($scope.org_settings[sho_set]) {
576                 field_visibility[field_key] = 2;
577             } else if ($scope.org_settings[sug_set]) {
578                 field_visibility[field_key] = 1;
579             } else {
580                 field_visibility[field_key] = 0;
581             }
582         }
583
584         return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
585     }
586
587     // generates a random 4-digit password
588     $scope.generate_password = function() {
589         $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
590     }
591
592     $scope.set_expire_date = function() {
593         if (!$scope.patron.profile) return;
594         var seconds = egCore.date.intervalToSeconds(
595             $scope.patron.profile.perm_interval());
596         var now_epoch = new Date().getTime();
597         $scope.patron.expire_date = new Date(
598             now_epoch + (seconds * 1000 /* milliseconds */))
599     }
600
601     // grp is the pgt object
602     $scope.set_profile = function(grp) {
603         $scope.patron.profile = grp;
604         $scope.set_expire_date();
605     }
606
607     $scope.new_address = function() {
608         var addr = egCore.idl.toHash(new egCore.idl.aua());
609         patronRegSvc.ingest_address($scope.patron, addr);
610         addr.id = patronRegSvc.virt_id--;
611         addr.isnew = true;
612         addr.valid = true;
613         addr.within_city_limits = true;
614         $scope.patron.addresses.push(addr);
615     }
616
617     // keep deleted addresses out of the patron object so
618     // they won't appear in the UI.  They'll be re-inserted
619     // when the patron is updated.
620     deleted_addresses = [];
621     $scope.delete_address = function(id) {
622         var addresses = [];
623         angular.forEach($scope.patron.addresses, function(addr) {
624             if (addr.id == id) {
625                 if (id > 0) {
626                     addr.isdeleted = true;
627                     deleted_addresses.push(addr);
628                 }
629             } else {
630                 addresses.push(addr);
631             }
632         });
633         $scope.patron.addresses = addresses;
634     } 
635
636     $scope.post_code_changed = function(addr) { 
637         egCore.net.request(
638             'open-ils.search', 'open-ils.search.zip', addr.post_code)
639         .then(function(resp) {
640             if (!resp) return;
641             if (resp.city) addr.city = resp.city;
642             if (resp.state) addr.state = resp.state;
643             if (resp.county) addr.county = resp.county;
644             if (resp.alert) alert(resp.alert);
645         });
646     }
647
648     $scope.replace_card = function() {
649         $scope.patron.card.active = false;
650         $scope.patron.card.ischanged = true;
651         $scope.disable_bc = false;
652
653         var new_card = egCore.idl.toHash(new egCore.idl.ac());
654         new_card.id = patronRegSvc.virt_id--;
655         new_card.isnew = true;
656         new_card.active = true;
657         new_card._primary = 'on';
658         $scope.patron.card = new_card;
659         $scope.patron.cards.push(new_card);
660     }
661
662     $scope.day_phone_changed = function(phone) {
663         if (phone && $scope.org_settings['patron.password.use_phone']) {
664            $scope.patron.passwd = phone.substr(-4);
665         }
666     }
667
668     $scope.barcode_changed = function(bc) {
669         if (!bc) return;
670         egCore.net.request(
671             'open-ils.actor',
672             'open-ils.actor.barcode.exists',
673             egCore.auth.token(), bc
674         ).then(function(resp) {
675             if (resp == '1') {
676                 console.log('duplicate barcode detected: ' + bc);
677                 // DUPLICATE CARD
678             } else {
679                 if (!$scope.patron.usrname)
680                     $scope.patron.usrname = bc;
681                 // No dupe -- A-OK
682             }
683         });
684     }
685
686     $scope.cards_dialog = function() {
687         $modal.open({
688             templateUrl: './circ/patron/t_patron_cards_dialog',
689             controller: 
690                    ['$scope','$modalInstance','cards',
691             function($scope , $modalInstance , cards) {
692                 // scope here is the modal-level scope
693                 $scope.args = {cards : cards};
694                 $scope.ok = function() { $modalInstance.close($scope.args) }
695                 $scope.cancel = function () { $modalInstance.dismiss() }
696             }],
697             resolve : {
698                 cards : function() {
699                     // scope here is the controller-level scope
700                     return $scope.patron.cards;
701                 }
702             }
703         }).result.then(
704             function(args) {
705                 angular.forEach(args.cards, function(card) {
706                     card.ischanged = true; // assume cards need updating, OK?
707                     if (card._primary == 'on' && 
708                         card.id != $scope.patron.card.id) {
709                         $scope.patron.card = card;
710                     }
711                 });
712             }
713         );
714     }
715
716     $scope.set_addr_type = function(addr, type) {
717         var addrs = $scope.patron.addresses;
718         if (addr['_is_'+type]) {
719             angular.forEach(addrs, function(a) {
720                 if (a.id != addr.id) a['_is_'+type] = false;
721             });
722         } else {
723             // unchecking mailing/billing means we have to randomly
724             // select another address to fill that role.  Select the
725             // first address in the list (that does not match the
726             // modifed address)
727             for (var i = 0; i < addrs.length; i++) {
728                 if (addrs[i].id != addr.id) {
729                     addrs[i]['_is_' + type] = true;
730                     break;
731                 }
732             }
733         }
734     }
735
736
737     // Translate hold notify preferences from the form/scope back into a 
738     // single user setting value for opac.hold_notify.
739     function compress_hold_notify() {
740         var hold_notify = '';
741         var splitter = '';
742         if ($scope.hold_notify_phone) {
743             hold_notify = 'phone';
744             splitter = ':';
745         }
746         if ($scope.hold_notify_email) {
747             hold_notify = splitter + 'email';
748             splitter = ':';
749         }
750         if ($scope.hold_notify_sms) {
751             hold_notify = splitter + 'sms';
752             splitter = ':';
753         }
754         $scope.user_settings['opac.hold_notify'] = hold_notify;
755     }
756
757     function extract_hold_notify() {
758         notify = $scope.user_settings['opac.hold_notify'];
759         if (!notify) return;
760         $scope.hold_notify_phone = Boolean(notify.match(/phone/));
761         $scope.hold_notify_email = Boolean(notify.match(/email/));
762         $scope.hold_notify_sms = Boolean(notify.match(/sms/));
763     }
764
765     $scope.edit_passthru.save = function() {
766
767         // toss the deleted addresses back into the patron's list of
768         // addresses so it's included in the update
769         $scope.patron.addresses = 
770             $scope.patron.addresses.concat(deleted_addresses);
771         
772         compress_hold_notify();
773
774         patronRegSvc.save_user($scope.patron)
775         .then(function(new_user) { 
776             if (new_user && new_user.classname) {
777                 return patronRegSvc.save_user_settings(
778                     new_user, $scope.user_settings); 
779             } else {
780                 alert('Patron update failed. \n\n' + js2JSON(new_user));
781                 return true; // ensure page reloads to reset
782             }
783         }).then(function(keep_going) {
784             // reloading the page means potentially losing some information
785             // (e.g. last patron search), but is the only way to ensure all
786             // components are properly updated to reflect the modified patron.
787             $window.location.href = location.href;
788         });
789     }
790 }
791
792 // This controller may be loaded from different modules (patron edit vs.
793 // register new patron), so we have to inject the controller params manually.
794 PatronRegCtrl.$inject = ['$scope', '$routeParams', '$q', '$modal', 
795     '$window', 'egCore', 'patronSvc', 'patronRegSvc'];
796