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