Bug 18201: Export data -Fix "Remove non-local items" option and add "Removes non...
[koha.git] / admin / auth_tag_structure.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Context;
27 use C4::Output;
28 use C4::Context;
29
30 use Koha::Authority::Types;
31
32 # retrieve parameters
33 my $input = new CGI;
34 my $authtypecode         = $input->param('authtypecode')         || '';    # set to select framework
35 my $existingauthtypecode = $input->param('existingauthtypecode') || '';    # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
36
37 my $searchfield = $input->param('searchfield') || 0;
38 my $offset      = $input->param('offset');
39 $offset = 0 if not defined $offset or $offset < 0;
40 my $op          = $input->param('op')     || '';
41 $searchfield =~ s/\,//g;
42
43
44 my $script_name = "/cgi-bin/koha/admin/auth_tag_structure.pl";
45
46 my $dbh = C4::Context->dbh;
47
48 # open template
49 my ($template, $loggedinuser, $cookie)
50     = get_template_and_user({template_name => "admin/auth_tag_structure.tt",
51                  query => $input,
52                  type => "intranet",
53                  authnotrequired => 0,
54                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
55                  debug => 1,
56                  });
57
58 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
59
60 my $sth;
61 # check that authtype framework is defined in auth_tag_structure if we are on a default action
62 if (!$op or $op eq 'authtype_create_confirm') {
63     $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
64     $sth->execute($authtypecode);
65     my ($authtypeexist) = $sth->fetchrow;
66     if ($authtypeexist) {
67     } else {
68         # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
69         # (op = authtyp_create_confirm)
70         if ($op eq "authtype_create_confirm") {
71             duplicate_auth_framework($authtypecode, $existingauthtypecode);
72         } else {
73             $op = "authtype_create";
74         }
75     }
76 }
77 $template->param(script_name  => $script_name);
78 $template->param(authority_types => $authority_types );
79 if ($op && $op ne 'authtype_create_confirm') {
80     $template->param($op  => 1);
81 } else {
82     $template->param(else => 1);
83 }
84  
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or  modify a record
87 if ($op eq 'add_form') {
88     #---- if primkey exists, it's a modify action, so read values to modify...
89     my $data;
90     if ($searchfield) {
91         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
92         $sth->execute($searchfield,$authtypecode);
93         $data=$sth->fetchrow_hashref;
94     }
95
96     if ($searchfield) {
97         $template->param('searchfield' => $searchfield);
98         $template->param('heading_modify_tag_p' => 1);
99     } else {
100         $template->param('heading_add_tag_p' => 1);
101     }
102     $template->param('use_heading_flags_p' => 1);
103     $template->param(liblibrarian => $data->{'liblibrarian'},
104                             libopac => $data->{'libopac'},
105                             repeatable => "".$data->{'repeatable'},
106                             mandatory => "".$data->{'mandatory'},
107                             authorised_value => $data->{authorised_value},
108                             authtypecode => $authtypecode,
109                             );
110                                                     # END $OP eq ADD_FORM
111 ################## ADD_VALIDATE ##################################
112 # called by add_form, used to insert/modify data in DB
113 } elsif ($op eq 'add_validate') {
114     my $tagfield         = $input->param('tagfield');
115     my $liblibrarian     = $input->param('liblibrarian');
116     my $libopac          = $input->param('libopac');
117     my $repeatable       = $input->param('repeatable') ? 1 : 0;
118     my $mandatory        = $input->param('mandatory')  ? 1 : 0;
119     my $authorised_value = $input->param('authorised_value');
120     if ($input->param('modif')) {
121         $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
122         $sth->execute(
123             $tagfield,
124             $liblibrarian,
125             $libopac,
126             $repeatable,
127             $mandatory,
128             $authorised_value,
129             $authtypecode,
130             $tagfield,
131         );
132     } else {
133         $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
134         $sth->execute(
135             $tagfield,
136             $liblibrarian,
137             $libopac,
138             $repeatable,
139             $mandatory,
140             $authorised_value,
141             $authtypecode
142        );
143     }
144     print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
145     exit;
146                                                     # END $OP eq ADD_VALIDATE
147 ################## DELETE_CONFIRM ##################################
148 # called by default form, used to confirm deletion of data in DB
149 } elsif ($op eq 'delete_confirm') {
150     $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
151     $sth->execute($searchfield);
152     my $data=$sth->fetchrow_hashref;
153     $template->param(liblibrarian => $data->{'liblibrarian'},
154                             searchfield => $searchfield,
155                             authtypecode => $authtypecode,
156                             );
157                                                     # END $OP eq DELETE_CONFIRM
158 ################## DELETE_CONFIRMED ##################################
159 # called by delete_confirm, used to effectively confirm deletion of data in DB
160 } elsif ($op eq 'delete_confirmed') {
161     $dbh->do(q|delete from auth_tag_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
162     $dbh->do(q|delete from auth_subfield_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
163     my $tagfield = $input->param('tagfield');
164     print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
165     exit;
166                                                     # END $OP eq DELETE_CONFIRMED
167 ################## ITEMTYPE_CREATE ##################################
168 # called automatically if an unexisting authtypecode is selected
169 } elsif ($op eq 'authtype_create') {
170     $sth = $dbh->prepare("select count(*),auth_tag_structure.authtypecode,authtypetext from auth_tag_structure,auth_types where auth_types.authtypecode=auth_tag_structure.authtypecode group by auth_tag_structure.authtypecode");
171     $sth->execute;
172     my @existingauthtypeloop;
173     while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
174         if ($tot>0) {
175             my %line = ( value => $thisauthtype,
176                         authtypetext => $authtypetext,
177                     );
178             push @existingauthtypeloop,\%line;
179         }
180     }
181     @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
182     $template->param(existingauthtypeloop => \@existingauthtypeloop,
183                     authtypecode => $authtypecode,
184                     );
185 ################## DEFAULT ##################################
186 } else { # DEFAULT
187     # here, $op can be unset or set to "authtype_create_confirm".
188 #   warn "authtype : $authtypecode";
189     if  ($searchfield ne '') {
190          $template->param(searchfield => $searchfield);
191     }
192     my ($count,$results)=StringSearch($searchfield,$authtypecode);
193     my @loop_data = ();
194     for ( my $i = $offset ; $i < $count ; $i++ ) {
195         my %row_data;  # get a fresh hash for the row data
196         $row_data{tagfield}         = $results->[$i]{'tagfield'};
197         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
198         $row_data{repeatable}       = $results->[$i]{'repeatable'};
199         $row_data{mandatory}        = $results->[$i]{'mandatory'};
200         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
201         $row_data{subfield_link}    = "auth_subfields_structure.pl?tagfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
202         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
203         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=" . $results->[$i]{'tagfield'} . "&amp;authtypecode=" . $authtypecode;
204         push(@loop_data, \%row_data);
205     }
206     $template->param(loop => \@loop_data,
207                     authtypecode => $authtypecode,
208     );
209     if ($offset>0) {
210         $template->param(isprevpage => $offset,
211                         searchfield => $searchfield,
212          );
213     }
214     if ( $offset < $count ) {
215         $template->param(
216                         searchfield => $searchfield,
217         );
218     }
219 } #---- END $OP eq DEFAULT
220
221 output_html_with_http_headers $input, $cookie, $template->output;
222
223 #
224 # the sub used for searches
225 #
226 sub StringSearch  {
227     my ($searchstring,$authtypecode)=@_;
228     my $dbh = C4::Context->dbh;
229     $searchstring=~ s/\'/\\\'/g;
230     my @data=split(' ',$searchstring);
231     my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
232     $sth->execute($data[0], $authtypecode);
233     my @results;
234     while (my $data=$sth->fetchrow_hashref){
235         push(@results,$data);
236     }
237     return (scalar(@results),\@results);
238 }
239
240 #
241 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
242 #
243 sub duplicate_auth_framework {
244     my ($newauthtype,$oldauthtype) = @_;
245 #   warn "TO $newauthtype FROM $oldauthtype";
246     my $dbh = C4::Context->dbh;
247     my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
248     $sth->execute($oldauthtype);
249     my $sth_insert = $dbh->prepare("insert into auth_tag_structure  (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
250     while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
251         $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
252     }
253
254     $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
255     $sth->execute($oldauthtype);
256     $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
257     while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
258         $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);
259     }
260 }
261