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