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
11 user_settings : {}, // applied user settings
12 user_setting_types : {}, // config.usr_setting_type
13 virt_id : -1 // virtual ID for new objects
16 // launch a series of parallel data retrieval calls
17 service.init = function(scope) {
19 service.get_field_doc(),
20 service.get_perm_groups(),
21 service.get_ident_types(),
22 service.get_user_settings(),
23 service.get_org_settings(),
24 service.get_stat_cats(),
25 service.get_surveys(),
26 service.get_net_access_levels()
30 service.get_surveys = function() {
31 var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
33 return egCore.pcrud.search('asv',
35 {flesh : 1, flesh_fields : {asv : ['questions']}},
37 ).then(function(surveys) {
38 service.surveys = surveys;
42 service.get_stat_cats = function() {
43 return egCore.net.request(
45 'open-ils.circ.stat_cat.actor.retrieve.all',
46 egCore.auth.token(), egCore.auth.user().ws_ou()
47 ).then(function(cats) {
48 service.stat_cats = cats;
52 service.get_org_settings = function() {
53 return egCore.org.settings([
54 'global.password_regex',
55 'global.juvenile_age_threshold',
56 'patron.password.use_phone',
57 'ui.patron.default_inet_access_level',
58 'ui.patron.default_ident_type',
59 'ui.patron.default_country',
60 'ui.patron.registration.require_address',
61 'circ.holds.behind_desk_pickup_supported',
62 'circ.patron_edit.clone.copy_address',
63 'ui.patron.edit.au.prefix.require',
64 'ui.patron.edit.au.prefix.show',
65 'ui.patron.edit.au.prefix.suggest',
66 'ui.patron.edit.ac.barcode.regex',
67 'ui.patron.edit.au.second_given_name.show',
68 'ui.patron.edit.au.second_given_name.suggest',
69 'ui.patron.edit.au.suffix.show',
70 'ui.patron.edit.au.suffix.suggest',
71 'ui.patron.edit.au.alias.show',
72 'ui.patron.edit.au.alias.suggest',
73 'ui.patron.edit.au.dob.require',
74 'ui.patron.edit.au.dob.show',
75 'ui.patron.edit.au.dob.suggest',
76 'ui.patron.edit.au.dob.calendar',
77 'ui.patron.edit.au.juvenile.show',
78 'ui.patron.edit.au.juvenile.suggest',
79 'ui.patron.edit.au.ident_value.show',
80 'ui.patron.edit.au.ident_value.suggest',
81 'ui.patron.edit.au.ident_value2.show',
82 'ui.patron.edit.au.ident_value2.suggest',
83 'ui.patron.edit.au.email.require',
84 'ui.patron.edit.au.email.show',
85 'ui.patron.edit.au.email.suggest',
86 'ui.patron.edit.au.email.regex',
87 'ui.patron.edit.au.email.example',
88 'ui.patron.edit.au.day_phone.require',
89 'ui.patron.edit.au.day_phone.show',
90 'ui.patron.edit.au.day_phone.suggest',
91 'ui.patron.edit.au.day_phone.regex',
92 'ui.patron.edit.au.day_phone.example',
93 'ui.patron.edit.au.evening_phone.require',
94 'ui.patron.edit.au.evening_phone.show',
95 'ui.patron.edit.au.evening_phone.suggest',
96 'ui.patron.edit.au.evening_phone.regex',
97 'ui.patron.edit.au.evening_phone.example',
98 'ui.patron.edit.au.other_phone.require',
99 'ui.patron.edit.au.other_phone.show',
100 'ui.patron.edit.au.other_phone.suggest',
101 'ui.patron.edit.au.other_phone.regex',
102 'ui.patron.edit.au.other_phone.example',
103 'ui.patron.edit.phone.regex',
104 'ui.patron.edit.phone.example',
105 'ui.patron.edit.au.active.show',
106 'ui.patron.edit.au.active.suggest',
107 'ui.patron.edit.au.barred.show',
108 'ui.patron.edit.au.barred.suggest',
109 'ui.patron.edit.au.master_account.show',
110 'ui.patron.edit.au.master_account.suggest',
111 'ui.patron.edit.au.claims_returned_count.show',
112 'ui.patron.edit.au.claims_returned_count.suggest',
113 'ui.patron.edit.au.claims_never_checked_out_count.show',
114 'ui.patron.edit.au.claims_never_checked_out_count.suggest',
115 'ui.patron.edit.au.alert_message.show',
116 'ui.patron.edit.au.alert_message.suggest',
117 'ui.patron.edit.aua.post_code.regex',
118 'ui.patron.edit.aua.post_code.example',
119 'ui.patron.edit.aua.county.require',
121 'ui.patron.edit.default_suggested',
122 'opac.barcode_regex',
123 'opac.username_regex',
125 'ui.patron.edit.aua.state.require',
126 'ui.patron.edit.aua.state.suggest',
127 'ui.patron.edit.aua.state.show'
128 ]).then(function(settings) {
129 service.org_settings = settings;
130 return service.process_org_settings(settings);
134 // some org settings require the retrieval of additional data
135 service.process_org_settings = function(settings) {
139 if (settings['sms.enable']) {
140 // fetch SMS carriers
142 egCore.pcrud.search('csc',
145 {'class':'csc', 'field':'name'},
146 {'class':'csc', 'field':'region'}
148 ).then(function(carriers) {
149 service.sms_carriers = carriers;
153 // if other promises are added below, this is not necessary.
154 promises.push($q.when());
157 // other post-org-settings processing goes here,
158 // adding to promises as needed.
160 return $q.all(promises);
163 service.get_ident_types = function() {
164 return egCore.pcrud.retrieveAll('cit', {}, {atomic : true})
165 .then(function(types) { service.ident_types = types });
168 service.get_net_access_levels = function() {
169 return egCore.pcrud.retrieveAll('cnal', {}, {atomic : true})
170 .then(function(levels) { service.net_access_levels = levels });
173 service.get_perm_groups = function() {
174 if (egCore.env.pgt) {
175 service.profiles = egCore.env.pgt.list;
178 return egCore.pcrud.search('pgt', {parent : null},
179 {flesh : -1, flesh_fields : {pgt : ['children']}}
182 egCore.env.absorbTree(tree, 'pgt')
183 service.profiles = egCore.env.pgt.list;
189 service.get_field_doc = function() {
191 return egCore.pcrud.search('fdoc', {
192 fm_class: ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva']})
193 .then(null, null, function(doc) {
194 if (!service.field_doc[doc.fm_class()]) {
195 service.field_doc[doc.fm_class()] = {};
197 service.field_doc[doc.fm_class()][doc.field()] = doc;
201 service.get_user_settings = function() {
202 var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
204 return egCore.pcrud.search('cust', {
206 {name : [ // common user settings
207 'circ.holds_behind_desk',
208 'circ.collections.exempt',
210 'opac.default_phone',
211 'opac.default_pickup_location',
212 'opac.default_sms_carrier',
213 'opac.default_sms_notify']},
214 {name : { // opt-in notification user settings
216 select : {atevdef : ['opt_in_setting']},
218 // we only care about opt-in settings for
219 // event_defs our users encounter
220 where : {'+atevdef' : {owner : org_ids}}
224 }, {}, {atomic : true}).then(function(setting_types) {
226 angular.forEach(setting_types, function(stype) {
227 service.user_setting_types[stype.name()] = stype;
230 if (service.patron_id) {
231 // retrieve applied values for the current user
232 // for the setting types we care about.
235 setting_types.map(function(obj) { return obj.name() });
237 return egCore.net.request(
239 'open-ils.actor.patron.settings.retrieve.authoritative',
243 ).then(function(settings) {
244 service.user_settings = settings;
248 // apply default user setting values
249 angular.forEach(setting_types, function(stype, index) {
250 if (stype.reg_default() != undefined) {
251 service.user_settings[setting.name()] =
252 setting.reg_default();
259 service.init_patron = function(current) {
262 return service.init_new_patron();
264 service.patron = current;
265 return service.init_existing_patron(current)
268 service.ingest_address = function(patron, addr) {
269 addr.valid = addr.valid == 't';
270 addr.within_city_limits = addr.within_city_limits == 't';
271 addr._is_mailing = (patron.mailing_address &&
272 addr.id == patron.mailing_address.id);
273 addr._is_billing = (patron.billing_address &&
274 addr.id == patron.billing_address.id);
278 * Existing patron objects reqire some data munging before insertion
281 * 1. Turn everything into a hash
282 * 2. ... Except certain fields (selectors) whose widgets require objects
283 * 3. Bools must be Boolean, not t/f.
285 service.init_existing_patron = function(current) {
287 var patron = egCore.idl.toHash(current);
289 patron.home_ou = egCore.org.get(patron.home_ou.id);
290 patron.expire_date = new Date(Date.parse(patron.expire_date));
291 patron.dob = new Date(Date.parse(patron.dob));
292 patron.profile = current.profile(); // pre-hash version
293 patron.net_access_level = current.net_access_level();
294 patron.ident_type = current.ident_type();
297 ['juvenile', 'barred', 'active', 'master_account'],
298 function(field) { patron[field] = patron[field] == 't'; }
301 angular.forEach(patron.cards, function(card) {
302 card.active = card.active == 't';
303 if (card.id == patron.card.id) {
305 card._primary = 'on';
309 angular.forEach(patron.addresses,
310 function(addr) { service.ingest_address(patron, addr) });
315 service.init_new_patron = function() {
317 id : service.virt_id--,
320 address_type : egCore.strings.REG_ADDR_TYPE,
323 within_city_limits : true
327 id : service.virt_id--,
338 home_ou : egCore.org.get(egCore.auth.user().ws_ou()),
340 // TODO default profile group?
345 // translate the patron back into IDL form
346 service.save_user = function(phash) {
348 var patron = egCore.idl.fromHash('au', phash);
350 patron.home_ou(patron.home_ou().id());
352 patron.expire_date().toISOString().replace(/T.*/,''));
353 patron.profile(patron.profile().id());
355 patron.dob(patron.dob().toISOString().replace(/T.*/,''));
356 if (patron.ident_type())
357 patron.ident_type(patron.ident_type().id());
358 if (patron.net_access_level())
359 patron.net_access_level(patron.net_access_level().id());
362 ['juvenile', 'barred', 'active', 'master_account'],
363 function(field) { patron[field](phash[field] ? 't' : 'f'); }
366 var card_hashes = patron.cards();
368 angular.forEach(card_hashes, function(chash) {
369 var card = egCore.idl.fromHash('ac', chash)
370 card.usr(patron.id());
371 card.active(chash.active ? 't' : 'f');
372 patron.cards().push(card);
373 if (chash._primary) {
378 var addr_hashes = patron.addresses();
379 patron.addresses([]);
380 angular.forEach(addr_hashes, function(addr_hash) {
381 if (!addr_hash.isnew && !addr_hash.isdeleted)
382 addr_hash.ischanged = true;
383 var addr = egCore.idl.fromHash('aua', addr_hash);
384 patron.addresses().push(addr);
385 addr.valid(addr.valid() ? 't' : 'f');
386 addr.within_city_limits(addr.within_city_limits() ? 't' : 'f');
387 if (addr_hash._is_mailing) patron.mailing_address(addr);
388 if (addr_hash._is_billing) patron.billing_address(addr);
390 console.log('deleted? ' + addr.isdeleted());
393 console.log(patron.addresses());
395 if (!patron.isnew()) patron.ischanged(true);
397 return egCore.net.request(
399 'open-ils.actor.patron.update',
400 egCore.auth.token(), patron);
403 service.save_user_settings = function(new_user, user_settings) {
404 // user_settings contains the values from the scope/form.
405 // service.user_settings contain the values from page load time.
408 if (service.patron_id) {
409 // only update modified settings for existing patrons
410 angular.forEach(user_settings, function(val, key) {
411 if (val !== service.user_settings[key])
416 // all non-null setting values are updated for new patrons
417 angular.forEach(user_settings, function(val, key) {
418 if (val !== null) settings[key] = val;
422 if (Object.keys(settings).length == 0) return $q.when();
424 return egCore.net.request(
426 'open-ils.actor.patron.settings.update',
427 egCore.auth.token(), new_user.id(), settings
428 ).then(function(resp) {
429 console.log('settings returned ' + resp);
438 function PatronRegCtrl($scope, $routeParams,
439 $q, $modal, $window, egCore, patronSvc, patronRegSvc) {
441 $scope.clone_id = $routeParams.clone_id;
442 $scope.stage_username = $routeParams.stage_username;
444 patronRegSvc.patron_id = $routeParams.edit_id || $routeParams.id;
446 // for existing patrons, disable barcode input by default
447 $scope.disable_bc = $scope.focus_usrname = Boolean($scope.patron_id);
448 $scope.focus_bc = !Boolean($scope.patron_id);
450 if (!$scope.edit_passthru) {
451 // in edit more, scope.edit_passthru is delivered to us by
452 // the enclosing controller. In register mode, there is
453 // no enclosing controller, so we create our own.
454 $scope.edit_passthru = {};
457 // 0=all, 1=suggested, 2=all
458 $scope.edit_passthru.vis_level = 0;
459 // TODO: add save/clone handlers here
463 $scope.initTab ? // initTab comes from patron app
464 $scope.initTab('edit', $routeParams.id) : $q.when(),
469 // called after initTab and patronRegSvc.init have completed
471 var prs = patronRegSvc; // brevity
472 // in standalone mode, we have no patronSvc
473 $scope.patron = prs.init_patron(patronSvc ? patronSvc.current : null);
474 $scope.field_doc = prs.field_doc;
475 $scope.profiles = prs.profiles;
476 $scope.ident_types = prs.ident_types;
477 $scope.net_access_levels = prs.net_access_levels;
478 $scope.user_setting_types = prs.user_setting_types;
479 $scope.org_settings = prs.org_settings;
480 $scope.sms_carriers = prs.sms_carriers;
481 $scope.stat_cats = prs.stat_cats;
482 $scope.surveys = prs.surveys;
484 $scope.user_settings = prs.user_settings;
485 // clone the user settings back into the patronRegSvc so
486 // we have a copy of the original state of the settings.
487 prs.user_settings = {};
488 angular.forEach($scope.user_settings, function(val, key) {
489 prs.user_settings[key] = val;
492 extract_hold_notify();
494 if ($scope.org_settings['ui.patron.edit.default_suggested'])
495 $scope.edit_passthru.vis_level = 1;
497 if ($scope.patron.isnew) {
498 $scope.generate_password();
499 $scope.hold_notify_phone = true;
500 $scope.hold_notify_email = true;
502 if (prs.org_settings['ui.patron.default_ident_type']) {
503 $scope.patron.ident_type =
504 prs.org_settings['ui.patron.default_ident_type'];
506 if (prs.org_settings['ui.patron.default_inet_access_level']) {
507 $scope.patron.ident_type =
508 prs.org_settings['ui.patron.default_inet_access_level'];
510 if (prs.org_settings['ui.patron.default_country']) {
511 $scope.patron.addresses[0].country =
512 prs.org_settings['ui.patron.default_country'];
518 // returns the tree depth of the selected profile group tree node.
519 $scope.pgt_depth = function(grp) {
521 while (grp = egCore.env.pgt.map[grp.parent()]) d++;
525 // IDL fields used for labels in the UI.
526 $scope.idl_fields = {
527 au : egCore.idl.classes.au.field_map,
528 ac : egCore.idl.classes.ac.field_map,
529 aua : egCore.idl.classes.aua.field_map
532 // field visibility cache. Some fields are universally required.
533 var field_visibility = {
538 'au.first_given_name' : 2,
539 'au.family_name' : 2,
543 'au.expire_date' : 2,
544 'au.net_access_level' : 2,
545 'aua.address_type' : 2,
554 'aua.within_city_limits' : 2,
559 // returns true if the selected field should be visible
560 // given the current required/suggested/all setting.
561 $scope.show_field = function(field_key) {
563 if (field_visibility[field_key] == undefined) {
564 // compile and cache the visibility for the selected field
566 // org settings have not been received yet.
567 if (!$scope.org_settings) return false;
569 var req_set = 'ui.patron.edit.' + field_key + '.require';
570 var sho_set = 'ui.patron.edit.' + field_key + '.show';
571 var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
573 if ($scope.org_settings[req_set]) {
574 field_visibility[field_key] = 2;
575 } else if ($scope.org_settings[sho_set]) {
576 field_visibility[field_key] = 2;
577 } else if ($scope.org_settings[sug_set]) {
578 field_visibility[field_key] = 1;
580 field_visibility[field_key] = 0;
584 return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
587 // generates a random 4-digit password
588 $scope.generate_password = function() {
589 $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
592 $scope.set_expire_date = function() {
593 if (!$scope.patron.profile) return;
594 var seconds = egCore.date.intervalToSeconds(
595 $scope.patron.profile.perm_interval());
596 var now_epoch = new Date().getTime();
597 $scope.patron.expire_date = new Date(
598 now_epoch + (seconds * 1000 /* milliseconds */))
601 // grp is the pgt object
602 $scope.set_profile = function(grp) {
603 $scope.patron.profile = grp;
604 $scope.set_expire_date();
607 $scope.new_address = function() {
608 var addr = egCore.idl.toHash(new egCore.idl.aua());
609 patronRegSvc.ingest_address($scope.patron, addr);
610 addr.id = patronRegSvc.virt_id--;
613 addr.within_city_limits = true;
614 $scope.patron.addresses.push(addr);
617 // keep deleted addresses out of the patron object so
618 // they won't appear in the UI. They'll be re-inserted
619 // when the patron is updated.
620 deleted_addresses = [];
621 $scope.delete_address = function(id) {
623 angular.forEach($scope.patron.addresses, function(addr) {
626 addr.isdeleted = true;
627 deleted_addresses.push(addr);
630 addresses.push(addr);
633 $scope.patron.addresses = addresses;
636 $scope.post_code_changed = function(addr) {
638 'open-ils.search', 'open-ils.search.zip', addr.post_code)
639 .then(function(resp) {
641 if (resp.city) addr.city = resp.city;
642 if (resp.state) addr.state = resp.state;
643 if (resp.county) addr.county = resp.county;
644 if (resp.alert) alert(resp.alert);
648 $scope.replace_card = function() {
649 $scope.patron.card.active = false;
650 $scope.patron.card.ischanged = true;
651 $scope.disable_bc = false;
653 var new_card = egCore.idl.toHash(new egCore.idl.ac());
654 new_card.id = patronRegSvc.virt_id--;
655 new_card.isnew = true;
656 new_card.active = true;
657 new_card._primary = 'on';
658 $scope.patron.card = new_card;
659 $scope.patron.cards.push(new_card);
662 $scope.barcode_changed = function(bc) {
666 'open-ils.actor.barcode.exists',
667 egCore.auth.token(), bc
668 ).then(function(resp) {
670 console.log('duplicate barcode detected: ' + bc);
673 if (!$scope.patron.usrname)
674 $scope.patron.usrname = bc;
680 $scope.cards_dialog = function() {
682 templateUrl: './circ/patron/t_patron_cards_dialog',
684 ['$scope','$modalInstance','cards',
685 function($scope , $modalInstance , cards) {
686 // scope here is the modal-level scope
687 $scope.args = {cards : cards};
688 $scope.ok = function() { $modalInstance.close($scope.args) }
689 $scope.cancel = function () { $modalInstance.dismiss() }
693 // scope here is the controller-level scope
694 return $scope.patron.cards;
699 angular.forEach(args.cards, function(card) {
700 card.ischanged = true; // assume cards need updating, OK?
701 if (card._primary == 'on' &&
702 card.id != $scope.patron.card.id) {
703 $scope.patron.card = card;
710 $scope.set_addr_type = function(addr, type) {
711 var addrs = $scope.patron.addresses;
712 if (addr['_is_'+type]) {
713 angular.forEach(addrs, function(a) {
714 if (a.id != addr.id) a['_is_'+type] = false;
717 // unchecking mailing/billing means we have to randomly
718 // select another address to fill that role. Select the
719 // first address in the list (that does not match the
721 for (var i = 0; i < addrs.length; i++) {
722 if (addrs[i].id != addr.id) {
723 addrs[i]['_is_' + type] = true;
731 // Translate hold notify preferences from the form/scope back into a
732 // single user setting value for opac.hold_notify.
733 function compress_hold_notify() {
734 var hold_notify = '';
736 if ($scope.hold_notify_phone) {
737 hold_notify = 'phone';
740 if ($scope.hold_notify_email) {
741 hold_notify = splitter + 'email';
744 if ($scope.hold_notify_sms) {
745 hold_notify = splitter + 'sms';
748 $scope.user_settings['opac.hold_notify'] = hold_notify;
751 function extract_hold_notify() {
752 notify = $scope.user_settings['opac.hold_notify'];
754 $scope.hold_notify_phone = Boolean(notify.match(/phone/));
755 $scope.hold_notify_email = Boolean(notify.match(/email/));
756 $scope.hold_notify_sms = Boolean(notify.match(/sms/));
759 $scope.edit_passthru.save = function() {
761 // toss the deleted addresses back into the patron's list of
762 // addresses so it's included in the update
763 $scope.patron.addresses =
764 $scope.patron.addresses.concat(deleted_addresses);
766 compress_hold_notify();
768 patronRegSvc.save_user($scope.patron)
769 .then(function(new_user) {
770 if (new_user && new_user.classname) {
771 return patronRegSvc.save_user_settings(
772 new_user, $scope.user_settings);
774 alert('Patron update failed. \n\n' + js2JSON(new_user));
775 return true; // ensure page reloads to reset
777 }).then(function(keep_going) {
778 // reloading the page means potentially losing some information
779 // (e.g. last patron search), but is the only way to ensure all
780 // components are properly updated to reflect the modified patron.
781 $window.location.href = location.href;
786 // This controller may be loaded from different modules (patron edit vs.
787 // register new patron), so we have to inject the controller params manually.
788 PatronRegCtrl.$inject = ['$scope', '$routeParams', '$q', '$modal',
789 '$window', 'egCore', 'patronSvc', 'patronRegSvc'];