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') || 0;
44 my $op = $input->param('op') || '';
45 my $dspchoice = $input->param('select_display');
48 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
50 my $dbh = C4::Context->dbh;
53 my ($template, $loggedinuser, $cookie)
54 = get_template_and_user({template_name => "admin/marctagstructure.tmpl",
58 flagsrequired => {parameters => 1},
63 my $frameworks = getframeworks();
65 foreach my $thisframeworkcode (keys %$frameworks) {
66 my $selected = 1 if $thisframeworkcode eq $frameworkcode;
67 my %row =(value => $thisframeworkcode,
68 selected => $selected,
69 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
71 push @frameworkloop, \%row;
74 # check that framework is defined in marc_tag_structure
75 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
76 $sth->execute($frameworkcode);
77 my ($frameworkexist) = $sth->fetchrow;
78 if ($frameworkexist) {
80 # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
81 # (op = itemtyp_create_confirm)
82 if ($op eq "framework_create_confirm") {
83 duplicate_framework($frameworkcode, $existingframeworkcode);
84 $op=""; # unset $op to go back to framework list
86 $op = "framework_create";
89 $template->param(frameworkloop => \@frameworkloop,
90 frameworkcode => $frameworkcode,
91 frameworktext => $frameworkinfo->{frameworktext});
93 $template->param(script_name => $script_name,
94 $op => 1); # we show only the TMPL_VAR names $op
96 $template->param(script_name => $script_name,
97 else => 1); # we show only the TMPL_VAR names $op
101 ################## ADD_FORM ##################################
102 # called by default. Used to create form to add or modify a record
103 if ($op eq 'add_form') {
104 #---- if primkey exists, it's a modify action, so read values to modify...
107 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
108 $sth->execute($searchfield,$frameworkcode);
109 $data=$sth->fetchrow_hashref;
112 my $sth = $dbh->prepare("select distinct category from authorised_values");
114 my @authorised_values;
115 push @authorised_values,"";
116 while ((my $category) = $sth->fetchrow_array) {
117 push @authorised_values, $category;
119 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
120 -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':'',
143 mandatory => CGI::checkbox(-name => 'mandatory',
144 -checked => $data->{'mandatory'}?'checked':'',
148 authorised_value => $authorised_value,
149 frameworkcode => $frameworkcode,
151 # END $OP eq ADD_FORM
152 ################## ADD_VALIDATE ##################################
153 # called by add_form, used to insert/modify data in DB
154 } elsif ($op eq 'add_validate') {
155 $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
156 my $tagfield =$input->param('tagfield');
157 my $liblibrarian = $input->param('liblibrarian');
158 my $libopac =$input->param('libopac');
159 my $repeatable =$input->param('repeatable');
160 my $mandatory =$input->param('mandatory');
161 my $authorised_value =$input->param('authorised_value');
162 unless (C4::Context->config('demo') eq 1) {
163 $sth->execute($tagfield,
173 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
175 # END $OP eq ADD_VALIDATE
176 ################## DELETE_CONFIRM ##################################
177 # called by default form, used to confirm deletion of data in DB
178 } elsif ($op eq 'delete_confirm') {
179 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
180 $sth->execute($searchfield,$frameworkcode);
181 my $data=$sth->fetchrow_hashref;
183 $template->param(liblibrarian => $data->{'liblibrarian'},
184 searchfield => $searchfield,
185 frameworkcode => $frameworkcode,
187 # END $OP eq DELETE_CONFIRM
188 ################## DELETE_CONFIRMED ##################################
189 # called by delete_confirm, used to effectively confirm deletion of data in DB
190 } elsif ($op eq 'delete_confirmed') {
191 unless (C4::Context->config('demo') eq 1) {
192 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
193 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
195 # END $OP eq DELETE_CONFIRMED
196 ################## ITEMTYPE_CREATE ##################################
197 # called automatically if an unexisting frameworkis selected
198 } elsif ($op eq 'framework_create') {
199 $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");
201 my @existingframeworkloop;
202 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
204 my %line = ( value => $thisframeworkcode,
205 frameworktext => $frameworktext,
207 push @existingframeworkloop,\%line;
210 $template->param(existingframeworkloop => \@existingframeworkloop,
211 frameworkcode => $frameworkcode,
212 # FRtext => $frameworkinfo->{frameworktext},
214 ################## DEFAULT ##################################
216 # here, $op can be unset or set to "framework_create_confirm".
217 if ($searchfield ne '') {
218 $template->param(searchfield => $searchfield);
222 #here, user only wants used tags/subfields displayed
224 $searchfield=~ s/\'/\\\'/g;
225 my @data=split(' ',$searchfield);
226 my $sth=$dbh->prepare("Select marc_tag_structure.tagfield as mts_tagfield,marc_tag_structure.liblibrarian as mts_liblibrarian,marc_tag_structure.libopac as mts_libopac,marc_tag_structure.repeatable as mts_repeatable,marc_tag_structure.mandatory as mts_mandatory,marc_tag_structure.authorised_value as mts_authorized_value,marc_subfield_structure.* from marc_tag_structure 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");
227 #could be ordoned by tab
228 $sth->execute($data[0], $frameworkcode);
230 while (my $data=$sth->fetchrow_hashref){
231 push(@results,$data);
240 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
246 my %row_data; # get a fresh hash for the row data
247 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
248 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
249 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
250 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
251 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
252 $row_data{subfield_link} ="marc_subfields_structure.pl?op=add_form&tagfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
253 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
254 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
255 $row_data{toggle} = $toggle;
257 my @internal_loop = ();
258 while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
265 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
266 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
267 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
268 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
269 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
270 $subfield_data{tab} = $results[$j]->{'tab'};
271 $subfield_data{seealso} = $results[$j]->{'seealso'};
272 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
273 $subfield_data{authtypecode}= $results[$j]->{'authtypecode'};
274 $subfield_data{value_builder}= $results[$j]->{'value_builder'};
275 $subfield_data{toggle} = $toggle;
276 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
277 push @internal_loop,\%subfield_data;
280 $row_data{'subfields'}=\@internal_loop;
281 push(@loop_data, \%row_data);
282 # undef @internal_loop;
285 $template->param(select_display => "True",
286 loop => \@loop_data);
290 #here, normal old style : display every tags
292 my ($count,$results)=StringSearch($env,$searchfield,$frameworkcode);
296 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
302 my %row_data; # get a fresh hash for the row data
303 $row_data{tagfield} = $results->[$i]{'tagfield'};
304 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
305 $row_data{repeatable} = $results->[$i]{'repeatable'};
306 $row_data{mandatory} = $results->[$i]{'mandatory'};
307 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
308 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
309 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
310 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
311 $row_data{toggle} = $toggle;
312 push(@loop_data, \%row_data);
314 $template->param(loop => \@loop_data);
317 my $prevpage = $offset-$pagesize;
318 $template->param(isprevpage => $offset,
319 prevpage=> $prevpage,
320 searchfield => $searchfield,
321 script_name => $script_name,
322 frameworkcode => $frameworkcode,
325 if ($offset+$pagesize<$cnt) {
326 my $nextpage =$offset+$pagesize;
327 $template->param(nextpage =>$nextpage,
328 searchfield => $searchfield,
329 script_name => $script_name,
330 frameworkcode => $frameworkcode,
333 } #---- END $OP eq DEFAULT
335 $template->param(loggeninuser => $loggedinuser);
336 output_html_with_http_headers $input, $cookie, $template->output;
340 # the sub used for searches
343 my ($env,$searchstring,$frameworkcode)=@_;
344 my $dbh = C4::Context->dbh;
345 $searchstring=~ s/\'/\\\'/g;
346 my @data=split(' ',$searchstring);
348 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
349 $sth->execute($data[0], $frameworkcode);
351 while (my $data=$sth->fetchrow_hashref){
352 push(@results,$data);
353 warn "=> ".$data->{liblibrarian};
357 return (scalar(@results),\@results);
361 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
363 sub duplicate_framework {
364 my ($newframeworkcode,$oldframeworkcode) = @_;
365 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
366 $sth->execute($oldframeworkcode);
367 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
368 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
369 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
372 $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=?");
373 $sth->execute($oldframeworkcode);
374 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
375 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso) = $sth->fetchrow) {
376 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso);