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";
91 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
92 $cgidir = C4::Context->config('intranetdir');
93 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
95 while (my $line = readdir(DIR)) {
96 if ( $line =~ /\.pl$/ &&
97 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
98 push (@value_builder,$line);
101 @value_builder= sort {$a cmp $b} @value_builder;
105 my $asses = Koha::Authority::Subfields->search({ tagfield => $tagfield, authtypecode => $authtypecode}, {order_by => 'display_order'})->unblessed;
107 for my $ass ( @$asses ) {
108 my %row_data = %$ass;
109 $row_data{kohafields} = \@kohafields;
110 $row_data{authorised_values} = \@authorised_value_categories;
111 $row_data{frameworkcodes} = \@authtypes;
112 $row_data{value_builders} = \@value_builder;
113 $row_data{row} = $i++;
114 push( @loop_data, \%row_data );
117 # Add a new row for the "New" tab
118 my %row_data; # get a fresh hash for the row data
119 $row_data{'new_subfield'} = 1;
120 $row_data{tab} = -1; # ignore
121 $row_data{ohidden} = 0; # show all
122 $row_data{tagsubfield} = "";
123 $row_data{liblibrarian} = "";
124 $row_data{libopac} = "";
125 $row_data{seealso} = "";
126 $row_data{hidden} = "000";
127 $row_data{repeatable} = 0;
128 $row_data{mandatory} = 0;
129 $row_data{isurl} = 0;
130 $row_data{kohafields} = \@kohafields,
131 $row_data{authorised_values} = \@authorised_value_categories;
132 $row_data{frameworkcodes} = \@authtypes;
133 $row_data{value_builders} = \@value_builder;
135 push( @loop_data, \%row_data );
137 $template->param('use_heading_flags_p' => 1);
138 $template->param('heading_edit_subfields_p' => 1);
139 $template->param(action => "Edit subfields",
140 tagfield => $tagfield,
141 tagsubfield => $tagsubfield,
143 more_tag => $tagfield);
145 # END $OP eq ADD_FORM
146 ################## ADD_VALIDATE ##################################
147 # called by add_form, used to insert/modify data in DB
148 } elsif ($op eq 'add_validate') {
149 $template->param(tagfield => "$input->param('tagfield')");
150 my @tagsubfield = $input->multi_param('tagsubfield');
151 my @liblibrarian = $input->multi_param('liblibrarian');
152 my @libopac = $input->multi_param('libopac');
153 my @kohafield = ''.$input->param('kohafield');
154 my @tab = $input->multi_param('tab');
155 my @seealso = $input->multi_param('seealso');
156 my @ohidden = $input->multi_param('ohidden');
157 my @authorised_value_categories = $input->multi_param('authorised_value');
158 my $authtypecode = $input->param('authtypecode');
159 my @frameworkcodes = $input->multi_param('frameworkcode');
160 my @value_builder =$input->multi_param('value_builder');
161 my @defaultvalue = $input->multi_param('defaultvalue');
164 for (my $i=0; $i<= $#tagsubfield ; $i++) {
165 my $tagfield =$input->param('tagfield');
166 my $tagsubfield =$tagsubfield[$i];
167 $tagsubfield="@" unless $tagsubfield ne '';
168 my $liblibrarian =$liblibrarian[$i];
169 my $libopac =$libopac[$i];
170 my $repeatable =$input->param("repeatable$i")?1:0;
171 my $mandatory =$input->param("mandatory$i")?1:0;
172 my $kohafield =$kohafield[$i];
174 my $seealso =$seealso[$i];
175 my $authorised_value = $authorised_value_categories[$i];
176 my $frameworkcode =$frameworkcodes[$i];
177 my $value_builder=$value_builder[$i];
178 my $defaultvalue = $defaultvalue[$i];
179 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
180 my $isurl = $input->param("isurl$i")?1:0;
182 my $ass = Koha::Authority::Subfields->find(
184 authtypecode => $authtypecode,
185 tagfield => $tagfield,
186 tagsubfield => $tagsubfield
190 liblibrarian => $liblibrarian,
192 repeatable => $repeatable,
193 mandatory => $mandatory,
194 kohafield => $kohafield,
197 authorised_value => $authorised_value,
198 frameworkcode => $frameworkcode,
199 value_builder => $value_builder,
202 defaultvalue => $defaultvalue,
203 display_order => $display_order->{$tagfield} || 0,
207 $ass->update($attributes);
210 Koha::Authority::Subfield->new(
212 authtypecode => $authtypecode,
213 tagfield => $tagfield,
214 tagsubfield => $tagsubfield,
219 $display_order->{$tagfield}++;
222 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode");
225 # END $OP eq ADD_VALIDATE
226 ################## DELETE_CONFIRM ##################################
227 # called by default form, used to confirm deletion of data in DB
229 elsif ( $op eq 'delete_confirm' ) {
230 my $ass = Koha::Authority::Subfields->find(
232 authtypecode => $authtypecode,
233 tagfield => $tagfield,
234 tagsubfield => $tagsubfield
239 delete_link => $script_name,
242 elsif ( $op eq 'delete_confirmed' ) {
243 Koha::Authority::Subfields->find(
245 authtypecode => $authtypecode,
246 tagfield => $tagfield,
247 tagsubfield => $tagsubfield
250 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode");
254 my $ass = Koha::Authority::Subfields->search(
256 tagfield => { -like => "$tagfield%" },
257 authtypecode => $authtypecode,
259 { order_by => [ 'tagfield', 'display_order' ] }
262 $template->param( loop => $ass );
264 edit_tagfield => $tagfield,
265 edit_authtypecode => $authtypecode,
268 } #---- END $OP eq DEFAULT
269 output_html_with_http_headers $input, $cookie, $template->output;