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>.
22 #use warnings; FIXME - Bug 2505
30 use Koha::Authority::Types;
34 my $authtypecode = $input->param('authtypecode') || ''; # set to select framework
35 my $existingauthtypecode = $input->param('existingauthtypecode') || ''; # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
37 my $searchfield = $input->param('searchfield') || 0;
38 my $offset = $input->param('offset');
39 $offset = 0 if not defined $offset or $offset < 0;
40 my $op = $input->param('op') || '';
41 $searchfield =~ s/\,//g;
44 my $script_name = "/cgi-bin/koha/admin/auth_tag_structure.pl";
46 my $dbh = C4::Context->dbh;
49 my ($template, $loggedinuser, $cookie)
50 = get_template_and_user({template_name => "admin/auth_tag_structure.tt",
54 flagsrequired => {parameters => 'parameters_remaining_permissions'},
58 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
61 # check that authtype framework is defined in auth_tag_structure if we are on a default action
62 if (!$op or $op eq 'authtype_create_confirm') {
63 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
64 $sth->execute($authtypecode);
65 my ($authtypeexist) = $sth->fetchrow;
68 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
69 # (op = authtyp_create_confirm)
70 if ($op eq "authtype_create_confirm") {
71 duplicate_auth_framework($authtypecode, $existingauthtypecode);
73 $op = "authtype_create";
77 $template->param(script_name => $script_name);
78 $template->param(authority_types => $authority_types );
79 if ($op && $op ne 'authtype_create_confirm') {
80 $template->param($op => 1);
82 $template->param(else => 1);
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or modify a record
87 if ($op eq 'add_form') {
88 #---- if primkey exists, it's a modify action, so read values to modify...
91 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
92 $sth->execute($searchfield,$authtypecode);
93 $data=$sth->fetchrow_hashref;
96 my @authorised_values = @{C4::Koha::GetAuthorisedValueCategories()}; # function returns array ref, dereferencing
97 unshift @authorised_values, ""; # put empty value first
98 my $authorised_value = {
99 values => \@authorised_values,
100 default => $data->{'authorised_value'},
104 $template->param('searchfield' => $searchfield);
105 $template->param('heading_modify_tag_p' => 1);
107 $template->param('heading_add_tag_p' => 1);
109 $template->param('use_heading_flags_p' => 1);
110 $template->param(liblibrarian => $data->{'liblibrarian'},
111 libopac => $data->{'libopac'},
112 repeatable => "".$data->{'repeatable'},
113 mandatory => "".$data->{'mandatory'},
114 authorised_value => $authorised_value,
115 authtypecode => $authtypecode,
117 # END $OP eq ADD_FORM
118 ################## ADD_VALIDATE ##################################
119 # called by add_form, used to insert/modify data in DB
120 } elsif ($op eq 'add_validate') {
121 my $tagfield = $input->param('tagfield');
122 my $liblibrarian = $input->param('liblibrarian');
123 my $libopac = $input->param('libopac');
124 my $repeatable = $input->param('repeatable') ? 1 : 0;
125 my $mandatory = $input->param('mandatory') ? 1 : 0;
126 my $authorised_value = $input->param('authorised_value');
127 unless (C4::Context->config('demo') eq 1) {
128 if ($input->param('modif')) {
129 $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
141 $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
153 print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&authtypecode=$authtypecode");
155 # END $OP eq ADD_VALIDATE
156 ################## DELETE_CONFIRM ##################################
157 # called by default form, used to confirm deletion of data in DB
158 } elsif ($op eq 'delete_confirm') {
159 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
160 $sth->execute($searchfield);
161 my $data=$sth->fetchrow_hashref;
162 $template->param(liblibrarian => $data->{'liblibrarian'},
163 searchfield => $searchfield,
164 authtypecode => $authtypecode,
166 # END $OP eq DELETE_CONFIRM
167 ################## DELETE_CONFIRMED ##################################
168 # called by delete_confirm, used to effectively confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirmed') {
170 unless (C4::Context->config('demo') eq 1) {
171 $dbh->do(q|delete from auth_tag_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
172 $dbh->do(q|delete from auth_subfield_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
174 my $tagfield = $input->param('tagfield');
175 print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&authtypecode=$authtypecode");
177 # END $OP eq DELETE_CONFIRMED
178 ################## ITEMTYPE_CREATE ##################################
179 # called automatically if an unexisting authtypecode is selected
180 } elsif ($op eq 'authtype_create') {
181 $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");
183 my @existingauthtypeloop;
184 while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
186 my %line = ( value => $thisauthtype,
187 authtypetext => $authtypetext,
189 push @existingauthtypeloop,\%line;
192 @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
193 $template->param(existingauthtypeloop => \@existingauthtypeloop,
194 authtypecode => $authtypecode,
196 ################## DEFAULT ##################################
198 # here, $op can be unset or set to "authtype_create_confirm".
199 # warn "authtype : $authtypecode";
200 if ($searchfield ne '') {
201 $template->param(searchfield => $searchfield);
203 my ($count,$results)=StringSearch($searchfield,$authtypecode);
205 for ( my $i = $offset ; $i < $count ; $i++ ) {
206 my %row_data; # get a fresh hash for the row data
207 $row_data{tagfield} = $results->[$i]{'tagfield'};
208 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
209 $row_data{repeatable} = $results->[$i]{'repeatable'};
210 $row_data{mandatory} = $results->[$i]{'mandatory'};
211 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
212 $row_data{subfield_link} = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&authtypecode=" . $authtypecode;
213 $row_data{edit} = "$script_name?op=add_form&searchfield=" . $results->[$i]{'tagfield'} . "&authtypecode=" . $authtypecode;
214 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=" . $results->[$i]{'tagfield'} . "&authtypecode=" . $authtypecode;
215 push(@loop_data, \%row_data);
217 $template->param(loop => \@loop_data,
218 authtypecode => $authtypecode,
221 $template->param(isprevpage => $offset,
222 searchfield => $searchfield,
225 if ( $offset < $count ) {
227 searchfield => $searchfield,
230 } #---- END $OP eq DEFAULT
232 output_html_with_http_headers $input, $cookie, $template->output;
235 # the sub used for searches
238 my ($searchstring,$authtypecode)=@_;
239 my $dbh = C4::Context->dbh;
240 $searchstring=~ s/\'/\\\'/g;
241 my @data=split(' ',$searchstring);
242 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
243 $sth->execute($data[0], $authtypecode);
245 while (my $data=$sth->fetchrow_hashref){
246 push(@results,$data);
248 return (scalar(@results),\@results);
252 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
254 sub duplicate_auth_framework {
255 my ($newauthtype,$oldauthtype) = @_;
256 # warn "TO $newauthtype FROM $oldauthtype";
257 my $dbh = C4::Context->dbh;
258 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
259 $sth->execute($oldauthtype);
260 my $sth_insert = $dbh->prepare("insert into auth_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
261 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
262 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
265 $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
266 $sth->execute($oldauthtype);
267 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
268 while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
269 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);