View | Details | Raw Unified | Return to bug 15875
Collapse All | Expand All

(-)a/C4/Breeding.pm (-1 / +164 lines)
Lines 20-25 package C4::Breeding; Link Here
20
20
21
use strict;
21
use strict;
22
use warnings;
22
use warnings;
23
use Data::Dumper;
23
24
24
use C4::Biblio;
25
use C4::Biblio;
25
use C4::Koha;
26
use C4::Koha;
Lines 28-33 use MARC::File::USMARC; Link Here
28
use C4::ImportBatch;
29
use C4::ImportBatch;
29
use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
30
use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
30
31
32
use Koha::Exception::DB;
33
use Koha::Exception::DuplicateObject;
34
use Koha::Exception::BadParameter;
35
31
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
37
33
BEGIN {
38
BEGIN {
Lines 675-680 sub Z3950SearchAuth { Link Here
675
    );
680
    );
676
}
681
}
677
682
683
=head getZ3950server
684
685
    my $servers = C4::Breeding::listZ3950servers({id => 443, name => "geigei"});
686
687
@RETURNS ARRAYRef of z3950servers-rows
688
689
=cut
690
691
sub listZ3950servers {
692
    my ($params) = @_;
693
    my $dbh=C4::Context->dbh;
694
    my ($sth, $sql);
695
696
    $sql = "SELECT * FROM z3950servers ";
697
    my @params;
698
    my @andedColumns;
699
    if (ref($params) eq 'HASH') {
700
        while (my ($column, $value) = each(%$params)) {
701
            push(@andedColumns, "$column = ?");
702
            push(@params, $value);
703
        }
704
    }
705
    if (@params) {
706
        $sql .= 'WHERE '.join(' AND ', @andedColumns);
707
    }
708
    $sql .= ' ORDER BY rank,name';
709
710
    eval {
711
        $sth = $dbh->prepare($sql);
712
        $sth->execute( @params );
713
    };
714
    if ($@ || $sth->err) {
715
        my @cc = caller(0);
716
        my $paramsStr = Data::Dumper::Dumper($params);
717
        Koha::Exception::DB->throw(error => $cc[3].'():>'.($@ || $sth->errstr)."\nWith search terms\n$paramsStr\n");
718
    }
719
    return $sth->fetchall_arrayref({});
720
}
721
722
=head getZ3950server
723
724
    my $server = C4::Breeding::getZ3950server({id => 443, name => "geigei"});
