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