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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 my $frameworkcode = $input->param('frameworkcode') || ''; # set to select framework
34 my $existingframeworkcode = $input->param('existingframeworkcode') || '';
35 my $searchfield = $input->param('searchfield') || 0;
36 # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
37 my $frameworkinfo = getframeworkinfo
($frameworkcode);
38 $searchfield=~ s/\,//g;
40 my $offset = $input->param('offset') || 0;
41 my $op = $input->param('op') || '';
42 my $dspchoice = $input->param('select_display');
45 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
47 my $dbh = C4
::Context
->dbh;
50 my ($template, $loggedinuser, $cookie)
51 = get_template_and_user
({template_name
=> "admin/marctagstructure.tmpl",
55 flagsrequired
=> {parameters
=> 'parameters_remaining_permissions'},
60 my $frameworks = getframeworks
();
62 foreach my $thisframeworkcode (keys %$frameworks) {
63 push @frameworkloop, {
64 value
=> $thisframeworkcode,
65 selected
=> ($thisframeworkcode eq $frameworkcode) ?
1 : 0,
66 frameworktext
=> $frameworks->{$thisframeworkcode}->{'frameworktext'},
70 # check that framework is defined in marc_tag_structure
71 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
72 $sth->execute($frameworkcode);
73 my ($frameworkexist) = $sth->fetchrow;
74 unless ($frameworkexist) {
75 # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
76 # (op = itemtyp_create_confirm)
77 if ($op eq "framework_create_confirm") {
78 duplicate_framework
($frameworkcode, $existingframeworkcode);
79 $op = ""; # unset $op to go back to framework list
81 $op = "framework_create";
85 frameworkloop
=> \
@frameworkloop,
86 frameworkcode
=> $frameworkcode,
87 frameworktext
=> $frameworkinfo->{frameworktext
},
88 script_name
=> $script_name,
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or modify a record
95 if ($op eq 'add_form') {
96 #---- if primkey exists, it's a modify action, so read values to modify...
99 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
100 $sth->execute($searchfield,$frameworkcode);
101 $data=$sth->fetchrow_hashref;
103 my $sth = $dbh->prepare("select distinct category from authorised_values");
105 my @authorised_values;
106 push @authorised_values,"";
107 while ((my $category) = $sth->fetchrow_array) {
108 push @authorised_values, $category;
110 my $authorised_value = CGI
::scrolling_list
(-name
=>'authorised_value',
111 -values=> \
@authorised_values,
113 -id
=>"authorised_value",
115 -default => $data->{'authorised_value'},
119 $template->param(searchfield
=> $searchfield);
120 $template->param(action
=> "Modify tag");
121 $template->param('heading_modify_tag_p' => 1);
123 $template->param(action
=> "Add tag");
124 $template->param('heading_add_tag_p' => 1);
126 $template->param('use_heading_flags_p' => 1);
127 $template->param(liblibrarian
=> $data->{'liblibrarian'},
128 libopac
=> $data->{'libopac'},
129 repeatable
=> $data->{'repeatable'},
130 mandatory
=> $data->{'mandatory'},
131 authorised_value
=> $authorised_value,
132 frameworkcode
=> $frameworkcode,
133 ); # FIXME: move checkboxes to presentation layer
134 # END $OP eq ADD_FORM
135 ################## ADD_VALIDATE ##################################
136 # called by add_form, used to insert/modify data in DB
137 } elsif ($op eq 'add_validate') {
138 my $tagfield = $input->param('tagfield');
139 my $liblibrarian = $input->param('liblibrarian');
140 my $libopac = $input->param('libopac');
141 my $repeatable = $input->param('repeatable') ?
1 : 0;
142 my $mandatory = $input->param('mandatory') ?
1 : 0;
143 my $authorised_value = $input->param('authorised_value');
144 unless (C4
::Context
->config('demo')) {
145 if ($input->param('modif')) {
146 $sth = $dbh->prepare(
147 "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?"
149 $sth->execute( $liblibrarian,
158 $sth = $dbh->prepare(
159 "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
161 $sth->execute($tagfield,
171 print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
173 # END $OP eq ADD_VALIDATE
174 ################## DELETE_CONFIRM ##################################
175 # called by default form, used to confirm deletion of data in DB
176 } elsif ($op eq 'delete_confirm') {
177 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
178 $sth->execute($searchfield, $frameworkcode);
179 my $data = $sth->fetchrow_hashref;
181 liblibrarian
=> $data->{'liblibrarian'},
182 searchfield
=> $searchfield,
183 frameworkcode
=> $frameworkcode,
185 # END $OP eq DELETE_CONFIRM
186 ################## DELETE_CONFIRMED ##################################
187 # called by delete_confirm, used to effectively confirm deletion of data in DB
188 } elsif ($op eq 'delete_confirmed') {
189 unless (C4
::Context
->config('demo')) {
190 my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?");
191 my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
192 $sth1->execute($searchfield, $frameworkcode);
193 $sth2->execute($searchfield, $frameworkcode);
196 searchfield
=> $searchfield,
197 frameworkcode
=> $frameworkcode,
199 # END $OP eq DELETE_CONFIRMED
200 ################## ITEMTYPE_CREATE ##################################
201 # called automatically if an unexisting frameworkis selected
202 } elsif ($op eq 'framework_create') {
203 $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");
205 my @existingframeworkloop;
206 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
208 push @existingframeworkloop, {
209 value
=> $thisframeworkcode,
210 frameworktext
=> $frameworktext,
214 $template->param(existingframeworkloop
=> \
@existingframeworkloop,
215 frameworkcode
=> $frameworkcode,
216 # FRtext => $frameworkinfo->{frameworktext},
218 ################## DEFAULT ##################################
220 # here, $op can be unset or set to "framework_create_confirm".
221 if ($searchfield ne '') {
222 $template->param(searchfield
=> $searchfield);
226 #here, user only wants used tags/subfields displayed
227 $searchfield=~ s/\'/\\\'/g;
228 my @data=split(' ',$searchfield);
229 my $sth=$dbh->prepare("
230 SELECT marc_tag_structure.tagfield AS mts_tagfield,
231 marc_tag_structure.liblibrarian as mts_liblibrarian,
232 marc_tag_structure.libopac as mts_libopac,
233 marc_tag_structure.repeatable as mts_repeatable,
234 marc_tag_structure.mandatory as mts_mandatory,
235 marc_tag_structure.authorised_value as mts_authorized_value,
236 marc_subfield_structure.*
237 FROM marc_tag_structure
238 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");
239 #could be ordoned by tab
240 $sth->execute($data[0], $frameworkcode);
242 while (my $data=$sth->fetchrow_hashref){
243 push(@results,$data);
250 while ( $i < $cnt ) {
251 my %row_data; # get a fresh hash for the row data
252 $row_data{tagfield
} = $results[$i]->{'mts_tagfield'};
253 $row_data{liblibrarian
} = $results[$i]->{'mts_liblibrarian'};
254 $row_data{repeatable
} = $results[$i]->{'mts_repeatable'};
255 $row_data{mandatory
} = $results[$i]->{'mts_mandatory'};
256 $row_data{authorised_value
} = $results[$i]->{'mts_authorised_value'};
257 $row_data{subfield_link
} = "marc_subfields_structure.pl?op=add_form&tagfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
258 $row_data{edit
} = "$script_name?op=add_form&searchfield=" .$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
259 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=" .$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
261 my @internal_loop = ();
262 while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
264 $subfield_data{tagsubfield
} = $results[$j]->{'tagsubfield'};
265 $subfield_data{liblibrarian
} = $results[$j]->{'liblibrarian'};
266 $subfield_data{kohafield
} = $results[$j]->{'kohafield'};
267 $subfield_data{repeatable
} = $results[$j]->{'repeatable'};
268 $subfield_data{mandatory
} = $results[$j]->{'mandatory'};
269 $subfield_data{tab
} = $results[$j]->{'tab'};
270 $subfield_data{seealso
} = $results[$j]->{'seealso'};
271 $subfield_data{authorised_value
} = $results[$j]->{'authorised_value'};
272 $subfield_data{authtypecode
} = $results[$j]->{'authtypecode'};
273 $subfield_data{value_builder
} = $results[$j]->{'value_builder'};
274 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
275 push @internal_loop,\
%subfield_data;
278 $row_data{'subfields'}=\
@internal_loop;
279 push(@loop_data, \
%row_data);
282 $template->param(select_display
=> "True",
283 loop => \
@loop_data);
285 #here, normal old style : display every tags
286 my ($count,$results)=StringSearch
($searchfield,$frameworkcode);
289 for ( my $i = $offset ; $i < $count ; $i++ ) {
290 my %row_data; # get a fresh hash for the row data
291 $row_data{tagfield
} = $results->[$i]{'tagfield'};
292 $row_data{liblibrarian
} = $results->[$i]{'liblibrarian'};
293 $row_data{repeatable
} = $results->[$i]{'repeatable'};
294 $row_data{mandatory
} = $results->[$i]{'mandatory'};
295 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
296 $row_data{subfield_link
} = "marc_subfields_structure.pl?tagfield=" .$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
297 $row_data{edit
} = "$script_name?op=add_form&searchfield=" .$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
298 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
299 push(@loop_data, \
%row_data);
301 $template->param(loop => \
@loop_data);
304 $template->param(isprevpage
=> $offset,
305 prevpage
=> $offset-$pagesize,
306 searchfield
=> $searchfield,
307 script_name
=> $script_name,
308 frameworkcode
=> $frameworkcode,
311 if ($offset+$pagesize<$cnt) {
312 $template->param(nextpage
=>$offset+$pagesize,
313 searchfield
=> $searchfield,
314 script_name
=> $script_name,
315 frameworkcode
=> $frameworkcode,
318 } #---- END $OP eq DEFAULT
320 output_html_with_http_headers
$input, $cookie, $template->output;
323 # the sub used for searches
326 my ($searchstring,$frameworkcode)=@_;
327 my $sth = C4
::Context
->dbh->prepare("
328 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value
329 FROM marc_tag_structure
330 WHERE (tagfield >= ? and frameworkcode=?)
333 $sth->execute($searchstring, $frameworkcode);
334 my $results = $sth->fetchall_arrayref({});
335 return (scalar(@
$results), $results);
339 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
341 sub duplicate_framework
{
342 my ($newframeworkcode,$oldframeworkcode) = @_;
343 my $dbh = C4
::Context
->dbh;
344 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
345 $sth->execute($oldframeworkcode);
346 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
347 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
348 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
351 $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=?");
352 $sth->execute($oldframeworkcode);
353 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
354 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
355 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);