2 angular.module('egCoreMod')
3 // toss tihs onto egCoreMod since the page app may vary
5 .factory('patronRegSvc', ['$q', 'egCore', function($q, egCore) {
8 field_doc : {}, // config.idl_field_doc
9 profiles : [], // permission groups
10 no_edit_profiles : [], // perm groups we cannot modify
12 user_settings : {}, // applied user settings
13 user_setting_types : {}, // config.usr_setting_type
14 opt_in_setting_types : {}, // config.usr_setting_type for event-def opt-in
15 survey_questions : {},
17 survey_responses : {}, // survey.responses for loaded patron in progress
18 stat_cat_entry_maps : {}, // cat.id to selected entry object map
19 virt_id : -1 // virtual ID for new objects
22 // launch a series of parallel data retrieval calls
23 service.init = function(scope) {
25 service.get_field_doc(),
26 service.get_perm_groups(),
27 service.get_ident_types(),
28 service.get_user_settings(),
29 service.get_org_settings(),
30 service.get_stat_cats(),
31 service.get_surveys(),
32 service.get_net_access_levels()
36 //service.check_grp_app_perm = function(grp_id) {
38 // determine which user groups our user is not allowed to modify
39 service.set_no_edit_profiles = function() {
40 var all_app_perms = [];
41 var failed_perms = [];
43 // extract the application permissions
44 angular.forEach(service.profiles, function(grp) {
45 if (grp.application_perm())
46 all_app_perms.push(grp.application_perm());
49 // fill in service.no_edit_profiles by inspecting failed_perms
50 function traverse_grp_tree(grp, failed) {
52 failed_perms.indexOf(grp.application_perm()) > -1;
54 if (failed) service.no_edit_profiles.push(grp.id());
57 service.profiles.filter( // children of grp
58 function(p) { return p.parent() == grp.id() }),
59 function(child) {traverse_grp_tree(child, failed)}
63 return egCore.perm.hasPermAt(all_app_perms, true).then(
66 angular.forEach(all_app_perms, function(p) {
67 if (perm_orgs[p].length == 0)
71 traverse_grp_tree(egCore.env.pgt.tree);
72 console.log('User is not allowed to edit profiles: '
73 + service.no_edit_profiles);
78 service.group_link_perms = function() {
81 service.get_surveys = function() {
82 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
84 return egCore.pcrud.search('asv', {
86 start_date : {'<=' : 'now'},
87 end_date : {'>=' : 'now'}
96 ).then(function(surveys) {
97 surveys = surveys.sort(function(a,b) {
98 return a.name() < b.name() ? -1 : 1 });
99 service.surveys = surveys;
100 angular.forEach(surveys, function(survey) {
101 angular.forEach(survey.questions(), function(question) {
102 service.survey_questions[question.id()] = question;
103 angular.forEach(question.answers(), function(answer) {
104 service.survey_answers[answer.id()] = answer;
111 service.get_stat_cats = function() {
112 return egCore.net.request(
114 'open-ils.circ.stat_cat.actor.retrieve.all',
115 egCore.auth.token(), egCore.auth.user().ws_ou()
116 ).then(function(cats) {
117 cats = cats.sort(function(a, b) {
118 return a.name() < b.name() ? -1 : 1});
119 angular.forEach(cats, function(cat) {
121 cat.entries().sort(function(a,b) {
122 return a.value() < b.value() ? -1 : 1
126 service.stat_cats = cats;
130 service.get_org_settings = function() {
131 return egCore.org.settings([
132 'global.password_regex',
133 'global.juvenile_age_threshold',
134 'patron.password.use_phone',
135 'ui.patron.default_inet_access_level',
136 'ui.patron.default_ident_type',
137 'ui.patron.default_country',
138 'ui.patron.registration.require_address',
139 'circ.holds.behind_desk_pickup_supported',
140 'circ.patron_edit.clone.copy_address',
141 'ui.patron.edit.au.prefix.require',
142 'ui.patron.edit.au.prefix.show',
143 'ui.patron.edit.au.prefix.suggest',
144 'ui.patron.edit.ac.barcode.regex',
145 'ui.patron.edit.au.second_given_name.show',
146 'ui.patron.edit.au.second_given_name.suggest',
147 'ui.patron.edit.au.suffix.show',
148 'ui.patron.edit.au.suffix.suggest',
149 'ui.patron.edit.au.alias.show',
150 'ui.patron.edit.au.alias.suggest',
151 'ui.patron.edit.au.dob.require',
152 'ui.patron.edit.au.dob.show',
153 'ui.patron.edit.au.dob.suggest',
154 'ui.patron.edit.au.dob.calendar',
155 'ui.patron.edit.au.juvenile.show',
156 'ui.patron.edit.au.juvenile.suggest',
157 'ui.patron.edit.au.ident_value.show',
158 'ui.patron.edit.au.ident_value.suggest',
159 'ui.patron.edit.au.ident_value2.show',
160 'ui.patron.edit.au.ident_value2.suggest',
161 'ui.patron.edit.au.email.require',
162 'ui.patron.edit.au.email.show',
163 'ui.patron.edit.au.email.suggest',
164 'ui.patron.edit.au.email.regex',
165 'ui.patron.edit.au.email.example',
166 'ui.patron.edit.au.day_phone.require',
167 'ui.patron.edit.au.day_phone.show',
168 'ui.patron.edit.au.day_phone.suggest',
169 'ui.patron.edit.au.day_phone.regex',
170 'ui.patron.edit.au.day_phone.example',
171 'ui.patron.edit.au.evening_phone.require',
172 'ui.patron.edit.au.evening_phone.show',
173 'ui.patron.edit.au.evening_phone.suggest',
174 'ui.patron.edit.au.evening_phone.regex',
175 'ui.patron.edit.au.evening_phone.example',
176 'ui.patron.edit.au.other_phone.require',
177 'ui.patron.edit.au.other_phone.show',
178 'ui.patron.edit.au.other_phone.suggest',
179 'ui.patron.edit.au.other_phone.regex',
180 'ui.patron.edit.au.other_phone.example',
181 'ui.patron.edit.phone.regex',
182 'ui.patron.edit.phone.example',
183 'ui.patron.edit.au.active.show',
184 'ui.patron.edit.au.active.suggest',
185 'ui.patron.edit.au.barred.show',
186 'ui.patron.edit.au.barred.suggest',
187 'ui.patron.edit.au.master_account.show',
188 'ui.patron.edit.au.master_account.suggest',
189 'ui.patron.edit.au.claims_returned_count.show',
190 'ui.patron.edit.au.claims_returned_count.suggest',
191 'ui.patron.edit.au.claims_never_checked_out_count.show',
192 'ui.patron.edit.au.claims_never_checked_out_count.suggest',
193 'ui.patron.edit.au.alert_message.show',
194 'ui.patron.edit.au.alert_message.suggest',
195 'ui.patron.edit.aua.post_code.regex',
196 'ui.patron.edit.aua.post_code.example',
197 'ui.patron.edit.aua.county.require',
199 'ui.patron.edit.default_suggested',
200 'opac.barcode_regex',
201 'opac.username_regex',
203 'ui.patron.edit.aua.state.require',
204 'ui.patron.edit.aua.state.suggest',
205 'ui.patron.edit.aua.state.show'
206 ]).then(function(settings) {
207 service.org_settings = settings;
208 return service.process_org_settings(settings);
212 // some org settings require the retrieval of additional data
213 service.process_org_settings = function(settings) {
217 if (settings['sms.enable']) {
218 // fetch SMS carriers
220 egCore.pcrud.search('csc',
223 {'class':'csc', 'field':'name'},
224 {'class':'csc', 'field':'region'}
226 ).then(function(carriers) {
227 service.sms_carriers = carriers;
231 // if other promises are added below, this is not necessary.
232 promises.push($q.when());
235 // other post-org-settings processing goes here,
236 // adding to promises as needed.
238 return $q.all(promises);
241 service.get_ident_types = function() {
242 if (egCore.env.cit) {
243 service.ident_types = egCore.env.cit.list;
246 return egCore.pcrud.retrieveAll('cit', {}, {atomic : true})
247 .then(function(types) {
248 egCore.env.absorbList(types, 'cit')
249 service.ident_types = types
254 service.get_net_access_levels = function() {
255 if (egCore.env.cnal) {
256 service.net_access_levels = egCore.env.cnal.list;
259 return egCore.pcrud.retrieveAll('cnal', {}, {atomic : true})
260 .then(function(levels) {
261 egCore.env.absorbList(levels, 'cnal')
262 service.net_access_levels = levels
267 service.get_perm_groups = function() {
268 if (egCore.env.pgt) {
269 service.profiles = egCore.env.pgt.list;
270 return service.set_no_edit_profiles();
272 return egCore.pcrud.search('pgt', {parent : null},
273 {flesh : -1, flesh_fields : {pgt : ['children']}}
276 egCore.env.absorbTree(tree, 'pgt')
277 service.profiles = egCore.env.pgt.list;
278 return service.set_no_edit_profiles();
284 service.get_field_doc = function() {
286 return egCore.pcrud.search('fdoc', {
287 fm_class: ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva']})
288 .then(null, null, function(doc) {
289 if (!service.field_doc[doc.fm_class()]) {
290 service.field_doc[doc.fm_class()] = {};
292 service.field_doc[doc.fm_class()][doc.field()] = doc;
296 service.get_user_settings = function() {
297 var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
300 'circ.holds_behind_desk',
301 'circ.collections.exempt',
303 'opac.default_phone',
304 'opac.default_pickup_location',
305 'opac.default_sms_carrier',
306 'opac.default_sms_notify'];
308 return egCore.pcrud.search('cust', {
310 {name : static_types}, // common user settings
311 {name : { // opt-in notification user settings
313 select : {atevdef : ['opt_in_setting']},
315 // we only care about opt-in settings for
316 // event_defs our users encounter
317 where : {'+atevdef' : {owner : org_ids}}
321 }, {}, {atomic : true}).then(function(setting_types) {
323 angular.forEach(setting_types, function(stype) {
324 service.user_setting_types[stype.name()] = stype;
325 if (static_types.indexOf(stype.name()) == -1) {
326 service.opt_in_setting_types[stype.name()] = stype;
330 if (service.patron_id) {
331 // retrieve applied values for the current user
332 // for the setting types we care about.
335 setting_types.map(function(obj) { return obj.name() });
337 return egCore.net.request(
339 'open-ils.actor.patron.settings.retrieve.authoritative',
343 ).then(function(settings) {
344 service.user_settings = settings;
348 // apply default user setting values
349 angular.forEach(setting_types, function(stype, index) {
350 if (stype.reg_default() != undefined) {
351 service.user_settings[setting.name()] =
352 setting.reg_default();
359 service.init_patron = function(current) {
362 return service.init_new_patron();
364 service.patron = current;
365 return service.init_existing_patron(current)
368 service.ingest_address = function(patron, addr) {
369 addr.valid = addr.valid == 't';
370 addr.within_city_limits = addr.within_city_limits == 't';
371 addr._is_mailing = (patron.mailing_address &&
372 addr.id == patron.mailing_address.id);
373 addr._is_billing = (patron.billing_address &&
374 addr.id == patron.billing_address.id);
378 * Existing patron objects reqire some data munging before insertion
381 * 1. Turn everything into a hash
382 * 2. ... Except certain fields (selectors) whose widgets require objects
383 * 3. Bools must be Boolean, not t/f.
385 service.init_existing_patron = function(current) {
387 var patron = egCore.idl.toHash(current);
389 patron.home_ou = egCore.org.get(patron.home_ou.id);
390 patron.expire_date = new Date(Date.parse(patron.expire_date));
391 patron.dob = new Date(Date.parse(patron.dob));
392 patron.profile = current.profile(); // pre-hash version
393 patron.net_access_level = current.net_access_level();
394 patron.ident_type = current.ident_type();
395 patron.groups = current.groups(); // pre-hash
398 ['juvenile', 'barred', 'active', 'master_account'],
399 function(field) { patron[field] = patron[field] == 't'; }
402 angular.forEach(patron.cards, function(card) {
403 card.active = card.active == 't';
404 if (card.id == patron.card.id) {
406 card._primary = 'on';
410 angular.forEach(patron.addresses,
411 function(addr) { service.ingest_address(patron, addr) });
413 // toss entries for existing stat cat maps into our living
414 // stat cat entry map, which is modified within the template.
415 angular.forEach(patron.stat_cat_entries, function(map) {
417 angular.forEach(service.stat_cats, function(cat) {
418 angular.forEach(cat.entries(), function(ent) {
419 if (ent.id() == map.stat_cat_entry)
423 service.stat_cat_entry_maps[map.stat_cat.id] = entry;
429 service.init_new_patron = function() {
431 id : service.virt_id--,
434 address_type : egCore.strings.REG_ADDR_TYPE,
437 within_city_limits : false,
438 stat_cat_entries : []
442 id : service.virt_id--,
453 home_ou : egCore.org.get(egCore.auth.user().ws_ou()),
455 // TODO default profile group?
460 // translate the patron back into IDL form
461 service.save_user = function(phash) {
463 var patron = egCore.idl.fromHash('au', phash);
465 patron.home_ou(patron.home_ou().id());
467 patron.expire_date().toISOString().replace(/T.*/,''));
468 patron.profile(patron.profile().id());
470 patron.dob(patron.dob().toISOString().replace(/T.*/,''));
471 if (patron.ident_type())
472 patron.ident_type(patron.ident_type().id());
473 if (patron.net_access_level())
474 patron.net_access_level(patron.net_access_level().id());
477 ['juvenile', 'barred', 'active', 'master_account'],
478 function(field) { patron[field](phash[field] ? 't' : 'f'); }
481 var card_hashes = patron.cards();
483 angular.forEach(card_hashes, function(chash) {
484 var card = egCore.idl.fromHash('ac', chash)
485 card.usr(patron.id());
486 card.active(chash.active ? 't' : 'f');
487 patron.cards().push(card);
488 if (chash._primary) {
493 var addr_hashes = patron.addresses();
494 patron.addresses([]);
495 angular.forEach(addr_hashes, function(addr_hash) {
496 if (!addr_hash.isnew && !addr_hash.isdeleted)
497 addr_hash.ischanged = true;
498 var addr = egCore.idl.fromHash('aua', addr_hash);
499 patron.addresses().push(addr);
500 addr.valid(addr.valid() ? 't' : 'f');
501 addr.within_city_limits(addr.within_city_limits() ? 't' : 'f');
502 if (addr_hash._is_mailing) patron.mailing_address(addr);
503 if (addr_hash._is_billing) patron.billing_address(addr);
506 patron.survey_responses([]);
507 angular.forEach(service.survey_responses, function(answer) {
508 var question = service.survey_questions[answer.question()];
509 var resp = new egCore.idl.asvr();
511 resp.survey(question.survey());
512 resp.question(question.id());
513 resp.answer(answer.id());
514 resp.usr(patron.id());
515 resp.answer_date('now');
516 patron.survey_responses().push(resp);
519 // re-object-ify the patron stat cat entry maps
521 angular.forEach(patron.stat_cat_entries(), function(entry) {
522 var e = egCore.idl.fromHash('actscecm', entry);
523 e.stat_cat(e.stat_cat().id);
526 patron.stat_cat_entries(maps);
528 // service.stat_cat_entry_maps maps stats to entries
529 // patron.stat_cat_entries is an array of stat_cat_entry_usr_map's
530 angular.forEach(service.stat_cat_entry_maps, function(entry) {
532 // see if we already have a mapping for this entry
533 var existing = patron.stat_cat_entries().filter(function(e) {
534 return e.stat_cat() == entry.stat_cat();
537 if (existing) { // we have a mapping
538 // if the existing mapping matches the new one,
539 // there' nothing left to do
540 if (existing.stat_cat_entry() == entry.id()) return;
542 // mappings differ. delete the old one and create
544 existing.isdeleted(true);
547 var newmap = new egCore.idl.actscecm();
548 newmap.target_usr(patron.id());
550 newmap.stat_cat(entry.stat_cat());
551 newmap.stat_cat_entry(entry.id());
552 patron.stat_cat_entries().push(newmap);
555 angular.forEach(patron.stat_cat_entries(), function(entry) {
556 console.log(egCore.idl.toString(entry));
559 if (!patron.isnew()) patron.ischanged(true);
561 return egCore.net.request(
563 'open-ils.actor.patron.update',
564 egCore.auth.token(), patron);
567 service.save_user_settings = function(new_user, user_settings) {
568 // user_settings contains the values from the scope/form.
569 // service.user_settings contain the values from page load time.
572 if (service.patron_id) {
573 // only update modified settings for existing patrons
574 angular.forEach(user_settings, function(val, key) {
575 if (val !== service.user_settings[key])
580 // all non-null setting values are updated for new patrons
581 angular.forEach(user_settings, function(val, key) {
582 if (val !== null) settings[key] = val;
586 if (Object.keys(settings).length == 0) return $q.when();
588 return egCore.net.request(
590 'open-ils.actor.patron.settings.update',
591 egCore.auth.token(), new_user.id(), settings
592 ).then(function(resp) {
593 console.log('settings returned ' + resp);
602 function PatronRegCtrl($scope, $routeParams,
603 $q, $modal, $window, egCore, patronSvc, patronRegSvc) {
605 $scope.page_data_loaded = false;
606 $scope.clone_id = $routeParams.clone_id;
607 $scope.stage_username = $routeParams.stage_username;
609 patronRegSvc.patron_id = $routeParams.edit_id || $routeParams.id;
611 // for existing patrons, disable barcode input by default
612 $scope.disable_bc = $scope.focus_usrname = Boolean($scope.patron_id);
613 $scope.focus_bc = !Boolean($scope.patron_id);
615 if (!$scope.edit_passthru) {
616 // in edit more, scope.edit_passthru is delivered to us by
617 // the enclosing controller. In register mode, there is
618 // no enclosing controller, so we create our own.
619 $scope.edit_passthru = {};
622 // 0=all, 1=suggested, 2=all
623 $scope.edit_passthru.vis_level = 0;
624 // TODO: add save/clone handlers here
626 // Apply default values for new patrons during initial registration
627 // prs is shorthand for patronSvc
628 function set_new_patron_defaults(prs) {
629 $scope.generate_password();
630 $scope.hold_notify_phone = true;
631 $scope.hold_notify_email = true;
633 if (prs.org_settings['ui.patron.default_ident_type']) {
634 // $scope.patron needs this field to be an object
635 var id = prs.org_settings['ui.patron.default_ident_type'];
636 var ident_type = $scope.ident_types.filter(
637 function(type) { return type.id() == id })[0];
638 $scope.patron.ident_type = ident_type;
640 if (prs.org_settings['ui.patron.default_inet_access_level']) {
641 // $scope.patron needs this field to be an object
642 var id = prs.org_settings['ui.patron.default_inet_access_level'];
643 var level = $scope.net_access_levels.filter(
644 function(lvl) { return lvl.id() == id })[0];
645 $scope.patron.net_access_level = level;
647 if (prs.org_settings['ui.patron.default_country']) {
648 $scope.patron.addresses[0].country =
649 prs.org_settings['ui.patron.default_country'];
655 $scope.initTab ? // initTab comes from patron app
656 $scope.initTab('edit', $routeParams.id) : $q.when(),
661 // called after initTab and patronRegSvc.init have completed
663 var prs = patronRegSvc; // brevity
664 // in standalone mode, we have no patronSvc
665 $scope.patron = prs.init_patron(patronSvc ? patronSvc.current : null);
666 $scope.field_doc = prs.field_doc;
667 $scope.profiles = prs.profiles;
668 $scope.ident_types = prs.ident_types;
669 $scope.net_access_levels = prs.net_access_levels;
670 $scope.user_setting_types = prs.user_setting_types;
671 $scope.opt_in_setting_types = prs.opt_in_setting_types;
672 $scope.org_settings = prs.org_settings;
673 $scope.sms_carriers = prs.sms_carriers;
674 $scope.stat_cats = prs.stat_cats;
675 $scope.surveys = prs.surveys;
676 $scope.survey_responses = prs.survey_responses;
677 $scope.stat_cat_entry_maps = prs.stat_cat_entry_maps;
679 $scope.user_settings = prs.user_settings;
680 // clone the user settings back into the patronRegSvc so
681 // we have a copy of the original state of the settings.
682 prs.user_settings = {};
683 angular.forEach($scope.user_settings, function(val, key) {
684 prs.user_settings[key] = val;
687 extract_hold_notify();
689 if ($scope.org_settings['ui.patron.edit.default_suggested'])
690 $scope.edit_passthru.vis_level = 1;
692 if ($scope.patron.isnew)
693 set_new_patron_defaults(prs);
695 $scope.page_data_loaded = true;
698 // update the currently displayed field documentation
699 $scope.set_selected_field_doc = function(cls, field) {
700 $scope.selected_field_doc = $scope.field_doc[cls][field];
703 // returns the tree depth of the selected profile group tree node.
704 $scope.pgt_depth = function(grp) {
706 while (grp = egCore.env.pgt.map[grp.parent()]) d++;
710 // IDL fields used for labels in the UI.
711 $scope.idl_fields = {
712 au : egCore.idl.classes.au.field_map,
713 ac : egCore.idl.classes.ac.field_map,
714 aua : egCore.idl.classes.aua.field_map
717 // field visibility cache. Some fields are universally required.
718 var field_visibility = {
723 'au.first_given_name' : 2,
724 'au.family_name' : 2,
728 'au.expire_date' : 2,
729 'au.net_access_level' : 2,
730 'aua.address_type' : 2,
739 'aua.within_city_limits' : 2,
744 // returns true if the selected field should be visible
745 // given the current required/suggested/all setting.
746 $scope.show_field = function(field_key) {
748 if (field_visibility[field_key] == undefined) {
749 // compile and cache the visibility for the selected field
751 // org settings have not been received yet.
752 if (!$scope.org_settings) return false;
754 var req_set = 'ui.patron.edit.' + field_key + '.require';
755 var sho_set = 'ui.patron.edit.' + field_key + '.show';
756 var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
758 if ($scope.org_settings[req_set]) {
759 field_visibility[field_key] = 2;
760 } else if ($scope.org_settings[sho_set]) {
761 field_visibility[field_key] = 2;
762 } else if ($scope.org_settings[sug_set]) {
763 field_visibility[field_key] = 1;
765 field_visibility[field_key] = 0;
769 return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
772 // generates a random 4-digit password
773 $scope.generate_password = function() {
774 $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
777 $scope.set_expire_date = function() {
778 if (!$scope.patron.profile) return;
779 var seconds = egCore.date.intervalToSeconds(
780 $scope.patron.profile.perm_interval());
781 var now_epoch = new Date().getTime();
782 $scope.patron.expire_date = new Date(
783 now_epoch + (seconds * 1000 /* milliseconds */))
786 // grp is the pgt object
787 $scope.set_profile = function(grp) {
788 $scope.patron.profile = grp;
789 $scope.set_expire_date();
792 $scope.new_address = function() {
793 var addr = egCore.idl.toHash(new egCore.idl.aua());
794 patronRegSvc.ingest_address($scope.patron, addr);
795 addr.id = patronRegSvc.virt_id--;
798 addr.within_city_limits = true;
799 $scope.patron.addresses.push(addr);
802 // keep deleted addresses out of the patron object so
803 // they won't appear in the UI. They'll be re-inserted
804 // when the patron is updated.
805 deleted_addresses = [];
806 $scope.delete_address = function(id) {
808 angular.forEach($scope.patron.addresses, function(addr) {
811 addr.isdeleted = true;
812 deleted_addresses.push(addr);
815 addresses.push(addr);
818 $scope.patron.addresses = addresses;
821 $scope.post_code_changed = function(addr) {
823 'open-ils.search', 'open-ils.search.zip', addr.post_code)
824 .then(function(resp) {
826 if (resp.city) addr.city = resp.city;
827 if (resp.state) addr.state = resp.state;
828 if (resp.county) addr.county = resp.county;
829 if (resp.alert) alert(resp.alert);
833 $scope.replace_card = function() {
834 $scope.patron.card.active = false;
835 $scope.patron.card.ischanged = true;
836 $scope.disable_bc = false;
838 var new_card = egCore.idl.toHash(new egCore.idl.ac());
839 new_card.id = patronRegSvc.virt_id--;
840 new_card.isnew = true;
841 new_card.active = true;
842 new_card._primary = 'on';
843 $scope.patron.card = new_card;
844 $scope.patron.cards.push(new_card);
847 $scope.day_phone_changed = function(phone) {
848 if (phone && $scope.org_settings['patron.password.use_phone']) {
849 $scope.patron.passwd = phone.substr(-4);
853 $scope.barcode_changed = function(bc) {
857 'open-ils.actor.barcode.exists',
858 egCore.auth.token(), bc
859 ).then(function(resp) {
861 console.log('duplicate barcode detected: ' + bc);
864 if (!$scope.patron.usrname)
865 $scope.patron.usrname = bc;
871 $scope.cards_dialog = function() {
873 templateUrl: './circ/patron/t_patron_cards_dialog',
875 ['$scope','$modalInstance','cards',
876 function($scope , $modalInstance , cards) {
877 // scope here is the modal-level scope
878 $scope.args = {cards : cards};
879 $scope.ok = function() { $modalInstance.close($scope.args) }
880 $scope.cancel = function () { $modalInstance.dismiss() }
884 // scope here is the controller-level scope
885 return $scope.patron.cards;
890 angular.forEach(args.cards, function(card) {
891 card.ischanged = true; // assume cards need updating, OK?
892 if (card._primary == 'on' &&
893 card.id != $scope.patron.card.id) {
894 $scope.patron.card = card;
901 $scope.set_addr_type = function(addr, type) {
902 var addrs = $scope.patron.addresses;
903 if (addr['_is_'+type]) {
904 angular.forEach(addrs, function(a) {
905 if (a.id != addr.id) a['_is_'+type] = false;
908 // unchecking mailing/billing means we have to randomly
909 // select another address to fill that role. Select the
910 // first address in the list (that does not match the
912 for (var i = 0; i < addrs.length; i++) {
913 if (addrs[i].id != addr.id) {
914 addrs[i]['_is_' + type] = true;
922 // Translate hold notify preferences from the form/scope back into a
923 // single user setting value for opac.hold_notify.
924 function compress_hold_notify() {
925 var hold_notify = '';
927 if ($scope.hold_notify_phone) {
928 hold_notify = 'phone';
931 if ($scope.hold_notify_email) {
932 hold_notify = splitter + 'email';
935 if ($scope.hold_notify_sms) {
936 hold_notify = splitter + 'sms';
939 $scope.user_settings['opac.hold_notify'] = hold_notify;
942 $scope.secondary_groups_dialog = function() {
944 templateUrl: './circ/patron/t_patron_groups_dialog',
946 ['$scope','$modalInstance','linked_groups',
947 function($scope , $modalInstance , linked_groups) {
948 // scope here is the modal-level scope
949 $scope.link_group = function(grp) {
950 $scope.args.linked_groups.push(grp);
951 $event.preventDefault(); // avoid close
953 $scope.unlink_group = function($event, grp) {
954 $scope.args.linked_groups =
955 $scope.args.linked_groups.filter(function(g) {
956 return g.id() != grp.id()
958 $event.preventDefault(); // avoid close
960 $scope.args = {linked_groups : linked_groups};
961 $scope.ok = function() { $modalInstance.close($scope.args) }
962 $scope.cancel = function () { $modalInstance.dismiss() }
965 linked_groups : function() {
966 // scope here is the controller-level scope
967 return $scope.patron.groups;
972 angular.forEach(args.linked_groups, function(grp) {
973 // TODO add/remove linked groups
979 function extract_hold_notify() {
980 notify = $scope.user_settings['opac.hold_notify'];
982 $scope.hold_notify_phone = Boolean(notify.match(/phone/));
983 $scope.hold_notify_email = Boolean(notify.match(/email/));
984 $scope.hold_notify_sms = Boolean(notify.match(/sms/));
987 $scope.edit_passthru.save = function() {
989 // toss the deleted addresses back into the patron's list of
990 // addresses so it's included in the update
991 $scope.patron.addresses =
992 $scope.patron.addresses.concat(deleted_addresses);
994 compress_hold_notify();
996 patronRegSvc.save_user($scope.patron)
997 .then(function(new_user) {
998 if (new_user && new_user.classname) {
999 return patronRegSvc.save_user_settings(
1000 new_user, $scope.user_settings);
1002 alert('Patron update failed. \n\n' + js2JSON(new_user));
1003 return true; // ensure page reloads to reset
1005 }).then(function(keep_going) {
1006 // reloading the page means potentially losing some information
1007 // (e.g. last patron search), but is the only way to ensure all
1008 // components are properly updated to reflect the modified patron.
1009 $window.location.href = location.href;
1014 // This controller may be loaded from different modules (patron edit vs.
1015 // register new patron), so we have to inject the controller params manually.
1016 PatronRegCtrl.$inject = ['$scope', '$routeParams', '$q', '$modal',
1017 '$window', 'egCore', 'patronSvc', 'patronRegSvc'];