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