bug 2503: fix call to AddIssue()
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Context;
28
29
30 # retrieve parameters
31 my $input = new CGI;
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;
40
41 my $offset=$input->param('offset');
42 my $op = $input->param('op');
43 my $pagesize=20;
44
45 my $script_name="/cgi-bin/koha/admin/auth_tag_structure.pl";
46
47 my $dbh = C4::Context->dbh;
48
49 # open template
50 my ($template, $loggedinuser, $cookie)
51     = get_template_and_user({template_name => "admin/auth_tag_structure.tmpl",
52                              query => $input,
53                              type => "intranet",
54                              authnotrequired => 0,
55                              flagsrequired => {parameters => 1},
56                              debug => 1,
57                              });
58
59 # get authtype list
60 my $authtypes = getauthtypes;
61 my @authtypesloop;
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'},
67                         );
68         push @authtypesloop, \%row;
69 }
70
71 my $sth;
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') {
74 #warn "IN";
75         $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
76         $sth->execute($authtypecode);
77         my ($authtypeexist) = $sth->fetchrow;
78         if ($authtypeexist) {
79         } else {
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);
84                 } else {
85                         $op = "authtype_create";
86                 }
87         }
88 }
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
93 } else {
94 $template->param(script_name => $script_name,
95                                                 else              => 1); # we show only the TMPL_VAR names $op
96 }
97  
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...
102         my $data;
103         if ($searchfield) {
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;
107                 $sth->finish;
108         }
109         my $sth = $dbh->prepare("select distinct category from authorised_values");
110         $sth->execute;
111         my @authorised_values;
112         push @authorised_values,"";
113         while ((my $category) = $sth->fetchrow_array) {
114                 push @authorised_values, $category;
115         }
116         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
117                 -id=>'authorised_value',
118                         -values=> \@authorised_values,
119                         -size=>1,
120                         -tabindex=>'',
121                         -multiple=>0,
122                         -default => $data->{'authorised_value'},
123                         );
124
125         if ($searchfield) {
126                 $template->param(action => "Modify tag",
127                                                                 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
128                 $template->param('heading-modify-tag-p' => 1);
129         } else {
130                 $template->param(action => "Add tag",
131                                                                 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
132                 $template->param('heading-add-tag-p' => 1);
133         }
134         $template->param('use-heading-flags-p' => 1);
135         $template->param(liblibrarian => $data->{'liblibrarian'},
136                                                         libopac => $data->{'libopac'},
137                                                         repeatable => "".$data->{'repeatable'},
138                                                         mandatory => "".$data->{'mandatory'},
139                                                         authorised_value => $authorised_value,
140                                                         authtypecode => $authtypecode,
141                                                         );
142                                                                                                         # END $OP eq ADD_FORM
143 ################## ADD_VALIDATE ##################################
144 # called by add_form, used to insert/modify data in DB
145 } elsif ($op eq 'add_validate') {
146     if ($input->param('modif')) {
147         $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
148         my $tagfield       =$input->param('tagfield');
149         my $liblibrarian  = $input->param('liblibrarian');
150         my $libopac       =$input->param('libopac');
151         my $repeatable =$input->param('repeatable');
152         my $mandatory =$input->param('mandatory');
153         my $authorised_value =$input->param('authorised_value');
154         unless (C4::Context->config('demo') eq 1) {
155             $sth->execute(
156                             $liblibrarian,
157                             $libopac,
158                             $repeatable?1:0,
159                             $mandatory?1:0,
160                             $authorised_value,
161                             $authtypecode,
162                             $tagfield,
163                             );
164         }
165         $sth->finish;
166     } else {
167         $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
168         my $tagfield       =$input->param('tagfield');
169         my $liblibrarian  = $input->param('liblibrarian');
170         my $libopac       =$input->param('libopac');
171         my $repeatable =$input->param('repeatable');
172         my $mandatory =$input->param('mandatory');
173         my $authorised_value =$input->param('authorised_value');
174         unless (C4::Context->config('demo') eq 1) {
175             $sth->execute($tagfield,
176                             $liblibrarian,
177                             $libopac,
178                             $repeatable?1:0,
179                             $mandatory?1:0,
180                             $authorised_value,
181                             $authtypecode
182                             );
183         }
184         $sth->finish;
185     }
186         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=".$input->param('tagfield')."&authtypecode=$authtypecode\">";
187         exit;
188                                                                                                         # END $OP eq ADD_VALIDATE
189 ################## DELETE_CONFIRM ##################################
190 # called by default form, used to confirm deletion of data in DB
191 } elsif ($op eq 'delete_confirm') {
192         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
193         $sth->execute($searchfield);
194         my $data=$sth->fetchrow_hashref;
195         $sth->finish;
196         $template->param(liblibrarian => $data->{'liblibrarian'},
197                                                         searchfield => $searchfield,
198                                                         authtypecode => $authtypecode,
199                                                         );
200                                                                                                         # END $OP eq DELETE_CONFIRM
201 ################## DELETE_CONFIRMED ##################################
202 # called by delete_confirm, used to effectively confirm deletion of data in DB
203 } elsif ($op eq 'delete_confirmed') {
204         unless (C4::Context->config('demo') eq 1) {
205                 $dbh->do("delete from auth_tag_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
206                 $dbh->do("delete from auth_subfield_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
207         }
208                                                                                                         # END $OP eq DELETE_CONFIRMED
209 ################## ITEMTYPE_CREATE ##################################
210 # called automatically if an unexisting authtypecode is selected
211 } elsif ($op eq 'authtype_create') {
212         $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");
213         $sth->execute;
214         my @existingauthtypeloop;
215         while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
216                 if ($tot>0) {
217                         my %line = ( value => $thisauthtype,
218                                                 authtypetext => $authtypetext,
219                                         );
220                         push @existingauthtypeloop,\%line;
221                 }
222         }
223         $template->param(existingauthtypeloop => \@existingauthtypeloop,
224                                         authtypecode => $authtypecode,
225                                         );
226 ################## DEFAULT ##################################
227 } else { # DEFAULT
228         # here, $op can be unset or set to "authtype_create_confirm".
229 #       warn "authtype : $authtypecode";
230         if  ($searchfield ne '') {
231                  $template->param(searchfield => $searchfield);
232         }
233         my ($count,$results)=StringSearch($searchfield,$authtypecode);
234         my $toggle=1;
235         my @loop_data = ();
236         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
237                 if ($toggle eq 1){
238                         $toggle=0;
239                 } else {
240                         $toggle=1;
241                 }
242                 my %row_data;  # get a fresh hash for the row data
243                 $row_data{tagfield} = $results->[$i]{'tagfield'};
244                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
245                 $row_data{repeatable} = $results->[$i]{'repeatable'};
246                 $row_data{mandatory} = $results->[$i]{'mandatory'};
247                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
248                 $row_data{subfield_link} ="auth_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&amp;authtypecode=".$authtypecode;
249                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;authtypecode=".$authtypecode;
250                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;authtypecode=".$authtypecode;
251                 $row_data{toggle} = $toggle;
252                 push(@loop_data, \%row_data);
253         }
254         $template->param(loop => \@loop_data,
255                                         authtypecode => $authtypecode,
256         );
257         if ($offset>0) {
258                 my $prevpage = $offset-$pagesize;
259                 $template->param(isprevpage => $offset,
260                                                 prevpage=> $prevpage,
261                                                 searchfield => $searchfield,
262                                                 script_name => $script_name,
263                  );
264         }
265         if ($offset+$pagesize<$count) {
266                 my $nextpage =$offset+$pagesize;
267                 $template->param(nextpage =>$nextpage,
268                                                 searchfield => $searchfield,
269                                                 script_name => $script_name,
270                 );
271         }
272 } #---- END $OP eq DEFAULT
273
274 $template->param(loggeninuser => $loggedinuser,
275                 );
276
277 output_html_with_http_headers $input, $cookie, $template->output;
278
279
280 #
281 # the sub used for searches
282 #
283 sub StringSearch  {
284         my ($searchstring,$authtypecode)=@_;
285         my $dbh = C4::Context->dbh;
286         $searchstring=~ s/\'/\\\'/g;
287         my @data=split(' ',$searchstring);
288         my $count=@data;
289         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
290         $sth->execute($data[0], $authtypecode);
291         my @results;
292         while (my $data=$sth->fetchrow_hashref){
293         push(@results,$data);
294         }
295         #  $sth->execute;
296         $sth->finish;
297         return (scalar(@results),\@results);
298 }
299
300 #
301 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
302 #
303 sub duplicate_auth_framework {
304         my ($newauthtype,$oldauthtype) = @_;
305 #       warn "TO $newauthtype FROM $oldauthtype";
306         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
307         $sth->execute($oldauthtype);
308         my $sth_insert = $dbh->prepare("insert into auth_tag_structure  (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
309         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
310                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
311         }
312
313         $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
314         $sth->execute($oldauthtype);
315         $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
316         while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
317                 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);
318         }
319 }
320