Bug 26083: Item editor defaults to lost
[koha.git] / admin / z3950servers.pl
blobd2e85c60147f3e8d44db791afcdc02195aeec8ea
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;
33 use Koha::Z3950Servers;
35 # Initialize CGI, template, database
37 my $input = new CGI;
38 my $op = $input->param('op') || 'list';
39 my $id = $input->param('id') || 0;
40 my $type = $input->param('type') || '';
41 my $searchfield = '';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( {
44 template_name => "admin/z3950servers.tt",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => { parameters => 'manage_search_targets' },
49 debug => 1,
50 });
51 my $script_name = "/cgi-bin/koha/admin/z3950servers.pl";
52 $template->param( script_name => $script_name );
54 my $schema = Koha::Database->new()->schema();
56 # Main code
57 # First process a confirmed delete, or save a validated record
59 if( $op eq 'delete_confirmed' && $id ) {
60 my $server = Koha::Z3950Servers->find($id);
61 if ( $server ) {
62 $server->delete;
63 $template->param( msg_deleted => 1, msg_add => $server->servername );
64 } else {
65 $template->param( msg_notfound => 1, msg_add => $id );
67 $id = 0;
68 } elsif ( $op eq 'add_validated' ) {
69 my @fields=qw/host port db userid password rank syntax encoding timeout
70 recordtype checked servername servertype sru_options sru_fields attributes
71 add_xslt/;
72 my $formdata = _form_data_hashref( $input, \@fields );
73 if( $id ) {
74 my $server = Koha::Z3950Servers->find($id);
75 if ( $server ) {
76 $server->set( $formdata )->store;
77 $template->param( msg_updated => 1, msg_add => $formdata->{servername} );
78 } else {
79 $template->param( msg_notfound => 1, msg_add => $id );
81 $id = 0;
82 } else {
83 Koha::Z3950Server->new( $formdata )->store;
84 $template->param( msg_added => 1, msg_add => $formdata->{servername} );
86 } else {
87 #use searchfield only in remaining operations
88 $searchfield = $input->param('searchfield') || '';
91 # Now list multiple records, or edit one record
93 my $data = [];
94 if ( $op eq 'add' || $op eq 'edit' ) {
95 $data = ServerSearch( $schema, $id, $searchfield ) if $searchfield || $id;
96 delete $data->[0]->{id} if @$data && $op eq 'add'; #cloning record
97 $template->param( add_form => 1, server => @$data? $data->[0]: undef,
98 op => $op, type => $op eq 'add'? lc $type: '' );
99 } else {
100 $data = ServerSearch( $schema, $id, $searchfield );
101 $template->param( loop => \@$data, searchfield => $searchfield, id => $id,
102 op => 'list' );
104 output_html_with_http_headers $input, $cookie, $template->output;
106 # End of main code
108 sub ServerSearch { #find server(s) by id or name
109 my ( $schema, $id, $searchstring )= @_;
111 return Koha::Z3950Servers->search(
112 $id ? { id => $id } : { servername => { like => $searchstring . '%' } },
113 )->unblessed;
116 sub _form_data_hashref {
117 my ( $input, $fieldref ) = @_;
118 return { map { ( $_ => scalar $input->param($_)//'' ) } @$fieldref };