Bug 30794: Insert blank rule (unlimited) for max_holds
[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 Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Context;
27
28 use Koha::Authority::Types;
29
30 # retrieve parameters
31 my $input = CGI->new;
32 my $authtypecode         = $input->param('authtypecode')         || '';    # set to select framework
33 my $existingauthtypecode = $input->param('existingauthtypecode') || '';    # set when we have to create a new framework (in authtype) by copying an old one (in existingauthtype)
34
35 my $searchfield = $input->param('searchfield') || 0;
36 my $offset      = $input->param('offset');
37 $offset = 0 if not defined $offset or $offset < 0;
38 my $op          = $input->param('op')     || '';
39 $searchfield =~ s/\,//g;
40
41
42 my $script_name = "/cgi-bin/koha/admin/auth_tag_structure.pl";
43
44 my $dbh = C4::Context->dbh;
45
46 # open template
47 my ($template, $loggedinuser, $cookie)
48     = get_template_and_user({template_name => "admin/auth_tag_structure.tt",
49                  query => $input,
50                  type => "intranet",
51                  flagsrequired => {parameters => 'manage_marc_frameworks'},
52                  });
53
54 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
55
56 my $sth;
57 # check that authtype framework is defined in auth_tag_structure if we are on a default action
58 if (!$op or $op eq 'authtype_create_confirm') {
59     $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
60     $sth->execute($authtypecode);
61     my ($authtypeexist) = $sth->fetchrow;
62     if ($authtypeexist) {
63     } else {
64         # if authtype does not exists, then OP must be changed to "create authtype" if we are not on the way to create it
65         # (op = authtyp_create_confirm)
66         if ($op eq "authtype_create_confirm") {
67             duplicate_auth_framework($authtypecode, $existingauthtypecode);
68         } else {
69             $op = "authtype_create";
70         }
71     }
72 }
73 $template->param(script_name  => $script_name);
74 $template->param(authority_types => $authority_types );
75 if ($op && $op ne 'authtype_create_confirm') {
76     $template->param($op  => 1);
77 } else {
78     $template->param(else => 1);
79 }
80  
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or  modify a record
83 if ($op eq 'add_form') {
84     #---- if primkey exists, it's a modify action, so read values to modify...
85     my $data;
86     if ($searchfield) {
87         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=? and authtypecode=?");
88         $sth->execute($searchfield,$authtypecode);
89         $data=$sth->fetchrow_hashref;
90     }
91
92     if ($searchfield) {
93         $template->param('searchfield' => $searchfield);
94         $template->param('heading_modify_tag_p' => 1);
95     } else {
96         $template->param('heading_add_tag_p' => 1);
97     }
98     $template->param('use_heading_flags_p' => 1);
99     $template->param(liblibrarian => $data->{'liblibrarian'},
100                             libopac => $data->{'libopac'},
101                             repeatable => "".$data->{'repeatable'},
102                             mandatory => "".$data->{'mandatory'},
103                             authorised_value => $data->{authorised_value},
104                             authtypecode => $authtypecode,
105                             );
106                                                     # END $OP eq ADD_FORM
107 ################## ADD_VALIDATE ##################################
108 # called by add_form, used to insert/modify data in DB
109 } elsif ($op eq 'add_validate') {
110     my $tagfield         = $input->param('tagfield');
111     my $liblibrarian     = $input->param('liblibrarian');
112     my $libopac          = $input->param('libopac');
113     my $repeatable       = $input->param('repeatable') ? 1 : 0;
114     my $mandatory        = $input->param('mandatory')  ? 1 : 0;
115     my $authorised_value = $input->param('authorised_value');
116     if ($input->param('modif')) {
117         $sth=$dbh->prepare("UPDATE auth_tag_structure SET tagfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, authorised_value=? WHERE authtypecode=? AND tagfield=?");
118         $sth->execute(
119             $tagfield,
120             $liblibrarian,
121             $libopac,
122             $repeatable,
123             $mandatory,
124             $authorised_value,
125             $authtypecode,
126             $tagfield,
127         );
128     } else {
129         $sth=$dbh->prepare("INSERT INTO auth_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,authtypecode) VALUES (?,?,?,?,?,?,?)");
130         $sth->execute(
131             $tagfield,
132             $liblibrarian,
133             $libopac,
134             $repeatable,
135             $mandatory,
136             $authorised_value,
137             $authtypecode
138        );
139     }
140     print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
141     exit;
142                                                     # END $OP eq ADD_VALIDATE
143 ################## DELETE_CONFIRM ##################################
144 # called by default form, used to confirm deletion of data in DB
145 } elsif ($op eq 'delete_confirm') {
146     $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where tagfield=?");
147     $sth->execute($searchfield);
148     my $data=$sth->fetchrow_hashref;
149     $template->param(liblibrarian => $data->{'liblibrarian'},
150                             searchfield => $searchfield,
151                             authtypecode => $authtypecode,
152                             );
153                                                     # END $OP eq DELETE_CONFIRM
154 ################## DELETE_CONFIRMED ##################################
155 # called by delete_confirm, used to effectively confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirmed') {
157     $dbh->do(q|delete from auth_tag_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
158     $dbh->do(q|delete from auth_subfield_structure where tagfield=? and authtypecode=?|, undef, $searchfield, $authtypecode);
159     my $tagfield = $input->param('tagfield');
160     print $input->redirect("/cgi-bin/koha/admin/auth_tag_structure.pl?searchfield=$tagfield&amp;authtypecode=$authtypecode");
161     exit;
162                                                     # END $OP eq DELETE_CONFIRMED
163 ################## ITEMTYPE_CREATE ##################################
164 # called automatically if an unexisting authtypecode is selected
165 } elsif ($op eq 'authtype_create') {
166     $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");
167     $sth->execute;
168     my @existingauthtypeloop;
169     while (my ($tot,$thisauthtype,$authtypetext) = $sth->fetchrow) {
170         if ($tot>0) {
171             my %line = ( value => $thisauthtype,
172                         authtypetext => $authtypetext,
173                     );
174             push @existingauthtypeloop,\%line;
175         }
176     }
177     @existingauthtypeloop = sort { lc($a->{authtypetext}) cmp lc($b->{authtypetext}) }@existingauthtypeloop;
178     $template->param(existingauthtypeloop => \@existingauthtypeloop,
179                     authtypecode => $authtypecode,
180                     );
181 ################## DEFAULT ##################################
182 } else { # DEFAULT
183     # here, $op can be unset or set to "authtype_create_confirm".
184 #   warn "authtype : $authtypecode";
185     if  ($searchfield ne '') {
186          $template->param(searchfield => $searchfield);
187     }
188     my ($count,$results)=StringSearch($searchfield,$authtypecode);
189     my @loop_data = ();
190     for ( my $i = $offset ; $i < $count ; $i++ ) {
191         my %row_data;  # get a fresh hash for the row data
192         $row_data{tagfield}         = $results->[$i]{'tagfield'};
193         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
194         $row_data{repeatable}       = $results->[$i]{'repeatable'};
195         $row_data{mandatory}        = $results->[$i]{'mandatory'};
196         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
197         push(@loop_data, \%row_data);
198     }
199     $template->param(loop => \@loop_data,
200                     authtypecode => $authtypecode,
201     );
202     if ($offset>0) {
203         $template->param(isprevpage => $offset,
204                         searchfield => $searchfield,
205          );
206     }
207     if ( $offset < $count ) {
208         $template->param(
209                         searchfield => $searchfield,
210         );
211     }
212 } #---- END $OP eq DEFAULT
213
214 output_html_with_http_headers $input, $cookie, $template->output;
215
216 #
217 # the sub used for searches
218 #
219 sub StringSearch  {
220     my ($searchstring,$authtypecode)=@_;
221     my $dbh = C4::Context->dbh;
222     $searchstring=~ s/\'/\\\'/g;
223     my @data=split(' ',$searchstring);
224     my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where (tagfield >= ? and authtypecode=?) order by tagfield");
225     $sth->execute($data[0], $authtypecode);
226     my @results;
227     while (my $data=$sth->fetchrow_hashref){
228         push(@results,$data);
229     }
230     return (scalar(@results),\@results);
231 }
232
233 #
234 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
235 #
236 sub duplicate_auth_framework {
237     my ($newauthtype,$oldauthtype) = @_;
238 #   warn "TO $newauthtype FROM $oldauthtype";
239     my $dbh = C4::Context->dbh;
240     my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from auth_tag_structure where authtypecode=?");
241     $sth->execute($oldauthtype);
242     my $sth_insert = $dbh->prepare("insert into auth_tag_structure  (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, authtypecode) values (?,?,?,?,?,?,?)");
243     while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
244         $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newauthtype);
245     }
246
247     $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,value_builder,seealso,hidden from auth_subfield_structure where authtypecode=?");
248     $sth->execute($oldauthtype);
249     $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 (?,?,?,?,?,?,?,?,?,?,?,?,?)");
250     while ( my ( $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield,$tab, $authorised_value, $thesaurus_category, $seealso,$hidden) = $sth->fetchrow) {
251         $sth_insert->execute($newauthtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory,$kohafield, $tab, $authorised_value, $thesaurus_category, $seealso,$hidden);
252     }
253 }
254