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 service.get_stage_user = function() {
71 if (!service.stage_username) return $q.when();
73 // fetch the staged user object
74 return egCore.net.request(
76 'open-ils.actor.user.stage.retrieve.by_username',
78 service.stage_username
79 ).then(function(suser) {
80 if (e = egCore.evt.parse(suser)) {
83 service.stage_user = suser;
87 if (!service.stage_user) return;
88 var requestor = service.stage_user.user.requesting_usr();
90 if (!requestor) return;
92 // fetch the requesting user
93 return egCore.net.request(
95 'open-ils.actor.user.retrieve.parts',
98 ['family_name', 'first_given_name', 'second_given_name']
99 ).then(function(parts) {
100 service.stage_user_requestor =
101 service.format_name(parts[0], parts[1], parts[2]);
106 // See note above about not loading egUser.
108 service.format_name = function(last, first, middle) {
109 return last + ', ' + first + (middle ? ' ' + middle : '');
112 //service.check_grp_app_perm = function(grp_id) {
114 // determine which user groups our user is not allowed to modify
115 service.set_edit_profiles = function() {
116 var all_app_perms = [];
117 var failed_perms = [];
119 // extract the application permissions
120 angular.forEach(service.profiles, function(grp) {
121 if (grp.application_perm())
122 all_app_perms.push(grp.application_perm());
125 // fill in service.edit_profiles by inspecting failed_perms
126 function traverse_grp_tree(grp, failed) {
128 failed_perms.indexOf(grp.application_perm()) > -1;
130 if (!failed) service.edit_profiles.push(grp);
133 service.profiles.filter( // children of grp
134 function(p) { return p.parent() == grp.id() }),
135 function(child) {traverse_grp_tree(child, failed)}
139 return egCore.perm.hasPermAt(all_app_perms, true).then(
140 function(perm_orgs) {
141 angular.forEach(all_app_perms, function(p) {
142 if (perm_orgs[p].length == 0)
143 failed_perms.push(p);
146 traverse_grp_tree(egCore.env.pgt.tree);
151 service.has_group_link_perms = function(org_id) {
152 return egCore.perm.hasPermAt('CREATE_USER_GROUP_LINK', true)
153 .then(function(p) { return p.indexOf(org_id) > -1; });
156 service.get_surveys = function() {
157 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
159 return egCore.pcrud.search('asv', {
161 start_date : {'<=' : 'now'},
162 end_date : {'>=' : 'now'}
171 ).then(function(surveys) {
172 surveys = surveys.sort(function(a,b) {
173 return a.name() < b.name() ? -1 : 1 });
174 service.surveys = surveys;
175 angular.forEach(surveys, function(survey) {
176 angular.forEach(survey.questions(), function(question) {
177 service.survey_questions[question.id()] = question;
178 angular.forEach(question.answers(), function(answer) {
179 service.survey_answers[answer.id()] = answer;
186 service.get_stat_cats = function() {
187 return egCore.net.request(
189 'open-ils.circ.stat_cat.actor.retrieve.all',
190 egCore.auth.token(), egCore.auth.user().ws_ou()
191 ).then(function(cats) {
192 cats = cats.sort(function(a, b) {
193 return a.name() < b.name() ? -1 : 1});
194 angular.forEach(cats, function(cat) {
196 cat.entries().sort(function(a,b) {
197 return a.value() < b.value() ? -1 : 1
201 service.stat_cats = cats;
205 service.get_org_settings = function() {
206 return egCore.org.settings([
207 'global.password_regex',
208 'global.juvenile_age_threshold',
209 'patron.password.use_phone',
210 'ui.patron.default_inet_access_level',
211 'ui.patron.default_ident_type',
212 'ui.patron.default_country',
213 'ui.patron.registration.require_address',
214 'circ.holds.behind_desk_pickup_supported',
215 'circ.patron_edit.clone.copy_address',
216 'ui.patron.edit.au.prefix.require',
217 'ui.patron.edit.au.prefix.show',
218 'ui.patron.edit.au.prefix.suggest',
219 'ui.patron.edit.ac.barcode.regex',
220 'ui.patron.edit.au.second_given_name.show',
221 'ui.patron.edit.au.second_given_name.suggest',
222 'ui.patron.edit.au.suffix.show',
223 'ui.patron.edit.au.suffix.suggest',
224 'ui.patron.edit.au.alias.show',
225 'ui.patron.edit.au.alias.suggest',
226 'ui.patron.edit.au.dob.require',
227 'ui.patron.edit.au.dob.show',
228 'ui.patron.edit.au.dob.suggest',
229 'ui.patron.edit.au.dob.calendar',
230 'ui.patron.edit.au.juvenile.show',
231 'ui.patron.edit.au.juvenile.suggest',
232 'ui.patron.edit.au.ident_value.show',
233 'ui.patron.edit.au.ident_value.suggest',
234 'ui.patron.edit.au.ident_value2.show',
235 'ui.patron.edit.au.ident_value2.suggest',
236 'ui.patron.edit.au.email.require',
237 'ui.patron.edit.au.email.show',
238 'ui.patron.edit.au.email.suggest',
239 'ui.patron.edit.au.email.regex',
240 'ui.patron.edit.au.email.example',
241 'ui.patron.edit.au.day_phone.require',
242 'ui.patron.edit.au.day_phone.show',
243 'ui.patron.edit.au.day_phone.suggest',
244 'ui.patron.edit.au.day_phone.regex',
245 'ui.patron.edit.au.day_phone.example',
246 'ui.patron.edit.au.evening_phone.require',
247 'ui.patron.edit.au.evening_phone.show',
248 'ui.patron.edit.au.evening_phone.suggest',
249 'ui.patron.edit.au.evening_phone.regex',
250 'ui.patron.edit.au.evening_phone.example',
251 'ui.patron.edit.au.other_phone.require',
252 'ui.patron.edit.au.other_phone.show',
253 'ui.patron.edit.au.other_phone.suggest',
254 'ui.patron.edit.au.other_phone.regex',
255 'ui.patron.edit.au.other_phone.example',
256 'ui.patron.edit.phone.regex',
257 'ui.patron.edit.phone.example',
258 'ui.patron.edit.au.active.show',
259 'ui.patron.edit.au.active.suggest',
260 'ui.patron.edit.au.barred.show',
261 'ui.patron.edit.au.barred.suggest',
262 'ui.patron.edit.au.master_account.show',
263 'ui.patron.edit.au.master_account.suggest',
264 'ui.patron.edit.au.claims_returned_count.show',
265 'ui.patron.edit.au.claims_returned_count.suggest',
266 'ui.patron.edit.au.claims_never_checked_out_count.show',
267 'ui.patron.edit.au.claims_never_checked_out_count.suggest',
268 'ui.patron.edit.au.alert_message.show',
269 'ui.patron.edit.au.alert_message.suggest',
270 'ui.patron.edit.aua.post_code.regex',
271 'ui.patron.edit.aua.post_code.example',
272 'ui.patron.edit.aua.county.require',
274 'ui.patron.edit.default_suggested',
275 'opac.barcode_regex',
276 'opac.username_regex',
278 'ui.patron.edit.aua.state.require',
279 'ui.patron.edit.aua.state.suggest',
280 'ui.patron.edit.aua.state.show'
281 ]).then(function(settings) {
282 service.org_settings = settings;
283 return service.process_org_settings(settings);
287 // some org settings require the retrieval of additional data
288 service.process_org_settings = function(settings) {
292 if (settings['sms.enable']) {
293 // fetch SMS carriers
295 egCore.pcrud.search('csc',
298 {'class':'csc', 'field':'name'},
299 {'class':'csc', 'field':'region'}
301 ).then(function(carriers) {
302 service.sms_carriers = carriers;
306 // if other promises are added below, this is not necessary.
307 promises.push($q.when());
310 // other post-org-settings processing goes here,
311 // adding to promises as needed.
313 return $q.all(promises);
316 service.get_ident_types = function() {
317 if (egCore.env.cit) {
318 service.ident_types = egCore.env.cit.list;
321 return egCore.pcrud.retrieveAll('cit', {}, {atomic : true})
322 .then(function(types) {
323 egCore.env.absorbList(types, 'cit')
324 service.ident_types = types
329 service.get_net_access_levels = function() {
330 if (egCore.env.cnal) {
331 service.net_access_levels = egCore.env.cnal.list;
334 return egCore.pcrud.retrieveAll('cnal', {}, {atomic : true})
335 .then(function(levels) {
336 egCore.env.absorbList(levels, 'cnal')
337 service.net_access_levels = levels
342 service.get_perm_groups = function() {
343 if (egCore.env.pgt) {
344 service.profiles = egCore.env.pgt.list;
345 return service.set_edit_profiles();
347 return egCore.pcrud.search('pgt', {parent : null},
348 {flesh : -1, flesh_fields : {pgt : ['children']}}
351 egCore.env.absorbTree(tree, 'pgt')
352 service.profiles = egCore.env.pgt.list;
353 return service.set_edit_profiles();
359 service.get_field_doc = function() {
360 return egCore.pcrud.search('fdoc', {
361 fm_class: ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva']})
362 .then(null, null, function(doc) {
363 if (!service.field_doc[doc.fm_class()]) {
364 service.field_doc[doc.fm_class()] = {};
366 service.field_doc[doc.fm_class()][doc.field()] = doc;
370 service.get_user_settings = function() {
371 var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
374 'circ.holds_behind_desk',
375 'circ.collections.exempt',
377 'opac.default_phone',
378 'opac.default_pickup_location',
379 'opac.default_sms_carrier',
380 'opac.default_sms_notify'];
382 return egCore.pcrud.search('cust', {
384 {name : static_types}, // common user settings
385 {name : { // opt-in notification user settings
387 select : {atevdef : ['opt_in_setting']},
389 // we only care about opt-in settings for
390 // event_defs our users encounter
391 where : {'+atevdef' : {owner : org_ids}}
395 }, {}, {atomic : true}).then(function(setting_types) {
397 angular.forEach(setting_types, function(stype) {
398 service.user_setting_types[stype.name()] = stype;
399 if (static_types.indexOf(stype.name()) == -1) {
400 service.opt_in_setting_types[stype.name()] = stype;
404 if (service.patron_id) {
405 // retrieve applied values for the current user
406 // for the setting types we care about.
409 setting_types.map(function(obj) { return obj.name() });
411 return egCore.net.request(
413 'open-ils.actor.patron.settings.retrieve.authoritative',
417 ).then(function(settings) {
418 service.user_settings = settings;
422 // apply default user setting values
423 angular.forEach(setting_types, function(stype, index) {
424 if (stype.reg_default() != undefined) {
425 service.user_settings[setting.name()] =
426 setting.reg_default();
433 service.invalidate_field = function(patron, field) {
434 console.log('Invalidating patron field ' + field);
436 return egCore.net.request(
438 'open-ils.actor.invalidate.' + field,
439 egCore.auth.token(), patron.id, null, patron.home_ou.id()
441 ).then(function(res) {
442 // clear the invalid value from the form
445 // update last_xact_id so future save operations
446 // on this patron will be allowed
447 patron.last_xact_id = res.payload.last_xact_id[patron.id];
451 service.dupe_patron_search = function(patron, type, value) {
454 console.log('Dupe search called with "' +
455 type +"' and value " + value);
460 var fname = patron.first_given_name;
461 var lname = patron.family_name;
462 if (!(fname && lname)) return;
464 first_given_name : {value : fname, group : 0},
465 family_name : {value : lname, group : 0}
470 search = {email : {value : value, group : 0}};
474 search = {ident : {value : value, group : 2}};
478 search = {phone : {value : value, group : 2}};
483 angular.forEach(['street1', 'street2', 'city', 'post_code'],
486 search[field] = {value : value[field], group: 1};
492 return egCore.net.request(
494 'open-ils.actor.patron.search.advanced',
495 egCore.auth.token(), search, null, null, 1
496 ).then(function(res) {
497 res = res.filter(function(id) {return id != patron.id});
505 service.init_patron = function(current) {
508 return service.init_new_patron();
510 service.patron = current;
511 return service.init_existing_patron(current)
514 service.ingest_address = function(patron, addr) {
515 addr.valid = addr.valid == 't';
516 addr.within_city_limits = addr.within_city_limits == 't';
517 addr._is_mailing = (patron.mailing_address &&
518 addr.id == patron.mailing_address.id);
519 addr._is_billing = (patron.billing_address &&
520 addr.id == patron.billing_address.id);
524 * Existing patron objects reqire some data munging before insertion
527 * 1. Turn everything into a hash
528 * 2. ... Except certain fields (selectors) whose widgets require objects
529 * 3. Bools must be Boolean, not t/f.
531 service.init_existing_patron = function(current) {
533 var patron = egCore.idl.toHash(current);
535 patron.home_ou = egCore.org.get(patron.home_ou.id);
536 patron.expire_date = new Date(Date.parse(patron.expire_date));
537 patron.dob = service.parse_dob(patron.dob);
538 patron.profile = current.profile(); // pre-hash version
539 patron.net_access_level = current.net_access_level();
540 patron.ident_type = current.ident_type();
541 patron.groups = current.groups(); // pre-hash
544 ['juvenile', 'barred', 'active', 'master_account'],
545 function(field) { patron[field] = patron[field] == 't'; }
548 angular.forEach(patron.cards, function(card) {
549 card.active = card.active == 't';
550 if (card.id == patron.card.id) {
552 card._primary = 'on';
556 angular.forEach(patron.addresses,
557 function(addr) { service.ingest_address(patron, addr) });
559 // toss entries for existing stat cat maps into our living
560 // stat cat entry map, which is modified within the template.
561 angular.forEach(patron.stat_cat_entries, function(map) {
562 service.stat_cat_entry_maps[map.stat_cat.id] = map.stat_cat_entry;
568 service.init_new_patron = function() {
570 id : service.virt_id--,
573 address_type : egCore.strings.REG_ADDR_TYPE,
576 within_city_limits : false,
577 stat_cat_entries : []
581 id : service.virt_id--,
592 home_ou : egCore.org.get(egCore.auth.user().ws_ou()),
596 if (service.clone_user)
597 service.copy_clone_data(user);
599 if (service.stage_user)
600 service.copy_stage_data(user);
605 // dob is always YYYY-MM-DD
606 // Dates of birth do not contain timezone info, which can lead to
607 // inconcistent timezone handling, potentially representing
608 // different points in time, depending on the implementation.
609 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
610 // See "Differences in assumed time zone"
611 // TODO: move this into egDate ?
612 service.parse_dob = function(dob) {
613 if (!dob) return null;
614 var parts = dob.split('-');
615 var d = new Date(); // always local time zone, yay.
616 d.setFullYear(parts[0]);
617 d.setMonth(parts[1]);
622 service.copy_stage_data = function(user) {
623 var cuser = service.stage_user;
625 // copy the data into our new user object
627 for (var key in egCore.idl.classes.stgu.field_map) {
628 if (egCore.idl.classes.au.field_map[key] &&
629 !egCore.idl.classes.stgu.field_map[key].virtual) {
630 if (cuser.user[key]() !== null)
631 user[key] = cuser.user[key]();
635 if (user.home_ou) user.home_ou = egCore.org.get(user.home_ou);
636 if (user.profile) user.profile = egCore.env.pgt.map[user.profile];
638 user.ident_type = egCore.env.cit.map[user.ident_type];
639 user.dob = service.parse_dob(user.dob);
641 // Clear the usrname if it looks like a UUID
642 if (user.usrname.replace(/-/g,'').match(/[0-9a-f]{32}/))
645 // Don't use stub address if we have one from the staged user.
646 if (cuser.mailing_addresses.length || cuser.billing_addresses.length)
649 // is_mailing=false implies is_billing
650 function addr_from_stage(stage_addr) {
651 if (!stage_addr) return;
652 var cls = stage_addr.classname;
655 id : service.virt_id--,
659 _is_mailing : cls == 'stgma',
660 _is_billing : cls == 'stgba'
663 user.mailing_address = addr;
664 user.addresses.push(addr);
666 for (var key in egCore.idl.classes[cls].field_map) {
667 if (egCore.idl.classes.aua.field_map[key] &&
668 !egCore.idl.classes[cls].field_map[key].virtual) {
669 if (stage_addr[key]() !== null)
670 addr[key] = stage_addr[key]();
675 addr_from_stage(cuser.mailing_addresses[0]);
676 addr_from_stage(cuser.billing_addresses[0]);
678 if (user.addresses.length == 1) {
679 // If there is only one address,
680 // use it as both mailing and billing.
681 var addr = user.addresses[0];
682 addr._is_mailing = addr._is_billing = true;
683 user.mailing_address = user.billing_address = addr;
686 if (cuser.cards.length) {
688 id : service.virt_id--,
689 barcode : cuser.cards[0].barcode(),
695 user.cards.push(user.card);
696 if (user.usrname == '')
697 user.usrname = card.barcode;
701 // copy select values from the cloned user to the new user.
703 service.copy_clone_data = function(user) {
704 var clone_user = service.clone_user;
706 // flesh the home org locally
707 user.home_ou = egCore.org.get(clone_user.home_ou());
708 if (user.profile) user.profile = egCore.env.pgt.map[user.profile];
710 if (!clone_user.billing_address() &&
711 !clone_user.mailing_address())
712 return; // no addresses to copy or link
714 // if the cloned user has any addresses, we don't need
715 // the stub address created in init_new_patron.
719 service.org_settings['circ.patron_edit.clone.copy_address'];
728 angular.forEach(clone_fields, function(field) {
729 user[field] = clone_user[field]();
732 if (copy_addresses) {
733 var bill_addr, mail_addr;
735 // copy the billing and mailing addresses into new addresses
736 function clone_addr(addr) {
737 var new_addr = egCore.idl.toHash(addr);
738 new_addr.id = service.virt_id--;
739 new_addr.usr = user.id;
740 new_addr.isnew = true;
741 new_addr.valid = true;
742 user.addresses.push(new_addr);
746 if (bill_addr = clone_user.billing_address()) {
747 var addr = clone_addr(bill_addr);
748 addr._is_billing = true;
749 user.billing_address = addr;
752 if (mail_addr = clone_user.mailing_address()) {
754 if (bill_addr && bill_addr.id() == mail_addr.id()) {
755 user.mailing_address = user.billing_address;
756 user.mailing_address._is_mailing = true;
758 var addr = clone_addr(mail_addr);
759 addr._is_mailing = true;
760 user.mailing_address = addr;
764 // if there is no billing addr, use the mailing addr
765 user.billing_address = user.mailing_address;
766 user.billing_address._is_billing = true;
773 // link the billing and mailing addresses
775 if (addr = clone_user.billing_address()) {
776 user.billing_address = egCore.idl.toHash(addr);
777 user.billing_address._is_billing = true;
778 user.addresses.push(user.billing_address);
779 user.billing_address._linked_owner_id = clone_user.id();
780 user.billing_address._linked_owner = service.format_name(
781 clone_user.family_name(),
782 clone_user.first_given_name(),
783 clone_user.second_given_name()
787 if (addr = clone_user.mailing_address()) {
788 if (user.billing_address &&
789 addr.id() == user.billing_address.id) {
790 // mailing matches billing
791 user.mailing_address = user.billing_address;
792 user.mailing_address._is_mailing = true;
794 user.mailing_address = egCore.idl.toHash(addr);
795 user.mailing_address._is_mailing = true;
796 user.addresses.push(user.mailing_address);
797 user.mailing_address._linked_owner_id = clone_user.id();
798 user.mailing_address._linked_owner = service.format_name(
799 clone_user.family_name(),
800 clone_user.first_given_name(),
801 clone_user.second_given_name()
808 // translate the patron back into IDL form
809 service.save_user = function(phash) {
811 var patron = egCore.idl.fromHash('au', phash);
813 patron.home_ou(patron.home_ou().id());
814 patron.expire_date(patron.expire_date().toISOString());
815 patron.profile(patron.profile().id());
817 patron.dob(patron.dob().toISOString().replace(/T.*/,''));
818 if (patron.ident_type())
819 patron.ident_type(patron.ident_type().id());
820 if (patron.net_access_level())
821 patron.net_access_level(patron.net_access_level().id());
824 ['juvenile', 'barred', 'active', 'master_account'],
825 function(field) { patron[field](phash[field] ? 't' : 'f'); }
828 var card_hashes = patron.cards();
830 angular.forEach(card_hashes, function(chash) {
831 var card = egCore.idl.fromHash('ac', chash)
832 card.usr(patron.id());
833 card.active(chash.active ? 't' : 'f');
834 patron.cards().push(card);
835 if (chash._primary) {
840 var addr_hashes = patron.addresses();
841 patron.addresses([]);
842 angular.forEach(addr_hashes, function(addr_hash) {
843 if (!addr_hash.isnew && !addr_hash.isdeleted)
844 addr_hash.ischanged = true;
845 var addr = egCore.idl.fromHash('aua', addr_hash);
846 patron.addresses().push(addr);
847 addr.valid(addr.valid() ? 't' : 'f');
848 addr.within_city_limits(addr.within_city_limits() ? 't' : 'f');
849 if (addr_hash._is_mailing) patron.mailing_address(addr);
850 if (addr_hash._is_billing) patron.billing_address(addr);
853 patron.survey_responses([]);
854 angular.forEach(service.survey_responses, function(answer) {
855 var question = service.survey_questions[answer.question()];
856 var resp = new egCore.idl.asvr();
858 resp.survey(question.survey());
859 resp.question(question.id());
860 resp.answer(answer.id());
861 resp.usr(patron.id());
862 resp.answer_date('now');
863 patron.survey_responses().push(resp);
866 // re-object-ify the patron stat cat entry maps
868 angular.forEach(patron.stat_cat_entries(), function(entry) {
869 var e = egCore.idl.fromHash('actscecm', entry);
870 e.stat_cat(e.stat_cat().id);
873 patron.stat_cat_entries(maps);
875 // service.stat_cat_entry_maps maps stats to values
876 // patron.stat_cat_entries is an array of stat_cat_entry_usr_map's
878 service.stat_cat_entry_maps, function(value, cat_id) {
880 // see if we already have a mapping for this entry
881 var existing = patron.stat_cat_entries().filter(
882 function(e) { return e.stat_cat() == cat_id })[0];
884 if (existing) { // we have a mapping
885 // if the existing mapping matches the new one,
886 // there' nothing left to do
887 if (existing.stat_cat_entry() == value) return;
889 // mappings differ. delete the old one and create
891 existing.isdeleted(true);
894 var newmap = new egCore.idl.actscecm();
895 newmap.target_usr(patron.id());
897 newmap.stat_cat(cat_id);
898 newmap.stat_cat_entry(value);
899 patron.stat_cat_entries().push(newmap);
902 if (!patron.isnew()) patron.ischanged(true);
904 return egCore.net.request(
906 'open-ils.actor.patron.update',
907 egCore.auth.token(), patron);
910 service.remove_staged_user = function() {
911 if (!service.stage_user) return $q.when();
912 return egCore.net.request(
914 'open-ils.actor.user.stage.delete',
916 service.stage_user.row_id()
920 service.save_user_settings = function(new_user, user_settings) {
921 // user_settings contains the values from the scope/form.
922 // service.user_settings contain the values from page load time.
925 if (service.patron_id) {
926 // only update modified settings for existing patrons
927 angular.forEach(user_settings, function(val, key) {
928 if (val !== service.user_settings[key])
933 // all non-null setting values are updated for new patrons
934 angular.forEach(user_settings, function(val, key) {
935 if (val !== null) settings[key] = val;
939 if (Object.keys(settings).length == 0) return $q.when();
941 return egCore.net.request(
943 'open-ils.actor.patron.settings.update',
944 egCore.auth.token(), new_user.id(), settings
945 ).then(function(resp) {
946 console.log('settings returned ' + resp);
955 function PatronRegCtrl($scope, $routeParams,
956 $q, $modal, $window, egCore, patronSvc, patronRegSvc, egUnloadPrompt) {
958 $scope.page_data_loaded = false;
959 $scope.clone_id = patronRegSvc.clone_id = $routeParams.clone_id;
960 $scope.stage_username =
961 patronRegSvc.stage_username = $routeParams.stage_username;
963 patronRegSvc.patron_id = $routeParams.edit_id || $routeParams.id;
965 // for existing patrons, disable barcode input by default
966 $scope.disable_bc = $scope.focus_usrname = Boolean($scope.patron_id);
967 $scope.focus_bc = !Boolean($scope.patron_id);
968 $scope.dupe_counts = {};
970 if (!$scope.edit_passthru) {
971 // in edit more, scope.edit_passthru is delivered to us by
972 // the enclosing controller. In register mode, there is
973 // no enclosing controller, so we create our own.
974 $scope.edit_passthru = {};
977 // 0=all, 1=suggested, 2=all
978 $scope.edit_passthru.vis_level = 0;
979 // TODO: add save/clone handlers here
981 $scope.field_modified = function() {
982 // Call attach with every field change, regardless of whether
983 // it's been called before. This will allow for re-attach after
984 // the user clicks through the unload warning. egUnloadPrompt
985 // will ensure we only attach once.
986 egUnloadPrompt.attach($scope);
989 // Apply default values for new patrons during initial registration
990 // prs is shorthand for patronSvc
991 function set_new_patron_defaults(prs) {
992 if (!$scope.patron.passwd) {
993 // passsword may originate from staged user.
994 $scope.generate_password();
996 $scope.hold_notify_phone = true;
997 $scope.hold_notify_email = true;
999 // staged users may be loaded w/ a profile.
1000 $scope.set_expire_date();
1002 if (prs.org_settings['ui.patron.default_ident_type']) {
1003 // $scope.patron needs this field to be an object
1004 var id = prs.org_settings['ui.patron.default_ident_type'];
1005 var ident_type = $scope.ident_types.filter(
1006 function(type) { return type.id() == id })[0];
1007 $scope.patron.ident_type = ident_type;
1009 if (prs.org_settings['ui.patron.default_inet_access_level']) {
1010 // $scope.patron needs this field to be an object
1011 var id = prs.org_settings['ui.patron.default_inet_access_level'];
1012 var level = $scope.net_access_levels.filter(
1013 function(lvl) { return lvl.id() == id })[0];
1014 $scope.patron.net_access_level = level;
1016 if (prs.org_settings['ui.patron.default_country']) {
1017 $scope.patron.addresses[0].country =
1018 prs.org_settings['ui.patron.default_country'];
1022 function handle_home_org_changed() {
1023 org_id = $scope.patron.home_ou.id();
1025 patronRegSvc.has_group_link_perms(org_id)
1026 .then(function(bool) {$scope.has_group_link_perm = bool});
1031 $scope.initTab ? // initTab comes from patron app
1032 $scope.initTab('edit', $routeParams.id) : $q.when(),
1036 ]).then(function() {
1037 // called after initTab and patronRegSvc.init have completed
1039 var prs = patronRegSvc; // brevity
1040 // in standalone mode, we have no patronSvc
1041 $scope.patron = prs.init_patron(patronSvc ? patronSvc.current : null);
1042 $scope.field_doc = prs.field_doc;
1043 $scope.edit_profiles = prs.edit_profiles;
1044 $scope.ident_types = prs.ident_types;
1045 $scope.net_access_levels = prs.net_access_levels;
1046 $scope.user_setting_types = prs.user_setting_types;
1047 $scope.opt_in_setting_types = prs.opt_in_setting_types;
1048 $scope.org_settings = prs.org_settings;
1049 $scope.sms_carriers = prs.sms_carriers;
1050 $scope.stat_cats = prs.stat_cats;
1051 $scope.surveys = prs.surveys;
1052 $scope.survey_responses = prs.survey_responses;
1053 $scope.stat_cat_entry_maps = prs.stat_cat_entry_maps;
1054 $scope.stage_user = prs.stage_user;
1055 $scope.stage_user_requestor = prs.stage_user_requestor;
1057 $scope.user_settings = prs.user_settings;
1058 // clone the user settings back into the patronRegSvc so
1059 // we have a copy of the original state of the settings.
1060 prs.user_settings = {};
1061 angular.forEach($scope.user_settings, function(val, key) {
1062 prs.user_settings[key] = val;
1065 extract_hold_notify();
1066 handle_home_org_changed();
1068 if ($scope.org_settings['ui.patron.edit.default_suggested'])
1069 $scope.edit_passthru.vis_level = 1;
1071 if ($scope.patron.isnew)
1072 set_new_patron_defaults(prs);
1074 $scope.page_data_loaded = true;
1078 // update the currently displayed field documentation
1079 $scope.set_selected_field_doc = function(cls, field) {
1080 $scope.selected_field_doc = $scope.field_doc[cls][field];
1083 // returns the tree depth of the selected profile group tree node.
1084 $scope.pgt_depth = function(grp) {
1086 while (grp = egCore.env.pgt.map[grp.parent()]) d++;
1090 // IDL fields used for labels in the UI.
1091 $scope.idl_fields = {
1092 au : egCore.idl.classes.au.field_map,
1093 ac : egCore.idl.classes.ac.field_map,
1094 aua : egCore.idl.classes.aua.field_map
1097 // field visibility cache. Some fields are universally required.
1098 var field_visibility = {
1103 'au.first_given_name' : 2,
1104 'au.family_name' : 2,
1105 'au.ident_type' : 2,
1108 'au.expire_date' : 2,
1109 'au.net_access_level' : 2,
1110 'aua.address_type' : 2,
1111 'aua.post_code' : 2,
1119 'aua.within_city_limits' : 2,
1124 // returns true if the selected field should be visible
1125 // given the current required/suggested/all setting.
1126 $scope.show_field = function(field_key) {
1128 if (field_visibility[field_key] == undefined) {
1129 // compile and cache the visibility for the selected field
1131 // org settings have not been received yet.
1132 if (!$scope.org_settings) return false;
1134 var req_set = 'ui.patron.edit.' + field_key + '.require';
1135 var sho_set = 'ui.patron.edit.' + field_key + '.show';
1136 var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
1138 if ($scope.org_settings[req_set]) {
1139 field_visibility[field_key] = 2;
1140 } else if ($scope.org_settings[sho_set]) {
1141 field_visibility[field_key] = 2;
1142 } else if ($scope.org_settings[sug_set]) {
1143 field_visibility[field_key] = 1;
1145 field_visibility[field_key] = 0;
1149 return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
1152 // generates a random 4-digit password
1153 $scope.generate_password = function() {
1154 $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
1157 $scope.set_expire_date = function() {
1158 if (!$scope.patron.profile) return;
1159 var seconds = egCore.date.intervalToSeconds(
1160 $scope.patron.profile.perm_interval());
1161 var now_epoch = new Date().getTime();
1162 $scope.patron.expire_date = new Date(
1163 now_epoch + (seconds * 1000 /* milliseconds */))
1166 // grp is the pgt object
1167 $scope.set_profile = function(grp) {
1168 $scope.patron.profile = grp;
1169 $scope.set_expire_date();
1170 $scope.field_modified();
1173 $scope.new_address = function() {
1174 var addr = egCore.idl.toHash(new egCore.idl.aua());
1175 patronRegSvc.ingest_address($scope.patron, addr);
1176 addr.id = patronRegSvc.virt_id--;
1179 addr.within_city_limits = true;
1180 $scope.patron.addresses.push(addr);
1183 // keep deleted addresses out of the patron object so
1184 // they won't appear in the UI. They'll be re-inserted
1185 // when the patron is updated.
1186 deleted_addresses = [];
1187 $scope.delete_address = function(id) {
1189 angular.forEach($scope.patron.addresses, function(addr) {
1190 if (addr.id == id) {
1192 addr.isdeleted = true;
1193 deleted_addresses.push(addr);
1196 addresses.push(addr);
1199 $scope.patron.addresses = addresses;
1202 $scope.post_code_changed = function(addr) {
1204 'open-ils.search', 'open-ils.search.zip', addr.post_code)
1205 .then(function(resp) {
1207 if (resp.city) addr.city = resp.city;
1208 if (resp.state) addr.state = resp.state;
1209 if (resp.county) addr.county = resp.county;
1210 if (resp.alert) alert(resp.alert);
1214 $scope.replace_card = function() {
1215 $scope.patron.card.active = false;
1216 $scope.patron.card.ischanged = true;
1217 $scope.disable_bc = false;
1219 var new_card = egCore.idl.toHash(new egCore.idl.ac());
1220 new_card.id = patronRegSvc.virt_id--;
1221 new_card.isnew = true;
1222 new_card.active = true;
1223 new_card._primary = 'on';
1224 $scope.patron.card = new_card;
1225 $scope.patron.cards.push(new_card);
1228 $scope.day_phone_changed = function(phone) {
1229 if (phone && $scope.patron.isnew &&
1230 $scope.org_settings['patron.password.use_phone']) {
1231 $scope.patron.passwd = phone.substr(-4);
1235 $scope.barcode_changed = function(bc) {
1239 'open-ils.actor.barcode.exists',
1240 egCore.auth.token(), bc
1241 ).then(function(resp) {
1243 console.log('duplicate barcode detected: ' + bc);
1246 if (!$scope.patron.usrname)
1247 $scope.patron.usrname = bc;
1253 $scope.cards_dialog = function() {
1255 templateUrl: './circ/patron/t_patron_cards_dialog',
1257 ['$scope','$modalInstance','cards',
1258 function($scope , $modalInstance , cards) {
1259 // scope here is the modal-level scope
1260 $scope.args = {cards : cards};
1261 $scope.ok = function() { $modalInstance.close($scope.args) }
1262 $scope.cancel = function () { $modalInstance.dismiss() }
1265 cards : function() {
1266 // scope here is the controller-level scope
1267 return $scope.patron.cards;
1272 angular.forEach(args.cards, function(card) {
1273 card.ischanged = true; // assume cards need updating, OK?
1274 if (card._primary == 'on' &&
1275 card.id != $scope.patron.card.id) {
1276 $scope.patron.card = card;
1283 $scope.set_addr_type = function(addr, type) {
1284 var addrs = $scope.patron.addresses;
1285 if (addr['_is_'+type]) {
1286 angular.forEach(addrs, function(a) {
1287 if (a.id != addr.id) a['_is_'+type] = false;
1290 // unchecking mailing/billing means we have to randomly
1291 // select another address to fill that role. Select the
1292 // first address in the list (that does not match the
1294 for (var i = 0; i < addrs.length; i++) {
1295 if (addrs[i].id != addr.id) {
1296 addrs[i]['_is_' + type] = true;
1304 // Translate hold notify preferences from the form/scope back into a
1305 // single user setting value for opac.hold_notify.
1306 function compress_hold_notify() {
1307 var hold_notify = '';
1309 if ($scope.hold_notify_phone) {
1310 hold_notify = 'phone';
1313 if ($scope.hold_notify_email) {
1314 hold_notify = splitter + 'email';
1317 if ($scope.hold_notify_sms) {
1318 hold_notify = splitter + 'sms';
1321 $scope.user_settings['opac.hold_notify'] = hold_notify;
1324 // dialog for selecting additional permission groups
1325 $scope.secondary_groups_dialog = function() {
1327 templateUrl: './circ/patron/t_patron_groups_dialog',
1329 ['$scope','$modalInstance','linked_groups','pgt_depth',
1330 function($scope , $modalInstance , linked_groups , pgt_depth) {
1332 $scope.pgt_depth = pgt_depth;
1334 linked_groups : linked_groups,
1335 edit_profiles : patronRegSvc.edit_profiles,
1336 new_profile : patronRegSvc.edit_profiles[0]
1339 // add a new group to the linked groups list
1340 $scope.link_group = function($event, grp) {
1341 var found = false; // avoid duplicates
1342 angular.forEach($scope.args.linked_groups,
1343 function(g) {if (g.id() == grp.id()) found = true});
1344 if (!found) $scope.args.linked_groups.push(grp);
1345 $event.preventDefault(); // avoid close
1348 // remove a group from the linked groups list
1349 $scope.unlink_group = function($event, grp) {
1350 $scope.args.linked_groups =
1351 $scope.args.linked_groups.filter(function(g) {
1352 return g.id() != grp.id()
1354 $event.preventDefault(); // avoid close
1357 $scope.ok = function() { $modalInstance.close($scope.args) }
1358 $scope.cancel = function () { $modalInstance.dismiss() }
1361 linked_groups : function() { return $scope.patron.groups },
1362 pgt_depth : function() { return $scope.pgt_depth }
1366 var ids = args.linked_groups.map(function(g) {return g.id()});
1367 console.log('linking permission groups ' + ids);
1368 return egCore.net.request(
1370 'open-ils.actor.user.set_groups',
1371 egCore.auth.token(), $scope.patron.id, ids)
1372 .then(function(resp) {
1374 $scope.patron.groups = args.linked_groups;
1376 // debugging -- should be no events
1377 alert('linked groups failure ' + egCore.evt.parse(resp));
1384 function extract_hold_notify() {
1385 notify = $scope.user_settings['opac.hold_notify'];
1386 if (!notify) return;
1387 $scope.hold_notify_phone = Boolean(notify.match(/phone/));
1388 $scope.hold_notify_email = Boolean(notify.match(/email/));
1389 $scope.hold_notify_sms = Boolean(notify.match(/sms/));
1392 $scope.invalidate_field = function(field) {
1393 patronRegSvc.invalidate_field($scope.patron, field);
1396 $scope.dupe_value_changed = function(type, value) {
1397 $scope.dupe_counts[type] = 0;
1398 patronRegSvc.dupe_patron_search($scope.patron, type, value)
1399 .then(function(res) {
1400 $scope.dupe_counts[type] = res.count;
1401 $scope.dupe_search_encoded =
1402 encodeURIComponent(js2JSON(res.search));
1406 $scope.edit_passthru.save = function(save_args) {
1407 if (!save_args) save_args = {};
1409 // remove page unload warning prompt
1410 egUnloadPrompt.clear();
1412 // toss the deleted addresses back into the patron's list of
1413 // addresses so it's included in the update
1414 $scope.patron.addresses =
1415 $scope.patron.addresses.concat(deleted_addresses);
1417 compress_hold_notify();
1421 patronRegSvc.save_user($scope.patron)
1422 .then(function(new_user) {
1423 if (new_user && new_user.classname) {
1424 updated_user = new_user;
1425 return patronRegSvc.save_user_settings(
1426 new_user, $scope.user_settings);
1428 alert('Patron update failed. \n\n' + js2JSON(new_user));
1431 }).then(function() {
1433 // only remove the staged user if the update succeeded.
1435 return patronRegSvc.remove_staged_user();
1437 }).then(function() {
1439 // reloading the page means potentially losing some information
1440 // (e.g. last patron search), but is the only way to ensure all
1441 // components are properly updated to reflect the modified patron.
1442 if (updated_user && save_args.clone) {
1443 // open a separate tab for registering a new
1444 // patron from our cloned data.
1445 var url = 'https://'
1446 + $window.location.hostname
1447 + egCore.env.basePath
1448 + '/circ/patron/register/clone/'
1449 + updated_user.id();
1450 $window.open(url, '_blank').focus();
1453 // reload the current page
1454 $window.location.href = location.href;
1460 // This controller may be loaded from different modules (patron edit vs.
1461 // register new patron), so we have to inject the controller params manually.
1462 PatronRegCtrl.$inject = ['$scope', '$routeParams', '$q', '$modal',
1463 '$window', 'egCore', 'patronSvc', 'patronRegSvc', 'egUnloadPrompt'];