FIXME added.
[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 => "admin/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                                         -tabindex=>'',
176                                         -multiple=>0,
177                                         );
178                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\">";
179                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
180                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
181                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
182                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
183                                         -id=>"kohafield$i",
184                                         -values=> \@kohafields,
185                                         -default=> "$data->{'kohafield'}",
186                                         -size=>1,
187                                         -tabindex=>'',
188                                         -multiple=>0,
189                                         );
190                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
191                                         -id=>'authorised_value',
192                                         -values=> \@authorised_values,
193                                         -default=>$data->{'authorised_value'},
194                                         -size=>1,
195                                         -tabindex=>'',
196                                         -multiple=>0,
197                                         );
198                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
199                                         -id=>'value_builder',
200                                         -values=> \@value_builder,
201                                         -default=>$data->{'value_builder'},
202                                         -size=>1,
203                                         -tabindex=>'',
204                                         -multiple=>0,
205                                         );
206                 $row_data{authtypes}  = CGI::scrolling_list(-name=>'authtypecode',
207                                         -id=>'authtypecode',
208                                         -values=> \@authtypes,
209                                         -default=>$data->{'authtypecode'},
210                                         -size=>1,
211                                         -tabindex=>'',
212                                         -multiple=>0,
213                                         );
214                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
215         -checked => $data->{'repeatable'}?'checked':'',
216         -value => 1,
217         -tabindex=>'',
218         -label => '',
219         -id => "repeatable$i");
220                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
221         -checked => $data->{'mandatory'}?'checked':'',
222         -value => 1,
223         -tabindex=>'',
224         -label => '',
225         -id => "mandatory$i");
226                 $row_data{hidden} = CGI::escapeHTML($data->{hidden});
227                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
228                         -id => "isurl$i",
229                         -checked => $data->{'isurl'}?'checked':'',
230                         -value => 1,
231                         -tabindex=>'',
232                         -label => '');
233                 $row_data{row} = $i;
234                 $row_data{toggle} = $toggle;
235                 $row_data{link} = CGI::escapeHTML($data->{'link'});
236                 push(@loop_data, \%row_data);
237                 $i++;
238         }
239         # add more_subfields empty lines for add if needed
240         for (my $i=1;$i<=$more_subfields;$i++) {
241                 my %row_data;  # get a fresh hash for the row data
242                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
243                                         -id => "tab$i",
244                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9','10'],
245                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1',
246                                                                         '2' =>'2','3'=>'3','4'=>'4',
247                                                                         '5' =>'5','6'=>'6','7'=>'7',
248                                                                         '8' =>'8','9'=>'9','10'=>'items (10)',
249                                                                         },
250                                         -default=>"",
251                                         -size=>1,
252                                         -tabindex=>'',
253                                         -multiple=>0,
254                                         );
255                 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
256                 $row_data{liblibrarian} = "";
257                 $row_data{libopac} = "";
258                 $row_data{seealso} = "";
259                 $row_data{hidden} = "";
260                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
261                                 -id => "repeatable$i",
262                                 -checked => '',
263                                 -value => 1,
264                                 -tabindex=>'',
265                                 -label => '');
266                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
267                         -id => "mandatory$i",
268                         -checked => '',
269                         -value => 1,
270                         -tabindex=>'',
271                         -label => '');
272                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
273                         -id => "isurl$i",
274                         -checked => '',
275                         -value => 1,
276                         -tabindex=>'',
277                         -label => '');
278                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
279                                         -id => "kohafield$i",
280                                         -values=> \@kohafields,
281                                         -default=> "",
282                                         -size=>1,
283                                         -tabindex=>'',
284                                         -multiple=>0,
285                                         );
286                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
287                                         -id => 'authorised_value',
288                                         -values=> \@authorised_values,
289                                         -size=>1,
290                                         -tabindex=>'',
291                                         -multiple=>0,
292                                         );
293                 $row_data{authtypes}  = CGI::scrolling_list(-name=>'authtypecode',
294                                         -id => 'authtypecode',
295                                         -values=> \@authtypes,
296                                         -size=>1,
297                                         -tabindex=>'',
298                                         -multiple=>0,
299                                         );
300                 $row_data{link} = CGI::escapeHTML($data->{'link'});
301                 $row_data{toggle} = $toggle;
302                 $row_data{row} = $i;
303                 push(@loop_data, \%row_data);
304         }
305         $template->param('use-heading-flags-p' => 1);
306         $template->param('heading-edit-subfields-p' => 1);
307         $template->param(action => "Edit subfields",
308                                                         tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
309                                                         loop => \@loop_data,
310                                                         more_subfields => $more_subfields,
311                                                         more_tag => $tagfield);
312
313                                                                                                 # END $OP eq ADD_FORM
314 ################## ADD_VALIDATE ##################################
315 # called by add_form, used to insert/modify data in DB
316 } elsif ($op eq 'add_validate') {
317         my $dbh = C4::Context->dbh;
318         $template->param(tagfield => "$input->param('tagfield')");
319         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)
320                                                                         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
321         my @tagsubfield = $input->param('tagsubfield');
322         my @liblibrarian        = $input->param('liblibrarian');
323         my @libopac             = $input->param('libopac');
324         my @kohafield           = $input->param('kohafield');
325         my @tab                         = $input->param('tab');
326         my @seealso             = $input->param('seealso');
327         my @hidden              = $input->param('hidden');
328         my @authorised_values   = $input->param('authorised_value');
329         my @authtypecodes       = $input->param('authtypecode');
330         my @value_builder       =$input->param('value_builder');
331         my @link                =$input->param('link');
332         for (my $i=0; $i<= $#tagsubfield ; $i++) {
333                 my $tagfield                    =$input->param('tagfield');
334                 my $tagsubfield         =$tagsubfield[$i];
335                 $tagsubfield="@" unless $tagsubfield ne '';
336                 my $liblibrarian                =$liblibrarian[$i];
337                 my $libopac                     =$libopac[$i];
338                 my $repeatable          =$input->param("repeatable$i")?1:0;
339                 my $mandatory           =$input->param("mandatory$i")?1:0;
340                 my $kohafield           =$kohafield[$i];
341                 my $tab                         =$tab[$i];
342                 my $seealso                             =$seealso[$i];
343                 my $authorised_value            =$authorised_values[$i];
344                 my $authtypecode                =$authtypecodes[$i];
345                 my $value_builder=$value_builder[$i];
346                 my $hidden = $hidden[$i]; #input->param("hidden$i");
347                 my $isurl = $input->param("isurl$i")?1:0;
348                 my $link = $link[$i];
349                 if ($liblibrarian) {
350                         unless (C4::Context->config('demo') eq 1) {
351                                 $sth->execute ($tagfield,
352                                                                         $tagsubfield,
353                                                                         $liblibrarian,
354                                                                         $libopac,
355                                                                         $repeatable,
356                                                                         $mandatory,
357                                                                         $kohafield,
358                                                                         $tab,
359                                                                         $seealso,
360                                                                         $authorised_value,
361                                                                         $authtypecode,
362                                                                         $value_builder,
363                                                                         $hidden,
364                                                                         $isurl,
365                                                                         $frameworkcode,
366
367          $link,
368                                               );
369                         }
370                 }
371         }
372         $sth->finish;
373         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
374         exit;
375
376                                                                                                         # END $OP eq ADD_VALIDATE
377 ################## DELETE_CONFIRM ##################################
378 # called by default form, used to confirm deletion of data in DB
379 } elsif ($op eq 'delete_confirm') {
380         my $dbh = C4::Context->dbh;
381         my $sth=$dbh->prepare("select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?");
382         #FIXME : called with 2 bind variables when 3 are needed
383         $sth->execute($tagfield,$tagsubfield);
384         my $data=$sth->fetchrow_hashref;
385         $sth->finish;
386         $template->param(liblibrarian => $data->{'liblibrarian'},
387                                                         tagsubfield => $data->{'tagsubfield'},
388                                                         delete_link => $script_name,
389                                                         tagfield      =>$tagfield,
390                                                         tagsubfield => $tagsubfield,
391                                                         frameworkcode => $frameworkcode,
392                                                         );
393                                                                                                         # END $OP eq DELETE_CONFIRM
394 ################## DELETE_CONFIRMED ##################################
395 # called by delete_confirm, used to effectively confirm deletion of data in DB
396 } elsif ($op eq 'delete_confirmed') {
397         my $dbh = C4::Context->dbh;
398         unless (C4::Context->config('demo') eq 1) {
399                 my $sth=$dbh->prepare("delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?");
400                 $sth->execute($tagfield,$tagsubfield,$frameworkcode);
401                 $sth->finish;
402         }
403         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
404         exit;
405         $template->param(tagfield => $tagfield);
406                                                                                                         # END $OP eq DELETE_CONFIRMED
407 ################## DEFAULT ##################################
408 } else { # DEFAULT
409         my $env;
410         my ($count,$results)=StringSearch($env,$tagfield,$frameworkcode);
411         my $toggle=1;
412         my @loop_data = ();
413         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
414                 if ($toggle eq 1){
415                         $toggle=0;
416                 } else {
417                         $toggle=1;
418                 }
419                 my %row_data;  # get a fresh hash for the row data
420                 $row_data{tagfield} = $results->[$i]{'tagfield'};
421                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
422                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
423                 $row_data{kohafield} = $results->[$i]{'kohafield'};
424                 $row_data{repeatable} = $results->[$i]{'repeatable'};
425                 $row_data{mandatory} = $results->[$i]{'mandatory'};
426                 $row_data{tab} = $results->[$i]{'tab'};
427                 $row_data{seealso} = $results->[$i]{'seealso'};
428                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
429                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
430                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
431                 $row_data{hidden}       = $results->[$i]{'hidden'};
432                 $row_data{isurl}        = $results->[$i]{'isurl'};
433                 $row_data{link} = $results->[$i]{'link'};
434                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&frameworkcode=$frameworkcode";
435                 $row_data{toggle} = $toggle;
436                 if ($row_data{tab} eq -1) {
437                         $row_data{subfield_ignored} = 1;
438                 }
439
440                 push(@loop_data, \%row_data);
441         }
442         $template->param(loop => \@loop_data);
443         $template->param(edit_tagfield => $tagfield,
444                 edit_frameworkcode => $frameworkcode);
445         
446         if ($offset>0) {
447                 my $prevpage = $offset-$pagesize;
448                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
449         }
450         if ($offset+$pagesize<$count) {
451                 my $nextpage =$offset+$pagesize;
452                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
453         }
454 } #---- END $OP eq DEFAULT
455 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
456                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
457                 IntranetNav => C4::Context->preference("IntranetNav"),
458                 );
459 output_html_with_http_headers $input, $cookie, $template->output;