[31/40] Misc bugfixes and cosmetic cleanup.
[koha.git] / labels / label-manage.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 vars qw($debug);
24
25 use Sys::Syslog qw(syslog);
26 use CGI;
27 use HTML::Template::Pro;
28 use Data::Dumper;
29
30 use C4::Auth qw(get_template_and_user);
31 use C4::Output qw(output_html_with_http_headers);
32 use autouse 'C4::Branch' => qw(get_branch_code_from_name);
33 use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_batch_summary html_table);
34 use C4::Labels::Layout 1.000000;
35 use C4::Labels::Template 1.000000;
36 use C4::Labels::Profile 1.000000;
37 use C4::Labels::Batch 1.000000;
38
39 my $cgi = new CGI;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "labels/label-manage.tmpl",
43         query           => $cgi,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { catalogue => 1 },
47         debug           => 1,
48     }
49 );
50
51 my $error = 0;
52 my $db_rows = {};
53 my $display_columns = { layout =>   [  # db column       => {col label                  is link? 
54                                         {layout_id       => {label => 'Layout ID',      link_field      => 0}},
55                                         {layout_name     => {label => 'Layout',         link_field      => 0}},
56                                         {barcode_type    => {label => 'Barcode Type',   link_field      => 0}},
57                                         {printing_type   => {label => 'Print Type',     link_field      => 0}},
58                                         {format_string   => {label => 'Fields to Print',link_field      => 0}},
59                                         {select          => {label => 'Select',         value           => 'layout_id'}},
60                                     ],
61                         template => [   {template_id     => {label => 'Template ID',    link_field      => 0}},
62                                         {template_code   => {label => 'Template Name',  link_field      => 0}},
63                                         {template_desc   => {label => 'Description',    link_field      => 0}},
64                                         {select          => {label => 'Select',         value           => 'template_id'}},
65                                     ],
66                         profile =>  [   {profile_id      => {label => 'Profile ID',     link_field      => 0}},
67                                         {printer_name    => {label => 'Printer Name',   link_field      => 0}},
68                                         {paper_bin       => {label => 'Paper Bin',      link_field      => 0}},
69                                         {_template_code  => {label => 'Template Name',  link_field      => 0}},     # this display column does not have a corrisponding db column in the profile table, hence the underscore
70                                         {select          => {label => 'Select',         value           => 'profile_id'}},
71                                     ],
72                         batch =>    [   {batch_id        => {label => 'Batch ID',       link_field      => 0}},
73                                         {_item_count     => {label => 'Item Count',     link_field      => 0}},
74                                         {select          => {label => 'Select',         value           => 'batch_id'}},
75                                     ],
76 };
77
78 my $label_element = $cgi->param('label_element') || undef;
79 my $op = $cgi->param('op') || 'none';
80 my $element_id = $cgi->param('element_id') || undef;
81
82 my $branch_code = ($label_element eq 'batch' ? get_branch_code_from_name($template->param('LoginBranchname')) : '');
83
84 if ($op eq 'delete') {
85     if          ($label_element eq 'layout')    {$error = C4::Labels::Layout::delete(layout_id => $element_id);}
86     elsif       ($label_element eq 'template')  {$error = C4::Labels::Template::delete(template_id => $element_id);}
87     elsif       ($label_element eq 'profile')   {$error = C4::Labels::Profile::delete(profile_id => $element_id);}
88     elsif       ($label_element eq 'batch')     {$error = C4::Labels::Batch::delete(batch_id => $element_id, branch_code => $branch_code);}
89     else                                        {}      # FIXME: Some error trapping code 
90 }
91
92 if      ($label_element eq 'layout')    {$db_rows = get_all_layouts();}
93 elsif   ($label_element eq 'template')  {$db_rows = get_all_templates();}
94 elsif   ($label_element eq 'profile')   {$db_rows = get_all_profiles();}
95 elsif   ($label_element eq 'batch')     {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\' OR branch_code=\'NB\'");}
96 else                                    {}      # FIXME: Some error trapping code
97
98 my $table = html_table($display_columns->{$label_element}, $db_rows);
99
100 $template->param(error => $error) if ($error ne 0);
101 $template->param(print => 1) if ($label_element eq 'batch');
102 $template->param(
103                 op              => $op,
104                 element_id      => $element_id,
105                 table_loop      => $table,
106                 label_element   => $label_element,
107                 label_element_title     => ($label_element eq 'layout' ? 'Layouts' :
108                                             $label_element eq 'template' ? 'Templates' :
109                                             $label_element eq 'profile' ? 'Profiles' :
110                                             $label_element eq 'batch' ? 'Batches' :
111                                             ''
112                                             ),
113 );
114
115 output_html_with_http_headers $cgi, $cookie, $template->output;