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