Bug 14487: Noise best detected under Debian 8
[koha.git] / admin / auth_tag_structure.pl
blobb00f4455a8614d480d89dcecf3601dbd50dfde3d
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Context;
27 use C4::Output;
28 use C4::Context;
31 # retrieve parameters
32 my $input = new CGI;
33 my $authtypecode = $input->param('authtypecode') || ''; # set to select framework
34 my $existingauthtypecode = $input->param('existingauthtypecode') || ''; # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
36 # my $authtypeinfo = getauthtypeinfo($authtype);
37 my $searchfield = $input->param('searchfield') || 0;
38 my $offset = $input->param('offset') || 0;
39 my $op = $input->param('op') || '';
40 $searchfield =~ s/\,//g;
43 my $script_name = "/cgi-bin/koha/admin/auth_tag_structure.pl";
45 my $dbh = C4::Context->dbh;
47 # open template
48 my ($template, $loggedinuser, $cookie)
49 = get_template_and_user({template_name => "admin/auth_tag_structure.tt",
50 query => $input,
51 type => "intranet",
52 authnotrequired => 0,
53 flagsrequired => {parameters => 'parameters_remaining_permissions'},
54 debug => 1,
55 });
57 # get authtype list
58 my $authtypes = getauthtypes;
59 my @authtypesloop = ();
60 foreach my $thisauthtype ( sort keys %{$authtypes} ) {
61 push @authtypesloop,
62 { value => $thisauthtype,
63 selected => $thisauthtype eq $authtypecode,
64 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
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";
85 $template->param(script_name => $script_name);
86 $template->param(authtypeloop => \@authtypesloop);
87 if ($op && $op ne 'authtype_create_confirm') {
88 $template->param($op => 1);
89 } else {
90 $template->param(else => 1);
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;
104 my @authorised_values = @{C4::Koha::GetAuthorisedValueCategories()}; # function returns array ref, dereferencing
105 unshift @authorised_values, ""; # put empty value first
106 my $authorised_value = {
107 values => \@authorised_values,
108 default => $data->{'authorised_value'},
111 if ($searchfield) {
112 $template->param('searchfield' => $searchfield);
113 $template->param('heading_modify_tag_p' => 1);
114 } else {
115 $template->param('heading_add_tag_p' => 1);
117 $template->param('use_heading_flags_p' => 1);
118 $template->param(liblibrarian => $data->{'liblibrarian'},
119 libopac => $data->{'libopac'},
120 repeatable => "".$data->{'repeatable'},
121 mandatory => "".$data->{'mandatory'},
122 authorised_value => $authorised_value,
123 authtypecode => $authtypecode,
125 # END $OP eq ADD_FORM
126 ################## ADD_VALIDATE ##################################
127 # called by add_form, used to insert/modify data in DB
128 } elsif ($op eq 'add_validate') {
129 my $tagfield = $input->param('tagfield');
130 my $liblibrarian = $input->param('liblibrarian');
131 my $libopac = $input->param('libopac');
132 my $repeatable = $input->param('repeatable') ? 1 : 0;
133 my $mandatory = $input->param('mandatory') ? 1 : 0;
134 my $authorised_value = $input->param('authorised_value');
135 unless (C4::Context->config('demo') eq 1) {
136 if ($input->param('modif')) {
137 $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
138 $sth->execute(
139 $tagfield,
140 $liblibrarian,
141 $libopac,
142 $repeatable,
143 $mandatory,
144 $authorised_value,
145 $authtypecode,
146 $tagfield,
148 } else {
149 $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
150 $sth->execute(
151 $tagfield,
152 $liblibrarian,
153 $libopac,
154 $repeatable,
155 $mandatory,
156 $authorised_value,
157 $authtypecode
161 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
162 exit;
163 # END $OP eq ADD_VALIDATE
164 ################## DELETE_CONFIRM ##################################
165 # called by default form, used to confirm deletion of data in DB
166 } elsif ($op eq 'delete_confirm') {
167 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
168 $sth->execute($searchfield);
169 my $data=$sth->fetchrow_hashref;
170 $template->param(liblibrarian => $data->{'liblibrarian'},
171 searchfield => $searchfield,
172 authtypecode => $authtypecode,
174 # END $OP eq DELETE_CONFIRM
175 ################## DELETE_CONFIRMED ##################################
176 # called by delete_confirm, used to effectively confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirmed') {
178 unless (C4::Context->config('demo') eq 1) {
179 my $sth = $dbh->prepare("delete from auth_tag_structure where tagfield=? and authtypecode=?");
180 $sth->execute($searchfield,$authtypecode);
181 my $sth = $dbh->prepare("delete from auth_subfield_structure where tagfield=? and authtypecode=?");
182 $sth->execute($searchfield,$authtypecode);
184 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
185 exit;
186 # END $OP eq DELETE_CONFIRMED
187 ################## ITEMTYPE_CREATE ##################################
188 # called automatically if an unexisting authtypecode is selected
189 } elsif ($op eq 'authtype_create') {
190 $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");
191 $sth->execute;
192 my @existingauthtypeloop;
193 while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
194 if ($tot>0) {
195 my %line = ( value => $thisauthtype,
196 authtypetext => $authtypetext,
198 push @existingauthtypeloop,\%line;
201 @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
202 $template->param(existingauthtypeloop => \@existingauthtypeloop,
203 authtypecode => $authtypecode,
205 ################## DEFAULT ##################################
206 } else { # DEFAULT
207 # here, $op can be unset or set to "authtype_create_confirm".
208 # warn "authtype : $authtypecode";
209 if ($searchfield ne '') {
210 $template->param(searchfield => $searchfield);
212 my ($count,$results)=StringSearch($searchfield,$authtypecode);
213 my @loop_data = ();
214 for ( my $i = $offset ; $i < $count ; $i++ ) {
215 my %row_data; # get a fresh hash for the row data
216 $row_data{tagfield} = $results->[$i]{'tagfield'};
217 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
218 $row_data{repeatable} = $results->[$i]{'repeatable'};
219 $row_data{mandatory} = $results->[$i]{'mandatory'};
220 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
221 $row_data{subfield_link} = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
222 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
223 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
224 push(@loop_data, \%row_data);
226 $template->param(loop => \@loop_data,
227 authtypecode => $authtypecode,
229 if ($offset>0) {
230 $template->param(isprevpage => $offset,
231 searchfield => $searchfield,
234 if ( $offset < $count ) {
235 $template->param(
236 searchfield => $searchfield,
239 } #---- END $OP eq DEFAULT
241 output_html_with_http_headers $input, $cookie, $template->output;
244 # the sub used for searches
246 sub StringSearch {
247 my ($searchstring,$authtypecode)=@_;
248 my $dbh = C4::Context->dbh;
249 $searchstring=~ s/\'/\\\'/g;
250 my @data=split(' ',$searchstring);
251 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
252 $sth->execute($data[0], $authtypecode);
253 my @results;
254 while (my $data=$sth->fetchrow_hashref){
255 push(@results,$data);
257 return (scalar(@results),\@results);
261 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
263 sub duplicate_auth_framework {
264 my ($newauthtype,$oldauthtype) = @_;
265 # warn "TO $newauthtype FROM $oldauthtype";
266 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
267 $sth->execute($oldauthtype);
268 my $sth_insert = $dbh->prepare("insert into auth_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
269 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
270 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
273 $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
274 $sth->execute($oldauthtype);
275 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
276 while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
277 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);