Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / admin / biblio_framework.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
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::Context;
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use Koha::Biblios;
27 use Koha::BiblioFramework;
28 use Koha::BiblioFrameworks;
29 use Koha::Caches;
30
31 my $input         = CGI->new;
32 my $frameworkcode = $input->param('frameworkcode') || q||;
33 my $op            = $input->param('op') || q|list|;
34 my $cache         = Koha::Caches->get_instance();
35 my @messages;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {   template_name   => "admin/biblio_framework.tt",
39         query           => $input,
40         type            => "intranet",
41         flagsrequired   => { parameters => 'manage_marc_frameworks' },
42     }
43 );
44
45 my $dbh = C4::Context->dbh;
46 if ( $op eq 'add_form' ) {
47     my $framework;
48     if ($frameworkcode) {
49         $framework = Koha::BiblioFrameworks->find($frameworkcode);
50     }
51     $template->param( framework => $framework );
52 } elsif ( $op eq 'add_validate' ) {
53     my $frameworkcode = $input->param('frameworkcode');
54     my $frameworktext = $input->param('frameworktext');
55     my $is_a_modif    = $input->param('is_a_modif');
56
57     if ($is_a_modif) {
58         my $framework = Koha::BiblioFrameworks->find($frameworkcode);
59         $framework->frameworktext($frameworktext);
60         eval { $framework->store; };
61         if ($@) {
62             push @messages, { type => 'error', code => 'error_on_update' };
63         } else {
64             push @messages, { type => 'message', code => 'success_on_update' };
65         }
66     } else {
67         my $framework = Koha::BiblioFramework->new(
68             {   frameworkcode => $frameworkcode,
69                 frameworktext => $frameworktext,
70             }
71         );
72         eval { $framework->store; };
73         if ($@) {
74             push @messages, { type => 'error', code => 'error_on_insert' };
75         } else {
76             push @messages, { type => 'message', code => 'success_on_insert' };
77         }
78     }
79     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
80     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
81     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
82     $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
83     $op = 'list';
84 } elsif ( $op eq 'delete_confirm' ) {
85     my $framework = Koha::BiblioFrameworks->find($frameworkcode);
86     my $count = Koha::Biblios->search( { frameworkcode => $frameworkcode, } )->count;
87
88     $template->param(
89         framework                  => $framework,
90         biblios_use_this_framework => $count,
91     );
92 } elsif ( $op eq 'delete_confirmed' ) {
93     my $framework = Koha::BiblioFrameworks->find($frameworkcode);
94     my $deleted = eval { $framework->delete; };
95
96     if ( $@ or not $deleted ) {
97         push @messages, { type => 'error', code => 'error_on_delete' };
98     } else {
99         eval {
100             my $dbh = C4::Context->dbh;
101             $dbh->do( q|DELETE FROM marc_tag_structure WHERE frameworkcode=?|,      undef, $frameworkcode );
102             $dbh->do( q|DELETE FROM marc_subfield_structure WHERE frameworkcode=?|, undef, $frameworkcode );
103         };
104         if ($@) {
105             push @messages, { type => 'error', code => 'error_on_delete_fk' };
106         } else {
107             push @messages, { type => 'message', code => 'success_on_delete' };
108         }
109     }
110     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
111     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
112     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
113     $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
114     $op = 'list';
115 }
116
117 if ( $op eq 'list' ) {
118     my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'], } );
119     $template->param( frameworks => $frameworks, );
120 }
121
122 $template->param(
123     messages => \@messages,
124     op       => $op,
125 );
126
127 output_html_with_http_headers $input, $cookie, $template->output;
128