adding authentification with Auth.pm
[koha.git] / admin / marctagstructure.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Output;
26 use C4::Search;
27 use C4::Context;
28 use HTML::Template;
29
30 sub StringSearch  {
31         my ($env,$searchstring,$type)=@_;
32         my $dbh = C4::Context->dbh;
33         $searchstring=~ s/\'/\\\'/g;
34         my @data=split(' ',$searchstring);
35         my $count=@data;
36         my $query="Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield like \"$data[0]%\") order by tagfield";
37         my $sth=$dbh->prepare($query);
38         $sth->execute;
39         my @results;
40         my $cnt=0;
41         while (my $data=$sth->fetchrow_hashref){
42         push(@results,$data);
43         $cnt ++;
44         }
45         #  $sth->execute;
46         $sth->finish;
47         return ($cnt,\@results);
48 }
49
50 my $input = new CGI;
51 my $searchfield=$input->param('searchfield');
52 my $pkfield="tagfield";
53 my $reqsel="select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where $pkfield='$searchfield'";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
56
57 my $dbh = C4::Context->dbh;
58
59 my ($template, $loggedinuser, $cookie)
60     = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
61                              query => $input,
62                              type => "intranet",
63                              authnotrequired => 0,
64                              flagsrequired => {parameters => 1},
65                              debug => 1,
66                              });
67 my $pagesize=20;
68 my $op = $input->param('op');
69 $searchfield=~ s/\,//g;
70
71 if ($op) {
72 $template->param(script_name => $script_name,
73                                                 $op              => 1); # we show only the TMPL_VAR names $op
74 } else {
75 $template->param(script_name => $script_name,
76                                                 else              => 1); # we show only the TMPL_VAR names $op
77 }
78
79 ################## ADD_FORM ##################################
80 # called by default. Used to create form to add or  modify a record
81 if ($op eq 'add_form') {
82         #---- if primkey exists, it's a modify action, so read values to modify...
83         my $data;
84         if ($searchfield) {
85                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where $pkfield='$searchfield'");
86                 $sth->execute;
87                 $data=$sth->fetchrow_hashref;
88                 $sth->finish;
89         }
90         my $sth = $dbh->prepare("select distinct category from authorised_values");
91         $sth->execute;
92         my @authorised_values;
93         push @authorised_values,"";
94         while ((my $category) = $sth->fetchrow_array) {
95                 push @authorised_values, $category;
96         }
97         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
98                         -values=> \@authorised_values,
99                         -size=>1,
100                         -multiple=>0,
101                         -default => $data->{'authorised_value'},
102                         );
103
104         if ($searchfield) {
105                 $template->param(action => "Modify tag",
106                                                                 searchfield => "<input type=hidden name=tagfield value='$searchfield'>$searchfield");
107         } else {
108                 $template->param(action => "Add tag",
109                                                                 searchfield => "<input type=text name=tagfield size=5 maxlength=3>");
110         }
111         $template->param(liblibrarian => $data->{'liblibrarian'},
112                                                         libopac => $data->{'libopac'},
113                                                         repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
114                                                         mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
115                                                         authorised_value => $authorised_value,
116                                                         );
117                                                                                                         # END $OP eq ADD_FORM
118 ################## ADD_VALIDATE ##################################
119 # called by add_form, used to insert/modify data in DB
120 } elsif ($op eq 'add_validate') {
121         my $dbh = C4::Context->dbh;
122         my $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value) values (?,?,?,?,?,?)");
123         my $tagfield       =$input->param('tagfield');
124         my $liblibrarian  = $input->param('liblibrarian');
125         my $libopac       =$input->param('libopac');
126         my $repeatable =$input->param('repeatable');
127         my $mandatory =$input->param('mandatory');
128         my $authorised_value =$input->param('authorised_value');
129         $sth->execute($tagfield,
130                                                 $liblibrarian,
131                                                 $libopac,
132                                                 $repeatable?1:0,
133                                                 $mandatory?1:0,
134                                                 $authorised_value
135                                                 );
136         $sth->finish;
137         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl\"></html>";
138         exit;
139                                                                                                         # END $OP eq ADD_VALIDATE
140 ################## DELETE_CONFIRM ##################################
141 # called by default form, used to confirm deletion of data in DB
142 } elsif ($op eq 'delete_confirm') {
143         my $dbh = C4::Context->dbh;
144         my $sth=$dbh->prepare($reqsel);
145         $sth->execute;
146         my $data=$sth->fetchrow_hashref;
147         $sth->finish;
148         $template->param(liblibrarian => $data->{'liblibrarian'},
149                                                         searchfield => $searchfield,
150                                                         );
151                                                                                                         # END $OP eq DELETE_CONFIRM
152 ################## DELETE_CONFIRMED ##################################
153 # called by delete_confirm, used to effectively confirm deletion of data in DB
154 } elsif ($op eq 'delete_confirmed') {
155         my $dbh = C4::Context->dbh;
156         $dbh->do("delete from marc_tag_structure where $pkfield='$searchfield'");
157         $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
158                                                                                                         # END $OP eq DELETE_CONFIRMED
159 ################## DEFAULT ##################################
160 } else { # DEFAULT
161         if  ($searchfield ne '') {
162                  $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
163         }
164         my $env;
165         my ($count,$results)=StringSearch($env,$searchfield,'web');
166         my $toggle="white";
167         my @loop_data = ();
168         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
169                 if ($toggle eq 'white'){
170                         $toggle="#ffffcc";
171                 } else {
172                         $toggle="white";
173                 }
174                 my %row_data;  # get a fresh hash for the row data
175                 $row_data{tagfield} = $results->[$i]{'tagfield'};
176                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
177                 $row_data{repeatable} = $results->[$i]{'repeatable'};
178                 $row_data{mandatory} = $results->[$i]{'mandatory'};
179                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
180                 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'};
181                 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'};
182                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'};
183                 $row_data{bgcolor} = $toggle;
184                 push(@loop_data, \%row_data);
185         }
186         $template->param(loop => \@loop_data);
187         if ($offset>0) {
188                 my $prevpage = $offset-$pagesize;
189                 $template->param(previous => "<a href=$script_name?offset=".$prevpage.'>&lt;&lt; Prev</a>');
190         }
191         if ($offset+$pagesize<$count) {
192                 my $nextpage =$offset+$pagesize;
193                 $template->param(next => "<a href=$script_name?offset=".$nextpage.'>Next &gt;&gt;</a>');
194         }
195 } #---- END $OP eq DEFAULT
196
197 $template->param(loggeninuser => $loggedinuser);
198 print $input->header(-cookie => $cookie), $template->output;