From 7a4107bbcb36b35733d7b611c0674711a60a9996 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Wed, 2 Oct 2019 16:41:07 +0100 Subject: [PATCH] Bug 23681: Add Management UI This patch adds UI to allow CRUD operations on restriction types Sponsored-by: Loughborough University Signed-off-by: Benjamin Veasey Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- admin/restrictions.pl | 112 +++++++++++ .../prog/css/src/staff-global.scss | 6 + .../prog/en/modules/admin/restrictions.tt | 174 ++++++++++++++++++ .../intranet-tmpl/prog/js/restrictiontypes.js | 54 ++++++ 4 files changed, 346 insertions(+) create mode 100755 admin/restrictions.pl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt create mode 100644 koha-tmpl/intranet-tmpl/prog/js/restrictiontypes.js diff --git a/admin/restrictions.pl b/admin/restrictions.pl new file mode 100755 index 0000000000..a0254ee1b6 --- /dev/null +++ b/admin/restrictions.pl @@ -0,0 +1,112 @@ +#!/usr/bin/perl + +# Copyright 2019 PTFS Europe +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use CGI qw ( -utf8 ); +use C4::Auth qw( get_template_and_user ); +use C4::Output qw( output_html_with_http_headers ); +use Koha::RestrictionTypes; + +my $input = new CGI; +my $op = $input->param('op') // 'list'; +my $code = uc $input->param('code'); +my @messages = (); + + +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "admin/restrictions.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { parameters => 'manage_patron_restrictions' }, + debug => 1, + } +); + +if ( $op eq 'add_form') { + # Get all existing restrictions, so we can do client-side validation + $template->param( + existing => scalar Koha::RestrictionTypes->search() + ); + if ($code) { + $template->param( + restriction => scalar Koha::RestrictionTypes->find($code) + ); + } +} elsif ( $op eq 'add_validate' ) { + + my $display_text = $input->param('display_text'); + my $is_a_modif = $input->param("is_a_modif"); + + if ($is_a_modif) { + # Check whether another restriction already has this display text + my $dupe = Koha::RestrictionTypes->find({ + display_text => $display_text + }); + if ($dupe) { + push @messages, { + type => 'error', code => 'duplicate_display_text' + }; + } else { + my $restriction = Koha::RestrictionTypes->find($code); + $restriction->display_text($display_text); + $restriction->store; + } + } else { + # Check whether another restriction already has this code + my $dupe = Koha::RestrictionTypes->find($code); + if ($dupe) { + push @messages, { + type => 'error', code => 'duplicate_code' + }; + } else { + my $restriction = Koha::RestrictionType->new({ + code => $code, + display_text => $display_text + }); + $restriction->store; + } + } + $op = 'list'; +} elsif ( $op eq 'delete_confirm' ) { + $template->param( + restriction => scalar Koha::RestrictionTypes->find($code) + ); +} elsif ( $op eq 'delete_confirmed' ) { + Koha::RestrictionTypes->find($code)->delete; + $op = 'list'; +} + +$template->param( + messages => \@messages, + op => $op +); + +if ( $op eq 'list' ) { + my $restrictions = Koha::RestrictionTypes->search(); + $template->param( + restrictions => $restrictions, + ) +} + +output_html_with_http_headers $input, $cookie, $template->output; + +exit 0; diff --git a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss index 23726640d8..4eb2e4401b 100644 --- a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss +++ b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss @@ -4296,6 +4296,12 @@ input.renew { background-color: rgba(0, 0, 0, 0.1); } +#restriction_form { + .type_input { + text-transform: uppercase; + } +} + #stage_list_headings { font-weight: bold; span { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt new file mode 100644 index 0000000000..6e2353b5a9 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt @@ -0,0 +1,174 @@ +[% USE raw %] +[% USE Asset %] +[% USE Koha %] +[% SET footerjs = 1 %] +[% INCLUDE 'doc-head-open.inc' %] +Koha › Administration › Patron restrictions › [% IF op == 'add_form' %][% IF ( restriction ) %]Modify restriction '[% restriction.display_text | html %]'[% ELSE %]New restriction[% END %][% END %] +[% IF op == 'delete_confirm' %]Confirm deletion of restriction '[% restriction.display_text | html %]'[% END %] + +[% INCLUDE 'doc-head-close.inc' %] + + + +[% INCLUDE 'header.inc' %] +[% INCLUDE 'patrons-admin-search.inc' %] + + + +
+
+
+
+ +[% FOR m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'duplicate_display_text' %] + Another restriction already has this label + [% CASE 'duplicate_code' %] + Another restriction already has this code + [% CASE %] + [% m.code | html %] + [% END %] +
+[% END %] + +[% IF op == 'add_form' %] +
+ + + [% IF restriction %] +

