Bug 22770: DBRev 19.05.00.003
[koha.git] / labels / label-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
25 use C4::Auth qw(get_template_and_user);
26 use C4::Output qw(output_html_with_http_headers);
27 use C4::Creators;
28 use C4::Labels;
29
30 my $cgi = new CGI;
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name   => "labels/label-edit-template.tt",
34         query           => $cgi,
35         type            => "intranet",
36         authnotrequired => 0,
37         flagsrequired   => { catalogue => 1 },
38         debug           => 1,
39     }
40 );
41
42 my $op = $cgi->param('op');
43 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
44 my $label_template = undef;
45 my $profile_list = undef;
46
47 my $units = get_unit_values();
48
49 if ($op eq 'edit') {
50     $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
51     $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [ $template_id, '' ] } } );
52     push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
53     foreach my $profile (@$profile_list) {
54         if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
55             $profile->{'selected'} = 1;
56         }
57         else {
58             $profile->{'selected'} = 0;
59         }
60     }
61 }
62 elsif ($op eq 'save') {
63     my @params = (      profile_id      => scalar $cgi->param('profile_id'),
64                         template_code   => scalar $cgi->param('template_code') || 'DEFAULT_TEMPLATE',
65                         template_desc   => scalar $cgi->param('template_desc') || 'Default description',
66                         page_width      => scalar $cgi->param('page_width') || 0,
67                         page_height     => scalar $cgi->param('page_height') || 0,
68                         label_width     => scalar $cgi->param('label_width') || 0,
69                         label_height    => scalar $cgi->param('label_height') || 0,
70                         top_text_margin => scalar $cgi->param('top_text_margin') || 0,
71                         left_text_margin=> scalar $cgi->param('left_text_margin') || 0,
72                         top_margin      => scalar $cgi->param('top_margin') || 0,
73                         left_margin     => scalar $cgi->param('left_margin') || 0,
74                         cols            => scalar $cgi->param('cols') || 0,
75                         rows            => scalar $cgi->param('rows') || 0,
76                         col_gap         => scalar $cgi->param('col_gap') || 0,
77                         row_gap         => scalar $cgi->param('row_gap') || 0,
78                         units           => scalar $cgi->param('units') || 'POINT',
79                         );
80     if ($template_id) {   # if a template_id was passed in, this is an update to an existing template
81         $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
82         if ($cgi->param('profile_id') && ($label_template->get_attr('template_id') != $cgi->param('profile_id'))) {
83             # Release the old profile if one is currently associated
84             if ($label_template->get_attr('profile_id') > 0) {
85                 my $old_profile = C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id'));
86                 $old_profile->set_attr(template_id => 0);
87                 $old_profile->save();
88             }
89             my $new_profile = C4::Labels::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
90             $new_profile->set_attr(template_id => $label_template->get_attr('template_id'));
91             $new_profile->save();
92         }
93         elsif ($cgi->param('profile_id') == 0) { # Disassociate any printer profile from the template
94             if ($label_template->get_attr('profile_id') > 0) {
95                 my $old_profile = C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id'));
96                 $old_profile->set_attr(template_id => 0);
97                 $old_profile->save();
98             }
99         }
100
101         $label_template->set_attr(@params);
102         $label_template->save();
103     }
104     else {      # if no template_id, this is a new template so insert it
105         $label_template = C4::Labels::Template->new(@params);
106         my $template_id = $label_template->save();
107         if ($cgi->param('profile_id')) {
108             my $profile = C4::Labels::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
109             $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
110             $profile->save();
111         }
112     }
113     print $cgi->redirect("label-manage.pl?label_element=template");
114     exit;
115 }
116 else {  # if we get here, this is a new layout
117     $label_template = C4::Labels::Template->new();
118     $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [''] } });
119     push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
120     foreach my $profile (@$profile_list) {
121         if ($profile->{'profile_id'} == 0) {
122             $profile->{'selected'} = 1;
123         }
124         else {
125             $profile->{'selected'} = 0;
126         }
127     }
128 }
129
130 foreach my $unit (@$units) {
131     if ($unit->{'type'} eq $label_template->get_attr('units')) {
132         $unit->{'selected'} = 1;
133     }
134 }
135
136 $template->param(
137     profile_list         => $profile_list,
138     template_id          => ($label_template->get_attr('template_id') > 0) ? $label_template->get_attr('template_id') : '',
139     template_code        => $label_template->get_attr('template_code'),
140     template_desc        => $label_template->get_attr('template_desc'),
141     page_width           => $label_template->get_attr('page_width'),
142     page_height          => $label_template->get_attr('page_height'),
143     label_width          => $label_template->get_attr('label_width'),
144     label_height         => $label_template->get_attr('label_height'),
145     top_text_margin      => $label_template->get_attr('top_text_margin'),
146     left_text_margin     => $label_template->get_attr('left_text_margin'),
147     top_margin           => $label_template->get_attr('top_margin'),
148     left_margin          => $label_template->get_attr('left_margin'),
149     cols                 => $label_template->get_attr('cols'),
150     rows                 => $label_template->get_attr('rows'),
151     col_gap              => $label_template->get_attr('col_gap'),
152     row_gap              => $label_template->get_attr('row_gap'),
153     units                => $units,
154 );
155
156 output_html_with_http_headers $cgi, $cookie, $template->output;