4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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
27 use C4::Interface::CGI::Output;
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;
43 my $offset=$input->param('offset');
44 my $op = $input->param('op');
47 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
49 my $dbh = C4::Context->dbh;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
57 flagsrequired => {parameters => 1},
62 my $frameworks = getframeworks();
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'},
70 push @frameworkloop, \%row;
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) {
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
85 $op = "framework_create";
88 $template->param(frameworkloop => \@frameworkloop,
89 frameworkcode => $frameworkcode,
90 frameworktext => $frameworkinfo->{frameworktext});
92 $template->param(script_name => $script_name,
93 $op => 1); # we show only the TMPL_VAR names $op
95 $template->param(script_name => $script_name,
96 else => 1); # we show only the TMPL_VAR names $op
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...
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;
111 my $sth = $dbh->prepare("select distinct category from authorised_values");
113 my @authorised_values;
114 push @authorised_values,"";
115 while ((my $category) = $sth->fetchrow_array) {
116 push @authorised_values, $category;
118 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
119 -values=> \@authorised_values,
122 -default => $data->{'authorised_value'},
126 $template->param(action => "Modify tag",
127 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
128 $template->param('heading-modify-tag-p' => 1);
130 $template->param(action => "Add tag",
131 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
132 $template->param('heading-add-tag-p' => 1);
134 $template->param('use-heading-flags-p' => 1);
135 $template->param(liblibrarian => $data->{'liblibrarian'},
136 libopac => $data->{'libopac'},
137 repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
138 mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
139 authorised_value => $authorised_value,
140 frameworkcode => $frameworkcode,
142 # END $OP eq ADD_FORM
143 ################## ADD_VALIDATE ##################################
144 # called by add_form, used to insert/modify data in DB
145 } elsif ($op eq 'add_validate') {
146 $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
147 my $tagfield =$input->param('tagfield');
148 my $liblibrarian = $input->param('liblibrarian');
149 my $libopac =$input->param('libopac');
150 my $repeatable =$input->param('repeatable');
151 my $mandatory =$input->param('mandatory');
152 my $authorised_value =$input->param('authorised_value');
153 unless (C4::Context->config('demo') eq 1) {
154 $sth->execute($tagfield,
164 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
166 # END $OP eq ADD_VALIDATE
167 ################## DELETE_CONFIRM ##################################
168 # called by default form, used to confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirm') {
170 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
171 $sth->execute($searchfield,$frameworkcode);
172 my $data=$sth->fetchrow_hashref;
174 $template->param(liblibrarian => $data->{'liblibrarian'},
175 searchfield => $searchfield,
176 frameworkcode => $frameworkcode,
178 # END $OP eq DELETE_CONFIRM
179 ################## DELETE_CONFIRMED ##################################
180 # called by delete_confirm, used to effectively confirm deletion of data in DB
181 } elsif ($op eq 'delete_confirmed') {
182 unless (C4::Context->config('demo') eq 1) {
183 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
184 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
186 # END $OP eq DELETE_CONFIRMED
187 ################## ITEMTYPE_CREATE ##################################
188 # called automatically if an unexisting frameworkis selected
189 } elsif ($op eq 'framework_create') {
190 $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");
192 my @existingframeworkloop;
193 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
195 my %line = ( value => $thisframeworkcode,
196 frameworktext => $frameworktext,
198 push @existingframeworkloop,\%line;
201 $template->param(existingframeworkloop => \@existingframeworkloop,
202 frameworkcode => $frameworkcode,
203 # FRtext => $frameworkinfo->{frameworktext},
205 ################## DEFAULT ##################################
207 # here, $op can be unset or set to "framework_create_confirm".
208 if ($searchfield ne '') {
209 $template->param(searchfield => $searchfield);
212 my ($count,$results)=StringSearch($env,$searchfield,$frameworkcode);
215 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
216 if ($toggle eq 'white'){
221 my %row_data; # get a fresh hash for the row data
222 $row_data{tagfield} = $results->[$i]{'tagfield'};
223 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
224 $row_data{repeatable} = $results->[$i]{'repeatable'};
225 $row_data{mandatory} = $results->[$i]{'mandatory'};
226 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
227 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
228 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
229 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
230 $row_data{bgcolor} = $toggle;
231 push(@loop_data, \%row_data);
233 $template->param(loop => \@loop_data);
235 my $prevpage = $offset-$pagesize;
236 $template->param(isprevpage => $offset,
237 prevpage=> $prevpage,
238 searchfield => $searchfield,
239 script_name => $script_name,
240 frameworkcode => $frameworkcode,
243 if ($offset+$pagesize<$count) {
244 my $nextpage =$offset+$pagesize;
245 $template->param(nextpage =>$nextpage,
246 searchfield => $searchfield,
247 script_name => $script_name,
248 frameworkcode => $frameworkcode,
251 } #---- END $OP eq DEFAULT
253 $template->param(loggeninuser => $loggedinuser);
254 output_html_with_http_headers $input, $cookie, $template->output;
258 # the sub used for searches
261 my ($env,$searchstring,$frameworkcode)=@_;
262 my $dbh = C4::Context->dbh;
263 $searchstring=~ s/\'/\\\'/g;
264 my @data=split(' ',$searchstring);
266 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
267 $sth->execute($data[0], $frameworkcode);
269 while (my $data=$sth->fetchrow_hashref){
270 push(@results,$data);
274 return (scalar(@results),\@results);
278 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
280 sub duplicate_framework {
281 my ($newframeworkcode,$oldframeworkcode) = @_;
282 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
283 $sth->execute($oldframeworkcode);
284 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
285 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
286 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
289 $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=?");
290 $sth->execute($oldframeworkcode);
291 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
292 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso) = $sth->fetchrow) {
293 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso);