Bug 17314: OpenAPI spec
[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
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 use autouse 'Data::Dumper' => qw(Dumper);
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Creators qw(
29     get_all_layouts
30     get_all_profiles
31     get_all_templates
32     get_batch_summary
33     html_table
34 );
35 use C4::Labels;
36 use Koha::List::Patron qw( GetPatronLists );
37
38 my $cgi = CGI->new;
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "patroncards/manage.tt",
42         query           => $cgi,
43         type            => "intranet",
44         flagsrequired   => { tools => 'label_creator' },
45     }
46 );
47
48 my $op = $cgi->param('op') || 'none';
49 my $card_element = $cgi->param('card_element') || 'template';   # default to template management
50 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
51
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                                         {_action         => {label => 'Action',         link_field      => 0}},
57                                         #{layout_xml      => {label => 'Layout XML',     link_field      => 0}},
58                                         {select          => {label => 'Select',         value           => 'layout_id'}},
59                                     ],
60                         template => [   {template_id     => {label => 'Template ID',    link_field      => 0}},
61                                         {template_code   => {label => 'Template Name',  link_field      => 0}},
62                                         {template_desc   => {label => 'Description',    link_field      => 0}},
63                                         {_action         => {label => 'Action',         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 corresponding db column in the profile table, hence the underscore
70                                         {_action         => {label => 'Action',         link_field      => 0}},
71                                         {select          => {label => 'Select',         value           => 'profile_id'}},
72                                     ],
73                         batch =>    [   {batch_id        => {label => 'Batch ID',       link_field      => 0}},
74                                         {description     => {label => 'Description',    link_field      => 0}},
75                                         {_item_count     => {label => 'Patron Count',   link_field      => 0}},
76                                         {_action         => {label => 'Actions',        link_field      => 0}},
77                                         {select          => {label => 'Select',         value           => 'batch_id'}},
78                                     ],
79 };
80
81 my $errstr = ($cgi->param('error') ? $cgi->param('error') : '');
82 my $branch_code = ($card_element eq 'batch' ? C4::Context->userenv->{'branch'} : '');
83
84 if ($op eq 'delete') {
85     my $err = 0;
86     my @element_ids = split(/,/, $element_id);
87     foreach my $element_id (@element_ids) {
88         if          ($card_element eq 'layout')    {$err = C4::Patroncards::Layout::delete(layout_id => $element_id);}
89         elsif       ($card_element eq 'template')  {$err = C4::Patroncards::Template::delete(template_id => $element_id);}
90         elsif       ($card_element eq 'profile')   {$err = C4::Patroncards::Profile::delete(profile_id => $element_id);}
91         elsif       ($card_element eq 'batch')     {$err = C4::Labels::Batch::delete(batch_id => $element_id, branch_code => $branch_code);}
92         else                                       {warn sprintf("Unknown card element passed in for delete operation: %s.",$card_element); $errstr = 202;}
93     }
94     print $cgi->redirect("manage.pl?card_element=$card_element" . ($err ? "&error=102" : ''));
95     exit;
96 }
97 elsif ($op eq 'none') {
98     if      ($card_element eq 'layout')    {$db_rows = get_all_layouts( { filters => { creator => 'Patroncards' } });}
99     elsif   ($card_element eq 'template')  {$db_rows = get_all_templates( { filters => { creator => 'Patroncards' } });}
100     elsif   ($card_element eq 'profile')   {$db_rows = get_all_profiles( { filters => { creator => 'Patroncards' } });}
101     elsif   ($card_element eq 'batch')     {$db_rows = get_batch_summary( { filters => { branch_code => [ $branch_code, 'NB' ], creator => 'Patroncards' } });}
102     else                                   {warn sprintf("Unknown card element passed in: %s.",$card_element); $errstr = 202;}
103 }
104 else { # trap unsupported operations here
105     warn sprintf('Manage interface called an unsupported operation: %s',$op);
106     print $cgi->redirect("manage.pl?card_element=$card_element&error=201");
107     exit;
108 }
109
110 my $table = html_table($display_columns->{$card_element}, $db_rows);
111
112 $template->param(print => 1) if ($card_element eq 'batch');
113 $template->param( patron_lists => [ GetPatronLists() ] ) if ($card_element eq 'batch');
114
115 $template->param(
116                 error           => $errstr,
117 );
118 $template->param(
119                 op              => $op,
120                 element_id      => $element_id,
121                 table_loop      => $table,
122                 card_element    => $card_element,
123                 card_element_title     => ($card_element eq 'layout' ? 'Layouts' :
124                                             $card_element eq 'template' ? 'Templates' :
125                                             $card_element eq 'profile' ? 'Profiles' :
126                                             $card_element eq 'batch' ? 'Batches' :
127                                             ''
128                                             ),
129 );
130
131 output_html_with_http_headers $cgi, $cookie, $template->output;