Modify restriction [% restriction.display_text | html %]

+ + [% ELSE %] +

New restriction

+ [% END %] +
+
    + [% IF restriction %] +
  1. + Code: [% restriction.code | html %] + +
  2. +
  3. + + + Required +
  4. + [% ELSE %] +
  5. + + + Required +
  6. +
  7. + + + Required +
  8. + [% END %] +
+
+ +
+ + Cancel +
+
+[% END %] + +[% IF op == 'delete_confirm' %] +
+
+ + Confirm restriction deletion + + +

Are you sure you want to delete "[% restriction.display_text | html %]"

+ +
+ + + + Cancel +
+
+
+[% END %] + +[% IF op == 'list' %] + + + +

Patron restrictions

+ [% IF searchfield %] + You Searched for [% searchfield | html %] + [% END %] + [% IF restrictions %] + + + + + + + + + + + [% FOREACH restriction IN restrictions %] + + + + + + + [% END %] + +
CodeLabelDefaultActions
+ [% restriction.code | html %] + + [% restriction.display_text | html %] + + [% IF restriction.dflt %]Yes[% END %] + + Edit + [% IF !restriction.ronly %] + Delete + [% END %] +
+ [% ELSE %] +
No restrictions have been defined. Create a new restriction.
+ [% END %] +[% END %] + +
+
+ +
+ +
+
+ +[% MACRO jsinclude BLOCK %] + [% Asset.js("js/admin-menu.js") | $raw %] + [% INCLUDE 'datatables.inc' %] + [% INCLUDE 'columns_settings.inc' %] + + [% Asset.js("js/restrictiontypes.js") | $raw %] +[% END %] +[% INCLUDE 'intranet-bottom.inc' %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/restrictiontypes.js b/koha-tmpl/intranet-tmpl/prog/js/restrictiontypes.js new file mode 100644 index 0000000000..d1a4e5383e --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/restrictiontypes.js @@ -0,0 +1,54 @@ +jQuery.validator.addMethod( "restrictionCode", function(value){ + var ex = Object.keys(existing); + return (value.length > 0 && ex.indexOf(value.toUpperCase()) > -1) ? + false : + true; +}, MSG_DUPLICATE_CODE); + +jQuery.validator.addMethod( "restrictionDisplayText", function(value){ + var ex = Object.values(existing).map(function(el) { + return el.toLowerCase(); + }); + return (value.length > 0 && ex.indexOf(value.toLowerCase()) > -1) ? + false : + true; +}, MSG_DUPLICATE_DISPLAY_TEXT); + +$(document).ready(function() { + KohaTable("restriction_types", { + "aoColumnDefs": [{ + "aTargets": [-1], + "bSortable": false, + "bSearchable": false + }, { + "aTargets": [0, 1], + "sType": "natural" + }, ], + "aaSorting": [ + [1, "asc"] + ], + "sPaginationType": "full", + "exportColumns": [0,1], + }); + + $("#restriction_form").validate({ + rules: { + code: { + required: true, + restrictionCode: true + }, + display_text: { + required: true, + restrictionDisplayText: true + } + }, + messages: { + code: { + restrictionCode: MSG_DUPLICATE_CODE + }, + display_text: { + restrictionDisplayText: MSG_DUPLICATE_DISPLAY_TEXT + } + } + }); +}); -- 2.39.5