replace syslog with warns
[koha.git] / labels / label-edit-profile.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 CGI;
25 use HTML::Template::Pro;
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Labels::Lib 1.000000 qw(get_all_templates get_unit_values);
30 use C4::Labels::Profile 1.000000;
31
32 my $cgi = new CGI;
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => "labels/label-edit-profile.tmpl",
36         query           => $cgi,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => { catalogue => 1 },
40         debug           => 1,
41     }
42 );
43
44 my $op = $cgi->param('op');
45 my $profile_id = $cgi->param('profile_id') || $cgi->param('element_id');
46 my $profile = undef;
47 my $template_list = undef;
48 my @label_template = ();
49
50 my $units = get_unit_values();
51
52 if ($op eq 'edit') {
53     $profile = C4::Labels::Profile->retrieve(profile_id => $profile_id);
54     $template_list = get_all_templates(field_list => 'template_id,template_code, profile_id');
55 }
56 elsif ($op eq 'save') {
57     my @params = (
58         printer_name        => $cgi->param('printer_name'),
59         paper_bin           => $cgi->param('paper_bin'),
60         offset_horz         => $cgi->param('offset_horz'),
61         offset_vert         => $cgi->param('offset_vert'),
62         creep_horz          => $cgi->param('creep_horz'),
63         creep_vert          => $cgi->param('creep_vert'),
64         units               => $cgi->param('units'),
65     );
66     if ($profile_id) {   # if a label_id was passed in, this is an update to an existing layout
67         $profile = C4::Labels::Profile->retrieve(profile_id => $profile_id);
68         $profile->set_attr(@params);
69         $profile->save();
70     }
71     else {      # if no label_id, this is a new layout so insert it
72         $profile = C4::Labels::Profile->new(@params);
73         $profile->save();
74     }
75     print $cgi->redirect("label-manage.pl?label_element=profile");
76     exit;
77 }
78 else {  # if we get here, this is a new layout
79     $profile = C4::Labels::Profile->new();
80 }
81
82 if ($profile_id) {
83     @label_template = grep{($_->{'profile_id'} == $profile->get_attr('profile_id')) && ($_->{'template_id'} == $profile->get_attr('template_id'))} @$template_list;
84 }
85
86 foreach my $unit (@$units) {
87     if ($unit->{'type'} eq $profile->get_attr('units')) {
88         $unit->{'selected'} = 1;
89     }
90 }
91
92 $template->param(profile_id => $profile->get_attr('profile_id')) if $profile->get_attr('profile_id') > 0;
93
94 $template->param(
95     label_template      => $label_template[0]->{'template_code'} || '',
96     printer_name        => $profile->get_attr('printer_name'),
97     paper_bin           => $profile->get_attr('paper_bin'),
98     offset_horz         => $profile->get_attr('offset_horz'),
99     offset_vert         => $profile->get_attr('offset_vert'),
100     creep_horz          => $profile->get_attr('creep_horz'),
101     creep_vert          => $profile->get_attr('creep_vert'),
102     units               => $units,
103     op                  => $op,
104 );
105
106 output_html_with_http_headers $cgi, $cookie, $template->output;