Bug 16127 - Add discharge menu item to patron toolbar
[koha.git] / admin / z3950servers.pl
blobd1fe85b2f0d9ed7c9ce19b890159dd450175672f
1 #!/usr/bin/perl
3 # Copyright 2002 paul.poulain@free.fr
4 # Copyright 2014 Rijksmuseum
6 # This file is part of Koha.
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.
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.
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>.
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.
27 use Modern::Perl;
28 use CGI qw ( -utf8 );
29 use C4::Context;
30 use C4::Auth;
31 use C4::Output;
32 use Koha::Database;
34 # Initialize CGI, template, database
36 my $input = new CGI;
37 my $op = $input->param('op') || 'list';
38 my $id = $input->param('id') || 0;
39 my $type = $input->param('type') || '';
40 my $searchfield = '';
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( {
43 template_name => "admin/z3950servers.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => {parameters => 'parameters_remaining_permissions'},
48 debug => 1,
49 });
50 my $script_name = "/cgi-bin/koha/admin/z3950servers.pl";
51 $template->param( script_name => $script_name );
53 my $schema = Koha::Database->new()->schema();
55 # Main code
56 # First process a confirmed delete, or save a validated record
58 if( $op eq 'delete_confirmed' && $id ) {
59 my $server = $schema->resultset('Z3950server')->find($id);
60 if ( $server ) {
61 $server->delete;
62 $template->param( msg_deleted => 1, msg_add => $server->servername );
63 } else {
64 $template->param( msg_notfound => 1, msg_add => $id );
66 $id = 0;
67 } elsif ( $op eq 'add_validated' ) {
68 my @fields=qw/host port db userid password rank syntax encoding timeout
69 recordtype checked servername servertype sru_options sru_fields
70 add_xslt/;
71 my $formdata = _form_data_hashref( $input, \@fields );
72 if( $id ) {
73 my $server = $schema->resultset('Z3950server')->find($id);
74 if ( $server ) {
75 $server->update( $formdata );
76 $template->param( msg_updated => 1, msg_add => $formdata->{servername} );
77 } else {
78 $template->param( msg_notfound => 1, msg_add => $id );
80 $id = 0;
81 } else {
82 $schema->resultset('Z3950server')->create( $formdata );
83 $template->param( msg_added => 1, msg_add => $formdata->{servername} );
85 } else {
86 #use searchfield only in remaining operations
87 $searchfield = $input->param('searchfield') || '';
90 # Now list multiple records, or edit one record
92 my $data = [];
93 if ( $op eq 'add' || $op eq 'edit' ) {
94 $data = ServerSearch( $schema, $id, $searchfield ) if $searchfield || $id;
95 delete $data->[0]->{id} if @$data && $op eq 'add'; #cloning record
96 $template->param( add_form => 1, server => @$data? $data->[0]: undef,
97 op => $op, type => $op eq 'add'? lc $type: '' );
98 } else {
99 $data = ServerSearch( $schema, $id, $searchfield );
100 $template->param( loop => \@$data, searchfield => $searchfield, id => $id,
101 op => 'list' );
103 output_html_with_http_headers $input, $cookie, $template->output;
105 # End of main code
107 sub ServerSearch { #find server(s) by id or name
108 my ( $schema, $id, $searchstring )= @_;
109 my $rs = $schema->resultset('Z3950server')->search(
110 $id ? { id => $id }: { servername => { like => $searchstring.'%' } },
111 { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }
113 return [ $rs->all ];
116 sub _form_data_hashref {
117 my ( $input, $fieldref ) = @_;
118 return { map { ( $_ => scalar $input->param($_)//'' ) } @$fieldref };