Bug 23846: Add a check to the data inconsistencies script
[koha.git] / patroncards / edit-template.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
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
23 use CGI qw ( -utf8 );
24 use autouse 'Data::Dumper' => qw(Dumper);
25
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators;
29 use C4::Patroncards;
30
31 my $cgi = new CGI;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "patroncards/edit-template.tt",
35         query           => $cgi,
36         type            => "intranet",
37         authnotrequired => 0,
38         flagsrequired   => { catalogue => 1 },
39         debug           => 1,
40     }
41 );
42
43 my $op = $cgi->param('op') || '';
44 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
45 my $card_template = undef;
46 my $profile_list = undef;
47
48 my $units = get_unit_values();
49
50 if ($op eq 'edit') {
51     $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
52     $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => {template_id => [ $template_id, '' ]} } );
53 }
54 elsif ($op eq 'save') {
55     my @params = (      profile_id      => scalar $cgi->param('profile_id') || '',
56                         template_code   => scalar $cgi->param('template_code'),
57                         template_desc   => scalar $cgi->param('template_desc'),
58                         page_width      => scalar $cgi->param('page_width'),
59                         page_height     => scalar $cgi->param('page_height'),
60                         label_width     => scalar $cgi->param('card_width'),
61                         label_height    => scalar $cgi->param('card_height'),
62                         top_margin      => scalar $cgi->param('top_margin'),
63                         left_margin     => scalar $cgi->param('left_margin'),
64                         cols            => scalar $cgi->param('cols'),
65                         rows            => scalar $cgi->param('rows'),
66                         col_gap         => scalar $cgi->param('col_gap'),
67                         row_gap         => scalar $cgi->param('row_gap'),
68                         units           => scalar $cgi->param('units'),
69                         );
70     if ($template_id) {   # if a template_id was passed in, this is an update to an existing template
71         $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
72         if ($cgi->param('profile_id') && ($card_template->get_attr('template_id') != $cgi->param('profile_id'))) {
73             if ($card_template->get_attr('profile_id') > 0) {   # no need to get the old one if there was no profile associated
74                 my $old_profile = C4::Patroncards::Profile->retrieve(profile_id => $card_template->get_attr('profile_id'));
75                 $old_profile->set_attr(template_id => 0);
76                 $old_profile->save();
77             }
78             my $new_profile = C4::Patroncards::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
79             $new_profile->set_attr(template_id => $card_template->get_attr('template_id'));
80             $new_profile->save();
81         }
82         $card_template->set_attr(@params);
83         $card_template->save();
84     }
85     else {      # if no template_id, this is a new template so insert it
86         $card_template = C4::Patroncards::Template->new(@params);
87         die "Error: $card_template\n" if !ref($card_template);
88         my $template_id = $card_template->save();
89         if ($cgi->param('profile_id')) {
90             my $profile = C4::Patroncards::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
91             $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
92             $profile->save();
93         }
94     }
95     print $cgi->redirect("manage.pl?card_element=template");
96     exit;
97 }
98 else {  # if we get here, this is a new layout
99     $card_template = C4::Patroncards::Template->new();
100 }
101 if ($template_id) {
102     foreach my $profile (@$profile_list) {
103         if ($profile->{'profile_id'} == $card_template->get_attr('profile_id')) {
104             $profile->{'selected'} = 1;
105         }
106         else {
107             $profile->{'selected'} = 0;
108         }
109     }
110 }
111
112 foreach my $unit (@$units) {
113     if ($unit->{'type'} eq $card_template->get_attr('units')) {
114         $unit->{'selected'} = 1;
115     }
116 }
117
118 $template->param(
119     profile_list         => $profile_list,
120     template_id          => ($card_template->get_attr('template_id') > 0) ? $card_template->get_attr('template_id') : '',
121     template_code        => $card_template->get_attr('template_code'),
122     template_desc        => $card_template->get_attr('template_desc'),
123     page_width           => $card_template->get_attr('page_width'),
124     page_height          => $card_template->get_attr('page_height'),
125     card_width           => $card_template->get_attr('label_width'),
126     card_height          => $card_template->get_attr('label_height'),
127     top_margin           => $card_template->get_attr('top_margin'),
128     left_margin          => $card_template->get_attr('left_margin'),
129     cols                 => $card_template->get_attr('cols'),
130     rows                 => $card_template->get_attr('rows'),
131     col_gap              => $card_template->get_attr('col_gap'),
132     row_gap              => $card_template->get_attr('row_gap'),
133     units                => $units,
134 );
135
136 output_html_with_http_headers $cgi, $cookie, $template->output;