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