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
32 my $frameworkcode = $input->param('frameworkcode'); # set to select framework
33 $frameworkcode="" unless $frameworkcode;
34 my $existingframeworkcode = $input->param('existingframeworkcode'); # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
35 $existingframeworkcode = "" unless $existingframeworkcode;
36 my $frameworkinfo = getframeworkinfo($frameworkcode);
37 my $searchfield=$input->param('searchfield');
38 $searchfield=0 unless $searchfield;
39 $searchfield=~ s/\,//g;
41 my $offset=$input->param('offset') || 0;
42 my $op = $input->param('op') || '';
43 my $dspchoice = $input->param('select_display');
46 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
48 my $dbh = C4::Context->dbh;
51 my ($template, $loggedinuser, $cookie)
52 = get_template_and_user({template_name => "admin/marctagstructure.tmpl",
56 flagsrequired => {parameters => 1},
61 my $frameworks = getframeworks();
63 foreach my $thisframeworkcode (keys %$frameworks) {
64 my $selected = 1 if $thisframeworkcode eq $frameworkcode;
65 my %row =(value => $thisframeworkcode,
66 selected => $selected,
67 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
69 push @frameworkloop, \%row;
72 # check that framework is defined in marc_tag_structure
73 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
74 $sth->execute($frameworkcode);
75 my ($frameworkexist) = $sth->fetchrow;
76 if ($frameworkexist) {
78 # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
79 # (op = itemtyp_create_confirm)
80 if ($op eq "framework_create_confirm") {
81 duplicate_framework($frameworkcode, $existingframeworkcode);
82 $op=""; # unset $op to go back to framework list
84 $op = "framework_create";
87 $template->param(frameworkloop => \@frameworkloop,
88 frameworkcode => $frameworkcode,
89 frameworktext => $frameworkinfo->{frameworktext});
91 $template->param(script_name => $script_name,
92 $op => 1); # we show only the TMPL_VAR names $op
94 $template->param(script_name => $script_name,
95 else => 1); # we show only the TMPL_VAR names $op
99 ################## ADD_FORM ##################################
100 # called by default. Used to create form to add or modify a record
101 if ($op eq 'add_form') {
102 #---- if primkey exists, it's a modify action, so read values to modify...
105 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
106 $sth->execute($searchfield,$frameworkcode);
107 $data=$sth->fetchrow_hashref;
110 my $sth = $dbh->prepare("select distinct category from authorised_values");
112 my @authorised_values;
113 push @authorised_values,"";
114 while ((my $category) = $sth->fetchrow_array) {
115 push @authorised_values, $category;
117 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
118 -values=> \@authorised_values,
121 -id=>"authorised_value",
123 -default => $data->{'authorised_value'},
127 $template->param(action => "Modify tag",
128 searchfield => $searchfield);
129 $template->param('heading-modify-tag-p' => 1);
131 $template->param(action => "Add tag");
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(-name=>'repeatable',
138 -checked=> $data->{'repeatable'}?'checked':'',
143 mandatory => CGI::checkbox(-name => 'mandatory',
144 -checked => $data->{'mandatory'}?'checked':'',
149 authorised_value => $authorised_value,
150 frameworkcode => $frameworkcode,
152 # END $OP eq ADD_FORM
153 ################## ADD_VALIDATE ##################################
154 # called by add_form, used to insert/modify data in DB
155 } elsif ($op eq 'add_validate') {
156 my $tagfield =$input->param('tagfield');
157 if ($input->param('modif')) {
158 $sth=$dbh->prepare("UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?");
159 my $liblibrarian = $input->param('liblibrarian');
160 my $libopac =$input->param('libopac');
161 my $repeatable =$input->param('repeatable');
162 my $mandatory =$input->param('mandatory');
163 my $authorised_value =$input->param('authorised_value');
164 unless (C4::Context->config('demo') eq 1) {
165 $sth->execute( $liblibrarian,
176 $sth=$dbh->prepare("INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
177 my $liblibrarian = $input->param('liblibrarian');
178 my $libopac =$input->param('libopac');
179 my $repeatable =$input->param('repeatable');
180 my $mandatory =$input->param('mandatory');
181 my $authorised_value =$input->param('authorised_value');
182 unless (C4::Context->config('demo') eq 1) {
183 $sth->execute($tagfield,
194 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
196 # END $OP eq ADD_VALIDATE
197 ################## DELETE_CONFIRM ##################################
198 # called by default form, used to confirm deletion of data in DB
199 } elsif ($op eq 'delete_confirm') {
200 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
201 $sth->execute($searchfield,$frameworkcode);
202 my $data=$sth->fetchrow_hashref;
204 $template->param(liblibrarian => $data->{'liblibrarian'},
205 searchfield => $searchfield,
206 frameworkcode => $frameworkcode,
208 # END $OP eq DELETE_CONFIRM
209 ################## DELETE_CONFIRMED ##################################
210 # called by delete_confirm, used to effectively confirm deletion of data in DB
211 } elsif ($op eq 'delete_confirmed') {
212 unless (C4::Context->config('demo') eq 1) {
213 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
214 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
216 # END $OP eq DELETE_CONFIRMED
217 ################## ITEMTYPE_CREATE ##################################
218 # called automatically if an unexisting frameworkis selected
219 } elsif ($op eq 'framework_create') {
220 $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");
222 my @existingframeworkloop;
223 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
225 my %line = ( value => $thisframeworkcode,
226 frameworktext => $frameworktext,
228 push @existingframeworkloop,\%line;
231 $template->param(existingframeworkloop => \@existingframeworkloop,
232 frameworkcode => $frameworkcode,
233 # FRtext => $frameworkinfo->{frameworktext},
235 ################## DEFAULT ##################################
237 # here, $op can be unset or set to "framework_create_confirm".
238 if ($searchfield ne '') {
239 $template->param(searchfield => $searchfield);
243 #here, user only wants used tags/subfields displayed
244 $searchfield=~ s/\'/\\\'/g;
245 my @data=split(' ',$searchfield);
246 my $sth=$dbh->prepare("
247 SELECT marc_tag_structure.tagfield AS mts_tagfield,
248 marc_tag_structure.liblibrarian as mts_liblibrarian,
249 marc_tag_structure.libopac as mts_libopac,
250 marc_tag_structure.repeatable as mts_repeatable,
251 marc_tag_structure.mandatory as mts_mandatory,
252 marc_tag_structure.authorised_value as mts_authorized_value,
253 marc_subfield_structure.*
254 FROM marc_tag_structure
255 LEFT JOIN marc_subfield_structure ON (marc_tag_structure.tagfield=marc_subfield_structure.tagfield AND marc_tag_structure.frameworkcode=marc_subfield_structure.frameworkcode) WHERE (marc_tag_structure.tagfield >= ? and marc_tag_structure.frameworkcode=?) AND marc_subfield_structure.tab>=0 ORDER BY marc_tag_structure.tagfield,marc_subfield_structure.tagsubfield");
256 #could be ordoned by tab
257 $sth->execute($data[0], $frameworkcode);
259 while (my $data=$sth->fetchrow_hashref){
260 push(@results,$data);
269 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
275 my %row_data; # get a fresh hash for the row data
276 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
277 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
278 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
279 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
280 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
281 $row_data{subfield_link} ="marc_subfields_structure.pl?op=add_form&tagfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
282 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
283 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
284 $row_data{toggle} = $toggle;
286 my @internal_loop = ();
287 while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
289 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
290 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
291 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
292 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
293 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
294 $subfield_data{tab} = $results[$j]->{'tab'};
295 $subfield_data{seealso} = $results[$j]->{'seealso'};
296 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
297 $subfield_data{authtypecode}= $results[$j]->{'authtypecode'};
298 $subfield_data{value_builder}= $results[$j]->{'value_builder'};
299 $subfield_data{toggle} = $toggle;
300 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
301 push @internal_loop,\%subfield_data;
304 $row_data{'subfields'}=\@internal_loop;
305 push(@loop_data, \%row_data);
306 # undef @internal_loop;
309 $template->param(select_display => "True",
310 loop => \@loop_data);
314 #here, normal old style : display every tags
315 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
319 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
325 my %row_data; # get a fresh hash for the row data
326 $row_data{tagfield} = $results->[$i]{'tagfield'};
327 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
328 $row_data{repeatable} = $results->[$i]{'repeatable'};
329 $row_data{mandatory} = $results->[$i]{'mandatory'};
330 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
331 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
332 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
333 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
334 $row_data{toggle} = $toggle;
335 push(@loop_data, \%row_data);
337 $template->param(loop => \@loop_data);
340 my $prevpage = $offset-$pagesize;
341 $template->param(isprevpage => $offset,
342 prevpage=> $prevpage,
343 searchfield => $searchfield,
344 script_name => $script_name,
345 frameworkcode => $frameworkcode,
348 if ($offset+$pagesize<$cnt) {
349 my $nextpage =$offset+$pagesize;
350 $template->param(nextpage =>$nextpage,
351 searchfield => $searchfield,
352 script_name => $script_name,
353 frameworkcode => $frameworkcode,
356 } #---- END $OP eq DEFAULT
358 $template->param(loggeninuser => $loggedinuser,
360 output_html_with_http_headers $input, $cookie, $template->output;
364 # the sub used for searches
367 my ($searchstring,$frameworkcode)=@_;
368 my $dbh = C4::Context->dbh;
369 $searchstring=~ s/\'/\\\'/g;
370 my @data=split(' ',$searchstring);
372 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
373 $sth->execute($data[0], $frameworkcode);
375 while (my $data=$sth->fetchrow_hashref){
376 push(@results,$data);
380 return (scalar(@results),\@results);
384 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
386 sub duplicate_framework {
387 my ($newframeworkcode,$oldframeworkcode) = @_;
388 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
389 $sth->execute($oldframeworkcode);
390 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
391 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
392 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
395 $sth = $dbh->prepare("select frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden from marc_subfield_structure where frameworkcode=?");
396 $sth->execute($oldframeworkcode);
397 $sth_insert = $dbh->prepare("insert into marc_subfield_structure (frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
398 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
399 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);