3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use autouse 'Data::Dumper' => qw(Dumper);
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Creators qw( get_all_profiles get_unit_values );
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34 template_name => "patroncards/edit-template.tt",
37 flagsrequired => { tools => 'label_creator' },
41 my $op = $cgi->param('op') || '';
42 my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
43 my $card_template = undef;
44 my $profile_list = undef;
46 my $units = get_unit_values();
49 $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
50 $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => {template_id => [ $template_id, '' ]} } );
52 elsif ($op eq 'save') {
53 my @params = ( profile_id => scalar $cgi->param('profile_id') || '',
54 template_code => scalar $cgi->param('template_code'),
55 template_desc => scalar $cgi->param('template_desc'),
56 page_width => scalar $cgi->param('page_width'),
57 page_height => scalar $cgi->param('page_height'),
58 label_width => scalar $cgi->param('card_width'),
59 label_height => scalar $cgi->param('card_height'),
60 top_margin => scalar $cgi->param('top_margin'),
61 left_margin => scalar $cgi->param('left_margin'),
62 cols => scalar $cgi->param('cols'),
63 rows => scalar $cgi->param('rows'),
64 col_gap => scalar $cgi->param('col_gap'),
65 row_gap => scalar $cgi->param('row_gap'),
66 units => scalar $cgi->param('units'),
68 if ($template_id) { # if a template_id was passed in, this is an update to an existing template
69 $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
70 if ($cgi->param('profile_id') && ($card_template->get_attr('template_id') != $cgi->param('profile_id'))) {
71 if ($card_template->get_attr('profile_id') > 0) { # no need to get the old one if there was no profile associated
72 my $old_profile = C4::Patroncards::Profile->retrieve(profile_id => $card_template->get_attr('profile_id'));
73 $old_profile->set_attr(template_id => 0);
76 my $new_profile = C4::Patroncards::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
77 $new_profile->set_attr(template_id => $card_template->get_attr('template_id'));
80 $card_template->set_attr(@params);
81 $card_template->save();
83 else { # if no template_id, this is a new template so insert it
84 $card_template = C4::Patroncards::Template->new(@params);
85 die "Error: $card_template\n" if !ref($card_template);
86 my $template_id = $card_template->save();
87 if ($cgi->param('profile_id')) {
88 my $profile = C4::Patroncards::Profile->retrieve(profile_id => scalar $cgi->param('profile_id'));
89 $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id');
93 print $cgi->redirect("manage.pl?card_element=template");
96 else { # if we get here, this is a new layout
97 $card_template = C4::Patroncards::Template->new();
100 foreach my $profile (@$profile_list) {
101 if ($profile->{'profile_id'} == $card_template->get_attr('profile_id')) {
102 $profile->{'selected'} = 1;
105 $profile->{'selected'} = 0;
110 foreach my $unit (@$units) {
111 if ($unit->{'type'} eq $card_template->get_attr('units')) {
112 $unit->{'selected'} = 1;
117 profile_list => $profile_list,
118 template_id => ($card_template->get_attr('template_id') > 0) ? $card_template->get_attr('template_id') : '',
119 template_code => $card_template->get_attr('template_code'),
120 template_desc => $card_template->get_attr('template_desc'),
121 page_width => $card_template->get_attr('page_width'),
122 page_height => $card_template->get_attr('page_height'),
123 card_width => $card_template->get_attr('label_width'),
124 card_height => $card_template->get_attr('label_height'),
125 top_margin => $card_template->get_attr('top_margin'),
126 left_margin => $card_template->get_attr('left_margin'),
127 cols => $card_template->get_attr('cols'),
128 rows => $card_template->get_attr('rows'),
129 col_gap => $card_template->get_attr('col_gap'),
130 row_gap => $card_template->get_attr('row_gap'),
134 output_html_with_http_headers $cgi, $cookie, $template->output;