Bug 15380: Use Koha::Authority::Type[s] in admin script
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 15 Dec 2015 11:41:51 +0000 (11:41 +0000)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Thu, 31 Dec 2015 18:50:41 +0000 (18:50 +0000)
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 <veron@veron.ch>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
admin/authtypes.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt

index 23e9d29..9f40827 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/perl
 
 #!/usr/bin/perl
 
-# written 20/02/2002 by paul.poulain@free.fr
-
+# Copyright 2002 paul.poulain@biblibre.com
 # Copyright 2000-2002 Katipo Communications
 # Copyright 2000-2002 Katipo Communications
+# Copyright 2015 Koha Development Team
 #
 # This file is part of Koha.
 #
 #
 # This file is part of Koha.
 #
@@ -25,91 +25,97 @@ use C4::Context;
 use C4::Auth;
 use C4::Output;
 
 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 $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;
 output_html_with_http_headers $input, $cookie, $template->output;
index 60b8be5..5a82fbf 100644 (file)
@@ -1,8 +1,8 @@
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; Authority types
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; Authority types
-[% IF ( add_form ) %]
-&rsaquo; [% IF authtypecode.defined %]Modify authority type[% ELSE %]New authority type[% END %]
-[% ELSIF ( delete_confirm ) %]
+[% IF op == 'add_form' %]
+&rsaquo; [% IF authority_type.authtypecode %]Modify authority type[% ELSE %]New authority type[% END %]
+[% ELSIF op == 'delete_confirm' %]
 &rsaquo; Confirm deletion of authority type
 [% END %]
 </title>
 &rsaquo; Confirm deletion of authority type
 [% END %]
 </title>
 <div id="breadcrumbs">
          <a href="/cgi-bin/koha/mainpage.pl">Home</a>
 &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
 <div id="breadcrumbs">
          <a href="/cgi-bin/koha/mainpage.pl">Home</a>
 &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
-&rsaquo; <a href="[% script_name %]">Authority types</a>
-[% IF ( add_form ) %]
-&rsaquo; [% IF authtypecode.defined %]Modify[% ELSE %]New[% END %] Authority type
-[% ELSIF ( delete_confirm ) %]
+&rsaquo; <a href="/cgi-bin/koha/admin/authtypes.pl">Authority types</a>
+[% IF op == 'add_form' %]
+&rsaquo; [% IF authority_type.authtypecode %]Modify[% ELSE %]New[% END %] Authority type
+[% ELSIF op == 'delete_confirm' %]
 &rsaquo; Confirm deletion of authority type
 [% END %]
 </div>
 &rsaquo; Confirm deletion of authority type
 [% END %]
 </div>
        <div id="yui-main">
          <div class="yui-b">
 
        <div id="yui-main">
          <div class="yui-b">
 
-[% IF ( add_form ) %]
-
-    <form action="[% script_name %]" name="Aform" method="post" class="validated">
-    <fieldset class="rows">
-    <legend>
-    [% IF authtypecode.defined %]
-        Modify authority type
-       [% ELSE %]
-        New authority type
-       [% END %]
-    </legend>
-    <ol>
-        <li>
-    [% IF authtypecode.defined %]
-            <span class="label">Authority type</span>
-            <input type="hidden" name="op" value="add_validate" />
-            <input type="hidden" name="checked" value="0" />
-            <input type="hidden" name="authtypecode" value="[% authtypecode %]" />[% authtypecode %]
-    [% ELSE %]
-            <label for="authtypecode" class="required">Authority type: </label>
-            <input id="authtypecode" type="text" class="required" required="required" name="authtypecode" size="10" maxlength="10" onblur="toUC(this)" />
-            <span class="required">Required</span>
-    [% END %]
-        </li>
-        <li>
-            <label for="authtypetext" class="required">Description: </label><input type="text" id="authtypetext" name="authtypetext" size="40" maxlength="80" value="[% authtypetext |html %]" class="required" required="required" />
-            <span class="required">Required</span>
-        </li>
-               <li><label for="summary">Summary: </label><textarea id="summary" name="summary" cols="55" rows="7">[% summary %]</textarea></li>
-               <li>
-               <p class="tip">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</p>
-               <label for="auth_tag_to_report">Authority field to copy: </label><input type="text" id="auth_tag_to_report" name="auth_tag_to_report" size="5" maxlength="3" value="[% auth_tag_to_report %]" />
-               <input type="hidden" name="op" value="add_validate" />
-        [% IF authtypecode.defined %]
-        <input type="hidden" name="modif" value="1" />
+[% FOR m IN messages %]
+    <div class="dialog [% m.type %]">
+        [% 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 %]
         [% END %]
-        </li>
-    </ol>
-       </fieldset>
-    <fieldset class="action">
-        <input type="submit" value="Submit" />
-        <a class="cancel" href="[% script_name %]">Cancel</a>
-    </fieldset>
-       </form>
+    </div>
 [% END %]
 
 [% END %]
 
-[% IF ( delete_confirm ) %]
-        <div class="dialog alert"><h3>Confirm deletion of authority structure definition for <span class="ex">'[% authtypetext %]' ([% authtypecode %])</span></h3>
-       [% IF ( total ) %]
-               <p>This record is used <strong>[% total %]</strong> times</p>
-       [% END %]               
-                       <form action="[% script_name %]" method="post"><input type="hidden" name="op" value="delete_confirmed" />
-                       <input type="hidden" name="authtypecode" value="[% authtypecode %]" />
-            <fieldset class="action"><input type="submit" class="approve" value="Yes, delete" />
-               </form>
-        <form action="[% script_name %]" method="get"><input type="submit" class="deny" value="No, do not delete" /></form>
-               </div>
-[% END %]
 
 
-[% IF ( else ) %]
 
 
-<div id="toolbar" class="btn-toolbar">
-    <a id="authtype" class="btn btn-small" href="[% script_name %]?op=add_form"><i class="fa fa-plus"></i> New authority type</a>
-</div>
+[% IF op == 'add_form' %]
+    <form action="/cgi-bin/koha/admin/authtypes.pl" name="Aform" method="post" class="validated">
+        <fieldset class="rows">
+            <legend>
+                [% IF authority_type.authtypecode %]
+                    Modify authority type
+                [% ELSE %]
+                    New authority type
+                [% END %]
+            </legend>
+            <ol>
+                <li>
+                    [% IF authority_type.authtypecode %]
+                            <span class="label">Authority type</span>
+                            <input type="hidden" name="op" value="add_validate" />
+                            <input type="hidden" name="checked" value="0" />
+                            <input type="hidden" name="authtypecode" value="[% authority_type.authtypecode %]" />[% authority_type.authtypecode %]
+                    [% ELSE %]
+                            <label for="authtypecode" class="required">Authority type: </label>
+                            <input id="authtypecode" type="text" class="required" required="required" name="authtypecode" size="10" maxlength="10" onblur="toUC(this)" />
+                            <span class="required">Required</span>
+                    [% END %]
+                </li>
+                <li>
+                    <label for="authtypetext" class="required">Description: </label>
+                    <input type="text" id="authtypetext" name="authtypetext" size="40" maxlength="80" value="[% authority_type.authtypetext |html %]" class="required" required="required" />
+                    <span class="required">Required</span>
+                </li>
+                <li>
+                    <label for="summary">Summary: </label>
+                    <textarea id="summary" name="summary" cols="55" rows="7">[% authority_type.summary %]</textarea>
+                </li>
+                <li>
+                    <p class="tip">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</p>
+                    <label for="auth_tag_to_report">Authority field to copy: </label>
+                    <input type="text" id="auth_tag_to_report" name="auth_tag_to_report" size="5" maxlength="3" value="[% authority_type.auth_tag_to_report %]" />
+                    <input type="hidden" name="op" value="add_validate" />
+                    [% IF authority_type.authtypecode %]
+                        <input type="hidden" name="is_a_modif" value="1" />
+                    [% END %]
+                </li>
+            </ol>
+        </fieldset>
+        <fieldset class="action">
+            <input type="submit" value="Submit" />
+            <a class="cancel" href="/cgi-bin/koha/admin/authtypes.pl">Cancel</a>
+        </fieldset>
+    </form>
+[% END %]
 
 
-<h1>Authority types</h1>
-<p>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</p>
-<table id="authtypes">
-    <thead>
-       <tr>
-               <th>Code</th>
-               <th>Description</th>
-               <th>Summary</th>
-               <th>Auth field copied</th>
-               <th>&nbsp;</th>
-               <th>Edit</th>
-               <th>Delete</th>
-       </tr>
-    </thead>
-    <tbody>
-       [% FOREACH loo IN loop %]
-               <tr>
-                       <td>[% loo.authtypecode %]</td>
-                       <td>[% loo.authtypetext %]</td>
-                       <td>[% loo.summary %]</td>
-                       <td>[% loo.auth_tag_to_report %]</td>
-                       <td><a href="auth_tag_structure.pl?authtypecode=[% loo.authtypecode %]" class="button parameters" >MARC structure</a></td>
-                       <td><a href="[% loo.script_name %]?op=add_form&amp;authtypecode=[% loo.authtypecode |html %]">Edit</a></td>
-                       <td><a href="[% loo.script_name %]?op=delete_confirm&amp;authtypecode=[% loo.authtypecode |html %]">Delete</a></td>
-               </tr>
-       [% END %]
-    <tbody>
-</table>
+[% IF op == 'delete_confirm' %]
+    <div class="dialog alert">
+        [% IF authorities_using_it %]
+            <h3>This authority type cannot be deleted</h3>
+            <p>This record is used <strong>[% authorities_using_it %]</strong> times</p>
+            <a class="cancel" href="/cgi-bin/koha/admin/authtypes.pl">Back to the list</a>
+        [% ELSE %]
+            <h3>Confirm deletion of authority structure definition for <span class="ex">'[% authority_type.authtypetext %]' ([% authority_type.authtypecode %])</span></h3>
+            <form action="/cgi-bin/koha/admin/authtypes.pl" method="post">
+                <input type="hidden" name="op" value="delete_confirmed" />
+                <input type="hidden" name="authtypecode" value="[% authority_type.authtypecode %]" />
+                <fieldset class="action"><input type="submit" class="approve" value="Yes, delete" /></fieldset>
+            </form>
+            <form action="/cgi-bin/koha/admin/authtypes.pl" method="get">
+                <input type="submit" class="deny" value="No, do not delete" />
+            </form>
+        [% END %]
+    </div>
+[% END %]
+
+[% IF op == 'list' %]
+    <div id="toolbar" class="btn-toolbar">
+        <a id="authtype" class="btn btn-small" href="/cgi-bin/koha/admin/authtypes.pl?op=add_form"><i class="fa fa-plus"></i> New authority type</a>
+    </div>
 
 
+    <h1>Authority types</h1>
+    <p>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</p>
+    <table id="authtypes">
+        <thead>
+        <tr>
+            <th>Code</th>
+            <th>Description</th>
+            <th>Summary</th>
+            <th>Auth field copied</th>
+            <th>&nbsp;</th>
+            <th>Edit</th>
+            <th>Delete</th>
+        </tr>
+        </thead>
+        <tbody>
+        [% FOREACH authority_type IN authority_types %]
+            <tr>
+                <td>[% authority_type.authtypecode %]</td>
+                <td>[% authority_type.authtypetext %]</td>
+                <td>[% authority_type.summary %]</td>
+                <td>[% authority_type.auth_tag_to_report %]</td>
+                <td><a href="auth_tag_structure.pl?authtypecode=[% authority_type.authtypecode %]" class="button parameters" >MARC structure</a></td>
+                <td><a href="/cgi-bin/koha/admin/authtypes.pl?op=add_form&amp;authtypecode=[% authority_type.authtypecode |html %]">Edit</a></td>
+                <td><a href="/cgi-bin/koha/admin/authtypes.pl?op=delete_confirm&amp;authtypecode=[% authority_type.authtypecode |html %]">Delete</a></td>
+            </tr>
+        [% END %]
+        <tbody>
+    </table>
 [% END %]
 [% END %]
+
 </div>
 </div>
 <div class="yui-b">
 </div>
 </div>
 <div class="yui-b">