Bug 29648: patron list table - normal
[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 qw( output_html_with_http_headers );
22 use C4::Auth qw( get_template_and_user );
23 use CGI qw ( -utf8 );
24 use C4::Context;
25
26 use Koha::Authority::Types;
27 use Koha::AuthorisedValues;
28 use Koha::Authority::Subfields;
29
30 use List::MoreUtils qw( uniq );
31
32 my $input        = CGI->new;
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";
38
39 my ($template, $borrowernumber, $cookie) = get_template_and_user(
40     {   template_name   => "admin/auth_subfields_structure.tt",
41         query           => $input,
42         type            => "intranet",
43         flagsrequired   => { parameters => 'manage_marc_frameworks' },
44     }
45 );
46 my $pagesize = 30;
47 $tagfield =~ s/\,//g;
48
49 if ($op) {
50 $template->param(script_name => $script_name,
51                                                 tagfield =>$tagfield,
52                                                 authtypecode => $authtypecode,
53                                                 $op              => 1); # we show only the TMPL_VAR names $op
54 } else {
55 $template->param(script_name => $script_name,
56                                                 tagfield =>$tagfield,
57                                                 authtypecode => $authtypecode,
58                                                 else              => 1); # we show only the TMPL_VAR names $op
59 }
60
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
66         my @kohafields;
67         push @kohafields, "";
68         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
69         $sth2->execute;
70         while ((my $field) = $sth2->fetchrow_array) {
71                 push @kohafields, "auth_header.".$field;
72         }
73         
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';
79
80         # build thesaurus categories list
81         my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
82
83         # build value_builder list
84         my @value_builder=('');
85
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: $!";
94         } 
95         while (my $line = readdir(DIR)) {
96         if ( $line =~ /\.pl$/ &&
97              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
98             push (@value_builder,$line);
99                 }
100         }
101         @value_builder= sort {$a cmp $b} @value_builder;
102         closedir DIR;
103
104     my @loop_data;
105     my $asses = Koha::Authority::Subfields->search({ tagfield => $tagfield, authtypecode => $authtypecode}, {order_by => 'display_order'})->unblessed;
106     my $i;
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 );
115     }
116
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;
134     $row_data{row} = $i;
135     push( @loop_data, \%row_data );
136
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                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
142                                                         loop => \@loop_data,
143                                                         more_tag => $tagfield);
144
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');
162
163     my $display_order;
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];
173                 my $tab                         =$tab[$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;
181         if ($liblibrarian) {
182             my $ass = Koha::Authority::Subfields->find(
183                 {
184                     authtypecode => $authtypecode,
185                     tagfield     => $tagfield,
186                     tagsubfield  => $tagsubfield
187                 }
188             );
189             my $attributes = {
190                 liblibrarian     => $liblibrarian,
191                 libopac          => $libopac,
192                 repeatable       => $repeatable,
193                 mandatory        => $mandatory,
194                 kohafield        => $kohafield,
195                 tab              => $tab,
196                 seealso          => $seealso,
197                 authorised_value => $authorised_value,
198                 frameworkcode    => $frameworkcode,
199                 value_builder    => $value_builder,
200                 hidden           => $hidden,
201                 isurl            => $isurl,
202                 defaultvalue     => $defaultvalue,
203                 display_order    => $display_order->{$tagfield} || 0,
204             };
205
206             if ($ass) {
207                 $ass->update($attributes);
208             }
209             else {
210                 Koha::Authority::Subfield->new(
211                     {
212                         authtypecode => $authtypecode,
213                         tagfield     => $tagfield,
214                         tagsubfield  => $tagsubfield,
215                         %$attributes
216                     }
217                 )->store;
218             }
219             $display_order->{$tagfield}++;
220         }
221         }
222     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
223     exit;
224
225                                                                                                         # END $OP eq ADD_VALIDATE
226 ################## DELETE_CONFIRM ##################################
227 # called by default form, used to confirm deletion of data in DB
228 }
229 elsif ( $op eq 'delete_confirm' ) {
230   my $ass = Koha::Authority::Subfields->find(
231       {
232           authtypecode => $authtypecode,
233           tagfield     => $tagfield,
234           tagsubfield  => $tagsubfield
235       }
236   );
237   $template->param(
238       ass         => $ass,
239       delete_link => $script_name,
240   );
241 }
242 elsif ( $op eq 'delete_confirmed' ) {
243     Koha::Authority::Subfields->find(
244         {
245             authtypecode => $authtypecode,
246             tagfield     => $tagfield,
247             tagsubfield  => $tagsubfield
248         }
249     )->delete;
250     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
251     exit;
252 }
253 else {    # DEFAULT
254     my $ass = Koha::Authority::Subfields->search(
255         {
256             tagfield      => { -like => "$tagfield%" },
257             authtypecode  => $authtypecode,
258         },
259         { order_by => [ 'tagfield', 'display_order' ] }
260     )->unblessed;
261
262     $template->param( loop => $ass );
263     $template->param(
264         edit_tagfield      => $tagfield,
265         edit_authtypecode  => $authtypecode,
266     );
267
268 } #---- END $OP eq DEFAULT
269 output_html_with_http_headers $input, $cookie, $template->output;