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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
27 use C4::Interface::CGI::Output;
34 my $authtypecode = $input->param('authtypecode'); # set to select framework
35 $authtypecode="" unless $authtypecode;
36 my $existingauthtypecode = $input->param('existingauthtypecode'); # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
37 $existingauthtypecode = "" unless $existingauthtypecode;
38 # my $authtypeinfo = getauthtypeinfo($authtype);
39 my $searchfield=$input->param('searchfield');
40 $searchfield=0 unless $searchfield;
41 $searchfield=~ s/\,//g;
43 my $offset=$input->param('offset');
44 my $op = $input->param('op');
47 my $script_name="/cgi-bin/koha/admin/auth_tag_structure.pl";
49 my $dbh = C4::Context->dbh;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "parameters/auth_tag_structure.tmpl",
57 flagsrequired => {parameters => 1},
62 my $authtypes = getauthtypes;
64 foreach my $thisauthtype (keys %$authtypes) {
65 my $selected = 1 if $thisauthtype eq $authtypecode;
66 my %row =(value => $thisauthtype,
67 selected => $selected,
68 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
70 push @authtypesloop, \%row;
74 # check that authtype framework is defined in auth_tag_structure if we are on a default action
75 if (!$op or $op eq 'authtype_create_confirm') {
77 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
78 $sth->execute($authtypecode);
79 my ($authtypeexist) = $sth->fetchrow;
82 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
83 # (op = authtyp_create_confirm)
84 if ($op eq "authtype_create_confirm") {
85 duplicate_auth_framework($authtypecode, $existingauthtypecode);
87 $op = "authtype_create";
91 $template->param(authtypeloop => \@authtypesloop);
92 if ($op && $op ne 'authtype_create_confirm') {
93 $template->param(script_name => $script_name,
94 $op => 1); # we show only the TMPL_VAR names $op
96 $template->param(script_name => $script_name,
97 else => 1); # we show only the TMPL_VAR names $op
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or modify a record
102 if ($op eq 'add_form') {
103 #---- if primkey exists, it's a modify action, so read values to modify...
106 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
107 $sth->execute($searchfield,$authtypecode);
108 $data=$sth->fetchrow_hashref;
111 my $sth = $dbh->prepare("select distinct category from authorised_values");
113 my @authorised_values;
114 push @authorised_values,"";
115 while ((my $category) = $sth->fetchrow_array) {
116 push @authorised_values, $category;
118 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
119 -values=> \@authorised_values,
122 -default => $data->{'authorised_value'},
126 $template->param(action => "Modify tag",
127 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
128 $template->param('heading-modify-tag-p' => 1);
130 $template->param(action => "Add tag",
131 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
132 $template->param('heading-add-tag-p' => 1);
134 $template->param('use-heading-flags-p' => 1);
135 $template->param(liblibrarian => $data->{'liblibrarian'},
136 libopac => $data->{'libopac'},
137 repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
138 mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
139 authorised_value => $authorised_value,
140 authtypecode => $authtypecode,
142 # END $OP eq ADD_FORM
143 ################## ADD_VALIDATE ##################################
144 # called by add_form, used to insert/modify data in DB
145 } elsif ($op eq 'add_validate') {
146 $sth=$dbh->prepare("replace auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) values (?,?,?,?,?,?,?)");
147 my $tagfield =$input->param('tagfield');
148 my $liblibrarian = $input->param('liblibrarian');
149 my $libopac =$input->param('libopac');
150 my $repeatable =$input->param('repeatable');
151 my $mandatory =$input->param('mandatory');
152 my $authorised_value =$input->param('authorised_value');
153 unless (C4::Context->config('demo') eq 1) {
154 $sth->execute($tagfield,
164 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
166 # END $OP eq ADD_VALIDATE
167 ################## DELETE_CONFIRM ##################################
168 # called by default form, used to confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirm') {
170 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
171 $sth->execute($searchfield);
172 my $data=$sth->fetchrow_hashref;
174 $template->param(liblibrarian => $data->{'liblibrarian'},
175 searchfield => $searchfield,
177 # END $OP eq DELETE_CONFIRM
178 ################## DELETE_CONFIRMED ##################################
179 # called by delete_confirm, used to effectively confirm deletion of data in DB
180 } elsif ($op eq 'delete_confirmed') {
181 unless (C4::Context->config('demo') eq 1) {
182 $dbh->do("delete from auth_tag_structure where tagfield='$searchfield'");
183 $dbh->do("delete from auth_subfield_structure where tagfield='$searchfield'");
185 # END $OP eq DELETE_CONFIRMED
186 ################## ITEMTYPE_CREATE ##################################
187 # called automatically if an unexisting authtypecode is selected
188 } elsif ($op eq 'authtype_create') {
189 $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 my @existingauthtypeloop;
192 while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
194 my %line = ( value => $thisauthtype,
195 authtypetext => $authtypetext,
197 push @existingauthtypeloop,\%line;
200 $template->param(existingauthtypeloop => \@existingauthtypeloop,
201 authtypecode => $authtypecode,
203 ################## DEFAULT ##################################
205 # here, $op can be unset or set to "authtype_create_confirm".
206 warn "authtype : $authtypecode";
207 if ($searchfield ne '') {
208 $template->param(searchfield => $searchfield);
211 my ($count,$results)=StringSearch($env,$searchfield,$authtypecode);
214 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
215 if ($toggle eq 'white'){
220 my %row_data; # get a fresh hash for the row data
221 $row_data{tagfield} = $results->[$i]{'tagfield'};
222 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
223 $row_data{repeatable} = $results->[$i]{'repeatable'};
224 $row_data{mandatory} = $results->[$i]{'mandatory'};
225 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
226 $row_data{subfield_link} ="auth_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
227 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
228 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
229 $row_data{bgcolor} = $toggle;
230 push(@loop_data, \%row_data);
232 $template->param(loop => \@loop_data,
233 authtypecode => $authtypecode,
236 my $prevpage = $offset-$pagesize;
237 $template->param(isprevpage => $offset,
238 prevpage=> $prevpage,
239 searchfield => $searchfield,
240 script_name => $script_name,
243 if ($offset+$pagesize<$count) {
244 my $nextpage =$offset+$pagesize;
245 $template->param(nextpage =>$nextpage,
246 searchfield => $searchfield,
247 script_name => $script_name,
250 } #---- END $OP eq DEFAULT
252 $template->param(loggeninuser => $loggedinuser);
253 output_html_with_http_headers $input, $cookie, $template->output;
257 # the sub used for searches
260 my ($env,$searchstring,$authtypecode)=@_;
261 my $dbh = C4::Context->dbh;
262 $searchstring=~ s/\'/\\\'/g;
263 my @data=split(' ',$searchstring);
265 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
266 $sth->execute($data[0], $authtypecode);
268 while (my $data=$sth->fetchrow_hashref){
269 push(@results,$data);
273 return (scalar(@results),\@results);
277 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
279 sub duplicate_auth_framework {
280 my ($newauthtype,$oldauthtype) = @_;
281 warn "TO $newauthtype FROM $oldauthtype";
282 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
283 $sth->execute($oldauthtype);
284 my $sth_insert = $dbh->prepare("insert into auth_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
285 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
286 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
289 $sth = $dbh->prepare("select authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,authorised_value,seealso from auth_subfield_structure where authtypecode=?");
290 $sth->execute($oldauthtype);
291 $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,authorised_value,value_builder,seealso) values (?,?,?,?,?,?,?,?,?,?,?)");
292 while ( my ($authtypecode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $tab, $authorised_value, $thesaurus_category, $seealso) = $sth->fetchrow) {
293 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $tab, $authorised_value, $thesaurus_category, $seealso);