Bug 11810: (followup) regenerate CSS due to conflict
[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 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
11 # version.
12 #
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.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Context;
27 use C4::Output;
28 use C4::Context;
29
30
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)
35
36 # my $authtypeinfo = getauthtypeinfo($authtype);
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.tmpl",
50                  query => $input,
51                  type => "intranet",
52                  authnotrequired => 0,
53                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
54                  debug => 1,
55                  });
56
57 # get authtype list
58 my $authtypes     = getauthtypes;
59 my @authtypesloop = ();
60 foreach my $thisauthtype ( sort keys %{$authtypes} ) {
61     push @authtypesloop,
62       { value        => $thisauthtype,
63         selected     => $thisauthtype eq $authtypecode,
64         authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
65       };
66 }
67
68 my $sth;
69 # check that authtype framework is defined in auth_tag_structure if we are on a default action
70 if (!$op or $op eq 'authtype_create_confirm') {
71     $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
72     $sth->execute($authtypecode);
73     my ($authtypeexist) = $sth->fetchrow;
74     if ($authtypeexist) {
75     } else {
76         # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
77         # (op = authtyp_create_confirm)
78         if ($op eq "authtype_create_confirm") {
79             duplicate_auth_framework($authtypecode, $existingauthtypecode);
80         } else {
81             $op = "authtype_create";
82         }
83     }
84 }
85 $template->param(script_name  => $script_name);
86 $template->param(authtypeloop => \@authtypesloop);
87 if ($op && $op ne 'authtype_create_confirm') {
88     $template->param($op  => 1);
89 } else {
90     $template->param(else => 1);
91 }
92  
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or  modify a record
95 if ($op eq 'add_form') {
96     #---- if primkey exists, it's a modify action, so read values to modify...
97     my $data;
98     if ($searchfield) {
99         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
100         $sth->execute($searchfield,$authtypecode);
101         $data=$sth->fetchrow_hashref;
102     }
103     my $sth = $dbh->prepare("select distinct category from authorised_values");
104     $sth->execute;
105     my @authorised_values;
106     push @authorised_values,"";
107     while ((my $category) = $sth->fetchrow_array) {
108         push @authorised_values, $category;
109     }
110     my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
111             -id=>'authorised_value',
112             -values=> \@authorised_values,
113             -size=>1,
114             -multiple=>0,
115             -default => $data->{'authorised_value'},
116             );
117
118     if ($searchfield) {
119         $template->param('searchfield' => $searchfield);
120         $template->param('heading_modify_tag_p' => 1);
121     } else {
122         $template->param('heading_add_tag_p' => 1);
123     }
124     $template->param('use_heading_flags_p' => 1);
125     $template->param(liblibrarian => $data->{'liblibrarian'},
126                             libopac => $data->{'libopac'},
127                             repeatable => "".$data->{'repeatable'},
128                             mandatory => "".$data->{'mandatory'},
129                             authorised_value => $authorised_value,
130                             authtypecode => $authtypecode,
131                             );
132                                                     # END $OP eq ADD_FORM
133 ################## ADD_VALIDATE ##################################
134 # called by add_form, used to insert/modify data in DB
135 } elsif ($op eq 'add_validate') {
136     my $tagfield         = $input->param('tagfield');
137     my $liblibrarian     = $input->param('liblibrarian');
138     my $libopac          = $input->param('libopac');
139     my $repeatable       = $input->param('repeatable') ? 1 : 0;
140     my $mandatory        = $input->param('mandatory')  ? 1 : 0;
141     my $authorised_value = $input->param('authorised_value');
142     unless (C4::Context->config('demo') eq 1) {
143         if ($input->param('modif')) {
144             $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
145             $sth->execute(
146                 $tagfield,
147                 $liblibrarian,
148                 $libopac,
149                 $repeatable,
150                 $mandatory,
151                 $authorised_value,
152                 $authtypecode,
153                 $tagfield,
154             );
155         } else {
156             $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
157             $sth->execute(
158                 $tagfield,
159                 $liblibrarian,
160                 $libopac,
161                 $repeatable,
162                 $mandatory,
163                 $authorised_value,
164                 $authtypecode
165            );
166         }
167     }
168     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
169     exit;
170                                                     # END $OP eq ADD_VALIDATE
171 ################## DELETE_CONFIRM ##################################
172 # called by default form, used to confirm deletion of data in DB
173 } elsif ($op eq 'delete_confirm') {
174     $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
175     $sth->execute($searchfield);
176     my $data=$sth->fetchrow_hashref;
177     $template->param(liblibrarian => $data->{'liblibrarian'},
178                             searchfield => $searchfield,
179                             authtypecode => $authtypecode,
180                             );
181                                                     # END $OP eq DELETE_CONFIRM
182 ################## DELETE_CONFIRMED ##################################
183 # called by delete_confirm, used to effectively confirm deletion of data in DB
184 } elsif ($op eq 'delete_confirmed') {
185     unless (C4::Context->config('demo') eq 1) {
186         my $sth = $dbh->prepare("delete from auth_tag_structure where tagfield=? and authtypecode=?");
187         $sth->execute($searchfield,$authtypecode);
188         my $sth = $dbh->prepare("delete from auth_subfield_structure where tagfield=? and authtypecode=?");
189         $sth->execute($searchfield,$authtypecode);
190     }
191     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
192     exit;
193                                                     # END $OP eq DELETE_CONFIRMED
194 ################## ITEMTYPE_CREATE ##################################
195 # called automatically if an unexisting authtypecode is selected
196 } elsif ($op eq 'authtype_create') {
197     $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");
198     $sth->execute;
199     my @existingauthtypeloop;
200     while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
201         if ($tot>0) {
202             my %line = ( value => $thisauthtype,
203                         authtypetext => $authtypetext,
204                     );
205             push @existingauthtypeloop,\%line;
206         }
207     }
208     @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
209     $template->param(existingauthtypeloop => \@existingauthtypeloop,
210                     authtypecode => $authtypecode,
211                     );
212 ################## DEFAULT ##################################
213 } else { # DEFAULT
214     # here, $op can be unset or set to "authtype_create_confirm".
215 #   warn "authtype : $authtypecode";
216     if  ($searchfield ne '') {
217          $template->param(searchfield => $searchfield);
218     }
219     my ($count,$results)=StringSearch($searchfield,$authtypecode);
220     my @loop_data = ();
221     for ( my $i = $offset ; $i < $count ; $i++ ) {
222         my %row_data;  # get a fresh hash for the row data
223         $row_data{tagfield}         = $results->[$i]{'tagfield'};
224         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
225         $row_data{repeatable}       = $results->[$i]{'repeatable'};
226         $row_data{mandatory}        = $results->[$i]{'mandatory'};
227         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
228         $row_data{subfield_link}    = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
229         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
230         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
231         push(@loop_data, \%row_data);
232     }
233     $template->param(loop => \@loop_data,
234                     authtypecode => $authtypecode,
235     );
236     if ($offset>0) {
237         $template->param(isprevpage => $offset,
238                         searchfield => $searchfield,
239          );
240     }
241     if ( $offset < $count ) {
242         $template->param(
243                         searchfield => $searchfield,
244         );
245     }
246 } #---- END $OP eq DEFAULT
247
248 output_html_with_http_headers $input, $cookie, $template->output;
249
250 #
251 # the sub used for searches
252 #
253 sub StringSearch  {
254     my ($searchstring,$authtypecode)=@_;
255     my $dbh = C4::Context->dbh;
256     $searchstring=~ s/\'/\\\'/g;
257     my @data=split(' ',$searchstring);
258     my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
259     $sth->execute($data[0], $authtypecode);
260     my @results;
261     while (my $data=$sth->fetchrow_hashref){
262         push(@results,$data);
263     }
264     return (scalar(@results),\@results);
265 }
266
267 #
268 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
269 #
270 sub duplicate_auth_framework {
271     my ($newauthtype,$oldauthtype) = @_;
272 #   warn "TO $newauthtype FROM $oldauthtype";
273     my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
274     $sth->execute($oldauthtype);
275     my $sth_insert = $dbh->prepare("insert into auth_tag_structure  (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
276     while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
277         $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
278     }
279
280     $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
281     $sth->execute($oldauthtype);
282     $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
283     while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
284         $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);
285     }
286 }
287