From b44467bf4a84084164ab69e8e7fdf9f666b8e1a5 Mon Sep 17 00:00:00 2001 From: Jacek Ablewicz Date: Fri, 20 Jun 2014 12:35:19 +0200 Subject: [PATCH] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2 (part #1: changes in opac-suggestions.* script + bootstrap template) This patch: - adds a configuration option which allows to define which fields (apart from the title) should be mandatory for a patron purchase suggestion form in OPAC - impoves the method how required form fields are enforced, by utilising new HTML5 "required" attribute (old method would still be operational in old/legacy browsers which do not support "required" attribute properly). Allowed fields that are configurable as mandatory/not mandatory for suggestions made via OPAC are: author|copyrightdate|collectiontitle| isbn|publishercode|place|note|itemtype|patronreason. Note: this patch only applies to OPAC bootstrap theme. To test: 1/ Apply patch. 2/ Without 'OPACSuggestionMandatoryFields' option set, OPAC suggestion form should only require the title field. 3/ Set 'OPACSuggestionMandatoryFields' syspref to (e.g.) 'author|isbn|itemtype' 4/ Note that suggestion form in OPAC now requires 3 more fields to be entered and/or choosen (they should be labeled in red), and the user instruction changes slightly ("Form fields labeled in red are mandatory" ..). 5/ Try to submit some suggestions: for each field which is configured as mandatory, browser should enforce this field to be filled (in modern, HTML5 compilant browsers), or (as a fallback mechanism for legacy browsers) there should be an alert message displayed stating that some mandatory fields are missing. 6/ In both modern and legacy browsers, unless all mandatory fields are filled with something, form submission should not be possible. 7/ Repeat test 3-6 for all supported field names configurable as mandatory. If possible, "legacy" browser tests should ideally involve IE9 and (some older version of) Safari, which are rumored to have somehow questionable reputation regarding "required" HTML attribute support. --- .../bootstrap/en/modules/opac-suggestions.tt | 37 ++++++++++++++++++---- opac/opac-suggestions.pl | 17 ++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt index 46d0031..4eb0a6f 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt @@ -29,9 +29,14 @@

Enter a new purchase suggestion

Please fill out this form to make a purchase suggestion. You will receive an email when the library processes your suggestion

-

Only the title is required, but the more information you enter the easier it will be for the librarians to find the title you're requesting. The "Notes" field can be used to provide any additional information.

+ [% IF (mandatoryfields) %] +

Form fields labeled in red are mandatory. The more information you enter the easier it will be for the librarians to find the title you're requesting. + [% ELSE %] +

Only the title is required, but the more information you enter the easier it will be for the librarians to find the title you're requesting. + [% END %] + The "Notes" field can be used to provide any additional information.

-
+
  1. @@ -94,7 +99,7 @@
    - Cancel + Cancel
    [% END # IF op_add %] @@ -304,23 +309,41 @@ return true; }); [% END %] + [% IF ( op_add && mandatoryfields ) %] + var FldsRequired = [[% mandatoryfields %]]; + for (var i = 0; i < FldsRequired.length; i++) { + var rqinput = $('#' + FldsRequired[i]); + if (rqinput.length != 1) continue; + $(rqinput).attr("required", "required"); + var rqlabel = $("label[for=" + rqinput.attr("id") + "]"); + if (rqlabel.length != 1) continue; + $(rqlabel).addClass('required'); + } + [% END %] }); function Check(f) { var _alertString=""; var alertString2; - if(f.title.value.length ==0){ - _alertString += _("- You must enter a Title") + "\n"; + var FieldsRequired = [[% mandatoryfields %]]; + FieldsRequired.unshift("title"); + + for (var i = 0; i < FieldsRequired.length; i++) { + var lbl = FieldsRequired[i]; + if (!f[lbl] || (f[lbl].value.length != 0)) continue; + _alertString += _("- Some mandatory fields are missing") + "\n"; + break; } - if (_alertString.length==0) { - f.submit(); + if (_alertString.length == 0) { + return true; } else { alertString2 = _("Form not submitted because of the following problem(s)"); alertString2 += "\n------------------------------------------------------------------------------------\n\n"; alertString2 += _alertString; alert(alertString2); + return false; } } //]]> diff --git a/opac/opac-suggestions.pl b/opac/opac-suggestions.pl index 2d89d93..770dd46 100755 --- a/opac/opac-suggestions.pl +++ b/opac/opac-suggestions.pl @@ -156,6 +156,22 @@ if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) { $template->param( branchloop => $branchloop ); } +my $mandatoryfields = ''; +{ + my $fldsreqsp = C4::Context->preference("OPACSuggestionMandatoryFields"); + last unless ($op eq 'add' && $fldsreqsp); + my $supported_rf = { + map { ($_, 1); } qw( + author copyrightdate isbn publishercode collectiontitle + place itemtype patronreason note + ) + }; + my %fh = map { + ($_ && $supported_rf->{$_})? ('"'.$_.'"', 1): (); + } (split(/\s*\|\s*/, $fldsreqsp)); + $mandatoryfields = join(',', (sort keys %fh)); +} + $template->param( %$suggestion, itemtypeloop=> $supportlist, @@ -164,6 +180,7 @@ $template->param( showall => $allsuggestions, "op_$op" => 1, suggestionsview => 1, + mandatoryfields => $mandatoryfields, ); output_html_with_http_headers $input, $cookie, $template->output; -- 2.1.0