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;
33 my $frameworkcode = $input->param('frameworkcode'); # set to select framework
34 $frameworkcode="" unless $frameworkcode;
35 my $existingframeworkcode = $input->param('existingframeworkcode'); # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
36 $existingframeworkcode = "" unless $existingframeworkcode;
37 my $frameworkinfo = getframeworkinfo($frameworkcode);
38 my $searchfield=$input->param('searchfield');
39 $searchfield=0 unless $searchfield;
40 $searchfield=~ s/\,//g;
42 my $offset=$input->param('offset') || 0;
43 my $op = $input->param('op') || '';
44 my $dspchoice = $input->param('select_display');
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 => "admin/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 -id=>"authorised_value",
124 -default => $data->{'authorised_value'},
128 $template->param(action => "Modify tag",
129 searchfield => $searchfield);
130 $template->param('heading-modify-tag-p' => 1);
132 $template->param(action => "Add tag");
133 $template->param('heading-add-tag-p' => 1);
135 $template->param('use-heading-flags-p' => 1);
136 $template->param(liblibrarian => $data->{'liblibrarian'},
137 libopac => $data->{'libopac'},
138 repeatable => CGI::checkbox(-name=>'repeatable',
139 -checked=> $data->{'repeatable'}?'checked':'',
144 mandatory => CGI::checkbox(-name => 'mandatory',
145 -checked => $data->{'mandatory'}?'checked':'',
150 authorised_value => $authorised_value,
151 frameworkcode => $frameworkcode,
153 # END $OP eq ADD_FORM
154 ################## ADD_VALIDATE ##################################
155 # called by add_form, used to insert/modify data in DB
156 } elsif ($op eq 'add_validate') {
157 $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
158 my $tagfield =$input->param('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($tagfield,
175 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
177 # END $OP eq ADD_VALIDATE
178 ################## DELETE_CONFIRM ##################################
179 # called by default form, used to confirm deletion of data in DB
180 } elsif ($op eq 'delete_confirm') {
181 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
182 $sth->execute($searchfield,$frameworkcode);
183 my $data=$sth->fetchrow_hashref;
185 $template->param(liblibrarian => $data->{'liblibrarian'},
186 searchfield => $searchfield,
187 frameworkcode => $frameworkcode,
189 # END $OP eq DELETE_CONFIRM
190 ################## DELETE_CONFIRMED ##################################
191 # called by delete_confirm, used to effectively confirm deletion of data in DB
192 } elsif ($op eq 'delete_confirmed') {
193 unless (C4::Context->config('demo') eq 1) {
194 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
195 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
197 # END $OP eq DELETE_CONFIRMED
198 ################## ITEMTYPE_CREATE ##################################
199 # called automatically if an unexisting frameworkis selected
200 } elsif ($op eq 'framework_create') {
201 $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");
203 my @existingframeworkloop;
204 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
206 my %line = ( value => $thisframeworkcode,
207 frameworktext => $frameworktext,
209 push @existingframeworkloop,\%line;
212 $template->param(existingframeworkloop => \@existingframeworkloop,
213 frameworkcode => $frameworkcode,
214 # FRtext => $frameworkinfo->{frameworktext},
216 ################## DEFAULT ##################################
218 # here, $op can be unset or set to "framework_create_confirm".
219 if ($searchfield ne '') {
220 $template->param(searchfield => $searchfield);
224 #here, user only wants used tags/subfields displayed
225 $searchfield=~ s/\'/\\\'/g;
226 my @data=split(' ',$searchfield);
227 my $sth=$dbh->prepare("
228 SELECT marc_tag_structure.tagfield AS mts_tagfield,
229 marc_tag_structure.liblibrarian as mts_liblibrarian,
230 marc_tag_structure.libopac as mts_libopac,
231 marc_tag_structure.repeatable as mts_repeatable,
232 marc_tag_structure.mandatory as mts_mandatory,
233 marc_tag_structure.authorised_value as mts_authorized_value,
234 marc_subfield_structure.*
235 FROM marc_tag_structure
236 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");
237 #could be ordoned by tab
238 $sth->execute($data[0], $frameworkcode);
240 while (my $data=$sth->fetchrow_hashref){
241 push(@results,$data);
250 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
256 my %row_data; # get a fresh hash for the row data
257 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
258 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
259 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
260 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
261 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
262 $row_data{subfield_link} ="marc_subfields_structure.pl?op=add_form&tagfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
263 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
264 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
265 $row_data{toggle} = $toggle;
267 my @internal_loop = ();
268 while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
270 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
271 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
272 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
273 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
274 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
275 $subfield_data{tab} = $results[$j]->{'tab'};
276 $subfield_data{seealso} = $results[$j]->{'seealso'};
277 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
278 $subfield_data{authtypecode}= $results[$j]->{'authtypecode'};
279 $subfield_data{value_builder}= $results[$j]->{'value_builder'};
280 $subfield_data{toggle} = $toggle;
281 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
282 push @internal_loop,\%subfield_data;
285 $row_data{'subfields'}=\@internal_loop;
286 push(@loop_data, \%row_data);
287 # undef @internal_loop;
290 $template->param(select_display => "True",
291 loop => \@loop_data);
295 #here, normal old style : display every tags
296 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
300 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
306 my %row_data; # get a fresh hash for the row data
307 $row_data{tagfield} = $results->[$i]{'tagfield'};
308 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
309 $row_data{repeatable} = $results->[$i]{'repeatable'};
310 $row_data{mandatory} = $results->[$i]{'mandatory'};
311 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
312 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
313 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
314 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
315 $row_data{toggle} = $toggle;
316 push(@loop_data, \%row_data);
318 $template->param(loop => \@loop_data);
321 my $prevpage = $offset-$pagesize;
322 $template->param(isprevpage => $offset,
323 prevpage=> $prevpage,
324 searchfield => $searchfield,
325 script_name => $script_name,
326 frameworkcode => $frameworkcode,
329 if ($offset+$pagesize<$cnt) {
330 my $nextpage =$offset+$pagesize;
331 $template->param(nextpage =>$nextpage,
332 searchfield => $searchfield,
333 script_name => $script_name,
334 frameworkcode => $frameworkcode,
337 } #---- END $OP eq DEFAULT
339 $template->param(loggeninuser => $loggedinuser,
340 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
341 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
342 IntranetNav => C4::Context->preference("IntranetNav"),
344 output_html_with_http_headers $input, $cookie, $template->output;
348 # the sub used for searches
351 my ($searchstring,$frameworkcode)=@_;
352 my $dbh = C4::Context->dbh;
353 $searchstring=~ s/\'/\\\'/g;
354 my @data=split(' ',$searchstring);
356 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
357 $sth->execute($data[0], $frameworkcode);
359 while (my $data=$sth->fetchrow_hashref){
360 push(@results,$data);
364 return (scalar(@results),\@results);
368 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
370 sub duplicate_framework {
371 my ($newframeworkcode,$oldframeworkcode) = @_;
372 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
373 $sth->execute($oldframeworkcode);
374 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
375 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
376 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
379 $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=?");
380 $sth->execute($oldframeworkcode);
381 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
382 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso) = $sth->fetchrow) {
383 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso);