New framework handling. New koha_attr table for mapping MARC to koha
[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::Output;
28 use C4::Search;
29 use C4::Auth;
30 use C4::Interface::CGI::Output;
31
32 sub StringSearch  {
33         my ($env,$searchstring,$type)=@_;
34         my $dbh = C4::Context->dbh;
35         $searchstring=~ s/\'/\\\'/g;
36         my @data=split(' ',$searchstring);
37         my $count=@data;
38         my $sth=$dbh->prepare("Select * from auth_types where (authtypecode like ?) order by authtypecode");
39         $sth->execute("$data[0]%");
40         my @results;
41         while (my $data=$sth->fetchrow_hashref){
42         push(@results,$data);
43         }
44         #  $sth->execute;
45         $sth->finish;
46         return (scalar(@results),\@results);
47 }
48
49 my $input = new CGI;
50 my $searchfield=$input->param('authtypecode');
51 my $offset=$input->param('offset');
52 my $script_name="/cgi-bin/koha/admin/authtypes.pl";
53 my $authtypecode=$input->param('authtypecode');
54 my $pagesize=20;
55 my $op = $input->param('op');
56 $searchfield=~ s/\,//g;
57 my ($template, $borrowernumber, $cookie)
58     = get_template_and_user({template_name => "admin/authtypes.tmpl",
59                              query => $input,
60                              type => "intranet",
61                              authnotrequired => 0,
62                              flagsrequired => {parameters => 1},
63                              debug => 1,
64                              });
65
66 if ($op) {
67 $template->param(script_name => $script_name,
68                                                 $op              => 1); # we show only the TMPL_VAR names $op
69 } else {
70 $template->param(script_name => $script_name,
71                                                 else              => 1); # we show only the TMPL_VAR names $op
72 }
73 ################## ADD_FORM ##################################
74 # called by default. Used to create form to add or  modify a record
75 if ($op eq 'add_form') {
76         #start the page and read in includes
77         #---- if primkey exists, it's a modify action, so read values to modify...
78         my $data;
79         if ($authtypecode) {
80                 my $dbh = C4::Context->dbh;
81                 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
82                 $sth->execute($authtypecode);
83                 $data=$sth->fetchrow_hashref;
84                 $sth->finish;
85         }
86 #       warn "=> $data->{'authtypetext'} : ".$data->{'summary'};
87         $template->param(authtypecode => $authtypecode,
88                                                         authtypetext => $data->{'authtypetext'},
89                                                         auth_tag_to_report => $data->{'auth_tag_to_report'},
90                                                         summary => $data->{'summary'},
91                                                         );
92 ;
93                                                                                                         # END $OP eq ADD_FORM
94 ################## ADD_VALIDATE ##################################
95 # called by add_form, used to insert/modify data in DB
96 } elsif ($op eq 'add_validate') {
97         my $dbh = C4::Context->dbh;
98         my $sth=$dbh->prepare("replace auth_types (authtypecode,authtypetext,auth_tag_to_report,summary) values (?,?,?,?)");
99         $sth->execute($input->param('authtypecode'),$input->param('authtypetext'),$input->param('auth_tag_to_report'),$input->param('summary'));
100         $sth->finish;
101         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authtypes.pl\"></html>";
102         exit;
103                                                                                                         # END $OP eq ADD_VALIDATE
104 ################## DELETE_CONFIRM ##################################
105 # called by default form, used to confirm deletion of data in DB
106 } elsif ($op eq 'delete_confirm') {
107         #start the page and read in includes
108         my $dbh = C4::Context->dbh;
109
110         my $total = 0;
111         for my $table ('auth_tag_structure') {
112            my $sth=$dbh->prepare("select count(*) as total from $table where authtypecode=?");
113            $sth->execute($authtypecode);
114            $total += $sth->fetchrow_hashref->{total};
115            $sth->finish;
116         }
117
118         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
119         $sth->execute($authtypecode);
120         my $data=$sth->fetchrow_hashref;
121         $sth->finish;
122
123         $template->param(authtypecode => $authtypecode,
124                                                         authtypetext => $data->{'authtypetext'},
125                                                         summary => $data->{'summary'},
126                                                         total => $total);
127                                                                                                         # END $OP eq DELETE_CONFIRM
128 ################## DELETE_CONFIRMED ##################################
129 # called by delete_confirm, used to effectively confirm deletion of data in DB
130 } elsif ($op eq 'delete_confirmed') {
131         #start the page and read in includes
132         my $dbh = C4::Context->dbh;
133         my $authtypecode=uc($input->param('authtypecode'));
134         my $sth=$dbh->prepare("delete from auth_tag_structure where authtypecode=?");
135         $sth->execute($authtypecode);
136         $sth=$dbh->prepare("delete from auth_subfield_structure where authtypecode=?");
137         $sth->execute($authtypecode);
138         $sth=$dbh->prepare("delete from auth_types where authtypecode=?");
139         $sth->execute($authtypecode);
140         $sth->finish;
141         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authtypes.pl\"></html>";
142         exit;
143                                                                                                         # END $OP eq DELETE_CONFIRMED
144 ################## DEFAULT ##################################
145 } else { # DEFAULT
146         my $env;
147         my ($count,$results)=StringSearch($env,$searchfield,'web');
148         my $toggle="white";
149         my @loop_data;
150         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
151                 my %row_data;
152                 if ($toggle eq 'white'){
153                         $row_data{toggle}="#ffffcc";
154                 } else {
155                         $row_data{toggle}="white";
156                 }
157                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
158                 $row_data{authtypetext} = $results->[$i]{'authtypetext'};
159                 $row_data{auth_tag_to_report} = $results->[$i]{'auth_tag_to_report'};
160                 $row_data{summary} = $results->[$i]{'summary'};
161                 push(@loop_data, \%row_data);
162         }
163         $template->param(loop => \@loop_data);
164         if ($offset>0) {
165                 my $prevpage = $offset-$pagesize;
166                 $template->param(previous => "$script_name?offset=".$prevpage);
167         }
168         if ($offset+$pagesize<$count) {
169                 my $nextpage =$offset+$pagesize;
170                 $template->param(next => "$script_name?offset=".$nextpage);
171         }
172 } #---- END $OP eq DEFAULT
173 output_html_with_http_headers $input, $cookie, $template->output;
174
175 # Local Variables:
176 # tab-width: 4
177 # End: