Bug 12265: Improve Z39.50 servers administration
[koha.git] / admin / z3950servers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2002 paul.poulain@free.fr
4 # Copyright 2014 Rijksmuseum
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 # This script is used to maintain the Z3950 servers table.
23 # Parameter $op is operation: list, new, edit, add_validated, delete_confirmed.
24 # add_validated saves a validated record and goes to list view.
25 # delete_confirmed deletes a record and goes to list view.
26
27 use Modern::Perl;
28 use CGI;
29 use C4::Context;
30 use C4::Auth;
31 use C4::Output;
32 use Koha::Database;
33
34 # Initialize CGI, template, database
35
36 my $input = new CGI;
37 my $op = $input->param('op') || 'list';
38 my $id = $input->param('id') || 0;
39 my $searchfield = '';
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( {
42     template_name => "admin/z3950servers.tmpl",
43     query => $input,
44     type => "intranet",
45     authnotrequired => 0,
46     flagsrequired => {parameters => 'parameters_remaining_permissions'},
47     debug => 1,
48 });
49 my $script_name = "/cgi-bin/koha/admin/z3950servers.pl";
50 $template->param( script_name => $script_name );
51
52 my $schema = Koha::Database->new()->schema();
53
54 # Main code
55 # First process a confirmed delete, or save a validated record
56
57 if( $op eq 'delete_confirmed' && $id ) {
58     my $rs=$schema->resultset('Z3950server')->search( { id => $id } );
59     my $name= $rs->first?$rs->first->name:'';
60     my $cnt=$rs->delete;
61     $template->param( msg_deleted => 1, msg_add => $name ) if $cnt==1;
62     $id=0;
63 } elsif( $op eq 'add_validated' ) {
64     my @fields=qw/host port db userid password rank syntax encoding timeout
65         recordtype checked/;
66     my $formdata = _form_data_hashref( $input, \@fields );
67     #add name from servername (an input with name="name" gave problems)
68     $formdata->{name} = $input->param('servername');
69     if( $id ) {
70         my @res= $schema->resultset('Z3950server')->search( { id => $id } );
71         $res[0]->update( $formdata );
72         $template->param( msg_updated => 1, msg_add => $formdata->{name} );
73         $id=0;
74     } else {
75         $schema->resultset('Z3950server')->create( $formdata );
76         $template->param( msg_added => 1, msg_add => $formdata->{name} );
77     }
78 } else {
79     #use searchfield only in remaining operations
80     $searchfield = $input->param('searchfield') || '';
81 }
82
83 # Now list multiple records, or edit one record
84
85 my $data = [];
86 if ( $op eq 'add' || $op eq 'edit' ) {
87     $data = ServerSearch( $schema, $id, $searchfield ) if $searchfield || $id;
88     delete $data->[0]->{id} if @$data && $op eq 'add'; #cloning record
89     $template->param( add_form => 1, server => @$data? $data->[0]: undef,
90         op => $op );
91 } else {
92     $data = ServerSearch( $schema, $id, $searchfield );
93     $template->param( loop => \@$data, searchfield => $searchfield, id => $id,
94         op => 'list' );
95 }
96 output_html_with_http_headers $input, $cookie, $template->output;
97
98 # End of main code
99
100 sub ServerSearch  { #find server(s) by id or name
101     my ( $schema, $id, $searchstring )= @_;
102     my @resobjs= $schema->resultset('Z3950server')->search(
103         $id ? { id => $id }: { name => { like => $searchstring.'%' } },
104         { order_by => 'rank,name' },
105     );
106     return [ map { {$_->get_columns} } @resobjs ];
107 }
108
109 sub _form_data_hashref {
110     my ( $input, $fieldref ) = @_;
111     return { map { ( $_ => $input->param($_) ) } @$fieldref };
112 }