725
726
=cut
727
728
sub getZ3950server {
729
    my ($params) = @_;
730
    my $servers = listZ3950servers($params);
731
732
    if (not(ref($servers) eq 'ARRAY') || not(scalar(@$servers))) {
733
        return undef;
734
    }
735
    elsif (scalar(@$servers) > 1) {
736
        my @cc = caller(0);
737
        my $paramsStr = Data::Dumper::Dumper($params);
738
        Koha::Exception::DuplicateObject->throw(error => $cc[3]."():> Too many Z3950 servers found with search terms\n$paramsStr\n");
739
    }
740
    return $servers->[0];
741
}
742
743
sub updateZ3950server {
744
    my ($params) = @_;
745
    my $dbh=C4::Context->dbh;
746
    my ($sth, $sql);
747
748
    unless($params->{name}) {
749
        my @cc = caller(0);
750
        Koha::Exception::BadParameter->throw(  error => $cc[3]."():> Missing attribute 'name'!"  );
751
    }
752
753
    eval {
754
        $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=?,encoding=?,timeout=?,recordtype=? where name=?");
755
        $sth->execute(
756
            $params->{'host'},
757
            $params->{'port'},
758
            $params->{'db'},
759
            $params->{'userid'},
760
            $params->{'password'},
761
            $params->{'name'},
762
            $params->{'checked'} ? 1 : 0,
763
            $params->{'rank'},
764
            $params->{'syntax'},
765
            $params->{'encoding'},
766
            $params->{'timeout'},
767
            $params->{'recordtype'},
768
            $params->{'name'},
769
        );
770
    };
771
    if ($@ || $sth->err) {
772
        my @cc = caller(0);
773
        Koha::Exception::DB->throw(error => $cc[3].'():>'.($@ || $sth->errstr));
774
    }
775
}
776
777
sub addZ3950server {
778
    my ($params) = @_;
779
    my $dbh=C4::Context->dbh;
780
    my ($sth, $sql);
781
782
    unless($params->{name}) {
783
        my @cc = caller(0);
784
        Koha::Exception::BadParameter->throw(  error => $cc[3]."():> Missing attribute 'name'!"  );
785
    }
786
787
    eval {
788
        $sth=$dbh->prepare(
789
          "INSERT INTO z3950servers " .
790
              "        (host,port,db,userid,password,name,checked,rank,syntax,encoding,timeout,recordtype) " .
791
               "VALUES (?,   ?,   ?, ?,     ?,       ?,   ?,      ?,   ?,     ?,       ?,      ?)" );
792
        $sth->execute(
793
            $params->{'host'},
794
            $params->{'port'},
795
            $params->{'db'},
796
            $params->{'userid'},
797
            $params->{'password'},
798
            $params->{'name'},
799
            $params->{'checked'} ? 1 : 0,
800
            $params->{'rank'},
801
            $params->{'syntax'},
802
            $params->{'encoding'},
803
            $params->{'timeout'},
804
            $params->{'recordtype'},
805
        );
806
    };
807
    if ($@ || $sth->err) {
808
        my @cc = caller(0);
809
        Koha::Exception::DB->throw(error => $cc[3].'():>'.($@ || $sth->errstr));
810
    }
811
}
812
813
sub upsertZ3950server {
814
    my ($params) = @_;
815
    my $dbh=C4::Context->dbh;
816
    my ($sth, $sql, $server);
817
818
    $server = getZ3950server(  $params->{id} ? {id => $params->{id}} : {name => $params->{name}}  );
819
    if ($server) {
820
        C4::Breeding::updateZ3950server($params);
821
    }
822
    else {
823
        C4::Breeding::addZ3950server($params);
824
    }
825
}
826
827
sub deleteZ3950server {
828
    my ($id) = @_;
829
    my $dbh=C4::Context->dbh;
830
    my ($sth, $sql);
831
832
    eval {
833
        $sth=$dbh->prepare("DELETE FROM z3950servers WHERE id=?");
834
        $sth->execute($id);
835
    };
836
    if ($@ || $sth->err) {
837
        my @cc = caller(0);
838
        Koha::Exception::DB->throw(error => $cc[3].'():>'.($@ || $sth->errstr));
839
    }
840
}
841
678
1;
842
1;
679
__END__
843
__END__
680
(-)a/admin/z3950servers.pl (-57 / +36 lines)
Lines 25-30 use CGI; Link Here
25
use C4::Context;
25
use C4::Context;
26
use C4::Auth;
26
use C4::Auth;
27
use C4::Output;
27
use C4::Output;
28
use C4::Breeding;
28
29
29
sub StringSearch  {
30
sub StringSearch  {
30
	my ($searchstring,$type)=@_;
31
	my ($searchstring,$type)=@_;
Lines 90-103 if ( $op eq 'add_form' ) { Link Here
90
91
91
    #---- if primkey exists, it's a modify action, so read values to modify...
92
    #---- if primkey exists, it's a modify action, so read values to modify...
92
    my $data;
93
    my $data;
93
    if ($searchfield) {
94
    if (defined($searchfield)) {
94
        my $dbh = C4::Context->dbh;
95
        $data = C4::Breeding::getZ3950server({name => $searchfield});
95
        my $sth = $dbh->prepare(
96
"select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype from z3950servers where (name = ?) order by rank,name"
97
        );
98
        $sth->execute($searchfield);
99
        $data = $sth->fetchrow_hashref;
100
        $sth->finish;
101
    }
96
    }
102
    $template->param( $_ => $data->{$_} )
97
    $template->param( $_ => $data->{$_} )
103
      for (qw( host port db userid password checked rank timeout encoding ));
98
      for (qw( host port db userid password checked rank timeout encoding ));
Lines 107-164 if ( $op eq 'add_form' ) { Link Here
107
################## ADD_VALIDATE ##################################
102
################## ADD_VALIDATE ##################################
108
    # called by add_form, used to insert/modify data in DB
103
    # called by add_form, used to insert/modify data in DB
109
} elsif ($op eq 'add_validate') {
104
} elsif ($op eq 'add_validate') {
110
	my $dbh=C4::Context->dbh;
105
    my $params = castCGIToZ3950serverParams($input);
111
	my $sth=$dbh->prepare("select * from z3950servers where name=?");
106
    my $dbh=C4::Context->dbh;
112
	$sth->execute($input->param('searchfield'));
107
    my ($sth, $sql, $server);
113
	my $checked = $input->param('checked') ? 1 : 0;
108
114
	if ($sth->rows) {
109
    $server = C4::Breeding::getZ3950server(  $params->{id} ? {id => $params->{id}} : {name => $params->{name}}  );
110
    if ($server) {
111
        C4::Breeding::updateZ3950server($params);
115
        $template->param(confirm_update => 1);
112
        $template->param(confirm_update => 1);
116
             $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=?,encoding=?,timeout=?,recordtype=? where name=?");
113
    }
117
		$sth->execute($input->param('host'),
114
    else {
118
		      $input->param('port'),
115
        C4::Breeding::addZ3950server($params);
119
		      $input->param('db'),
120
		      $input->param('userid'),
121
		      $input->param('password'),
122
		      $input->param('searchfield'),
123
		      $checked,
124
		      $input->param('rank'),
125
			  $input->param('syntax'),
126
              $input->param('encoding'),
127
              $input->param('timeout'),
128
              $input->param('recordtype'),
129
		      $input->param('searchfield'),
130
		      );
131
	} 
132
	else {
133
        $template->param(confirm_add => 1);
116
        $template->param(confirm_add => 1);
134
		$sth=$dbh->prepare(
135
		  "INSERT INTO z3950servers " .
136
              "(host,port,db,userid,password,name,checked,rank,syntax,encoding,timeout,recordtype) " .
137
               "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
138
        $sth->execute(
139
            $input->param('host'),     $input->param('port'),
140
            $input->param('db'),       $input->param('userid'),
141
            $input->param('password'), $input->param('searchfield'),
142
            $checked,                  $input->param('rank'),
143
            $input->param('syntax'),   $input->param('encoding'),
144
            $input->param('timeout'),  $input->param('recordtype')
145
        );
146
    }
117
    }
147
    $sth->finish;
148
149
    # END $OP eq ADD_VALIDATE
118
    # END $OP eq ADD_VALIDATE
150
################## DELETE_CONFIRM ##################################
119
################## DELETE_CONFIRM ##################################
151
# called by default form, used to confirm deletion of data in DB
120
# called by default form, used to confirm deletion of data in DB
152
} elsif ($op eq 'delete_confirm') {
121
} elsif ($op eq 'delete_confirm') {
153
    $template->param( delete_confirm => 1 );
122
    $template->param( delete_confirm => 1 );
154
    my $dbh = C4::Context->dbh;
123
    my $data = C4::Breeding::getZ3950server({name => $searchfield});
155
156
    my $sth2 = $dbh->prepare(
157
"select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype from z3950servers where (name = ?) order by rank,name"
158
    );
159
    $sth2->execute($searchfield);
160
    my $data = $sth2->fetchrow_hashref;
161
    $sth2->finish;
162
124
163
    $template->param(
125
    $template->param(
164
        host       => $data->{'host'},
126
        host       => $data->{'host'},
Lines 179-188 if ( $op eq 'add_form' ) { Link Here
179
# called by delete_confirm, used to effectively confirm deletion of data in DB
141
# called by delete_confirm, used to effectively confirm deletion of data in DB
180
} elsif ($op eq 'delete_confirmed') {
142
} elsif ($op eq 'delete_confirmed') {
181
	$template->param(delete_confirmed => 1);
143
	$template->param(delete_confirmed => 1);
182
	my $dbh=C4::Context->dbh;
144
    my $server = C4::Breeding::getZ3950server({name => $searchfield});
183
	my $sth=$dbh->prepare("delete from z3950servers where name=?");
145
    C4::Breeding::deleteZ3950server($server->{id});
184
	$sth->execute($searchfield);
185
	$sth->finish;
186
													# END $OP eq DELETE_CONFIRMED
146
													# END $OP eq DELETE_CONFIRMED
187
################## DEFAULT ##################################
147
################## DEFAULT ##################################
188
} else { # DEFAULT
148
} else { # DEFAULT
Lines 219-221 if ( $op eq 'add_form' ) { Link Here
219
	}
179
	}
220
} #---- END $OP eq DEFAULT
180
} #---- END $OP eq DEFAULT
221
output_html_with_http_headers $input, $cookie, $template->output;
181
output_html_with_http_headers $input, $cookie, $template->output;
222
- 
182
183
sub castCGIToZ3950serverParams {
184
    my ($cgi) = @_;
185
186
    my $params = {};
187
    $params->{'host'} =        $input->param('host'),
188
    $params->{'port'} =        $input->param('port'),
189
    $params->{'db'} =          $input->param('db'),
190
    $params->{'userid'} =      $input->param('userid'),
191
    $params->{'password'} =    $input->param('password'),
192
    $params->{'name'} =        $input->param('searchfield'),
193
    $params->{'checked'} =     $input->param('checked') ? 1 : 0,
194
    $params->{'rank'} =        $input->param('rank'),
195
    $params->{'syntax'} =      $input->param('syntax'),
196
    $params->{'encoding'} =    $input->param('encoding'),
197
    $params->{'timeout'} =     $input->param('timeout'),
198
    $params->{'recordtype'} =  $input->param('recordtype'),
199
    $params->{'searchfield'} = $input->param('searchfield'),
200
    return $params;
201
}

Return to bug 15875