3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 my ( $searchstring, $frameworkcode ) = @_;
29 my $dbh = C4::Context->dbh;
30 $searchstring =~ s/\'/\\\'/g;
31 my @data = split( ' ', $searchstring );
35 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
37 $sth->execute( "$searchstring%", $frameworkcode );
42 while ( my $data = $sth->fetchrow_hashref ) {
43 push( @results, $data );
48 return ( $cnt, \@results );
51 sub marc_subfield_structure_exists {
52 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
53 my $dbh = C4::Context->dbh;
54 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
55 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
60 my $tagfield = $input->param('tagfield');
61 my $tagsubfield = $input->param('tagsubfield');
62 my $frameworkcode = $input->param('frameworkcode');
63 my $pkfield = "tagfield";
64 my $offset = $input->param('offset') || 0;
65 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
67 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
69 template_name => "admin/marc_subfields_structure.tt",
73 flagsrequired => { parameters => 'parameters_remaining_permissions' },
77 my $cache = Koha::Cache->get_instance();
79 my $op = $input->param('op') || "";
84 script_name => $script_name,
85 tagfield => $tagfield,
86 frameworkcode => $frameworkcode,
88 ); # we show only the TMPL_VAR names $op
92 script_name => $script_name,
93 tagfield => $tagfield,
94 frameworkcode => $frameworkcode,
96 ); # 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 my $dbh = C4::Context->dbh;
104 # builds kohafield tables
106 push @kohafields, "";
107 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
109 while ( ( my $field ) = $sth2->fetchrow_array ) {
110 push @kohafields, "biblio." . $field;
112 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
114 while ( ( my $field ) = $sth2->fetchrow_array ) {
115 if ( $field eq 'notes' ) { $field = 'bnotes'; }
116 push @kohafields, "biblioitems." . $field;
118 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
120 while ( ( my $field ) = $sth2->fetchrow_array ) {
121 push @kohafields, "items." . $field;
124 # build authorised value list
126 $sth2 = $dbh->prepare("select distinct category from authorised_values");
128 my @authorised_values;
129 push @authorised_values, "";
130 while ( ( my $category ) = $sth2->fetchrow_array ) {
131 push @authorised_values, $category;
133 push( @authorised_values, "branches" );
134 push( @authorised_values, "itemtypes" );
135 push( @authorised_values, "cn_source" );
137 # build thesaurus categories list
139 $sth2 = $dbh->prepare("select authtypecode from auth_types");
143 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
144 push @authtypes, $authtypecode;
147 # build value_builder list
148 my @value_builder = ('');
150 # read value_builder directory.
151 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
152 # on a standard install, /cgi-bin need to be added.
153 # test one, then the other
154 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
155 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
156 $cgidir = C4::Context->config('intranetdir');
157 opendir( DIR, "$cgidir/cataloguing/value_builder" )
158 || die "can't opendir $cgidir/value_builder: $!";
160 while ( my $line = readdir(DIR) ) {
161 if ( $line =~ /\.pl$/ &&
162 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
163 push( @value_builder, $line );
166 @value_builder= sort {$a cmp $b} @value_builder;
172 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
173 ); # and tagsubfield='$tagsubfield'");
174 $sth->execute( $tagfield, $frameworkcode );
177 while ( my $data = $sth->fetchrow_hashref ) {
178 my %row_data; # get a fresh hash for the row data
179 $row_data{defaultvalue} = $data->{defaultvalue};
180 $row_data{maxlength} = $data->{maxlength};
181 $row_data{tab} = $data->{tab};
182 $row_data{tagsubfield} = $data->{tagsubfield};
183 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@' ? '_' : $data->{'tagsubfield'};
184 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
185 $row_data{liblibrarian} = $data->{'liblibrarian'};
186 $row_data{libopac} = $data->{'libopac'};
187 $row_data{seealso} = $data->{'seealso'};
188 $row_data{kohafields} = \@kohafields;
189 $row_data{kohafield} = $data->{kohafield};
190 $row_data{authorised_values} = \@authorised_values;
191 $row_data{authorised_value} = $data->{authorised_value};
192 $row_data{value_builders} = \@value_builder;
193 $row_data{value_builder} = $data->{'value_builder'};
194 $row_data{authtypes} = \@authtypes;
195 $row_data{authtypecode} = $data->{'authtypecode'};
196 $row_data{repeatable} = $data->{repeatable};
197 $row_data{mandatory} = $data->{mandatory};
198 $row_data{hidden} = $data->{hidden};
199 $row_data{isurl} = $data->{isurl};
201 $row_data{link} = $data->{'link'};
202 push( @loop_data, \%row_data );
206 # Add a new row for the "New" tab
207 my %row_data; # get a fresh hash for the row data
208 $row_data{'new_subfield'} = 1;
209 $row_data{'subfieldcode'} = '';
210 $row_data{'maxlength'} = 9999;
211 $row_data{tab} = -1; #ignore
212 $row_data{tagsubfield} = "";
213 $row_data{liblibrarian} = "";
214 $row_data{libopac} = "";
215 $row_data{seealso} = "";
216 $row_data{hidden} = "";
217 $row_data{repeatable} = 0;
218 $row_data{mandatory} = 0;
219 $row_data{isurl} = 0;
220 $row_data{kohafields} = \@kohafields;
221 $row_data{authorised_values} = \@authorised_values;
222 $row_data{value_builders} = \@value_builder;
223 $row_data{authtypes} = \@authtypes;
224 $row_data{link} = "";
226 push( @loop_data, \%row_data );
228 $template->param( 'use_heading_flags_p' => 1 );
229 $template->param( 'heading_edit_subfields_p' => 1 );
231 action => "Edit subfields",
232 tagfield => $tagfield,
234 more_tag => $tagfield
237 # END $OP eq ADD_FORM
238 ################## ADD_VALIDATE ##################################
239 # called by add_form, used to insert/modify data in DB
241 elsif ( $op eq 'add_validate' ) {
242 my $dbh = C4::Context->dbh;
243 $template->param( tagfield => "$input->param('tagfield')" );
244 # my $sth = $dbh->prepare(
245 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
246 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
248 my $sth_insert = $dbh->prepare(qq{
249 insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue,maxlength)
250 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
252 my $sth_update = $dbh->prepare(qq{
253 update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?, link=?, defaultvalue=?, maxlength=?
254 where tagfield=? and tagsubfield=? and frameworkcode=?
256 my @tagsubfield = $input->multi_param('tagsubfield');
257 my @liblibrarian = $input->multi_param('liblibrarian');
258 my @libopac = $input->multi_param('libopac');
259 my @kohafield = $input->multi_param('kohafield');
260 my @tab = $input->multi_param('tab');
261 my @seealso = $input->multi_param('seealso');
262 my @hidden = $input->multi_param('hidden');
263 my @authorised_values = $input->multi_param('authorised_value');
264 my @authtypecodes = $input->multi_param('authtypecode');
265 my @value_builder = $input->multi_param('value_builder');
266 my @link = $input->multi_param('link');
267 my @defaultvalue = $input->multi_param('defaultvalue');
268 my @maxlength = $input->multi_param('maxlength');
270 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
271 my $tagfield = $input->param('tagfield');
272 my $tagsubfield = $tagsubfield[$i];
273 $tagsubfield = "@" unless $tagsubfield ne '';
274 $tagsubfield = "@" if $tagsubfield eq '_';
275 my $liblibrarian = $liblibrarian[$i];
276 my $libopac = $libopac[$i];
277 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
278 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
279 my $kohafield = $kohafield[$i];
281 my $seealso = $seealso[$i];
282 my $authorised_value = $authorised_values[$i];
283 my $authtypecode = $authtypecodes[$i];
284 my $value_builder = $value_builder[$i];
285 my $hidden = $hidden[$i]; #input->param("hidden$i");
286 my $isurl = $input->param("isurl$i") ? 1 : 0;
287 my $link = $link[$i];
288 my $defaultvalue = $defaultvalue[$i];
289 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
291 if (defined($liblibrarian) && $liblibrarian ne "") {
292 my $is_demo = C4::Context->config('demo') || '';
293 if ( $is_demo ne '1' ) {
294 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
295 $sth_update->execute(
321 $sth_insert->execute(
347 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
348 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
349 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
351 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
354 # END $OP eq ADD_VALIDATE
355 ################## DELETE_CONFIRM ##################################
356 # called by default form, used to confirm deletion of data in DB
358 elsif ( $op eq 'delete_confirm' ) {
359 my $dbh = C4::Context->dbh;
362 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
365 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
366 my $data = $sth->fetchrow_hashref;
369 liblibrarian => $data->{'liblibrarian'},
370 tagsubfield => $data->{'tagsubfield'},
371 delete_link => $script_name,
372 tagfield => $tagfield,
373 tagsubfield => $tagsubfield,
374 frameworkcode => $frameworkcode,
377 # END $OP eq DELETE_CONFIRM
378 ################## DELETE_CONFIRMED ##################################
379 # called by delete_confirm, used to effectively confirm deletion of data in DB
381 elsif ( $op eq 'delete_confirmed' ) {
382 my $dbh = C4::Context->dbh;
383 my $is_demo = C4::Context->config('demo') || '';
384 if ( $is_demo ne '1' ) {
387 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
389 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
392 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
393 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
394 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
395 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
398 # END $OP eq DELETE_CONFIRMED
399 ################## DEFAULT ##################################
402 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
404 for ( my $i = 0; $i < $count; $i++ ) {
405 my %row_data; # get a fresh hash for the row data
406 $row_data{tagfield} = $results->[$i]{'tagfield'};
407 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
408 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
409 $row_data{kohafield} = $results->[$i]{'kohafield'};
410 $row_data{repeatable} = $results->[$i]{'repeatable'};
411 $row_data{mandatory} = $results->[$i]{'mandatory'};
412 $row_data{tab} = $results->[$i]{'tab'};
413 $row_data{seealso} = $results->[$i]{'seealso'};
414 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
415 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
416 $row_data{value_builder} = $results->[$i]{'value_builder'};
417 $row_data{hidden} = $results->[$i]{'hidden'};
418 $row_data{isurl} = $results->[$i]{'isurl'};
419 $row_data{link} = $results->[$i]{'link'};
421 if ( $row_data{tab} eq -1 ) {
422 $row_data{subfield_ignored} = 1;
425 push( @loop_data, \%row_data );
427 $template->param( loop => \@loop_data );
429 edit_tagfield => $tagfield,
430 edit_frameworkcode => $frameworkcode
433 } #---- END $OP eq DEFAULT
435 output_html_with_http_headers $input, $cookie, $template->output;