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