Bug 27070: Add cross_fields type to our searches
[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
30 use List::MoreUtils qw( uniq );
31
32 sub string_search  {
33         my ($searchstring,$authtypecode)=@_;
34         my $dbh = C4::Context->dbh;
35         $searchstring=~ s/\'/\\\'/g;
36         my @data=split(' ',$searchstring);
37         my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
38         $sth->execute("$searchstring%",$authtypecode);
39         my $results = $sth->fetchall_arrayref({});
40         return (scalar(@$results), $results);
41 }
42
43 sub auth_subfield_structure_exists {
44         my ($authtypecode, $tagfield, $tagsubfield) = @_;
45         my $dbh  = C4::Context->dbh;
46         my $sql  = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
47         my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
48         return @$rows > 0;
49 }
50
51 my $input        = CGI->new;
52 my $tagfield     = $input->param('tagfield');
53 my $tagsubfield  = $input->param('tagsubfield');
54 my $authtypecode = $input->param('authtypecode');
55 my $offset       = $input->param('offset');
56 $offset = 0 if not defined $offset or $offset < 0;
57 my $op           = $input->param('op') || '';
58 my $script_name  = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
59
60 my ($template, $borrowernumber, $cookie) = get_template_and_user(
61     {   template_name   => "admin/auth_subfields_structure.tt",
62         query           => $input,
63         type            => "intranet",
64         flagsrequired   => { parameters => 'manage_marc_frameworks' },
65         debug           => 1,
66     }
67 );
68 my $pagesize = 30;
69 $tagfield =~ s/\,//g;
70
71 if ($op) {
72 $template->param(script_name => $script_name,
73                                                 tagfield =>$tagfield,
74                                                 authtypecode => $authtypecode,
75                                                 $op              => 1); # we show only the TMPL_VAR names $op
76 } else {
77 $template->param(script_name => $script_name,
78                                                 tagfield =>$tagfield,
79                                                 authtypecode => $authtypecode,
80                                                 else              => 1); # we show only the TMPL_VAR names $op
81 }
82
83 my $dbh = C4::Context->dbh;
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or  modify a record
86 if ($op eq 'add_form') {
87         # builds kohafield tables
88         my @kohafields;
89         push @kohafields, "";
90         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
91         $sth2->execute;
92         while ((my $field) = $sth2->fetchrow_array) {
93                 push @kohafields, "auth_header.".$field;
94         }
95         
96         # build authorised value category list
97         my @authorised_value_categories = Koha::AuthorisedValues->new->categories;
98         unshift @authorised_value_categories, '';
99         push @authorised_value_categories, 'branches';
100         push @authorised_value_categories, 'itemtypes';
101
102         # build thesaurus categories list
103         my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
104
105         # build value_builder list
106         my @value_builder=('');
107
108         # read value_builder directory.
109         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
110         # on a standard install, /cgi-bin need to be added. 
111         # test one, then the other
112     my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
113         unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
114         $cgidir = C4::Context->config('intranetdir');
115                 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
116         } 
117         while (my $line = readdir(DIR)) {
118         if ( $line =~ /\.pl$/ &&
119              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
120             push (@value_builder,$line);
121                 }
122         }
123         @value_builder= sort {$a cmp $b} @value_builder;
124         closedir DIR;
125
126         # build values list
127         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
128         $sth->execute($tagfield,$authtypecode);
129         my @loop_data = ();
130         my $i=0;
131     while ( my $data = $sth->fetchrow_hashref ) {
132         my %row_data;    # get a fresh hash for the row data
133         $row_data{defaultvalue}      = $data->{defaultvalue};
134         $row_data{tab}               = $data->{tab};
135         $row_data{ohidden}           = $data->{'hidden'};
136         $row_data{tagsubfield}       = $data->{'tagsubfield'};
137         $row_data{liblibrarian}      = $data->{'liblibrarian'};
138         $row_data{libopac}           = $data->{'libopac'};
139         $row_data{seealso}           = $data->{'seealso'};
140         $row_data{kohafields}        = \@kohafields;
141         $row_data{kohafield}         = $data->{'kohafield'};
142         $row_data{authorised_values} = \@authorised_value_categories;
143         $row_data{authorised_value}  = $data->{'authorised_value'};
144         $row_data{frameworkcodes}    = \@authtypes;
145         $row_data{frameworkcode}     = $data->{'frameworkcode'};
146         $row_data{value_builders}    = \@value_builder;
147         $row_data{value_builder}     = $data->{'value_builder'};
148         $row_data{repeatable}        = $data->{repeatable};
149         $row_data{mandatory}         = $data->{mandatory};
150         $row_data{hidden}            = $data->{hidden};
151         $row_data{isurl}             = $data->{isurl};
152         $row_data{row}               = $i;
153         push( @loop_data, \%row_data );
154         $i++;
155     }
156
157     # Add a new row for the "New" tab
158     my %row_data;    # get a fresh hash for the row data
159     $row_data{'new_subfield'} = 1;
160     $row_data{tab} = -1; # ignore
161     $row_data{ohidden} = 0; # show all
162     $row_data{tagsubfield}      = "";
163     $row_data{liblibrarian}     = "";
164     $row_data{libopac}          = "";
165     $row_data{seealso}          = "";
166     $row_data{hidden}           = "000";
167     $row_data{repeatable}       = 0;
168     $row_data{mandatory}        = 0;
169     $row_data{isurl}            = 0;
170     $row_data{kohafields} = \@kohafields,
171     $row_data{authorised_values} = \@authorised_value_categories;
172     $row_data{frameworkcodes} = \@authtypes;
173     $row_data{value_builders} = \@value_builder;
174     $row_data{row} = $i;
175     push( @loop_data, \%row_data );
176
177         $template->param('use_heading_flags_p' => 1);
178         $template->param('heading_edit_subfields_p' => 1);
179         $template->param(action => "Edit subfields",
180                                                         tagfield => $tagfield,
181                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
182                                                         loop => \@loop_data,
183                                                         more_tag => $tagfield);
184
185                                                                                                 # END $OP eq ADD_FORM
186 ################## ADD_VALIDATE ##################################
187 # called by add_form, used to insert/modify data in DB
188 } elsif ($op eq 'add_validate') {
189         $template->param(tagfield => "$input->param('tagfield')");
190 #       my $sth=$dbh->prepare("replace auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl)
191 #                                                                       values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
192     my $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl,defaultvalue)
193                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
194     my $sth_update = $dbh->prepare("update auth_subfield_structure set authtypecode=?, tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, frameworkcode=?, value_builder=?, hidden=?, isurl=?, defaultvalue=?
195                                     where authtypecode=? and tagfield=? and tagsubfield=?");
196         my @tagsubfield = $input->multi_param('tagsubfield');
197         my @liblibrarian        = $input->multi_param('liblibrarian');
198         my @libopac             = $input->multi_param('libopac');
199         my @kohafield           = ''.$input->param('kohafield');
200         my @tab                         = $input->multi_param('tab');
201         my @seealso             = $input->multi_param('seealso');
202     my @ohidden             = $input->multi_param('ohidden');
203     my @authorised_value_categories = $input->multi_param('authorised_value');
204         my $authtypecode        = $input->param('authtypecode');
205         my @frameworkcodes      = $input->multi_param('frameworkcode');
206         my @value_builder       =$input->multi_param('value_builder');
207     my @defaultvalue = $input->multi_param('defaultvalue');
208         for (my $i=0; $i<= $#tagsubfield ; $i++) {
209                 my $tagfield                    =$input->param('tagfield');
210                 my $tagsubfield         =$tagsubfield[$i];
211                 $tagsubfield="@" unless $tagsubfield ne '';
212                 my $liblibrarian                =$liblibrarian[$i];
213                 my $libopac                     =$libopac[$i];
214                 my $repeatable          =$input->param("repeatable$i")?1:0;
215                 my $mandatory           =$input->param("mandatory$i")?1:0;
216                 my $kohafield           =$kohafield[$i];
217                 my $tab                         =$tab[$i];
218                 my $seealso                             =$seealso[$i];
219         my $authorised_value = $authorised_value_categories[$i];
220                 my $frameworkcode               =$frameworkcodes[$i];
221                 my $value_builder=$value_builder[$i];
222         my $defaultvalue = $defaultvalue[$i];
223                 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
224                 my $isurl = $input->param("isurl$i")?1:0;
225                 if ($liblibrarian) {
226                                 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
227                                         $sth_update->execute(
228                                                 $authtypecode,
229                                                 $tagfield,
230                                                 $tagsubfield,
231                                                 $liblibrarian,
232                                                 $libopac,
233                                                 $repeatable,
234                                                 $mandatory,
235                                                 $kohafield,
236                                                 $tab,
237                                                 $seealso,
238                                                 $authorised_value,
239                                                 $frameworkcode,
240                                                 $value_builder,
241                                                 $hidden,
242                                                 $isurl,
243                         $defaultvalue,
244                                                 (
245                                                         $authtypecode,
246                                                         $tagfield,
247                                                         $tagsubfield
248                                                 ),
249                                         );
250                                 } else {
251                                         $sth_insert->execute(
252                                                 $authtypecode,
253                                                 $tagfield,
254                                                 $tagsubfield,
255                                                 $liblibrarian,
256                                                 $libopac,
257                                                 $repeatable,
258                                                 $mandatory,
259                                                 $kohafield,
260                                                 $tab,
261                                                 $seealso,
262                                                 $authorised_value,
263                                                 $frameworkcode,
264                                                 $value_builder,
265                                                 $hidden,
266                                                 $isurl,
267                         $defaultvalue,
268                                         );
269                                 }
270                 }
271         }
272     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
273     exit;
274
275                                                                                                         # END $OP eq ADD_VALIDATE
276 ################## DELETE_CONFIRM ##################################
277 # called by default form, used to confirm deletion of data in DB
278 } elsif ($op eq 'delete_confirm') {
279         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
280         $sth->execute($tagfield,$tagsubfield,$authtypecode);
281         my $data=$sth->fetchrow_hashref;
282         $template->param(liblibrarian => $data->{'liblibrarian'},
283                                                         tagsubfield => $data->{'tagsubfield'},
284                                                         delete_link => $script_name,
285                                                         tagfield      =>$tagfield,
286                                                         tagsubfield => $tagsubfield,
287                                                         authtypecode => $authtypecode,
288                                                         );
289                                                                                                         # END $OP eq DELETE_CONFIRM
290 ################## DELETE_CONFIRMED ##################################
291 # called by delete_confirm, used to effectively confirm deletion of data in DB
292 } elsif ($op eq 'delete_confirmed') {
293     my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
294     $sth->execute($tagfield,$tagsubfield,$authtypecode);
295     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
296     exit;
297                                                                                                         # END $OP eq DELETE_CONFIRMED
298 ################## DEFAULT ##################################
299 } else { # DEFAULT
300         my ($count,$results)=string_search($tagfield,$authtypecode);
301         my @loop_data = ();
302         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
303                 my %row_data;  # get a fresh hash for the row data
304                 $row_data{tagfield} = $results->[$i]{'tagfield'};
305                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
306                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
307                 $row_data{kohafield} = $results->[$i]{'kohafield'};
308                 $row_data{repeatable} = $results->[$i]{'repeatable'};
309                 $row_data{mandatory} = $results->[$i]{'mandatory'};
310                 $row_data{tab} = $results->[$i]{'tab'};
311                 $row_data{seealso} = $results->[$i]{'seealso'};
312                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
313                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
314                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
315                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
316                 $row_data{isurl}        = $results->[$i]{'isurl'};
317                 if ($row_data{tab} eq -1) {
318                         $row_data{subfield_ignored} = 1;
319                 }
320
321                 push(@loop_data, \%row_data);
322         }
323         $template->param(loop => \@loop_data);
324         $template->param(edit_tagfield => $tagfield,
325                 edit_authtypecode => $authtypecode);
326         
327         if ($offset>0) {
328                 my $prevpage = $offset-$pagesize;
329                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
330         }
331         if ($offset+$pagesize<$count) {
332                 my $nextpage =$offset+$pagesize;
333                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
334         }
335 } #---- END $OP eq DEFAULT
336 output_html_with_http_headers $input, $cookie, $template->output;