Bug 7143: Follow up patch for Updating about.tt
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use autouse 'Data::Dumper' => qw(Dumper);
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Creators 1.000000;
30 use C4::Patroncards 1.000000;
31
32 my $cgi = new CGI;
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => "patroncards/edit-template.tmpl",
36         query           => $cgi,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => { catalogue => 1 },
40         debug           => 1,
41     }
42 );
43
44 my $op = $cgi->param('op') || '';
45 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
46 my $card_template = undef;
47 my $profile_list = undef;
48
49 my $units = get_unit_values();
50
51 if ($op eq 'edit') {
52     $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
53     $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin', filter => "template_id=$template_id OR template_id=''");
54 }
55 elsif ($op eq 'save') {
56     my @params = (      profile_id      => $cgi->param('profile_id') || '',
57                         template_code   => $cgi->param('template_code'),
58                         template_desc   => $cgi->param('template_desc'),
59                         page_width      => $cgi->param('page_width'),
60                         page_height     => $cgi->param('page_height'),
61                         label_width     => $cgi->param('card_width'),
62                         label_height    => $cgi->param('card_height'),
63                         top_margin      => $cgi->param('top_margin'),
64                         left_margin     => $cgi->param('left_margin'),
65                         cols            => $cgi->param('cols'),
66                         rows            => $cgi->param('rows'),
67                         col_gap         => $cgi->param('col_gap'),
68                         row_gap         => $cgi->param('row_gap'),
69                         units           => $cgi->param('units'),
70                         );
71     if ($template_id) {   # if a template_id was passed in, this is an update to an existing template
72         $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
73         if ($cgi->param('profile_id') && ($card_template->get_attr('template_id') != $cgi->param('profile_id'))) {
74             if ($card_template->get_attr('profile_id') > 0) {   # no need to get the old one if there was no profile associated
75                 my $old_profile = C4::Patroncards::Profile->retrieve(profile_id => $card_template->get_attr('profile_id'));
76                 $old_profile->set_attr(template_id => 0);
77                 $old_profile->save();
78             }
79             my $new_profile = C4::Patroncards::Profile->retrieve(profile_id => $cgi->param('profile_id'));
80             $new_profile->set_attr(template_id => $card_template->get_attr('template_id'));
81             $new_profile->save();
82         }
83         $card_template->set_attr(@params);
84         $card_template->save();
85     }
86     else {      # if no template_id, this is a new template so insert it
87         $card_template = C4::Patroncards::Template->new(@params);
88         die "Error: $card_template\n" if !ref($card_template);
89         my $template_id = $card_template->save();
90         if ($cgi->param('profile_id')) {
91             my $profile = C4::Patroncards::Profile->retrieve(profile_id => $cgi->param('profile_id'));
92             $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
93             $profile->save();
94         }
95     }
96     print $cgi->redirect("manage.pl?card_element=template");
97     exit;
98 }
99 else {  # if we get here, this is a new layout
100     $card_template = C4::Patroncards::Template->new();
101 }
102 if ($template_id) {
103     foreach my $profile (@$profile_list) {
104         if ($profile->{'profile_id'} == $card_template->get_attr('profile_id')) {
105             $profile->{'selected'} = 1;
106         }
107         else {
108             $profile->{'selected'} = 0;
109         }
110     }
111 }
112
113 foreach my $unit (@$units) {
114     if ($unit->{'type'} eq $card_template->get_attr('units')) {
115         $unit->{'selected'} = 1;
116     }
117 }
118
119 $template->param(
120     profile_list         => $profile_list,
121     template_id          => ($card_template->get_attr('template_id') > 0) ? $card_template->get_attr('template_id') : '',
122     template_code        => $card_template->get_attr('template_code'),
123     template_desc        => $card_template->get_attr('template_desc'),
124     page_width           => $card_template->get_attr('page_width'),
125     page_height          => $card_template->get_attr('page_height'),
126     card_width           => $card_template->get_attr('label_width'),
127     card_height          => $card_template->get_attr('label_height'),
128     top_margin           => $card_template->get_attr('top_margin'),
129     left_margin          => $card_template->get_attr('left_margin'),
130     cols                 => $card_template->get_attr('cols'),
131     rows                 => $card_template->get_attr('rows'),
132     col_gap              => $card_template->get_attr('col_gap'),
133     row_gap              => $card_template->get_attr('row_gap'),
134     units                => $units,
135 );
136
137 output_html_with_http_headers $cgi, $cookie, $template->output;