[36/40] Bugfix for profile update on profile/template association change.
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23
24 use Sys::Syslog qw(syslog);
25 use CGI;
26 use HTML::Template::Pro;
27
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Labels::Lib 1.000000 qw(get_all_profiles get_unit_values);
31 use C4::Labels::Template 1.000000;
32
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "labels/label-edit-template.tmpl",
37         query           => $cgi,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { catalogue => 1 },
41         debug           => 1,
42     }
43 );
44
45 my $op = $cgi->param('op');
46 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
47 my $label_template = undef;
48 my $profile_list = undef;
49
50 my $units = get_unit_values();
51
52 if ($op eq 'edit') {
53     $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
54     $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin',filter => "template_id=$template_id OR template_id=''");
55 }
56 elsif ($op eq 'save') {
57     my @params = (      profile_id      => $cgi->param('profile_id') || '',
58                         template_code   => $cgi->param('template_code'),
59                         template_desc   => $cgi->param('template_desc'),
60                         page_width      => $cgi->param('page_width'),
61                         page_height     => $cgi->param('page_height'),
62                         label_width     => $cgi->param('label_width'),
63                         label_height    => $cgi->param('label_height'),
64                         top_text_margin => $cgi->param('top_text_margin'),
65                         left_text_margin=> $cgi->param('left_text_margin'),
66                         top_margin      => $cgi->param('top_margin'),
67                         left_margin     => $cgi->param('left_margin'),
68                         cols            => $cgi->param('cols'),
69                         rows            => $cgi->param('rows'),
70                         col_gap         => $cgi->param('col_gap'),
71                         row_gap         => $cgi->param('row_gap'),
72                         units           => $cgi->param('units'),
73                         );
74     if ($template_id) {   # if a label_id was passed in, this is an update to an existing layout
75         $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
76         my $old_profile = C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id'));
77         my $new_profile = C4::Labels::Profile->retrieve(profile_id => $cgi->param('profile_id'));
78         if ($label_template->get_attr('template_id') != $new_profile->get_attr('template_id')) {
79             $new_profile->set_attr(template_id => $label_template->get_attr('template_id'));
80             $old_profile->set_attr(template_id => 0);
81             $new_profile->save();
82             $old_profile->save();
83         }
84         $label_template->set_attr(@params);
85         $label_template->save();
86     }
87     else {      # if no label_id, this is a new layout so insert it
88         $label_template = C4::Labels::Template->new(@params);
89         my $template_id = $label_template->save();
90         my $profile = C4::Labels::Profile->retrieve(profile_id => $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     print $cgi->redirect("label-manage.pl?label_element=template");
95     exit;
96 }
97 else {  # if we get here, this is a new layout
98     $label_template = C4::Labels::Template->new();
99 }
100 if ($template_id) {
101     foreach my $profile (@$profile_list) {
102         if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
103             $profile->{'selected'} = 1;
104         }
105         else {
106             $profile->{'selected'} = 0;
107         }
108     }
109 }
110
111 foreach my $unit (@$units) {
112     if ($unit->{'type'} eq $label_template->get_attr('units')) {
113         $unit->{'selected'} = 1;
114     }
115 }
116
117 $template->param(
118     profile_list         => $profile_list,
119     template_id          => ($label_template->get_attr('template_id') > 0) ? $label_template->get_attr('template_id') : '',
120     template_code        => $label_template->get_attr('template_code'),
121     template_desc        => $label_template->get_attr('template_desc'),
122     page_width           => $label_template->get_attr('page_width'),
123     page_height          => $label_template->get_attr('page_height'),
124     label_width          => $label_template->get_attr('label_width'),
125     label_height         => $label_template->get_attr('label_height'),
126     top_text_margin      => $label_template->get_attr('top_text_margin'),
127     left_text_margin     => $label_template->get_attr('left_text_margin'),
128     top_margin           => $label_template->get_attr('top_margin'),
129     left_margin          => $label_template->get_attr('left_margin'),
130     cols                 => $label_template->get_attr('cols'),
131     rows                 => $label_template->get_attr('rows'),
132     col_gap              => $label_template->get_attr('col_gap'),
133     row_gap              => $label_template->get_attr('row_gap'),
134     units                => $units,
135 );
136
137 output_html_with_http_headers $cgi, $cookie, $template->output;