From 5fc9a42c7c7a89a7ddd91b73639654e16da29980 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 15 Dec 2015 11:41:51 +0000 Subject: [PATCH] Bug 15380: Use Koha::Authority::Type[s] in admin script MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This path rewrites the admin/authtypes.pl script to use the new modules instead of executing the SQL queries from the script. Test plan: 1/ Create 1+ authority types 2/ Update one 3/ Delete it 4/ Create an authority record using this authority type. 5/ Try to delete the authority type Here there is a change in the behavior. Prior to this patch, the user was able to delete the authority type, now it's not. I think it was a bug, feel free to give your opinion if you disagree on this change. Signed-off-by: Marc Véron Signed-off-by: Martin Renvoize Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com --- admin/authtypes.pl | 166 ++++++------- .../prog/en/modules/admin/authtypes.tt | 221 ++++++++++-------- 2 files changed, 213 insertions(+), 174 deletions(-) diff --git a/admin/authtypes.pl b/admin/authtypes.pl index 23e9d29568..9f40827029 100755 --- a/admin/authtypes.pl +++ b/admin/authtypes.pl @@ -1,8 +1,8 @@ #!/usr/bin/perl -# written 20/02/2002 by paul.poulain@free.fr - +# Copyright 2002 paul.poulain@biblibre.com # Copyright 2000-2002 Katipo Communications +# Copyright 2015 Koha Development Team # # This file is part of Koha. # @@ -25,91 +25,97 @@ use C4::Context; use C4::Auth; use C4::Output; -sub StringSearch { - my $string = shift || ''; - my $dbh = C4::Context->dbh; - return $dbh->selectall_arrayref(q| - SELECT authtypecode, authtypetext, auth_tag_to_report, summary - FROM auth_types - WHERE (authtypecode like ?) ORDER BY authtypecode - |, { Slice => {} }, $string . "%" ); -} +use Koha::Authorities; +use Koha::Authority::Types; -my $input = new CGI; -my $script_name = "/cgi-bin/koha/admin/authtypes.pl"; -my $searchfield = $input->param('authtypecode'); # FIXME: Auth Type search not really implemented +my $input = new CGI; my $authtypecode = $input->param('authtypecode'); -my $op = $input->param('op') || ''; -my ($template, $borrowernumber, $cookie) - = get_template_and_user({template_name => "admin/authtypes.tt", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => {parameters => 'parameters_remaining_permissions'}, - debug => 1, - }); - -$template->param( - script_name => $script_name, - ($op || 'else') => 1, +my $op = $input->param('op') || 'list'; +my @messages; +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { template_name => "admin/authtypes.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { parameters => 'parameters_remaining_permissions' }, + debug => 1, + } ); -my $dbh = C4::Context->dbh; +if ( $op eq 'add_form' ) { + my $authority_type; + if ($authtypecode) { + $authority_type = Koha::Authority::Types->find($authtypecode); + } + + $template->param( authority_type => $authority_type ); +} elsif ( $op eq 'add_validate' ) { + my $authtypecode = $input->param('authtypecode'); + my $authtypetext = $input->param('authtypetext'); + my $auth_tag_to_report = $input->param('auth_tag_to_report'); + my $summary = $input->param('summary'); + my $is_a_modif = $input->param('is_a_modif'); -# called by default. Used to create form to add or modify a record -if ($op eq 'add_form') { - #---- if primkey exists, it's a modify action, so read values to modify... - if ( defined $authtypecode) { - my $sth = $dbh->prepare("SELECT * FROM auth_types WHERE authtypecode=?"); - $sth->execute($authtypecode); - my $data = $sth->fetchrow_hashref(); - $template->param( - authtypecode => $authtypecode, - authtypetext => $data->{'authtypetext'}, - auth_tag_to_report => $data->{'auth_tag_to_report'}, - summary => $data->{'summary'}, + if ($is_a_modif) { + my $authority_type = Koha::Authority::Types->find($authtypecode); + $authority_type->authtypetext($authtypetext); + $authority_type->auth_tag_to_report($auth_tag_to_report); + $authority_type->summary($summary); + eval { $authority_type->store; }; + if ($@) { + push @messages, { type => 'error', code => 'error_on_update' }; + } else { + push @messages, { type => 'message', code => 'success_on_update' }; + } + } else { + my $authority_type = Koha::Authority::Type->new( + { authtypecode => $authtypecode, + authtypetext => $authtypetext, + auth_tag_to_report => $auth_tag_to_report, + summary => $summary, + } ); + eval { $authority_type->store; }; + if ($@) { + push @messages, { type => 'error', code => 'error_on_insert' }; + } else { + push @messages, { type => 'message', code => 'success_on_insert' }; + } } - # END $OP eq ADD_FORM -################## ADD_VALIDATE ################################## -# called by add_form, used to insert/modify data in DB -} elsif ($op eq 'add_validate') { - my $sth = $input->param('modif') ? - $dbh->prepare("UPDATE auth_types SET authtypetext=? ,auth_tag_to_report=?, summary=? WHERE authtypecode=?") : - $dbh->prepare("INSERT INTO auth_types SET authtypetext=?, auth_tag_to_report=?, summary=?, authtypecode=?") ; - $sth->execute($input->param('authtypetext'),$input->param('auth_tag_to_report'),$input->param('summary'),$input->param('authtypecode')); - print $input->redirect($script_name); # FIXME: unnecessary redirect - exit; - # END $OP eq ADD_VALIDATE -################## DELETE_CONFIRM ################################## -# called by default form, used to confirm deletion of data in DB -} elsif ($op eq 'delete_confirm') { - #start the page and read in includes - my $sth=$dbh->prepare("SELECT count(*) AS total FROM auth_tag_structure WHERE authtypecode=?"); - $sth->execute($authtypecode); - my $total = $sth->fetchrow_hashref->{total}; + $op = 'list'; + +} elsif ( $op eq 'delete_confirm' ) { + my $authority_type = Koha::Authority::Types->find($authtypecode); + my $authorities_using_it = Koha::Authorities->search( { authtypecode => $authtypecode } )->count; + $template->param( + authority_type => $authority_type, + authorities_using_it => $authorities_using_it, + ); +} elsif ( $op eq 'delete_confirmed' ) { + my $authorities_using_it = Koha::Authorities->search( { authtypecode => $authtypecode } )->count; + if ( $authorities_using_it == 0 ) { + my $authority_type = Koha::Authority::Types->find($authtypecode); + my $deleted = eval { $authority_type->delete; }; - my $sth2 = $dbh->prepare("SELECT * FROM auth_types WHERE authtypecode=?"); - $sth2->execute($authtypecode); - my $data = $sth2->fetchrow_hashref; + if ( $@ or not $deleted ) { + push @messages, { type => 'error', code => 'error_on_delete' }; + } else { + push @messages, { type => 'message', code => 'success_on_delete' }; + } + } else { + push @messages, { type => 'error', code => 'error_on_delete' }; + } + $op = 'list'; +} + +if ( $op eq 'list' ) { + my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } ); + $template->param( authority_types => $authority_types, ); +} + +$template->param( + messages => \@messages, + op => $op, +); - $template->param(authtypecode => $authtypecode, - authtypetext => $data->{'authtypetext'}, - summary => $data->{'summary'}, - total => $total); - # END $OP eq DELETE_CONFIRM -################## DELETE_CONFIRMED ################################## -# called by delete_confirm, used to effectively confirm deletion of data in DB -} elsif ($op eq 'delete_confirmed') { - #start the page and read in includes - my $sth=$dbh->prepare("DELETE FROM auth_types WHERE authtypecode=?"); - $sth->execute(uc $input->param('authtypecode')); - print $input->redirect($script_name); # FIXME: unnecessary redirect - exit; - # END $OP eq DELETE_CONFIRMED -################## DEFAULT ################################## -} else { # DEFAULT - my $results = StringSearch($searchfield); - $template->param( loop => $results ); -} #---- END $OP eq DEFAULT output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt index 60b8be5953..5a82fbf4f5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt @@ -1,8 +1,8 @@ [% INCLUDE 'doc-head-open.inc' %] Koha › Administration › Authority types -[% IF ( add_form ) %] -› [% IF authtypecode.defined %]Modify authority type[% ELSE %]New authority type[% END %] -[% ELSIF ( delete_confirm ) %] +[% IF op == 'add_form' %] +› [% IF authority_type.authtypecode %]Modify authority type[% ELSE %]New authority type[% END %] +[% ELSIF op == 'delete_confirm' %] › Confirm deletion of authority type [% END %] @@ -30,10 +30,10 @@ @@ -43,101 +43,134 @@
-[% IF ( add_form ) %] - -
-
- - [% IF authtypecode.defined %] - Modify authority type - [% ELSE %] - New authority type - [% END %] - -
    -
  1. - [% IF authtypecode.defined %] - Authority type - - - [% authtypecode %] - [% ELSE %] - - - Required - [% END %] -
  2. -
  3. - - Required -
  4. -
  5. -
  6. -

    Note: for 'Authority field to copy', enter the authority field that should be copied from the authority record to the bibliographic record. E.g., in MARC21, field 100 in the authority record should be copied to field 100 in the bibliographic record

    - - - [% IF authtypecode.defined %] - +[% FOR m IN messages %] +
    + [% SWITCH m.code %] + [% CASE 'error_on_update' %] + An error occurred when updating this authority type. Perhaps it already exists. + [% CASE 'error_on_insert' %] + An error occurred when adding this authority type. The authority type code might already exist. + [% CASE 'error_on_delete' %] + An error occurred when deleting this authority type. Check the logs. + [% CASE 'success_on_update' %] + Authority type updated successfully. + [% CASE 'success_on_insert' %] + Authority type added successfully. + [% CASE 'success_on_delete' %] + Authority type deleted successfully. + [% CASE %] + [% m.code %] [% END %] -
  7. -
-
-
- - Cancel -
-
+
[% END %] -[% IF ( delete_confirm ) %] -

Confirm deletion of authority structure definition for '[% authtypetext %]' ([% authtypecode %])

- [% IF ( total ) %] -

This record is used [% total %] times

- [% END %] -
- -
- -
-
-[% END %] -[% IF ( else ) %] - +[% IF op == 'add_form' %] +
+
+ + [% IF authority_type.authtypecode %] + Modify authority type + [% ELSE %] + New authority type + [% END %] + +
    +
  1. + [% IF authority_type.authtypecode %] + Authority type + + + [% authority_type.authtypecode %] + [% ELSE %] + + + Required + [% END %] +
  2. +
  3. + + + Required +
  4. +
  5. + + +
  6. +
  7. +

    Note: for 'Authority field to copy', enter the authority field that should be copied from the authority record to the bibliographic record. E.g., in MARC21, field 100 in the authority record should be copied to field 100 in the bibliographic record

    + + + + [% IF authority_type.authtypecode %] + + [% END %] +
  8. +
+
+
+ + Cancel +
+
+[% END %] -

Authority types

-

Define authority types, then authority MARC structure in the same way you define itemtypes and biblio MARC tag structure. Authority values are managed through plugins

- - - - - - - - - - - - - - [% FOREACH loo IN loop %] - - - - - - - - - - [% END %] - -
CodeDescriptionSummaryAuth field copied EditDelete
[% loo.authtypecode %][% loo.authtypetext %][% loo.summary %][% loo.auth_tag_to_report %]MARC structureEditDelete
+[% IF op == 'delete_confirm' %] +
+ [% IF authorities_using_it %] +

This authority type cannot be deleted

+

This record is used [% authorities_using_it %] times

+ Back to the list + [% ELSE %] +

Confirm deletion of authority structure definition for '[% authority_type.authtypetext %]' ([% authority_type.authtypecode %])

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

Authority types

+

Define authority types, then authority MARC structure in the same way you define itemtypes and biblio MARC tag structure. Authority values are managed through plugins

+ + + + + + + + + + + + + + [% FOREACH authority_type IN authority_types %] + + + + + + + + + + [% END %] + +
CodeDescriptionSummaryAuth field copied EditDelete
[% authority_type.authtypecode %][% authority_type.authtypetext %][% authority_type.summary %][% authority_type.auth_tag_to_report %]MARC structureEditDelete
[% END %] +
-- 2.39.5