Fix FSF address in directory labels/
[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 1.000000;
29 use C4::Labels 1.000000;
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 warn "operation = $op\n";
45 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
46 my $label_template = undef;
47 my $profile_list = undef;
48
49 my $units = get_unit_values();
50
51 if ($op eq 'edit') {
52     $label_template = C4::Labels::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('label_width'),
62                         label_height    => $cgi->param('label_height'),
63                         top_text_margin => $cgi->param('top_text_margin'),
64                         left_text_margin=> $cgi->param('left_text_margin'),
65                         top_margin      => $cgi->param('top_margin'),
66                         left_margin     => $cgi->param('left_margin'),
67                         cols            => $cgi->param('cols'),
68                         rows            => $cgi->param('rows'),
69                         col_gap         => $cgi->param('col_gap'),
70                         row_gap         => $cgi->param('row_gap'),
71                         units           => $cgi->param('units'),
72                         );
73     if ($template_id) {   # if a label_id was passed in, this is an update to an existing layout
74         $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
75         if ($cgi->param('profile_id') && ($label_template->get_attr('template_id') != $cgi->param('profile_id'))) {
76             if ($label_template->get_attr('profile_id') > 0) {   # no need to get the old one if there was no profile associated
77                 my $old_profile = C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id'));
78                 $old_profile->set_attr(template_id => 0);
79                 $old_profile->save();
80             }
81             my $new_profile = C4::Labels::Profile->retrieve(profile_id => $cgi->param('profile_id'));
82             $new_profile->set_attr(template_id => $label_template->get_attr('template_id'));
83             $new_profile->save();
84         }
85
86 #        if ($cgi->param('profile_id')) {
87 #            my $old_profile = ($label_template->get_attr('profile_id') ? C4::Labels::Profile->retrieve(profile_id => $label_template->get_attr('profile_id')) : undef);
88 #            my $new_profile = C4::Labels::Profile->retrieve(profile_id => $cgi->param('profile_id'));
89 #            if ($label_template->get_attr('template_id') != $new_profile->get_attr('template_id')) {
90 #                $new_profile->set_attr(template_id => $label_template->get_attr('template_id'));
91 #                $new_profile->save();
92 #                if ($old_profile) {
93 #                    $old_profile->set_attr(template_id => 0);
94 #                    $old_profile->save();
95 #                }
96 #            }
97 #        }
98         $label_template->set_attr(@params);
99         $label_template->save();
100     }
101     else {      # if no label_id, this is a new layout so insert it
102         $label_template = C4::Labels::Template->new(@params);
103         my $template_id = $label_template->save();
104         if ($cgi->param('profile_id')) {
105             my $profile = C4::Labels::Profile->retrieve(profile_id => $cgi->param('profile_id'));
106             $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
107             $profile->save();
108         }
109     }
110     print $cgi->redirect("label-manage.pl?label_element=template");
111     exit;
112 }
113 else {  # if we get here, this is a new layout
114     $label_template = C4::Labels::Template->new();
115 }
116 if ($template_id) {
117     foreach my $profile (@$profile_list) {
118         if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
119             $profile->{'selected'} = 1;
120         }
121         else {
122             $profile->{'selected'} = 0;
123         }
124     }
125 }
126
127 foreach my $unit (@$units) {
128     if ($unit->{'type'} eq $label_template->get_attr('units')) {
129         $unit->{'selected'} = 1;
130     }
131 }
132
133 $template->param(
134     profile_list         => $profile_list,
135     template_id          => ($label_template->get_attr('template_id') > 0) ? $label_template->get_attr('template_id') : '',
136     template_code        => $label_template->get_attr('template_code'),
137     template_desc        => $label_template->get_attr('template_desc'),
138     page_width           => $label_template->get_attr('page_width'),
139     page_height          => $label_template->get_attr('page_height'),
140     label_width          => $label_template->get_attr('label_width'),
141     label_height         => $label_template->get_attr('label_height'),
142     top_text_margin      => $label_template->get_attr('top_text_margin'),
143     left_text_margin     => $label_template->get_attr('left_text_margin'),
144     top_margin           => $label_template->get_attr('top_margin'),
145     left_margin          => $label_template->get_attr('left_margin'),
146     cols                 => $label_template->get_attr('cols'),
147     rows                 => $label_template->get_attr('rows'),
148     col_gap              => $label_template->get_attr('col_gap'),
149     row_gap              => $label_template->get_attr('row_gap'),
150     units                => $units,
151 );
152
153 output_html_with_http_headers $cgi, $cookie, $template->output;