Bug 16615: Open Library API fails for full-site-SSL
[koha.git] / admin / auth_tag_structure.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Context;
27 use C4::Output;
28 use C4::Context;
29
30 use Koha::Authority::Types;
31
32 # retrieve parameters
33 my $input = new CGI;
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)
36
37 my $searchfield = $input->param('searchfield') || 0;
38 my $offset      = $input->param('offset') || 0;
39 my $op          = $input->param('op')     || '';
40 $searchfield =~ s/\,//g;
41
42
43 my $script_name = "/cgi-bin/koha/admin/auth_tag_structure.pl";
44
45 my $dbh = C4::Context->dbh;
46
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 => 'parameters_remaining_permissions'},
54                  debug => 1,
55                  });
56
57 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
58
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";
73         }
74     }
75 }
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);
82 }
83  
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;
93     }
94
95     my @authorised_values = @{C4::Koha::GetAuthorisedValueCategories()};    # function returns array ref, dereferencing
96     unshift @authorised_values, "";                                         # put empty value first
97     my $authorised_value = {
98         values  => \@authorised_values,
99         default => $data->{'authorised_value'},
100     };
101
102     if ($searchfield) {
103         $template->param('searchfield' => $searchfield);
104         $template->param('heading_modify_tag_p' => 1);
105     } else {
106         $template->param('heading_add_tag_p' => 1);
107     }
108     $template->param('use_heading_flags_p' => 1);
109     $template->param(liblibrarian => $data->{'liblibrarian'},
110                             libopac => $data->{'libopac'},
111                             repeatable => "".$data->{'repeatable'},
112                             mandatory => "".$data->{'mandatory'},
113                             authorised_value => $authorised_value,
114                             authtypecode => $authtypecode,
115                             );
116                                                     # END $OP eq ADD_FORM
117 ################## ADD_VALIDATE ##################################
118 # called by add_form, used to insert/modify data in DB
119 } elsif ($op eq 'add_validate') {
120     my $tagfield         = $input->param('tagfield');
121     my $liblibrarian     = $input->param('liblibrarian');
122     my $libopac          = $input->param('libopac');
123     my $repeatable       = $input->param('repeatable') ? 1 : 0;
124     my $mandatory        = $input->param('mandatory')  ? 1 : 0;
125     my $authorised_value = $input->param('authorised_value');
126     unless (C4::Context->config('demo') eq 1) {
127         if ($input->param('modif')) {
128             $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
129             $sth->execute(
130                 $tagfield,
131                 $liblibrarian,
132                 $libopac,
133                 $repeatable,
134                 $mandatory,
135                 $authorised_value,
136                 $authtypecode,
137                 $tagfield,
138             );
139         } else {
140             $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
141             $sth->execute(
142                 $tagfield,
143                 $liblibrarian,
144                 $libopac,
145                 $repeatable,
146                 $mandatory,
147                 $authorised_value,
148                 $authtypecode
149            );
150         }
151     }
152     print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
153     exit;
154                                                     # END $OP eq ADD_VALIDATE
155 ################## DELETE_CONFIRM ##################################
156 # called by default form, used to confirm deletion of data in DB
157 } elsif ($op eq 'delete_confirm') {
158     $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
159     $sth->execute($searchfield);
160     my $data=$sth->fetchrow_hashref;
161     $template->param(liblibrarian => $data->{'liblibrarian'},
162                             searchfield => $searchfield,
163                             authtypecode => $authtypecode,
164                             );
165                                                     # END $OP eq DELETE_CONFIRM
166 ################## DELETE_CONFIRMED ##################################
167 # called by delete_confirm, used to effectively confirm deletion of data in DB
168 } elsif ($op eq 'delete_confirmed') {
169     unless (C4::Context->config('demo') eq 1) {
170         my $sth = $dbh->prepare("delete from auth_tag_structure where tagfield=? and authtypecode=?");
171         $sth->execute($searchfield,$authtypecode);
172         my $sth = $dbh->prepare("delete from auth_subfield_structure where tagfield=? and authtypecode=?");
173         $sth->execute($searchfield,$authtypecode);
174     }
175     my $tagfield = $input->param('tagfield');
176     print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
177     exit;
178                                                     # END $OP eq DELETE_CONFIRMED
179 ################## ITEMTYPE_CREATE ##################################
180 # called automatically if an unexisting authtypecode is selected
181 } elsif ($op eq 'authtype_create') {
182     $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     $sth->execute;
184     my @existingauthtypeloop;
185     while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
186         if ($tot>0) {
187             my %line = ( value => $thisauthtype,
188                         authtypetext => $authtypetext,
189                     );
190             push @existingauthtypeloop,\%line;
191         }
192     }
193     @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
194     $template->param(existingauthtypeloop => \@existingauthtypeloop,
195                     authtypecode => $authtypecode,
196                     );
197 ################## DEFAULT ##################################
198 } else { # DEFAULT
199     # here, $op can be unset or set to "authtype_create_confirm".
200 #   warn "authtype : $authtypecode";
201     if  ($searchfield ne '') {
202          $template->param(searchfield => $searchfield);
203     }
204     my ($count,$results)=StringSearch($searchfield,$authtypecode);
205     my @loop_data = ();
206     for ( my $i = $offset ; $i < $count ; $i++ ) {
207         my %row_data;  # get a fresh hash for the row data
208         $row_data{tagfield}         = $results->[$i]{'tagfield'};
209         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
210         $row_data{repeatable}       = $results->[$i]{'repeatable'};
211         $row_data{mandatory}        = $results->[$i]{'mandatory'};
212         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
213         $row_data{subfield_link}    = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
214         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
215         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
216         push(@loop_data, \%row_data);
217     }
218     $template->param(loop => \@loop_data,
219                     authtypecode => $authtypecode,
220     );
221     if ($offset>0) {
222         $template->param(isprevpage => $offset,
223                         searchfield => $searchfield,
224          );
225     }
226     if ( $offset < $count ) {
227         $template->param(
228                         searchfield => $searchfield,
229         );
230     }
231 } #---- END $OP eq DEFAULT
232
233 output_html_with_http_headers $input, $cookie, $template->output;
234
235 #
236 # the sub used for searches
237 #
238 sub StringSearch  {
239     my ($searchstring,$authtypecode)=@_;
240     my $dbh = C4::Context->dbh;
241     $searchstring=~ s/\'/\\\'/g;
242     my @data=split(' ',$searchstring);
243     my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
244     $sth->execute($data[0], $authtypecode);
245     my @results;
246     while (my $data=$sth->fetchrow_hashref){
247         push(@results,$data);
248     }
249     return (scalar(@results),\@results);
250 }
251
252 #
253 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
254 #
255 sub duplicate_auth_framework {
256     my ($newauthtype,$oldauthtype) = @_;
257 #   warn "TO $newauthtype FROM $oldauthtype";
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);
263     }
264
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);
270     }
271 }
272