Bug 19382: (QA follow-up) Fix typos
[koha.git] / labels / label-edit-template.pl
blob6beb0b4cc468dcfb8de3d034829e8354323545f9
1 #!/usr/bin/perl
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
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;
30 my $cgi = new CGI;
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33 template_name => "labels/label-edit-template.tt",
34 query => $cgi,
35 type => "intranet",
36 flagsrequired => { catalogue => 1 },
37 debug => 1,
41 my $op = $cgi->param('op');
42 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
43 my $label_template = undef;
44 my $profile_list = undef;
46 my $units = get_unit_values();
48 if ($op eq 'edit') {
49 $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
50 $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [ $template_id, '' ] } } );
51 push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
52 foreach my $profile (@$profile_list) {
53 if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
54 $profile->{'selected'} = 1;
56 else {
57 $profile->{'selected'} = 0;
61 elsif ($op eq 'save') {
62 my @params = ( profile_id => scalar $cgi->param('profile_id'),
63 template_code => scalar $cgi->param('template_code') || 'DEFAULT_TEMPLATE',
64 template_desc => scalar $cgi->param('template_desc') || 'Default description',
65 page_width => scalar $cgi->param('page_width') || 0,
66 page_height => scalar $cgi->param('page_height') || 0,
67 label_width => scalar $cgi->param('label_width') || 0,
68 label_height => scalar $cgi->param('label_height') || 0,
69 top_text_margin => scalar $cgi->param('top_text_margin') || 0,
70 left_text_margin=> scalar $cgi->param('left_text_margin') || 0,
71 top_margin => scalar $cgi->param('top_margin') || 0,
72 left_margin => scalar $cgi->param('left_margin') || 0,
73 cols => scalar $cgi->param('cols') || 0,
74 rows => scalar $cgi->param('rows') || 0,
75 col_gap => scalar $cgi->param('col_gap') || 0,
76 row_gap => scalar $cgi->param('row_gap') || 0,
77 units => scalar $cgi->param('units') || 'POINT',
79 if ($template_id) { # if a template_id was passed in, this is an update to an existing template
80 $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
81 if ($cgi->param('profile_id') && ($label_template->get_attr('template_id') != $cgi->param('profile_id'))) {
82 # Release the old profile if one is currently associated
83 if ($label_template->get_attr('profile_id') > 0) {
84 my $old_profile = C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id'));
85 $old_profile->set_attr(template_id => 0);
86 $old_profile->save();
88 my $new_profile = C4::Labels::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
89 $new_profile->set_attr(template_id => $label_template->get_attr('template_id'));
90 $new_profile->save();
92 elsif ($cgi->param('profile_id') == 0) { # Disassociate any printer profile from the template
93 if ($label_template->get_attr('profile_id') > 0) {
94 my $old_profile = C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id'));
95 $old_profile->set_attr(template_id => 0);
96 $old_profile->save();
100 $label_template->set_attr(@params);
101 $label_template->save();
103 else { # if no template_id, this is a new template so insert it
104 $label_template = C4::Labels::Template->new(@params);
105 my $template_id = $label_template->save();
106 if ($cgi->param('profile_id')) {
107 my $profile = C4::Labels::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
108 $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
109 $profile->save();
112 print $cgi->redirect("label-manage.pl?label_element=template");
113 exit;
115 else { # if we get here, this is a new layout
116 $label_template = C4::Labels::Template->new();
117 $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [''] } });
118 push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
119 foreach my $profile (@$profile_list) {
120 if ($profile->{'profile_id'} == 0) {
121 $profile->{'selected'} = 1;
123 else {
124 $profile->{'selected'} = 0;
129 foreach my $unit (@$units) {
130 if ($unit->{'type'} eq $label_template->get_attr('units')) {
131 $unit->{'selected'} = 1;
135 $template->param(
136 profile_list => $profile_list,
137 template_id => ($label_template->get_attr('template_id') > 0) ? $label_template->get_attr('template_id') : '',
138 template_code => $label_template->get_attr('template_code'),
139 template_desc => $label_template->get_attr('template_desc'),
140 page_width => $label_template->get_attr('page_width'),
141 page_height => $label_template->get_attr('page_height'),
142 label_width => $label_template->get_attr('label_width'),
143 label_height => $label_template->get_attr('label_height'),
144 top_text_margin => $label_template->get_attr('top_text_margin'),
145 left_text_margin => $label_template->get_attr('left_text_margin'),
146 top_margin => $label_template->get_attr('top_margin'),
147 left_margin => $label_template->get_attr('left_margin'),
148 cols => $label_template->get_attr('cols'),
149 rows => $label_template->get_attr('rows'),
150 col_gap => $label_template->get_attr('col_gap'),
151 row_gap => $label_template->get_attr('row_gap'),
152 units => $units,
155 output_html_with_http_headers $cgi, $cookie, $template->output;