Bug 23151: (QA follow-up) Fix atomic update
[koha.git] / admin / auth_tag_structure.pl
blobb66cbff017051c3701dfcd55db2c493a4a1ed5fc
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 Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Context;
29 use Koha::Authority::Types;
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 $searchfield = $input->param('searchfield') || 0;
37 my $offset = $input->param('offset');
38 $offset = 0 if not defined $offset or $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 => 'manage_marc_frameworks'},
54 debug => 1,
55 });
57 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
59 my $sth;
60 # check that authtype framework is defined in auth_tag_structure if we are on a default action
61 if (!$op or $op eq 'authtype_create_confirm') {
62 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
63 $sth->execute($authtypecode);
64 my ($authtypeexist) = $sth->fetchrow;
65 if ($authtypeexist) {
66 } else {
67 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
68 # (op = authtyp_create_confirm)
69 if ($op eq "authtype_create_confirm") {
70 duplicate_auth_framework($authtypecode, $existingauthtypecode);
71 } else {
72 $op = "authtype_create";
76 $template->param(script_name => $script_name);
77 $template->param(authority_types => $authority_types );
78 if ($op && $op ne 'authtype_create_confirm') {
79 $template->param($op => 1);
80 } else {
81 $template->param(else => 1);
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or modify a record
86 if ($op eq 'add_form') {
87 #---- if primkey exists, it's a modify action, so read values to modify...
88 my $data;
89 if ($searchfield) {
90 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
91 $sth->execute($searchfield,$authtypecode);
92 $data=$sth->fetchrow_hashref;
95 if ($searchfield) {
96 $template->param('searchfield' => $searchfield);
97 $template->param('heading_modify_tag_p' => 1);
98 } else {
99 $template->param('heading_add_tag_p' => 1);
101 $template->param('use_heading_flags_p' => 1);
102 $template->param(liblibrarian => $data->{'liblibrarian'},
103 libopac => $data->{'libopac'},
104 repeatable => "".$data->{'repeatable'},
105 mandatory => "".$data->{'mandatory'},
106 authorised_value => $data->{authorised_value},
107 authtypecode => $authtypecode,
109 # END $OP eq ADD_FORM
110 ################## ADD_VALIDATE ##################################
111 # called by add_form, used to insert/modify data in DB
112 } elsif ($op eq 'add_validate') {
113 my $tagfield = $input->param('tagfield');
114 my $liblibrarian = $input->param('liblibrarian');
115 my $libopac = $input->param('libopac');
116 my $repeatable = $input->param('repeatable') ? 1 : 0;
117 my $mandatory = $input->param('mandatory') ? 1 : 0;
118 my $authorised_value = $input->param('authorised_value');
119 if ($input->param('modif')) {
120 $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
121 $sth->execute(
122 $tagfield,
123 $liblibrarian,
124 $libopac,
125 $repeatable,
126 $mandatory,
127 $authorised_value,
128 $authtypecode,
129 $tagfield,
131 } else {
132 $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
133 $sth->execute(
134 $tagfield,
135 $liblibrarian,
136 $libopac,
137 $repeatable,
138 $mandatory,
139 $authorised_value,
140 $authtypecode
143 print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
144 exit;
145 # END $OP eq ADD_VALIDATE
146 ################## DELETE_CONFIRM ##################################
147 # called by default form, used to confirm deletion of data in DB
148 } elsif ($op eq 'delete_confirm') {
149 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
150 $sth->execute($searchfield);
151 my $data=$sth->fetchrow_hashref;
152 $template->param(liblibrarian => $data->{'liblibrarian'},
153 searchfield => $searchfield,
154 authtypecode => $authtypecode,
156 # END $OP eq DELETE_CONFIRM
157 ################## DELETE_CONFIRMED ##################################
158 # called by delete_confirm, used to effectively confirm deletion of data in DB
159 } elsif ($op eq 'delete_confirmed') {
160 $dbh->do(q|delete from auth_tag_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
161 $dbh->do(q|delete from auth_subfield_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
162 my $tagfield = $input->param('tagfield');
163 print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
164 exit;
165 # END $OP eq DELETE_CONFIRMED
166 ################## ITEMTYPE_CREATE ##################################
167 # called automatically if an unexisting authtypecode is selected
168 } elsif ($op eq 'authtype_create') {
169 $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");
170 $sth->execute;
171 my @existingauthtypeloop;
172 while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
173 if ($tot>0) {
174 my %line = ( value => $thisauthtype,
175 authtypetext => $authtypetext,
177 push @existingauthtypeloop,\%line;
180 @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
181 $template->param(existingauthtypeloop => \@existingauthtypeloop,
182 authtypecode => $authtypecode,
184 ################## DEFAULT ##################################
185 } else { # DEFAULT
186 # here, $op can be unset or set to "authtype_create_confirm".
187 # warn "authtype : $authtypecode";
188 if ($searchfield ne '') {
189 $template->param(searchfield => $searchfield);
191 my ($count,$results)=StringSearch($searchfield,$authtypecode);
192 my @loop_data = ();
193 for ( my $i = $offset ; $i < $count ; $i++ ) {
194 my %row_data; # get a fresh hash for the row data
195 $row_data{tagfield} = $results->[$i]{'tagfield'};
196 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
197 $row_data{repeatable} = $results->[$i]{'repeatable'};
198 $row_data{mandatory} = $results->[$i]{'mandatory'};
199 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
200 push(@loop_data, \%row_data);
202 $template->param(loop => \@loop_data,
203 authtypecode => $authtypecode,
205 if ($offset>0) {
206 $template->param(isprevpage => $offset,
207 searchfield => $searchfield,
210 if ( $offset < $count ) {
211 $template->param(
212 searchfield => $searchfield,
215 } #---- END $OP eq DEFAULT
217 output_html_with_http_headers $input, $cookie, $template->output;
220 # the sub used for searches
222 sub StringSearch {
223 my ($searchstring,$authtypecode)=@_;
224 my $dbh = C4::Context->dbh;
225 $searchstring=~ s/\'/\\\'/g;
226 my @data=split(' ',$searchstring);
227 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
228 $sth->execute($data[0], $authtypecode);
229 my @results;
230 while (my $data=$sth->fetchrow_hashref){
231 push(@results,$data);
233 return (scalar(@results),\@results);
237 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
239 sub duplicate_auth_framework {
240 my ($newauthtype,$oldauthtype) = @_;
241 # warn "TO $newauthtype FROM $oldauthtype";
242 my $dbh = C4::Context->dbh;
243 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
244 $sth->execute($oldauthtype);
245 my $sth_insert = $dbh->prepare("insert into auth_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
246 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
247 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
250 $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
251 $sth->execute($oldauthtype);
252 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
253 while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
254 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);