Adding id attributes to CGI-generated form inputs so that HTML labels can transfer...
[koha.git] / admin / marctagstructure.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 CGI;
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Interface::CGI::Output;
28 use C4::Search;
29 use C4::Context;
30 use HTML::Template;
31
32 # retrieve parameters
33 my $input = new CGI;
34 my $frameworkcode = $input->param('frameworkcode'); # set to select framework
35 $frameworkcode="" unless $frameworkcode;
36 my $existingframeworkcode = $input->param('existingframeworkcode'); # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
37 $existingframeworkcode = "" unless $existingframeworkcode;
38 my $frameworkinfo = getframeworkinfo($frameworkcode);
39 my $searchfield=$input->param('searchfield');
40 $searchfield=0 unless $searchfield;
41 $searchfield=~ s/\,//g;
42
43 my $offset=$input->param('offset');
44 my $op = $input->param('op');
45 my $pagesize=20;
46
47 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
48
49 my $dbh = C4::Context->dbh;
50
51 # open template
52 my ($template, $loggedinuser, $cookie)
53     = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
54                              query => $input,
55                              type => "intranet",
56                              authnotrequired => 0,
57                              flagsrequired => {parameters => 1},
58                              debug => 1,
59                              });
60
61 # get framework list
62 my $frameworks = getframeworks();
63 my @frameworkloop;
64 foreach my $thisframeworkcode (keys %$frameworks) {
65         my $selected = 1 if $thisframeworkcode eq $frameworkcode;
66         my %row =(value => $thisframeworkcode,
67                                 selected => $selected,
68                                 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
69                         );
70         push @frameworkloop, \%row;
71 }
72
73 # check that framework is defined in marc_tag_structure
74 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
75 $sth->execute($frameworkcode);
76 my ($frameworkexist) = $sth->fetchrow;
77 if ($frameworkexist) {
78 } else {
79         # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
80         # (op = itemtyp_create_confirm)
81         if ($op eq "framework_create_confirm") {
82                 duplicate_framework($frameworkcode, $existingframeworkcode);
83                 $op=""; # unset $op to go back to framework list
84         } else {
85                 $op = "framework_create";
86         }
87 }
88 $template->param(frameworkloop => \@frameworkloop,
89                                 frameworkcode => $frameworkcode,
90                                 frameworktext => $frameworkinfo->{frameworktext});
91 if ($op) {
92 $template->param(script_name => $script_name,
93                                                 $op              => 1); # we show only the TMPL_VAR names $op
94 } else {
95 $template->param(script_name => $script_name,
96                                                 else              => 1); # we show only the TMPL_VAR names $op
97 }
98
99
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or  modify a record
102 if ($op eq 'add_form') {
103         #---- if primkey exists, it's a modify action, so read values to modify...
104         my $data;
105         if ($searchfield) {
106                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
107                 $sth->execute($searchfield,$frameworkcode);
108                 $data=$sth->fetchrow_hashref;
109                 $sth->finish;
110         }
111         my $sth = $dbh->prepare("select distinct category from authorised_values");
112         $sth->execute;
113         my @authorised_values;
114         push @authorised_values,"";
115         while ((my $category) = $sth->fetchrow_array) {
116                 push @authorised_values, $category;
117         }
118         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
119                         -values=> \@authorised_values,
120                         -size=>1,
121                         -id=>"authorised_value",
122                         -multiple=>0,
123                         -default => $data->{'authorised_value'},
124                         );
125
126         if ($searchfield) {
127                 $template->param(action => "Modify tag",
128                                                                 searchfield => $searchfield);
129                 $template->param('heading-modify-tag-p' => 1);
130         } else {
131                 $template->param(action => "Add tag");
132                 $template->param('heading-add-tag-p' => 1);
133         }
134         $template->param('use-heading-flags-p' => 1);
135         $template->param(liblibrarian => $data->{'liblibrarian'},
136                                                         libopac => $data->{'libopac'},
137                                                         repeatable => CGI::checkbox(-name=>'repeatable',
138                                                 -checked=> $data->{'repeatable'}?'checked':'',
139                                                 -value=> 1,
140                                                 -label => '',
141                                                 -id=> 'repeatable'),
142                         mandatory => CGI::checkbox(-name => 'mandatory',
143                                                 -checked => $data->{'mandatory'}?'checked':'',
144                                                 -value => 1,
145                                                 -label => '',
146                                                 -id => 'mandatory'),
147                                                         authorised_value => $authorised_value,
148                                                         frameworkcode => $frameworkcode,
149                                                         );
150                                                                                                         # END $OP eq ADD_FORM
151 ################## ADD_VALIDATE ##################################
152 # called by add_form, used to insert/modify data in DB
153 } elsif ($op eq 'add_validate') {
154         $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
155         my $tagfield       =$input->param('tagfield');
156         my $liblibrarian  = $input->param('liblibrarian');
157         my $libopac       =$input->param('libopac');
158         my $repeatable =$input->param('repeatable');
159         my $mandatory =$input->param('mandatory');
160         my $authorised_value =$input->param('authorised_value');
161         unless (C4::Context->config('demo') eq 1) {
162                 $sth->execute($tagfield,
163                                                         $liblibrarian,
164                                                         $libopac,
165                                                         $repeatable?1:0,
166                                                         $mandatory?1:0,
167                                                         $authorised_value,
168                                                         $frameworkcode
169                                                         );
170         }
171         $sth->finish;
172         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
173         exit;
174                                                                                                         # END $OP eq ADD_VALIDATE
175 ################## DELETE_CONFIRM ##################################
176 # called by default form, used to confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirm') {
178         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
179         $sth->execute($searchfield,$frameworkcode);
180         my $data=$sth->fetchrow_hashref;
181         $sth->finish;
182         $template->param(liblibrarian => $data->{'liblibrarian'},
183                                                         searchfield => $searchfield,
184                                                         frameworkcode => $frameworkcode,
185                                                         );
186                                                                                                         # END $OP eq DELETE_CONFIRM
187 ################## DELETE_CONFIRMED ##################################
188 # called by delete_confirm, used to effectively confirm deletion of data in DB
189 } elsif ($op eq 'delete_confirmed') {
190         unless (C4::Context->config('demo') eq 1) {
191                 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
192                 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
193         }
194                                                                                                         # END $OP eq DELETE_CONFIRMED
195 ################## ITEMTYPE_CREATE ##################################
196 # called automatically if an unexisting  frameworkis selected
197 } elsif ($op eq 'framework_create') {
198         $sth = $dbh->prepare("select count(*),marc_tag_structure.frameworkcode,frameworktext from marc_tag_structure,biblio_framework where biblio_framework.frameworkcode=marc_tag_structure.frameworkcode group by marc_tag_structure.frameworkcode");
199         $sth->execute;
200         my @existingframeworkloop;
201         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
202                 if ($tot>0) {
203                         my %line = ( value => $thisframeworkcode,
204                                                 frameworktext => $frameworktext,
205                                         );
206                         push @existingframeworkloop,\%line;
207                 }
208         }
209         $template->param(existingframeworkloop => \@existingframeworkloop,
210                                         frameworkcode => $frameworkcode,
211 #                                       FRtext => $frameworkinfo->{frameworktext},
212                                         );
213 ################## DEFAULT ##################################
214 } else { # DEFAULT
215         # here, $op can be unset or set to "framework_create_confirm".
216         if  ($searchfield ne '') {
217                  $template->param(searchfield => $searchfield);
218         }
219         my $env;
220         my ($count,$results)=StringSearch($env,$searchfield,$frameworkcode);
221         my $toggle="white";
222         my @loop_data = ();
223         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
224                 if ($toggle eq 'white'){
225                         $toggle="#ffffcc";
226                 } else {
227                         $toggle="white";
228                 }
229                 my %row_data;  # get a fresh hash for the row data
230                 $row_data{tagfield} = $results->[$i]{'tagfield'};
231                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
232                 $row_data{repeatable} = $results->[$i]{'repeatable'};
233                 $row_data{mandatory} = $results->[$i]{'mandatory'};
234                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
235                 $row_data{bgcolor} = $toggle;
236                 push(@loop_data, \%row_data);
237         }
238         $template->param(loop => \@loop_data);
239         if ($offset>0) {
240                 my $prevpage = $offset-$pagesize;
241                 $template->param(isprevpage => $offset,
242                                                 prevpage=> $prevpage,
243                                                 searchfield => $searchfield,
244                                                 script_name => $script_name,
245                                                 frameworkcode => $frameworkcode,
246                  );
247         }
248         if ($offset+$pagesize<$count) {
249                 my $nextpage =$offset+$pagesize;
250                 $template->param(nextpage =>$nextpage,
251                                                 searchfield => $searchfield,
252                                                 script_name => $script_name,
253                                                 frameworkcode => $frameworkcode,
254                 );
255         }
256 } #---- END $OP eq DEFAULT
257
258 $template->param(loggeninuser => $loggedinuser);
259 output_html_with_http_headers $input, $cookie, $template->output;
260
261
262 #
263 # the sub used for searches
264 #
265 sub StringSearch  {
266         my ($env,$searchstring,$frameworkcode)=@_;
267         my $dbh = C4::Context->dbh;
268         $searchstring=~ s/\'/\\\'/g;
269         my @data=split(' ',$searchstring);
270         my $count=@data;
271         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
272         $sth->execute($data[0], $frameworkcode);
273         my @results;
274         while (my $data=$sth->fetchrow_hashref){
275         push(@results,$data);
276         }
277         #  $sth->execute;
278         $sth->finish;
279         return (scalar(@results),\@results);
280 }
281
282 #
283 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
284 #
285 sub duplicate_framework {
286         my ($newframeworkcode,$oldframeworkcode) = @_;
287         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
288         $sth->execute($oldframeworkcode);
289         my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
290         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
291                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
292         }
293
294         $sth = $dbh->prepare("select frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso from marc_subfield_structure where frameworkcode=?");
295         $sth->execute($oldframeworkcode);
296         $sth_insert = $dbh->prepare("insert into marc_subfield_structure (frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
297         while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso) = $sth->fetchrow) {
298             $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso);
299         }
300 }
301