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
32 my $authtypecode = $input->param('authtypecode'); # set to select framework
33 $authtypecode="" unless $authtypecode;
34 my $existingauthtypecode = $input->param('existingauthtypecode'); # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
35 $existingauthtypecode = "" unless $existingauthtypecode;
36 # my $authtypeinfo = getauthtypeinfo($authtype);
37 my $searchfield=$input->param('searchfield');
38 $searchfield=0 unless $searchfield;
39 $searchfield=~ s/\,//g;
41 my $offset=$input->param('offset');
42 my $op = $input->param('op');
45 my $script_name="/cgi-bin/koha/admin/auth_tag_structure.pl";
47 my $dbh = C4::Context->dbh;
50 my ($template, $loggedinuser, $cookie)
51 = get_template_and_user({template_name => "admin/auth_tag_structure.tmpl",
55 flagsrequired => {parameters => 1},
60 my $authtypes = getauthtypes;
62 foreach my $thisauthtype (keys %$authtypes) {
63 my $selected = 1 if $thisauthtype eq $authtypecode;
64 my %row =(value => $thisauthtype,
65 selected => $selected,
66 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
68 push @authtypesloop, \%row;
72 # check that authtype framework is defined in auth_tag_structure if we are on a default action
73 if (!$op or $op eq 'authtype_create_confirm') {
75 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
76 $sth->execute($authtypecode);
77 my ($authtypeexist) = $sth->fetchrow;
80 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
81 # (op = authtyp_create_confirm)
82 if ($op eq "authtype_create_confirm") {
83 duplicate_auth_framework($authtypecode, $existingauthtypecode);
85 $op = "authtype_create";
89 $template->param(authtypeloop => \@authtypesloop);
90 if ($op && $op ne 'authtype_create_confirm') {
91 $template->param(script_name => $script_name,
92 $op => 1); # we show only the TMPL_VAR names $op
94 $template->param(script_name => $script_name,
95 else => 1); # we show only the TMPL_VAR names $op
98 ################## ADD_FORM ##################################
99 # called by default. Used to create form to add or modify a record
100 if ($op eq 'add_form') {
101 #---- if primkey exists, it's a modify action, so read values to modify...
104 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
105 $sth->execute($searchfield,$authtypecode);
106 $data=$sth->fetchrow_hashref;
109 my $sth = $dbh->prepare("select distinct category from authorised_values");
111 my @authorised_values;
112 push @authorised_values,"";
113 while ((my $category) = $sth->fetchrow_array) {
114 push @authorised_values, $category;
116 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
117 -values=> \@authorised_values,
121 -default => $data->{'authorised_value'},
125 $template->param(action => "Modify tag",
126 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
127 $template->param('heading-modify-tag-p' => 1);
129 $template->param(action => "Add tag",
130 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
131 $template->param('heading-add-tag-p' => 1);
133 $template->param('use-heading-flags-p' => 1);
134 $template->param(liblibrarian => $data->{'liblibrarian'},
135 libopac => $data->{'libopac'},
136 repeatable => "".$data->{'repeatable'},
137 mandatory => "".$data->{'mandatory'},
138 authorised_value => $authorised_value,
139 authtypecode => $authtypecode,
141 # END $OP eq ADD_FORM
142 ################## ADD_VALIDATE ##################################
143 # called by add_form, used to insert/modify data in DB
144 } elsif ($op eq 'add_validate') {
145 $sth=$dbh->prepare("replace auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) values (?,?,?,?,?,?,?)");
146 my $tagfield =$input->param('tagfield');
147 my $liblibrarian = $input->param('liblibrarian');
148 my $libopac =$input->param('libopac');
149 my $repeatable =$input->param('repeatable');
150 my $mandatory =$input->param('mandatory');
151 my $authorised_value =$input->param('authorised_value');
152 unless (C4::Context->config('demo') eq 1) {
153 $sth->execute($tagfield,
163 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=$tagfield&authtypecode=$authtypecode\">";
165 # END $OP eq ADD_VALIDATE
166 ################## DELETE_CONFIRM ##################################
167 # called by default form, used to confirm deletion of data in DB
168 } elsif ($op eq 'delete_confirm') {
169 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
170 $sth->execute($searchfield);
171 my $data=$sth->fetchrow_hashref;
173 $template->param(liblibrarian => $data->{'liblibrarian'},
174 searchfield => $searchfield,
175 authtypecode => $authtypecode,
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' and authtypecode='$authtypecode'");
183 $dbh->do("delete from auth_subfield_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
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);
210 my ($count,$results)=StringSearch($searchfield,$authtypecode);
213 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
219 my %row_data; # get a fresh hash for the row data
220 $row_data{tagfield} = $results->[$i]{'tagfield'};
221 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
222 $row_data{repeatable} = $results->[$i]{'repeatable'};
223 $row_data{mandatory} = $results->[$i]{'mandatory'};
224 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
225 $row_data{subfield_link} ="auth_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
226 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
227 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
228 $row_data{toggle} = $toggle;
229 push(@loop_data, \%row_data);
231 $template->param(loop => \@loop_data,
232 authtypecode => $authtypecode,
235 my $prevpage = $offset-$pagesize;
236 $template->param(isprevpage => $offset,
237 prevpage=> $prevpage,
238 searchfield => $searchfield,
239 script_name => $script_name,
242 if ($offset+$pagesize<$count) {
243 my $nextpage =$offset+$pagesize;
244 $template->param(nextpage =>$nextpage,
245 searchfield => $searchfield,
246 script_name => $script_name,
249 } #---- END $OP eq DEFAULT
251 $template->param(loggeninuser => $loggedinuser,
252 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
253 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
254 IntranetNav => C4::Context->preference("IntranetNav"),
257 output_html_with_http_headers $input, $cookie, $template->output;
261 # the sub used for searches
264 my ($searchstring,$authtypecode)=@_;
265 my $dbh = C4::Context->dbh;
266 $searchstring=~ s/\'/\\\'/g;
267 my @data=split(' ',$searchstring);
269 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
270 $sth->execute($data[0], $authtypecode);
272 while (my $data=$sth->fetchrow_hashref){
273 push(@results,$data);
277 return (scalar(@results),\@results);
281 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
283 sub duplicate_auth_framework {
284 my ($newauthtype,$oldauthtype) = @_;
285 # warn "TO $newauthtype FROM $oldauthtype";
286 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
287 $sth->execute($oldauthtype);
288 my $sth_insert = $dbh->prepare("insert into auth_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
289 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
290 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
293 $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden,link from auth_subfield_structure where authtypecode=?");
294 $sth->execute($oldauthtype);
295 $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden,link) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
296 while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden,$link) = $sth->fetchrow) {
297 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden,$link);