Bug 30477: Add new UNIMARC installer translation files
[koha.git] / patroncards / 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
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Creators::Lib qw( get_all_templates get_unit_values );
28 use C4::Patroncards::Profile;
29
30 my $cgi = CGI->new;
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name   => "patroncards/edit-profile.tt",
34         query           => $cgi,
35         type            => "intranet",
36         flagsrequired   => { tools => 'label_creator' },
37     }
38 );
39
40 my $op = $cgi->param('op') || '';
41 my $profile_id = $cgi->param('profile_id') || $cgi->param('element_id');
42 my $profile = undef;
43 my $template_list = undef;
44 my @label_template = ();
45
46 my $units = get_unit_values();
47
48 if ($op eq 'edit') {
49     $profile = C4::Patroncards::Profile->retrieve(profile_id => $profile_id);
50     $template_list = get_all_templates({ fields => [ qw( template_id template_code profile_id ) ] });
51 }
52 elsif ($op eq 'save') {
53     my @params = (
54         printer_name        => scalar $cgi->param('printer_name'),
55         paper_bin           => scalar $cgi->param('paper_bin'),
56         offset_horz         => scalar $cgi->param('offset_horz'),
57         offset_vert         => scalar $cgi->param('offset_vert'),
58         creep_horz          => scalar $cgi->param('creep_horz'),
59         creep_vert          => scalar $cgi->param('creep_vert'),
60         units               => scalar $cgi->param('units'),
61     );
62     if ($profile_id) {   # if a label_id was passed in, this is an update to an existing layout
63         $profile = C4::Patroncards::Profile->retrieve(profile_id => $profile_id);
64         $profile->set_attr(@params);
65         $profile->save();
66     }
67     else {      # if no label_id, this is a new layout so insert it
68         $profile = C4::Patroncards::Profile->new(@params);
69         $profile->save();
70     }
71     print $cgi->redirect("manage.pl?card_element=profile");
72     exit;
73 }
74 else {  # if we get here, this is a new layout
75     $profile = C4::Patroncards::Profile->new();
76 }
77
78 if ($profile_id) {
79     @label_template = grep{($_->{'profile_id'} == $profile->get_attr('profile_id')) && ($_->{'template_id'} == $profile->get_attr('template_id'))} @$template_list;
80 }
81
82 foreach my $unit (@$units) {
83     if ($unit->{'type'} eq $profile->get_attr('units')) {
84         $unit->{'selected'} = 1;
85     }
86 }
87
88 # if new layout, there will be no profile id, so shouldn't look for it
89 if ( $profile_id && $profile->get_attr('profile_id') > 0 ) {
90     $template->param( profile_id => $profile->get_attr('profile_id') );
91 }
92
93 $template->param(
94     label_template      => $label_template[0]->{'template_code'} || '',
95     printer_name        => $profile->get_attr('printer_name'),
96     paper_bin           => $profile->get_attr('paper_bin'),
97     offset_horz         => $profile->get_attr('offset_horz'),
98     offset_vert         => $profile->get_attr('offset_vert'),
99     creep_horz          => $profile->get_attr('creep_horz'),
100     creep_vert          => $profile->get_attr('creep_vert'),
101     units               => $units,
102     op                  => $op,
103 );
104
105 output_html_with_http_headers $cgi, $cookie, $template->output;