[14/30] Patron Card Creator card printing interface and code
[koha.git] / patroncards / 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 CGI;
25 use autouse 'Data::Dumper' => qw(Dumper);
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Creators::Lib 1.000000 qw(get_all_profiles get_unit_values);
30 use C4::Patroncards::Template 1.000000;
31 use C4::Patroncards::Profile 1.000000;
32
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "patroncards/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 $card_template = undef;
48 my $profile_list = undef;
49
50 my $units = get_unit_values();
51
52 if ($op eq 'edit') {
53     $card_template = C4::Patroncards::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('card_width'),
63                         label_height    => $cgi->param('card_height'),
64                         top_margin      => $cgi->param('top_margin'),
65                         left_margin     => $cgi->param('left_margin'),
66                         cols            => $cgi->param('cols'),
67                         rows            => $cgi->param('rows'),
68                         col_gap         => $cgi->param('col_gap'),
69                         row_gap         => $cgi->param('row_gap'),
70                         units           => $cgi->param('units'),
71                         );
72     if ($template_id) {   # if a template_id was passed in, this is an update to an existing template
73         $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
74         if ($cgi->param('profile_id') && ($card_template->get_attr('template_id') != $cgi->param('profile_id'))) {
75             if ($card_template->get_attr('profile_id') > 0) {   # no need to get the old one if there was no profile associated
76                 my $old_profile = C4::Patroncards::Profile->retrieve(profile_id => $card_template->get_attr('profile_id'));
77                 $old_profile->set_attr(template_id => 0);
78                 $old_profile->save();
79             }
80             my $new_profile = C4::Patroncards::Profile->retrieve(profile_id => $cgi->param('profile_id'));
81             $new_profile->set_attr(template_id => $card_template->get_attr('template_id'));
82             $new_profile->save();
83         }
84         $card_template->set_attr(@params);
85         $card_template->save();
86     }
87     else {      # if no template_id, this is a new template so insert it
88         $card_template = C4::Patroncards::Template->new(@params);
89         die "Error: $card_template\n" if !ref($card_template);
90         my $template_id = $card_template->save();
91         if ($cgi->param('profile_id')) {
92             my $profile = C4::Patroncards::Profile->retrieve(profile_id => $cgi->param('profile_id'));
93             $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
94             $profile->save();
95         }
96     }
97     print $cgi->redirect("manage.pl?card_element=template");
98     exit;
99 }
100 else {  # if we get here, this is a new layout
101     $card_template = C4::Patroncards::Template->new();
102 }
103 if ($template_id) {
104     foreach my $profile (@$profile_list) {
105         if ($profile->{'profile_id'} == $card_template->get_attr('profile_id')) {
106             $profile->{'selected'} = 1;
107         }
108         else {
109             $profile->{'selected'} = 0;
110         }
111     }
112 }
113
114 foreach my $unit (@$units) {
115     if ($unit->{'type'} eq $card_template->get_attr('units')) {
116         $unit->{'selected'} = 1;
117     }
118 }
119
120 $template->param(
121     profile_list         => $profile_list,
122     template_id          => ($card_template->get_attr('template_id') > 0) ? $card_template->get_attr('template_id') : '',
123     template_code        => $card_template->get_attr('template_code'),
124     template_desc        => $card_template->get_attr('template_desc'),
125     page_width           => $card_template->get_attr('page_width'),
126     page_height          => $card_template->get_attr('page_height'),
127     card_width           => $card_template->get_attr('label_width'),
128     card_height          => $card_template->get_attr('label_height'),
129     top_margin           => $card_template->get_attr('top_margin'),
130     left_margin          => $card_template->get_attr('left_margin'),
131     cols                 => $card_template->get_attr('cols'),
132     rows                 => $card_template->get_attr('rows'),
133     col_gap              => $card_template->get_attr('col_gap'),
134     row_gap              => $card_template->get_attr('row_gap'),
135     units                => $units,
136 );
137
138 output_html_with_http_headers $cgi, $cookie, $template->output;