Use DBI not mysql command-line to load the database tables
[koha.git] / admin / authtypes.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29
30
31 sub StringSearch  {
32         my ($searchstring,$type)=@_;
33         my $dbh = C4::Context->dbh;
34         $searchstring=~ s/\'/\\\'/g;
35         my @data=split(' ',$searchstring);
36         my $count=@data;
37         my $sth=$dbh->prepare("Select * from auth_types where (authtypecode like ?) order by authtypecode");
38         $sth->execute("$data[0]%");
39         my @results;
40         while (my $data=$sth->fetchrow_hashref){
41         push(@results,$data);
42         }
43         #  $sth->execute;
44         $sth->finish;
45         return (scalar(@results),\@results);
46 }
47
48 my $input = new CGI;
49 my $searchfield=$input->param('authtypecode');
50 my $offset=$input->param('offset');
51 my $script_name="/cgi-bin/koha/admin/authtypes.pl";
52 my $authtypecode=$input->param('authtypecode');
53 my $pagesize=20;
54 my $op = $input->param('op');
55 $searchfield=~ s/\,//g;
56 my ($template, $borrowernumber, $cookie)
57     = get_template_and_user({template_name => "admin/authtypes.tmpl",
58                              query => $input,
59                              type => "intranet",
60                              authnotrequired => 0,
61                              flagsrequired => {parameters => 1},
62                              debug => 1,
63                              });
64
65 if ($op) {
66 $template->param(script_name => $script_name,
67                                                 $op              => 1); # we show only the TMPL_VAR names $op
68 } else {
69 $template->param(script_name => $script_name,
70                                                 'else'              => 1); # we show only the TMPL_VAR names $op
71 }
72 ################## ADD_FORM ##################################
73 # called by default. Used to create form to add or  modify a record
74 if ($op eq 'add_form') {
75         #start the page and read in includes
76         #---- if primkey exists, it's a modify action, so read values to modify...
77         my $data;
78         if ($authtypecode) {
79                 my $dbh = C4::Context->dbh;
80                 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
81                 $sth->execute($authtypecode);
82                 $data=$sth->fetchrow_hashref;
83                 $sth->finish;
84         }
85         warn "=> $data->{'authtypetext'} : ".$data->{'summary'};
86         $template->param(authtypecode => $authtypecode,
87                                                         authtypetext => $data->{'authtypetext'},
88                                                         auth_tag_to_report => $data->{'auth_tag_to_report'},
89                                                         summary => $data->{'summary'},
90                                                         );
91 ;
92                                                                                                         # END $OP eq ADD_FORM
93 ################## ADD_VALIDATE ##################################
94 # called by add_form, used to insert/modify data in DB
95 } elsif ($op eq 'add_validate') {
96         my $dbh = C4::Context->dbh;
97         my $sth=$dbh->prepare("replace auth_types (authtypecode,authtypetext,auth_tag_to_report,summary) values (?,?,?,?)");
98         $sth->execute($input->param('authtypecode'),$input->param('authtypetext'),$input->param('auth_tag_to_report'),$input->param('summary'));
99         $sth->finish;
100         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authtypes.pl\"></html>";
101         exit;
102                                                                                                         # END $OP eq ADD_VALIDATE
103 ################## DELETE_CONFIRM ##################################
104 # called by default form, used to confirm deletion of data in DB
105 } elsif ($op eq 'delete_confirm') {
106         #start the page and read in includes
107         my $dbh = C4::Context->dbh;
108
109         my $total = 0;
110         for my $table ('auth_tag_structure') {
111            my $sth=$dbh->prepare("select count(*) as total from $table where authtypecode=?");
112            $sth->execute($authtypecode);
113            $total += $sth->fetchrow_hashref->{total};
114            $sth->finish;
115         }
116
117         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
118         $sth->execute($authtypecode);
119         my $data=$sth->fetchrow_hashref;
120         $sth->finish;
121
122         $template->param(authtypecode => $authtypecode,
123                                                         authtypetext => $data->{'authtypetext'},
124                                                         summary => $data->{'summary'},
125                                                         total => $total);
126                                                                                                         # END $OP eq DELETE_CONFIRM
127 ################## DELETE_CONFIRMED ##################################
128 # called by delete_confirm, used to effectively confirm deletion of data in DB
129 } elsif ($op eq 'delete_confirmed') {
130         #start the page and read in includes
131         my $dbh = C4::Context->dbh;
132         my $authtypecode=uc($input->param('authtypecode'));
133         my $sth=$dbh->prepare("delete from auth_tag_structure where authtypecode=?");
134         $sth->execute($authtypecode);
135         $sth=$dbh->prepare("delete from auth_subfield_structure where authtypecode=?");
136         $sth->execute($authtypecode);
137         $sth=$dbh->prepare("delete from auth_types where authtypecode=?");
138         $sth->execute($authtypecode);
139         $sth->finish;
140         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authtypes.pl\"></html>";
141         exit;
142                                                                                                         # END $OP eq DELETE_CONFIRMED
143 ################## DEFAULT ##################################
144 } else { # DEFAULT
145         my ($count,$results)=StringSearch($searchfield,'web');
146         my $toggle="white";
147         my @loop_data;
148         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
149                 my %row_data;
150                 if ($toggle eq 'white'){
151                         $row_data{toggle}="#ffffcc";
152                 } else {
153                         $row_data{toggle}="white";
154                 }
155                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
156                 $row_data{authtypetext} = $results->[$i]{'authtypetext'};
157                 $row_data{auth_tag_to_report} = $results->[$i]{'auth_tag_to_report'};
158                 $row_data{summary} = $results->[$i]{'summary'};
159                 push(@loop_data, \%row_data);
160         }
161         $template->param(loop => \@loop_data);
162         if ($offset>0) {
163                 my $prevpage = $offset-$pagesize;
164                 $template->param(previous => "$script_name?offset=".$prevpage);
165         }
166         if ($offset+$pagesize<$count) {
167                 my $nextpage =$offset+$pagesize;
168                 $template->param(next => "$script_name?offset=".$nextpage);
169         }
170 } #---- END $OP eq DEFAULT
171 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
172                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
173                 IntranetNav => C4::Context->preference("IntranetNav"),
174                 );
175 output_html_with_http_headers $input, $cookie, $template->output;
176
177 # Local Variables:
178 # tab-width: 4
179 # End: