fcb419eed529249b5a459457602744d36d363a32
[srvgit] / 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     }
46 );
47 my $pagesize = 30;
48 $tagfield =~ s/\,//g;
49
50 if ($op) {
51 $template->param(script_name => $script_name,
52                                                 tagfield =>$tagfield,
53                                                 authtypecode => $authtypecode,
54                                                 $op              => 1); # we show only the TMPL_VAR names $op
55 } else {
56 $template->param(script_name => $script_name,
57                                                 tagfield =>$tagfield,
58                                                 authtypecode => $authtypecode,
59                                                 else              => 1); # we show only the TMPL_VAR names $op
60 }
61
62 my $dbh = C4::Context->dbh;
63 ################## ADD_FORM ##################################
64 # called by default. Used to create form to add or  modify a record
65 if ($op eq 'add_form') {
66         # builds kohafield tables
67         my @kohafields;
68         push @kohafields, "";
69         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
70         $sth2->execute;
71         while ((my $field) = $sth2->fetchrow_array) {
72                 push @kohafields, "auth_header.".$field;
73         }
74         
75         # build authorised value category list
76         my @authorised_value_categories = Koha::AuthorisedValues->new->categories;
77         unshift @authorised_value_categories, '';
78         push @authorised_value_categories, 'branches';
79         push @authorised_value_categories, 'itemtypes';
80
81         # build thesaurus categories list
82         my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
83
84         # build value_builder list
85         my @value_builder=('');
86
87         # read value_builder directory.
88         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
89         # on a standard install, /cgi-bin need to be added. 
90         # test one, then the other
91     my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
92         unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
93         $cgidir = C4::Context->config('intranetdir');
94                 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
95         } 
96         while (my $line = readdir(DIR)) {
97         if ( $line =~ /\.pl$/ &&
98              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
99             push (@value_builder,$line);
100                 }
101         }
102         @value_builder= sort {$a cmp $b} @value_builder;
103         closedir DIR;
104
105     my @loop_data;
106     my $asses = Koha::Authority::Subfields->search({ tagfield => $tagfield, authtypecode => $authtypecode}, {order_by => 'display_order'})->unblessed;
107     my $i;
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 );
116     }
117
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;
135     $row_data{row} = $i;
136     push( @loop_data, \%row_data );
137
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                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
143                                                         loop => \@loop_data,
144                                                         more_tag => $tagfield);
145
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 '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');
163
164     my $display_order;
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];
174                 my $tab                         =$tab[$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;
182         if ($liblibrarian) {
183             my $ass = Koha::Authority::Subfields->find(
184                 {
185                     authtypecode => $authtypecode,
186                     tagfield     => $tagfield,
187                     tagsubfield  => $tagsubfield
188                 }
189             );
190             my $attributes = {
191                 liblibrarian     => $liblibrarian,
192                 libopac          => $libopac,
193                 repeatable       => $repeatable,
194                 mandatory        => $mandatory,
195                 kohafield        => $kohafield,
196                 tab              => $tab,
197                 seealso          => $seealso,
198                 authorised_value => $authorised_value,
199                 frameworkcode    => $frameworkcode,
200                 value_builder    => $value_builder,
201                 hidden           => $hidden,
202                 isurl            => $isurl,
203                 defaultvalue     => $defaultvalue,
204                 display_order    => $display_order->{$tagfield} || 0,
205             };
206
207             if ($ass) {
208                 $ass->update($attributes);
209             }
210             else {
211                 Koha::Authority::Subfield->new(
212                     {
213                         authtypecode => $authtypecode,
214                         tagfield     => $tagfield,
215                         tagsubfield  => $tagsubfield,
216                         %$attributes
217                     }
218                 )->store;
219             }
220             $display_order->{$tagfield}++;
221         }
222         }
223     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
224     exit;
225
226                                                                                                         # END $OP eq ADD_VALIDATE
227 ################## DELETE_CONFIRM ##################################
228 # called by default form, used to confirm deletion of data in DB
229 }
230 elsif ( $op eq 'delete_confirm' ) {
231   my $ass = Koha::Authority::Subfields->find(
232       {
233           authtypecode => $authtypecode,
234           tagfield     => $tagfield,
235           tagsubfield  => $tagsubfield
236       }
237   );
238   $template->param(
239       ass         => $ass,
240       delete_link => $script_name,
241   );
242 }
243 elsif ( $op eq 'delete_confirmed' ) {
244     Koha::Authority::Subfields->find(
245         {
246             authtypecode => $authtypecode,
247             tagfield     => $tagfield,
248             tagsubfield  => $tagsubfield
249         }
250     )->delete;
251     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
252     exit;
253 }
254 else {    # DEFAULT
255     my $ass = Koha::Authority::Subfields->search(
256         {
257             tagfield      => { -like => "$tagfield%" },
258             authtypecode  => $authtypecode,
259         },
260         { order_by => [ 'tagfield', 'display_order' ] }
261     )->unblessed;
262
263     $template->param( loop => $ass );
264     $template->param(
265         edit_tagfield      => $tagfield,
266         edit_authtypecode  => $authtypecode,
267     );
268
269 } #---- END $OP eq DEFAULT
270 output_html_with_http_headers $input, $cookie, $template->output;