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>.
21 use C4::Output qw( output_html_with_http_headers );
22 use C4::Auth qw( get_template_and_user );
26 use Koha::Authority::Types;
27 use Koha::AuthorisedValues;
28 use Koha::Authority::Subfields;
30 use List::MoreUtils qw( uniq );
33 my $tagfield = $input->param('tagfield');
34 my $tagsubfield = $input->param('tagsubfield');
35 my $authtypecode = $input->param('authtypecode');
36 my $op = $input->param('op') || '';
37 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
39 my ($template, $borrowernumber, $cookie) = get_template_and_user(
40 { template_name => "admin/auth_subfields_structure.tt",
43 flagsrequired => { parameters => 'manage_marc_frameworks' },
50 $template->param(script_name => $script_name,
52 authtypecode => $authtypecode,
53 $op => 1); # we show only the TMPL_VAR names $op
55 $template->param(script_name => $script_name,
57 authtypecode => $authtypecode,
58 else => 1); # we show only the TMPL_VAR names $op
61 my $dbh = C4::Context->dbh;
62 ################## ADD_FORM ##################################
63 # called by default. Used to create form to add or modify a record
64 if ($op eq 'add_form') {
65 # builds kohafield tables
68 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
70 while ((my $field) = $sth2->fetchrow_array) {
71 push @kohafields, "auth_header.".$field;
74 # build authorised value category list
75 my @authorised_value_categories = Koha::AuthorisedValues->new->categories;
76 unshift @authorised_value_categories, '';
77 push @authorised_value_categories, 'branches';
78 push @authorised_value_categories, 'itemtypes';
80 # build thesaurus categories list
81 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
83 # build value_builder list
84 my @value_builder=('');
86 # read value_builder directory.
87 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
88 # on a standard install, /cgi-bin need to be added.
89 # test one, then the other
90 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
92 unless ( opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) ) {
93 $cgidir = C4::Context->config('intranetdir');
94 opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) || die "can't opendir $cgidir/value_builder: $!";
96 while ( my $line = readdir($dir_h) ) {
98 && $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
99 push( @value_builder, $line );
102 @value_builder = sort { $a cmp $b } @value_builder;
106 my $asses = Koha::Authority::Subfields->search({ tagfield => $tagfield, authtypecode => $authtypecode}, {order_by => 'display_order'})->unblessed;
108 for my $ass ( @$asses ) {
109 my %row_data = %$ass;
110 $row_data{kohafields} = \@kohafields;
111 $row_data{authorised_values} = \@authorised_value_categories;
112 $row_data{frameworkcodes} = \@authtypes;
113 $row_data{value_builders} = \@value_builder;
114 $row_data{row} = $i++;
115 push( @loop_data, \%row_data );
118 # Add a new row for the "New" tab
119 my %row_data; # get a fresh hash for the row data
120 $row_data{'new_subfield'} = 1;
121 $row_data{tab} = -1; # ignore
122 $row_data{ohidden} = 0; # show all
123 $row_data{tagsubfield} = "";
124 $row_data{liblibrarian} = "";
125 $row_data{libopac} = "";
126 $row_data{seealso} = "";
127 $row_data{hidden} = "000";
128 $row_data{repeatable} = 0;
129 $row_data{mandatory} = 0;
130 $row_data{isurl} = 0;
131 $row_data{kohafields} = \@kohafields,
132 $row_data{authorised_values} = \@authorised_value_categories;
133 $row_data{frameworkcodes} = \@authtypes;
134 $row_data{value_builders} = \@value_builder;
136 push( @loop_data, \%row_data );
138 $template->param('use_heading_flags_p' => 1);
139 $template->param('heading_edit_subfields_p' => 1);
140 $template->param(action => "Edit subfields",
141 tagfield => $tagfield,
142 tagsubfield => $tagsubfield,
144 more_tag => $tagfield);
146 # END $OP eq ADD_FORM
147 ################## ADD_VALIDATE ##################################
148 # called by add_form, used to insert/modify data in DB
149 } elsif ($op eq 'cud-add_validate') {
150 $template->param(tagfield => "$input->param('tagfield')");
151 my @tagsubfield = $input->multi_param('tagsubfield');
152 my @liblibrarian = $input->multi_param('liblibrarian');
153 my @libopac = $input->multi_param('libopac');
154 my @kohafield = ''.$input->param('kohafield');
155 my @tab = $input->multi_param('tab');
156 my @seealso = $input->multi_param('seealso');
157 my @ohidden = $input->multi_param('ohidden');
158 my @authorised_value_categories = $input->multi_param('authorised_value');
159 my $authtypecode = $input->param('authtypecode');
160 my @frameworkcodes = $input->multi_param('frameworkcode');
161 my @value_builder =$input->multi_param('value_builder');
162 my @defaultvalue = $input->multi_param('defaultvalue');
165 for (my $i=0; $i<= $#tagsubfield ; $i++) {
166 my $tagfield =$input->param('tagfield');
167 my $tagsubfield =$tagsubfield[$i];
168 $tagsubfield="@" unless $tagsubfield ne '';
169 my $liblibrarian =$liblibrarian[$i];
170 my $libopac =$libopac[$i];
171 my $repeatable =$input->param("repeatable$i")?1:0;
172 my $mandatory =$input->param("mandatory$i")?1:0;
173 my $kohafield =$kohafield[$i];
175 my $seealso =$seealso[$i];
176 my $authorised_value = $authorised_value_categories[$i];
177 my $frameworkcode =$frameworkcodes[$i];
178 my $value_builder=$value_builder[$i];
179 my $defaultvalue = $defaultvalue[$i];
180 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
181 my $isurl = $input->param("isurl$i")?1:0;
183 my $ass = Koha::Authority::Subfields->find(
185 authtypecode => $authtypecode,
186 tagfield => $tagfield,
187 tagsubfield => $tagsubfield
191 liblibrarian => $liblibrarian,
193 repeatable => $repeatable,
194 mandatory => $mandatory,
195 kohafield => $kohafield,
198 authorised_value => $authorised_value,
199 frameworkcode => $frameworkcode,
200 value_builder => $value_builder,
203 defaultvalue => $defaultvalue,
204 display_order => $display_order->{$tagfield} || 0,
208 $ass->update($attributes);
211 Koha::Authority::Subfield->new(
213 authtypecode => $authtypecode,
214 tagfield => $tagfield,
215 tagsubfield => $tagsubfield,
220 $display_order->{$tagfield}++;
223 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode");
226 # END $OP eq ADD_VALIDATE
227 ################## DELETE_CONFIRM ##################################
228 # called by default form, used to confirm deletion of data in DB
230 elsif ( $op eq 'delete_confirm' ) {
231 my $ass = Koha::Authority::Subfields->find(
233 authtypecode => $authtypecode,
234 tagfield => $tagfield,
235 tagsubfield => $tagsubfield
240 delete_link => $script_name,
243 elsif ( $op eq 'cud-delete_confirmed' ) {
244 Koha::Authority::Subfields->find(
246 authtypecode => $authtypecode,
247 tagfield => $tagfield,
248 tagsubfield => $tagsubfield
251 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode");
255 my $ass = Koha::Authority::Subfields->search(
257 tagfield => { -like => "$tagfield%" },
258 authtypecode => $authtypecode,
260 { order_by => [ 'tagfield', 'display_order' ] }
263 $template->param( loop => $ass );
265 edit_tagfield => $tagfield,
266 edit_authtypecode => $authtypecode,
269 } #---- END $OP eq DEFAULT
270 output_html_with_http_headers $input, $cookie, $template->output;