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 edit_profiles : [], // perm groups we can 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
16 survey_questions : {},
18 survey_responses : {}, // survey.responses for loaded patron in progress
20 stat_cat_entry_maps : {}, // cat.id to selected value
21 virt_id : -1, // virtual ID for new objects
22 init_done : false // have we loaded our initialization data?
25 // launch a series of parallel data retrieval calls
26 service.init = function(scope) {
28 // Data loaded here only needs to be retrieved the first time this
29 // tab becomes active within the current instance of the patron app.
30 // In other words, navigating between patron tabs will not cause
31 // all of this data to be reloaded. Navigating to a separate app
32 // and returning will cause the data to be reloaded.
33 if (service.init_done) return $q.when();
34 service.init_done = true;
37 service.get_field_doc(),
38 service.get_perm_groups(),
39 service.get_ident_types(),
40 service.get_user_settings(),
41 service.get_org_settings(),
42 service.get_stat_cats(),
43 service.get_surveys(),
44 service.get_clone_user(),
45 service.get_stage_user(),
46 service.get_net_access_levels()
50 service.get_clone_user = function() {
51 if (!service.clone_id) return $q.when();
52 // we could load egUser and use its get() function, but loading
53 // user.js into the standalone register UI would mean creating a
54 // new module, since egUser is not loaded into egCoreMod. This
56 return egCore.net.request(
58 'open-ils.actor.user.fleshed.retrieve',
59 egCore.auth.token(), service.clone_id,
60 ['billing_address', 'mailing_address'])
61 .then(function(cuser) {
62 if (e = egCore.evt.parse(cuser)) {
65 service.clone_user = cuser;
70 // When editing a user with addresses linked to other users, fetch
71 // the linked user(s) so we can display their names and edit links.
72 service.get_linked_addr_users = function(addrs) {
73 angular.forEach(addrs, function(addr) {
74 if (addr.usr == service.existing_patron.id()) return;
75 egCore.pcrud.retrieve('au', addr.usr)
77 addr._linked_owner_id = usr.id();
78 addr._linked_owner = service.format_name(
80 usr.first_given_name(),
81 usr.second_given_name()
87 service.apply_secondary_groups = function(user_id, group_ids) {
88 return egCore.net.request(
90 'open-ils.actor.user.set_groups',
91 egCore.auth.token(), user_id, group_ids)
92 .then(function(resp) {
96 // debugging -- should be no events
97 alert('linked groups failure ' + egCore.evt.parse(resp));
102 service.get_stage_user = function() {
103 if (!service.stage_username) return $q.when();
105 // fetch the staged user object
106 return egCore.net.request(
108 'open-ils.actor.user.stage.retrieve.by_username',
110 service.stage_username
111 ).then(function(suser) {
112 if (e = egCore.evt.parse(suser)) {
115 service.stage_user = suser;
119 if (!service.stage_user) return;
120 var requestor = service.stage_user.user.requesting_usr();
122 if (!requestor) return;
124 // fetch the requesting user
125 return egCore.net.request(
127 'open-ils.actor.user.retrieve.parts',
130 ['family_name', 'first_given_name', 'second_given_name']
131 ).then(function(parts) {
132 service.stage_user_requestor =
133 service.format_name(parts[0], parts[1], parts[2]);
138 // See note above about not loading egUser.
140 service.format_name = function(last, first, middle) {
141 return last + ', ' + first + (middle ? ' ' + middle : '');
144 service.check_dupe_username = function(usrname) {
146 // empty usernames can't be dupes
147 if (!usrname) return $q.when(false);
149 // avoid dupe check if username matches the originally loaded usrname
150 if (service.existing_patron) {
151 if (usrname == service.existing_patron.usrname())
152 return $q.when(false);
155 return egCore.net.request(
157 'open-ils.actor.username.exists',
158 egCore.auth.token(), usrname);
161 //service.check_grp_app_perm = function(grp_id) {
163 // determine which user groups our user is not allowed to modify
164 service.set_edit_profiles = function() {
165 var all_app_perms = [];
166 var failed_perms = [];
168 // extract the application permissions
169 angular.forEach(service.profiles, function(grp) {
170 if (grp.application_perm())
171 all_app_perms.push(grp.application_perm());
174 // fill in service.edit_profiles by inspecting failed_perms
175 function traverse_grp_tree(grp, failed) {
177 failed_perms.indexOf(grp.application_perm()) > -1;
179 if (!failed) service.edit_profiles.push(grp);
182 service.profiles.filter( // children of grp
183 function(p) { return p.parent() == grp.id() }),
184 function(child) {traverse_grp_tree(child, failed)}
188 return egCore.perm.hasPermAt(all_app_perms, true).then(
189 function(perm_orgs) {
190 angular.forEach(all_app_perms, function(p) {
191 if (perm_orgs[p].length == 0)
192 failed_perms.push(p);
195 traverse_grp_tree(egCore.env.pgt.tree);
200 // resolves to a hash of perm-name => boolean value indicating
201 // wether the user has the permission at org_id.
202 service.has_perms_for_org = function(org_id) {
207 'CREATE_USER_GROUP_LINK',
208 'UPDATE_PATRON_COLLECTIONS_EXEMPT',
209 'UPDATE_PATRON_CLAIM_RETURN_COUNT',
210 'UPDATE_PATRON_CLAIM_NEVER_CHECKED_OUT_COUNT',
211 'UPDATE_PATRON_ACTIVE_CARD',
212 'UPDATE_PATRON_PRIMARY_CARD'
215 return egCore.perm.hasPermAt(perms_needed, true)
216 .then(function(perm_map) {
218 angular.forEach(perms_needed, function(perm) {
220 Boolean(perm_map[perm].indexOf(org_id) > -1);
227 service.get_surveys = function() {
228 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
230 return egCore.pcrud.search('asv', {
232 start_date : {'<=' : 'now'},
233 end_date : {'>=' : 'now'}
242 ).then(function(surveys) {
243 surveys = surveys.sort(function(a,b) {
244 return a.name() < b.name() ? -1 : 1 });
245 service.surveys = surveys;
246 angular.forEach(surveys, function(survey) {
247 angular.forEach(survey.questions(), function(question) {
248 service.survey_questions[question.id()] = question;
249 angular.forEach(question.answers(), function(answer) {
250 service.survey_answers[answer.id()] = answer;
257 service.get_stat_cats = function() {
258 return egCore.net.request(
260 'open-ils.circ.stat_cat.actor.retrieve.all',
261 egCore.auth.token(), egCore.auth.user().ws_ou()
262 ).then(function(cats) {
263 cats = cats.sort(function(a, b) {
264 return a.name() < b.name() ? -1 : 1});
265 angular.forEach(cats, function(cat) {
267 cat.entries().sort(function(a,b) {
268 return a.value() < b.value() ? -1 : 1
272 service.stat_cats = cats;
276 service.get_org_settings = function() {
277 return egCore.org.settings([
278 'global.password_regex',
279 'global.juvenile_age_threshold',
280 'patron.password.use_phone',
281 'ui.patron.default_inet_access_level',
282 'ui.patron.default_ident_type',
283 'ui.patron.default_country',
284 'ui.patron.registration.require_address',
285 'circ.holds.behind_desk_pickup_supported',
286 'circ.patron_edit.clone.copy_address',
287 'ui.patron.edit.au.prefix.require',
288 'ui.patron.edit.au.prefix.show',
289 'ui.patron.edit.au.prefix.suggest',
290 'ui.patron.edit.ac.barcode.regex',
291 'ui.patron.edit.au.second_given_name.show',
292 'ui.patron.edit.au.second_given_name.suggest',
293 'ui.patron.edit.au.suffix.show',
294 'ui.patron.edit.au.suffix.suggest',
295 'ui.patron.edit.au.alias.show',
296 'ui.patron.edit.au.alias.suggest',
297 'ui.patron.edit.au.dob.require',
298 'ui.patron.edit.au.dob.show',
299 'ui.patron.edit.au.dob.suggest',
300 'ui.patron.edit.au.dob.calendar',
301 'ui.patron.edit.au.juvenile.show',
302 'ui.patron.edit.au.juvenile.suggest',
303 'ui.patron.edit.au.ident_value.show',
304 'ui.patron.edit.au.ident_value.suggest',
305 'ui.patron.edit.au.ident_value2.show',
306 'ui.patron.edit.au.ident_value2.suggest',
307 'ui.patron.edit.au.email.require',
308 'ui.patron.edit.au.email.show',
309 'ui.patron.edit.au.email.suggest',
310 'ui.patron.edit.au.email.regex',
311 'ui.patron.edit.au.email.example',
312 'ui.patron.edit.au.day_phone.require',
313 'ui.patron.edit.au.day_phone.show',
314 'ui.patron.edit.au.day_phone.suggest',
315 'ui.patron.edit.au.day_phone.regex',
316 'ui.patron.edit.au.day_phone.example',
317 'ui.patron.edit.au.evening_phone.require',
318 'ui.patron.edit.au.evening_phone.show',
319 'ui.patron.edit.au.evening_phone.suggest',
320 'ui.patron.edit.au.evening_phone.regex',
321 'ui.patron.edit.au.evening_phone.example',
322 'ui.patron.edit.au.other_phone.require',
323 'ui.patron.edit.au.other_phone.show',
324 'ui.patron.edit.au.other_phone.suggest',
325 'ui.patron.edit.au.other_phone.regex',
326 'ui.patron.edit.au.other_phone.example',
327 'ui.patron.edit.phone.regex',
328 'ui.patron.edit.phone.example',
329 'ui.patron.edit.au.active.show',
330 'ui.patron.edit.au.active.suggest',
331 'ui.patron.edit.au.barred.show',
332 'ui.patron.edit.au.barred.suggest',
333 'ui.patron.edit.au.master_account.show',
334 'ui.patron.edit.au.master_account.suggest',
335 'ui.patron.edit.au.claims_returned_count.show',
336 'ui.patron.edit.au.claims_returned_count.suggest',
337 'ui.patron.edit.au.claims_never_checked_out_count.show',
338 'ui.patron.edit.au.claims_never_checked_out_count.suggest',
339 'ui.patron.edit.au.alert_message.show',
340 'ui.patron.edit.au.alert_message.suggest',
341 'ui.patron.edit.aua.post_code.regex',
342 'ui.patron.edit.aua.post_code.example',
343 'ui.patron.edit.aua.county.require',
345 'ui.patron.edit.default_suggested',
346 'opac.barcode_regex',
347 'opac.username_regex',
349 'ui.patron.edit.aua.state.require',
350 'ui.patron.edit.aua.state.suggest',
351 'ui.patron.edit.aua.state.show',
352 'ui.admin.work_log.max_entries',
353 'ui.admin.patron_log.max_entries'
354 ]).then(function(settings) {
355 service.org_settings = settings;
356 if (egCore && egCore.env && !egCore.env.aous) {
357 egCore.env.aous = settings;
358 console.log('setting egCore.env.aous');
360 return service.process_org_settings(settings);
364 // some org settings require the retrieval of additional data
365 service.process_org_settings = function(settings) {
369 if (settings['sms.enable']) {
370 // fetch SMS carriers
372 egCore.pcrud.search('csc',
375 {'class':'csc', 'field':'name'},
376 {'class':'csc', 'field':'region'}
378 ).then(function(carriers) {
379 service.sms_carriers = carriers;
383 // if other promises are added below, this is not necessary.
384 promises.push($q.when());
387 // other post-org-settings processing goes here,
388 // adding to promises as needed.
390 return $q.all(promises);
393 service.get_ident_types = function() {
394 if (egCore.env.cit) {
395 service.ident_types = egCore.env.cit.list;
398 return egCore.pcrud.retrieveAll('cit', {}, {atomic : true})
399 .then(function(types) {
400 egCore.env.absorbList(types, 'cit')
401 service.ident_types = types
406 service.get_net_access_levels = function() {
407 if (egCore.env.cnal) {
408 service.net_access_levels = egCore.env.cnal.list;
411 return egCore.pcrud.retrieveAll('cnal', {}, {atomic : true})
412 .then(function(levels) {
413 egCore.env.absorbList(levels, 'cnal')
414 service.net_access_levels = levels
419 service.get_perm_groups = function() {
420 if (egCore.env.pgt) {
421 service.profiles = egCore.env.pgt.list;
422 return service.set_edit_profiles();
424 return egCore.pcrud.search('pgt', {parent : null},
425 {flesh : -1, flesh_fields : {pgt : ['children']}}
428 egCore.env.absorbTree(tree, 'pgt')
429 service.profiles = egCore.env.pgt.list;
430 return service.set_edit_profiles();
436 service.get_field_doc = function() {
437 return egCore.pcrud.search('fdoc', {
438 fm_class: ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva']})
439 .then(null, null, function(doc) {
440 if (!service.field_doc[doc.fm_class()]) {
441 service.field_doc[doc.fm_class()] = {};
443 service.field_doc[doc.fm_class()][doc.field()] = doc;
447 service.get_user_settings = function() {
448 var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
451 'circ.holds_behind_desk',
452 'circ.collections.exempt',
454 'opac.default_phone',
455 'opac.default_pickup_location',
456 'opac.default_sms_carrier',
457 'opac.default_sms_notify'];
459 return egCore.pcrud.search('cust', {
461 {name : static_types}, // common user settings
462 {name : { // opt-in notification user settings
464 select : {atevdef : ['opt_in_setting']},
466 // we only care about opt-in settings for
467 // event_defs our users encounter
468 where : {'+atevdef' : {owner : org_ids}}
472 }, {}, {atomic : true}).then(function(setting_types) {
474 angular.forEach(setting_types, function(stype) {
475 service.user_setting_types[stype.name()] = stype;
476 if (static_types.indexOf(stype.name()) == -1) {
477 service.opt_in_setting_types[stype.name()] = stype;
481 if (service.patron_id) {
482 // retrieve applied values for the current user
483 // for the setting types we care about.
486 setting_types.map(function(obj) { return obj.name() });
488 return egCore.net.request(
490 'open-ils.actor.patron.settings.retrieve.authoritative',
494 ).then(function(settings) {
495 service.user_settings = settings;
499 // apply default user setting values
500 angular.forEach(setting_types, function(stype, index) {
501 if (stype.reg_default() != undefined) {
502 service.user_settings[setting.name()] =
503 setting.reg_default();
510 service.invalidate_field = function(patron, field) {
511 console.log('Invalidating patron field ' + field);
513 return egCore.net.request(
515 'open-ils.actor.invalidate.' + field,
516 egCore.auth.token(), patron.id, null, patron.home_ou.id()
518 ).then(function(res) {
519 // clear the invalid value from the form
522 // update last_xact_id so future save operations
523 // on this patron will be allowed
524 patron.last_xact_id = res.payload.last_xact_id[patron.id];
528 service.dupe_patron_search = function(patron, type, value) {
531 console.log('Dupe search called with "'+ type +'" and value '+ value);
536 var fname = patron.first_given_name;
537 var lname = patron.family_name;
538 if (!(fname && lname)) return $q.when({count:0});
540 first_given_name : {value : fname, group : 0},
541 family_name : {value : lname, group : 0}
546 search = {email : {value : value, group : 0}};
550 search = {ident : {value : value, group : 2}};
554 search = {phone : {value : value, group : 2}};
559 angular.forEach(['street1', 'street2', 'city', 'post_code'],
562 search[field] = {value : value[field], group: 1};
568 return egCore.net.request(
570 'open-ils.actor.patron.search.advanced',
571 egCore.auth.token(), search, null, null, 1
572 ).then(function(res) {
573 res = res.filter(function(id) {return id != patron.id});
581 service.init_patron = function(current) {
584 return service.init_new_patron();
586 service.patron = current;
587 return service.init_existing_patron(current)
590 service.ingest_address = function(patron, addr) {
591 addr.valid = addr.valid == 't';
592 addr.within_city_limits = addr.within_city_limits == 't';
593 addr._is_mailing = (patron.mailing_address &&
594 addr.id == patron.mailing_address.id);
595 addr._is_billing = (patron.billing_address &&
596 addr.id == patron.billing_address.id);
600 * Existing patron objects reqire some data munging before insertion
603 * 1. Turn everything into a hash
604 * 2. ... Except certain fields (selectors) whose widgets require objects
605 * 3. Bools must be Boolean, not t/f.
607 service.init_existing_patron = function(current) {
609 service.existing_patron = current;
611 var patron = egCore.idl.toHash(current);
613 patron.home_ou = egCore.org.get(patron.home_ou.id);
614 patron.expire_date = new Date(Date.parse(patron.expire_date));
615 patron.dob = service.parse_dob(patron.dob);
616 patron.profile = current.profile(); // pre-hash version
617 patron.net_access_level = current.net_access_level();
618 patron.ident_type = current.ident_type();
619 patron.groups = current.groups(); // pre-hash
622 ['juvenile', 'barred', 'active', 'master_account'],
623 function(field) { patron[field] = patron[field] == 't'; }
626 angular.forEach(patron.cards, function(card) {
627 card.active = card.active == 't';
628 if (card.id == patron.card.id) {
630 card._primary = 'on';
634 angular.forEach(patron.addresses,
635 function(addr) { service.ingest_address(patron, addr) });
637 service.get_linked_addr_users(patron.addresses);
639 // Remove stat cat entries that link to out-of-scope stat
640 // cats. With this, we avoid unnecessarily updating (or worse,
641 // modifying) stat cat values that are not ours to modify.
642 patron.stat_cat_entries = patron.stat_cat_entries.filter(
645 // service.stat_cats only contains in-scope stat cats.
646 service.stat_cats.filter(function(cat) {
647 return (cat.id() == map.stat_cat.id) })[0]
652 // toss entries for existing stat cat maps into our living
653 // stat cat entry map, which is modified within the template.
654 angular.forEach(patron.stat_cat_entries, function(map) {
655 service.stat_cat_entry_maps[map.stat_cat.id] = map.stat_cat_entry;
661 service.init_new_patron = function() {
663 id : service.virt_id--,
666 address_type : egCore.strings.REG_ADDR_TYPE,
669 within_city_limits : false,
670 country : service.org_settings['ui.patron.default_country'],
674 id : service.virt_id--,
685 home_ou : egCore.org.get(egCore.auth.user().ws_ou()),
686 stat_cat_entries : [],
691 if (service.clone_user)
692 service.copy_clone_data(user);
694 if (service.stage_user)
695 service.copy_stage_data(user);
700 // dob is always YYYY-MM-DD
701 // Dates of birth do not contain timezone info, which can lead to
702 // inconcistent timezone handling, potentially representing
703 // different points in time, depending on the implementation.
704 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
705 // See "Differences in assumed time zone"
706 // TODO: move this into egDate ?
707 service.parse_dob = function(dob) {
708 if (!dob) return null;
709 var parts = dob.split('-');
710 var d = new Date(); // always local time zone, yay.
711 d.setFullYear(parts[0]);
712 d.setMonth(parts[1] - 1);
717 service.copy_stage_data = function(user) {
718 var cuser = service.stage_user;
720 // copy the data into our new user object
722 for (var key in egCore.idl.classes.stgu.field_map) {
723 if (egCore.idl.classes.au.field_map[key] &&
724 !egCore.idl.classes.stgu.field_map[key].virtual) {
725 if (cuser.user[key]() !== null)
726 user[key] = cuser.user[key]();
730 if (user.home_ou) user.home_ou = egCore.org.get(user.home_ou);
731 if (user.profile) user.profile = egCore.env.pgt.map[user.profile];
733 user.ident_type = egCore.env.cit.map[user.ident_type];
734 user.dob = service.parse_dob(user.dob);
736 // Clear the usrname if it looks like a UUID
737 if (user.usrname.replace(/-/g,'').match(/[0-9a-f]{32}/))
740 // Don't use stub address if we have one from the staged user.
741 if (cuser.mailing_addresses.length || cuser.billing_addresses.length)
744 // is_mailing=false implies is_billing
745 function addr_from_stage(stage_addr) {
746 if (!stage_addr) return;
747 var cls = stage_addr.classname;
750 id : service.virt_id--,
754 _is_mailing : cls == 'stgma',
755 _is_billing : cls == 'stgba'
758 user.mailing_address = addr;
759 user.addresses.push(addr);
761 for (var key in egCore.idl.classes[cls].field_map) {
762 if (egCore.idl.classes.aua.field_map[key] &&
763 !egCore.idl.classes[cls].field_map[key].virtual) {
764 if (stage_addr[key]() !== null)
765 addr[key] = stage_addr[key]();
770 addr_from_stage(cuser.mailing_addresses[0]);
771 addr_from_stage(cuser.billing_addresses[0]);
773 if (user.addresses.length == 1) {
774 // If there is only one address,
775 // use it as both mailing and billing.
776 var addr = user.addresses[0];
777 addr._is_mailing = addr._is_billing = true;
778 user.mailing_address = user.billing_address = addr;
781 if (cuser.cards.length) {
783 id : service.virt_id--,
784 barcode : cuser.cards[0].barcode(),
790 user.cards.push(user.card);
791 if (user.usrname == '')
792 user.usrname = card.barcode;
796 // copy select values from the cloned user to the new user.
798 service.copy_clone_data = function(user) {
799 var clone_user = service.clone_user;
801 // flesh the home org locally
802 user.home_ou = egCore.org.get(clone_user.home_ou());
803 if (user.profile) user.profile = egCore.env.pgt.map[user.profile];
805 if (!clone_user.billing_address() &&
806 !clone_user.mailing_address())
807 return; // no addresses to copy or link
809 // if the cloned user has any addresses, we don't need
810 // the stub address created in init_new_patron.
814 service.org_settings['circ.patron_edit.clone.copy_address'];
823 angular.forEach(clone_fields, function(field) {
824 user[field] = clone_user[field]();
827 if (copy_addresses) {
828 var bill_addr, mail_addr;
830 // copy the billing and mailing addresses into new addresses
831 function clone_addr(addr) {
832 var new_addr = egCore.idl.toHash(addr);
833 new_addr.id = service.virt_id--;
834 new_addr.usr = user.id;
835 new_addr.isnew = true;
836 new_addr.valid = true;
837 user.addresses.push(new_addr);
841 if (bill_addr = clone_user.billing_address()) {
842 var addr = clone_addr(bill_addr);
843 addr._is_billing = true;
844 user.billing_address = addr;
847 if (mail_addr = clone_user.mailing_address()) {
849 if (bill_addr && bill_addr.id() == mail_addr.id()) {
850 user.mailing_address = user.billing_address;
851 user.mailing_address._is_mailing = true;
853 var addr = clone_addr(mail_addr);
854 addr._is_mailing = true;
855 user.mailing_address = addr;
859 // if there is no billing addr, use the mailing addr
860 user.billing_address = user.mailing_address;
861 user.billing_address._is_billing = true;
868 // link the billing and mailing addresses
870 if (addr = clone_user.billing_address()) {
871 user.billing_address = egCore.idl.toHash(addr);
872 user.billing_address._is_billing = true;
873 user.addresses.push(user.billing_address);
874 user.billing_address._linked_owner_id = clone_user.id();
875 user.billing_address._linked_owner = service.format_name(
876 clone_user.family_name(),
877 clone_user.first_given_name(),
878 clone_user.second_given_name()
882 if (addr = clone_user.mailing_address()) {
883 if (user.billing_address &&
884 addr.id() == user.billing_address.id) {
885 // mailing matches billing
886 user.mailing_address = user.billing_address;
887 user.mailing_address._is_mailing = true;
889 user.mailing_address = egCore.idl.toHash(addr);
890 user.mailing_address._is_mailing = true;
891 user.addresses.push(user.mailing_address);
892 user.mailing_address._linked_owner_id = clone_user.id();
893 user.mailing_address._linked_owner = service.format_name(
894 clone_user.family_name(),
895 clone_user.first_given_name(),
896 clone_user.second_given_name()
903 // translate the patron back into IDL form
904 service.save_user = function(phash) {
906 var patron = egCore.idl.fromHash('au', phash);
908 patron.home_ou(patron.home_ou().id());
909 patron.expire_date(patron.expire_date().toISOString());
910 patron.profile(patron.profile().id());
912 patron.dob(patron.dob().toISOString().replace(/T.*/,''));
913 if (patron.ident_type())
914 patron.ident_type(patron.ident_type().id());
915 if (patron.net_access_level())
916 patron.net_access_level(patron.net_access_level().id());
919 ['juvenile', 'barred', 'active', 'master_account'],
920 function(field) { patron[field](phash[field] ? 't' : 'f'); }
923 var card_hashes = patron.cards();
925 angular.forEach(card_hashes, function(chash) {
926 var card = egCore.idl.fromHash('ac', chash)
927 card.usr(patron.id());
928 card.active(chash.active ? 't' : 'f');
929 patron.cards().push(card);
930 if (chash._primary) {
935 var addr_hashes = patron.addresses();
936 patron.addresses([]);
937 angular.forEach(addr_hashes, function(addr_hash) {
938 if (!addr_hash.isnew && !addr_hash.isdeleted)
939 addr_hash.ischanged = true;
940 var addr = egCore.idl.fromHash('aua', addr_hash);
941 patron.addresses().push(addr);
942 addr.valid(addr.valid() ? 't' : 'f');
943 addr.within_city_limits(addr.within_city_limits() ? 't' : 'f');
944 if (addr_hash._is_mailing) patron.mailing_address(addr);
945 if (addr_hash._is_billing) patron.billing_address(addr);
948 patron.survey_responses([]);
949 angular.forEach(service.survey_responses, function(answer) {
950 var question = service.survey_questions[answer.question()];
951 var resp = new egCore.idl.asvr();
953 resp.survey(question.survey());
954 resp.question(question.id());
955 resp.answer(answer.id());
956 resp.usr(patron.id());
957 resp.answer_date('now');
958 patron.survey_responses().push(resp);
961 // re-object-ify the patron stat cat entry maps
963 angular.forEach(patron.stat_cat_entries(), function(entry) {
964 var e = egCore.idl.fromHash('actscecm', entry);
965 e.stat_cat(e.stat_cat().id);
968 patron.stat_cat_entries(maps);
970 // service.stat_cat_entry_maps maps stats to values
971 // patron.stat_cat_entries is an array of stat_cat_entry_usr_map's
973 service.stat_cat_entry_maps, function(value, cat_id) {
975 // see if we already have a mapping for this entry
976 var existing = patron.stat_cat_entries().filter(
977 function(e) { return e.stat_cat() == cat_id })[0];
979 if (existing) { // we have a mapping
980 // if the existing mapping matches the new one,
981 // there' nothing left to do
982 if (existing.stat_cat_entry() == value) return;
984 // mappings differ. delete the old one and create
986 existing.isdeleted(true);
989 var newmap = new egCore.idl.actscecm();
990 newmap.target_usr(patron.id());
992 newmap.stat_cat(cat_id);
993 newmap.stat_cat_entry(value);
994 patron.stat_cat_entries().push(newmap);
997 if (!patron.isnew()) patron.ischanged(true);
999 return egCore.net.request(
1001 'open-ils.actor.patron.update',
1002 egCore.auth.token(), patron);
1005 service.remove_staged_user = function() {
1006 if (!service.stage_user) return $q.when();
1007 return egCore.net.request(
1009 'open-ils.actor.user.stage.delete',
1010 egCore.auth.token(),
1011 service.stage_user.row_id()
1015 service.save_user_settings = function(new_user, user_settings) {
1016 // user_settings contains the values from the scope/form.
1017 // service.user_settings contain the values from page load time.
1020 if (service.patron_id) {
1021 // only update modified settings for existing patrons
1022 angular.forEach(user_settings, function(val, key) {
1023 if (val !== service.user_settings[key])
1024 settings[key] = val;
1028 // all non-null setting values are updated for new patrons
1029 angular.forEach(user_settings, function(val, key) {
1030 if (val !== null) settings[key] = val;
1034 if (Object.keys(settings).length == 0) return $q.when();
1036 return egCore.net.request(
1038 'open-ils.actor.patron.settings.update',
1039 egCore.auth.token(), new_user.id(), settings
1040 ).then(function(resp) {
1045 // Applies field-specific validation regex's from org settings
1046 // to form fields. Be careful not remove any pattern data we
1047 // are not explicitly over-writing in the provided patterns obj.
1048 service.set_field_patterns = function(patterns) {
1049 if (service.org_settings['opac.username_regex']) {
1050 patterns.au.usrname =
1051 new RegExp(service.org_settings['opac.username_regex']);
1054 if (service.org_settings['opac.barcode_regex']) {
1055 patterns.ac.barcode =
1056 new RegExp(service.org_settings['opac.barcode_regex']);
1059 if (service.org_settings['global.password_regex']) {
1060 patterns.au.passwd =
1061 new RegExp(service.org_settings['global.password_regex']);
1064 var phone_reg = service.org_settings['ui.patron.edit.phone.regex'];
1066 // apply generic phone regex first, replace below as needed.
1067 patterns.au.day_phone = new RegExp(phone_reg);
1068 patterns.au.evening_phone = new RegExp(phone_reg);
1069 patterns.au.other_phone = new RegExp(phone_reg);
1072 // the remaining patterns fit a well-known key name pattern
1074 angular.forEach(service.org_settings, function(val, key) {
1076 var parts = key.match(/ui.patron.edit\.(\w+)\.(\w+)\.regex/);
1079 var name = parts[2];
1080 patterns[cls][name] = new RegExp(val);
1087 .controller('PatronRegCtrl',
1088 ['$scope','$routeParams','$q','$uibModal','$window','egCore',
1089 'patronSvc','patronRegSvc','egUnloadPrompt','egAlertDialog',
1091 function($scope , $routeParams , $q , $uibModal , $window , egCore ,
1092 patronSvc , patronRegSvc , egUnloadPrompt, egAlertDialog ,
1095 $scope.page_data_loaded = false;
1096 $scope.clone_id = patronRegSvc.clone_id = $routeParams.clone_id;
1097 $scope.stage_username =
1098 patronRegSvc.stage_username = $routeParams.stage_username;
1100 patronRegSvc.patron_id = $routeParams.edit_id || $routeParams.id;
1102 // for existing patrons, disable barcode input by default
1103 $scope.disable_bc = $scope.focus_usrname = Boolean($scope.patron_id);
1104 $scope.focus_bc = !Boolean($scope.patron_id);
1105 $scope.dupe_counts = {};
1107 // map of perm name to true/false for perms the logged in user
1108 // has at the currently selected patron home org unit.
1111 if (!$scope.edit_passthru) {
1112 // in edit more, scope.edit_passthru is delivered to us by
1113 // the enclosing controller. In register mode, there is
1114 // no enclosing controller, so we create our own.
1115 $scope.edit_passthru = {};
1118 // 0=all, 1=suggested, 2=all
1119 $scope.edit_passthru.vis_level = 0;
1121 // Apply default values for new patrons during initial registration
1122 // prs is shorthand for patronSvc
1123 function set_new_patron_defaults(prs) {
1124 if (!$scope.patron.passwd) {
1125 // passsword may originate from staged user.
1126 $scope.generate_password();
1128 $scope.hold_notify_phone = true;
1129 $scope.hold_notify_email = true;
1131 // staged users may be loaded w/ a profile.
1132 $scope.set_expire_date();
1134 if (prs.org_settings['ui.patron.default_ident_type']) {
1135 // $scope.patron needs this field to be an object
1136 var id = prs.org_settings['ui.patron.default_ident_type'];
1137 var ident_type = $scope.ident_types.filter(
1138 function(type) { return type.id() == id })[0];
1139 $scope.patron.ident_type = ident_type;
1141 if (prs.org_settings['ui.patron.default_inet_access_level']) {
1142 // $scope.patron needs this field to be an object
1143 var id = prs.org_settings['ui.patron.default_inet_access_level'];
1144 var level = $scope.net_access_levels.filter(
1145 function(lvl) { return lvl.id() == id })[0];
1146 $scope.patron.net_access_level = level;
1148 if (prs.org_settings['ui.patron.default_country']) {
1149 $scope.patron.addresses[0].country =
1150 prs.org_settings['ui.patron.default_country'];
1154 // A null or undefined pattern leads to exceptions. Before the
1155 // patterns are loaded from the server, default all patterns
1156 // to an innocuous regex. To avoid re-creating numerous
1157 // RegExp objects, cache the stub RegExp after initial creation.
1158 // note: angular docs say ng-pattern accepts a regexp or string,
1159 // but as of writing, it only works with a regexp object.
1160 // (Likely an angular 1.2 vs. 1.4 issue).
1161 var field_patterns = {au : {}, ac : {}, aua : {}};
1162 $scope.field_pattern = function(cls, field) {
1163 if (!field_patterns[cls][field])
1164 field_patterns[cls][field] = new RegExp('.*');
1165 return field_patterns[cls][field];
1168 // Main page load function. Kicks off tab init and data loading.
1171 $scope.initTab ? // initTab comes from patron app
1172 $scope.initTab('edit', $routeParams.id) : $q.when(),
1176 ]).then(function() {
1177 // called after initTab and patronRegSvc.init have completed
1179 var prs = patronRegSvc; // brevity
1180 // in standalone mode, we have no patronSvc
1181 $scope.patron = prs.init_patron(patronSvc ? patronSvc.current : null);
1182 $scope.field_doc = prs.field_doc;
1183 $scope.edit_profiles = prs.edit_profiles;
1184 $scope.ident_types = prs.ident_types;
1185 $scope.net_access_levels = prs.net_access_levels;
1186 $scope.user_setting_types = prs.user_setting_types;
1187 $scope.opt_in_setting_types = prs.opt_in_setting_types;
1188 $scope.org_settings = prs.org_settings;
1189 $scope.sms_carriers = prs.sms_carriers;
1190 $scope.stat_cats = prs.stat_cats;
1191 $scope.surveys = prs.surveys;
1192 $scope.survey_responses = prs.survey_responses;
1193 $scope.stat_cat_entry_maps = prs.stat_cat_entry_maps;
1194 $scope.stage_user = prs.stage_user;
1195 $scope.stage_user_requestor = prs.stage_user_requestor;
1197 $scope.user_settings = prs.user_settings;
1198 // clone the user settings back into the patronRegSvc so
1199 // we have a copy of the original state of the settings.
1200 prs.user_settings = {};
1201 angular.forEach($scope.user_settings, function(val, key) {
1202 prs.user_settings[key] = val;
1205 extract_hold_notify();
1206 $scope.handle_home_org_changed();
1208 if ($scope.org_settings['ui.patron.edit.default_suggested'])
1209 $scope.edit_passthru.vis_level = 1;
1211 if ($scope.patron.isnew)
1212 set_new_patron_defaults(prs);
1214 $scope.page_data_loaded = true;
1216 prs.set_field_patterns(field_patterns);
1217 apply_username_regex();
1221 // update the currently displayed field documentation
1222 $scope.set_selected_field_doc = function(cls, field) {
1223 $scope.selected_field_doc = $scope.field_doc[cls][field];
1226 // returns the tree depth of the selected profile group tree node.
1227 $scope.pgt_depth = function(grp) {
1229 while (grp = egCore.env.pgt.map[grp.parent()]) d++;
1233 // IDL fields used for labels in the UI.
1234 $scope.idl_fields = {
1235 au : egCore.idl.classes.au.field_map,
1236 ac : egCore.idl.classes.ac.field_map,
1237 aua : egCore.idl.classes.aua.field_map
1240 // field visibility cache. Some fields are universally required.
1241 // 3 == value universally required
1242 // 2 == field is visible by default
1243 // 1 == field is suggested by default
1244 var field_visibility = {};
1245 var default_field_visibility = {
1249 'au.first_given_name' : 3,
1250 'au.family_name' : 3,
1251 'au.ident_type' : 3,
1254 'au.expire_date' : 3,
1255 'au.net_access_level' : 3,
1256 'aua.address_type' : 3,
1257 'aua.post_code' : 3,
1265 'aua.within_city_limits' : 2,
1270 // Returns true if the selected field should be visible
1271 // given the current required/suggested/all setting.
1272 // The visibility flag applied to each field as a result of calling
1273 // this function also sets (via the same flag) the requiredness state.
1274 $scope.show_field = function(field_key) {
1275 // org settings have not been received yet.
1276 if (!$scope.org_settings) return false;
1278 if (field_visibility[field_key] == undefined) {
1279 // compile and cache the visibility for the selected field
1281 var req_set = 'ui.patron.edit.' + field_key + '.require';
1282 var sho_set = 'ui.patron.edit.' + field_key + '.show';
1283 var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
1285 if ($scope.org_settings[req_set]) {
1286 field_visibility[field_key] = 3;
1288 } else if ($scope.org_settings[sho_set]) {
1289 field_visibility[field_key] = 2;
1291 } else if ($scope.org_settings[sug_set]) {
1292 field_visibility[field_key] = 1;
1296 if (field_visibility[field_key] == undefined) {
1297 // No org settings were applied above. Use the default
1298 // settings if present or assume the field has no
1299 // visibility flags applied.
1300 field_visibility[field_key] =
1301 default_field_visibility[field_key] || 0;
1304 return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
1307 // See $scope.show_field().
1308 // A field with visbility level 3 means it's required.
1309 $scope.field_required = function(cls, field) {
1311 // Value in the password field is not required
1312 // for existing patrons.
1313 if (field == 'passwd' && $scope.patron && !$scope.patron.isnew)
1316 return (field_visibility[cls + '.' + field] == 3);
1319 // generates a random 4-digit password
1320 $scope.generate_password = function() {
1321 $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
1324 $scope.set_expire_date = function() {
1325 if (!$scope.patron.profile) return;
1326 var seconds = egCore.date.intervalToSeconds(
1327 $scope.patron.profile.perm_interval());
1328 var now_epoch = new Date().getTime();
1329 $scope.patron.expire_date = new Date(
1330 now_epoch + (seconds * 1000 /* milliseconds */))
1333 // grp is the pgt object
1334 $scope.set_profile = function(grp) {
1335 $scope.patron.profile = grp;
1336 $scope.set_expire_date();
1337 $scope.field_modified();
1340 $scope.invalid_profile = function() {
1343 $scope.patron.profile &&
1344 $scope.patron.profile.usergroup() == 't'
1348 $scope.new_address = function() {
1349 var addr = egCore.idl.toHash(new egCore.idl.aua());
1350 patronRegSvc.ingest_address($scope.patron, addr);
1351 addr.id = patronRegSvc.virt_id--;
1354 addr.within_city_limits = true;
1355 addr.country = $scope.org_settings['ui.patron.default_country'];
1356 $scope.patron.addresses.push(addr);
1359 // keep deleted addresses out of the patron object so
1360 // they won't appear in the UI. They'll be re-inserted
1361 // when the patron is updated.
1362 deleted_addresses = [];
1363 $scope.delete_address = function(id) {
1365 if ($scope.patron.isnew &&
1366 $scope.patron.addresses.length == 1 &&
1367 $scope.org_settings['ui.patron.registration.require_address']) {
1368 egAlertDialog.open(egCore.strings.REG_ADDR_REQUIRED);
1373 angular.forEach($scope.patron.addresses, function(addr) {
1374 if (addr.id == id) {
1376 addr.isdeleted = true;
1377 deleted_addresses.push(addr);
1380 addresses.push(addr);
1383 $scope.patron.addresses = addresses;
1386 $scope.post_code_changed = function(addr) {
1388 'open-ils.search', 'open-ils.search.zip', addr.post_code)
1389 .then(function(resp) {
1391 if (resp.city) addr.city = resp.city;
1392 if (resp.state) addr.state = resp.state;
1393 if (resp.county) addr.county = resp.county;
1394 if (resp.alert) alert(resp.alert);
1398 $scope.replace_card = function() {
1399 $scope.patron.card.active = false;
1400 $scope.patron.card.ischanged = true;
1401 $scope.disable_bc = false;
1403 var new_card = egCore.idl.toHash(new egCore.idl.ac());
1404 new_card.id = patronRegSvc.virt_id--;
1405 new_card.isnew = true;
1406 new_card.active = true;
1407 new_card._primary = 'on';
1408 $scope.patron.card = new_card;
1409 $scope.patron.cards.push(new_card);
1412 $scope.day_phone_changed = function(phone) {
1413 if (phone && $scope.patron.isnew &&
1414 $scope.org_settings['patron.password.use_phone']) {
1415 $scope.patron.passwd = phone.substr(-4);
1419 $scope.barcode_changed = function(bc) {
1421 $scope.dupe_barcode = false;
1424 'open-ils.actor.barcode.exists',
1425 egCore.auth.token(), bc
1426 ).then(function(resp) {
1427 if (resp == '1') { // duplicate card
1428 $scope.dupe_barcode = true;
1429 console.log('duplicate barcode detected: ' + bc);
1431 if (!$scope.patron.usrname)
1432 $scope.patron.usrname = bc;
1438 $scope.cards_dialog = function() {
1440 templateUrl: './circ/patron/t_patron_cards_dialog',
1442 ['$scope','$uibModalInstance','cards','perms',
1443 function($scope , $uibModalInstance , cards , perms) {
1444 // scope here is the modal-level scope
1445 $scope.args = {cards : cards};
1446 $scope.perms = perms;
1447 $scope.ok = function() { $uibModalInstance.close($scope.args) }
1448 $scope.cancel = function () { $uibModalInstance.dismiss() }
1451 cards : function() {
1452 // scope here is the controller-level scope
1453 return $scope.patron.cards;
1455 perms : function() {
1456 return $scope.perms;
1461 angular.forEach(args.cards, function(card) {
1462 card.ischanged = true; // assume cards need updating, OK?
1463 if (card._primary == 'on' &&
1464 card.id != $scope.patron.card.id) {
1465 $scope.patron.card = card;
1472 $scope.set_addr_type = function(addr, type) {
1473 var addrs = $scope.patron.addresses;
1474 if (addr['_is_'+type]) {
1475 angular.forEach(addrs, function(a) {
1476 if (a.id != addr.id) a['_is_'+type] = false;
1479 // unchecking mailing/billing means we have to randomly
1480 // select another address to fill that role. Select the
1481 // first address in the list (that does not match the
1483 for (var i = 0; i < addrs.length; i++) {
1484 if (addrs[i].id != addr.id) {
1485 addrs[i]['_is_' + type] = true;
1493 // Translate hold notify preferences from the form/scope back into a
1494 // single user setting value for opac.hold_notify.
1495 function compress_hold_notify() {
1496 var hold_notify = '';
1498 if ($scope.hold_notify_phone) {
1499 hold_notify = 'phone';
1502 if ($scope.hold_notify_email) {
1503 hold_notify = splitter + 'email';
1506 if ($scope.hold_notify_sms) {
1507 hold_notify = splitter + 'sms';
1510 $scope.user_settings['opac.hold_notify'] = hold_notify;
1513 // dialog for selecting additional permission groups
1514 $scope.secondary_groups_dialog = function() {
1516 templateUrl: './circ/patron/t_patron_groups_dialog',
1518 ['$scope','$uibModalInstance','linked_groups','pgt_depth',
1519 function($scope , $uibModalInstance , linked_groups , pgt_depth) {
1521 $scope.pgt_depth = pgt_depth;
1523 linked_groups : linked_groups,
1524 edit_profiles : patronRegSvc.edit_profiles,
1525 new_profile : patronRegSvc.edit_profiles[0]
1528 // add a new group to the linked groups list
1529 $scope.link_group = function($event, grp) {
1530 var found = false; // avoid duplicates
1531 angular.forEach($scope.args.linked_groups,
1532 function(g) {if (g.id() == grp.id()) found = true});
1533 if (!found) $scope.args.linked_groups.push(grp);
1534 $event.preventDefault(); // avoid close
1537 // remove a group from the linked groups list
1538 $scope.unlink_group = function($event, grp) {
1539 $scope.args.linked_groups =
1540 $scope.args.linked_groups.filter(function(g) {
1541 return g.id() != grp.id()
1543 $event.preventDefault(); // avoid close
1546 $scope.ok = function() { $uibModalInstance.close($scope.args) }
1547 $scope.cancel = function () { $uibModalInstance.dismiss() }
1550 linked_groups : function() { return $scope.patron.groups },
1551 pgt_depth : function() { return $scope.pgt_depth }
1556 if ($scope.patron.isnew) {
1557 // groups must be linked for new patrons after the
1558 // patron is created.
1559 $scope.patron.groups = args.linked_groups;
1563 // update links groups for existing users in real time.
1564 var ids = args.linked_groups.map(function(g) {return g.id()});
1565 patronRegSvc.apply_secondary_groups($scope.patron.id, ids)
1566 .then(function(success) {
1568 $scope.patron.groups = args.linked_groups;
1574 function extract_hold_notify() {
1575 notify = $scope.user_settings['opac.hold_notify'];
1576 if (!notify) return;
1577 $scope.hold_notify_phone = Boolean(notify.match(/phone/));
1578 $scope.hold_notify_email = Boolean(notify.match(/email/));
1579 $scope.hold_notify_sms = Boolean(notify.match(/sms/));
1582 $scope.invalidate_field = function(field) {
1583 patronRegSvc.invalidate_field($scope.patron, field);
1587 $scope.dupe_value_changed = function(type, value) {
1588 $scope.dupe_counts[type] = 0;
1589 patronRegSvc.dupe_patron_search($scope.patron, type, value)
1590 .then(function(res) {
1591 $scope.dupe_counts[type] = res.count;
1593 $scope.dupe_search_encoded =
1594 encodeURIComponent(js2JSON(res.search));
1596 $scope.dupe_search_encoded = '';
1601 $scope.handle_home_org_changed = function() {
1602 org_id = $scope.patron.home_ou.id();
1603 patronRegSvc.has_perms_for_org(org_id).then(function(map) {
1604 angular.forEach(map, function(v, k) { $scope.perms[k] = v });
1608 // This is called with every character typed in a form field,
1609 // since that's the only way to gaurantee something has changed.
1610 // See handle_field_changed for ng-change vs. ng-blur.
1611 $scope.field_modified = function() {
1612 // Call attach with every field change, regardless of whether
1613 // it's been called before. This will allow for re-attach after
1614 // the user clicks through the unload warning. egUnloadPrompt
1615 // will ensure we only attach once.
1616 egUnloadPrompt.attach($scope);
1619 // also monitor when form is changed *by the user*, as using
1620 // an ng-change handler doesn't work with eg-date-input
1621 $scope.$watch('reg_form.$pristine', function(newVal, oldVal) {
1622 if (!newVal) egUnloadPrompt.attach($scope);
1625 // username regex (if present) must be removed any time
1626 // the username matches the barcode to avoid firing the
1627 // invalid field handlers.
1628 function apply_username_regex() {
1629 var regex = $scope.org_settings['opac.username_regex'];
1631 if ($scope.patron.card.barcode) {
1632 // username must match the regex or the barcode
1633 field_patterns.au.usrname =
1635 regex + '|^' + $scope.patron.card.barcode + '$');
1637 // username must match the regex
1638 field_patterns.au.usrname = new RegExp(regex);
1641 // username can be any format.
1642 field_patterns.au.usrname = new RegExp('.*');
1646 // obj could be the patron, an address, etc.
1647 // This is called any time a form field achieves then loses focus.
1648 // It does not necessarily mean the field has changed.
1649 // The alternative is ng-change, but it's called with each character
1650 // typed, which would be overkill for many of the actions called here.
1651 $scope.handle_field_changed = function(obj, field_name) {
1652 var cls = obj.classname; // set by egIdl
1653 var value = obj[field_name];
1655 console.log('changing field ' + field_name + ' to ' + value);
1657 switch (field_name) {
1659 if ($scope.patron.day_phone &&
1660 $scope.patron.isnew &&
1661 $scope.org_settings['patron.password.use_phone']) {
1662 $scope.patron.passwd = phone.substr(-4);
1664 case 'evening_phone' :
1665 case 'other_phone' :
1666 $scope.dupe_value_changed('phone', value);
1670 case 'ident_value2':
1671 $scope.dupe_value_changed('ident', value);
1674 case 'first_given_name':
1676 $scope.dupe_value_changed('name', value);
1680 $scope.dupe_value_changed('email', value);
1686 // dupe search on address wants the address object as the value.
1687 $scope.dupe_value_changed('address', obj);
1691 $scope.post_code_changed(obj);
1695 patronRegSvc.check_dupe_username(value)
1696 .then(function(yes) {$scope.dupe_username = Boolean(yes)});
1700 // TODO: finish barcode_changed handler.
1701 $scope.barcode_changed(value);
1702 apply_username_regex();
1706 maintain_juvenile_flag();
1711 // patron.juvenile is set to true if the user was born after
1712 function maintain_juvenile_flag() {
1713 if ( !($scope.patron && $scope.patron.dob) ) return;
1716 $scope.org_settings['global.juvenile_age_threshold']
1719 var base = new Date();
1721 base.setTime(base.getTime() -
1722 Number(egCore.date.intervalToSeconds(juv_interval) + '000'));
1724 $scope.patron.juvenile = ($scope.patron.dob > base);
1727 // returns true (disable) for orgs that cannot have users.
1728 $scope.disable_home_org = function(org_id) {
1729 if (!org_id) return;
1730 var org = egCore.org.get(org_id);
1734 org.ou_type().can_have_users() == 'f'
1738 // Returns true if the Save and Save & Clone buttons should be disabled.
1739 $scope.edit_passthru.hide_save_actions = function() {
1740 return $scope.patron.isnew ?
1741 !$scope.perms.CREATE_USER :
1742 !$scope.perms.UPDATE_USER;
1745 // Returns true if any input elements are tagged as invalid
1746 // via Angular patterns or required attributes.
1747 function form_has_invalid_fields() {
1748 return $('#patron-reg-container .ng-invalid').length > 0;
1751 function form_is_incomplete() {
1753 $scope.dupe_username ||
1754 $scope.dupe_barcode ||
1755 form_has_invalid_fields()
1760 $scope.edit_passthru.save = function(save_args) {
1761 if (!save_args) save_args = {};
1763 if (form_is_incomplete()) {
1764 // User has not provided valid values for all required fields.
1765 return egAlertDialog.open(egCore.strings.REG_INVALID_FIELDS);
1768 // remove page unload warning prompt
1769 egUnloadPrompt.clear();
1771 // toss the deleted addresses back into the patron's list of
1772 // addresses so it's included in the update
1773 $scope.patron.addresses =
1774 $scope.patron.addresses.concat(deleted_addresses);
1776 compress_hold_notify();
1780 patronRegSvc.save_user($scope.patron)
1781 .then(function(new_user) {
1782 if (new_user && new_user.classname) {
1783 updated_user = new_user;
1784 return patronRegSvc.save_user_settings(
1785 new_user, $scope.user_settings);
1787 alert('Patron update failed. \n\n' + js2JSON(new_user));
1790 }).then(function() {
1792 // only remove the staged user if the update succeeded.
1794 return patronRegSvc.remove_staged_user();
1798 }).then(function() {
1800 // linked groups for new users must be created after the new
1802 if ($scope.patron.isnew &&
1803 $scope.patron.groups && $scope.patron.groups.length) {
1804 var ids = $scope.patron.groups.map(function(g) {return g.id()});
1805 return patronRegSvc.apply_secondary_groups(updated_user.id(), ids)
1810 }).then(function() {
1815 ? egCore.strings.EG_WORK_LOG_REGISTERED_PATRON
1816 : egCore.strings.EG_WORK_LOG_EDITED_PATRON, {
1817 'action' : $scope.patron.isnew ? 'registered_patron' : 'edited_patron',
1818 'patron_id' : updated_user.id()
1823 // reloading the page means potentially losing some information
1824 // (e.g. last patron search), but is the only way to ensure all
1825 // components are properly updated to reflect the modified patron.
1826 if (updated_user && save_args.clone) {
1827 // open a separate tab for registering a new
1828 // patron from our cloned data.
1829 var url = 'https://'
1830 + $window.location.hostname
1831 + egCore.env.basePath
1832 + '/circ/patron/register/clone/'
1833 + updated_user.id();
1834 $window.open(url, '_blank').focus();
1837 // reload the current page
1838 $window.location.href = location.href;