rel_3_0 moved to HEAD
[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::Interface::CGI::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 $authtypecode="" unless $authtypecode;
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 $existingauthtypecode = "" unless $existingauthtypecode;
37 # my $authtypeinfo = getauthtypeinfo($authtype);
38 my $searchfield=$input->param('searchfield');
39 $searchfield=0 unless $searchfield;
40 $searchfield=~ s/\,//g;
41
42 my $offset=$input->param('offset');
43 my $op = $input->param('op');
44 my $pagesize=20;
45
46 my $script_name="/cgi-bin/koha/admin/auth_tag_structure.pl";
47
48 my $dbh = C4::Context->dbh;
49
50 # open template
51 my ($template, $loggedinuser, $cookie)
52     = get_template_and_user({template_name => "admin/auth_tag_structure.tmpl",
53                              query => $input,
54                              type => "intranet",
55                              authnotrequired => 0,
56                              flagsrequired => {parameters => 1},
57                              debug => 1,
58                              });
59
60 # get authtype list
61 my $authtypes = getauthtypes;
62 my @authtypesloop;
63 foreach my $thisauthtype (keys %$authtypes) {
64         my $selected = 1 if $thisauthtype eq $authtypecode;
65         my %row =(value => $thisauthtype,
66                                 selected => $selected,
67                                 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
68                         );
69         push @authtypesloop, \%row;
70 }
71
72 my $sth;
73 # check that authtype framework is defined in auth_tag_structure if we are on a default action
74 if (!$op or $op eq 'authtype_create_confirm') {
75 #warn "IN";
76         $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
77         $sth->execute($authtypecode);
78         my ($authtypeexist) = $sth->fetchrow;
79         if ($authtypeexist) {
80         } else {
81                 # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
82                 # (op = authtyp_create_confirm)
83                 if ($op eq "authtype_create_confirm") {
84                         duplicate_auth_framework($authtypecode, $existingauthtypecode);
85                 } else {
86                         $op = "authtype_create";
87                 }
88         }
89 }
90 $template->param(authtypeloop => \@authtypesloop);
91 if ($op && $op ne 'authtype_create_confirm') {
92 $template->param(script_name => $script_name,
93                                                 $op              => 1); # we show only the TMPL_VAR names $op
94 } else {
95 $template->param(script_name => $script_name,
96                                                 else              => 1); # we show only the TMPL_VAR names $op
97 }
98
99 ################## ADD_FORM ##################################
100 # called by default. Used to create form to add or  modify a record
101 if ($op eq 'add_form') {
102         #---- if primkey exists, it's a modify action, so read values to modify...
103         my $data;
104         if ($searchfield) {
105                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
106                 $sth->execute($searchfield,$authtypecode);
107                 $data=$sth->fetchrow_hashref;
108                 $sth->finish;
109         }
110         my $sth = $dbh->prepare("select distinct category from authorised_values");
111         $sth->execute;
112         my @authorised_values;
113         push @authorised_values,"";
114         while ((my $category) = $sth->fetchrow_array) {
115                 push @authorised_values, $category;
116         }
117         my $authorised_value  = CGI::scrolling_list(-name=>'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         $sth=$dbh->prepare("replace auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) values (?,?,?,?,?,?,?)");
147         my $tagfield       =$input->param('tagfield');
148         my $liblibrarian  = $input->param('liblibrarian');
149         my $libopac       =$input->param('libopac');
150         my $repeatable =$input->param('repeatable');
151         my $mandatory =$input->param('mandatory');
152         my $authorised_value =$input->param('authorised_value');
153         unless (C4::Context->config('demo') eq 1) {
154                 $sth->execute($tagfield,
155                                                 $liblibrarian,
156                                                 $libopac,
157                                                 $repeatable?1:0,
158                                                 $mandatory?1:0,
159                                                 $authorised_value,
160                                                 $authtypecode
161                                                 );
162         }
163         $sth->finish;
164         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_tag_structure.pl?searchfield=$tagfield&authtypecode=$authtypecode\">";
165         exit;
166                                                                                                         # END $OP eq ADD_VALIDATE
167 ################## DELETE_CONFIRM ##################################
168 # called by default form, used to confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirm') {
170         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
171         $sth->execute($searchfield);
172         my $data=$sth->fetchrow_hashref;
173         $sth->finish;
174         $template->param(liblibrarian => $data->{'liblibrarian'},
175                                                         searchfield => $searchfield,
176                                                         authtypecode => $authtypecode,
177                                                         );
178                                                                                                         # END $OP eq DELETE_CONFIRM
179 ################## DELETE_CONFIRMED ##################################
180 # called by delete_confirm, used to effectively confirm deletion of data in DB
181 } elsif ($op eq 'delete_confirmed') {
182         unless (C4::Context->config('demo') eq 1) {
183                 $dbh->do("delete from auth_tag_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
184                 $dbh->do("delete from auth_subfield_structure where tagfield='$searchfield' and authtypecode='$authtypecode'");
185         }
186                                                                                                         # END $OP eq DELETE_CONFIRMED
187 ################## ITEMTYPE_CREATE ##################################
188 # called automatically if an unexisting authtypecode is selected
189 } elsif ($op eq 'authtype_create') {
190         $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");
191         $sth->execute;
192         my @existingauthtypeloop;
193         while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
194                 if ($tot>0) {
195                         my %line = ( value => $thisauthtype,
196                                                 authtypetext => $authtypetext,
197                                         );
198                         push @existingauthtypeloop,\%line;
199                 }
200         }
201         $template->param(existingauthtypeloop => \@existingauthtypeloop,
202                                         authtypecode => $authtypecode,
203                                         );
204 ################## DEFAULT ##################################
205 } else { # DEFAULT
206         # here, $op can be unset or set to "authtype_create_confirm".
207 #       warn "authtype : $authtypecode";
208         if  ($searchfield ne '') {
209                  $template->param(searchfield => $searchfield);
210         }
211         my $env;
212         my ($count,$results)=StringSearch($env,$searchfield,$authtypecode);
213         my $toggle=1;
214         my @loop_data = ();
215         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
216                 if ($toggle eq 1){
217                         $toggle=0;
218                 } else {
219                         $toggle=1;
220                 }
221                 my %row_data;  # get a fresh hash for the row data
222                 $row_data{tagfield} = $results->[$i]{'tagfield'};
223                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
224                 $row_data{repeatable} = $results->[$i]{'repeatable'};
225                 $row_data{mandatory} = $results->[$i]{'mandatory'};
226                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
227                 $row_data{subfield_link} ="auth_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
228                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
229                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&authtypecode=".$authtypecode;
230                 $row_data{toggle} = $toggle;
231                 push(@loop_data, \%row_data);
232         }
233         $template->param(loop => \@loop_data,
234                                         authtypecode => $authtypecode,
235         );
236         if ($offset>0) {
237                 my $prevpage = $offset-$pagesize;
238                 $template->param(isprevpage => $offset,
239                                                 prevpage=> $prevpage,
240                                                 searchfield => $searchfield,
241                                                 script_name => $script_name,
242                  );
243         }
244         if ($offset+$pagesize<$count) {
245                 my $nextpage =$offset+$pagesize;
246                 $template->param(nextpage =>$nextpage,
247                                                 searchfield => $searchfield,
248                                                 script_name => $script_name,
249                 );
250         }
251 } #---- END $OP eq DEFAULT
252
253 $template->param(loggeninuser => $loggedinuser,
254                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
255                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
256                 IntranetNav => C4::Context->preference("IntranetNav"),
257                 );
258
259 output_html_with_http_headers $input, $cookie, $template->output;
260
261
262 #
263 # the sub used for searches
264 #
265 sub StringSearch  {
266         my ($env,$searchstring,$authtypecode)=@_;
267         my $dbh = C4::Context->dbh;
268         $searchstring=~ s/\'/\\\'/g;
269         my @data=split(' ',$searchstring);
270         my $count=@data;
271         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
272         $sth->execute($data[0], $authtypecode);
273         my @results;
274         while (my $data=$sth->fetchrow_hashref){
275         push(@results,$data);
276         }
277         #  $sth->execute;
278         $sth->finish;
279         return (scalar(@results),\@results);
280 }
281
282 #
283 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
284 #
285 sub duplicate_auth_framework {
286         my ($newauthtype,$oldauthtype) = @_;
287 #       warn "TO $newauthtype FROM $oldauthtype";
288         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
289         $sth->execute($oldauthtype);
290         my $sth_insert = $dbh->prepare("insert into auth_tag_structure  (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
291         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
292                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
293         }
294
295         $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden,link from auth_subfield_structure where authtypecode=?");
296         $sth->execute($oldauthtype);
297         $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden,link) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
298         while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden,$link) = $sth->fetchrow) {
299                 $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden,$link);
300         }
301 }
302