MT 1486 : Results filtering, table collapsing / expanding, multiple display enhancements
[koha.git] / admin / auth_subfields_structure.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Output;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25
26
27 sub string_search  {
28         my ($searchstring,$authtypecode)=@_;
29         my $dbh = C4::Context->dbh;
30         $searchstring=~ s/\'/\\\'/g;
31         my @data=split(' ',$searchstring);
32         my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
33         $sth->execute("$searchstring%",$authtypecode);
34         my $results = $sth->fetchall_arrayref({});
35         return (scalar(@$results), $results);
36 }
37
38 sub auth_subfield_structure_exists {
39         my ($authtypecode, $tagfield, $tagsubfield) = @_;
40         my $dbh  = C4::Context->dbh;
41         my $sql  = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
42         my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
43         return @$rows > 0;
44 }
45
46 my $input        = new CGI;
47 my $tagfield     = $input->param('tagfield');
48 my $tagsubfield  = $input->param('tagsubfield');
49 my $authtypecode = $input->param('authtypecode');
50 my $offset       = $input->param('offset');
51 my $op           = $input->param('op') || '';
52 my $script_name  = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
53
54 my ($template, $borrowernumber, $cookie) = get_template_and_user(
55     {   template_name   => "admin/auth_subfields_structure.tmpl",
56         query           => $input,
57         type            => "intranet",
58         authnotrequired => 0,
59         flagsrequired   => { parameters => 1 },
60         debug           => 1,
61     }
62 );
63 my $pagesize = 30;
64 $tagfield =~ s/\,//g;
65
66 if ($op) {
67 $template->param(script_name => $script_name,
68                                                 tagfield =>$tagfield,
69                                                 authtypecode => $authtypecode,
70                                                 $op              => 1); # we show only the TMPL_VAR names $op
71 } else {
72 $template->param(script_name => $script_name,
73                                                 tagfield =>$tagfield,
74                                                 authtypecode => $authtypecode,
75                                                 else              => 1); # we show only the TMPL_VAR names $op
76 }
77
78 my $dbh = C4::Context->dbh;
79 ################## ADD_FORM ##################################
80 # called by default. Used to create form to add or  modify a record
81 if ($op eq 'add_form') {
82         my $data;
83         my $more_subfields = $input->param("more_subfields")+1;
84         # builds kohafield tables
85         my @kohafields;
86         push @kohafields, "";
87         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
88         $sth2->execute;
89         while ((my $field) = $sth2->fetchrow_array) {
90                 push @kohafields, "auth_header.".$field;
91         }
92         
93         # build authorised value list
94         $sth2 = $dbh->prepare("select distinct category from authorised_values");
95         $sth2->execute;
96         my @authorised_values;
97         push @authorised_values,"";
98         while ((my $category) = $sth2->fetchrow_array) {
99                 push @authorised_values, $category;
100         }
101         push (@authorised_values,"branches");
102         push (@authorised_values,"itemtypes");
103     
104     # build thesaurus categories list
105     $sth2 = $dbh->prepare("select authtypecode from auth_types");
106     $sth2->execute;
107     my @authtypes;
108     push @authtypes, "";
109     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
110         push @authtypes, $authtypecode;
111     }
112
113         # build value_builder list
114         my @value_builder=('');
115
116         # read value_builder directory.
117         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
118         # on a standard install, /cgi-bin need to be added. 
119         # test one, then the other
120         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
121         unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
122                 $cgidir = C4::Context->intranetdir;
123                 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
124         } 
125         while (my $line = readdir(DIR)) {
126                 if ($line =~ /\.pl$/) {
127                         push (@value_builder,$line);
128                 }
129         }
130         @value_builder= sort {$a cmp $b} @value_builder;
131         closedir DIR;
132
133         # build values list
134         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
135         $sth->execute($tagfield,$authtypecode);
136         my @loop_data = ();
137         my $i=0;
138         while ($data =$sth->fetchrow_hashref) {
139
140                 my %row_data;  # get a fresh hash for the row data
141                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
142                                         -id=>"tab$i",
143                                         -values =>
144                                         [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
145                                         -labels => {
146                                             '-1' => 'ignore',
147                                             '0'  => '0',
148                                             '1'  => '1',
149                                             '2'  => '2',
150                                             '3'  => '3',
151                                             '4'  => '4',
152                                             '5'  => '5',
153                                             '6'  => '6',
154                                             '7'  => '7',
155                                             '8'  => '8',
156                                             '9'  => '9',
157                                         },
158                                         -default=>$data->{'tab'},
159                                         -size=>1,
160                                         -tabindex=>'',
161                                         -multiple=>0,
162                                         );
163                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
164                                         -id=>"ohidden$i",
165                                         -values=>['0','1','2'],
166                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
167                                                                         '2' =>'Hide',
168                                                                         },
169                                         -default=>substr($data->{'hidden'},0,1),
170                                         -size=>1,
171                                         -multiple=>0,
172                                         );
173                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
174                                         -id=>"ihidden$i",
175                                         -values=>['0','1','2'],
176                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
177                                                                         '2' =>'Hide',
178                                                                         },
179                                         -default=>substr($data->{'hidden'},1,1),
180                                         -size=>1,
181                                         -multiple=>0,
182                                         );
183                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
184                                         -id=>"ehidden$i",
185                                         -values=>['0','1','2'],
186                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
187                                                                         '2' =>'Hide',
188                                                                         },
189                                         -default=>substr($data->{'hidden'}."  ",2,1),
190                                         -size=>1,
191                                         -multiple=>0,
192                                         );
193                 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
194                 $row_data{tagsubfield} = $data->{'tagsubfield'};
195                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
196                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
197                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
198                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
199                                         -id=>"kohafield$i",
200                                         -values=> \@kohafields,
201                                         -default=> "$data->{'kohafield'}",
202                                         -size=>1,
203                                         -multiple=>0,
204                                         );
205                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
206                                         -id=>"authorised_value$i",
207                                         -values=> \@authorised_values,
208                                         -default=>$data->{'authorised_value'},
209                                         -size=>1,
210                                         -tabindex=>'',
211                                         -multiple=>0,
212                                         );
213                 $row_data{frameworkcode}  = CGI::scrolling_list(-name=>'frameworkcode',
214                                         -id=>"frameworkcode$i",
215                                         -values=> \@authtypes,
216                                         -default=>$data->{'frameworkcode'},
217                                         -size=>1,
218                                         -tabindex=>'',
219                                         -multiple=>0,
220                                         );
221                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
222                                         -id=>"value_builder$i",
223                                         -values=> \@value_builder,
224                                         -default=>$data->{'value_builder'},
225                                         -size=>1,
226                                         -tabindex=>'',
227                                         -multiple=>0,
228                                         );
229                 
230                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
231         -checked => $data->{'repeatable'}?'checked':'',
232         -value => 1,
233         -label => '',
234         -id => "repeatable$i");
235                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
236         -checked => $data->{'mandatory'}?'checked':'',
237         -value => 1,
238         -label => '',
239         -id => "mandatory$i");
240                 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
241                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
242                         -id => "isurl$i",
243                         -checked => $data->{'isurl'}?'checked':'',
244                         -value => 1,
245                         -label => '');
246                 $row_data{row} = $i;
247                 push(@loop_data, \%row_data);
248                 $i++;
249         }
250         # add more_subfields empty lines for add if needed
251         for (my $i=1;$i<=$more_subfields;$i++) {
252                 my %row_data;  # get a fresh hash for the row data
253         $row_data{'new_subfield'} = 1;
254                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
255                                         -id => "tab$i",
256                                         -values =>
257                                         [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
258                                         -labels => {
259                                             '-1' => 'ignore',
260                                             '0'  => '0',
261                                             '1'  => '1',
262                                             '2'  => '2',
263                                             '3'  => '3',
264                                             '4'  => '4',
265                                             '5'  => '5',
266                                             '6'  => '6',
267                                             '7'  => '7',
268                                             '8'  => '8',
269                                             '9'  => '9',
270                                         },
271                                         -default=>"",
272                                         -size=>1,
273                                         -tabindex=>'',
274                                         -multiple=>0,
275                                         );
276                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
277                                         -id=>"ohidden$i",
278                                         -values=>['0','1','2'],
279                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
280                                                                         '2' =>'Hide',
281                                                                         },
282                                         -default=>"0",
283                                         -size=>1,
284                                         -multiple=>0,
285                                         );
286
287                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
288                                         -id=>"ihidden$i",
289                                         -values=>['0','1','2'],
290                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
291                                                                         '2' =>'Hide',
292                                                                         },
293                                         -default=>"0",
294                                         -size=>1,
295                                         -multiple=>0,
296                                         );
297                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
298                                         -id=>"ehidden$i",
299                                         -values=>['0','1','2'],
300                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
301                                                                         '2' =>'Hide',
302                                                                         },
303                                         -default=>"0",
304                                         -size=>1,
305                                         -multiple=>0,
306                                         );
307                 $row_data{tagsubfieldinput} = 
308                         "<label><input type=\"text\" name=\"tagsubfield\" value=\""
309                         . $data->{'tagsubfield'}
310                         . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
311                 $row_data{tagsubfield} = $data->{'tagsubfield'};
312                 $row_data{liblibrarian} = "";
313                 $row_data{libopac} = "";
314                 $row_data{seealso} = "";
315                 $row_data{hidden} = "000";
316                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
317                                 -id => "repeatable$i",
318                                 -checked => '',
319                                 -value => 1,
320                                 -label => '');
321                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
322                         -id => "mandatory$i",
323                         -checked => '',
324                         -value => 1,
325                         -label => '');
326                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
327                         -id => "isurl$i",
328                         -checked => '',
329                         -value => 1,
330                         -label => '');
331                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
332                                         -id => "kohafield$i",
333                                         -values=> \@kohafields,
334                                         -default=> "",
335                                         -size=>1,
336                                         -multiple=>0,
337                                         );
338                 $row_data{frameworkcode}  = CGI::scrolling_list(-name=>'frameworkcode',
339                                         -id=>'frameworkcode',
340                                         -values=> \@authtypes,
341                                         -default=>$data->{'frameworkcode'},
342                                         -size=>1,
343                                         -tabindex=>'',
344                                         -multiple=>0,
345                                         );
346                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
347                                         -id => 'authorised_value',
348                                         -values=> \@authorised_values,
349                                         -size=>1,
350                                         -tabindex=>'',
351                                         -multiple=>0,
352                                         );
353                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
354                                         -id=>'value_builder',
355                                         -values=> \@value_builder,
356                                         -default=>$data->{'value_builder'},
357                                         -size=>1,
358                                         -tabindex=>'',
359                                         -multiple=>0,
360                                         );
361                 $row_data{row} = $i;
362                 push(@loop_data, \%row_data);
363         }
364         $template->param('use-heading-flags-p' => 1);
365         $template->param('heading-edit-subfields-p' => 1);
366         $template->param(action => "Edit subfields",
367                                                         tagfield => $tagfield,
368                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
369                                                         loop => \@loop_data,
370                                                         more_subfields => $more_subfields,
371                                                         more_tag => $tagfield);
372
373                                                                                                 # END $OP eq ADD_FORM
374 ################## ADD_VALIDATE ##################################
375 # called by add_form, used to insert/modify data in DB
376 } elsif ($op eq 'add_validate') {
377         $template->param(tagfield => "$input->param('tagfield')");
378 #       my $sth=$dbh->prepare("replace auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl)
379 #                                                                       values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
380         my $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl)
381                                                                         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
382         my $sth_update = $dbh->prepare("update auth_subfield_structure set authtypecode=?, tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, frameworkcode=?, value_builder=?, hidden=?, isurl=?
383                                                                         where authtypecode=? and tagfield=? and tagsubfield=?");
384         my @tagsubfield = $input->param('tagsubfield');
385         my @liblibrarian        = $input->param('liblibrarian');
386         my @libopac             = $input->param('libopac');
387         my @kohafield           = ''.$input->param('kohafield');
388         my @tab                         = $input->param('tab');
389         my @seealso             = $input->param('seealso');
390         my @hidden;
391         my @ohidden             = $input->param('ohidden');
392         my @ihidden             = $input->param('ihidden');
393         my @ehidden             = $input->param('ehidden');
394         my @authorised_values   = $input->param('authorised_value');
395         my $authtypecode        = $input->param('authtypecode');
396         my @frameworkcodes      = $input->param('frameworkcode');
397         my @value_builder       =$input->param('value_builder');
398         for (my $i=0; $i<= $#tagsubfield ; $i++) {
399                 my $tagfield                    =$input->param('tagfield');
400                 my $tagsubfield         =$tagsubfield[$i];
401                 $tagsubfield="@" unless $tagsubfield ne '';
402                 my $liblibrarian                =$liblibrarian[$i];
403                 my $libopac                     =$libopac[$i];
404                 my $repeatable          =$input->param("repeatable$i")?1:0;
405                 my $mandatory           =$input->param("mandatory$i")?1:0;
406                 my $kohafield           =$kohafield[$i];
407                 my $tab                         =$tab[$i];
408                 my $seealso                             =$seealso[$i];
409                 my $authorised_value            =$authorised_values[$i];
410                 my $frameworkcode               =$frameworkcodes[$i];
411                 my $value_builder=$value_builder[$i];
412                 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
413                 my $isurl = $input->param("isurl$i")?1:0;
414                 if ($liblibrarian) {
415                         unless (C4::Context->config('demo') eq 1) {
416                                 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
417                                         $sth_update->execute(
418                                                 $authtypecode,
419                                                 $tagfield,
420                                                 $tagsubfield,
421                                                 $liblibrarian,
422                                                 $libopac,
423                                                 $repeatable,
424                                                 $mandatory,
425                                                 $kohafield,
426                                                 $tab,
427                                                 $seealso,
428                                                 $authorised_value,
429                                                 $frameworkcode,
430                                                 $value_builder,
431                                                 $hidden,
432                                                 $isurl,
433                                                 (
434                                                         $authtypecode,
435                                                         $tagfield,
436                                                         $tagsubfield
437                                                 ),
438                                         );
439                                 } else {
440                                         $sth_insert->execute(
441                                                 $authtypecode,
442                                                 $tagfield,
443                                                 $tagsubfield,
444                                                 $liblibrarian,
445                                                 $libopac,
446                                                 $repeatable,
447                                                 $mandatory,
448                                                 $kohafield,
449                                                 $tab,
450                                                 $seealso,
451                                                 $authorised_value,
452                                                 $frameworkcode,
453                                                 $value_builder,
454                                                 $hidden,
455                                                 $isurl,
456                                         );
457                                 }
458                         }
459                 }
460         }
461         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
462         exit;
463
464                                                                                                         # END $OP eq ADD_VALIDATE
465 ################## DELETE_CONFIRM ##################################
466 # called by default form, used to confirm deletion of data in DB
467 } elsif ($op eq 'delete_confirm') {
468         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
469         $sth->execute($tagfield,$tagsubfield,$authtypecode);
470         my $data=$sth->fetchrow_hashref;
471         $template->param(liblibrarian => $data->{'liblibrarian'},
472                                                         tagsubfield => $data->{'tagsubfield'},
473                                                         delete_link => $script_name,
474                                                         tagfield      =>$tagfield,
475                                                         tagsubfield => $tagsubfield,
476                                                         authtypecode => $authtypecode,
477                                                         );
478                                                                                                         # END $OP eq DELETE_CONFIRM
479 ################## DELETE_CONFIRMED ##################################
480 # called by delete_confirm, used to effectively confirm deletion of data in DB
481 } elsif ($op eq 'delete_confirmed') {
482         unless (C4::Context->config('demo') eq 1) {
483                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
484                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
485         }
486         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
487         exit;
488         $template->param(tagfield => $tagfield);
489                                                                                                         # END $OP eq DELETE_CONFIRMED
490 ################## DEFAULT ##################################
491 } else { # DEFAULT
492         my ($count,$results)=string_search($tagfield,$authtypecode);
493         my @loop_data = ();
494         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
495                 my %row_data;  # get a fresh hash for the row data
496                 $row_data{tagfield} = $results->[$i]{'tagfield'};
497                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
498                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
499                 $row_data{kohafield} = $results->[$i]{'kohafield'};
500                 $row_data{repeatable} = $results->[$i]{'repeatable'};
501                 $row_data{mandatory} = $results->[$i]{'mandatory'};
502                 $row_data{tab} = $results->[$i]{'tab'};
503                 $row_data{seealso} = $results->[$i]{'seealso'};
504                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
505                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
506                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
507                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
508                 $row_data{isurl}        = $results->[$i]{'isurl'};
509                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
510                 if ($row_data{tab} eq -1) {
511                         $row_data{subfield_ignored} = 1;
512                 }
513
514                 push(@loop_data, \%row_data);
515         }
516         $template->param(loop => \@loop_data);
517         $template->param(edit_tagfield => $tagfield,
518                 edit_authtypecode => $authtypecode);
519         
520         if ($offset>0) {
521                 my $prevpage = $offset-$pagesize;
522                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
523         }
524         if ($offset+$pagesize<$count) {
525                 my $nextpage =$offset+$pagesize;
526                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
527         }
528 } #---- END $OP eq DEFAULT
529 output_html_with_http_headers $input, $cookie, $template->output;