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 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
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.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
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;
48 my ($template, $loggedinuser, $cookie)
49 = get_template_and_user
({template_name
=> "admin/auth_tag_structure.tmpl",
53 flagsrequired
=> {parameters
=> 1},
58 my $authtypes = getauthtypes
;
60 foreach my $thisauthtype (keys %$authtypes) {
61 my $selected = 1 if $thisauthtype eq $authtypecode;
62 my %row =(value
=> $thisauthtype,
63 selected
=> $selected,
64 authtypetext
=> $authtypes->{$thisauthtype}->{'authtypetext'},
66 push @authtypesloop, \
%row;
70 # check that authtype framework is defined in auth_tag_structure if we are on a default action
71 if (!$op or $op eq 'authtype_create_confirm') {
72 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
73 $sth->execute($authtypecode);
74 my ($authtypeexist) = $sth->fetchrow;
77 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
78 # (op = authtyp_create_confirm)
79 if ($op eq "authtype_create_confirm") {
80 duplicate_auth_framework
($authtypecode, $existingauthtypecode);
82 $op = "authtype_create";
86 $template->param(script_name
=> $script_name);
87 $template->param(authtypeloop
=> \
@authtypesloop);
88 if ($op && $op ne 'authtype_create_confirm') {
89 $template->param($op => 1);
91 $template->param(else => 1);
94 ################## ADD_FORM ##################################
95 # called by default. Used to create form to add or modify a record
96 if ($op eq 'add_form') {
97 #---- if primkey exists, it's a modify action, so read values to modify...
100 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
101 $sth->execute($searchfield,$authtypecode);
102 $data=$sth->fetchrow_hashref;
104 my $sth = $dbh->prepare("select distinct category from authorised_values");
106 my @authorised_values;
107 push @authorised_values,"";
108 while ((my $category) = $sth->fetchrow_array) {
109 push @authorised_values, $category;
111 my $authorised_value = CGI
::scrolling_list
(-name
=>'authorised_value',
112 -id
=>'authorised_value',
113 -values=> \
@authorised_values,
117 -default => $data->{'authorised_value'},
121 $template->param(action
=> "Modify tag",
122 searchfield
=> "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
123 $template->param('heading-modify-tag-p' => 1);
125 $template->param(action
=> "Add tag",
126 searchfield
=> "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
127 $template->param('heading-add-tag-p' => 1);
129 $template->param('use-heading-flags-p' => 1);
130 $template->param(liblibrarian
=> $data->{'liblibrarian'},
131 libopac
=> $data->{'libopac'},
132 repeatable
=> "".$data->{'repeatable'},
133 mandatory
=> "".$data->{'mandatory'},
134 authorised_value
=> $authorised_value,
135 authtypecode
=> $authtypecode,
137 # END $OP eq ADD_FORM
138 ################## ADD_VALIDATE ##################################
139 # called by add_form, used to insert/modify data in DB
140 } elsif ($op eq 'add_validate') {
141 my $tagfield = $input->param('tagfield');
142 my $liblibrarian = $input->param('liblibrarian');
143 my $libopac = $input->param('libopac');
144 my $repeatable = $input->param('repeatable') ?
1 : 0;
145 my $mandatory = $input->param('mandatory') ?
1 : 0;
146 my $authorised_value = $input->param('authorised_value');
147 unless (C4
::Context
->config('demo') eq 1) {
148 if ($input->param('modif')) {
149 $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
161 $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
173 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
175 # END $OP eq ADD_VALIDATE
176 ################## DELETE_CONFIRM ##################################
177 # called by default form, used to confirm deletion of data in DB
178 } elsif ($op eq 'delete_confirm') {
179 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
180 $sth->execute($searchfield);
181 my $data=$sth->fetchrow_hashref;
182 $template->param(liblibrarian
=> $data->{'liblibrarian'},
183 searchfield
=> $searchfield,
184 authtypecode
=> $authtypecode,
186 # END $OP eq DELETE_CONFIRM
187 ################## DELETE_CONFIRMED ##################################
188 # called by delete_confirm, used to effectively confirm deletion of data in DB
189 } elsif ($op eq 'delete_confirmed') {
190 unless (C4
::Context
->config('demo') eq 1) {
191 $dbh->do("delete from auth_tag_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
192 $dbh->do("delete from auth_subfield_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
193 # FIXME: Secuity vulnerability -- use placeholders, prepare and execute!
195 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
197 # END $OP eq DELETE_CONFIRMED
198 ################## ITEMTYPE_CREATE ##################################
199 # called automatically if an unexisting authtypecode is selected
200 } elsif ($op eq 'authtype_create') {
201 $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");
203 my @existingauthtypeloop;
204 while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
206 my %line = ( value
=> $thisauthtype,
207 authtypetext
=> $authtypetext,
209 push @existingauthtypeloop,\
%line;
212 $template->param(existingauthtypeloop
=> \
@existingauthtypeloop,
213 authtypecode
=> $authtypecode,
215 ################## DEFAULT ##################################
217 # here, $op can be unset or set to "authtype_create_confirm".
218 # warn "authtype : $authtypecode";
219 if ($searchfield ne '') {
220 $template->param(searchfield
=> $searchfield);
222 my ($count,$results)=StringSearch
($searchfield,$authtypecode);
224 for (my $i=$offset; $i < ($offset+$pagesize<$count?
$offset+$pagesize:$count); $i++){
225 my %row_data; # get a fresh hash for the row data
226 $row_data{tagfield
} = $results->[$i]{'tagfield'};
227 $row_data{liblibrarian
} = $results->[$i]{'liblibrarian'};
228 $row_data{repeatable
} = $results->[$i]{'repeatable'};
229 $row_data{mandatory
} = $results->[$i]{'mandatory'};
230 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
231 $row_data{subfield_link
} = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&authtypecode=" . $authtypecode;
232 $row_data{edit
} = "$script_name?op=add_form&searchfield=" . $results->[$i]{'tagfield'} . "&authtypecode=" . $authtypecode;
233 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=" . $results->[$i]{'tagfield'} . "&authtypecode=" . $authtypecode;
234 push(@loop_data, \
%row_data);
236 $template->param(loop => \
@loop_data,
237 authtypecode
=> $authtypecode,
240 my $prevpage = $offset-$pagesize;
241 $template->param(isprevpage
=> $offset,
242 prevpage
=> $prevpage,
243 searchfield
=> $searchfield,
246 if ($offset+$pagesize<$count) {
247 my $nextpage =$offset+$pagesize;
248 $template->param(nextpage
=>$nextpage,
249 searchfield
=> $searchfield,
252 } #---- END $OP eq DEFAULT
254 output_html_with_http_headers
$input, $cookie, $template->output;
257 # the sub used for searches
260 my ($searchstring,$authtypecode)=@_;
261 my $dbh = C4
::Context
->dbh;
262 $searchstring=~ s/\'/\\\'/g;
263 my @data=split(' ',$searchstring);
264 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
265 $sth->execute($data[0], $authtypecode);
267 while (my $data=$sth->fetchrow_hashref){
268 push(@results,$data);
270 return (scalar(@results),\
@results);
274 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
276 sub duplicate_auth_framework
{
277 my ($newauthtype,$oldauthtype) = @_;
278 # warn "TO $newauthtype FROM $oldauthtype";
279 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
280 $sth->execute($oldauthtype);
281 my $sth_insert = $dbh->prepare("insert into auth_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
282 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
283 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
286 $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
287 $sth->execute($oldauthtype);
288 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
289 while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
290 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);