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.tt",
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;
104 my @authorised_values = @{C4::Koha::GetAuthorisedValueCategories()}; # function returns array ref, dereferencing
105 unshift @authorised_values, ""; # put empty value first
106 my $authorised_value = {
107 values => \@authorised_values,
108 default => $data->{'authorised_value'},
112 $template->param(searchfield => $searchfield);
113 $template->param(action => "Modify tag");
114 $template->param('heading_modify_tag_p' => 1);
116 $template->param(action => "Add tag");
117 $template->param('heading_add_tag_p' => 1);
119 $template->param('use_heading_flags_p' => 1);
120 $template->param(liblibrarian => $data->{'liblibrarian'},
121 libopac => $data->{'libopac'},
122 repeatable => $data->{'repeatable'},
123 mandatory => $data->{'mandatory'},
124 authorised_value => $authorised_value,
125 frameworkcode => $frameworkcode,
126 ); # FIXME: move checkboxes to presentation layer
127 # END $OP eq ADD_FORM
128 ################## ADD_VALIDATE ##################################
129 # called by add_form, used to insert/modify data in DB
130 } elsif ($op eq 'add_validate') {
131 my $tagfield = $input->param('tagfield');
132 my $liblibrarian = $input->param('liblibrarian');
133 my $libopac = $input->param('libopac');
134 my $repeatable = $input->param('repeatable') ? 1 : 0;
135 my $mandatory = $input->param('mandatory') ? 1 : 0;
136 my $authorised_value = $input->param('authorised_value');
137 unless (C4::Context->config('demo')) {
138 if ($input->param('modif')) {
139 $sth = $dbh->prepare(
140 "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?"
142 $sth->execute( $liblibrarian,
151 $sth = $dbh->prepare(
152 "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
154 $sth->execute($tagfield,
164 print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
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 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')) {
183 my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?");
184 my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
185 $sth1->execute($searchfield, $frameworkcode);
186 $sth2->execute($searchfield, $frameworkcode);
189 searchfield => $searchfield,
190 frameworkcode => $frameworkcode,
192 # END $OP eq DELETE_CONFIRMED
193 ################## ITEMTYPE_CREATE ##################################
194 # called automatically if an unexisting frameworkis selected
195 } elsif ($op eq 'framework_create') {
196 $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");
198 my @existingframeworkloop;
199 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
201 push @existingframeworkloop, {
202 value => $thisframeworkcode,
203 frameworktext => $frameworktext,
207 $template->param(existingframeworkloop => \@existingframeworkloop,
208 frameworkcode => $frameworkcode,
209 # FRtext => $frameworkinfo->{frameworktext},
211 ################## DEFAULT ##################################
213 # here, $op can be unset or set to "framework_create_confirm".
214 if ($searchfield ne '') {
215 $template->param(searchfield => $searchfield);
219 #here, user only wants used tags/subfields displayed
220 $searchfield=~ s/\'/\\\'/g;
221 my @data=split(' ',$searchfield);
222 my $sth=$dbh->prepare("
223 SELECT marc_tag_structure.tagfield AS mts_tagfield,
224 marc_tag_structure.liblibrarian as mts_liblibrarian,
225 marc_tag_structure.libopac as mts_libopac,
226 marc_tag_structure.repeatable as mts_repeatable,
227 marc_tag_structure.mandatory as mts_mandatory,
228 marc_tag_structure.authorised_value as mts_authorized_value,
229 marc_subfield_structure.*
230 FROM marc_tag_structure
231 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");
232 #could be ordoned by tab
233 $sth->execute($data[0], $frameworkcode);
235 while (my $data=$sth->fetchrow_hashref){
236 push(@results,$data);
243 while ( $i < $cnt ) {
244 my %row_data; # get a fresh hash for the row data
245 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
246 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
247 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
248 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
249 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
250 $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&tagfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
251 $row_data{edit} = "$script_name?op=add_form&searchfield=" .$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
252 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=" .$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
254 my @internal_loop = ();
255 while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
257 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
258 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
259 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
260 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
261 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
262 $subfield_data{tab} = $results[$j]->{'tab'};
263 $subfield_data{seealso} = $results[$j]->{'seealso'};
264 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
265 $subfield_data{authtypecode} = $results[$j]->{'authtypecode'};
266 $subfield_data{value_builder} = $results[$j]->{'value_builder'};
267 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
268 push @internal_loop,\%subfield_data;
271 $row_data{'subfields'}=\@internal_loop;
272 push(@loop_data, \%row_data);
275 $template->param(select_display => "True",
276 loop => \@loop_data);
278 #here, normal old style : display every tags
279 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
282 for ( my $i = $offset ; $i < $count ; $i++ ) {
283 my %row_data; # get a fresh hash for the row data
284 $row_data{tagfield} = $results->[$i]{'tagfield'};
285 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
286 $row_data{repeatable} = $results->[$i]{'repeatable'};
287 $row_data{mandatory} = $results->[$i]{'mandatory'};
288 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
289 $row_data{subfield_link} = "marc_subfields_structure.pl?tagfield=" .$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
290 $row_data{edit} = "$script_name?op=add_form&searchfield=" .$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
291 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
292 push(@loop_data, \%row_data);
294 $template->param(loop => \@loop_data);
297 $template->param(isprevpage => $offset,
298 prevpage=> $offset-$pagesize,
299 searchfield => $searchfield,
300 script_name => $script_name,
301 frameworkcode => $frameworkcode,
304 if ($offset+$pagesize<$cnt) {
305 $template->param(nextpage =>$offset+$pagesize,
306 searchfield => $searchfield,
307 script_name => $script_name,
308 frameworkcode => $frameworkcode,
311 } #---- END $OP eq DEFAULT
313 output_html_with_http_headers $input, $cookie, $template->output;
316 # the sub used for searches
319 my ($searchstring,$frameworkcode)=@_;
320 my $sth = C4::Context->dbh->prepare("
321 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value
322 FROM marc_tag_structure
323 WHERE (tagfield >= ? and frameworkcode=?)
326 $sth->execute($searchstring, $frameworkcode);
327 my $results = $sth->fetchall_arrayref({});
328 return (scalar(@$results), $results);
332 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
334 sub duplicate_framework {
335 my ($newframeworkcode,$oldframeworkcode) = @_;
336 my $dbh = C4::Context->dbh;
337 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
338 $sth->execute($oldframeworkcode);
339 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
340 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
341 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
344 $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=?");
345 $sth->execute($oldframeworkcode);
346 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
347 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
348 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);