From f2538c6bd0dd14579a985d20025c81c2120c8f59 Mon Sep 17 00:00:00 2001 From: Cesar Velez Date: Tue, 23 May 2017 16:36:44 -0400 Subject: [PATCH] LP#1098685: Require OPAC patron holds w/ phone/SMS notification to enter that info This provides some plain vanilla JS form validation, and error feedback using a yellow background color on the culprit input field. Signed-off by: Cesar Velez Signed-off-by: Chris Sharp Signed-off-by: Galen Charlton Conflicts: Open-ILS/src/templates/opac/parts/js.tt2 --- Open-ILS/src/templates/opac/parts/js.tt2 | 31 ++------ .../src/templates/opac/parts/place_hold.tt2 | 8 +- .../js/ui/default/opac/holds-validation.js | 76 +++++++++++++++++++ 3 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 Open-ILS/web/js/ui/default/opac/holds-validation.js diff --git a/Open-ILS/src/templates/opac/parts/js.tt2 b/Open-ILS/src/templates/opac/parts/js.tt2 index 4d5b400f31..2e878c784d 100644 --- a/Open-ILS/src/templates/opac/parts/js.tt2 +++ b/Open-ILS/src/templates/opac/parts/js.tt2 @@ -59,6 +59,13 @@ IF CGI.https; url = url.replace('^http:', 'https:'); END; %] [%- END %] + + +[% IF ctx.page == 'place_hold' %] + +[% END %] + [%- IF want_dojo; -%] - -[% IF ctx.page == 'place_hold' %] - -[% END %] - [%- END; # want_dojo -%] diff --git a/Open-ILS/src/templates/opac/parts/place_hold.tt2 b/Open-ILS/src/templates/opac/parts/place_hold.tt2 index 8bf17134e6..9ca96f98ed 100644 --- a/Open-ILS/src/templates/opac/parts/place_hold.tt2 +++ b/Open-ILS/src/templates/opac/parts/place_hold.tt2 @@ -23,7 +23,7 @@ ELSE; some_holds_allowed = 1; END; END %] -
0 AND enable.radio.parts == 'true' %] onsubmit="return validateHoldForm()" [% END %] > + [% redirect = CGI.param('hold_source_page') || CGI.param('redirect_to') || CGI.referer; @@ -175,7 +175,7 @@

[% l('Notify when hold is ready for pickup?') %]

-
@@ -183,7 +183,7 @@ ELSE; l('Email Address:') %] [% ctx.user.email %][% END %]
[%- IF allow_phone_notifications == 'true' %] -
@@ -193,7 +193,7 @@
[%- END -%] [% IF ctx.get_org_setting(ctx.search_ou, 'sms.enable') == 1 %] -
diff --git a/Open-ILS/web/js/ui/default/opac/holds-validation.js b/Open-ILS/web/js/ui/default/opac/holds-validation.js new file mode 100644 index 0000000000..842366c50e --- /dev/null +++ b/Open-ILS/web/js/ui/default/opac/holds-validation.js @@ -0,0 +1,76 @@ +/* JS form validation for holds page alert methods */ +function resetBackgrounds(names){ + for (var key in names) { + if (names.hasOwnProperty(key)) { + var l = document.getElementsByName(names[key]); + if (l.length > 0) { + l[0].style.backgroundColor = ""; + } + } + } +} + +function validateMethodSelections (alertMethodCboxes) { + var needsPhone = false; + var hasPhone = false; + + var needsEmail = false; + var hasEmail = false; + + var needsSms = false; + var hasSms = false; + var inputNames = { e: "email_address", ph: "phone_notify", sms: "sms_notify", carrier: "sms_carrier"}; + resetBackgrounds(inputNames); + + //Array.from(alertMethodCboxes).forEach(function(cbox){ + for (var i = 0; i < alertMethodCboxes.length; i++){ + var cbox = alertMethodCboxes[i]; + if (cbox.checked && !cbox.disabled) { + switch(cbox.id){ + case "email_notify_checkbox": + needsEmail = true; + hasEmail = document.getElementsByName(inputNames.e)[0].innerHTML !== ""; + break; + case "phone_notify_checkbox": + needsPhone = true; + hasPhone = document.getElementsByName(inputNames.ph)[0].value !== ""; + break; + case "sms_notify_checkbox": + needsSms = true; + var smsNumInput = document.getElementsByName(inputNames.sms)[0]; + hasSms = document.getElementsByName(inputNames.carrier)[0].value !== "" && smsNumInput.value !== ""; // todo: properly validate phone nums + break; + } + } + } + + var culprits = []; + var emailOK = (needsEmail && hasEmail) || (!needsEmail); + var phoneOK = needsPhone && hasPhone || (!needsPhone); + var smsOK = needsSms && hasSms || (!needsSms); + + if (!phoneOK) { + culprits.push("phone_notify"); + } + if (!smsOK) { + culprits.push("sms_notify", "sms_carrier"); + } + + var isFormOK = emailOK && phoneOK && smsOK; + return { isValid: isFormOK, culpritNames : culprits }; +} + +function validateHoldForm() { + var res = validateMethodSelections(document.getElementsByClassName("hold-alert-method")); + if (res.isValid) + { + return true; + } else { + alert ("Please complete hold notification method info."); + res.culpritNames.forEach(function(n){ + document.getElementsByName(n)[0].style.backgroundColor = "yellow"; + }); + return false; + } +} + -- 2.43.2