f2eb49a89a8015884d4835c7d68c296882669cd3
[koha.git] / label-create-profile.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5 use C4::Auth;
6 use C4::Context;
7 use C4::Output;
8 use C4::Labels;
9 use HTML::Template::Pro;
10 use POSIX;
11
12 use Data::Dumper;
13 #use Smart::Comments;
14
15 my $dbh   = C4::Context->dbh;
16 my $query = new CGI;
17 ### $query
18
19 my $op          = $query->param('op');
20
21 my $prof_id     = $query->param('prof_id');
22 my $printername = $query->param('printername');
23 my $paper_bin   = $query->param('paper_bin');
24 my $tmpl_id     = $query->param('tmpl_id');
25 my $offset_horz = $query->param('offset_horz');
26 my $offset_vert = $query->param('offset_vert');
27 my $creep_horz  = $query->param('creep_horz');
28 my $creep_vert  = $query->param('creep_vert');
29 my $units       = $query->param('unit');
30
31 my @resultsloop;
32 my @tmpllist;
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35         {
36             template_name   => "labels/label-create-profile.tmpl",
37             query           => $query,
38             type            => "intranet",
39             authnotrequired => 1,
40             flagsrequired   => { catalogue => 1 },
41             debug           => 1,
42         }
43     );
44
45 if ( $op eq 'blank' || $op eq '' ) {
46
47     my @units = (
48         { unit => 'INCH',  desc => 'Inches' },
49         { unit => 'CM',    desc => 'Centimeters' },
50         { unit => 'MM',    desc => 'Millimeters' },
51         { unit => 'POINT', desc => 'Postscript Points' },
52     );
53
54     my @tmpl = GetAllLabelTemplates();
55
56     foreach my $data (@tmpl) {
57         push ( @tmpllist, {tmpl_id      => $data->{'tmpl_id'},
58                            tmpl_code    => $data->{'tmpl_code'}} );
59     }
60
61     $template->param(
62         tmpllist        => \@tmpllist,
63         unit            => \@units,
64     );
65
66 }
67
68 elsif ( $op eq 'Save' ) {
69     my $errmsg;
70     my $dberror = CreateProfile(
71         $prof_id,       $printername,   $paper_bin,     $tmpl_id,     $offset_horz,
72         $offset_vert,   $creep_horz,    $creep_vert,    $units
73     );
74     unless ( $dberror ) {
75         print $query->redirect("./label-profiles.pl");
76         exit;
77     }
78     
79     # FIXME: This exposes all CGI vars. Surely there is a better way to do it? -fbcit
80     if ( $dberror =~ /Duplicate/ && $dberror =~ /$paper_bin/ ) {
81         $errmsg = "You cannot create duplicate profiles for $printername/$paper_bin.
82                     Click the Back button on your browser and enter a different paper bin
83                     for $printername.";
84     }
85
86     else {
87         $errmsg = $dberror;
88     }
89
90     $template->param (
91         dberror         => $dberror,
92         errmsg          => $errmsg,
93     );
94
95     warn "DB error: $dberror";
96 }
97
98 elsif ( $op eq 'Cancel' ) {
99     print $query->redirect("./label-profiles.pl");
100     exit;
101 }
102
103 output_html_with_http_headers $query, $cookie, $template->output;