Merge remote-tracking branch 'origin/new/bug_8233'
[koha.git] / admin / biblio_framework.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 use strict;
25 use warnings;
26 use CGI;
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30
31 sub StringSearch  {
32         my $dbh = C4::Context->dbh;
33         my $sth=$dbh->prepare("Select * from biblio_framework where (frameworkcode like ?) order by frameworktext");
34         $sth->execute((shift || '') . '%');
35     return $sth->fetchall_arrayref({});
36 }
37
38 my $input = new CGI;
39 my $script_name   = "/cgi-bin/koha/admin/biblio_framework.pl";
40 my $frameworkcode = $input->param('frameworkcode') || '';
41 my $offset        = $input->param('offset') || 0;
42 my $op            = $input->param('op') || '';
43 my $pagesize      = 20;
44
45 my ($template, $borrowernumber, $cookie)
46     = get_template_and_user({template_name => "admin/biblio_framework.tmpl",
47                              query => $input,
48                              type => "intranet",
49                              authnotrequired => 0,
50                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
51                              debug => 1,
52                              });
53
54 $template->param( script_name  => $script_name);
55 $template->param(($op||'else') => 1);
56
57 my $dbh = C4::Context->dbh;
58 ################## ADD_FORM ##################################
59 # called by default. Used to create form to add or  modify a record
60 if ($op eq 'add_form') {
61         #start the page and read in includes
62         #---- if primkey exists, it's a modify action, so read values to modify...
63         my $data;
64         if ($frameworkcode) {
65                 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
66                 $sth->execute($frameworkcode);
67                 $data=$sth->fetchrow_hashref;
68         }
69         $template->param(
70         frameworkcode => $frameworkcode,
71         frameworktext => $data->{'frameworktext'},
72     );
73                                                                                                         # END $OP eq ADD_FORM
74 ################## ADD_VALIDATE ##################################
75 # called by add_form, used to insert/modify data in DB
76 } elsif ($op eq 'add_validate') {
77         my $dbh = C4::Context->dbh;
78         if($input->param('frameworktext') and $input->param('frameworkcode')){
79         if ($input->param('modif')) {
80             my $sth=$dbh->prepare("UPDATE biblio_framework SET frameworktext=? WHERE frameworkcode=?");
81             $sth->execute($input->param('frameworktext'),$input->param('frameworkcode'));
82         } else {
83             my $sth=$dbh->prepare("INSERT into biblio_framework (frameworkcode,frameworktext) values (?,?)");
84             $sth->execute($input->param('frameworkcode'),$input->param('frameworktext'));
85         }
86         }
87         print $input->redirect($script_name);   # FIXME: unnecessary redirect
88         exit;
89                                                                                                         # END $OP eq ADD_VALIDATE
90 ################## DELETE_CONFIRM ##################################
91 # called by default form, used to confirm deletion of data in DB
92 } elsif ($op eq 'delete_confirm') {
93         # Check both categoryitem and biblioitems, see Bug 199
94     my $sth = $dbh->prepare("select count(*) as total from biblio where frameworkcode=?");
95     $sth->execute($frameworkcode);
96     my $total = $sth->fetchrow_hashref->{total};
97
98         $sth = $dbh->prepare("select * from biblio_framework where frameworkcode=?");
99         $sth->execute($frameworkcode);
100         my $data = $sth->fetchrow_hashref;
101
102         $template->param(
103         frameworkcode => $frameworkcode,
104         frameworktext => $data->{'frameworktext'},
105         total => $total
106     );
107                                                                                                         # END $OP eq DELETE_CONFIRM
108 ################## DELETE_CONFIRMED ##################################
109 # called by delete_confirm, used to effectively confirm deletion of data in DB
110 } elsif ($op eq 'delete_confirmed') {
111     if ($frameworkcode) { 
112                 my $sth=$dbh->prepare("delete from marc_tag_structure where frameworkcode=?");
113                 $sth->execute($frameworkcode);
114                 $sth=$dbh->prepare("delete from marc_subfield_structure where frameworkcode=?");
115                 $sth->execute($frameworkcode);
116                 $sth=$dbh->prepare("delete from biblio_framework where frameworkcode=?");
117                 $sth->execute($frameworkcode);
118         }
119         print $input->redirect($script_name);   # FIXME: unnecessary redirect
120         exit;
121                                                                                                         # END $OP eq DELETE_CONFIRMED
122 ################## DEFAULT ##################################
123 } else { # DEFAULT
124         my $results = StringSearch($frameworkcode);
125     my $count = scalar(@$results);
126         my @loop_data;
127         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
128                 push @loop_data, {
129             frameworkcode => $results->[$i]{'frameworkcode'},
130             frameworktext => $results->[$i]{'frameworktext'},
131         };
132         }
133         $template->param(loop => \@loop_data);
134         if ($offset>0) {
135                 my $prevpage = $offset-$pagesize;
136                 $template->param(previous => "$script_name?offset=".$prevpage);
137         }
138         if ($offset+$pagesize<$count) {
139                 my $nextpage =$offset+$pagesize;
140                 $template->param(next => "$script_name?offset=".$nextpage);
141         }
142 } #---- END $OP eq DEFAULT
143
144 output_html_with_http_headers $input, $cookie, $template->output;
145