Bug 28036: Improve breadcrumbs of serial claims page
[koha.git] / admin / auth_subfields_structure.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 use C4::Output;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Koha;
26
27 use Koha::Authority::Types;
28 use Koha::AuthorisedValues;
29 use Koha::Authority::Subfields;
30
31 use List::MoreUtils qw( uniq );
32
33 my $input        = CGI->new;
34 my $tagfield     = $input->param('tagfield');
35 my $tagsubfield  = $input->param('tagsubfield');
36 my $authtypecode = $input->param('authtypecode');
37 my $op           = $input->param('op') || '';
38 my $script_name  = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
39
40 my ($template, $borrowernumber, $cookie) = get_template_and_user(
41     {   template_name   => "admin/auth_subfields_structure.tt",
42         query           => $input,
43         type            => "intranet",
44         flagsrequired   => { parameters => 'manage_marc_frameworks' },
45         debug           => 1,
46     }
47 );
48 my $pagesize = 30;
49 $tagfield =~ s/\,//g;
50
51 if ($op) {
52 $template->param(script_name => $script_name,
53                                                 tagfield =>$tagfield,
54                                                 authtypecode => $authtypecode,
55                                                 $op              => 1); # we show only the TMPL_VAR names $op
56 } else {
57 $template->param(script_name => $script_name,
58                                                 tagfield =>$tagfield,
59                                                 authtypecode => $authtypecode,
60                                                 else              => 1); # we show only the TMPL_VAR names $op
61 }
62
63 my $dbh = C4::Context->dbh;
64 ################## ADD_FORM ##################################
65 # called by default. Used to create form to add or  modify a record
66 if ($op eq 'add_form') {
67         # builds kohafield tables
68         my @kohafields;
69         push @kohafields, "";
70         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
71         $sth2->execute;
72         while ((my $field) = $sth2->fetchrow_array) {
73                 push @kohafields, "auth_header.".$field;
74         }
75         
76         # build authorised value category list
77         my @authorised_value_categories = Koha::AuthorisedValues->new->categories;
78         unshift @authorised_value_categories, '';
79         push @authorised_value_categories, 'branches';
80         push @authorised_value_categories, 'itemtypes';
81
82         # build thesaurus categories list
83         my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
84
85         # build value_builder list
86         my @value_builder=('');
87
88         # read value_builder directory.
89         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
90         # on a standard install, /cgi-bin need to be added. 
91         # test one, then the other
92     my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
93         unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
94         $cgidir = C4::Context->config('intranetdir');
95                 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
96         } 
97         while (my $line = readdir(DIR)) {
98         if ( $line =~ /\.pl$/ &&
99              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
100             push (@value_builder,$line);
101                 }
102         }
103         @value_builder= sort {$a cmp $b} @value_builder;
104         closedir DIR;
105
106     my @loop_data;
107     my $asses = Koha::Authority::Subfields->search({ tagfield => $tagfield, authtypecode => $authtypecode}, {order_by => 'display_order'})->unblessed;
108     my $i;
109     for my $ass ( @$asses ) {
110         my %row_data = %$ass;
111         $row_data{kohafields}        = \@kohafields;
112         $row_data{authorised_values} = \@authorised_value_categories;
113         $row_data{frameworkcodes}    = \@authtypes;
114         $row_data{value_builders}    = \@value_builder;
115         $row_data{row}               = $i++;
116         push( @loop_data, \%row_data );
117     }
118
119     # Add a new row for the "New" tab
120     my %row_data;    # get a fresh hash for the row data
121     $row_data{'new_subfield'} = 1;
122     $row_data{tab} = -1; # ignore
123     $row_data{ohidden} = 0; # show all
124     $row_data{tagsubfield}      = "";
125     $row_data{liblibrarian}     = "";
126     $row_data{libopac}          = "";
127     $row_data{seealso}          = "";
128     $row_data{hidden}           = "000";
129     $row_data{repeatable}       = 0;
130     $row_data{mandatory}        = 0;
131     $row_data{isurl}            = 0;
132     $row_data{kohafields} = \@kohafields,
133     $row_data{authorised_values} = \@authorised_value_categories;
134     $row_data{frameworkcodes} = \@authtypes;
135     $row_data{value_builders} = \@value_builder;
136     $row_data{row} = $i;
137     push( @loop_data, \%row_data );
138
139         $template->param('use_heading_flags_p' => 1);
140         $template->param('heading_edit_subfields_p' => 1);
141         $template->param(action => "Edit subfields",
142                                                         tagfield => $tagfield,
143                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
144                                                         loop => \@loop_data,
145                                                         more_tag => $tagfield);
146
147                                                                                                 # END $OP eq ADD_FORM
148 ################## ADD_VALIDATE ##################################
149 # called by add_form, used to insert/modify data in DB
150 } elsif ($op eq 'add_validate') {
151         $template->param(tagfield => "$input->param('tagfield')");
152         my @tagsubfield = $input->multi_param('tagsubfield');
153         my @liblibrarian        = $input->multi_param('liblibrarian');
154         my @libopac             = $input->multi_param('libopac');
155         my @kohafield           = ''.$input->param('kohafield');
156         my @tab                         = $input->multi_param('tab');
157         my @seealso             = $input->multi_param('seealso');
158     my @ohidden             = $input->multi_param('ohidden');
159     my @authorised_value_categories = $input->multi_param('authorised_value');
160         my $authtypecode        = $input->param('authtypecode');
161         my @frameworkcodes      = $input->multi_param('frameworkcode');
162         my @value_builder       =$input->multi_param('value_builder');
163     my @defaultvalue = $input->multi_param('defaultvalue');
164
165     my $display_order;
166         for (my $i=0; $i<= $#tagsubfield ; $i++) {
167                 my $tagfield                    =$input->param('tagfield');
168                 my $tagsubfield         =$tagsubfield[$i];
169                 $tagsubfield="@" unless $tagsubfield ne '';
170                 my $liblibrarian                =$liblibrarian[$i];
171                 my $libopac                     =$libopac[$i];
172                 my $repeatable          =$input->param("repeatable$i")?1:0;
173                 my $mandatory           =$input->param("mandatory$i")?1:0;
174                 my $kohafield           =$kohafield[$i];
175                 my $tab                         =$tab[$i];
176                 my $seealso                             =$seealso[$i];
177         my $authorised_value = $authorised_value_categories[$i];
178                 my $frameworkcode               =$frameworkcodes[$i];
179                 my $value_builder=$value_builder[$i];
180         my $defaultvalue = $defaultvalue[$i];
181                 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
182                 my $isurl = $input->param("isurl$i")?1:0;
183         if ($liblibrarian) {
184             my $ass = Koha::Authority::Subfields->find(
185                 {
186                     authtypecode => $authtypecode,
187                     tagfield     => $tagfield,
188                     tagsubfield  => $tagsubfield
189                 }
190             );
191             my $attributes = {
192                 liblibrarian     => $liblibrarian,
193                 libopac          => $libopac,
194                 repeatable       => $repeatable,
195                 mandatory        => $mandatory,
196                 kohafield        => $kohafield,
197                 tab              => $tab,
198                 seealso          => $seealso,
199                 authorised_value => $authorised_value,
200                 frameworkcode    => $frameworkcode,
201                 value_builder    => $value_builder,
202                 hidden           => $hidden,
203                 isurl            => $isurl,
204                 defaultvalue     => $defaultvalue,
205                 display_order    => $display_order->{$tagfield} || 0,
206             };
207
208             if ($ass) {
209                 $ass->update($attributes);
210             }
211             else {
212                 Koha::Authority::Subfield->new(
213                     {
214                         authtypecode => $authtypecode,
215                         tagfield     => $tagfield,
216                         tagsubfield  => $tagsubfield,
217                         %$attributes
218                     }
219                 )->store;
220             }
221             $display_order->{$tagfield}++;
222         }
223         }
224     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
225     exit;
226
227                                                                                                         # END $OP eq ADD_VALIDATE
228 ################## DELETE_CONFIRM ##################################
229 # called by default form, used to confirm deletion of data in DB
230 }
231 elsif ( $op eq 'delete_confirm' ) {
232   my $ass = Koha::Authority::Subfields->find(
233       {
234           authtypecode => $authtypecode,
235           tagfield     => $tagfield,
236           tagsubfield  => $tagsubfield
237       }
238   );
239   $template->param(
240       ass         => $ass,
241       delete_link => $script_name,
242   );
243 }
244 elsif ( $op eq 'delete_confirmed' ) {
245     Koha::Authority::Subfields->find(
246         {
247             authtypecode => $authtypecode,
248             tagfield     => $tagfield,
249             tagsubfield  => $tagsubfield
250         }
251     )->delete;
252     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
253     exit;
254 }
255 else {    # DEFAULT
256     my $ass = Koha::Authority::Subfields->search(
257         {
258             tagfield      => { -like => "$tagfield%" },
259             authtypecode  => $authtypecode,
260         },
261         { order_by => [ 'tagfield', 'display_order' ] }
262     )->unblessed;
263
264     $template->param( loop => $ass );
265     $template->param(
266         edit_tagfield      => $tagfield,
267         edit_authtypecode  => $authtypecode,
268     );
269
270 } #---- END $OP eq DEFAULT
271 output_html_with_http_headers $input, $cookie, $template->output;