Bug 11842 - fix framework caching with memcache/plack
[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 use Koha::Cache;
31
32 sub StringSearch  {
33         my $dbh = C4::Context->dbh;
34         my $sth=$dbh->prepare("Select * from biblio_framework where (frameworkcode like ?) order by frameworktext");
35         $sth->execute((shift || '') . '%');
36     return $sth->fetchall_arrayref({});
37 }
38
39 my $input = new CGI;
40 my $script_name   = "/cgi-bin/koha/admin/biblio_framework.pl";
41 my $frameworkcode = $input->param('frameworkcode') || '';
42 my $offset        = $input->param('offset') || 0;
43 my $op            = $input->param('op') || '';
44 my $pagesize      = 20;
45 my $cache         = Koha::Cache->get_instance();
46
47 my ($template, $borrowernumber, $cookie)
48     = get_template_and_user({template_name => "admin/biblio_framework.tt",
49                              query => $input,
50                              type => "intranet",
51                              authnotrequired => 0,
52                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
53                              debug => 1,
54                              });
55
56 $template->param( script_name  => $script_name);
57 $template->param(($op||'else') => 1);
58
59 my $dbh = C4::Context->dbh;
60 ################## ADD_FORM ##################################
61 # called by default. Used to create form to add or  modify a record
62 if ($op eq 'add_form') {
63         #start the page and read in includes
64         #---- if primkey exists, it's a modify action, so read values to modify...
65         my $data;
66         if ($frameworkcode) {
67                 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
68                 $sth->execute($frameworkcode);
69                 $data=$sth->fetchrow_hashref;
70         }
71         $template->param(
72         frameworkcode => $frameworkcode,
73         frameworktext => $data->{'frameworktext'},
74     );
75                                                                                                         # END $OP eq ADD_FORM
76 ################## ADD_VALIDATE ##################################
77 # called by add_form, used to insert/modify data in DB
78 } elsif ($op eq 'add_validate') {
79     my $dbh = C4::Context->dbh;
80     if ( $input->param('frameworktext') and $frameworkcode ) {
81         if ($input->param('modif')) {
82             my $sth=$dbh->prepare("UPDATE biblio_framework SET frameworktext=? WHERE frameworkcode=?");
83             $sth->execute( $input->param('frameworktext'), $frameworkcode );
84         } else {
85             my $sth=$dbh->prepare("INSERT into biblio_framework (frameworkcode,frameworktext) values (?,?)");
86             $sth->execute( $frameworkcode, $input->param('frameworktext') );
87         }
88         $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
89         $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
90         }
91         print $input->redirect($script_name);   # FIXME: unnecessary redirect
92         exit;
93                                                                                                         # END $OP eq ADD_VALIDATE
94 ################## DELETE_CONFIRM ##################################
95 # called by default form, used to confirm deletion of data in DB
96 } elsif ($op eq 'delete_confirm') {
97         # Check both categoryitem and biblioitems, see Bug 199
98     my $sth = $dbh->prepare("select count(*) as total from biblio where frameworkcode=?");
99     $sth->execute($frameworkcode);
100     my $total = $sth->fetchrow_hashref->{total};
101
102         $sth = $dbh->prepare("select * from biblio_framework where frameworkcode=?");
103         $sth->execute($frameworkcode);
104         my $data = $sth->fetchrow_hashref;
105
106         $template->param(
107         frameworkcode => $frameworkcode,
108         frameworktext => $data->{'frameworktext'},
109         total => $total
110     );
111                                                                                                         # END $OP eq DELETE_CONFIRM
112 ################## DELETE_CONFIRMED ##################################
113 # called by delete_confirm, used to effectively confirm deletion of data in DB
114 } elsif ($op eq 'delete_confirmed') {
115     if ($frameworkcode) { 
116                 my $sth=$dbh->prepare("delete from marc_tag_structure where frameworkcode=?");
117                 $sth->execute($frameworkcode);
118                 $sth=$dbh->prepare("delete from marc_subfield_structure where frameworkcode=?");
119                 $sth->execute($frameworkcode);
120                 $sth=$dbh->prepare("delete from biblio_framework where frameworkcode=?");
121                 $sth->execute($frameworkcode);
122         $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
123         $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
124         }
125         print $input->redirect($script_name);   # FIXME: unnecessary redirect
126         exit;
127                                                                                                         # END $OP eq DELETE_CONFIRMED
128 ################## DEFAULT ##################################
129 } else { # DEFAULT
130         my $results = StringSearch($frameworkcode);
131     my $count = scalar(@$results);
132         my @loop_data;
133         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
134                 push @loop_data, {
135             frameworkcode => $results->[$i]{'frameworkcode'},
136             frameworktext => $results->[$i]{'frameworktext'},
137         };
138         }
139         $template->param(loop => \@loop_data);
140         if ($offset>0) {
141                 my $prevpage = $offset-$pagesize;
142                 $template->param(previous => "$script_name?offset=".$prevpage);
143         }
144         if ($offset+$pagesize<$count) {
145                 my $nextpage =$offset+$pagesize;
146                 $template->param(next => "$script_name?offset=".$nextpage);
147         }
148 } #---- END $OP eq DEFAULT
149
150 output_html_with_http_headers $input, $cookie, $template->output;
151