Cleanup - admin scripts
[koha.git] / admin / auth_tag_structure.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::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Context;
28
29
30 # retrieve parameters
31 my $input = new CGI;
32 my $authtypecode         = $input->param('authtypecode')         || '';    # set to select framework
33 my $existingauthtypecode = $input->param('existingauthtypecode') || '';    # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
34
35 # my $authtypeinfo = getauthtypeinfo($authtype);
36 my $searchfield = $input->param('searchfield') || 0;
37 my $offset      = $input->param('offset') || 0;
38 my $op          = $input->param('op')     || '';
39 $searchfield =~ s/\,//g;
40
41 my $pagesize    = 20;
42 my $script_name = "/cgi-bin/koha/admin/auth_tag_structure.pl";
43
44 my $dbh = C4::Context->dbh;
45
46 # open template
47 my ($template, $loggedinuser, $cookie)
48     = get_template_and_user({template_name => "admin/auth_tag_structure.tmpl",
49                              query => $input,
50                              type => "intranet",
51                              authnotrequired => 0,
52                              flagsrequired => {parameters => 1},
53                              debug => 1,
54                              });
55
56 # get authtype list
57 my $authtypes = getauthtypes;
58 my @authtypesloop;
59 foreach my $thisauthtype (keys %$authtypes) {
60         my $selected = 1 if $thisauthtype eq $authtypecode;
61         my %row =(value => $thisauthtype,
62                                 selected => $selected,
63                                 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
64                         );
65         push @authtypesloop, \%row;
66 }
67
68 my $sth;
69 # check that authtype framework is defined in auth_tag_structure if we are on a default action
70 if (!$op or $op eq 'authtype_create_confirm') {
71         $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
72         $sth->execute($authtypecode);
73         my ($authtypeexist) = $sth->fetchrow;
74         if ($authtypeexist) {
75         } else {
76                 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
77                 # (op = authtyp_create_confirm)
78                 if ($op eq "authtype_create_confirm") {
79                         duplicate_auth_framework($authtypecode, $existingauthtypecode);
80                 } else {
81                         $op = "authtype_create";
82                 }
83         }
84 }
85 $template->param(script_name  => $script_name);
86 $template->param(authtypeloop => \@authtypesloop);
87 if ($op ne 'authtype_create_confirm') {
88     $template->param($op  => 1);
89 } else {
90     $template->param(else => 1);
91 }
92  
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or  modify a record
95 if ($op eq 'add_form') {
96         #---- if primkey exists, it's a modify action, so read values to modify...
97         my $data;
98         if ($searchfield) {
99                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
100                 $sth->execute($searchfield,$authtypecode);
101                 $data=$sth->fetchrow_hashref;
102         }
103         my $sth = $dbh->prepare("select distinct category from authorised_values");
104         $sth->execute;
105         my @authorised_values;
106         push @authorised_values,"";
107         while ((my $category) = $sth->fetchrow_array) {
108                 push @authorised_values, $category;
109         }
110         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
111                 -id=>'authorised_value',
112                         -values=> \@authorised_values,
113                         -size=>1,
114                         -tabindex=>'',
115                         -multiple=>0,
116                         -default => $data->{'authorised_value'},
117                         );
118
119         if ($searchfield) {
120                 $template->param(action => "Modify tag",
121                                                                 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
122                 $template->param('heading-modify-tag-p' => 1);
123         } else {
124                 $template->param(action => "Add tag",
125                                                                 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
126                 $template->param('heading-add-tag-p' => 1);
127         }
128         $template->param('use-heading-flags-p' => 1);
129         $template->param(liblibrarian => $data->{'liblibrarian'},
130                                                         libopac => $data->{'libopac'},
131                                                         repeatable => "".$data->{'repeatable'},
132                                                         mandatory => "".$data->{'mandatory'},
133                                                         authorised_value => $authorised_value,
134                                                         authtypecode => $authtypecode,
135                                                         );
136                                                                                                         # END $OP eq ADD_FORM
137 ################## ADD_VALIDATE ##################################
138 # called by add_form, used to insert/modify data in DB
139 } elsif ($op eq 'add_validate') {
140     my $tagfield         = $input->param('tagfield');
141     my $liblibrarian     = $input->param('liblibrarian');
142     my $libopac          = $input->param('libopac');
143     my $repeatable       = $input->param('repeatable') ? 1 : 0;
144     my $mandatory        = $input->param('mandatory')  ? 1 : 0;
145     my $authorised_value = $input->param('authorised_value');
146     unless (C4::Context->config('demo') eq 1) {
147         if ($input->param('modif')) {
148             $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
149             $sth->execute(
150                 $tagfield,
151                 $liblibrarian,
152                 $libopac,
153                 $repeatable,
154                 $mandatory,
155                 $authorised_value,
156                 $authtypecode,
157                 $tagfield,
158             );
159         } else {
160             $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
161             $sth->execute(
162                 $tagfield,
163                 $liblibrarian,
164                 $libopac,
165                 $repeatable,
166                 $mandatory,
167                 $authorised_value,
168                 $authtypecode
169            );
170         }
171     }
172         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
173         exit;
174                                                                                                         # END $OP eq ADD_VALIDATE
175 ################## DELETE_CONFIRM ##################################
176 # called by default form, used to confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirm') {
178         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
179         $sth->execute($searchfield);
180         my $data=$sth->fetchrow_hashref;
181         $template->param(liblibrarian => $data->{'liblibrarian'},
182                                                         searchfield => $searchfield,
183                                                         authtypecode => $authtypecode,
184                                                         );
185                                                                                                         # END $OP eq DELETE_CONFIRM
186 ################## DELETE_CONFIRMED ##################################
187 # called by delete_confirm, used to effectively confirm deletion of data in DB
188 } elsif ($op eq 'delete_confirmed') {
189         unless (C4::Context->config('demo') eq 1) {
190                 $dbh->do("delete from auth_tag_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
191                 $dbh->do("delete from auth_subfield_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
192         # FIXME: Secuity vulnerability -- use placeholders, prepare and execute!
193         }
194     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
195     exit;
196                                                                                                         # END $OP eq DELETE_CONFIRMED
197 ################## ITEMTYPE_CREATE ##################################
198 # called automatically if an unexisting authtypecode is selected
199 } elsif ($op eq 'authtype_create') {
200         $sth = $dbh->prepare("select count(*),auth_tag_structure.authtypecode,authtypetext from auth_tag_structure,auth_types where auth_types.authtypecode=auth_tag_structure.authtypecode group by auth_tag_structure.authtypecode");
201         $sth->execute;
202         my @existingauthtypeloop;
203         while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
204                 if ($tot>0) {
205                         my %line = ( value => $thisauthtype,
206                                                 authtypetext => $authtypetext,
207                                         );
208                         push @existingauthtypeloop,\%line;
209                 }
210         }
211         $template->param(existingauthtypeloop => \@existingauthtypeloop,
212                                         authtypecode => $authtypecode,
213                                         );
214 ################## DEFAULT ##################################
215 } else { # DEFAULT
216         # here, $op can be unset or set to "authtype_create_confirm".
217 #       warn "authtype : $authtypecode";
218         if  ($searchfield ne '') {
219                  $template->param(searchfield => $searchfield);
220         }
221         my ($count,$results)=StringSearch($searchfield,$authtypecode);
222         my $toggle=1;
223         my @loop_data = ();
224         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
225                 if ($toggle eq 1){
226                         $toggle=0;
227                 } else {
228                         $toggle=1;
229                 }
230                 my %row_data;  # get a fresh hash for the row data
231         $row_data{tagfield}         = $results->[$i]{'tagfield'};
232         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
233         $row_data{repeatable}       = $results->[$i]{'repeatable'};
234         $row_data{mandatory}        = $results->[$i]{'mandatory'};
235         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
236         $row_data{subfield_link}    = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
237         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
238         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
239         $row_data{toggle}           = $toggle;
240                 push(@loop_data, \%row_data);
241         }
242         $template->param(loop => \@loop_data,
243                                         authtypecode => $authtypecode,
244         );
245         if ($offset>0) {
246                 my $prevpage = $offset-$pagesize;
247                 $template->param(isprevpage => $offset,
248                                                 prevpage=> $prevpage,
249                                                 searchfield => $searchfield,
250                  );
251         }
252         if ($offset+$pagesize<$count) {
253                 my $nextpage =$offset+$pagesize;
254                 $template->param(nextpage =>$nextpage,
255                                                 searchfield => $searchfield,
256                 );
257         }
258 } #---- END $OP eq DEFAULT
259
260 output_html_with_http_headers $input, $cookie, $template->output;
261
262 #
263 # the sub used for searches
264 #
265 sub StringSearch  {
266         my ($searchstring,$authtypecode)=@_;
267         my $dbh = C4::Context->dbh;
268         $searchstring=~ s/\'/\\\'/g;
269         my @data=split(' ',$searchstring);
270         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
271         $sth->execute($data[0], $authtypecode);
272         my @results;
273         while (my $data=$sth->fetchrow_hashref){
274         push(@results,$data);
275         }
276         return (scalar(@results),\@results);
277 }
278
279 #
280 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
281 #
282 sub duplicate_auth_framework {
283         my ($newauthtype,$oldauthtype) = @_;
284 #       warn "TO $newauthtype FROM $oldauthtype";
285         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
286         $sth->execute($oldauthtype);
287         my $sth_insert = $dbh->prepare("insert into auth_tag_structure  (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
288         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
289                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
290         }
291
292         $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
293         $sth->execute($oldauthtype);
294         $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
295         while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
296                 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);
297         }
298 }
299