merging 2.2 branch with head. Sorry for not making it before, many many commits done...
[koha.git] / admin / auth_subfields_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 C4::Output;
23 use C4::Interface::CGI::Output;
24 use C4::Auth;
25 use CGI;
26 use C4::Search;
27 use C4::Context;
28 use HTML::Template;
29
30 sub StringSearch  {
31         my ($env,$searchstring,$authtypecode)=@_;
32         my $dbh = C4::Context->dbh;
33         $searchstring=~ s/\'/\\\'/g;
34         my @data=split(' ',$searchstring);
35         my $count=@data;
36         my $sth=$dbh->prepare("Select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,seealso,authorised_value,value_builder from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
37         $sth->execute("$searchstring%",$authtypecode);
38         my @results;
39         my $cnt=0;
40         while (my $data=$sth->fetchrow_hashref){
41                 push(@results,$data);
42                 $cnt ++;
43         }
44         $sth->finish;
45         $dbh->disconnect;
46         return ($cnt,\@results);
47 }
48
49 my $input = new CGI;
50 my $tagfield=$input->param('tagfield');
51 my $tagsubfield=$input->param('tagsubfield');
52 my $authtypecode=$input->param('authtypecode');
53 my $pkfield="tagfield";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
56
57 my ($template, $borrowernumber, $cookie)
58     = get_template_and_user({template_name => "parameters/auth_subfields_structure.tmpl",
59                              query => $input,
60                              type => "intranet",
61                              authnotrequired => 0,
62                              flagsrequired => {parameters => 1},
63                              debug => 1,
64                              });
65 my $pagesize=30;
66 my $op = $input->param('op');
67 $tagfield=~ s/\,//g;
68
69 if ($op) {
70 $template->param(script_name => $script_name,
71                                                 tagfield =>$tagfield,
72                                                 authtypecode => $authtypecode,
73                                                 $op              => 1); # we show only the TMPL_VAR names $op
74 } else {
75 $template->param(script_name => $script_name,
76                                                 tagfield =>$tagfield,
77                                                 authtypecode => $authtypecode,
78                                                 else              => 1); # we show only the TMPL_VAR names $op
79 }
80
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or  modify a record
83 if ($op eq 'add_form') {
84         my $data;
85         my $dbh = C4::Context->dbh;
86         my $more_subfields = $input->param("more_subfields")+1;
87         
88         # build authorised value list
89         my $sth2 = $dbh->prepare("select distinct category from authorised_values");
90         $sth2->execute;
91         my @authorised_values;
92         push @authorised_values,"";
93         while ((my $category) = $sth2->fetchrow_array) {
94                 push @authorised_values, $category;
95         }
96         push (@authorised_values,"branches");
97         push (@authorised_values,"itemtypes");
98         # build value_builder list
99         my @value_builder=('');
100         opendir(DIR, "../value_builder") || die "can't opendir ../value_builder: $!";
101         while (my $line = readdir(DIR)) {
102                 if ($line =~ /\.pl$/) {
103                         push (@value_builder,$line);
104                 }
105         }
106         closedir DIR;
107
108         # build values list
109         my $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,seealso,authorised_value,value_builder from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
110         $sth->execute($tagfield,$authtypecode);
111         my @loop_data = ();
112         my $toggle="white";
113         my $i=0;
114         while ($data =$sth->fetchrow_hashref) {
115                 my %row_data;  # get a fresh hash for the row data
116                 if ($toggle eq 'white'){
117                         $toggle="#ffffcc";
118                 } else {
119                         $toggle="white";
120                 }
121                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
122                                         -values=>['-1','0'],
123                                         -labels => {'-1' =>'No (ignore)','0'=>'yes'
124                                                                         },
125                                         -default=>$data->{'tab'},
126                                         -size=>1,
127                                         -multiple=>0,
128                                         );
129                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type='hidden' name='tagsubfield' value='".$data->{'tagsubfield'}."'>";
130                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
131                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
132                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
133                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
134                                         -values=> \@authorised_values,
135                                         -default=>$data->{'authorised_value'},
136                                         -size=>1,
137                                         -multiple=>0,
138                                         );
139                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
140                                         -values=> \@value_builder,
141                                         -default=>$data->{'value_builder'},
142                                         -size=>1,
143                                         -multiple=>0,
144                                         );
145                 $row_data{repeatable} = CGI::checkbox("repeatable$i",$data->{'repeatable'}?'checked':'',1,'');
146                 $row_data{mandatory} = CGI::checkbox("mandatory$i",$data->{'mandatory'}?'checked':'',1,'');
147                 $row_data{bgcolor} = $toggle;
148                 push(@loop_data, \%row_data);
149                 $i++;
150         }
151         # add more_subfields empty lines for add if needed
152         for (my $i=1;$i<=$more_subfields;$i++) {
153                 my %row_data;  # get a fresh hash for the row data
154                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
155                                         -values=>['-1','0'],
156                                         -labels => {'-1' =>'no (ignore)','0'=>'yes',
157                                                                         },
158                                         -default=>"",
159                                         -size=>1,
160                                         -multiple=>0,
161                                         );
162                 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"3\" maxlength=\"1\">";
163                 $row_data{liblibrarian} = "";
164                 $row_data{libopac} = "";
165                 $row_data{seealso} = "";
166                 $row_data{repeatable} = CGI::checkbox('repeatable','',1,'');
167                 $row_data{mandatory} = CGI::checkbox('mandatory','',1,'');
168                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
169                                         -values=> \@authorised_values,
170                                         -size=>1,
171                                         -multiple=>0,
172                                         );
173                 $row_data{bgcolor} = $toggle;
174                 push(@loop_data, \%row_data);
175         }
176         $template->param('use-heading-flags-p' => 1);
177         $template->param('heading-edit-subfields-p' => 1);
178         $template->param(action => "Edit subfields",
179                                                         tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
180                                                         loop => \@loop_data,
181                                                         more_subfields => $more_subfields,
182                                                         more_tag => $tagfield);
183
184                                                                                                 # END $OP eq ADD_FORM
185 ################## ADD_VALIDATE ##################################
186 # called by add_form, used to insert/modify data in DB
187 } elsif ($op eq 'add_validate') {
188         my $dbh = C4::Context->dbh;
189         $template->param(tagfield => "$input->param('tagfield')");
190         my $sth=$dbh->prepare("replace auth_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,seealso,authorised_value,value_builder,authtypecode)
191                                                                         values (?,?,?,?,?,?,?,?,?,?,?)");
192         my @tagsubfield = $input->param('tagsubfield');
193         my @liblibrarian        = $input->param('liblibrarian');
194         my @libopac             = $input->param('libopac');
195         my @tab                         = $input->param('tab');
196         my @seealso             = $input->param('seealso');
197         my @authorised_values   = $input->param('authorised_value');
198         my @value_builder       =$input->param('value_builder');
199         for (my $i=0; $i<= $#tagsubfield ; $i++) {
200                 my $tagfield                    =$input->param('tagfield');
201                 my $tagsubfield         =$tagsubfield[$i];
202                 $tagsubfield="@" unless $tagsubfield ne '';
203                 my $liblibrarian                =$liblibrarian[$i];
204                 my $libopac                     =$libopac[$i];
205                 my $repeatable          =$input->param("repeatable$i")?1:0;
206                 my $mandatory           =$input->param("mandatory$i")?1:0;
207                 my $tab                         =$tab[$i];
208                 my $seealso                             =$seealso[$i];
209                 my $authorised_value            =$authorised_values[$i];
210                 my $value_builder=$value_builder[$i];
211                 if ($liblibrarian) {
212                         unless (C4::Context->config('demo') eq 1) {
213                                 $sth->execute ($tagfield,
214                                                                         $tagsubfield,
215                                                                         $liblibrarian,
216                                                                         $libopac,
217                                                                         $repeatable,
218                                                                         $mandatory,
219                                                                         $tab,
220                                                                         $seealso,
221                                                                         $authorised_value,
222                                                                         $value_builder,$authtypecode);
223                         }
224                 }
225         }
226         $sth->finish;
227         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
228         exit;
229
230                                                                                                         # END $OP eq ADD_VALIDATE
231 ################## DELETE_CONFIRM ##################################
232 # called by default form, used to confirm deletion of data in DB
233 } elsif ($op eq 'delete_confirm') {
234         my $dbh = C4::Context->dbh;
235         my $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,authorised_value,value_builder from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
236         $sth->execute($tagfield,$tagsubfield,$authtypecode);
237         my $data=$sth->fetchrow_hashref;
238         $sth->finish;
239         $template->param(liblibrarian => $data->{'liblibrarian'},
240                                                         tagsubfield => $data->{'tagsubfield'},
241                                                         delete_link => $script_name,
242                                                         tagfield      =>$tagfield,
243                                                         tagsubfield => $tagsubfield,
244                                                         authtypecode => $authtypecode,
245                                                         );
246                                                                                                         # END $OP eq DELETE_CONFIRM
247 ################## DELETE_CONFIRMED ##################################
248 # called by delete_confirm, used to effectively confirm deletion of data in DB
249 } elsif ($op eq 'delete_confirmed') {
250         my $dbh = C4::Context->dbh;
251         unless (C4::Context->config('demo') eq 1) {
252                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
253                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
254                 warn "DEL : $tagfield,$tagsubfield,$authtypecode";
255                 $sth->finish;
256         }
257         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
258         exit;
259         $template->param(tagfield => $tagfield);
260                                                                                                         # END $OP eq DELETE_CONFIRMED
261 ################## DEFAULT ##################################
262 } else { # DEFAULT
263         my $env;
264         my ($count,$results)=StringSearch($env,$tagfield,$authtypecode);
265         my $toggle="white";
266         my @loop_data = ();
267         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
268                 if ($toggle eq 'white'){
269                         $toggle="#ffffcc";
270                 } else {
271                         $toggle="white";
272                 }
273                 my %row_data;  # get a fresh hash for the row data
274                 $row_data{tagfield} = $results->[$i]{'tagfield'};
275                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
276                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
277                 $row_data{repeatable} = $results->[$i]{'repeatable'};
278                 $row_data{mandatory} = $results->[$i]{'mandatory'};
279                 $row_data{tab} = $results->[$i]{'tab'};
280                 $row_data{seealso} = $results->[$i]{'seealso'};
281                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
282                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
283                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&authtypecode=$authtypecode";
284                 $row_data{bgcolor} = $toggle;
285                 if ($row_data{tab} eq -1) {
286                         $row_data{subfield_ignored} = 1;
287                 }
288                 push(@loop_data, \%row_data);
289         }
290         $template->param(loop => \@loop_data);
291         $template->param(edit => "<a href=\"$script_name?op=add_form&amp;tagfield=$tagfield&authtypecode=$authtypecode\">");
292         if ($offset>0) {
293                 my $prevpage = $offset-$pagesize;
294                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage&amp;tagfield=$tagfield&authtypecode=$authtypecode\">");
295         }
296         if ($offset+$pagesize<$count) {
297                 my $nextpage =$offset+$pagesize;
298                 $template->param(next => "<a href=\"$script_name?offset=$nextpage&amp;tagfield=$tagfield&authtypecode=$authtypecode\">");
299         }
300 } #---- END $OP eq DEFAULT
301
302 output_html_with_http_headers $input, $cookie, $template->output;