synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
[koha.git] / admin / marc_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,$frameworkcode)=@_;
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 * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield");
37         $sth->execute("$searchstring%",$frameworkcode);
38         my @results;
39         my $cnt=0;
40         my $u=1;
41         while (my $data=$sth->fetchrow_hashref){
42                 push(@results,$data);
43                 $cnt ++;
44                 $u++;
45         }
46         $sth->finish;
47         $dbh->disconnect;
48         return ($cnt,\@results);
49 }
50
51 my $input = new CGI;
52 my $tagfield=$input->param('tagfield');
53 my $tagsubfield=$input->param('tagsubfield');
54 my $frameworkcode=$input->param('frameworkcode');
55 my $pkfield="tagfield";
56 my $offset=$input->param('offset');
57 my $script_name="/cgi-bin/koha/admin/marc_subfields_structure.pl";
58
59 my ($template, $borrowernumber, $cookie)
60     = get_template_and_user({template_name => "parameters/marc_subfields_structure.tmpl",
61                              query => $input,
62                              type => "intranet",
63                              authnotrequired => 0,
64                              flagsrequired => {parameters => 1},
65                              debug => 1,
66                              });
67 my $pagesize=30;
68 my $op = $input->param('op');
69 $tagfield=~ s/\,//g;
70
71 if ($op) {
72 $template->param(script_name => $script_name,
73                                                 tagfield =>$tagfield,
74                                                 frameworkcode => $frameworkcode,
75                                                 $op              => 1); # we show only the TMPL_VAR names $op
76 } else {
77 $template->param(script_name => $script_name,
78                                                 tagfield =>$tagfield,
79                                                 frameworkcode => $frameworkcode,
80                                                 else              => 1); # we show only the TMPL_VAR names $op
81 }
82
83 ################## ADD_FORM ##################################
84 # called by default. Used to create form to add or  modify a record
85 if ($op eq 'add_form') {
86         my $data;
87         my $dbh = C4::Context->dbh;
88         my $more_subfields = $input->param("more_subfields")+1;
89         # builds kohafield tables
90         my @kohafields;
91         push @kohafields, "";
92         my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
93         $sth2->execute;
94         while ((my $field) = $sth2->fetchrow_array) {
95                 push @kohafields, "biblio.".$field;
96         }
97         my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
98         $sth2->execute;
99         while ((my $field) = $sth2->fetchrow_array) {
100                 if ($field eq 'notes') { $field = 'bnotes'; }
101                 push @kohafields, "biblioitems.".$field;
102         }
103         my $sth2=$dbh->prepare("SHOW COLUMNS from items");
104         $sth2->execute;
105         while ((my $field) = $sth2->fetchrow_array) {
106                 push @kohafields, "items.".$field;
107         }
108         
109         # other subfields
110         push @kohafields, "additionalauthors.author";
111         push @kohafields, "bibliosubject.subject";
112         push @kohafields, "bibliosubtitle.subtitle";
113         # build authorised value list
114         $sth2->finish;
115         $sth2 = $dbh->prepare("select distinct category from authorised_values");
116         $sth2->execute;
117         my @authorised_values;
118         push @authorised_values,"";
119         while ((my $category) = $sth2->fetchrow_array) {
120                 push @authorised_values, $category;
121         }
122         push (@authorised_values,"branches");
123         push (@authorised_values,"itemtypes");
124         # build thesaurus categories list
125         $sth2->finish;
126         $sth2 = $dbh->prepare("select authtypecode from auth_types");
127         $sth2->execute;
128         my @authtypes;
129         push @authtypes,"";
130         while ((my $authtypecode) = $sth2->fetchrow_array) {
131                 push @authtypes, $authtypecode;
132         }
133         # build value_builder list
134         my @value_builder=('');
135
136         # read value_builder directory.
137         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
138         # on a standard install, /cgi-bin need to be added. 
139         # test one, then the other
140         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
141         unless (opendir(DIR, "$cgidir/value_builder")) {
142                 $cgidir = C4::Context->intranetdir;
143                 opendir(DIR, "$cgidir/value_builder") || die "can't opendir $cgidir/value_builder: $!";
144         } 
145         while (my $line = readdir(DIR)) {
146                 if ($line =~ /\.pl$/) {
147                         push (@value_builder,$line);
148                 }
149         }
150         closedir DIR;
151
152         # build values list
153         my $sth=$dbh->prepare("select * from marc_subfield_structure where tagfield=? and frameworkcode=?"); # and tagsubfield='$tagsubfield'");
154         $sth->execute($tagfield,$frameworkcode);
155         my @loop_data = ();
156         my $toggle=1;
157         my $i=0;
158         while ($data =$sth->fetchrow_hashref) {
159                 my %row_data;  # get a fresh hash for the row data
160                 if ($toggle eq 1){
161                         $toggle=0;
162                 } else {
163                         $toggle=1;
164                 }
165                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
166                                         -id=>"tab$i",
167                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9','10'],
168                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
169                                                                         '2' =>'2','3'=>'3','4'=>'4',
170                                                                         '5' =>'5','6'=>'6','7'=>'7',
171                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
172                                                                         },
173                                         -default=>$data->{'tab'},
174                                         -size=>1,
175                                         -multiple=>0,
176                                         );
177                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\">";
178                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
179                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
180                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
181                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
182                                         -id=>"kohafield$i",
183                                         -values=> \@kohafields,
184                                         -default=> "$data->{'kohafield'}",
185                                         -size=>1,
186                                         -multiple=>0,
187                                         );
188                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
189                                         -id=>'authorised_value',
190                                         -values=> \@authorised_values,
191                                         -default=>$data->{'authorised_value'},
192                                         -size=>1,
193                                         -multiple=>0,
194                                         );
195                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
196                                         -id=>'value_builder',
197                                         -values=> \@value_builder,
198                                         -default=>$data->{'value_builder'},
199                                         -size=>1,
200                                         -multiple=>0,
201                                         );
202                 $row_data{authtypes}  = CGI::scrolling_list(-name=>'authtypecode',
203                                         -id=>'authtypecode',
204                                         -values=> \@authtypes,
205                                         -default=>$data->{'authtypecode'},
206                                         -size=>1,
207                                         -multiple=>0,
208                                         );
209                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
210         -checked => $data->{'repeatable'}?'checked':'',
211         -value => 1,
212         -label => '',
213         -id => "repeatable$i");
214                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
215         -checked => $data->{'mandatory'}?'checked':'',
216         -value => 1,
217         -label => '',
218         -id => "mandatory$i");
219                 $row_data{hidden} = CGI::checkbox( -name=>"hidden$i",
220                         -id => "hidden$i",
221                         -checked => $data->{'hidden'}?'checked':'',
222                         -value => 1,
223                         -label => '');
224                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
225                         -id => "isurl$i",
226                         -checked => $data->{'isurl'}?'checked':'',
227                         -value => 1,
228                         -label => '');
229                 $row_data{row} = $i;
230                 $row_data{toggle} = $toggle;
231                 $row_data{link} = CGI::escapeHTML($data->{'link'});
232                 push(@loop_data, \%row_data);
233                 $i++;
234         }
235         # add more_subfields empty lines for add if needed
236         for (my $i=1;$i<=$more_subfields;$i++) {
237                 my %row_data;  # get a fresh hash for the row data
238                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
239                                         -id => "tab$i",
240                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9','10'],
241                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
242                                                                         '2' =>'2','3'=>'3','4'=>'4',
243                                                                         '5' =>'5','6'=>'6','7'=>'7',
244                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
245                                                                         },
246                                         -default=>"",
247                                         -size=>1,
248                                         -multiple=>0,
249                                         );
250                 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
251                 $row_data{liblibrarian} = "";
252                 $row_data{libopac} = "";
253                 $row_data{seealso} = "";
254                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
255                                 -id => "repeatable$i",
256                                 -checked => '',
257                                 -value => 1,
258                                 -label => '');
259                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
260                         -id => "mandatory$i",
261                         -checked => '',
262                         -value => 1,
263                         -label => '');
264                 $row_data{hidden} = CGI::checkbox( -name => 'hidden',
265                         -id => "hidden$i",
266                         -checked=> '',
267                         -value => 1,
268                         -label => '');
269                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
270                         -id => "isurl$i",
271                         -checked => '',
272                         -value => 1,
273                         -label => '');
274                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
275                                         -id => "kohafield$i",
276                                         -values=> \@kohafields,
277                                         -default=> "",
278                                         -size=>1,
279                                         -multiple=>0,
280                                         );
281                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
282                                         -id => 'authorised_value',
283                                         -values=> \@authorised_values,
284                                         -size=>1,
285                                         -multiple=>0,
286                                         );
287                 $row_data{authtypes}  = CGI::scrolling_list(-name=>'authtypecode',
288                                         -id => 'authtypecode',
289                                         -values=> \@authtypes,
290                                         -size=>1,
291                                         -multiple=>0,
292                                         );
293                 $row_data{link} = CGI::escapeHTML($data->{'link'});
294                 $row_data{toggle} = $toggle;
295                 $row_data{row} = $i;
296                 push(@loop_data, \%row_data);
297         }
298         $template->param('use-heading-flags-p' => 1);
299         $template->param('heading-edit-subfields-p' => 1);
300         $template->param(action => "Edit subfields",
301                                                         tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
302                                                         loop => \@loop_data,
303                                                         more_subfields => $more_subfields,
304                                                         more_tag => $tagfield);
305
306                                                                                                 # END $OP eq ADD_FORM
307 ################## ADD_VALIDATE ##################################
308 # called by add_form, used to insert/modify data in DB
309 } elsif ($op eq 'add_validate') {
310         my $dbh = C4::Context->dbh;
311         $template->param(tagfield => "$input->param('tagfield')");
312         my $sth=$dbh->prepare("replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link)
313                                                                         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
314         my @tagsubfield = $input->param('tagsubfield');
315         my @liblibrarian        = $input->param('liblibrarian');
316         my @libopac             = $input->param('libopac');
317         my @kohafield           = $input->param('kohafield');
318         my @tab                         = $input->param('tab');
319         my @seealso             = $input->param('seealso');
320         my @authorised_values   = $input->param('authorised_value');
321         my @authtypecodes       = $input->param('authtypecode');
322         my @value_builder       =$input->param('value_builder');
323         my @link                =$input->param('link');
324         for (my $i=0; $i<= $#tagsubfield ; $i++) {
325                 my $tagfield                    =$input->param('tagfield');
326                 my $tagsubfield         =$tagsubfield[$i];
327                 $tagsubfield="@" unless $tagsubfield ne '';
328                 my $liblibrarian                =$liblibrarian[$i];
329                 my $libopac                     =$libopac[$i];
330                 my $repeatable          =$input->param("repeatable$i")?1:0;
331                 my $mandatory           =$input->param("mandatory$i")?1:0;
332                 my $kohafield           =$kohafield[$i];
333                 my $tab                         =$tab[$i];
334                 my $seealso                             =$seealso[$i];
335                 my $authorised_value            =$authorised_values[$i];
336                 my $authtypecode                =$authtypecodes[$i];
337                 my $value_builder=$value_builder[$i];
338                 my $hidden = $input->param("hidden$i")?1:0;
339                 my $isurl = $input->param("isurl$i")?1:0;
340                 my $link = $link[$i];
341                 if ($liblibrarian) {
342                         unless (C4::Context->config('demo') eq 1) {
343                                 $sth->execute ($tagfield,
344                                                                         $tagsubfield,
345                                                                         $liblibrarian,
346                                                                         $libopac,
347                                                                         $repeatable,
348                                                                         $mandatory,
349                                                                         $kohafield,
350                                                                         $tab,
351                                                                         $seealso,
352                                                                         $authorised_value,
353                                                                         $authtypecode,
354                                                                         $value_builder,
355                                                                         $hidden,
356                                                                         $isurl,
357                                                                         $frameworkcode,
358
359          $link,
360                                               );
361                         }
362                 }
363         }
364         $sth->finish;
365         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
366         exit;
367
368                                                                                                         # END $OP eq ADD_VALIDATE
369 ################## DELETE_CONFIRM ##################################
370 # called by default form, used to confirm deletion of data in DB
371 } elsif ($op eq 'delete_confirm') {
372         my $dbh = C4::Context->dbh;
373         my $sth=$dbh->prepare("select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?");
374         #FIXME : called with 2 bind variables when 3 are needed
375         $sth->execute($tagfield,$tagsubfield);
376         my $data=$sth->fetchrow_hashref;
377         $sth->finish;
378         $template->param(liblibrarian => $data->{'liblibrarian'},
379                                                         tagsubfield => $data->{'tagsubfield'},
380                                                         delete_link => $script_name,
381                                                         tagfield      =>$tagfield,
382                                                         tagsubfield => $tagsubfield,
383                                                         frameworkcode => $frameworkcode,
384                                                         );
385                                                                                                         # END $OP eq DELETE_CONFIRM
386 ################## DELETE_CONFIRMED ##################################
387 # called by delete_confirm, used to effectively confirm deletion of data in DB
388 } elsif ($op eq 'delete_confirmed') {
389         my $dbh = C4::Context->dbh;
390         unless (C4::Context->config('demo') eq 1) {
391                 my $sth=$dbh->prepare("delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?");
392                 $sth->execute($tagfield,$tagsubfield,$frameworkcode);
393                 $sth->finish;
394         }
395         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
396         exit;
397         $template->param(tagfield => $tagfield);
398                                                                                                         # END $OP eq DELETE_CONFIRMED
399 ################## DEFAULT ##################################
400 } else { # DEFAULT
401         my $env;
402         my ($count,$results)=StringSearch($env,$tagfield,$frameworkcode);
403         my $toggle=1;
404         my @loop_data = ();
405         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
406                 if ($toggle eq 1){
407                         $toggle=0;
408                 } else {
409                         $toggle=1;
410                 }
411                 my %row_data;  # get a fresh hash for the row data
412                 $row_data{tagfield} = $results->[$i]{'tagfield'};
413                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
414                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
415                 $row_data{kohafield} = $results->[$i]{'kohafield'};
416                 $row_data{repeatable} = $results->[$i]{'repeatable'};
417                 $row_data{mandatory} = $results->[$i]{'mandatory'};
418                 $row_data{tab} = $results->[$i]{'tab'};
419                 $row_data{seealso} = $results->[$i]{'seealso'};
420                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
421                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
422                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
423                 $row_data{hidden}       = $results->[$i]{'hidden'};
424                 $row_data{isurl}        = $results->[$i]{'isurl'};
425                 $row_data{link} = $results->[$i]{'link'};
426                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&frameworkcode=$frameworkcode";
427                 $row_data{toggle} = $toggle;
428                 if ($row_data{tab} eq -1) {
429                         $row_data{subfield_ignored} = 1;
430                 }
431
432                 push(@loop_data, \%row_data);
433         }
434         $template->param(loop => \@loop_data);
435         $template->param(edit_tagfield => $tagfield,
436                 edit_frameworkcode => $frameworkcode);
437         
438         if ($offset>0) {
439                 my $prevpage = $offset-$pagesize;
440                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
441         }
442         if ($offset+$pagesize<$count) {
443                 my $nextpage =$offset+$pagesize;
444                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
445         }
446 } #---- END $OP eq DEFAULT
447
448 output_html_with_http_headers $input, $cookie, $template->output;