Bugfix: Enabling guide box configuration parameter
[koha.git] / patroncards / 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 CGI;
26 use autouse 'Data::Dumper' => qw(Dumper);
27
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use autouse 'C4::Branch' => qw(get_branch_code_from_name);
31 #use C4::Creators::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_batch_summary html_table);
32 #use C4::Patroncards::Layout 1.000000;
33 #use C4::Patroncards::Template 1.000000;
34 #use C4::Patroncards::Profile 1.000000;
35 #use C4::Labels::Batch 1.000000;
36 use C4::Creators 1.000000;
37 use C4::Patroncards 1.000000;
38 use C4::Labels 1.000000;
39
40 my $cgi = new CGI;
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "patroncards/manage.tmpl",
44         query           => $cgi,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { catalogue => 1 },
48         debug           => 1,
49     }
50 );
51
52 my $op = $cgi->param('op') || 'none';
53 my $card_element = $cgi->param('card_element') || 'template';   # default to template managment
54 my $element_id = $cgi->param('element_id') || 0; # there should never be an element with a id of 0 so this is a safe default
55
56 my $db_rows = {};
57 my $display_columns = { layout =>   [  # db column       => {col label                  is link?
58                                         {layout_id       => {label => 'Layout ID',      link_field      => 0}},
59                                         {layout_name     => {label => 'Layout',         link_field      => 0}},
60                                         #{layout_xml      => {label => 'Layout XML',     link_field      => 0}},
61                                         {select          => {label => 'Select',         value           => 'layout_id'}},
62                                     ],
63                         template => [   {template_id     => {label => 'Template ID',    link_field      => 0}},
64                                         {template_code   => {label => 'Template Name',  link_field      => 0}},
65                                         {template_desc   => {label => 'Description',    link_field      => 0}},
66                                         {select          => {label => 'Select',         value           => 'template_id'}},
67                                     ],
68                         profile =>  [   {profile_id      => {label => 'Profile ID',     link_field      => 0}},
69                                         {printer_name    => {label => 'Printer Name',   link_field      => 0}},
70                                         {paper_bin       => {label => 'Paper Bin',      link_field      => 0}},
71                                         {_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
72                                         {select          => {label => 'Select',         value           => 'profile_id'}},
73                                     ],
74                         batch =>    [   {batch_id        => {label => 'Batch ID',       link_field      => 0}},
75                                         {_item_count     => {label => 'Item Count',     link_field      => 0}},
76                                         {select          => {label => 'Select',         value           => 'batch_id'}},
77                                     ],
78 };
79
80 my $errstr = ($cgi->param('error') ? $cgi->param('error') : '');
81 my $branch_code = ($card_element eq 'batch' ? get_branch_code_from_name($template->param('LoginBranchname')) : '');
82
83 if ($op eq 'delete') {
84     my $err = 0;
85     if          ($card_element eq 'layout')    {$err = C4::Patroncards::Layout::delete(layout_id => $element_id);}
86     elsif       ($card_element eq 'template')  {$err = C4::Patroncards::Template::delete(template_id => $element_id);}
87     elsif       ($card_element eq 'profile')   {$err = C4::Patroncards::Profile::delete(profile_id => $element_id);}
88     elsif       ($card_element eq 'batch')     {$err = C4::Labels::Batch::delete(batch_id => $element_id, branch_code => $branch_code);}
89     else                                       {warn sprintf("Unknown card element passed in for delete operation: %s.",$card_element); $errstr = 202;}
90     print $cgi->redirect("manage.pl?card_element=$card_element" . ($err ? "&error=102" : ''));
91     exit;
92 }
93 elsif ($op eq 'none') {
94     if      ($card_element eq 'layout')    {$db_rows = get_all_layouts(table_name => 'creator_layouts', filter => 'creator=\'Patroncards\'');}
95     elsif   ($card_element eq 'template')  {$db_rows = get_all_templates(table_name => 'creator_templates', filter => 'creator=\'Patroncards\'');}
96     elsif   ($card_element eq 'profile')   {$db_rows = get_all_profiles(table_name => 'printers_profile', filter => 'creator=\'Patroncards\'');}
97     elsif   ($card_element eq 'batch')     {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\' OR branch_code=\'NB\'", creator => 'Patroncards');}
98     else                                   {warn sprintf("Unknown card element passed in: %s.",$card_element); $errstr = 202;}
99 }
100 else { # trap unsupported operations here
101     warn sprintf('Manage interface called an unsupported operation: %s',$op);
102     print $cgi->redirect("manage.pl?card_element=$card_element&error=201");
103     exit;
104 }
105
106 my $table = html_table($display_columns->{$card_element}, $db_rows);
107
108 $template->param(print => 1) if ($card_element eq 'batch');
109 $template->param(
110                 error           => ($errstr ? 1 : 0),
111                 $errstr         => 1,
112 );
113 $template->param(
114                 op              => $op,
115                 element_id      => $element_id,
116                 table_loop      => $table,
117                 card_element    => $card_element,
118                 card_element_title     => ($card_element eq 'layout' ? 'Layouts' :
119                                             $card_element eq 'template' ? 'Templates' :
120                                             $card_element eq 'profile' ? 'Profiles' :
121                                             $card_element eq 'batch' ? 'Batches' :
122                                             ''
123                                             ),
124 );
125
126 output_html_with_http_headers $cgi, $cookie, $template->output;